synckit 0.3.3 → 0.5.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 +81 -0
- package/README.md +5 -11
- package/lib/index.cjs +117 -0
- package/lib/index.d.ts +3 -8
- package/lib/index.js +42 -103
- package/lib/index.js.map +1 -1
- package/lib/types.js +1 -2
- package/package.json +90 -6
- package/lib/es2015.js +0 -167
package/CHANGELOG.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# synckit
|
2
|
+
|
3
|
+
## 0.5.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- [#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
|
+
|
9
|
+
## 0.4.0
|
10
|
+
|
11
|
+
### Minor Changes
|
12
|
+
|
13
|
+
- [#45](https://github.com/rx-ts/synckit/pull/45) [`f38de5f`](https://github.com/rx-ts/synckit/commit/f38de5fe5dfc8e4a8871d3d55e7a4d9bdc3a5d05) Thanks [@JounQin](https://github.com/JounQin)! - feat: use native esm
|
14
|
+
|
15
|
+
## 0.3.4
|
16
|
+
|
17
|
+
### Patch Changes
|
18
|
+
|
19
|
+
- [#39](https://github.com/rx-ts/synckit/pull/39) [`0698572`](https://github.com/rx-ts/synckit/commit/0698572d048e38d9c1e5de233c07e89a7ca01eca) Thanks [@JounQin](https://github.com/JounQin)! - fix: test whether `receiveMessageOnPort` available for `--experimental-worker`
|
20
|
+
|
21
|
+
## 0.3.3
|
22
|
+
|
23
|
+
### Patch Changes
|
24
|
+
|
25
|
+
- [#37](https://github.com/rx-ts/synckit/pull/37) [`4ae675a`](https://github.com/rx-ts/synckit/commit/4ae675ad4b0bc02ac459e9d49319154048ee40dd) Thanks [@JounQin](https://github.com/JounQin)! - fix: `worker_threads` API changes a lot
|
26
|
+
|
27
|
+
## 0.3.2
|
28
|
+
|
29
|
+
### Patch Changes
|
30
|
+
|
31
|
+
- [#35](https://github.com/rx-ts/synckit/pull/35) [`578db5b`](https://github.com/rx-ts/synckit/commit/578db5bd33fdd137ca09b450a211e46d3f7299cf) Thanks [@JounQin](https://github.com/JounQin)! - fix: improve compatibility with node >=8.10 <12.11
|
32
|
+
|
33
|
+
## 0.3.1
|
34
|
+
|
35
|
+
### Patch Changes
|
36
|
+
|
37
|
+
- [#34](https://github.com/rx-ts/synckit/pull/34) [`255736c`](https://github.com/rx-ts/synckit/commit/255736ca98731cf52aa1391d855737f45edb457f) Thanks [@JounQin](https://github.com/JounQin)! - fix: `worker_threads` is only available on Node 12
|
38
|
+
|
39
|
+
- [#32](https://github.com/rx-ts/synckit/pull/32) [`d84e48e`](https://github.com/rx-ts/synckit/commit/d84e48e643124c3d5801bec3147ec158ccf6db49) Thanks [@JounQin](https://github.com/JounQin)! - fix(types): stricter but internal types
|
40
|
+
|
41
|
+
## 0.3.0
|
42
|
+
|
43
|
+
### Minor Changes
|
44
|
+
|
45
|
+
- [#27](https://github.com/rx-ts/synckit/pull/27) [`2809da0`](https://github.com/rx-ts/synckit/commit/2809da0d25b9e4c617b3699c78cf80fbae895c6f) Thanks [@JounQin](https://github.com/JounQin)! - feat: add more env variables support
|
46
|
+
|
47
|
+
## 0.2.0
|
48
|
+
|
49
|
+
### Minor Changes
|
50
|
+
|
51
|
+
- [#23](https://github.com/rx-ts/synckit/pull/23) [`6577e86`](https://github.com/rx-ts/synckit/commit/6577e86bff97a6a4d803394571e9406a86dd82dc) Thanks [@JounQin](https://github.com/JounQin)! - feat: use worker_threads by default for performance
|
52
|
+
|
53
|
+
## 0.1.6
|
54
|
+
|
55
|
+
### Patch Changes
|
56
|
+
|
57
|
+
- [`b3e9760`](https://github.com/rx-ts/synckit/commit/b3e976062f1c568d495ad0a578c55b83506208c9) Thanks [@JounQin](https://github.com/JounQin)! - feat: support custom TSCONFIG_PATH env
|
58
|
+
|
59
|
+
## 0.1.4
|
60
|
+
|
61
|
+
### Patch Changes
|
62
|
+
|
63
|
+
- [#9](https://github.com/rx-ts/synckit/pull/9) [`cad2e05`](https://github.com/rx-ts/synckit/commit/cad2e059bba51779115f63121077532b97d8aa6e) Thanks [@JounQin](https://github.com/JounQin)! - fix: try to fix clean-publish + changeset publish again
|
64
|
+
|
65
|
+
## 0.1.2
|
66
|
+
|
67
|
+
### Patch Changes
|
68
|
+
|
69
|
+
- [#7](https://github.com/rx-ts/synckit/pull/7) [`0336c22`](https://github.com/rx-ts/synckit/commit/0336c22a5f159fa6d35b9a6b8f93f0a56a35b3dd) Thanks [@JounQin](https://github.com/JounQin)! - fix: try to fix clean-publish + changeset publish
|
70
|
+
|
71
|
+
## 0.1.1
|
72
|
+
|
73
|
+
### Patch Changes
|
74
|
+
|
75
|
+
- [#5](https://github.com/rx-ts/synckit/pull/5) [`e451004`](https://github.com/rx-ts/synckit/commit/e4510040211b139a423d64eaf6607804c00a9915) Thanks [@JounQin](https://github.com/JounQin)! - fix: improve type definitions, mark tslib as dep
|
76
|
+
|
77
|
+
## 0.1.0
|
78
|
+
|
79
|
+
### Minor Changes
|
80
|
+
|
81
|
+
- [`e7446f9`](https://github.com/rx-ts/synckit/commit/e7446f91421df5ee59e05adc002b3daa52dff96f) Thanks [@JounQin](https://github.com/JounQin)! - feat: first blood, should just work
|
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,12 @@ 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. `SYNCKIT_TIMEOUT`: `timeout` for performing the async job (no default)
|
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)
|
79
73
|
|
80
74
|
### TypeScript
|
81
75
|
|
@@ -91,7 +85,7 @@ It is about 20x faster than [`sync-threads`](https://github.com/lambci/sync-thre
|
|
91
85
|
|
92
86
|
And it's almost same as [`deasync`](https://github.com/abbr/deasync) but requires no native bindings or `node-gyp`.
|
93
87
|
|
94
|
-
See [benchmark](./benchmarks/benchmark.txt) for more details.
|
88
|
+
See [benchmark.cjs](./benchmarks/benchmark.cjs.txt) and [benchmark.esm](./benchmarks/benchmark.esm.txt) for more details.
|
95
89
|
|
96
90
|
You can try it with running `yarn benchmark` by yourself. [Here](./benchmarks/benchmark.js) is the benchmark source code.
|
97
91
|
|
package/lib/index.cjs
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
+
|
5
|
+
var path = require('path');
|
6
|
+
var worker_threads = require('worker_threads');
|
7
|
+
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
9
|
+
|
10
|
+
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
11
|
+
|
12
|
+
var __async = (__this, __arguments, generator) => {
|
13
|
+
return new Promise((resolve, reject) => {
|
14
|
+
var fulfilled = (value) => {
|
15
|
+
try {
|
16
|
+
step(generator.next(value));
|
17
|
+
} catch (e) {
|
18
|
+
reject(e);
|
19
|
+
}
|
20
|
+
};
|
21
|
+
var rejected = (value) => {
|
22
|
+
try {
|
23
|
+
step(generator.throw(value));
|
24
|
+
} catch (e) {
|
25
|
+
reject(e);
|
26
|
+
}
|
27
|
+
};
|
28
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
29
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
30
|
+
});
|
31
|
+
};
|
32
|
+
const { SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT, SYNCKIT_TS_ESM } = process.env;
|
33
|
+
const TS_USE_ESM = !!SYNCKIT_TS_ESM && ["1", "true"].includes(SYNCKIT_TS_ESM);
|
34
|
+
const DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE ? +SYNCKIT_BUFFER_SIZE : void 0;
|
35
|
+
const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : void 0;
|
36
|
+
const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024;
|
37
|
+
const syncFnCache = new Map();
|
38
|
+
function createSyncFn(workerPath, bufferSize, timeout = DEFAULT_TIMEOUT) {
|
39
|
+
if (!path__default['default'].isAbsolute(workerPath)) {
|
40
|
+
throw new Error("`workerPath` must be absolute");
|
41
|
+
}
|
42
|
+
const cachedSyncFn = syncFnCache.get(workerPath);
|
43
|
+
if (cachedSyncFn) {
|
44
|
+
return cachedSyncFn;
|
45
|
+
}
|
46
|
+
const syncFn = startWorkerThread(workerPath, bufferSize, timeout);
|
47
|
+
syncFnCache.set(workerPath, syncFn);
|
48
|
+
return syncFn;
|
49
|
+
}
|
50
|
+
const throwError = (msg) => {
|
51
|
+
throw new Error(msg);
|
52
|
+
};
|
53
|
+
function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE, timeout) {
|
54
|
+
const { port1: mainPort, port2: workerPort } = new worker_threads.MessageChannel();
|
55
|
+
const isTs = workerPath.endsWith(".ts");
|
56
|
+
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, {
|
57
|
+
eval: isTs,
|
58
|
+
workerData: { workerPort },
|
59
|
+
transferList: [workerPort],
|
60
|
+
execArgv: []
|
61
|
+
});
|
62
|
+
let nextID = 0;
|
63
|
+
const syncFn = (...args) => {
|
64
|
+
const id = nextID++;
|
65
|
+
const sharedBuffer = new SharedArrayBuffer(bufferSize);
|
66
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
67
|
+
const msg = { sharedBuffer, id, args };
|
68
|
+
worker.postMessage(msg);
|
69
|
+
const status = Atomics.wait(sharedBufferView, 0, 0, timeout);
|
70
|
+
if (!["ok", "not-equal"].includes(status)) {
|
71
|
+
throw new Error("Internal error: Atomics.wait() failed: " + status);
|
72
|
+
}
|
73
|
+
const {
|
74
|
+
id: id2,
|
75
|
+
result,
|
76
|
+
error
|
77
|
+
} = worker_threads.receiveMessageOnPort(mainPort).message;
|
78
|
+
if (id !== id2) {
|
79
|
+
throw new Error(`Internal error: Expected id ${id} but got id ${id2}`);
|
80
|
+
}
|
81
|
+
if (error) {
|
82
|
+
throw error;
|
83
|
+
}
|
84
|
+
return result;
|
85
|
+
};
|
86
|
+
worker.unref();
|
87
|
+
return syncFn;
|
88
|
+
}
|
89
|
+
function runAsWorker(fn) {
|
90
|
+
if (!worker_threads.workerData) {
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
const { workerPort } = worker_threads.workerData;
|
94
|
+
worker_threads.parentPort.on("message", ({ sharedBuffer, id, args }) => {
|
95
|
+
(() => __async(this, null, function* () {
|
96
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
97
|
+
let msg;
|
98
|
+
try {
|
99
|
+
msg = { id, result: yield fn(...args) };
|
100
|
+
} catch (error) {
|
101
|
+
msg = {
|
102
|
+
id,
|
103
|
+
error
|
104
|
+
};
|
105
|
+
}
|
106
|
+
workerPort.postMessage(msg);
|
107
|
+
Atomics.add(sharedBufferView, 0, 1);
|
108
|
+
Atomics.notify(sharedBufferView, 0);
|
109
|
+
}))();
|
110
|
+
});
|
111
|
+
}
|
112
|
+
|
113
|
+
exports.DEFAULT_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
|
114
|
+
exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
|
115
|
+
exports.DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_WORKER_BUFFER_SIZE;
|
116
|
+
exports.createSyncFn = createSyncFn;
|
117
|
+
exports.runAsWorker = runAsWorker;
|
package/lib/index.d.ts
CHANGED
@@ -1,12 +1,7 @@
|
|
1
|
-
import { AnyAsyncFn, Syncify } from './types';
|
2
|
-
export * from './types';
|
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;
|
1
|
+
import { AnyAsyncFn, Syncify } from './types.js';
|
2
|
+
export * from './types.js';
|
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;
|
11
6
|
export declare function createSyncFn<T extends AnyAsyncFn>(workerPath: string, bufferSize?: number, timeout?: number): Syncify<T>;
|
12
|
-
export declare function runAsWorker<T extends AnyAsyncFn>(fn: T):
|
7
|
+
export declare function runAsWorker<R = unknown, T extends AnyAsyncFn<R> = AnyAsyncFn<R>>(fn: T): void;
|
package/lib/index.js
CHANGED
@@ -1,82 +1,40 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
const
|
6
|
-
const
|
7
|
-
const
|
8
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
9
|
-
const uuid_1 = require("uuid");
|
10
|
-
tslib_1.__exportStar(require("./types"), exports);
|
11
|
-
/**
|
12
|
-
* @link https://github.com/sindresorhus/temp-dir/blob/main/index.js#L9
|
13
|
-
*/
|
14
|
-
exports.tmpdir = fs_1.default.realpathSync(os_1.tmpdir());
|
15
|
-
let workerThreads;
|
16
|
-
try {
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-assignment
|
18
|
-
workerThreads = require('worker_threads');
|
19
|
-
}
|
20
|
-
catch (_a) {
|
21
|
-
/* istanbul ignore next */
|
22
|
-
console.warn('`worker_threads` is not available in current environment, `Node >= 12` is required');
|
23
|
-
}
|
24
|
-
const { SYNCKIT_WORKER_THREADS, SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT } = process.env;
|
25
|
-
exports.useWorkerThreads = !!workerThreads &&
|
26
|
-
(!SYNCKIT_WORKER_THREADS || !['0', 'false'].includes(SYNCKIT_WORKER_THREADS));
|
27
|
-
exports.DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE
|
1
|
+
import { __awaiter } from "tslib";
|
2
|
+
import path from 'path';
|
3
|
+
import { MessageChannel, Worker, receiveMessageOnPort, workerData, parentPort, } from 'worker_threads';
|
4
|
+
export * from './types.js';
|
5
|
+
const { SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT, SYNCKIT_TS_ESM } = process.env;
|
6
|
+
const TS_USE_ESM = !!SYNCKIT_TS_ESM && ['1', 'true'].includes(SYNCKIT_TS_ESM);
|
7
|
+
export const DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE
|
28
8
|
? +SYNCKIT_BUFFER_SIZE
|
29
9
|
: undefined;
|
30
|
-
|
31
|
-
|
10
|
+
export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : undefined;
|
11
|
+
export const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024;
|
32
12
|
const syncFnCache = new Map();
|
33
|
-
function createSyncFn(workerPath, bufferSize, timeout =
|
34
|
-
if (!
|
13
|
+
export function createSyncFn(workerPath, bufferSize, timeout = DEFAULT_TIMEOUT) {
|
14
|
+
if (!path.isAbsolute(workerPath)) {
|
35
15
|
throw new Error('`workerPath` must be absolute');
|
36
16
|
}
|
37
17
|
const cachedSyncFn = syncFnCache.get(workerPath);
|
38
18
|
if (cachedSyncFn) {
|
39
19
|
return cachedSyncFn;
|
40
20
|
}
|
41
|
-
const syncFn =
|
21
|
+
const syncFn = startWorkerThread(workerPath, bufferSize, timeout);
|
42
22
|
syncFnCache.set(workerPath, syncFn);
|
43
23
|
return syncFn;
|
44
24
|
}
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
fs_1.default.writeFileSync(filename, JSON.stringify(args));
|
51
|
-
const command = `${executor} ${workerPath} ${filename}`;
|
52
|
-
try {
|
53
|
-
child_process_1.execSync(command, {
|
54
|
-
stdio: 'inherit',
|
55
|
-
maxBuffer: bufferSize,
|
56
|
-
timeout,
|
57
|
-
});
|
58
|
-
const { result, error } = JSON.parse(fs_1.default.readFileSync(filename, 'utf8'));
|
59
|
-
if (error) {
|
60
|
-
throw typeof error === 'object' && 'message' in error
|
61
|
-
? // eslint-disable-next-line unicorn/error-message
|
62
|
-
Object.assign(new Error(), error)
|
63
|
-
: error;
|
64
|
-
}
|
65
|
-
return result;
|
66
|
-
}
|
67
|
-
finally {
|
68
|
-
fs_1.default.unlinkSync(filename);
|
69
|
-
}
|
70
|
-
};
|
71
|
-
}
|
72
|
-
function startWorkerThread(workerPath, bufferSize = exports.DEFAULT_WORKER_BUFFER_SIZE, timeout) {
|
73
|
-
const { port1: mainPort, port2: workerPort } = new workerThreads.MessageChannel();
|
25
|
+
const throwError = (msg) => {
|
26
|
+
throw new Error(msg);
|
27
|
+
};
|
28
|
+
function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE, timeout) {
|
29
|
+
const { port1: mainPort, port2: workerPort } = new MessageChannel();
|
74
30
|
const isTs = workerPath.endsWith('.ts');
|
75
|
-
const worker = new
|
76
|
-
?
|
31
|
+
const worker = new Worker(isTs
|
32
|
+
? TS_USE_ESM
|
33
|
+
? throwError('Native esm in `.ts` file is not supported yet, please use `.cjs` instead')
|
34
|
+
: `require('ts-node/register');require('${workerPath}')`
|
77
35
|
: workerPath, {
|
78
36
|
eval: isTs,
|
79
|
-
workerData: {
|
37
|
+
workerData: { workerPort },
|
80
38
|
transferList: [workerPort],
|
81
39
|
execArgv: [],
|
82
40
|
});
|
@@ -92,8 +50,7 @@ function startWorkerThread(workerPath, bufferSize = exports.DEFAULT_WORKER_BUFFE
|
|
92
50
|
if (!['ok', 'not-equal'].includes(status)) {
|
93
51
|
throw new Error('Internal error: Atomics.wait() failed: ' + status);
|
94
52
|
}
|
95
|
-
const { id: id2, result, error, } =
|
96
|
-
.message;
|
53
|
+
const { id: id2, result, error, } = receiveMessageOnPort(mainPort).message;
|
97
54
|
/* istanbul ignore if */
|
98
55
|
if (id !== id2) {
|
99
56
|
throw new Error(`Internal error: Expected id ${id} but got id ${id2}`);
|
@@ -106,49 +63,31 @@ function startWorkerThread(workerPath, bufferSize = exports.DEFAULT_WORKER_BUFFE
|
|
106
63
|
worker.unref();
|
107
64
|
return syncFn;
|
108
65
|
}
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
66
|
+
/* istanbul ignore next */
|
67
|
+
export function runAsWorker(fn) {
|
68
|
+
if (!workerData) {
|
69
|
+
return;
|
70
|
+
}
|
71
|
+
const { workerPort } = workerData;
|
72
|
+
parentPort.on('message', ({ sharedBuffer, id, args }) => {
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
74
|
+
;
|
75
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
76
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
115
77
|
let msg;
|
116
78
|
try {
|
117
|
-
msg = { result: yield fn(...args) };
|
79
|
+
msg = { id, result: yield fn(...args) };
|
118
80
|
}
|
119
|
-
catch (
|
81
|
+
catch (error) {
|
120
82
|
msg = {
|
121
|
-
|
122
|
-
|
83
|
+
id,
|
84
|
+
error,
|
123
85
|
};
|
124
86
|
}
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
const { workerPort } = workerThreads.workerData;
|
130
|
-
/* istanbul ignore next */
|
131
|
-
workerThreads.parentPort.on('message', ({ sharedBuffer, id, args }) => {
|
132
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
133
|
-
;
|
134
|
-
(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
135
|
-
const sharedBufferView = new Int32Array(sharedBuffer);
|
136
|
-
let msg;
|
137
|
-
try {
|
138
|
-
msg = { id, result: yield fn(...args) };
|
139
|
-
}
|
140
|
-
catch (error) {
|
141
|
-
msg = {
|
142
|
-
id,
|
143
|
-
error,
|
144
|
-
};
|
145
|
-
}
|
146
|
-
workerPort.postMessage(msg);
|
147
|
-
Atomics.add(sharedBufferView, 0, 1);
|
148
|
-
Atomics.notify(sharedBufferView, 0);
|
149
|
-
}))();
|
150
|
-
});
|
87
|
+
workerPort.postMessage(msg);
|
88
|
+
Atomics.add(sharedBufferView, 0, 1);
|
89
|
+
Atomics.notify(sharedBufferView, 0);
|
90
|
+
}))();
|
151
91
|
});
|
152
92
|
}
|
153
|
-
exports.runAsWorker = runAsWorker;
|
154
93
|
//# 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":"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"}
|
package/lib/types.js
CHANGED
package/package.json
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "synckit",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.0",
|
4
|
+
"type": "module",
|
4
5
|
"description": "Perform async work synchronously in Node.js using `worker_threads`, or `child_process` as fallback, with first-class TypeScript support.",
|
5
6
|
"repository": "git+https://github.com/rx-ts/synckit.git",
|
6
7
|
"author": "JounQin <admin@1stg.me>",
|
7
8
|
"license": "MIT",
|
8
9
|
"engines": {
|
9
|
-
"node": ">=
|
10
|
+
"node": ">=12.3"
|
11
|
+
},
|
12
|
+
"exports": {
|
13
|
+
"import": "./lib/index.js",
|
14
|
+
"require": "./lib/index.cjs"
|
10
15
|
},
|
11
|
-
"main": "lib",
|
12
|
-
"module": "lib/es2015",
|
13
16
|
"types": "lib",
|
14
17
|
"files": [
|
15
18
|
"lib",
|
@@ -24,8 +27,89 @@
|
|
24
27
|
"synchronize",
|
25
28
|
"synckit"
|
26
29
|
],
|
30
|
+
"scripts": {
|
31
|
+
"benchmark": "run-s benchmark:*",
|
32
|
+
"benchmark-export": "run-s benchmark-export:*",
|
33
|
+
"benchmark-export:cjs": "yarn benchmark:cjs > benchmarks/benchmark.cjs.txt",
|
34
|
+
"benchmark-export:esm": "yarn benchmark:esm> benchmarks/benchmark.esm.txt",
|
35
|
+
"benchmark:cjs": "node benchmarks/benchmark.cjs",
|
36
|
+
"benchmark:esm": "node benchmarks/benchmark.js",
|
37
|
+
"build": "run-p build:*",
|
38
|
+
"build:r": "r -f cjs",
|
39
|
+
"build:ts": "tsc -p src",
|
40
|
+
"jest": "node --experimental-vm-modules node_modules/.bin/jest --setupFiles dotenv/config",
|
41
|
+
"lint": "run-p lint:*",
|
42
|
+
"lint:es": "eslint . --cache -f friendly --max-warnings 10",
|
43
|
+
"lint:tsc": "tsc --noEmit",
|
44
|
+
"prepare": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
|
45
|
+
"prerelease": "npm run build",
|
46
|
+
"pretest": "yarn build:ts",
|
47
|
+
"release": "clean-publish && changeset publish",
|
48
|
+
"test": "yarn jest",
|
49
|
+
"typecov": "type-coverage"
|
50
|
+
},
|
27
51
|
"dependencies": {
|
28
|
-
"tslib": "^2.3.
|
29
|
-
|
52
|
+
"tslib": "^2.3.1"
|
53
|
+
},
|
54
|
+
"devDependencies": {
|
55
|
+
"@1stg/lib-config": "^4.0.0",
|
56
|
+
"@changesets/changelog-github": "^0.4.0",
|
57
|
+
"@changesets/cli": "^2.16.0",
|
58
|
+
"@types/jest": "^27.0.1",
|
59
|
+
"@types/node": "^16.7.5",
|
60
|
+
"@types/uuid": "^8.3.1",
|
61
|
+
"clean-publish": "^2.1.1",
|
62
|
+
"deasync": "^0.1.23",
|
63
|
+
"enhanced-resolve": "^5.8.2",
|
64
|
+
"postcss": "^8.3.6",
|
65
|
+
"sync-threads": "^1.0.1",
|
66
|
+
"ts-expect": "^1.3.0",
|
67
|
+
"ts-jest": "^27.0.5",
|
68
|
+
"ts-node": "^10.2.1",
|
69
|
+
"type-coverage": "^2.18.1",
|
70
|
+
"typescript": "^4.4.2"
|
71
|
+
},
|
72
|
+
"resolutions": {
|
73
|
+
"prettier": "^2.3.2",
|
74
|
+
"tslib": "^2.3.1"
|
75
|
+
},
|
76
|
+
"commitlint": {
|
77
|
+
"extends": "@1stg"
|
78
|
+
},
|
79
|
+
"jest": {
|
80
|
+
"preset": "ts-jest",
|
81
|
+
"testEnvironment": "node",
|
82
|
+
"collectCoverage": true,
|
83
|
+
"extensionsToTreatAsEsm": [
|
84
|
+
".ts"
|
85
|
+
],
|
86
|
+
"moduleNameMapper": {
|
87
|
+
"^(\\.{1,2}/.*)\\.js$": "$1",
|
88
|
+
"^synckit$": "<rootDir>/src"
|
89
|
+
},
|
90
|
+
"globals": {
|
91
|
+
"ts-jest": {
|
92
|
+
"useESM": true,
|
93
|
+
"tsconfig": {
|
94
|
+
"importHelpers": false
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
},
|
99
|
+
"prettier": "@1stg/prettier-config",
|
100
|
+
"renovate": {
|
101
|
+
"extends": [
|
102
|
+
"@1stg"
|
103
|
+
]
|
104
|
+
},
|
105
|
+
"typeCoverage": {
|
106
|
+
"atLeast": 99.2,
|
107
|
+
"cache": true,
|
108
|
+
"detail": true,
|
109
|
+
"ignoreAsAssertion": true,
|
110
|
+
"ignoreNonNullAssertion": true,
|
111
|
+
"showRelativePath": true,
|
112
|
+
"strict": true,
|
113
|
+
"update": true
|
30
114
|
}
|
31
115
|
}
|
package/lib/es2015.js
DELETED
@@ -1,167 +0,0 @@
|
|
1
|
-
import { execSync } from 'child_process';
|
2
|
-
import { tmpdir as tmpdir$1 } from 'os';
|
3
|
-
import path from 'path';
|
4
|
-
import fs from 'fs';
|
5
|
-
import { v4 } from 'uuid';
|
6
|
-
|
7
|
-
var __defProp = Object.defineProperty;
|
8
|
-
var __defProps = Object.defineProperties;
|
9
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
10
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
11
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
12
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
13
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
14
|
-
var __spreadValues = (a, b) => {
|
15
|
-
for (var prop in b || (b = {}))
|
16
|
-
if (__hasOwnProp.call(b, prop))
|
17
|
-
__defNormalProp(a, prop, b[prop]);
|
18
|
-
if (__getOwnPropSymbols)
|
19
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
20
|
-
if (__propIsEnum.call(b, prop))
|
21
|
-
__defNormalProp(a, prop, b[prop]);
|
22
|
-
}
|
23
|
-
return a;
|
24
|
-
};
|
25
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
26
|
-
var __async = (__this, __arguments, generator) => {
|
27
|
-
return new Promise((resolve, reject) => {
|
28
|
-
var fulfilled = (value) => {
|
29
|
-
try {
|
30
|
-
step(generator.next(value));
|
31
|
-
} catch (e) {
|
32
|
-
reject(e);
|
33
|
-
}
|
34
|
-
};
|
35
|
-
var rejected = (value) => {
|
36
|
-
try {
|
37
|
-
step(generator.throw(value));
|
38
|
-
} catch (e) {
|
39
|
-
reject(e);
|
40
|
-
}
|
41
|
-
};
|
42
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
43
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
44
|
-
});
|
45
|
-
};
|
46
|
-
const tmpdir = fs.realpathSync(tmpdir$1());
|
47
|
-
let workerThreads;
|
48
|
-
try {
|
49
|
-
workerThreads = require("worker_threads");
|
50
|
-
} catch (e) {
|
51
|
-
console.warn("`worker_threads` is not available in current environment, `Node >= 12` is required");
|
52
|
-
}
|
53
|
-
const { SYNCKIT_WORKER_THREADS, SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT } = process.env;
|
54
|
-
const useWorkerThreads = !!workerThreads && (!SYNCKIT_WORKER_THREADS || !["0", "false"].includes(SYNCKIT_WORKER_THREADS));
|
55
|
-
const DEFAULT_BUFFER_SIZE = SYNCKIT_BUFFER_SIZE ? +SYNCKIT_BUFFER_SIZE : void 0;
|
56
|
-
const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : void 0;
|
57
|
-
const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024;
|
58
|
-
const syncFnCache = new Map();
|
59
|
-
function createSyncFn(workerPath, bufferSize, timeout = DEFAULT_TIMEOUT) {
|
60
|
-
if (!path.isAbsolute(workerPath)) {
|
61
|
-
throw new Error("`workerPath` must be absolute");
|
62
|
-
}
|
63
|
-
const cachedSyncFn = syncFnCache.get(workerPath);
|
64
|
-
if (cachedSyncFn) {
|
65
|
-
return cachedSyncFn;
|
66
|
-
}
|
67
|
-
const syncFn = (useWorkerThreads ? startWorkerThread : startChildProcess)(workerPath, bufferSize, timeout);
|
68
|
-
syncFnCache.set(workerPath, syncFn);
|
69
|
-
return syncFn;
|
70
|
-
}
|
71
|
-
function startChildProcess(workerPath, bufferSize = DEFAULT_BUFFER_SIZE, timeout) {
|
72
|
-
const executor = workerPath.endsWith(".ts") ? "ts-node" : "node";
|
73
|
-
return (...args) => {
|
74
|
-
const filename = path.resolve(tmpdir, `synckit-${v4()}.json`);
|
75
|
-
fs.writeFileSync(filename, JSON.stringify(args));
|
76
|
-
const command = `${executor} ${workerPath} ${filename}`;
|
77
|
-
try {
|
78
|
-
execSync(command, {
|
79
|
-
stdio: "inherit",
|
80
|
-
maxBuffer: bufferSize,
|
81
|
-
timeout
|
82
|
-
});
|
83
|
-
const { result, error } = JSON.parse(fs.readFileSync(filename, "utf8"));
|
84
|
-
if (error) {
|
85
|
-
throw typeof error === "object" && "message" in error ? Object.assign(new Error(), error) : error;
|
86
|
-
}
|
87
|
-
return result;
|
88
|
-
} finally {
|
89
|
-
fs.unlinkSync(filename);
|
90
|
-
}
|
91
|
-
};
|
92
|
-
}
|
93
|
-
function startWorkerThread(workerPath, bufferSize = DEFAULT_WORKER_BUFFER_SIZE, timeout) {
|
94
|
-
const { port1: mainPort, port2: workerPort } = new workerThreads().MessageChannel();
|
95
|
-
const isTs = workerPath.endsWith(".ts");
|
96
|
-
const worker = new workerThreads().Worker(isTs ? `require('ts-node/register');require(require('worker_threads').workerData.workerPath)` : workerPath, {
|
97
|
-
eval: isTs,
|
98
|
-
workerData: { workerPath, workerPort },
|
99
|
-
transferList: [workerPort],
|
100
|
-
execArgv: []
|
101
|
-
});
|
102
|
-
let nextID = 0;
|
103
|
-
const syncFn = (...args) => {
|
104
|
-
const id = nextID++;
|
105
|
-
const sharedBuffer = new SharedArrayBuffer(bufferSize);
|
106
|
-
const sharedBufferView = new Int32Array(sharedBuffer);
|
107
|
-
const msg = { sharedBuffer, id, args };
|
108
|
-
worker.postMessage(msg);
|
109
|
-
const status = Atomics.wait(sharedBufferView, 0, 0, timeout);
|
110
|
-
if (!["ok", "not-equal"].includes(status)) {
|
111
|
-
throw new Error("Internal error: Atomics.wait() failed: " + status);
|
112
|
-
}
|
113
|
-
const {
|
114
|
-
id: id2,
|
115
|
-
result,
|
116
|
-
error
|
117
|
-
} = workerThreads.receiveMessageOnPort(mainPort).message;
|
118
|
-
if (id !== id2) {
|
119
|
-
throw new Error(`Internal error: Expected id ${id} but got id ${id2}`);
|
120
|
-
}
|
121
|
-
if (error) {
|
122
|
-
throw error;
|
123
|
-
}
|
124
|
-
return result;
|
125
|
-
};
|
126
|
-
worker.unref();
|
127
|
-
return syncFn;
|
128
|
-
}
|
129
|
-
function runAsWorker(fn) {
|
130
|
-
return __async(this, null, function* () {
|
131
|
-
if (!(workerThreads == null ? void 0 : workerThreads.workerData)) {
|
132
|
-
const filename = process.argv[2];
|
133
|
-
const content = fs.readFileSync(filename, "utf8");
|
134
|
-
const args = JSON.parse(content);
|
135
|
-
let msg;
|
136
|
-
try {
|
137
|
-
msg = { result: yield fn(...args) };
|
138
|
-
} catch (err) {
|
139
|
-
msg = {
|
140
|
-
error: err instanceof Error ? __spreadProps(__spreadValues({}, err), { name: err.name, message: err.message, stack: err.stack }) : err
|
141
|
-
};
|
142
|
-
}
|
143
|
-
fs.writeFileSync(filename, JSON.stringify(msg));
|
144
|
-
return;
|
145
|
-
}
|
146
|
-
const { workerPort } = workerThreads.workerData;
|
147
|
-
workerThreads.parentPort.on("message", ({ sharedBuffer, id, args }) => {
|
148
|
-
(() => __async(this, null, function* () {
|
149
|
-
const sharedBufferView = new Int32Array(sharedBuffer);
|
150
|
-
let msg;
|
151
|
-
try {
|
152
|
-
msg = { id, result: yield fn(...args) };
|
153
|
-
} catch (error) {
|
154
|
-
msg = {
|
155
|
-
id,
|
156
|
-
error
|
157
|
-
};
|
158
|
-
}
|
159
|
-
workerPort.postMessage(msg);
|
160
|
-
Atomics.add(sharedBufferView, 0, 1);
|
161
|
-
Atomics.notify(sharedBufferView, 0);
|
162
|
-
}))();
|
163
|
-
});
|
164
|
-
});
|
165
|
-
}
|
166
|
-
|
167
|
-
export { DEFAULT_BUFFER_SIZE, DEFAULT_TIMEOUT, DEFAULT_WORKER_BUFFER_SIZE, createSyncFn, runAsWorker, tmpdir, useWorkerThreads };
|