33
44const { callFetch : requestPromise } = require ( './fetch' )
55const { uniq } = require ( 'lodash' )
6- const Cache = require ( '../providers/caching/memory' )
6+ const createCache = require ( '../providers/caching/memory' )
77
8+ /**
9+ * @typedef {import('./condaRepoAccess').CondaChannels } CondaChannels
10+ *
11+ * @typedef {import('./condaRepoAccess').CondaChannelData } CondaChannelData
12+ *
13+ * @typedef {import('./condaRepoAccess').CondaRepoData } CondaRepoData
14+ *
15+ * @typedef {import('./condaRepoAccess').CondaPackageMatch } CondaPackageMatch
16+ *
17+ * @typedef {import('../providers/caching').ICache } ICache
18+ */
19+
20+ /**
21+ * Configuration mapping of Conda channel names to their base URLs
22+ *
23+ * @type {CondaChannels }
24+ */
825const condaChannels = {
926 'anaconda-main' : 'https://repo.anaconda.com/pkgs/main' ,
1027 'anaconda-r' : 'https://repo.anaconda.com/pkgs/r' ,
1128 'conda-forge' : 'https://conda.anaconda.org/conda-forge'
1229}
1330
31+ /**
32+ * Main class for accessing Conda repository data. Provides methods to fetch channel data, repository data, and search
33+ * for packages.
34+ */
1435class CondaRepoAccess {
36+ /**
37+ * Creates a new CondaRepoAccess instance
38+ *
39+ * @param {ICache } [cache] - Cache instance to use for storing data. Defaults to memory cache with 8 hour TTL if not
40+ * provided.
41+ */
1542 constructor ( cache ) {
16- this . cache = cache || Cache ( { defaultTtlSeconds : 8 * 60 * 60 } ) // 8 hours
43+ this . cache = cache || createCache ( { defaultTtlSeconds : 8 * 60 * 60 } ) // 8 hours
1744 }
1845
46+ /**
47+ * Validates if a channel is recognized and supported
48+ *
49+ * @param {string } channel - Channel name to validate
50+ * @throws {Error } When channel is not recognized
51+ */
1952 checkIfValidChannel ( channel ) {
2053 if ( ! condaChannels [ channel ] ) {
2154 throw new Error ( `Unrecognized Conda channel ${ channel } ` )
2255 }
2356 }
2457
58+ /**
59+ * Fetches channel data from cache or network. Channel data contains information about all packages available in a
60+ * channel.
61+ *
62+ * @param {string } channel - Channel name
63+ * @returns {Promise<CondaChannelData> } Promise resolving to channel data
64+ * @throws {Error } When channel is invalid or fetch fails
65+ */
2566 async fetchChannelData ( channel ) {
2667 const key = `${ channel } -channelData`
2768 let channelData = this . cache . get ( key )
@@ -33,6 +74,15 @@ class CondaRepoAccess {
3374 return channelData
3475 }
3576
77+ /**
78+ * Fetches repository data for a specific channel and subdirectory. Repository data contains detailed package
79+ * information for a specific platform.
80+ *
81+ * @param {string } channel - Channel name
82+ * @param {string } subdir - Subdirectory name (platform like 'linux-64', 'win-64')
83+ * @returns {Promise<CondaRepoData> } Promise resolving to repository data
84+ * @throws {Error } When channel is invalid or fetch fails
85+ */
3686 async fetchRepoData ( channel , subdir ) {
3787 const key = `${ channel } -${ subdir } -repoData`
3888 let repoData = this . cache . get ( key )
@@ -44,6 +94,16 @@ class CondaRepoAccess {
4494 return repoData
4595 }
4696
97+ /**
98+ * Gets all available revisions for a package across specified subdirectories. Each revision represents a specific
99+ * build of a package version.
100+ *
101+ * @param {string } channel - Channel name
102+ * @param {string } subdir - Subdirectory name or '-' to search all available subdirs
103+ * @param {string } name - Package name
104+ * @returns {Promise<string[]> } Promise resolving to array of revision strings in format "subdir:version-build"
105+ * @throws {Error } When package not found or subdir doesn't exist
106+ */
47107 async getRevisions ( channel , subdir , name ) {
48108 channel = encodeURIComponent ( channel )
49109 name = encodeURIComponent ( name )
@@ -56,6 +116,7 @@ class CondaRepoAccess {
56116 if ( subdir !== '-' && ! channelData . subdirs . find ( x => x === subdir ) ) {
57117 throw new Error ( `Subdir ${ subdir } is non-existent in channel ${ channel } , subdirs: ${ channelData . subdirs } ` )
58118 }
119+ /** @type {string[] } */
59120 let revisions = [ ]
60121 const subdirs = subdir === '-' ? channelData . packages [ name ] . subdirs : [ subdir ]
61122 for ( let subdir of subdirs ) {
@@ -72,6 +133,15 @@ class CondaRepoAccess {
72133 return uniq ( revisions )
73134 }
74135
136+ /**
137+ * Searches for packages by name pattern in the specified channel. Returns packages whose names contain the search
138+ * term.
139+ *
140+ * @param {string } channel - Channel name
141+ * @param {string } name - Package name pattern to search for
142+ * @returns {Promise<CondaPackageMatch[]> } Promise resolving to array of matching packages
143+ * @throws {Error } When channel is invalid or fetch fails
144+ */
75145 async getPackages ( channel , name ) {
76146 channel = encodeURIComponent ( channel )
77147 name = encodeURIComponent ( name )
@@ -84,4 +154,10 @@ class CondaRepoAccess {
84154 }
85155}
86156
157+ /**
158+ * Factory function that creates a new CondaRepoAccess instance
159+ *
160+ * @param {ICache } [cache] - Optional cache instance
161+ * @returns {CondaRepoAccess } New CondaRepoAccess instance
162+ */
87163module . exports = cache => new CondaRepoAccess ( cache )
0 commit comments