@@ -46,11 +46,11 @@ export const TsRunner = {
4646export type TsRunner = ValueOf < typeof TsRunner >
4747
4848const {
49- SYNCKIT_TIMEOUT ,
49+ NODE_OPTIONS ,
5050 SYNCKIT_EXEC_ARGV ,
51- SYNCKIT_TS_RUNNER ,
5251 SYNCKIT_GLOBAL_SHIMS ,
53- NODE_OPTIONS ,
52+ SYNCKIT_TIMEOUT ,
53+ SYNCKIT_TS_RUNNER ,
5454} = process . env
5555
5656export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? + SYNCKIT_TIMEOUT : undefined
@@ -78,14 +78,14 @@ export const DEFAULT_GLOBAL_SHIMS_PRESET: GlobalShim[] = [
7878
7979export const MTS_SUPPORTED_NODE_VERSION = 16
8080
81- const syncFnCache = new Map < string , AnyFn > ( )
81+ let syncFnCache : Map < string , AnyFn > | undefined
8282
8383export interface SynckitOptions {
84- timeout ?: number
8584 execArgv ?: string [ ]
86- tsRunner ?: TsRunner
87- transferList ?: TransferListItem [ ]
8885 globalShims ?: GlobalShim [ ] | boolean
86+ timeout ?: number
87+ transferList ?: TransferListItem [ ]
88+ tsRunner ?: TsRunner
8989}
9090
9191// MessagePort doesn't copy the properties of Error objects. We still want
@@ -107,16 +107,18 @@ export function createSyncFn<T extends AnyAsyncFn<R>, R = unknown>(
107107 workerPath : string ,
108108 timeoutOrOptions ?: SynckitOptions | number ,
109109) : Syncify < T > {
110- if ( ! path . isAbsolute ( workerPath ) ) {
111- throw new Error ( '`workerPath` must be absolute' )
112- }
110+ syncFnCache ??= new Map ( )
113111
114112 const cachedSyncFn = syncFnCache . get ( workerPath )
115113
116114 if ( cachedSyncFn ) {
117115 return cachedSyncFn as Syncify < T >
118116 }
119117
118+ if ( ! path . isAbsolute ( workerPath ) ) {
119+ throw new Error ( '`workerPath` must be absolute' )
120+ }
121+
120122 const syncFn = startWorkerThread < R , T > (
121123 workerPath ,
122124 /* istanbul ignore next */ typeof timeoutOrOptions === 'number'
@@ -309,8 +311,8 @@ export const encodeImportModule = (
309311 ( named === null
310312 ? '* as ' + globalName
311313 : named ?. trim ( )
312- ? `{${ named } }`
313- : globalName ) +
314+ ? `{${ named } }`
315+ : globalName ) +
314316 ' from'
315317 : ''
316318 } '${
@@ -354,7 +356,7 @@ export const _generateGlobals = (
354356 '' ,
355357 )
356358
357- const globalsCache = new Map < string , [ content : string , filepath ?: string ] > ( )
359+ let globalsCache : Map < string , [ content : string , filepath ?: string ] > | undefined
358360
359361let tmpdir : string
360362
@@ -371,6 +373,8 @@ export const generateGlobals = (
371373 globalShims : GlobalShim [ ] ,
372374 type : 'import' | 'require' = 'import' ,
373375) => {
376+ globalsCache ??= new Map ( )
377+
374378 const cached = globalsCache . get ( workerPath )
375379
376380 if ( cached ) {
@@ -462,14 +466,17 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
462466 globalShims === true
463467 ? DEFAULT_GLOBAL_SHIMS_PRESET
464468 : Array . isArray ( globalShims )
465- ? globalShims
466- : [ ]
469+ ? globalShims
470+ : [ ]
467471 ) . filter ( ( { moduleName } ) => isPkgAvailable ( moduleName ) )
468472
469473 // We store a single Byte in the SharedArrayBuffer
470474 // for the notification, we can used a fixed size
471- sharedBuffer ??= new SharedArrayBuffer ( INT32_BYTES )
472- sharedBufferView ??= new Int32Array ( sharedBuffer , 0 , 1 )
475+ sharedBufferView ??= new Int32Array (
476+ ( sharedBuffer ??= new SharedArrayBuffer ( INT32_BYTES ) ) ,
477+ 0 ,
478+ 1 ,
479+ )
473480
474481 const useGlobals = finalGlobalShims . length > 0
475482
@@ -484,12 +491,12 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
484491 ) } ;import '${ String ( workerPathUrl ) } '`,
485492 )
486493 : useEval
487- ? `${ generateGlobals (
488- finalWorkerPath ,
489- finalGlobalShims ,
490- 'require' ,
491- ) } ;${ encodeImportModule ( finalWorkerPath , 'require' ) } `
492- : workerPathUrl ,
494+ ? `${ generateGlobals (
495+ finalWorkerPath ,
496+ finalGlobalShims ,
497+ 'require' ,
498+ ) } ;${ encodeImportModule ( finalWorkerPath , 'require' ) } `
499+ : workerPathUrl ,
493500 {
494501 eval : useEval ,
495502 workerData : { sharedBuffer, workerPort } ,
@@ -504,9 +511,11 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
504511 const id = nextID ++
505512
506513 const msg : MainToWorkerMessage < Parameters < T > > = { id, args }
514+
507515 worker . postMessage ( msg )
508516
509517 const status = Atomics . wait ( sharedBufferView ! , 0 , 0 , timeout )
518+
510519 // Reset SharedArrayBuffer for next call
511520 Atomics . store ( sharedBufferView ! , 0 , 0 )
512521
@@ -551,6 +560,7 @@ export function runAsWorker<
551560 }
552561
553562 const { workerPort, sharedBuffer } = workerData as WorkerData
563+
554564 const sharedBufferView = new Int32Array ( sharedBuffer , 0 , 1 )
555565
556566 parentPort ! . on (
0 commit comments