11import os from 'os' ;
22import path from 'path' ;
33import semver from 'semver' ;
4- import { getVersion , DownloadInfo , VersionListOpts , clearCache } from './version-list' ;
4+ import type { DownloadInfo , VersionListOpts } from './version-list' ;
5+ import { getVersion , clearCache } from './version-list' ;
56import { getCurrentLinuxDistro } from './linux-distro' ;
67import { inspect } from 'util' ;
78import _debug from 'debug' ;
89const debug = _debug ( 'mongodb-download-url' ) ;
910
10- type PriorityValue < T > = { value : T ; priority : number ; }
11+ type PriorityValue < T > = { value : T ; priority : number } ;
1112
1213type ArtifactOptions = {
1314 /**
@@ -51,7 +52,7 @@ type ArtifactOptions = {
5152 /**
5253 * @deprecated Use arch instead.
5354 */
54- bits ?: '32' | '64' | 32 | 64
55+ bits ?: '32' | '64' | 32 | 64 ;
5556} ;
5657
5758export type Options = ArtifactOptions & VersionListOpts ;
@@ -121,7 +122,12 @@ function parseArch(arch: string): string[] {
121122 return [ arch ] ;
122123}
123124
124- async function parseTarget ( distro : string | undefined , platform : string , archs : string [ ] , version : string ) : Promise < PriorityValue < string > [ ] > {
125+ async function parseTarget (
126+ distro : string | undefined ,
127+ platform : string ,
128+ archs : string [ ] ,
129+ version : string
130+ ) : Promise < PriorityValue < string > [ ] > {
125131 if ( platform === 'linux' ) {
126132 const results : PriorityValue < string > [ ] = [ ] ;
127133 if ( distro ) {
@@ -145,15 +151,17 @@ async function parseTarget(distro: string | undefined, platform: string, archs:
145151
146152 let distroResultsErr ;
147153 try {
148- results . push ( ...await getCurrentLinuxDistro ( ) ) ;
154+ results . push ( ...( await getCurrentLinuxDistro ( ) ) ) ;
149155 } catch ( err ) {
150156 distroResultsErr = err ;
151157 }
152- if ( distro === undefined &&
153- distroResultsErr &&
154- ( version === '*' ||
155- version === 'latest-alpha' ||
156- semver . gte ( version , '4.0.0' ) ) ) {
158+ if (
159+ distro === undefined &&
160+ distroResultsErr &&
161+ ( version === '*' ||
162+ version === 'latest-alpha' ||
163+ semver . gte ( version , '4.0.0' ) )
164+ ) {
157165 throw distroResultsErr ;
158166 }
159167 return results ;
@@ -163,39 +171,39 @@ async function parseTarget(distro: string | undefined, platform: string, archs:
163171 if ( archs . includes ( 'i686' ) ) {
164172 return [
165173 { value : 'windows' , priority : 1 } ,
166- { value : 'windows_i686' , priority : 10 }
174+ { value : 'windows_i686' , priority : 10 } ,
167175 ] ;
168176 } else {
169177 return [
170178 { value : 'windows' , priority : 1 } ,
171179 { value : 'windows_x86_64' , priority : 10 } ,
172180 { value : 'windows_x86_64-2008plus' , priority : 10 } ,
173181 { value : 'windows_x86_64-2008plus-ssl' , priority : 100 } ,
174- { value : 'windows_x86_64-2012plus' , priority : 100 }
182+ { value : 'windows_x86_64-2012plus' , priority : 100 } ,
175183 ] ;
176184 }
177185 } else if ( [ 'darwin' , 'osx' , 'macos' ] . includes ( platform ) ) {
178186 return [
179187 { value : 'osx' , priority : 1 } ,
180188 { value : 'osx-ssl' , priority : 10 } ,
181189 { value : 'darwin' , priority : 1 } ,
182- { value : 'macos' , priority : 1 }
190+ { value : 'macos' , priority : 1 } ,
183191 ] ;
184192 }
185193 return [ { value : platform , priority : 1 } ] ;
186194}
187195
188196async function resolve ( opts : ProcessedOptions ) : Promise < DownloadArtifactInfo > {
189- let download : DownloadInfo ;
197+ let download : DownloadInfo | undefined ;
190198 if ( opts . version === 'latest-alpha' && opts . enterprise ) {
191199 const targets = opts . target . map ( ( { value } ) => value ) ;
192200 const arch = opts . arch . includes ( 'arm64' ) ? 'arm64' : 'x86_64' ;
193- let url , target ;
201+ let url , target : string | undefined ;
194202 if ( targets . includes ( 'macos' ) ) {
195203 url = `https://downloads.mongodb.com/osx/mongodb-macos-${ arch } -enterprise-latest.tgz` ;
196204 target = 'macos' ;
197205 } else if ( targets . includes ( 'linux_x86_64' ) ) {
198- target = maximizer ( opts . target , candidate => candidate . priority ) . value ;
206+ target = maximizer ( opts . target , ( candidate ) => candidate . priority ) ! . value ;
199207 url = `https://downloads.mongodb.com/linux/mongodb-linux-${ arch } -enterprise-${ target } -latest.tgz` ;
200208 } else if ( targets . includes ( 'windows_x86_64' ) ) {
201209 target = 'windows' ;
@@ -210,8 +218,8 @@ async function resolve(opts: ProcessedOptions): Promise<DownloadArtifactInfo> {
210218 url,
211219 sha1 : '' ,
212220 sha256 : '' ,
213- debug_symbols : ''
214- }
221+ debug_symbols : '' ,
222+ } ,
215223 } ;
216224 }
217225 }
@@ -222,44 +230,59 @@ async function resolve(opts: ProcessedOptions): Promise<DownloadArtifactInfo> {
222230 if ( ! version ) {
223231 throw new Error ( `Could not find version matching ${ inspect ( opts ) } ` ) ;
224232 }
225- const bestDownload = maximizer ( version . downloads . map ( ( candidate : DownloadInfo ) => {
226- if ( opts . enterprise ) {
227- if ( candidate . edition !== 'enterprise' ) {
228- return { value : candidate , priority : 0 } ;
233+ const bestDownload = maximizer (
234+ version . downloads . map ( ( candidate : DownloadInfo ) => {
235+ if ( opts . enterprise ) {
236+ if ( candidate . edition !== 'enterprise' ) {
237+ return { value : candidate , priority : 0 } ;
238+ }
239+ } else {
240+ if (
241+ candidate . edition !== 'targeted' &&
242+ candidate . edition !== 'base'
243+ ) {
244+ return { value : candidate , priority : 0 } ;
245+ }
229246 }
230- } else {
231- if ( candidate . edition !== 'targeted' && candidate . edition !== 'base' ) {
247+
248+ if ( ! candidate . arch || ! opts . arch . includes ( candidate . arch ) ) {
232249 return { value : candidate , priority : 0 } ;
233250 }
234- }
235251
236- if ( ! opts . arch . includes ( candidate . arch ) ) {
237- return { value : candidate , priority : 0 } ;
238- }
252+ const targetPriority = getPriority ( opts . target , candidate . target ) ;
253+ return { value : candidate , priority : targetPriority } ;
254+ } ) ,
255+ ( candidate : PriorityValue < DownloadInfo > ) => candidate . priority
256+ ) ;
239257
240- const targetPriority = getPriority ( opts . target , candidate . target ) ;
241- return { value : candidate , priority : targetPriority } ;
242- } ) , ( candidate : PriorityValue < DownloadInfo > ) => candidate . priority ) ;
243- if ( bestDownload . priority > 0 ) {
258+ if ( bestDownload && bestDownload . priority > 0 ) {
244259 download = bestDownload . value ;
245260 }
246261 }
247262 if ( ! download ) {
248- throw new Error ( `Could not find download URL for version ${ version ?. version } ${ inspect ( opts ) } ` ) ;
263+ throw new Error (
264+ `Could not find download URL for version ${ version ?. version } ${ inspect (
265+ opts
266+ ) } `
267+ ) ;
249268 }
250269
251270 const wantsCryptd = opts . cryptd && download . target ;
252271 const wantsCryptShared = opts . crypt_shared && download . target ;
253272
254273 if ( wantsCryptShared && ! download . crypt_shared && ! download . csfle ) {
255- throw new Error ( `No crypt_shared library download for version ${ version ?. version } available ${ inspect ( opts ) } ` ) ;
274+ throw new Error (
275+ `No crypt_shared library download for version ${
276+ version ?. version
277+ } available ${ inspect ( opts ) } `
278+ ) ;
256279 }
257280
258281 debug ( 'fully resolved' , JSON . stringify ( opts , null , 2 ) , download ) ;
259282 // mongocryptd is contained in the regular enterprise archive, the csfle lib is not
260283 let { url } = wantsCryptShared
261- ? ( download . crypt_shared ?? download . csfle )
262- : ( ( wantsCryptd ? download . cryptd : null ) ?? download . archive ) ;
284+ ? ( download . crypt_shared ?? download . csfle ) !
285+ : ( wantsCryptd ? download . cryptd : null ) ?? download . archive ;
263286 if ( wantsCryptd ) {
264287 // cryptd package on Windows was buggy: https://jira.mongodb.org/browse/BUILD-13653
265288 url = url . replace ( 'mongodb-shell-windows' , 'mongodb-cryptd-windows' ) ;
@@ -269,24 +292,26 @@ async function resolve(opts: ProcessedOptions): Promise<DownloadArtifactInfo> {
269292 ...opts ,
270293 name : 'mongodb' ,
271294 url : url ,
272- arch : download . arch ,
273- distro : download . target ,
274- platform : download . target ,
275- filenamePlatform : download . target ,
295+ arch : download . arch ! ,
296+ distro : download . target ! ,
297+ platform : download . target ! ,
298+ filenamePlatform : download . target ! ,
276299 version : version ?. version ?? '*' ,
277300 artifact : path . basename ( url ) ,
278301 debug : false ,
279302 enterprise : download . edition === 'enterprise' ,
280303 branch : 'master' ,
281- bits : [ 'i386' , 'i686' ] . includes ( download . arch ) ? '32' : '64' ,
282- ext : url . match ( / \. ( [ ^ . ] + ) $ / ) ?. [ 1 ] ?? 'tgz'
304+ bits : [ 'i386' , 'i686' ] . includes ( download . arch ! ) ? '32' : '64' ,
305+ ext : url . match ( / \. ( [ ^ . ] + ) $ / ) ?. [ 1 ] ?? 'tgz' ,
283306 } ;
284307}
285308
286- async function options ( opts : Options | string = { } ) : Promise < ProcessedOptions & VersionListOpts > {
309+ async function options (
310+ opts : Options | string = { }
311+ ) : Promise < ProcessedOptions & VersionListOpts > {
287312 if ( typeof opts === 'string' ) {
288313 opts = {
289- version : opts
314+ version : opts ,
290315 } ;
291316 } else {
292317 opts = { ...opts } ;
@@ -314,10 +339,15 @@ async function options(opts: Options | string = {}): Promise<ProcessedOptions &
314339 opts . allowedTags = [ 'production_release' ] ;
315340 }
316341
317- if ( opts . version === 'stable' || opts . version === 'latest' || opts . version === '*' ) {
342+ if (
343+ opts . version === 'stable' ||
344+ opts . version === 'latest' ||
345+ opts . version === '*'
346+ ) {
318347 opts . version = '*' ;
319348 opts . allowedTags ??= [ 'production_release' ] ;
320- } else if ( opts . version === 'rapid' || opts . version === 'continuous' ) { // a.k.a. quarterly etc.
349+ } else if ( opts . version === 'rapid' || opts . version === 'continuous' ) {
350+ // a.k.a. quarterly etc.
321351 opts . version = '*' ;
322352 opts . allowedTags ??= [ 'production_release' , 'continuous_release' ] ;
323353 } else if ( opts . version === 'unstable' ) {
@@ -332,17 +362,20 @@ async function options(opts: Options | string = {}): Promise<ProcessedOptions &
332362 enterprise : ! ! opts . enterprise ,
333363 cryptd : ! ! opts . cryptd ,
334364 crypt_shared : ! ! opts . crypt_shared ,
335- version : opts . version as string
365+ version : opts . version ,
336366 } ;
337367 processedOptions . target = await parseTarget (
338368 opts . distro ,
339369 opts . platform ,
340370 processedOptions . arch ,
341- processedOptions . version ) ;
371+ processedOptions . version
372+ ) ;
342373 return processedOptions ;
343374}
344375
345- export async function getDownloadURL ( opts ?: Options | string ) : Promise < DownloadArtifactInfo > {
376+ export async function getDownloadURL (
377+ opts ?: Options | string
378+ ) : Promise < DownloadArtifactInfo > {
346379 const parsedOptions = await options ( opts ) ;
347380
348381 debug ( 'Building URL for options `%j`' , parsedOptions ) ;
0 commit comments