synckit 0.4.1 → 0.6.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/CHANGELOG.md +11 -3
- package/README.md +5 -10
- package/lib/index.cjs +33 -104
- package/lib/index.d.ts +8 -6
- package/lib/index.js +29 -100
- package/lib/index.js.map +1 -1
- package/package.json +17 -19
package/CHANGELOG.md
CHANGED
@@ -1,10 +1,18 @@
|
|
1
1
|
# synckit
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.6.0
|
4
4
|
|
5
|
-
###
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- [`18106a5`](https://github.com/rx-ts/synckit/commit/18106a511c9dfc21a9ae19c70ab19fb3b71fcbf5) Thanks [@JounQin](https://github.com/JounQin)! - feat: add custom `execArgv` support
|
8
|
+
|
9
|
+
close #55
|
10
|
+
|
11
|
+
## 0.5.0
|
12
|
+
|
13
|
+
### Minor Changes
|
6
14
|
|
7
|
-
- [`
|
15
|
+
- [#47](https://github.com/rx-ts/synckit/pull/47) [`a362982`](https://github.com/rx-ts/synckit/commit/a362982eac4083ff3d00b4f6a9d4f4183dd2418e) Thanks [@JounQin](https://github.com/JounQin)! - feat: drop child_process
|
8
16
|
|
9
17
|
## 0.4.0
|
10
18
|
|
package/README.md
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
[](https://standardjs.com)
|
17
17
|
[](https://github.com/prettier/prettier)
|
18
18
|
|
19
|
-
Perform async work synchronously in Node.js using `worker_threads
|
19
|
+
Perform async work synchronously in Node.js using `worker_threads` with first-class TypeScript support.
|
20
20
|
|
21
21
|
## TOC <!-- omit in toc -->
|
22
22
|
|
@@ -43,8 +43,6 @@ npm i synckit
|
|
43
43
|
|
44
44
|
### API
|
45
45
|
|
46
|
-
`worker_threads` is used by default for performance, if you have any problem with it, you can set env `SYNCKIT_WORKER_THREADS=0` to disable it and fallback to previously `child_process` solution, and please raise an issue here so that we can improve it.
|
47
|
-
|
48
46
|
```js
|
49
47
|
// runner.js
|
50
48
|
import { createSyncFn } from 'synckit'
|
@@ -66,16 +64,13 @@ runAsWorker(async (...args) => {
|
|
66
64
|
})
|
67
65
|
```
|
68
66
|
|
69
|
-
You must make sure
|
70
|
-
|
71
|
-
1. if `worker_threads` is enabled (by default), the `result` is serialized by [`Structured Clone Algorithm`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
72
|
-
2. if `child_process` is used, the `result` is serialized by `JSON.stringify`
|
67
|
+
You must make sure, the `result` is serialized by [`Structured Clone Algorithm`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
73
68
|
|
74
69
|
### Envs
|
75
70
|
|
76
|
-
1. `
|
77
|
-
2. `
|
78
|
-
3. `
|
71
|
+
1. `SYNCKIT_BUFFER_SIZE`: `bufferSize` to create `SharedArrayBuffer` for `worker_threads` (default as `1024`)
|
72
|
+
2. `SYNCKIT_TIMEOUT`: `timeout` for performing the async job (no default)
|
73
|
+
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)
|
79
74
|
|
80
75
|
### TypeScript
|
81
76
|
|
package/lib/index.cjs
CHANGED
@@ -2,37 +2,13 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
var child_process = require('child_process');
|
6
|
-
var os = require('os');
|
7
5
|
var path = require('path');
|
8
|
-
var
|
9
|
-
var module$1 = require('module');
|
10
|
-
var uuid = require('uuid');
|
6
|
+
var worker_threads = require('worker_threads');
|
11
7
|
|
12
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
13
9
|
|
14
10
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
15
|
-
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
16
11
|
|
17
|
-
var __defProp = Object.defineProperty;
|
18
|
-
var __defProps = Object.defineProperties;
|
19
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
20
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
21
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
22
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
23
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
24
|
-
var __spreadValues = (a, b) => {
|
25
|
-
for (var prop in b || (b = {}))
|
26
|
-
if (__hasOwnProp.call(b, prop))
|
27
|
-
__defNormalProp(a, prop, b[prop]);
|
28
|
-
if (__getOwnPropSymbols)
|
29
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
30
|
-
if (__propIsEnum.call(b, prop))
|
31
|
-
__defNormalProp(a, prop, b[prop]);
|
32
|
-
}
|
33
|
-
return a;
|
34
|
-
};
|
35
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
36
12
|
var __async = (__this, __arguments, generator) => {
|
37
13
|
return new Promise((resolve, reject) => {
|
38
14
|
var fulfilled = (value) => {
|
@@ -53,78 +29,46 @@ var __async = (__this, __arguments, generator) => {
|
|
53
29
|
step((generator = generator.apply(__this, __arguments)).next());
|
54
30
|
});
|
55
31
|
};
|
56
|
-
|
57
|
-
const tmpdir = fs__default['default'].realpathSync(os.tmpdir());
|
58
|
-
let workerThreads;
|
59
|
-
const cjsRequire = typeof require === "undefined" ? module$1.createRequire(import_meta.url) : require;
|
60
|
-
try {
|
61
|
-
workerThreads = cjsRequire("worker_threads");
|
62
|
-
if (typeof workerThreads.receiveMessageOnPort !== "function") {
|
63
|
-
workerThreads = void 0;
|
64
|
-
}
|
65
|
-
} catch (e) {
|
66
|
-
}
|
67
|
-
if (!workerThreads) {
|
68
|
-
console.warn("[synckit]: `worker_threads` or `receiveMessageOnPort` is not available in current environment, `Node >= 12` is required");
|
69
|
-
}
|
32
|
+
var _a;
|
70
33
|
const {
|
71
|
-
SYNCKIT_WORKER_THREADS,
|
72
34
|
SYNCKIT_BUFFER_SIZE,
|
73
35
|
SYNCKIT_TIMEOUT,
|
74
|
-
SYNCKIT_TS_ESM
|
36
|
+
SYNCKIT_TS_ESM,
|
37
|
+
SYNCKIT_EXEC_ARV
|
75
38
|
} = process.env;
|
76
|
-
const useWorkerThreads = !!workerThreads && (!SYNCKIT_WORKER_THREADS || !["0", "false"].includes(SYNCKIT_WORKER_THREADS));
|
77
39
|
const TS_USE_ESM = !!SYNCKIT_TS_ESM && ["1", "true"].includes(SYNCKIT_TS_ESM);
|
78
40
|
const DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE ? +SYNCKIT_BUFFER_SIZE : void 0;
|
79
41
|
const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : void 0;
|
80
42
|
const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024;
|
43
|
+
const DEFAULT_EXEC_ARGV = (_a = SYNCKIT_EXEC_ARV == null ? void 0 : SYNCKIT_EXEC_ARV.split(",")) != null ? _a : [];
|
81
44
|
const syncFnCache = new Map();
|
82
|
-
function createSyncFn(workerPath,
|
83
|
-
if (!path__default[
|
45
|
+
function createSyncFn(workerPath, bufferSizeOrOptions, timeout) {
|
46
|
+
if (!path__default["default"].isAbsolute(workerPath)) {
|
84
47
|
throw new Error("`workerPath` must be absolute");
|
85
48
|
}
|
86
49
|
const cachedSyncFn = syncFnCache.get(workerPath);
|
87
50
|
if (cachedSyncFn) {
|
88
51
|
return cachedSyncFn;
|
89
52
|
}
|
90
|
-
const syncFn = (
|
53
|
+
const syncFn = startWorkerThread(workerPath, typeof bufferSizeOrOptions === "number" ? { bufferSize: bufferSizeOrOptions, timeout } : bufferSizeOrOptions);
|
91
54
|
syncFnCache.set(workerPath, syncFn);
|
92
55
|
return syncFn;
|
93
56
|
}
|
94
|
-
function startChildProcess(workerPath, bufferSize = DEFAULT_BUFFER_SIZE, timeout) {
|
95
|
-
const executor = workerPath.endsWith(".ts") ? TS_USE_ESM ? "node --loader ts-node/esm" : "ts-node" : "node";
|
96
|
-
return (...args) => {
|
97
|
-
const filename = path__default['default'].resolve(tmpdir, `synckit-${uuid.v4()}.json`);
|
98
|
-
fs__default['default'].writeFileSync(filename, JSON.stringify(args));
|
99
|
-
const command = `${executor} ${workerPath} ${filename}`;
|
100
|
-
try {
|
101
|
-
child_process.execSync(command, {
|
102
|
-
stdio: "inherit",
|
103
|
-
maxBuffer: bufferSize,
|
104
|
-
timeout
|
105
|
-
});
|
106
|
-
const { result, error } = JSON.parse(fs__default['default'].readFileSync(filename, "utf8"));
|
107
|
-
if (error) {
|
108
|
-
throw typeof error === "object" && "message" in error ? Object.assign(new Error(), error) : error;
|
109
|
-
}
|
110
|
-
return result;
|
111
|
-
} finally {
|
112
|
-
fs__default['default'].unlinkSync(filename);
|
113
|
-
}
|
114
|
-
};
|
115
|
-
}
|
116
57
|
const throwError = (msg) => {
|
117
58
|
throw new Error(msg);
|
118
59
|
};
|
119
|
-
function startWorkerThread(workerPath,
|
120
|
-
|
121
|
-
|
60
|
+
function startWorkerThread(workerPath, {
|
61
|
+
bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
|
62
|
+
timeout = DEFAULT_TIMEOUT,
|
63
|
+
execArgv = DEFAULT_EXEC_ARGV
|
64
|
+
} = {}) {
|
65
|
+
const { port1: mainPort, port2: workerPort } = new worker_threads.MessageChannel();
|
122
66
|
const isTs = workerPath.endsWith(".ts");
|
123
|
-
const worker = new Worker(isTs ? TS_USE_ESM ? throwError("Native esm in `.ts` file is not supported yet, please use `.cjs` instead") : `require('ts-node/register');require('${workerPath}')` : workerPath, {
|
67
|
+
const worker = new worker_threads.Worker(isTs ? TS_USE_ESM ? throwError("Native esm in `.ts` file is not supported yet, please use `.cjs` instead") : `require('ts-node/register');require('${workerPath}')` : workerPath, {
|
124
68
|
eval: isTs,
|
125
69
|
workerData: { workerPort },
|
126
70
|
transferList: [workerPort],
|
127
|
-
execArgv
|
71
|
+
execArgv
|
128
72
|
});
|
129
73
|
let nextID = 0;
|
130
74
|
const syncFn = (...args) => {
|
@@ -141,7 +85,7 @@ function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
|
|
141
85
|
id: id2,
|
142
86
|
result,
|
143
87
|
error
|
144
|
-
} =
|
88
|
+
} = worker_threads.receiveMessageOnPort(mainPort).message;
|
145
89
|
if (id !== id2) {
|
146
90
|
throw new Error(`Internal error: Expected id ${id} but got id ${id2}`);
|
147
91
|
}
|
@@ -154,47 +98,32 @@ function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
|
|
154
98
|
return syncFn;
|
155
99
|
}
|
156
100
|
function runAsWorker(fn) {
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
101
|
+
if (!worker_threads.workerData) {
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
const { workerPort } = worker_threads.workerData;
|
105
|
+
worker_threads.parentPort.on("message", ({ sharedBuffer, id, args }) => {
|
106
|
+
(() => __async(this, null, function* () {
|
107
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
162
108
|
let msg;
|
163
109
|
try {
|
164
|
-
msg = { result: yield fn(...args) };
|
165
|
-
} catch (
|
110
|
+
msg = { id, result: yield fn(...args) };
|
111
|
+
} catch (error) {
|
166
112
|
msg = {
|
167
|
-
|
113
|
+
id,
|
114
|
+
error
|
168
115
|
};
|
169
116
|
}
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
workerThreads.parentPort.on("message", ({ sharedBuffer, id, args }) => {
|
175
|
-
(() => __async(this, null, function* () {
|
176
|
-
const sharedBufferView = new Int32Array(sharedBuffer);
|
177
|
-
let msg;
|
178
|
-
try {
|
179
|
-
msg = { id, result: yield fn(...args) };
|
180
|
-
} catch (error) {
|
181
|
-
msg = {
|
182
|
-
id,
|
183
|
-
error
|
184
|
-
};
|
185
|
-
}
|
186
|
-
workerPort.postMessage(msg);
|
187
|
-
Atomics.add(sharedBufferView, 0, 1);
|
188
|
-
Atomics.notify(sharedBufferView, 0);
|
189
|
-
}))();
|
190
|
-
});
|
117
|
+
workerPort.postMessage(msg);
|
118
|
+
Atomics.add(sharedBufferView, 0, 1);
|
119
|
+
Atomics.notify(sharedBufferView, 0);
|
120
|
+
}))();
|
191
121
|
});
|
192
122
|
}
|
193
123
|
|
194
124
|
exports.DEFAULT_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
|
125
|
+
exports.DEFAULT_EXEC_ARGV = DEFAULT_EXEC_ARGV;
|
195
126
|
exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
|
196
127
|
exports.DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_WORKER_BUFFER_SIZE;
|
197
128
|
exports.createSyncFn = createSyncFn;
|
198
129
|
exports.runAsWorker = runAsWorker;
|
199
|
-
exports.tmpdir = tmpdir;
|
200
|
-
exports.useWorkerThreads = useWorkerThreads;
|
package/lib/index.d.ts
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
import { AnyAsyncFn, Syncify } from './types.js';
|
2
2
|
export * from './types.js';
|
3
|
-
/**
|
4
|
-
* @link https://github.com/sindresorhus/temp-dir/blob/main/index.js#L9
|
5
|
-
*/
|
6
|
-
export declare const tmpdir: string;
|
7
|
-
export declare const useWorkerThreads: boolean;
|
8
3
|
export declare const DEFAULT_BUFFER_SIZE: number | undefined;
|
9
4
|
export declare const DEFAULT_TIMEOUT: number | undefined;
|
10
5
|
export declare const DEFAULT_WORKER_BUFFER_SIZE: number;
|
6
|
+
export declare const DEFAULT_EXEC_ARGV: string[];
|
7
|
+
export interface SynckitOptions {
|
8
|
+
bufferSize?: number;
|
9
|
+
timeout?: number;
|
10
|
+
execArgv?: string[];
|
11
|
+
}
|
11
12
|
export declare function createSyncFn<T extends AnyAsyncFn>(workerPath: string, bufferSize?: number, timeout?: number): Syncify<T>;
|
12
|
-
export declare function
|
13
|
+
export declare function createSyncFn<T extends AnyAsyncFn>(workerPath: string, options?: SynckitOptions): Syncify<T>;
|
14
|
+
export declare function runAsWorker<R = unknown, T extends AnyAsyncFn<R> = AnyAsyncFn<R>>(fn: T): void;
|
package/lib/index.js
CHANGED
@@ -1,42 +1,18 @@
|
|
1
|
+
var _a;
|
1
2
|
import { __awaiter } from "tslib";
|
2
|
-
import { execSync } from 'child_process';
|
3
|
-
import { tmpdir as _tmpdir } from 'os';
|
4
3
|
import path from 'path';
|
5
|
-
import
|
6
|
-
import { createRequire } from 'module';
|
7
|
-
import { v4 as uuid } from 'uuid';
|
4
|
+
import { MessageChannel, Worker, receiveMessageOnPort, workerData, parentPort, } from 'worker_threads';
|
8
5
|
export * from './types.js';
|
9
|
-
|
10
|
-
* @link https://github.com/sindresorhus/temp-dir/blob/main/index.js#L9
|
11
|
-
*/
|
12
|
-
export const tmpdir = fs.realpathSync(_tmpdir());
|
13
|
-
let workerThreads;
|
14
|
-
const cjsRequire =
|
15
|
-
/* istanbul ignore next */
|
16
|
-
typeof require === 'undefined' ? createRequire(import.meta.url) : require;
|
17
|
-
try {
|
18
|
-
workerThreads = cjsRequire('worker_threads');
|
19
|
-
/* istanbul ignore if */
|
20
|
-
if (typeof workerThreads.receiveMessageOnPort !== 'function') {
|
21
|
-
workerThreads = undefined;
|
22
|
-
}
|
23
|
-
}
|
24
|
-
catch (_a) { }
|
25
|
-
/* istanbul ignore if */
|
26
|
-
if (!workerThreads) {
|
27
|
-
console.warn('[synckit]: `worker_threads` or `receiveMessageOnPort` is not available in current environment, `Node >= 12` is required');
|
28
|
-
}
|
29
|
-
const { SYNCKIT_WORKER_THREADS, SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT, SYNCKIT_TS_ESM, } = process.env;
|
30
|
-
export const useWorkerThreads = !!workerThreads &&
|
31
|
-
(!SYNCKIT_WORKER_THREADS || !['0', 'false'].includes(SYNCKIT_WORKER_THREADS));
|
6
|
+
const { SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT, SYNCKIT_TS_ESM, SYNCKIT_EXEC_ARV, } = process.env;
|
32
7
|
const TS_USE_ESM = !!SYNCKIT_TS_ESM && ['1', 'true'].includes(SYNCKIT_TS_ESM);
|
33
8
|
export const DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE
|
34
9
|
? +SYNCKIT_BUFFER_SIZE
|
35
10
|
: undefined;
|
36
11
|
export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : undefined;
|
37
12
|
export const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024;
|
13
|
+
export const DEFAULT_EXEC_ARGV = (_a = SYNCKIT_EXEC_ARV === null || SYNCKIT_EXEC_ARV === void 0 ? void 0 : SYNCKIT_EXEC_ARV.split(',')) !== null && _a !== void 0 ? _a : [];
|
38
14
|
const syncFnCache = new Map();
|
39
|
-
export function createSyncFn(workerPath,
|
15
|
+
export function createSyncFn(workerPath, bufferSizeOrOptions, timeout) {
|
40
16
|
if (!path.isAbsolute(workerPath)) {
|
41
17
|
throw new Error('`workerPath` must be absolute');
|
42
18
|
}
|
@@ -44,45 +20,16 @@ export function createSyncFn(workerPath, bufferSize, timeout = DEFAULT_TIMEOUT)
|
|
44
20
|
if (cachedSyncFn) {
|
45
21
|
return cachedSyncFn;
|
46
22
|
}
|
47
|
-
const syncFn =
|
23
|
+
const syncFn = startWorkerThread(workerPath, typeof bufferSizeOrOptions === 'number'
|
24
|
+
? { bufferSize: bufferSizeOrOptions, timeout }
|
25
|
+
: bufferSizeOrOptions);
|
48
26
|
syncFnCache.set(workerPath, syncFn);
|
49
27
|
return syncFn;
|
50
28
|
}
|
51
|
-
function startChildProcess(workerPath, bufferSize = DEFAULT_BUFFER_SIZE, timeout) {
|
52
|
-
const executor = workerPath.endsWith('.ts')
|
53
|
-
? TS_USE_ESM
|
54
|
-
? 'node --loader ts-node/esm'
|
55
|
-
: 'ts-node'
|
56
|
-
: 'node';
|
57
|
-
return (...args) => {
|
58
|
-
const filename = path.resolve(tmpdir, `synckit-${uuid()}.json`);
|
59
|
-
fs.writeFileSync(filename, JSON.stringify(args));
|
60
|
-
const command = `${executor} ${workerPath} ${filename}`;
|
61
|
-
try {
|
62
|
-
execSync(command, {
|
63
|
-
stdio: 'inherit',
|
64
|
-
maxBuffer: bufferSize,
|
65
|
-
timeout,
|
66
|
-
});
|
67
|
-
const { result, error } = JSON.parse(fs.readFileSync(filename, 'utf8'));
|
68
|
-
if (error) {
|
69
|
-
throw typeof error === 'object' && 'message' in error
|
70
|
-
? // eslint-disable-next-line unicorn/error-message
|
71
|
-
Object.assign(new Error(), error)
|
72
|
-
: error;
|
73
|
-
}
|
74
|
-
return result;
|
75
|
-
}
|
76
|
-
finally {
|
77
|
-
fs.unlinkSync(filename);
|
78
|
-
}
|
79
|
-
};
|
80
|
-
}
|
81
29
|
const throwError = (msg) => {
|
82
30
|
throw new Error(msg);
|
83
31
|
};
|
84
|
-
function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE, timeout) {
|
85
|
-
const { MessageChannel, Worker } = workerThreads;
|
32
|
+
function startWorkerThread(workerPath, { bufferSize = DEFAULT_WORKER_BUFFER_SIZE, timeout = DEFAULT_TIMEOUT, execArgv = DEFAULT_EXEC_ARGV, } = {}) {
|
86
33
|
const { port1: mainPort, port2: workerPort } = new MessageChannel();
|
87
34
|
const isTs = workerPath.endsWith('.ts');
|
88
35
|
const worker = new Worker(isTs
|
@@ -93,7 +40,7 @@ function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
|
|
93
40
|
eval: isTs,
|
94
41
|
workerData: { workerPort },
|
95
42
|
transferList: [workerPort],
|
96
|
-
execArgv
|
43
|
+
execArgv,
|
97
44
|
});
|
98
45
|
let nextID = 0;
|
99
46
|
const syncFn = (...args) => {
|
@@ -107,8 +54,7 @@ function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
|
|
107
54
|
if (!['ok', 'not-equal'].includes(status)) {
|
108
55
|
throw new Error('Internal error: Atomics.wait() failed: ' + status);
|
109
56
|
}
|
110
|
-
const { id: id2, result, error, } =
|
111
|
-
.message;
|
57
|
+
const { id: id2, result, error, } = receiveMessageOnPort(mainPort).message;
|
112
58
|
/* istanbul ignore if */
|
113
59
|
if (id !== id2) {
|
114
60
|
throw new Error(`Internal error: Expected id ${id} but got id ${id2}`);
|
@@ -121,48 +67,31 @@ function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
|
|
121
67
|
worker.unref();
|
122
68
|
return syncFn;
|
123
69
|
}
|
70
|
+
/* istanbul ignore next */
|
124
71
|
export function runAsWorker(fn) {
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
72
|
+
if (!workerData) {
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
const { workerPort } = workerData;
|
76
|
+
parentPort.on('message', ({ sharedBuffer, id, args }) => {
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
78
|
+
;
|
79
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
80
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
130
81
|
let msg;
|
131
82
|
try {
|
132
|
-
msg = { result: yield fn(...args) };
|
83
|
+
msg = { id, result: yield fn(...args) };
|
133
84
|
}
|
134
|
-
catch (
|
85
|
+
catch (error) {
|
135
86
|
msg = {
|
136
|
-
|
137
|
-
|
87
|
+
id,
|
88
|
+
error,
|
138
89
|
};
|
139
90
|
}
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
const { workerPort } = workerThreads.workerData;
|
145
|
-
/* istanbul ignore next */
|
146
|
-
workerThreads.parentPort.on('message', ({ sharedBuffer, id, args }) => {
|
147
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
148
|
-
;
|
149
|
-
(() => __awaiter(this, void 0, void 0, function* () {
|
150
|
-
const sharedBufferView = new Int32Array(sharedBuffer);
|
151
|
-
let msg;
|
152
|
-
try {
|
153
|
-
msg = { id, result: yield fn(...args) };
|
154
|
-
}
|
155
|
-
catch (error) {
|
156
|
-
msg = {
|
157
|
-
id,
|
158
|
-
error,
|
159
|
-
};
|
160
|
-
}
|
161
|
-
workerPort.postMessage(msg);
|
162
|
-
Atomics.add(sharedBufferView, 0, 1);
|
163
|
-
Atomics.notify(sharedBufferView, 0);
|
164
|
-
}))();
|
165
|
-
});
|
91
|
+
workerPort.postMessage(msg);
|
92
|
+
Atomics.add(sharedBufferView, 0, 1);
|
93
|
+
Atomics.notify(sharedBufferView, 0);
|
94
|
+
}))();
|
166
95
|
});
|
167
96
|
}
|
168
97
|
//# sourceMappingURL=index.js.map
|
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,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EACL,cAAc,EACd,MAAM,EACN,oBAAoB,EACpB,UAAU,EACV,UAAU,GACX,MAAM,gBAAgB,CAAA;AAWvB,cAAc,YAAY,CAAA;AAE1B,MAAM,EACJ,mBAAmB,EACnB,eAAe,EACf,cAAc,EACd,gBAAgB,GACjB,GAAG,OAAO,CAAC,GAAG,CAAA;AAEf,MAAM,UAAU,GAAG,CAAC,CAAC,cAAc,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAA;AAE7E,MAAM,CAAC,MAAM,mBAAmB,GAAG,mBAAmB;IACpD,CAAC,CAAC,CAAC,mBAAmB;IACtB,CAAC,CAAC,SAAS,CAAA;AAEb,MAAM,CAAC,MAAM,eAAe,GAAG,eA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+BAA+B,EAAE,eAAe,GAAG,EAAE,CAAC,CAAA;SACvE;QAED,IAAI,KAAK,EAAE;YACT,MAAM,KAAK,CAAA;SACZ;QAED,OAAO,MAAO,CAAA;IAChB,CAAC,CAAA;IAED,MAAM,CAAC,KAAK,EAAE,CAAA;IAEd,OAAO,MAAM,CAAA;AACf,CAAC;AAED,0BAA0B;AAC1B,MAAM,UAAU,WAAW,CAGzB,EAAK;IACL,IAAI,CAAC,UAAU,EAAE;QACf,OAAM;KACP;IAED,MAAM,EAAE,UAAU,EAAE,GAAG,UAAwB,CAAA;IAC/C,UAAW,CAAC,EAAE,CACZ,SAAS,EACT,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,IAAI,EAAsC,EAAE,EAAE;QACjE,mEAAmE;QACnE,CAAC;QAAA,CAAC,GAAS,EAAE;YACX,MAAM,gBAAgB,GAAG,IAAI,UAAU,CAAC,YAAY,CAAC,CAAA;YACrD,IAAI,GAA2B,CAAA;YAC/B,IAAI;gBACF,GAAG,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;iBACN,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/package.json
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "synckit",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.6.0",
|
4
4
|
"type": "module",
|
5
5
|
"description": "Perform async work synchronously in Node.js using `worker_threads`, or `child_process` as fallback, with first-class TypeScript support.",
|
6
6
|
"repository": "git+https://github.com/rx-ts/synckit.git",
|
7
7
|
"author": "JounQin <admin@1stg.me>",
|
8
8
|
"license": "MIT",
|
9
9
|
"engines": {
|
10
|
-
"node": ">=12"
|
10
|
+
"node": ">=12.3"
|
11
11
|
},
|
12
12
|
"main": "./lib/index.cjs",
|
13
13
|
"module": "./lib/index.js",
|
@@ -22,6 +22,7 @@
|
|
22
22
|
],
|
23
23
|
"keywords": [
|
24
24
|
"deasync",
|
25
|
+
"make-synchronous",
|
25
26
|
"sync",
|
26
27
|
"sync-exec",
|
27
28
|
"sync-rpc",
|
@@ -45,35 +46,32 @@
|
|
45
46
|
"lint:tsc": "tsc --noEmit",
|
46
47
|
"prepare": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
|
47
48
|
"prerelease": "npm run build",
|
48
|
-
"pretest": "yarn build
|
49
|
+
"pretest": "yarn build",
|
49
50
|
"release": "clean-publish && changeset publish",
|
50
51
|
"test": "yarn jest",
|
51
52
|
"typecov": "type-coverage"
|
52
53
|
},
|
53
54
|
"dependencies": {
|
54
|
-
"tslib": "^2.3.1"
|
55
|
-
"uuid": "^8.3.2"
|
55
|
+
"tslib": "^2.3.1"
|
56
56
|
},
|
57
57
|
"devDependencies": {
|
58
|
-
"@1stg/lib-config": "^4.
|
59
|
-
"@changesets/changelog-github": "^0.4.
|
60
|
-
"@changesets/cli": "^2.
|
61
|
-
"@types/jest": "^27.0.
|
62
|
-
"@types/node": "^16.
|
58
|
+
"@1stg/lib-config": "^4.1.2",
|
59
|
+
"@changesets/changelog-github": "^0.4.1",
|
60
|
+
"@changesets/cli": "^2.17.0",
|
61
|
+
"@types/jest": "^27.0.2",
|
62
|
+
"@types/node": "^16.11.0",
|
63
63
|
"@types/uuid": "^8.3.1",
|
64
|
-
"clean-publish": "^
|
64
|
+
"clean-publish": "^3.4.1",
|
65
65
|
"deasync": "^0.1.23",
|
66
|
-
"enhanced-resolve": "^5.8.2",
|
67
|
-
"postcss": "^8.3.6",
|
68
66
|
"sync-threads": "^1.0.1",
|
69
67
|
"ts-expect": "^1.3.0",
|
70
|
-
"ts-jest": "^27.0.
|
71
|
-
"ts-node": "^10.
|
72
|
-
"type-coverage": "^2.18.
|
73
|
-
"typescript": "^4.4.
|
68
|
+
"ts-jest": "^27.0.6",
|
69
|
+
"ts-node": "^10.3.0",
|
70
|
+
"type-coverage": "^2.18.2",
|
71
|
+
"typescript": "^4.4.4"
|
74
72
|
},
|
75
73
|
"resolutions": {
|
76
|
-
"prettier": "^2.
|
74
|
+
"prettier": "^2.4.1",
|
77
75
|
"tslib": "^2.3.1"
|
78
76
|
},
|
79
77
|
"commitlint": {
|
@@ -106,7 +104,7 @@
|
|
106
104
|
]
|
107
105
|
},
|
108
106
|
"typeCoverage": {
|
109
|
-
"atLeast": 99.
|
107
|
+
"atLeast": 99.46,
|
110
108
|
"cache": true,
|
111
109
|
"detail": true,
|
112
110
|
"ignoreAsAssertion": true,
|