@@ -84,16 +84,8 @@ export async function componentize(
8484 }
8585 }
8686
87- const tmpDir = join (
88- tmpdir ( ) ,
89- createHash ( 'sha256' )
90- . update ( Math . random ( ) . toString ( ) )
91- . digest ( 'hex' )
92- . slice ( 0 , 12 ) ,
93- ) ;
94- await mkdir ( tmpDir ) ;
95- const sourceDir = join ( tmpDir , 'sources' ) ;
96- await mkdir ( sourceDir ) ;
87+ // Prepare a working diretory for use during componentization
88+ const { sourcesDir, baseDir : workDir } = await prepWorkDir ( ) ;
9789
9890 let {
9991 sourceName = 'source.js' ,
@@ -113,16 +105,8 @@ export async function componentize(
113105 ) ,
114106 } = opts ;
115107
116- const engine =
117- opts . engine ||
118- fileURLToPath (
119- new URL (
120- opts . enableAot
121- ? `../lib/starlingmonkey_embedding_weval.wasm`
122- : `../lib/starlingmonkey_embedding${ debugBuild ? '.debug' : '' } .wasm` ,
123- import . meta. url ,
124- ) ,
125- ) ;
108+ // Determine the path to the StarlingMonkey binary
109+ const engine = getEnginePath ( opts ) ;
126110
127111 let { wasm, jsBindings, exports, imports } = spliceBindings (
128112 await readFile ( engine ) ,
@@ -132,11 +116,11 @@ export async function componentize(
132116 false ,
133117 ) ;
134118
135- const input = join ( tmpDir , 'in.wasm' ) ;
136- const output = join ( tmpDir , 'out.wasm' ) ;
119+ const inputWasmPath = join ( workDir , 'in.wasm' ) ;
120+ const outputWasmPath = join ( workDir , 'out.wasm' ) ;
137121
138- await writeFile ( input , Buffer . from ( wasm ) ) ;
139- await writeFile ( join ( sourceDir , 'initializer.js' ) , jsBindings ) ;
122+ await writeFile ( inputWasmPath , Buffer . from ( wasm ) ) ;
123+ await writeFile ( join ( sourcesDir , 'initializer.js' ) , jsBindings ) ;
140124
141125 if ( debugBindings ) {
142126 console . log ( '--- JS Bindings ---' ) ;
@@ -312,11 +296,7 @@ export async function componentize(
312296 throw new Error ( err ) ;
313297 }
314298
315- const bin = await readFile ( output ) ;
316-
317- const tmpdirRemovePromise = debugBindings
318- ? Promise . resolve ( )
319- : rm ( tmpDir , { recursive : true } ) ;
299+ const bin = await readFile ( outputWasmPath ) ;
320300
321301 // Check for initialization errors
322302 // By actually executing the binary in a mini sandbox to get back
@@ -326,7 +306,10 @@ export async function componentize(
326306 getStderr,
327307 } = await initWasm ( bin ) ;
328308
329- await tmpdirRemovePromise ;
309+ // If not in debug mode, clean up
310+ if ( ! debugBindings ) {
311+ await rm ( workDir , { recursive : true } ) ;
312+ }
330313
331314 /// Process output of check init, throwing if necessary
332315 handleCheckInitOutput ( check_init ( ) , initializerPath , workDir , getStderr ) ;
@@ -430,6 +413,34 @@ function isNumeric(n) {
430413 }
431414}
432415
416+ /** Determine the correct path for the engine */
417+ function getEnginePath ( opts ) {
418+ if ( opts . engine ) {
419+ return opts . engine ;
420+ }
421+ const debugSuffix = opts ?. debugBuild ? '.debug' : '' ;
422+ let engineBinaryRelPath = `../lib/starlingmonkey_embedding${ debugSuffix } .wasm` ;
423+ if ( opts . enableAot ) {
424+ engineBinaryRelPath = '../lib/starlingmonkey_embedding_weval.wasm' ;
425+ }
426+ return fileURLToPath ( new URL ( engineBinaryRelPath , import . meta. url ) ) ;
427+ }
428+
429+ /** Prepare a work directory for use with componentization */
430+ async function prepWorkDir ( ) {
431+ const baseDir = join (
432+ tmpdir ( ) ,
433+ createHash ( 'sha256' )
434+ . update ( Math . random ( ) . toString ( ) )
435+ . digest ( 'hex' )
436+ . slice ( 0 , 12 ) ,
437+ ) ;
438+ await mkdir ( baseDir ) ;
439+ const sourcesDir = join ( baseDir , 'sources' ) ;
440+ await mkdir ( sourcesDir ) ;
441+ return { baseDir, sourcesDir } ;
442+ }
443+
433444/**
434445 * Initialize a WebAssembly binary, given the
435446 *
0 commit comments