@@ -46,11 +46,11 @@ export const TsRunner = {
46
46
export type TsRunner = ValueOf < typeof TsRunner >
47
47
48
48
const {
49
- SYNCKIT_TIMEOUT ,
49
+ NODE_OPTIONS ,
50
50
SYNCKIT_EXEC_ARGV ,
51
- SYNCKIT_TS_RUNNER ,
52
51
SYNCKIT_GLOBAL_SHIMS ,
53
- NODE_OPTIONS ,
52
+ SYNCKIT_TIMEOUT ,
53
+ SYNCKIT_TS_RUNNER ,
54
54
} = process . env
55
55
56
56
export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? + SYNCKIT_TIMEOUT : undefined
@@ -78,14 +78,14 @@ export const DEFAULT_GLOBAL_SHIMS_PRESET: GlobalShim[] = [
78
78
79
79
export const MTS_SUPPORTED_NODE_VERSION = 16
80
80
81
- const syncFnCache = new Map < string , AnyFn > ( )
81
+ let syncFnCache : Map < string , AnyFn > | undefined
82
82
83
83
export interface SynckitOptions {
84
- timeout ?: number
85
84
execArgv ?: string [ ]
86
- tsRunner ?: TsRunner
87
- transferList ?: TransferListItem [ ]
88
85
globalShims ?: GlobalShim [ ] | boolean
86
+ timeout ?: number
87
+ transferList ?: TransferListItem [ ]
88
+ tsRunner ?: TsRunner
89
89
}
90
90
91
91
// 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>(
107
107
workerPath : string ,
108
108
timeoutOrOptions ?: SynckitOptions | number ,
109
109
) : Syncify < T > {
110
- if ( ! path . isAbsolute ( workerPath ) ) {
111
- throw new Error ( '`workerPath` must be absolute' )
112
- }
110
+ syncFnCache ??= new Map ( )
113
111
114
112
const cachedSyncFn = syncFnCache . get ( workerPath )
115
113
116
114
if ( cachedSyncFn ) {
117
115
return cachedSyncFn as Syncify < T >
118
116
}
119
117
118
+ if ( ! path . isAbsolute ( workerPath ) ) {
119
+ throw new Error ( '`workerPath` must be absolute' )
120
+ }
121
+
120
122
const syncFn = startWorkerThread < R , T > (
121
123
workerPath ,
122
124
/* istanbul ignore next */ typeof timeoutOrOptions === 'number'
@@ -309,8 +311,8 @@ export const encodeImportModule = (
309
311
( named === null
310
312
? '* as ' + globalName
311
313
: named ?. trim ( )
312
- ? `{${ named } }`
313
- : globalName ) +
314
+ ? `{${ named } }`
315
+ : globalName ) +
314
316
' from'
315
317
: ''
316
318
} '${
@@ -354,7 +356,7 @@ export const _generateGlobals = (
354
356
'' ,
355
357
)
356
358
357
- const globalsCache = new Map < string , [ content : string , filepath ?: string ] > ( )
359
+ let globalsCache : Map < string , [ content : string , filepath ?: string ] > | undefined
358
360
359
361
let tmpdir : string
360
362
@@ -371,6 +373,8 @@ export const generateGlobals = (
371
373
globalShims : GlobalShim [ ] ,
372
374
type : 'import' | 'require' = 'import' ,
373
375
) => {
376
+ globalsCache ??= new Map ( )
377
+
374
378
const cached = globalsCache . get ( workerPath )
375
379
376
380
if ( cached ) {
@@ -462,14 +466,17 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
462
466
globalShims === true
463
467
? DEFAULT_GLOBAL_SHIMS_PRESET
464
468
: Array . isArray ( globalShims )
465
- ? globalShims
466
- : [ ]
469
+ ? globalShims
470
+ : [ ]
467
471
) . filter ( ( { moduleName } ) => isPkgAvailable ( moduleName ) )
468
472
469
473
// We store a single Byte in the SharedArrayBuffer
470
474
// 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
+ )
473
480
474
481
const useGlobals = finalGlobalShims . length > 0
475
482
@@ -484,12 +491,12 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
484
491
) } ;import '${ String ( workerPathUrl ) } '`,
485
492
)
486
493
: 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 ,
493
500
{
494
501
eval : useEval ,
495
502
workerData : { sharedBuffer, workerPort } ,
@@ -504,9 +511,11 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
504
511
const id = nextID ++
505
512
506
513
const msg : MainToWorkerMessage < Parameters < T > > = { id, args }
514
+
507
515
worker . postMessage ( msg )
508
516
509
517
const status = Atomics . wait ( sharedBufferView ! , 0 , 0 , timeout )
518
+
510
519
// Reset SharedArrayBuffer for next call
511
520
Atomics . store ( sharedBufferView ! , 0 , 0 )
512
521
@@ -551,6 +560,7 @@ export function runAsWorker<
551
560
}
552
561
553
562
const { workerPort, sharedBuffer } = workerData as WorkerData
563
+
554
564
const sharedBufferView = new Int32Array ( sharedBuffer , 0 , 1 )
555
565
556
566
parentPort ! . on (
0 commit comments