@@ -32,54 +32,67 @@ export interface MongoBinaryOpts {
3232export default class MongoBinary {
3333 static cache : MongoBinaryCache = { } ;
3434
35+ /**
36+ * Probe if the provided "systemBinary" is an existing path
37+ * @param systemBinary The Path to probe for an System-Binary
38+ * @return System Binary path or empty string
39+ */
3540 static async getSystemPath ( systemBinary : string ) : Promise < string > {
3641 let binaryPath = '' ;
3742
3843 try {
3944 await promisify ( fs . access ) ( systemBinary ) ;
4045
41- log ( `MongoBinary: found sytem binary path at ${ systemBinary } ` ) ;
46+ log ( `MongoBinary: found system binary path at " ${ systemBinary } " ` ) ;
4247 binaryPath = systemBinary ;
4348 } catch ( err ) {
44- log ( `MongoBinary: can't find system binary at ${ systemBinary } . ${ err . message } ` ) ;
49+ log ( `MongoBinary: can't find system binary at " ${ systemBinary } ".\n ${ err . message } ` ) ;
4550 }
4651
4752 return binaryPath ;
4853 }
4954
50- static async getCachePath ( version : string ) : Promise < string > {
55+ /**
56+ * Check if specified version already exists in the cache
57+ * @param version The Version to check for
58+ */
59+ static getCachePath ( version : string ) : string {
5160 return this . cache [ version ] ;
5261 }
5362
63+ /**
64+ * Probe download path and download the binary
65+ * @param options Options Configuring which binary to download and to which path
66+ * @returns The BinaryPath the binary has been downloaded to
67+ */
5468 static async getDownloadPath ( options : Required < MongoBinaryOpts > ) : Promise < string > {
5569 const { downloadDir, platform, arch, version, checkMD5 } = options ;
56- // create downloadDir if not exists
70+ // create downloadDir
5771 await mkdirp ( downloadDir ) ;
5872
73+ /** Lockfile path */
5974 const lockfile = path . resolve ( downloadDir , `${ version } .lock` ) ;
60- // wait lock
75+ // wait to get a lock
76+ // downloading of binaries may be quite long procedure
77+ // that's why we are using so big wait/stale periods
6178 await new Promise ( ( resolve , reject ) => {
6279 LockFile . lock (
6380 lockfile ,
6481 {
65- wait : 120000 ,
82+ wait : 1000 * 120 , // 120 seconds
6683 pollPeriod : 100 ,
67- stale : 110000 ,
84+ stale : 1000 * 110 , // 110 seconds
6885 retries : 3 ,
6986 retryWait : 100 ,
7087 } ,
7188 ( err : any ) => {
72- if ( err ) {
73- reject ( err ) ;
74- } else {
75- resolve ( ) ;
76- }
89+ return err ? reject ( err ) : resolve ( ) ;
7790 }
7891 ) ;
7992 } ) ;
8093
81- // again check cache, maybe other instance resolve it
82- if ( ! this . cache [ version ] ) {
94+ // check cache if it got already added to the cache
95+ if ( ! this . getCachePath ( version ) ) {
8396 const downloader = new MongoBinaryDownload ( {
8497 downloadDir,
8598 platform,
@@ -90,16 +103,25 @@ export default class MongoBinary {
90103 this . cache [ version ] = await downloader . getMongodPath ( ) ;
91104 }
92105 // remove lock
93- LockFile . unlock ( lockfile , ( err : any ) => {
94- log (
95- err
96- ? `MongoBinary: Error when removing download lock ${ err } `
97- : `MongoBinary: Download lock removed`
98- ) ;
106+ await new Promise ( ( res ) => {
107+ LockFile . unlock ( lockfile , ( err ) => {
108+ log (
109+ err
110+ ? `MongoBinary: Error when removing download lock ${ err } `
111+ : `MongoBinary: Download lock removed`
112+ ) ;
113+ res ( ) ; // we don't care if it was successful or not
114+ } ) ;
99115 } ) ;
100- return this . cache [ version ] ;
116+ return this . getCachePath ( version ) ;
101117 }
102118
119+ /**
120+ * Probe all supported paths for an binary and return the binary path
121+ * @param opts Options configuring which binary to search for
122+ * @throws {Error } if no valid BinaryPath has been found
123+ * @return The first found BinaryPath
124+ */
103125 static async getPath ( opts : MongoBinaryOpts = { } ) : Promise < string > {
104126 const legacyDLDir = path . resolve ( os . homedir ( ) , '.cache/mongodb-binaries' ) ;
105127
@@ -109,6 +131,7 @@ export default class MongoBinary {
109131 nodeModulesDLDir = path . resolve ( nodeModulesDLDir , '..' , '..' ) ;
110132 }
111133
134+ // "||" is still used here, because it should default if the value is false-y (like an empty string)
112135 const defaultOptions = {
113136 downloadDir :
114137 resolveConfig ( 'DOWNLOAD_DIR' ) ||
@@ -128,17 +151,16 @@ export default class MongoBinary {
128151 checkMD5 : envToBool ( resolveConfig ( 'MD5_CHECK' ) ?? '' ) ,
129152 } ;
130153
154+ /** Provided Options combined with the Default Options */
131155 const options = { ...defaultOptions , ...opts } ;
132- log ( `MongoBinary options: ${ JSON . stringify ( options ) } ` ) ;
133-
134- const { version, systemBinary } = options ;
156+ log ( `MongoBinary options:` , JSON . stringify ( options , null , 2 ) ) ;
135157
136158 let binaryPath = '' ;
137159
138- if ( systemBinary ) {
139- binaryPath = await this . getSystemPath ( systemBinary ) ;
160+ if ( options . systemBinary ) {
161+ binaryPath = await this . getSystemPath ( options . systemBinary ) ;
140162 if ( binaryPath ) {
141- if ( ~ binaryPath . indexOf ( ' ' ) ) {
163+ if ( binaryPath . indexOf ( ' ' ) >= 0 ) {
142164 binaryPath = `"${ binaryPath } "` ;
143165 }
144166
@@ -147,36 +169,33 @@ export default class MongoBinary {
147169 . split ( '\n' ) [ 0 ]
148170 . split ( ' ' ) [ 2 ] ;
149171
150- if ( version !== LATEST_VERSION && version !== binaryVersion ) {
172+ if ( options . version !== LATEST_VERSION && options . version !== binaryVersion ) {
151173 // we will log the version number of the system binary and the version requested so the user can see the difference
152174 log (
153175 'MongoMemoryServer: Possible version conflict\n' +
154176 ` SystemBinary version: ${ binaryVersion } \n` +
155- ` Requested version: ${ version } \n\n` +
177+ ` Requested version: ${ options . version } \n\n` +
156178 ' Using SystemBinary!'
157179 ) ;
158180 }
159181 }
160182 }
161183
162184 if ( ! binaryPath ) {
163- binaryPath = await this . getCachePath ( version ) ;
185+ binaryPath = this . getCachePath ( options . version ) ;
164186 }
165187
166188 if ( ! binaryPath ) {
167189 binaryPath = await this . getDownloadPath ( options ) ;
168190 }
169191
170- log ( `MongoBinary: Mongod binary path: ${ binaryPath } ` ) ;
171- return binaryPath ;
172- }
173-
174- static hasValidBinPath ( files : string [ ] ) : boolean {
175- if ( files . length === 1 ) {
176- return true ;
177- } else if ( files . length > 1 ) {
178- return false ;
192+ if ( ! binaryPath ) {
193+ throw new Error (
194+ `MongoBinary.getPath: could not find an valid binary path! (Got: "${ binaryPath } ")`
195+ ) ;
179196 }
180- return false ;
197+
198+ log ( `MongoBinary: Mongod binary path: "${ binaryPath } "` ) ;
199+ return binaryPath ;
181200 }
182201}
0 commit comments