threadsx 2.0.2 → 2.2.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.
Files changed (115) hide show
  1. package/dist/errors.d.ts +16 -0
  2. package/dist/errors.js +31 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +3 -1
  5. package/dist/master/implementation.node.js +34 -18
  6. package/dist/master/invocation-proxy.js +45 -11
  7. package/dist/master/pool.d.ts +1 -24
  8. package/dist/master/pool.js +41 -8
  9. package/dist/master/spawn.js +24 -7
  10. package/dist/node-require.d.ts +13 -0
  11. package/dist/node-require.js +87 -0
  12. package/dist/transferable.d.ts +1 -0
  13. package/dist/types/master.d.ts +22 -1
  14. package/dist/types/master.js +0 -1
  15. package/dist/types/worker.d.ts +1 -0
  16. package/dist/worker/implementation.browser.d.ts +1 -1
  17. package/dist/worker/implementation.worker_threads.d.ts +1 -1
  18. package/dist/worker_threads.d.ts +3 -7
  19. package/dist/worker_threads.js +35 -11
  20. package/dist-esm/common.js +1 -0
  21. package/dist-esm/common.js.map +1 -0
  22. package/dist-esm/errors.js +27 -0
  23. package/dist-esm/errors.js.map +1 -0
  24. package/dist-esm/index.js +2 -0
  25. package/dist-esm/index.js.map +1 -0
  26. package/dist-esm/master/get-bundle-url.browser.js +1 -0
  27. package/dist-esm/master/get-bundle-url.browser.js.map +1 -0
  28. package/dist-esm/master/implementation.browser.js +1 -0
  29. package/dist-esm/master/implementation.browser.js.map +1 -0
  30. package/dist-esm/master/implementation.js +1 -0
  31. package/dist-esm/master/implementation.js.map +1 -0
  32. package/dist-esm/master/implementation.node.js +35 -18
  33. package/dist-esm/master/implementation.node.js.map +1 -0
  34. package/dist-esm/master/index.js +1 -0
  35. package/dist-esm/master/index.js.map +1 -0
  36. package/dist-esm/master/invocation-proxy.js +46 -11
  37. package/dist-esm/master/invocation-proxy.js.map +1 -0
  38. package/dist-esm/master/pool-types.js +1 -0
  39. package/dist-esm/master/pool-types.js.map +1 -0
  40. package/dist-esm/master/pool.js +42 -8
  41. package/dist-esm/master/pool.js.map +1 -0
  42. package/dist-esm/master/register.js +1 -0
  43. package/dist-esm/master/register.js.map +1 -0
  44. package/dist-esm/master/spawn.js +25 -7
  45. package/dist-esm/master/spawn.js.map +1 -0
  46. package/dist-esm/master/thread.js +1 -0
  47. package/dist-esm/master/thread.js.map +1 -0
  48. package/dist-esm/node-require.js +50 -0
  49. package/dist-esm/node-require.js.map +1 -0
  50. package/dist-esm/observable-promise.js +1 -0
  51. package/dist-esm/observable-promise.js.map +1 -0
  52. package/dist-esm/observable.js +1 -0
  53. package/dist-esm/observable.js.map +1 -0
  54. package/dist-esm/ponyfills.js +1 -0
  55. package/dist-esm/ponyfills.js.map +1 -0
  56. package/dist-esm/promise.js +1 -0
  57. package/dist-esm/promise.js.map +1 -0
  58. package/dist-esm/serializers.js +1 -0
  59. package/dist-esm/serializers.js.map +1 -0
  60. package/dist-esm/symbols.js +1 -0
  61. package/dist-esm/symbols.js.map +1 -0
  62. package/dist-esm/transferable.js +1 -0
  63. package/dist-esm/transferable.js.map +1 -0
  64. package/dist-esm/types/master.js +1 -1
  65. package/dist-esm/types/master.js.map +1 -0
  66. package/dist-esm/types/messages.js +1 -0
  67. package/dist-esm/types/messages.js.map +1 -0
  68. package/dist-esm/types/worker.js +1 -0
  69. package/dist-esm/types/worker.js.map +1 -0
  70. package/dist-esm/worker/bundle-entry.js +1 -0
  71. package/dist-esm/worker/bundle-entry.js.map +1 -0
  72. package/dist-esm/worker/implementation.browser.js +1 -0
  73. package/dist-esm/worker/implementation.browser.js.map +1 -0
  74. package/dist-esm/worker/implementation.js +1 -0
  75. package/dist-esm/worker/implementation.js.map +1 -0
  76. package/dist-esm/worker/implementation.worker_threads.js +1 -0
  77. package/dist-esm/worker/implementation.worker_threads.js.map +1 -0
  78. package/dist-esm/worker/index.js +1 -0
  79. package/dist-esm/worker/index.js.map +1 -0
  80. package/dist-esm/worker_threads.js +3 -11
  81. package/dist-esm/worker_threads.js.map +1 -0
  82. package/esm/common.mjs +20 -0
  83. package/esm/errors.mjs +34 -0
  84. package/esm/index.mjs +16 -0
  85. package/esm/master/get-bundle-url.browser.mjs +32 -0
  86. package/esm/master/implementation.browser.mjs +76 -0
  87. package/esm/master/implementation.mjs +24 -0
  88. package/esm/master/implementation.node.mjs +186 -0
  89. package/esm/master/index.mjs +17 -0
  90. package/esm/master/invocation-proxy.mjs +164 -0
  91. package/esm/master/pool-types.mjs +20 -0
  92. package/esm/master/pool.mjs +324 -0
  93. package/esm/master/register.mjs +14 -0
  94. package/esm/master/spawn.mjs +179 -0
  95. package/esm/master/thread.mjs +28 -0
  96. package/esm/node-require.mjs +58 -0
  97. package/esm/observable-promise.mjs +155 -0
  98. package/esm/observable.mjs +45 -0
  99. package/esm/ponyfills.mjs +26 -0
  100. package/esm/promise.mjs +32 -0
  101. package/esm/serializers.mjs +55 -0
  102. package/esm/symbols.mjs +13 -0
  103. package/esm/transferable.mjs +31 -0
  104. package/esm/types/master.mjs +15 -0
  105. package/esm/types/messages.mjs +25 -0
  106. package/esm/worker/implementation.browser.mjs +31 -0
  107. package/esm/worker/implementation.mjs +21 -0
  108. package/esm/worker/implementation.worker_threads.mjs +44 -0
  109. package/esm/worker/index.mjs +224 -0
  110. package/esm/worker_threads.mjs +13 -0
  111. package/package.json +32 -24
  112. package/index.mjs +0 -4
  113. package/observable.mjs +0 -1
  114. package/register.mjs +0 -2
  115. package/worker.mjs +0 -1
@@ -0,0 +1,224 @@
1
+ import { createRequire as __createRequire } from "module";
2
+ import { fileURLToPath as __fileURLToPath } from "url";
3
+ import { dirname as __pathDirname } from "path";
4
+ const require = __createRequire(import.meta.url);
5
+ const __filename = __fileURLToPath(import.meta.url);
6
+ const __dirname = __pathDirname(__filename);
7
+ import { serialize, deserialize } from '../common.mjs';
8
+ export { registerSerializer } from '../common.mjs';
9
+ import { isTransferDescriptor } from '../transferable.mjs';
10
+ export { Transfer } from '../transferable.mjs';
11
+ import { WorkerMessageType, MasterMessageType } from '../types/messages.mjs';
12
+ import Implementation from './implementation.mjs';
13
+
14
+ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
15
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
+ return new (P || (P = Promise))(function (resolve, reject) {
17
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
21
+ });
22
+ };
23
+ /** Returns `true` if this code is currently running in a worker. */
24
+ const isWorkerRuntime = Implementation.isWorkerRuntime;
25
+ let exposeCalled = false;
26
+ const activeSubscriptions = new Map();
27
+ const isMasterJobCancelMessage = (thing) => thing && thing.type === MasterMessageType.cancel;
28
+ const isMasterJobRunMessage = (thing) => thing && thing.type === MasterMessageType.run;
29
+ /**
30
+ * Detects observables via the `Symbol.observable` / `@@observable` interop protocol.
31
+ * Inlined from the `is-observable` package (which is now ESM-only) to keep the
32
+ * CommonJS build working without an extra dependency.
33
+ */
34
+ function isInteropObservable(thing) {
35
+ if (!thing) {
36
+ return false;
37
+ }
38
+ const observableSymbol = Symbol.observable;
39
+ if (typeof observableSymbol === "symbol" && typeof thing[observableSymbol] === "function") {
40
+ return thing === thing[observableSymbol]();
41
+ }
42
+ if (typeof thing["@@observable"] === "function") {
43
+ return thing === thing["@@observable"]();
44
+ }
45
+ return false;
46
+ }
47
+ /**
48
+ * There are issues with interop observable detection not recognizing zen-observable's instances.
49
+ * We are using `observable-fns`, but it's based on zen-observable, too.
50
+ */
51
+ const isObservable = (thing) => isInteropObservable(thing) || isZenObservable(thing);
52
+ function isZenObservable(thing) {
53
+ return thing && typeof thing === "object" && typeof thing.subscribe === "function";
54
+ }
55
+ function deconstructTransfer(thing) {
56
+ return isTransferDescriptor(thing)
57
+ ? { payload: thing.send, transferables: thing.transferables }
58
+ : { payload: thing, transferables: undefined };
59
+ }
60
+ function postFunctionInitMessage() {
61
+ const initMessage = {
62
+ type: WorkerMessageType.init,
63
+ exposed: {
64
+ type: "function"
65
+ }
66
+ };
67
+ Implementation.postMessageToMaster(initMessage);
68
+ }
69
+ function postModuleInitMessage(methodNames) {
70
+ const initMessage = {
71
+ type: WorkerMessageType.init,
72
+ exposed: {
73
+ type: "module",
74
+ methods: methodNames
75
+ }
76
+ };
77
+ Implementation.postMessageToMaster(initMessage);
78
+ }
79
+ function postJobErrorMessage(uid, rawError) {
80
+ const { payload: error, transferables } = deconstructTransfer(rawError);
81
+ const errorMessage = {
82
+ type: WorkerMessageType.error,
83
+ uid,
84
+ error: serialize(error)
85
+ };
86
+ Implementation.postMessageToMaster(errorMessage, transferables);
87
+ }
88
+ function postJobResultMessage(uid, completed, resultValue) {
89
+ const { payload, transferables } = deconstructTransfer(resultValue);
90
+ const resultMessage = {
91
+ type: WorkerMessageType.result,
92
+ uid,
93
+ complete: completed ? true : undefined,
94
+ payload
95
+ };
96
+ Implementation.postMessageToMaster(resultMessage, transferables);
97
+ }
98
+ function postJobStartMessage(uid, resultType) {
99
+ const startMessage = {
100
+ type: WorkerMessageType.running,
101
+ uid,
102
+ resultType
103
+ };
104
+ Implementation.postMessageToMaster(startMessage);
105
+ }
106
+ function postUncaughtErrorMessage(error) {
107
+ try {
108
+ const errorMessage = {
109
+ type: WorkerMessageType.uncaughtError,
110
+ error: serialize(error)
111
+ };
112
+ Implementation.postMessageToMaster(errorMessage);
113
+ }
114
+ catch (subError) {
115
+ console.error("Not reporting uncaught error back to master thread as it " +
116
+ "occured while reporting an uncaught error already." +
117
+ "\nLatest error:", subError, "\nOriginal error:", error);
118
+ }
119
+ }
120
+ function runFunction(jobUID, fn, args) {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ let syncResult;
123
+ try {
124
+ syncResult = fn(...args);
125
+ }
126
+ catch (error) {
127
+ return postJobErrorMessage(jobUID, error);
128
+ }
129
+ const resultType = isObservable(syncResult) ? "observable" : "promise";
130
+ postJobStartMessage(jobUID, resultType);
131
+ if (isObservable(syncResult)) {
132
+ const subscription = syncResult.subscribe(value => postJobResultMessage(jobUID, false, serialize(value)), error => {
133
+ postJobErrorMessage(jobUID, serialize(error));
134
+ activeSubscriptions.delete(jobUID);
135
+ }, () => {
136
+ postJobResultMessage(jobUID, true);
137
+ activeSubscriptions.delete(jobUID);
138
+ });
139
+ activeSubscriptions.set(jobUID, subscription);
140
+ }
141
+ else {
142
+ try {
143
+ const result = yield syncResult;
144
+ postJobResultMessage(jobUID, true, serialize(result));
145
+ }
146
+ catch (error) {
147
+ postJobErrorMessage(jobUID, serialize(error));
148
+ }
149
+ }
150
+ });
151
+ }
152
+ /**
153
+ * Expose a function or a module (an object whose values are functions)
154
+ * to the main thread. Must be called exactly once in every worker thread
155
+ * to signal its API to the main thread.
156
+ *
157
+ * @param exposed Function or object whose values are functions
158
+ */
159
+ function expose(exposed) {
160
+ if (!Implementation.isWorkerRuntime()) {
161
+ throw Error("expose() called in the master thread.");
162
+ }
163
+ if (exposeCalled) {
164
+ throw Error("expose() called more than once. This is not possible. Pass an object to expose() if you want to expose multiple functions.");
165
+ }
166
+ exposeCalled = true;
167
+ if (typeof exposed === "function") {
168
+ Implementation.subscribeToMasterMessages(messageData => {
169
+ if (isMasterJobRunMessage(messageData) && !messageData.method) {
170
+ runFunction(messageData.uid, exposed, messageData.args.map(deserialize));
171
+ }
172
+ });
173
+ postFunctionInitMessage();
174
+ }
175
+ else if (typeof exposed === "object" && exposed) {
176
+ Implementation.subscribeToMasterMessages(messageData => {
177
+ if (isMasterJobRunMessage(messageData) && messageData.method) {
178
+ runFunction(messageData.uid, exposed[messageData.method], messageData.args.map(deserialize));
179
+ }
180
+ });
181
+ const methodNames = Object.keys(exposed).filter(key => typeof exposed[key] === "function");
182
+ postModuleInitMessage(methodNames);
183
+ }
184
+ else {
185
+ throw Error(`Invalid argument passed to expose(). Expected a function or an object, got: ${exposed}`);
186
+ }
187
+ Implementation.subscribeToMasterMessages(messageData => {
188
+ if (isMasterJobCancelMessage(messageData)) {
189
+ const jobUID = messageData.uid;
190
+ const subscription = activeSubscriptions.get(jobUID);
191
+ if (subscription) {
192
+ subscription.unsubscribe();
193
+ activeSubscriptions.delete(jobUID);
194
+ }
195
+ }
196
+ });
197
+ }
198
+ if (typeof self !== "undefined" && typeof self.addEventListener === "function" && Implementation.isWorkerRuntime()) {
199
+ self.addEventListener("error", event => {
200
+ // Post with some delay, so the master had some time to subscribe to messages
201
+ setTimeout(() => postUncaughtErrorMessage(event.error || event), 250);
202
+ });
203
+ self.addEventListener("unhandledrejection", event => {
204
+ const error = event.reason;
205
+ if (error && typeof error.message === "string") {
206
+ // Post with some delay, so the master had some time to subscribe to messages
207
+ setTimeout(() => postUncaughtErrorMessage(error), 250);
208
+ }
209
+ });
210
+ }
211
+ if (typeof process !== "undefined" && typeof process.on === "function" && Implementation.isWorkerRuntime()) {
212
+ process.on("uncaughtException", (error) => {
213
+ // Post with some delay, so the master had some time to subscribe to messages
214
+ setTimeout(() => postUncaughtErrorMessage(error), 250);
215
+ });
216
+ process.on("unhandledRejection", (error) => {
217
+ if (error && typeof error.message === "string") {
218
+ // Post with some delay, so the master had some time to subscribe to messages
219
+ setTimeout(() => postUncaughtErrorMessage(error), 250);
220
+ }
221
+ });
222
+ }
223
+
224
+ export { expose, isWorkerRuntime };
@@ -0,0 +1,13 @@
1
+ import { createRequire as __createRequire } from "module";
2
+ import { fileURLToPath as __fileURLToPath } from "url";
3
+ import { dirname as __pathDirname } from "path";
4
+ const require = __createRequire(import.meta.url);
5
+ const __filename = __fileURLToPath(import.meta.url);
6
+ const __dirname = __pathDirname(__filename);
7
+ import * as WorkerThreadsModule from 'worker_threads';
8
+
9
+ function getImplementation() {
10
+ return WorkerThreadsModule;
11
+ }
12
+
13
+ export { getImplementation as default };
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "threadsx",
3
- "version": "2.0.2",
3
+ "version": "2.2.0",
4
4
  "description": "Make web workers & worker threads as simple as a function call. Maintained, modernized fork of threads.js — Node.js 20+, ESM & CommonJS, TypeScript, observables and thread pools.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist-esm/index.js",
8
8
  "scripts": {
9
- "clean": "rimraf dist/ dist-esm/",
9
+ "clean": "rimraf dist/ dist-esm/ esm/",
10
10
  "dev": "npm run clean && tsc -p tsconfig.json --watch",
11
11
  "build": "npm run clean && npm run build:cjs && npm run build:es",
12
12
  "build:cjs": "tsc -p tsconfig.json",
13
13
  "build:es": "tsc -p tsconfig-esm.json",
14
- "postbuild": "npm run bundle",
14
+ "postbuild": "npm run bundle && npm run bundle:entries",
15
15
  "bundle": "rollup -c -f umd --file=bundle/worker.js --name=threads --silent -- dist-esm/worker/bundle-entry.js",
16
+ "bundle:entries": "rollup -c rollup.entries.config.js --silent",
16
17
  "lint": "eslint .",
17
18
  "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
18
19
  "test": "npm run lint && npm run test:library && npm run test:tooling && npm run test:browser",
@@ -20,38 +21,41 @@
20
21
  "test:tooling": "ava ./test-tooling/**/*.test.ts",
21
22
  "test:browser": "playwright test",
22
23
  "test:coverage": "c8 ava ./test/**/*.test.ts",
24
+ "test:coverage:browser": "COVERAGE=1 playwright test",
25
+ "test:coverage:merged": "npm run test:coverage && npm run test:coverage:browser && node scripts/merge-coverage.mjs",
23
26
  "prepare": "npm run build"
24
27
  },
25
28
  "exports": {
26
29
  ".": {
27
30
  "types": "./dist/index.d.ts",
28
31
  "module": "./dist-esm/index.js",
29
- "import": "./index.mjs",
32
+ "import": "./esm/index.mjs",
30
33
  "require": "./dist/index.js"
31
34
  },
32
35
  "./observable": {
33
36
  "types": "./dist/observable.d.ts",
34
37
  "module": "./dist-esm/observable.js",
35
- "import": "./observable.mjs",
38
+ "import": "./esm/observable.mjs",
36
39
  "require": "./dist/observable.js"
37
40
  },
38
41
  "./register": {
42
+ "types": "./dist/master/register.d.ts",
39
43
  "module": "./dist-esm/master/register.js",
40
- "import": "./register.mjs",
44
+ "import": "./esm/master/register.mjs",
41
45
  "require": "./dist/master/register.js"
42
46
  },
43
47
  "./worker": {
44
48
  "types": "./dist/worker/index.d.ts",
45
49
  "module": "./dist-esm/worker/index.js",
46
- "import": "./worker.mjs",
50
+ "import": "./esm/worker/index.mjs",
47
51
  "require": "./dist/worker/index.js"
48
52
  }
49
53
  },
50
54
  "sideEffects": [
51
55
  "./dist*/master/register.js",
52
56
  "./dist*/worker/index.js",
53
- "./register.mjs",
54
- "./worker.mjs"
57
+ "./esm/master/register.mjs",
58
+ "./esm/worker/index.mjs"
55
59
  ],
56
60
  "repository": {
57
61
  "type": "git",
@@ -94,22 +98,25 @@
94
98
  "@rollup/plugin-commonjs": "^29.0.3",
95
99
  "@rollup/plugin-node-resolve": "^16.0.3",
96
100
  "@types/debug": "^4.1.12",
97
- "@types/execa": "^2.0.0",
98
101
  "@types/node": "^20.19.0",
99
102
  "ava": "^7.0.0",
100
- "c8": "^10.1.3",
103
+ "c8": "^11.0.0",
101
104
  "esbuild": "^0.25.12",
102
105
  "eslint": "^10.5.0",
103
- "eslint-config-prettier": "^9.1.2",
104
- "execa": "^4.0.3",
106
+ "eslint-config-prettier": "^10.1.8",
105
107
  "globals": "^16.5.0",
108
+ "istanbul-lib-coverage": "^3.2.2",
109
+ "istanbul-lib-instrument": "^6.0.3",
110
+ "istanbul-lib-report": "^3.0.1",
111
+ "istanbul-lib-source-maps": "^5.0.6",
112
+ "istanbul-reports": "^3.2.0",
106
113
  "prettier": "^3.8.4",
107
114
  "rimraf": "^6.0.1",
108
115
  "rollup": "^4.62.0",
109
116
  "ts-loader": "^9.6.1",
110
117
  "tsx": "^4.22.4",
111
- "typescript": "^5.9.3",
112
- "typescript-eslint": "^8.61.1",
118
+ "typescript": "^6.0.3",
119
+ "typescript-eslint": "^8.62.1",
113
120
  "webpack": "^5.107.2"
114
121
  },
115
122
  "ava": {
@@ -142,12 +149,13 @@
142
149
  "reporter": [
143
150
  "text-summary",
144
151
  "text",
145
- "lcov"
152
+ "lcov",
153
+ "json"
146
154
  ],
147
155
  "check-coverage": true,
148
- "lines": 91,
149
- "branches": 80,
150
- "functions": 90
156
+ "lines": 94,
157
+ "branches": 84,
158
+ "functions": 91
151
159
  },
152
160
  "browser": {
153
161
  "callsites": false,
@@ -171,9 +179,9 @@
171
179
  "files": [
172
180
  "dist/**",
173
181
  "dist-esm/**",
174
- "index.mjs",
175
- "observable.mjs",
176
- "register.mjs",
177
- "worker.mjs"
178
- ]
182
+ "esm/**"
183
+ ],
184
+ "overrides": {
185
+ "js-yaml": "^4.1.0"
186
+ }
179
187
  }
package/index.mjs DELETED
@@ -1,4 +0,0 @@
1
- // ESM entry for Node. Re-exports the CommonJS build (which is runtime-safe in
2
- // both module systems); bundlers get the tree-shakeable dist-esm/ build via the
3
- // "module" condition in package.json#exports.
4
- export * from "./dist/index.js"
package/observable.mjs DELETED
@@ -1 +0,0 @@
1
- export * from "./dist/observable.js"
package/register.mjs DELETED
@@ -1,2 +0,0 @@
1
- // Side-effect only: registers the threads.js Worker as the global Worker.
2
- import "./dist/master/register.js"
package/worker.mjs DELETED
@@ -1 +0,0 @@
1
- export * from "./dist/worker/index.js"