defuss-multicore 0.0.2 → 0.0.3
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/dist/index.cjs
CHANGED
|
@@ -21,6 +21,7 @@ const serializeFunction = (fn) => {
|
|
|
21
21
|
'use strict';
|
|
22
22
|
|
|
23
23
|
const __fn = ${fnString};
|
|
24
|
+
let __nodeParentPort = null;
|
|
24
25
|
|
|
25
26
|
const __isTypedArray = (v) =>
|
|
26
27
|
ArrayBuffer.isView(v) && !(v instanceof DataView);
|
|
@@ -39,18 +40,27 @@ const __getTransferables = (value) => {
|
|
|
39
40
|
return [];
|
|
40
41
|
};
|
|
41
42
|
|
|
43
|
+
const __dispatch = (msg, transfer) => {
|
|
44
|
+
if (typeof postMessage === 'function') {
|
|
45
|
+
postMessage(msg, transfer ?? []);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (__nodeParentPort) {
|
|
50
|
+
__nodeParentPort.postMessage(msg, transfer ?? []);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
throw new Error('Node worker parentPort is not ready.');
|
|
55
|
+
};
|
|
56
|
+
|
|
42
57
|
const __handleMessage = (data) => {
|
|
43
58
|
if (data.type === 'execute') {
|
|
44
59
|
try {
|
|
45
60
|
const result = __fn(...data.args);
|
|
46
61
|
const transfer = __getTransferables(result);
|
|
47
62
|
const msg = { type: 'result', id: data.id, value: result };
|
|
48
|
-
|
|
49
|
-
postMessage(msg, transfer);
|
|
50
|
-
} else {
|
|
51
|
-
const { parentPort } = require('node:worker_threads');
|
|
52
|
-
parentPort.postMessage(msg, transfer);
|
|
53
|
-
}
|
|
63
|
+
__dispatch(msg, transfer);
|
|
54
64
|
} catch (err) {
|
|
55
65
|
const msg = {
|
|
56
66
|
type: 'error',
|
|
@@ -58,12 +68,7 @@ const __handleMessage = (data) => {
|
|
|
58
68
|
error: err instanceof Error ? err.message : String(err),
|
|
59
69
|
stack: err instanceof Error ? err.stack : undefined,
|
|
60
70
|
};
|
|
61
|
-
|
|
62
|
-
postMessage(msg);
|
|
63
|
-
} else {
|
|
64
|
-
const { parentPort } = require('node:worker_threads');
|
|
65
|
-
parentPort.postMessage(msg);
|
|
66
|
-
}
|
|
71
|
+
__dispatch(msg);
|
|
67
72
|
}
|
|
68
73
|
}
|
|
69
74
|
// 'abort' type is a no-op; the caller terminates the worker externally
|
|
@@ -75,15 +80,17 @@ if (typeof self !== 'undefined' && typeof self.onmessage !== 'undefined') {
|
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
// Node.js worker_threads
|
|
78
|
-
if (typeof
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
if (typeof postMessage !== 'function') {
|
|
84
|
+
import('node:worker_threads')
|
|
85
|
+
.then(({ parentPort }) => {
|
|
86
|
+
__nodeParentPort = parentPort ?? null;
|
|
87
|
+
if (__nodeParentPort) {
|
|
88
|
+
__nodeParentPort.on('message', __handleMessage);
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
.catch(() => {
|
|
92
|
+
// Not in worker_threads context - ignore
|
|
93
|
+
});
|
|
87
94
|
}
|
|
88
95
|
`.trim();
|
|
89
96
|
};
|
|
@@ -164,12 +171,16 @@ const getCoreCount = () => {
|
|
|
164
171
|
if (typeof navigator !== "undefined" && navigator.hardwareConcurrency) {
|
|
165
172
|
return navigator.hardwareConcurrency;
|
|
166
173
|
}
|
|
174
|
+
const nodeProcess = typeof process !== "undefined" ? process : null;
|
|
167
175
|
try {
|
|
168
|
-
const
|
|
169
|
-
|
|
176
|
+
const getBuiltinModule = nodeProcess?.getBuiltinModule;
|
|
177
|
+
if (typeof getBuiltinModule === "function") {
|
|
178
|
+
const os = getBuiltinModule("node:os");
|
|
179
|
+
return os.cpus().length;
|
|
180
|
+
}
|
|
170
181
|
} catch {
|
|
171
|
-
return 4;
|
|
172
182
|
}
|
|
183
|
+
return 4;
|
|
173
184
|
};
|
|
174
185
|
|
|
175
186
|
const isBrowser = () => typeof globalThis !== "undefined" && typeof globalThis.Worker === "function";
|
|
@@ -180,8 +191,17 @@ const getPoolFactory = async () => {
|
|
|
180
191
|
const { createBrowserPool } = await Promise.resolve().then(function () { return require('./pool-browser-BsBdOUwc.cjs'); });
|
|
181
192
|
createPoolFn = createBrowserPool;
|
|
182
193
|
} else {
|
|
183
|
-
|
|
184
|
-
|
|
194
|
+
try {
|
|
195
|
+
const [{ createNodePool }, { Worker }] = await Promise.all([
|
|
196
|
+
Promise.resolve().then(function () { return require('./pool-node-DUsc5lQk.cjs'); }),
|
|
197
|
+
import('node:worker_threads')
|
|
198
|
+
]);
|
|
199
|
+
createPoolFn = (script, config) => createNodePool(script, Worker, config);
|
|
200
|
+
} catch {
|
|
201
|
+
throw new Error(
|
|
202
|
+
"[defuss-multicore] node:worker_threads not available. Are you running in Node.js?"
|
|
203
|
+
);
|
|
204
|
+
}
|
|
185
205
|
}
|
|
186
206
|
return createPoolFn;
|
|
187
207
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
const isTypedArray = (v) => ArrayBuffer.isView(v) && !(v instanceof DataView);
|
|
2
2
|
const isCallOptions = (v) => typeof v === "object" && v !== null && !Array.isArray(v) && !ArrayBuffer.isView(v) && ("cores" in v || "signal" in v || "transfer" in v);
|
|
3
3
|
|
|
4
4
|
const warnIfClosure = (fn) => {
|
|
@@ -19,6 +19,7 @@ const serializeFunction = (fn) => {
|
|
|
19
19
|
'use strict';
|
|
20
20
|
|
|
21
21
|
const __fn = ${fnString};
|
|
22
|
+
let __nodeParentPort = null;
|
|
22
23
|
|
|
23
24
|
const __isTypedArray = (v) =>
|
|
24
25
|
ArrayBuffer.isView(v) && !(v instanceof DataView);
|
|
@@ -37,18 +38,27 @@ const __getTransferables = (value) => {
|
|
|
37
38
|
return [];
|
|
38
39
|
};
|
|
39
40
|
|
|
41
|
+
const __dispatch = (msg, transfer) => {
|
|
42
|
+
if (typeof postMessage === 'function') {
|
|
43
|
+
postMessage(msg, transfer ?? []);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (__nodeParentPort) {
|
|
48
|
+
__nodeParentPort.postMessage(msg, transfer ?? []);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw new Error('Node worker parentPort is not ready.');
|
|
53
|
+
};
|
|
54
|
+
|
|
40
55
|
const __handleMessage = (data) => {
|
|
41
56
|
if (data.type === 'execute') {
|
|
42
57
|
try {
|
|
43
58
|
const result = __fn(...data.args);
|
|
44
59
|
const transfer = __getTransferables(result);
|
|
45
60
|
const msg = { type: 'result', id: data.id, value: result };
|
|
46
|
-
|
|
47
|
-
postMessage(msg, transfer);
|
|
48
|
-
} else {
|
|
49
|
-
const { parentPort } = require('node:worker_threads');
|
|
50
|
-
parentPort.postMessage(msg, transfer);
|
|
51
|
-
}
|
|
61
|
+
__dispatch(msg, transfer);
|
|
52
62
|
} catch (err) {
|
|
53
63
|
const msg = {
|
|
54
64
|
type: 'error',
|
|
@@ -56,12 +66,7 @@ const __handleMessage = (data) => {
|
|
|
56
66
|
error: err instanceof Error ? err.message : String(err),
|
|
57
67
|
stack: err instanceof Error ? err.stack : undefined,
|
|
58
68
|
};
|
|
59
|
-
|
|
60
|
-
postMessage(msg);
|
|
61
|
-
} else {
|
|
62
|
-
const { parentPort } = require('node:worker_threads');
|
|
63
|
-
parentPort.postMessage(msg);
|
|
64
|
-
}
|
|
69
|
+
__dispatch(msg);
|
|
65
70
|
}
|
|
66
71
|
}
|
|
67
72
|
// 'abort' type is a no-op; the caller terminates the worker externally
|
|
@@ -73,15 +78,17 @@ if (typeof self !== 'undefined' && typeof self.onmessage !== 'undefined') {
|
|
|
73
78
|
}
|
|
74
79
|
|
|
75
80
|
// Node.js worker_threads
|
|
76
|
-
if (typeof
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
if (typeof postMessage !== 'function') {
|
|
82
|
+
import('node:worker_threads')
|
|
83
|
+
.then(({ parentPort }) => {
|
|
84
|
+
__nodeParentPort = parentPort ?? null;
|
|
85
|
+
if (__nodeParentPort) {
|
|
86
|
+
__nodeParentPort.on('message', __handleMessage);
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
.catch(() => {
|
|
90
|
+
// Not in worker_threads context - ignore
|
|
91
|
+
});
|
|
85
92
|
}
|
|
86
93
|
`.trim();
|
|
87
94
|
};
|
|
@@ -162,12 +169,16 @@ const getCoreCount = () => {
|
|
|
162
169
|
if (typeof navigator !== "undefined" && navigator.hardwareConcurrency) {
|
|
163
170
|
return navigator.hardwareConcurrency;
|
|
164
171
|
}
|
|
172
|
+
const nodeProcess = typeof process !== "undefined" ? process : null;
|
|
165
173
|
try {
|
|
166
|
-
const
|
|
167
|
-
|
|
174
|
+
const getBuiltinModule = nodeProcess?.getBuiltinModule;
|
|
175
|
+
if (typeof getBuiltinModule === "function") {
|
|
176
|
+
const os = getBuiltinModule("node:os");
|
|
177
|
+
return os.cpus().length;
|
|
178
|
+
}
|
|
168
179
|
} catch {
|
|
169
|
-
return 4;
|
|
170
180
|
}
|
|
181
|
+
return 4;
|
|
171
182
|
};
|
|
172
183
|
|
|
173
184
|
const isBrowser = () => typeof globalThis !== "undefined" && typeof globalThis.Worker === "function";
|
|
@@ -178,8 +189,17 @@ const getPoolFactory = async () => {
|
|
|
178
189
|
const { createBrowserPool } = await import('./pool-browser-IHMOtzRq.mjs');
|
|
179
190
|
createPoolFn = createBrowserPool;
|
|
180
191
|
} else {
|
|
181
|
-
|
|
182
|
-
|
|
192
|
+
try {
|
|
193
|
+
const [{ createNodePool }, { Worker }] = await Promise.all([
|
|
194
|
+
import('./pool-node-CmCaEceF.mjs'),
|
|
195
|
+
import('node:worker_threads')
|
|
196
|
+
]);
|
|
197
|
+
createPoolFn = (script, config) => createNodePool(script, Worker, config);
|
|
198
|
+
} catch {
|
|
199
|
+
throw new Error(
|
|
200
|
+
"[defuss-multicore] node:worker_threads not available. Are you running in Node.js?"
|
|
201
|
+
);
|
|
202
|
+
}
|
|
183
203
|
}
|
|
184
204
|
return createPoolFn;
|
|
185
205
|
};
|
|
@@ -1,17 +1,7 @@
|
|
|
1
1
|
import { c as createPool } from './pool-DhG3ybwo.mjs';
|
|
2
|
-
import { createRequire } from 'node:module';
|
|
3
2
|
import './index.mjs';
|
|
4
3
|
|
|
5
|
-
const createNodePool = (script, config) => {
|
|
6
|
-
let WorkerClass;
|
|
7
|
-
try {
|
|
8
|
-
const _require = createRequire(import.meta.url);
|
|
9
|
-
WorkerClass = _require("node:worker_threads").Worker;
|
|
10
|
-
} catch {
|
|
11
|
-
throw new Error(
|
|
12
|
-
"[defuss-multicore] node:worker_threads not available. Are you running in Node.js?"
|
|
13
|
-
);
|
|
14
|
-
}
|
|
4
|
+
const createNodePool = (script, WorkerClass, config) => {
|
|
15
5
|
const hooks = {
|
|
16
6
|
createWorker(script2) {
|
|
17
7
|
return new WorkerClass(script2, { eval: true });
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var pool = require('./pool-DqNSYahJ.cjs');
|
|
4
|
+
require('./index.cjs');
|
|
5
|
+
|
|
6
|
+
const createNodePool = (script, WorkerClass, config) => {
|
|
7
|
+
const hooks = {
|
|
8
|
+
createWorker(script2) {
|
|
9
|
+
return new WorkerClass(script2, { eval: true });
|
|
10
|
+
},
|
|
11
|
+
postMessage(worker, data, transfer) {
|
|
12
|
+
worker.postMessage(data, transfer);
|
|
13
|
+
},
|
|
14
|
+
terminateWorker(worker) {
|
|
15
|
+
worker.terminate();
|
|
16
|
+
},
|
|
17
|
+
onMessage(worker, handler) {
|
|
18
|
+
worker.on("message", handler);
|
|
19
|
+
},
|
|
20
|
+
onError(worker, handler) {
|
|
21
|
+
worker.on("error", handler);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
return pool.createPool(script, config ?? {}, hooks);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
exports.createNodePool = createNodePool;
|
package/package.json
CHANGED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var pool = require('./pool-DqNSYahJ.cjs');
|
|
4
|
-
var node_module = require('node:module');
|
|
5
|
-
require('./index.cjs');
|
|
6
|
-
|
|
7
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
8
|
-
const createNodePool = (script, config) => {
|
|
9
|
-
let WorkerClass;
|
|
10
|
-
try {
|
|
11
|
-
const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('pool-node-DKGzL78_.cjs', document.baseURI).href)));
|
|
12
|
-
WorkerClass = _require("node:worker_threads").Worker;
|
|
13
|
-
} catch {
|
|
14
|
-
throw new Error(
|
|
15
|
-
"[defuss-multicore] node:worker_threads not available. Are you running in Node.js?"
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
const hooks = {
|
|
19
|
-
createWorker(script2) {
|
|
20
|
-
return new WorkerClass(script2, { eval: true });
|
|
21
|
-
},
|
|
22
|
-
postMessage(worker, data, transfer) {
|
|
23
|
-
worker.postMessage(data, transfer);
|
|
24
|
-
},
|
|
25
|
-
terminateWorker(worker) {
|
|
26
|
-
worker.terminate();
|
|
27
|
-
},
|
|
28
|
-
onMessage(worker, handler) {
|
|
29
|
-
worker.on("message", handler);
|
|
30
|
-
},
|
|
31
|
-
onError(worker, handler) {
|
|
32
|
-
worker.on("error", handler);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
return pool.createPool(script, config ?? {}, hooks);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
exports.createNodePool = createNodePool;
|