-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
639 lines (553 loc) · 16.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import { createHash } from 'node:crypto'
import fs from 'node:fs'
import module from 'node:module'
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import {
MessageChannel,
MessagePort,
type TransferListItem,
Worker,
parentPort,
receiveMessageOnPort,
// type-coverage:ignore-next-line -- we can't control
workerData,
} from 'node:worker_threads'
import { findUp, isPkgAvailable, tryExtensions } from '@pkgr/core'
import type {
AnyAsyncFn,
AnyFn,
GlobalShim,
MainToWorkerCommandMessage,
MainToWorkerMessage,
Syncify,
ValueOf,
WorkerData,
WorkerToMainMessage,
} from './types.js'
const INT32_BYTES = 4
export * from './types.js'
export const TsRunner = {
// https://github.com/TypeStrong/ts-node
TsNode: 'ts-node',
// https://github.com/egoist/esbuild-register
EsbuildRegister: 'esbuild-register',
// https://github.com/folke/esbuild-runner
EsbuildRunner: 'esbuild-runner',
// https://github.com/swc-project/swc-node/tree/master/packages/register
SWC: 'swc',
// https://github.com/esbuild-kit/tsx
TSX: 'tsx',
} as const
export type TsRunner = ValueOf<typeof TsRunner>
const {
NODE_OPTIONS,
SYNCKIT_EXEC_ARGV,
SYNCKIT_GLOBAL_SHIMS,
SYNCKIT_TIMEOUT,
SYNCKIT_TS_RUNNER,
} = process.env
const IS_NODE_20 = Number(process.versions.node.split('.')[0]) >= 20
export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : undefined
/* istanbul ignore next */
export const DEFAULT_EXEC_ARGV = SYNCKIT_EXEC_ARGV?.split(',') || []
export const DEFAULT_TS_RUNNER = SYNCKIT_TS_RUNNER as TsRunner | undefined
export const DEFAULT_GLOBAL_SHIMS = ['1', 'true'].includes(
SYNCKIT_GLOBAL_SHIMS!,
)
export const DEFAULT_GLOBAL_SHIMS_PRESET: GlobalShim[] = [
{
moduleName: 'node-fetch',
globalName: 'fetch',
},
{
moduleName: 'node:perf_hooks',
globalName: 'performance',
named: 'performance',
},
]
export const MTS_SUPPORTED_NODE_VERSION = 16
let syncFnCache: Map<string, AnyFn> | undefined
export interface SynckitOptions {
execArgv?: string[]
globalShims?: GlobalShim[] | boolean
timeout?: number
transferList?: TransferListItem[]
tsRunner?: TsRunner
}
// MessagePort doesn't copy the properties of Error objects. We still want
// error objects to have extra properties such as "warnings" so implement the
// property copying manually.
export function extractProperties<T extends object>(object: T): T
export function extractProperties<T>(object?: T): T | undefined
export function extractProperties<T>(object?: T) {
if (object && typeof object === 'object') {
const properties = {} as T
for (const key in object) {
properties[key as keyof T] = object[key]
}
return properties
}
}
export function createSyncFn<T extends AnyAsyncFn<R>, R = unknown>(
workerPath: string,
timeoutOrOptions?: SynckitOptions | number,
): Syncify<T> {
syncFnCache ??= new Map()
const cachedSyncFn = syncFnCache.get(workerPath)
if (cachedSyncFn) {
return cachedSyncFn as Syncify<T>
}
if (!path.isAbsolute(workerPath)) {
throw new Error('`workerPath` must be absolute')
}
const syncFn = startWorkerThread<R, T>(
workerPath,
/* istanbul ignore next */ typeof timeoutOrOptions === 'number'
? { timeout: timeoutOrOptions }
: timeoutOrOptions,
)
syncFnCache.set(workerPath, syncFn)
return syncFn as Syncify<T>
}
const cjsRequire =
typeof require === 'undefined'
? module.createRequire(import.meta.url)
: /* istanbul ignore next */ require
const dataUrl = (code: string) =>
new URL(`data:text/javascript,${encodeURIComponent(code)}`)
export const isFile = (path: string) => {
try {
return !!fs.statSync(path, { throwIfNoEntry: false })?.isFile()
} catch {
/* istanbul ignore next */
return false
}
}
const setupTsRunner = (
workerPath: string,
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner?: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
) => {
let ext = path.extname(workerPath)
if (
!/[/\\]node_modules[/\\]/.test(workerPath) &&
(!ext || /^\.[cm]?js$/.test(ext))
) {
const workPathWithoutExt = ext
? workerPath.slice(0, -ext.length)
: workerPath
let extensions: string[]
switch (ext) {
case '.cjs': {
extensions = ['.cts', '.cjs']
break
}
case '.mjs': {
extensions = ['.mts', '.mjs']
break
}
default: {
extensions = ['.ts', '.js']
break
}
}
const found = tryExtensions(workPathWithoutExt, extensions)
let differentExt: boolean | undefined
if (found && (!ext || (differentExt = found !== workPathWithoutExt))) {
workerPath = found
if (differentExt) {
ext = path.extname(workerPath)
}
}
}
const isTs = /\.[cm]?ts$/.test(workerPath)
let jsUseEsm = workerPath.endsWith('.mjs')
let tsUseEsm = workerPath.endsWith('.mts')
if (isTs) {
if (!tsUseEsm) {
const pkg = findUp(workerPath)
if (pkg) {
tsUseEsm =
(cjsRequire(pkg) as { type?: 'commonjs' | 'module' }).type ===
'module'
}
}
if (tsRunner == null && isPkgAvailable(TsRunner.TsNode)) {
tsRunner = TsRunner.TsNode
}
switch (tsRunner) {
case TsRunner.TsNode: {
if (tsUseEsm) {
if (!execArgv.includes('--loader')) {
execArgv = ['--loader', `${TsRunner.TsNode}/esm`, ...execArgv]
}
} else if (!execArgv.includes('-r')) {
execArgv = ['-r', `${TsRunner.TsNode}/register`, ...execArgv]
}
break
}
case TsRunner.EsbuildRegister: {
if (!execArgv.includes('-r')) {
execArgv = ['-r', TsRunner.EsbuildRegister, ...execArgv]
}
break
}
case TsRunner.EsbuildRunner: {
if (!execArgv.includes('-r')) {
execArgv = ['-r', `${TsRunner.EsbuildRunner}/register`, ...execArgv]
}
break
}
case TsRunner.SWC: {
if (!execArgv.includes('-r')) {
execArgv = ['-r', `@${TsRunner.SWC}-node/register`, ...execArgv]
}
break
}
case TsRunner.TSX: {
if (!execArgv.includes('--loader')) {
execArgv = ['--loader', TsRunner.TSX, ...execArgv]
}
break
}
default: {
throw new Error(`Unknown ts runner: ${String(tsRunner)}`)
}
}
} else if (!jsUseEsm) {
const pkg = findUp(workerPath)
if (pkg) {
jsUseEsm =
(cjsRequire(pkg) as { type?: 'commonjs' | 'module' }).type === 'module'
}
}
let resolvedPnpLoaderPath: string | undefined
/* istanbul ignore if -- https://github.com/facebook/jest/issues/5274 */
if (process.versions.pnp) {
const nodeOptions = NODE_OPTIONS?.split(/\s+/)
let pnpApiPath: string | undefined
try {
/** @see https://github.com/facebook/jest/issues/9543 */
pnpApiPath = cjsRequire.resolve('pnpapi')
} catch {}
if (
pnpApiPath &&
!nodeOptions?.some(
(option, index) =>
['-r', '--require'].includes(option) &&
pnpApiPath === cjsRequire.resolve(nodeOptions[index + 1]),
) &&
!execArgv.includes(pnpApiPath)
) {
execArgv = ['-r', pnpApiPath, ...execArgv]
const pnpLoaderPath = path.resolve(pnpApiPath, '../.pnp.loader.mjs')
if (isFile(pnpLoaderPath)) {
// Transform path to file URL because nodejs does not accept
// absolute Windows paths in the --experimental-loader option.
// https://github.com/un-ts/synckit/issues/123
resolvedPnpLoaderPath = pathToFileURL(pnpLoaderPath).toString()
if (!IS_NODE_20) {
execArgv = [
'--experimental-loader',
resolvedPnpLoaderPath,
...execArgv,
]
}
}
}
}
return {
ext,
isTs,
jsUseEsm,
tsRunner,
tsUseEsm,
workerPath,
pnpLoaderPath: resolvedPnpLoaderPath,
execArgv,
}
}
const md5Hash = (text: string) => createHash('md5').update(text).digest('hex')
export const encodeImportModule = (
moduleNameOrGlobalShim: GlobalShim | string,
type: 'import' | 'require' = 'import',
// eslint-disable-next-line sonarjs/cognitive-complexity
) => {
const { moduleName, globalName, named, conditional }: GlobalShim =
typeof moduleNameOrGlobalShim === 'string'
? { moduleName: moduleNameOrGlobalShim }
: moduleNameOrGlobalShim
const importStatement =
type === 'import'
? `import${
globalName
? ' ' +
(named === null
? '* as ' + globalName
: named?.trim()
? `{${named}}`
: globalName) +
' from'
: ''
} '${
path.isAbsolute(moduleName)
? String(pathToFileURL(moduleName))
: moduleName
}'`
: `${
globalName
? 'const ' + (named?.trim() ? `{${named}}` : globalName) + '='
: ''
}require('${moduleName
// eslint-disable-next-line unicorn/prefer-string-replace-all -- compatibility
.replace(/\\/g, '\\\\')}')`
if (!globalName) {
return importStatement
}
const overrideStatement = `globalThis.${globalName}=${
named?.trim() ? named : globalName
}`
return (
importStatement +
(conditional === false
? `;${overrideStatement}`
: `;if(!globalThis.${globalName})${overrideStatement}`)
)
}
/**
* @internal
*/
export const _generateGlobals = (
globalShims: GlobalShim[],
type: 'import' | 'require',
) =>
globalShims.reduce(
(acc, shim) => `${acc}${acc ? ';' : ''}${encodeImportModule(shim, type)}`,
'',
)
let globalsCache: Map<string, [content: string, filepath?: string]> | undefined
let tmpdir: string
const _dirname =
typeof __dirname === 'undefined'
? path.dirname(fileURLToPath(import.meta.url))
: /* istanbul ignore next */ __dirname
let sharedBuffer: SharedArrayBuffer | undefined
let sharedBufferView: Int32Array | undefined
export const generateGlobals = (
workerPath: string,
globalShims: GlobalShim[],
type: 'import' | 'require' = 'import',
) => {
globalsCache ??= new Map()
const cached = globalsCache.get(workerPath)
if (cached) {
const [content, filepath] = cached
if (
(type === 'require' && !filepath) ||
(type === 'import' && filepath && isFile(filepath))
) {
return content
}
}
const globals = _generateGlobals(globalShims, type)
let content = globals
let filepath: string | undefined
if (type === 'import') {
if (!tmpdir) {
tmpdir = path.resolve(findUp(_dirname), '../node_modules/.synckit')
}
fs.mkdirSync(tmpdir, { recursive: true })
filepath = path.resolve(tmpdir, md5Hash(workerPath) + '.mjs')
content = encodeImportModule(filepath)
fs.writeFileSync(filepath, globals)
}
globalsCache.set(workerPath, [content, filepath])
return content
}
// eslint-disable-next-line sonarjs/cognitive-complexity
function startWorkerThread<R, T extends AnyAsyncFn<R>>(
workerPath: string,
{
timeout = DEFAULT_TIMEOUT,
execArgv = DEFAULT_EXEC_ARGV,
tsRunner = DEFAULT_TS_RUNNER,
transferList = [],
globalShims = DEFAULT_GLOBAL_SHIMS,
}: SynckitOptions = {},
) {
const { port1: mainPort, port2: workerPort } = new MessageChannel()
const {
isTs,
ext,
jsUseEsm,
tsUseEsm,
tsRunner: finalTsRunner,
workerPath: finalWorkerPath,
pnpLoaderPath,
execArgv: finalExecArgv,
} = setupTsRunner(workerPath, { execArgv, tsRunner })
const workerPathUrl = pathToFileURL(finalWorkerPath)
if (/\.[cm]ts$/.test(finalWorkerPath)) {
const isTsxSupported =
!tsUseEsm ||
Number.parseFloat(process.versions.node) >= MTS_SUPPORTED_NODE_VERSION
/* istanbul ignore if */
if (!finalTsRunner) {
throw new Error('No ts runner specified, ts worker path is not supported')
} /* istanbul ignore if */ else if (
(
[
// https://github.com/egoist/esbuild-register/issues/79
TsRunner.EsbuildRegister,
// https://github.com/folke/esbuild-runner/issues/67
TsRunner.EsbuildRunner,
// https://github.com/swc-project/swc-node/issues/667
TsRunner.SWC,
.../* istanbul ignore next */ (isTsxSupported ? [] : [TsRunner.TSX]),
] as TsRunner[]
).includes(finalTsRunner)
) {
throw new Error(
`${finalTsRunner} is not supported for ${ext} files yet` +
/* istanbul ignore next */ (isTsxSupported
? ', you can try [tsx](https://github.com/esbuild-kit/tsx) instead'
: ''),
)
}
}
const finalGlobalShims = (
globalShims === true
? DEFAULT_GLOBAL_SHIMS_PRESET
: Array.isArray(globalShims)
? globalShims
: []
).filter(({ moduleName }) => isPkgAvailable(moduleName))
// We store a single Byte in the SharedArrayBuffer
// for the notification, we can used a fixed size
sharedBufferView ??= new Int32Array(
/* istanbul ignore next */ (sharedBuffer ??= new SharedArrayBuffer(
INT32_BYTES,
)),
0,
1,
)
const useGlobals = finalGlobalShims.length > 0
const useEval = isTs ? !tsUseEsm : !jsUseEsm && useGlobals
const worker = new Worker(
(jsUseEsm && useGlobals) || (tsUseEsm && finalTsRunner === TsRunner.TsNode)
? dataUrl(
`${generateGlobals(
finalWorkerPath,
finalGlobalShims,
)};import '${String(workerPathUrl)}'`,
)
: useEval
? `${generateGlobals(
finalWorkerPath,
finalGlobalShims,
'require',
)};${encodeImportModule(finalWorkerPath, 'require')}`
: workerPathUrl,
{
eval: useEval,
workerData: { sharedBuffer, workerPort, pnpLoaderPath },
transferList: [workerPort, ...transferList],
execArgv: finalExecArgv,
},
)
let nextID = 0
const receiveMessageWithId = (
port: MessagePort,
expectedId: number,
waitingTimeout?: number,
): WorkerToMainMessage<R> => {
const start = Date.now()
const status = Atomics.wait(sharedBufferView!, 0, 0, waitingTimeout)
Atomics.store(sharedBufferView!, 0, 0)
if (!['ok', 'not-equal'].includes(status)) {
const abortMsg: MainToWorkerCommandMessage = {
id: expectedId,
cmd: 'abort',
}
port.postMessage(abortMsg)
throw new Error('Internal error: Atomics.wait() failed: ' + status)
}
const { id, ...message } = (
receiveMessageOnPort(mainPort) as { message: WorkerToMainMessage<R> }
).message
if (id < expectedId) {
const waitingTime = Date.now() - start
return receiveMessageWithId(
port,
expectedId,
waitingTimeout ? waitingTimeout - waitingTime : undefined,
)
}
if (expectedId !== id) {
throw new Error(
`Internal error: Expected id ${expectedId} but got id ${id}`,
)
}
return { id, ...message }
}
const syncFn = (...args: Parameters<T>): R => {
const id = nextID++
const msg: MainToWorkerMessage<Parameters<T>> = { id, args }
worker.postMessage(msg)
const { result, error, properties } = receiveMessageWithId(
mainPort,
id,
timeout,
)
if (error) {
throw Object.assign(error as object, properties)
}
return result!
}
worker.unref()
return syncFn
}
/* istanbul ignore next */
export function runAsWorker<
R = unknown,
T extends AnyAsyncFn<R> = AnyAsyncFn<R>,
>(fn: T) {
// type-coverage:ignore-next-line -- we can't control
if (!workerData) {
return
}
const { workerPort, sharedBuffer, pnpLoaderPath } = workerData as WorkerData
if (pnpLoaderPath && IS_NODE_20) {
module.register(pnpLoaderPath)
}
const sharedBufferView = new Int32Array(sharedBuffer, 0, 1)
parentPort!.on(
'message',
({ id, args }: MainToWorkerMessage<Parameters<T>>) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
let isAborted = false
const handleAbortMessage = (msg: MainToWorkerCommandMessage) => {
if (msg.id === id && msg.cmd === 'abort') {
isAborted = true
}
}
workerPort.on('message', handleAbortMessage)
let msg: WorkerToMainMessage<R>
try {
msg = { id, result: await fn(...args) }
} catch (error: unknown) {
msg = { id, error, properties: extractProperties(error) }
}
workerPort.off('message', handleAbortMessage)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (isAborted) {
return
}
workerPort.postMessage(msg)
Atomics.add(sharedBufferView, 0, 1)
Atomics.notify(sharedBufferView, 0)
})()
},
)
}