@@ -17,6 +17,7 @@ import osFilterObject from '@xhmikosr/os-filter-obj';
1717 * @property {string } url - The URL of the file.
1818 * @property {string } [os] - The operating system the file is for.
1919 * @property {string } [arch] - The architecture the file is for.
20+ * @property {string } [hash] - Expected hash as `"<algorithm>:<hex>"`, verified after download.
2021 */
2122
2223export default class BinWrapper {
@@ -39,15 +40,21 @@ export default class BinWrapper {
3940 * @param {string } [src] - The source URL of the file.
4041 * @param {string } [os] - The operating system the file is for.
4142 * @param {string } [arch] - The architecture the file is for.
43+ * @param {string } [hash] - Expected hash as `"<algorithm>:<hex>"`, verified after download.
4244 * @returns {SourceFile[]|undefined|this } - Returns the source files if no arguments are provided, otherwise returns `this`.
4345 */
44- src ( src , os , arch ) {
46+ src ( src , os , arch , hash ) {
4547 if ( arguments . length === 0 ) {
4648 return this . _src ;
4749 }
4850
4951 this . _src ||= [ ] ;
50- this . _src . push ( { url : src , os, arch} ) ;
52+ this . _src . push ( {
53+ url : src ,
54+ os,
55+ arch,
56+ hash,
57+ } ) ;
5158
5259 return this ;
5360 }
@@ -109,13 +116,22 @@ export default class BinWrapper {
109116 return path . join ( this . dest ( ) , this . use ( ) ) ;
110117 }
111118
119+ /**
120+ * Filter the configured sources down to the ones matching the current OS and arch
121+ *
122+ * @returns {SourceFile[] }
123+ */
124+ #resolveSources( ) {
125+ return osFilterObject ( this . src ( ) || [ ] ) ;
126+ }
127+
112128 /**
113129 * Get the source URLs matching the current OS and arch
114130 *
115131 * @returns {string[] }
116132 */
117133 resolvedUrls ( ) {
118- return osFilterObject ( this . src ( ) || [ ] ) . map ( file => file . url ) ;
134+ return this . #resolveSources ( ) . map ( file => file . url ) ;
119135 }
120136
121137 /**
@@ -177,15 +193,16 @@ export default class BinWrapper {
177193 * @api private
178194 */
179195 async download ( ) {
180- const urls = this . resolvedUrls ( ) ;
196+ const sources = this . #resolveSources ( ) ;
181197
182- if ( urls . length === 0 ) {
198+ if ( sources . length === 0 ) {
183199 throw new Error ( 'No binary found matching your system. It\'s probably not supported.' ) ;
184200 }
185201
186- const results = await Promise . all ( urls . map ( url =>
187- downloader ( url , this . dest ( ) , {
202+ const results = await Promise . all ( sources . map ( source =>
203+ downloader ( source . url , this . dest ( ) , {
188204 extract : true ,
205+ hash : source . hash ,
189206 decompress : {
190207 ...this . options . decompress ,
191208 strip : this . options . strip ,
@@ -197,7 +214,7 @@ export default class BinWrapper {
197214 return item . map ( file => file . path ) ;
198215 }
199216
200- const parsedUrl = new URL ( urls [ index ] ) ;
217+ const parsedUrl = new URL ( sources [ index ] . url ) ;
201218
202219 return path . parse ( parsedUrl . pathname ) . base ;
203220 } ) ;
0 commit comments