@@ -14,10 +14,35 @@ import { ExecutableMetadata, generateRCFile } from './executable-metadata';
14
14
import { spawnBuildCommand , ProcessEnv , pipeline , createCppJsStringDefinition } from './helpers' ;
15
15
import { Readable } from 'stream' ;
16
16
import nv from '@pkgjs/nv' ;
17
+ import { fileURLToPath , URL } from 'url' ;
17
18
18
19
// Download and unpack a tarball containing the code for a specific Node.js version.
19
20
async function getNodeSourceForVersion ( range : string , dir : string , logger : Logger , retries = 2 ) : Promise < string > {
20
21
logger . stepStarting ( `Looking for Node.js version matching ${ JSON . stringify ( range ) } ` ) ;
22
+
23
+ let inputIsFileUrl = false ;
24
+ try {
25
+ inputIsFileUrl = new URL ( range ) . protocol === 'file:' ;
26
+ } catch { /* not a valid URL */ }
27
+
28
+ if ( inputIsFileUrl ) {
29
+ logger . stepStarting ( `Extracting tarball from ${ range } ` ) ;
30
+ await pipeline (
31
+ createReadStream ( fileURLToPath ( range ) ) ,
32
+ zlib . createGunzip ( ) ,
33
+ tar . x ( {
34
+ cwd : dir
35
+ } )
36
+ ) ;
37
+ logger . stepCompleted ( ) ;
38
+ const filesInDir = await fs . readdir ( dir , { withFileTypes : true } ) ;
39
+ const dirsInDir = filesInDir . filter ( f => f . isDirectory ( ) ) ;
40
+ if ( dirsInDir . length !== 1 ) {
41
+ throw new Error ( 'Node.js tarballs should contain exactly one directory' ) ;
42
+ }
43
+ return path . join ( dir , dirsInDir [ 0 ] . name ) ;
44
+ }
45
+
21
46
const ver = ( await nv ( range ) ) . pop ( ) ;
22
47
if ( ! ver ) {
23
48
throw new Error ( `No node version found for ${ range } ` ) ;
0 commit comments