synckit 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +39 -6
- package/lib/index.cjs +92 -28
- package/lib/index.d.ts +10 -1
- package/lib/index.js +99 -28
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +2 -1
- package/package.json +37 -5
package/LICENSE
CHANGED
package/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# synckit
|
2
2
|
|
3
|
-
[](https://github.com/un-ts/synckit/actions/workflows/ci.yml)
|
4
|
+
[](https://codecov.io/gh/un-ts/synckit)
|
5
|
+
[](https://lgtm.com/projects/g/un-ts/synckit/context:javascript)
|
6
|
+
[](https://github.com/plantain-00/type-coverage)
|
7
7
|
[](https://www.npmjs.com/package/synckit)
|
8
|
-
[](https://github.com/un-ts/synckit/releases)
|
9
9
|
|
10
10
|
[](https://conventionalcommits.org)
|
11
11
|
[](https://renovatebot.com)
|
@@ -21,7 +21,13 @@ Perform async work synchronously in Node.js using `worker_threads` with first-cl
|
|
21
21
|
- [API](#api)
|
22
22
|
- [Envs](#envs)
|
23
23
|
- [TypeScript](#typescript)
|
24
|
+
- [`ts-node`](#ts-node)
|
25
|
+
- [`esbuild-register`](#esbuild-register)
|
26
|
+
- [`esbuild-runner`](#esbuild-runner)
|
27
|
+
- [`tsx`](#tsx)
|
24
28
|
- [Benchmark](#benchmark)
|
29
|
+
- [Sponsors](#sponsors)
|
30
|
+
- [Backers](#backers)
|
25
31
|
- [Changelog](#changelog)
|
26
32
|
- [License](#license)
|
27
33
|
|
@@ -60,22 +66,37 @@ runAsWorker(async (...args) => {
|
|
60
66
|
})
|
61
67
|
```
|
62
68
|
|
63
|
-
You must make sure, the `result` is
|
69
|
+
You must make sure, the `result` is serializable by [`Structured Clone Algorithm`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
64
70
|
|
65
71
|
### Envs
|
66
72
|
|
67
73
|
1. `SYNCKIT_BUFFER_SIZE`: `bufferSize` to create `SharedArrayBuffer` for `worker_threads` (default as `1024`)
|
68
74
|
2. `SYNCKIT_TIMEOUT`: `timeout` for performing the async job (no default)
|
69
75
|
3. `SYNCKIT_EXEC_ARGV`: List of node CLI options passed to the worker, split with comma `,`. (default as `[]`), see also [`node` docs](https://nodejs.org/api/worker_threads.html)
|
76
|
+
4. `SYNCKIT_TS_RUNNER`: Which TypeScript runner to be used, it could be very useful for development, could be `'ts-node' | 'esbuild-register' | 'esbuild-runner' | 'tsx'`, `'ts-node'` is used by default, make sure you have installed them already
|
70
77
|
|
71
78
|
### TypeScript
|
72
79
|
|
80
|
+
#### `ts-node`
|
81
|
+
|
73
82
|
If you want to use `ts-node` for worker file (a `.ts` file), it is supported out of box!
|
74
83
|
|
75
84
|
If you want to use a custom tsconfig as project instead of default `tsconfig.json`, use `TS_NODE_PROJECT` env. Please view [ts-node](https://github.com/TypeStrong/ts-node#tsconfig) for more details.
|
76
85
|
|
77
86
|
If you want to integrate with [tsconfig-paths](https://www.npmjs.com/package/tsconfig-paths), please view [ts-node](https://github.com/TypeStrong/ts-node#paths-and-baseurl) for more details.
|
78
87
|
|
88
|
+
#### `esbuild-register`
|
89
|
+
|
90
|
+
Please view <https://github.com/egoist/esbuild-register> for its document
|
91
|
+
|
92
|
+
#### `esbuild-runner`
|
93
|
+
|
94
|
+
Please view <https://github.com/folke/esbuild-runner> for its document
|
95
|
+
|
96
|
+
#### `tsx`
|
97
|
+
|
98
|
+
Please view <https://github.com/esbuild-kit/tsx> for its document
|
99
|
+
|
79
100
|
## Benchmark
|
80
101
|
|
81
102
|
It is about 20x faster than [`sync-threads`](https://github.com/lambci/sync-threads) but 3x slower than native for reading the file content itself 1000 times during runtime, and 18x faster than `sync-threads` but 4x slower than native for total time.
|
@@ -86,6 +107,18 @@ See [benchmark.cjs](./benchmarks/benchmark.cjs.txt) and [benchmark.esm](./benchm
|
|
86
107
|
|
87
108
|
You can try it with running `yarn benchmark` by yourself. [Here](./benchmarks/benchmark.js) is the benchmark source code.
|
88
109
|
|
110
|
+
## Sponsors
|
111
|
+
|
112
|
+
| 1stG | RxTS | UnTS |
|
113
|
+
| ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
|
114
|
+
| [](https://opencollective.com/1stG) | [](https://opencollective.com/rxts) | [](https://opencollective.com/unts) |
|
115
|
+
|
116
|
+
## Backers
|
117
|
+
|
118
|
+
| 1stG | RxTS | UnTS |
|
119
|
+
| -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
120
|
+
| [](https://opencollective.com/1stG) | [](https://opencollective.com/unts) | [](https://opencollective.com/unts) |
|
121
|
+
|
89
122
|
## Changelog
|
90
123
|
|
91
124
|
Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.md).
|
package/lib/index.cjs
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
var
|
6
|
-
var path = require('path');
|
7
|
-
var
|
8
|
-
var
|
5
|
+
var node_module = require('node:module');
|
6
|
+
var path = require('node:path');
|
7
|
+
var node_url = require('node:url');
|
8
|
+
var node_worker_threads = require('node:worker_threads');
|
9
9
|
var utils = require('@pkgr/utils');
|
10
10
|
|
11
11
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
@@ -33,12 +33,23 @@ var __async = (__this, __arguments, generator) => {
|
|
33
33
|
});
|
34
34
|
};
|
35
35
|
const import_meta = {};
|
36
|
-
|
37
|
-
|
36
|
+
const TsRunner = {
|
37
|
+
TsNode: "ts-node",
|
38
|
+
EsbuildRegister: "esbuild-register",
|
39
|
+
EsbuildRunner: "esbuild-runner",
|
40
|
+
TSX: "tsx"
|
41
|
+
};
|
42
|
+
const {
|
43
|
+
SYNCKIT_BUFFER_SIZE,
|
44
|
+
SYNCKIT_TIMEOUT,
|
45
|
+
SYNCKIT_EXEC_ARV,
|
46
|
+
SYNCKIT_TS_RUNNER
|
47
|
+
} = process.env;
|
38
48
|
const DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE ? +SYNCKIT_BUFFER_SIZE : void 0;
|
39
49
|
const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : void 0;
|
40
50
|
const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024;
|
41
|
-
const DEFAULT_EXEC_ARGV = (
|
51
|
+
const DEFAULT_EXEC_ARGV = (SYNCKIT_EXEC_ARV == null ? void 0 : SYNCKIT_EXEC_ARV.split(",")) || [];
|
52
|
+
const DEFAULT_TS_RUNNER = SYNCKIT_TS_RUNNER || TsRunner.TsNode;
|
42
53
|
const syncFnCache = /* @__PURE__ */ new Map();
|
43
54
|
const extractProperties = (object) => {
|
44
55
|
if (object && typeof object === "object") {
|
@@ -61,16 +72,27 @@ function createSyncFn(workerPath, bufferSizeOrOptions, timeout) {
|
|
61
72
|
syncFnCache.set(workerPath, syncFn);
|
62
73
|
return syncFn;
|
63
74
|
}
|
64
|
-
const cjsRequire = typeof require === "undefined" ?
|
75
|
+
const cjsRequire = typeof require === "undefined" ? node_module.createRequire(import_meta.url) : require;
|
65
76
|
const dataUrl = (code) => new URL(`data:text/javascript,${encodeURIComponent(code)}`);
|
66
|
-
const
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
77
|
+
const setupTsRunner = (workerPath, { execArgv, tsRunner }) => {
|
78
|
+
const ext = path__default["default"].extname(workerPath);
|
79
|
+
if (!/[/\\]node_modules[/\\]/.test(workerPath) && (!ext || /^\.[cm]?js$/.test(ext))) {
|
80
|
+
const workPathWithoutExt = ext ? workerPath.slice(0, -ext.length) : workerPath;
|
81
|
+
let extensions;
|
82
|
+
switch (ext) {
|
83
|
+
case ".cjs":
|
84
|
+
extensions = [".cts", ".cjs"];
|
85
|
+
break;
|
86
|
+
case ".mjs":
|
87
|
+
extensions = [".mts", ".mjs"];
|
88
|
+
break;
|
89
|
+
default:
|
90
|
+
extensions = [".ts", ".js"];
|
91
|
+
break;
|
92
|
+
}
|
93
|
+
const found = utils.tryExtensions(workPathWithoutExt, extensions);
|
94
|
+
if (found && (!ext || found !== workPathWithoutExt)) {
|
95
|
+
workerPath = found;
|
74
96
|
}
|
75
97
|
}
|
76
98
|
const isTs = /\.[cm]?ts$/.test(workerPath);
|
@@ -82,11 +104,42 @@ const setupTsNode = (workerPath, execArgv) => {
|
|
82
104
|
tsUseEsm = cjsRequire(pkg).type === "module";
|
83
105
|
}
|
84
106
|
}
|
85
|
-
|
86
|
-
|
107
|
+
switch (tsRunner) {
|
108
|
+
case TsRunner.TsNode: {
|
109
|
+
if (tsUseEsm) {
|
110
|
+
if (!execArgv.includes("--loader")) {
|
111
|
+
execArgv = ["--loader", `${TsRunner.TsNode}/esm`, ...execArgv];
|
112
|
+
}
|
113
|
+
} else if (!execArgv.includes("-r")) {
|
114
|
+
execArgv = ["-r", `${TsRunner.TsNode}/register`, ...execArgv];
|
115
|
+
}
|
116
|
+
break;
|
117
|
+
}
|
118
|
+
case TsRunner.EsbuildRegister: {
|
119
|
+
if (!execArgv.includes("-r")) {
|
120
|
+
execArgv = ["-r", TsRunner.EsbuildRegister, ...execArgv];
|
121
|
+
}
|
122
|
+
break;
|
123
|
+
}
|
124
|
+
case TsRunner.EsbuildRunner: {
|
125
|
+
if (!execArgv.includes("-r")) {
|
126
|
+
execArgv = ["-r", `${TsRunner.EsbuildRunner}/register`, ...execArgv];
|
127
|
+
}
|
128
|
+
break;
|
129
|
+
}
|
130
|
+
case TsRunner.TSX: {
|
131
|
+
if (!execArgv.includes("--loader")) {
|
132
|
+
execArgv = ["--loader", TsRunner.TSX, ...execArgv];
|
133
|
+
}
|
134
|
+
break;
|
135
|
+
}
|
136
|
+
default: {
|
137
|
+
throw new Error(`Unknown ts runner: ${String(tsRunner)}`);
|
138
|
+
}
|
87
139
|
}
|
88
140
|
}
|
89
141
|
return {
|
142
|
+
ext,
|
90
143
|
isTs,
|
91
144
|
tsUseEsm,
|
92
145
|
workerPath,
|
@@ -96,17 +149,26 @@ const setupTsNode = (workerPath, execArgv) => {
|
|
96
149
|
function startWorkerThread(workerPath, {
|
97
150
|
bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
|
98
151
|
timeout = DEFAULT_TIMEOUT,
|
99
|
-
execArgv = DEFAULT_EXEC_ARGV
|
152
|
+
execArgv = DEFAULT_EXEC_ARGV,
|
153
|
+
tsRunner = DEFAULT_TS_RUNNER
|
100
154
|
} = {}) {
|
101
|
-
const { port1: mainPort, port2: workerPort } = new
|
155
|
+
const { port1: mainPort, port2: workerPort } = new node_worker_threads.MessageChannel();
|
102
156
|
const {
|
103
|
-
|
157
|
+
ext,
|
104
158
|
tsUseEsm,
|
105
159
|
workerPath: finalWorkerPath,
|
106
160
|
execArgv: finalExecArgv
|
107
|
-
} =
|
108
|
-
const
|
109
|
-
|
161
|
+
} = setupTsRunner(workerPath, { execArgv, tsRunner });
|
162
|
+
const workerPathUrl = node_url.pathToFileURL(finalWorkerPath);
|
163
|
+
if (/\.[cm]ts$/.test(finalWorkerPath) && [
|
164
|
+
TsRunner.EsbuildRegister,
|
165
|
+
TsRunner.EsbuildRunner
|
166
|
+
].includes(tsRunner)) {
|
167
|
+
throw new Error(`${tsRunner} is not supported for ${ext} files yet, you can try [tsx](https://github.com/esbuild-kit/tsx) instead`);
|
168
|
+
}
|
169
|
+
const worker = new node_worker_threads.Worker(tsUseEsm && [
|
170
|
+
TsRunner.TsNode
|
171
|
+
].includes(tsRunner) ? dataUrl(`import '${String(workerPathUrl)}'`) : workerPathUrl, {
|
110
172
|
workerData: { workerPort },
|
111
173
|
transferList: [workerPort],
|
112
174
|
execArgv: finalExecArgv
|
@@ -127,7 +189,7 @@ function startWorkerThread(workerPath, {
|
|
127
189
|
result,
|
128
190
|
error,
|
129
191
|
properties
|
130
|
-
} =
|
192
|
+
} = node_worker_threads.receiveMessageOnPort(mainPort).message;
|
131
193
|
if (id !== id2) {
|
132
194
|
throw new Error(`Internal error: Expected id ${id} but got id ${id2}`);
|
133
195
|
}
|
@@ -140,11 +202,11 @@ function startWorkerThread(workerPath, {
|
|
140
202
|
return syncFn;
|
141
203
|
}
|
142
204
|
function runAsWorker(fn) {
|
143
|
-
if (!
|
205
|
+
if (!node_worker_threads.workerData) {
|
144
206
|
return;
|
145
207
|
}
|
146
|
-
const { workerPort } =
|
147
|
-
|
208
|
+
const { workerPort } = node_worker_threads.workerData;
|
209
|
+
node_worker_threads.parentPort.on("message", ({ sharedBuffer, id, args }) => {
|
148
210
|
(() => __async(this, null, function* () {
|
149
211
|
const sharedBufferView = new Int32Array(sharedBuffer);
|
150
212
|
let msg;
|
@@ -167,7 +229,9 @@ function runAsWorker(fn) {
|
|
167
229
|
exports.DEFAULT_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
|
168
230
|
exports.DEFAULT_EXEC_ARGV = DEFAULT_EXEC_ARGV;
|
169
231
|
exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
|
232
|
+
exports.DEFAULT_TS_RUNNER = DEFAULT_TS_RUNNER;
|
170
233
|
exports.DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_WORKER_BUFFER_SIZE;
|
234
|
+
exports.TsRunner = TsRunner;
|
171
235
|
exports.createSyncFn = createSyncFn;
|
172
236
|
exports.extractProperties = extractProperties;
|
173
237
|
exports.runAsWorker = runAsWorker;
|
package/lib/index.d.ts
CHANGED
@@ -1,13 +1,22 @@
|
|
1
|
-
import { AnyAsyncFn, Syncify } from './types.js';
|
1
|
+
import { AnyAsyncFn, Syncify, ValueOf } from './types.js';
|
2
2
|
export * from './types.js';
|
3
|
+
export declare const TsRunner: {
|
4
|
+
readonly TsNode: "ts-node";
|
5
|
+
readonly EsbuildRegister: "esbuild-register";
|
6
|
+
readonly EsbuildRunner: "esbuild-runner";
|
7
|
+
readonly TSX: "tsx";
|
8
|
+
};
|
9
|
+
export declare type TsRunner = ValueOf<typeof TsRunner>;
|
3
10
|
export declare const DEFAULT_BUFFER_SIZE: number | undefined;
|
4
11
|
export declare const DEFAULT_TIMEOUT: number | undefined;
|
5
12
|
export declare const DEFAULT_WORKER_BUFFER_SIZE: number;
|
6
13
|
export declare const DEFAULT_EXEC_ARGV: string[];
|
14
|
+
export declare const DEFAULT_TS_RUNNER: TsRunner;
|
7
15
|
export interface SynckitOptions {
|
8
16
|
bufferSize?: number;
|
9
17
|
timeout?: number;
|
10
18
|
execArgv?: string[];
|
19
|
+
tsRunner?: TsRunner;
|
11
20
|
}
|
12
21
|
export declare const extractProperties: <T>(object?: T | undefined) => T | undefined;
|
13
22
|
export declare function createSyncFn<T extends AnyAsyncFn>(workerPath: string, bufferSize?: number, timeout?: number): Syncify<T>;
|
package/lib/index.js
CHANGED
@@ -1,19 +1,32 @@
|
|
1
|
-
var _a;
|
2
1
|
import { __awaiter } from "tslib";
|
3
|
-
import { createRequire } from 'module';
|
4
|
-
import path from 'path';
|
5
|
-
import { pathToFileURL } from 'url';
|
6
|
-
import { MessageChannel, Worker, receiveMessageOnPort,
|
2
|
+
import { createRequire } from 'node:module';
|
3
|
+
import path from 'node:path';
|
4
|
+
import { pathToFileURL } from 'node:url';
|
5
|
+
import { MessageChannel, Worker, receiveMessageOnPort,
|
6
|
+
// type-coverage:ignore-next-line -- we can't control
|
7
|
+
workerData, parentPort, } from 'node:worker_threads';
|
7
8
|
import { findUp, tryExtensions } from '@pkgr/utils';
|
8
9
|
export * from './types.js';
|
9
|
-
const
|
10
|
+
export const TsRunner = {
|
11
|
+
// https://github.com/TypeStrong/ts-node
|
12
|
+
TsNode: 'ts-node',
|
13
|
+
// https://github.com/egoist/esbuild-register
|
14
|
+
EsbuildRegister: 'esbuild-register',
|
15
|
+
// https://github.com/folke/esbuild-runner
|
16
|
+
EsbuildRunner: 'esbuild-runner',
|
17
|
+
// https://github.com/esbuild-kit/tsx
|
18
|
+
TSX: 'tsx',
|
19
|
+
};
|
20
|
+
const { SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT, SYNCKIT_EXEC_ARV, SYNCKIT_TS_RUNNER, } = process.env;
|
10
21
|
export const DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE
|
11
22
|
? +SYNCKIT_BUFFER_SIZE
|
12
23
|
: undefined;
|
13
24
|
export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : undefined;
|
14
25
|
export const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024;
|
15
26
|
/* istanbul ignore next */
|
16
|
-
export const DEFAULT_EXEC_ARGV = (
|
27
|
+
export const DEFAULT_EXEC_ARGV = (SYNCKIT_EXEC_ARV === null || SYNCKIT_EXEC_ARV === void 0 ? void 0 : SYNCKIT_EXEC_ARV.split(',')) || [];
|
28
|
+
export const DEFAULT_TS_RUNNER = (SYNCKIT_TS_RUNNER ||
|
29
|
+
TsRunner.TsNode);
|
17
30
|
const syncFnCache = new Map();
|
18
31
|
// MessagePort doesn't copy the properties of Error objects. We still want
|
19
32
|
// error objects to have extra properties such as "warnings" so implement the
|
@@ -46,20 +59,31 @@ const cjsRequire = typeof require === 'undefined'
|
|
46
59
|
? createRequire(import.meta.url)
|
47
60
|
: /* istanbul ignore next */ require;
|
48
61
|
const dataUrl = (code) => new URL(`data:text/javascript,${encodeURIComponent(code)}`);
|
49
|
-
|
50
|
-
const
|
51
|
-
if (!/[/\\]node_modules[/\\]/.test(workerPath)
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
62
|
+
const setupTsRunner = (workerPath, { execArgv, tsRunner }) => {
|
63
|
+
const ext = path.extname(workerPath);
|
64
|
+
if (!/[/\\]node_modules[/\\]/.test(workerPath) &&
|
65
|
+
(!ext || /^\.[cm]?js$/.test(ext))) {
|
66
|
+
const workPathWithoutExt = ext
|
67
|
+
? workerPath.slice(0, -ext.length)
|
68
|
+
: workerPath;
|
69
|
+
let extensions;
|
70
|
+
switch (ext) {
|
71
|
+
case '.cjs':
|
72
|
+
extensions = ['.cts', '.cjs'];
|
73
|
+
break;
|
74
|
+
case '.mjs':
|
75
|
+
extensions = ['.mts', '.mjs'];
|
76
|
+
break;
|
77
|
+
default:
|
78
|
+
extensions = ['.ts', '.js'];
|
79
|
+
break;
|
80
|
+
}
|
81
|
+
const found = tryExtensions(workPathWithoutExt, extensions);
|
82
|
+
if (found && (!ext || found !== workPathWithoutExt)) {
|
83
|
+
workerPath = found;
|
59
84
|
}
|
60
85
|
}
|
61
86
|
const isTs = /\.[cm]?ts$/.test(workerPath);
|
62
|
-
// TODO: it does not work for `ts-node` for now
|
63
87
|
let tsUseEsm = workerPath.endsWith('.mts');
|
64
88
|
if (isTs) {
|
65
89
|
if (!tsUseEsm) {
|
@@ -70,26 +94,72 @@ const setupTsNode = (workerPath, execArgv) => {
|
|
70
94
|
'module';
|
71
95
|
}
|
72
96
|
}
|
73
|
-
|
74
|
-
|
97
|
+
switch (tsRunner) {
|
98
|
+
case TsRunner.TsNode: {
|
99
|
+
if (tsUseEsm) {
|
100
|
+
if (!execArgv.includes('--loader')) {
|
101
|
+
execArgv = ['--loader', `${TsRunner.TsNode}/esm`, ...execArgv];
|
102
|
+
}
|
103
|
+
}
|
104
|
+
else if (!execArgv.includes('-r')) {
|
105
|
+
execArgv = ['-r', `${TsRunner.TsNode}/register`, ...execArgv];
|
106
|
+
}
|
107
|
+
break;
|
108
|
+
}
|
109
|
+
case TsRunner.EsbuildRegister: {
|
110
|
+
if (!execArgv.includes('-r')) {
|
111
|
+
execArgv = ['-r', TsRunner.EsbuildRegister, ...execArgv];
|
112
|
+
}
|
113
|
+
break;
|
114
|
+
}
|
115
|
+
case TsRunner.EsbuildRunner: {
|
116
|
+
if (!execArgv.includes('-r')) {
|
117
|
+
execArgv = ['-r', `${TsRunner.EsbuildRunner}/register`, ...execArgv];
|
118
|
+
}
|
119
|
+
break;
|
120
|
+
}
|
121
|
+
case TsRunner.TSX: {
|
122
|
+
if (!execArgv.includes('--loader')) {
|
123
|
+
execArgv = ['--loader', TsRunner.TSX, ...execArgv];
|
124
|
+
}
|
125
|
+
break;
|
126
|
+
}
|
127
|
+
default: {
|
128
|
+
throw new Error(`Unknown ts runner: ${String(tsRunner)}`);
|
129
|
+
}
|
75
130
|
}
|
76
131
|
}
|
77
132
|
return {
|
133
|
+
ext,
|
78
134
|
isTs,
|
79
135
|
tsUseEsm,
|
80
136
|
workerPath,
|
81
137
|
execArgv,
|
82
138
|
};
|
83
139
|
};
|
84
|
-
function startWorkerThread(workerPath, { bufferSize = DEFAULT_WORKER_BUFFER_SIZE, timeout = DEFAULT_TIMEOUT, execArgv = DEFAULT_EXEC_ARGV, } = {}) {
|
140
|
+
function startWorkerThread(workerPath, { bufferSize = DEFAULT_WORKER_BUFFER_SIZE, timeout = DEFAULT_TIMEOUT, execArgv = DEFAULT_EXEC_ARGV, tsRunner = DEFAULT_TS_RUNNER, } = {}) {
|
85
141
|
const { port1: mainPort, port2: workerPort } = new MessageChannel();
|
86
|
-
const {
|
87
|
-
const
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
142
|
+
const { ext, tsUseEsm, workerPath: finalWorkerPath, execArgv: finalExecArgv, } = setupTsRunner(workerPath, { execArgv, tsRunner });
|
143
|
+
const workerPathUrl = pathToFileURL(finalWorkerPath);
|
144
|
+
if (/\.[cm]ts$/.test(finalWorkerPath) &&
|
145
|
+
[
|
146
|
+
// https://github.com/egoist/esbuild-register/issues/79
|
147
|
+
TsRunner.EsbuildRegister,
|
148
|
+
// https://github.com/folke/esbuild-runner/issues/67
|
149
|
+
TsRunner.EsbuildRunner,
|
150
|
+
].includes(tsRunner)) {
|
151
|
+
throw new Error(`${tsRunner} is not supported for ${ext} files yet, you can try [tsx](https://github.com/esbuild-kit/tsx) instead`);
|
152
|
+
}
|
153
|
+
const worker = new Worker(tsUseEsm &&
|
154
|
+
[
|
155
|
+
TsRunner.TsNode,
|
156
|
+
// https://github.com/egoist/esbuild-register/issues/79
|
157
|
+
// TsRunner.EsbuildRegister,
|
158
|
+
// https://github.com/folke/esbuild-runner/issues/67
|
159
|
+
// TsRunner.EsbuildRunner
|
160
|
+
].includes(tsRunner)
|
161
|
+
? dataUrl(`import '${String(workerPathUrl)}'`)
|
162
|
+
: workerPathUrl, {
|
93
163
|
workerData: { workerPort },
|
94
164
|
transferList: [workerPort],
|
95
165
|
execArgv: finalExecArgv,
|
@@ -122,6 +192,7 @@ function startWorkerThread(workerPath, { bufferSize = DEFAULT_WORKER_BUFFER_SIZE
|
|
122
192
|
}
|
123
193
|
/* istanbul ignore next */
|
124
194
|
export function runAsWorker(fn) {
|
195
|
+
// type-coverage:ignore-next-line -- we can't control
|
125
196
|
if (!workerData) {
|
126
197
|
return;
|
127
198
|
}
|
package/lib/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,EACL,cAAc,EACd,MAAM,EACN,oBAAoB;AACpB,qDAAqD;AACrD,UAAU,EACV,UAAU,GACX,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAYnD,cAAc,YAAY,CAAA;AAE1B,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB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,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,EAAE,CAAA;aACxC;YAAC,OAAO,KAAc,EAAE;gBACvB,GAAG,GAAG;oBACJ,EAAE;oBACF,KAAK;oBACL,UAAU,EAAE,iBAAiB,CAAC,KAAK,CAAC;iBACrC,CAAA;aACF;YACD,UAAU,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;YAC3B,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;YACnC,OAAO,CAAC,MAAM,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAA;QACrC,CAAC,CAAA,CAAC,EAAE,CAAA;IACN,CAAC,CACF,CAAA;AACH,CAAC"}
|
package/lib/types.d.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
-
import { MessagePort } from 'worker_threads';
|
2
|
+
import { MessagePort } from 'node:worker_threads';
|
3
3
|
export declare type AnyFn<R = any, T extends any[] = any[]> = (...args: T) => R;
|
4
4
|
export declare type AnyPromise<T = any> = Promise<T>;
|
5
5
|
export declare type AnyAsyncFn<T = any> = AnyFn<Promise<T>>;
|
6
6
|
export declare type Syncify<T extends AnyAsyncFn> = T extends (...args: infer Args) => Promise<infer R> ? (...args: Args) => R : never;
|
7
7
|
export declare type PromiseType<T extends AnyPromise> = T extends Promise<infer R> ? R : never;
|
8
|
+
export declare type ValueOf<T> = T[keyof T];
|
8
9
|
export interface MainToWorkerMessage<T extends unknown[]> {
|
9
10
|
sharedBuffer: SharedArrayBuffer;
|
10
11
|
id: number;
|
package/package.json
CHANGED
@@ -1,13 +1,45 @@
|
|
1
1
|
{
|
2
2
|
"name": "synckit",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.8.0",
|
4
4
|
"type": "module",
|
5
|
-
"description": "Perform async work synchronously in Node.js using `worker_threads
|
5
|
+
"description": "Perform async work synchronously in Node.js using `worker_threads` with first-class TypeScript support.",
|
6
6
|
"repository": "git+https://github.com/rx-ts/synckit.git",
|
7
|
-
"author": "JounQin <admin@1stg.me>",
|
7
|
+
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
|
8
|
+
"donate": {
|
9
|
+
"recipients": [
|
10
|
+
{
|
11
|
+
"name": "unts",
|
12
|
+
"platform": "opencollective",
|
13
|
+
"address": "https://opencollective.com/unts",
|
14
|
+
"weight": 60
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"name": "rxts",
|
18
|
+
"platform": "opencollective",
|
19
|
+
"address": "https://opencollective.com/rxts",
|
20
|
+
"weight": 20
|
21
|
+
},
|
22
|
+
{
|
23
|
+
"name": "1stG",
|
24
|
+
"email": "i@1stg.me",
|
25
|
+
"weight": 20,
|
26
|
+
"platforms": [
|
27
|
+
{
|
28
|
+
"platform": "opencollective",
|
29
|
+
"address": "https://opencollective.com/1stG"
|
30
|
+
},
|
31
|
+
{
|
32
|
+
"platform": "patreon",
|
33
|
+
"address": "https://www.patreon.com/1stG"
|
34
|
+
}
|
35
|
+
]
|
36
|
+
}
|
37
|
+
]
|
38
|
+
},
|
39
|
+
"funding": "https://opencollective.com/unts",
|
8
40
|
"license": "MIT",
|
9
41
|
"engines": {
|
10
|
-
"node": ">=
|
42
|
+
"node": "^14.18.0 || >=16.0.0"
|
11
43
|
},
|
12
44
|
"main": "./lib/index.cjs",
|
13
45
|
"module": "./lib/index.js",
|
@@ -32,7 +64,7 @@
|
|
32
64
|
"synckit"
|
33
65
|
],
|
34
66
|
"dependencies": {
|
35
|
-
"@pkgr/utils": "^2.
|
67
|
+
"@pkgr/utils": "^2.3.0",
|
36
68
|
"tslib": "^2.4.0"
|
37
69
|
}
|
38
70
|
}
|