@coherentglobal/wasm-runner 0.0.103 → 0.1.4
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/CancellationToken.js.map +1 -1
- package/dist/browser/logger.js.map +1 -1
- package/dist/browser/template/runtime.template.js.map +1 -1
- package/dist/browser/template/worker.template.js.map +1 -1
- package/dist/browser.js.map +1 -1
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +14 -0
- package/dist/constants.js.map +1 -0
- package/dist/mockConstants.d.ts +5 -0
- package/dist/mockConstants.js +14 -0
- package/dist/mockConstants.js.map +1 -0
- package/dist/node/logger.d.ts +1 -4
- package/dist/node/template/main.template.ejs +55 -28
- package/dist/node/threads/mockWorkerThread.d.ts +28 -0
- package/dist/node/threads/mockWorkerThread.js +89 -0
- package/dist/node/threads/mockWorkerThread.js.map +1 -0
- package/dist/node/threads/workerPool.d.ts +19 -0
- package/dist/node/threads/workerPool.js +31 -0
- package/dist/node/threads/workerPool.js.map +1 -0
- package/dist/node/threads/workerPool.ts +36 -0
- package/dist/node/threads/workerThread.d.ts +30 -0
- package/dist/node/threads/workerThread.js +83 -0
- package/dist/node/threads/workerThread.js.map +1 -0
- package/dist/node/threads/workerThread.ts +85 -0
- package/dist/node.d.ts +20 -25
- package/dist/node.js +105 -40
- package/dist/node.js.map +1 -1
- package/dist/serializer/columnarSerializer.js.map +1 -1
- package/dist/types.d.ts +9 -2
- package/dist/utils.d.ts +26 -0
- package/dist/utils.js +33 -1
- package/dist/utils.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const events = require("events");
|
|
2
|
+
const { isMainThread, parentPort } = require("worker_threads");
|
|
3
|
+
const { createId } = require("@paralleldrive/cuid2");
|
|
4
|
+
const { ThreadWorker } = require("poolifier");
|
|
5
|
+
// eslint-disable-next-line import/no-unresolved, import/extensions
|
|
6
|
+
const { THREAD_EVENT_TYPES } = require("../../mockConstants");
|
|
7
|
+
|
|
8
|
+
class WorkerThread extends ThreadWorker {
|
|
9
|
+
// eslint-disable-next-line no-useless-constructor
|
|
10
|
+
constructor(taskFunctions, opts) {
|
|
11
|
+
super(taskFunctions, opts);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Sends message back to the main thread.
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} message
|
|
18
|
+
*/
|
|
19
|
+
postMessage(message) {
|
|
20
|
+
if (isMainThread) {
|
|
21
|
+
throw Error("Failed to post message using main thread.");
|
|
22
|
+
} else {
|
|
23
|
+
parentPort.postMessage(message);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Receives message from the main thread or other threads.
|
|
29
|
+
*
|
|
30
|
+
* @param {Function} callback
|
|
31
|
+
* Function to receive the message data.
|
|
32
|
+
*/
|
|
33
|
+
onMessage(callback) {
|
|
34
|
+
if (isMainThread) {
|
|
35
|
+
throw Error("Failed to receive message using main thread.");
|
|
36
|
+
} else {
|
|
37
|
+
parentPort.on("message", callback);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Sends a request to the main thread and waits for a response.
|
|
43
|
+
*
|
|
44
|
+
* @param {Object} payload
|
|
45
|
+
* Object or details that needs to be passed to main thread as `payload`.
|
|
46
|
+
*
|
|
47
|
+
* @returns {Promise<Object>}
|
|
48
|
+
* Response from main thread.
|
|
49
|
+
*/
|
|
50
|
+
async requestMessage(payload, context) {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const eventId = createId();
|
|
53
|
+
const eventEmitter = new events.EventEmitter();
|
|
54
|
+
|
|
55
|
+
// Prepare to process the response from parent
|
|
56
|
+
eventEmitter.once(eventId, (response) => {
|
|
57
|
+
eventEmitter.removeAllListeners(eventId);
|
|
58
|
+
if (response.type === THREAD_EVENT_TYPES.ERROR) {
|
|
59
|
+
reject(response?.payload);
|
|
60
|
+
} else {
|
|
61
|
+
resolve(response?.payload);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Send the request to parent
|
|
66
|
+
this.postMessage({
|
|
67
|
+
payload,
|
|
68
|
+
context,
|
|
69
|
+
type: THREAD_EVENT_TYPES.REQUEST_EXECUTE,
|
|
70
|
+
eventId,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Receive the response from parent once ready
|
|
74
|
+
this.onMessage((message) => {
|
|
75
|
+
if (
|
|
76
|
+
message.eventId === eventId &&
|
|
77
|
+
(message?.type === THREAD_EVENT_TYPES.RESPONSE ||
|
|
78
|
+
message?.type === THREAD_EVENT_TYPES.ERROR)
|
|
79
|
+
) {
|
|
80
|
+
eventEmitter.emit(eventId, message);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = {
|
|
88
|
+
default: WorkerThread,
|
|
89
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mockWorkerThread.js","sourceRoot":"","sources":["../../../src/node/threads/mockWorkerThread.js"],"names":[],"mappings":";;;;;;;;;AAAA,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AACjC,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAC/D,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAC;AACrD,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AAC9C,mEAAmE;AACnE,MAAM,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;AAE9D,MAAM,YAAa,SAAQ,YAAY;IACrC,kDAAkD;IAClD,YAAY,aAAa,EAAE,IAAI;QAC7B,KAAK,CAAC,aAAa,EAAE,IAAI,CAAC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O;iBACR,CAAC,CAAC;gBAEH,8CAA8C;gBAC9C,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,EAAE;oBACzB,IACE,OAAO,CAAC,OAAO,KAAK,OAAO;wBAC3B,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,MAAK,kBAAkB,CAAC,QAAQ;4BAC5C,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,MAAK,kBAAkB,CAAC,KAAK,CAAC,EAC7C,CAAC;wBACD,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;oBACtC,CAAC;gBACH,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;KAAA;CACF;AAED,MAAM,CAAC,OAAO,GAAG;IACf,OAAO,EAAE,YAAY;CACtB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FixedThreadPool } from "poolifier";
|
|
2
|
+
import type { ThreadPoolOptions } from "poolifier";
|
|
3
|
+
declare class WorkerPool extends FixedThreadPool {
|
|
4
|
+
constructor(numberOfThreads: number, filePath: string, opts?: ThreadPoolOptions);
|
|
5
|
+
/**
|
|
6
|
+
* Sends message to a specific worker thread or broadcasts the message
|
|
7
|
+
* to all running threads.
|
|
8
|
+
*
|
|
9
|
+
* @param message
|
|
10
|
+
* Any data that needs to passed to the worker.
|
|
11
|
+
*
|
|
12
|
+
* @param workerIndex
|
|
13
|
+
* Optional. Zero base index of the worker that will receive the message. Leave
|
|
14
|
+
* this field empty to enable the message broadcast to all workers.
|
|
15
|
+
*/
|
|
16
|
+
postMessage(message: unknown, workerIndex?: number): void;
|
|
17
|
+
}
|
|
18
|
+
export default WorkerPool;
|
|
19
|
+
export type { FixedThreadPool };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const poolifier_1 = require("poolifier");
|
|
4
|
+
class WorkerPool extends poolifier_1.FixedThreadPool {
|
|
5
|
+
constructor(numberOfThreads, filePath, opts) {
|
|
6
|
+
super(numberOfThreads, filePath, opts);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Sends message to a specific worker thread or broadcasts the message
|
|
10
|
+
* to all running threads.
|
|
11
|
+
*
|
|
12
|
+
* @param message
|
|
13
|
+
* Any data that needs to passed to the worker.
|
|
14
|
+
*
|
|
15
|
+
* @param workerIndex
|
|
16
|
+
* Optional. Zero base index of the worker that will receive the message. Leave
|
|
17
|
+
* this field empty to enable the message broadcast to all workers.
|
|
18
|
+
*/
|
|
19
|
+
postMessage(message, workerIndex) {
|
|
20
|
+
if (workerIndex && workerIndex < this.workerNodes.length) {
|
|
21
|
+
this.workerNodes[workerIndex].worker.postMessage(message);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
for (let i = 0; i < this.workerNodes.length; i++) {
|
|
25
|
+
this.workerNodes[i].worker.postMessage(message);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.default = WorkerPool;
|
|
31
|
+
//# sourceMappingURL=workerPool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workerPool.js","sourceRoot":"","sources":["../../../src/node/threads/workerPool.ts"],"names":[],"mappings":";;AAAA,yCAA4C;AAG5C,MAAM,UAAW,SAAQ,2BAAe;IACtC,YACE,eAAuB,EACvB,QAAgB,EAChB,IAAwB;QAExB,KAAK,CAAC,eAAe,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IACzC,CAAC;IAED;;;;;;;;;;OAUG;IACH,WAAW,CAAC,OAAgB,EAAE,WAAoB;QAChD,IAAI,WAAW,IAAI,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC;YACzD,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAC5D,CAAC;aAAM,CAAC;YACN,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;gBACjD,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;YAClD,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED,kBAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { FixedThreadPool } from "poolifier";
|
|
2
|
+
import type { ThreadPoolOptions } from "poolifier";
|
|
3
|
+
|
|
4
|
+
class WorkerPool extends FixedThreadPool {
|
|
5
|
+
constructor(
|
|
6
|
+
numberOfThreads: number,
|
|
7
|
+
filePath: string,
|
|
8
|
+
opts?: ThreadPoolOptions
|
|
9
|
+
) {
|
|
10
|
+
super(numberOfThreads, filePath, opts);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Sends message to a specific worker thread or broadcasts the message
|
|
15
|
+
* to all running threads.
|
|
16
|
+
*
|
|
17
|
+
* @param message
|
|
18
|
+
* Any data that needs to passed to the worker.
|
|
19
|
+
*
|
|
20
|
+
* @param workerIndex
|
|
21
|
+
* Optional. Zero base index of the worker that will receive the message. Leave
|
|
22
|
+
* this field empty to enable the message broadcast to all workers.
|
|
23
|
+
*/
|
|
24
|
+
postMessage(message: unknown, workerIndex?: number) {
|
|
25
|
+
if (workerIndex && workerIndex < this.workerNodes.length) {
|
|
26
|
+
this.workerNodes[workerIndex].worker.postMessage(message);
|
|
27
|
+
} else {
|
|
28
|
+
for (let i = 0; i < this.workerNodes.length; i++) {
|
|
29
|
+
this.workerNodes[i].worker.postMessage(message);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default WorkerPool;
|
|
36
|
+
export type { FixedThreadPool };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ThreadWorker } from "poolifier";
|
|
2
|
+
import type { TaskFunctions, TaskFunction, WorkerOptions } from "poolifier";
|
|
3
|
+
import type { EventRequestData } from "../../types";
|
|
4
|
+
declare class WorkerThread extends ThreadWorker {
|
|
5
|
+
constructor(taskFunctions: TaskFunction<unknown, Response> | TaskFunctions<unknown, Response>, opts?: WorkerOptions);
|
|
6
|
+
/**
|
|
7
|
+
* Sends message back to the main thread.
|
|
8
|
+
*
|
|
9
|
+
* @param {EventRequestData} message
|
|
10
|
+
*/
|
|
11
|
+
postMessage(message: EventRequestData): void;
|
|
12
|
+
/**
|
|
13
|
+
* Receives message from the main thread or other threads.
|
|
14
|
+
*
|
|
15
|
+
* @param {Function} callback
|
|
16
|
+
* Function to receive the message data.
|
|
17
|
+
*/
|
|
18
|
+
onMessage(callback: (value?: any) => void): void;
|
|
19
|
+
/**
|
|
20
|
+
* Sends a request to the main thread and waits for a response.
|
|
21
|
+
*
|
|
22
|
+
* @param {Object} payload
|
|
23
|
+
* Object or details that needs to be passed to main thread as `payload`.
|
|
24
|
+
*
|
|
25
|
+
* @returns {Promise<Object>}
|
|
26
|
+
* Response from main thread.
|
|
27
|
+
*/
|
|
28
|
+
requestMessage(payload: any, context: any): Promise<unknown>;
|
|
29
|
+
}
|
|
30
|
+
export default WorkerThread;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const worker_threads_1 = require("worker_threads");
|
|
13
|
+
const cuid2_1 = require("@paralleldrive/cuid2");
|
|
14
|
+
const poolifier_1 = require("poolifier");
|
|
15
|
+
const constants_1 = require("../../constants");
|
|
16
|
+
class WorkerThread extends poolifier_1.ThreadWorker {
|
|
17
|
+
constructor(taskFunctions, opts) {
|
|
18
|
+
super(taskFunctions, opts);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Sends message back to the main thread.
|
|
22
|
+
*
|
|
23
|
+
* @param {EventRequestData} message
|
|
24
|
+
*/
|
|
25
|
+
postMessage(message) {
|
|
26
|
+
if (worker_threads_1.isMainThread) {
|
|
27
|
+
throw Error("Failed to post message using main thread.");
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
worker_threads_1.parentPort.postMessage(message);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Receives message from the main thread or other threads.
|
|
35
|
+
*
|
|
36
|
+
* @param {Function} callback
|
|
37
|
+
* Function to receive the message data.
|
|
38
|
+
*/
|
|
39
|
+
onMessage(callback) {
|
|
40
|
+
if (worker_threads_1.isMainThread) {
|
|
41
|
+
throw Error("Failed to receive message using main thread.");
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
worker_threads_1.parentPort.on("message", callback);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Sends a request to the main thread and waits for a response.
|
|
49
|
+
*
|
|
50
|
+
* @param {Object} payload
|
|
51
|
+
* Object or details that needs to be passed to main thread as `payload`.
|
|
52
|
+
*
|
|
53
|
+
* @returns {Promise<Object>}
|
|
54
|
+
* Response from main thread.
|
|
55
|
+
*/
|
|
56
|
+
requestMessage(payload, context) {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
const eventId = (0, cuid2_1.createId)();
|
|
60
|
+
// Send the request to parent
|
|
61
|
+
this.postMessage({
|
|
62
|
+
payload,
|
|
63
|
+
context,
|
|
64
|
+
type: constants_1.THREAD_EVENT_TYPES.REQUEST_EXECUTE,
|
|
65
|
+
eventId,
|
|
66
|
+
});
|
|
67
|
+
// Receive the response from parent once ready
|
|
68
|
+
this.onMessage((message) => {
|
|
69
|
+
if (message.eventId === eventId &&
|
|
70
|
+
(message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.RESPONSE) {
|
|
71
|
+
resolve(message === null || message === void 0 ? void 0 : message.payload);
|
|
72
|
+
}
|
|
73
|
+
else if (message.eventId === eventId &&
|
|
74
|
+
(message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.ERROR) {
|
|
75
|
+
reject(message === null || message === void 0 ? void 0 : message.payload);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.default = WorkerThread;
|
|
83
|
+
//# sourceMappingURL=workerThread.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workerThread.js","sourceRoot":"","sources":["../../../src/node/threads/workerThread.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mDAA0D;AAC1D,gDAAwD;AACxD,yCAAyC;AACzC,+CAAqD;AAKrD,MAAM,YAAa,SAAQ,wBAAY;IACrC,YACE,aAEoC,EACpC,IAAoB;QAEpB,KAAK,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,WAAW,CAAC,OAAyB;QACnC,IAAI,6BAAY,EAAE,CAAC;YACjB,MAAM,KAAK,CAAC,2CAA2C,CAAC,CAAC;QAC3D,CAAC;aAAM,CAAC;YACN,2BAAU,CAAC,WAAW,CAA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@@
|
|
|
1
|
+
import { isMainThread, parentPort } from "worker_threads";
|
|
2
|
+
import { createId as cuid } from "@paralleldrive/cuid2";
|
|
3
|
+
import { ThreadWorker } from "poolifier";
|
|
4
|
+
import { THREAD_EVENT_TYPES } from "../../constants";
|
|
5
|
+
|
|
6
|
+
import type { TaskFunctions, TaskFunction, WorkerOptions } from "poolifier";
|
|
7
|
+
import type { EVENT_TYPE, EventRequestData } from "../../types";
|
|
8
|
+
|
|
9
|
+
class WorkerThread extends ThreadWorker {
|
|
10
|
+
constructor(
|
|
11
|
+
taskFunctions:
|
|
12
|
+
| TaskFunction<unknown, Response>
|
|
13
|
+
| TaskFunctions<unknown, Response>,
|
|
14
|
+
opts?: WorkerOptions
|
|
15
|
+
) {
|
|
16
|
+
super(taskFunctions, opts);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sends message back to the main thread.
|
|
21
|
+
*
|
|
22
|
+
* @param {EventRequestData} message
|
|
23
|
+
*/
|
|
24
|
+
postMessage(message: EventRequestData) {
|
|
25
|
+
if (isMainThread) {
|
|
26
|
+
throw Error("Failed to post message using main thread.");
|
|
27
|
+
} else {
|
|
28
|
+
parentPort.postMessage(message);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Receives message from the main thread or other threads.
|
|
34
|
+
*
|
|
35
|
+
* @param {Function} callback
|
|
36
|
+
* Function to receive the message data.
|
|
37
|
+
*/
|
|
38
|
+
onMessage(callback: (value?: any) => void) {
|
|
39
|
+
if (isMainThread) {
|
|
40
|
+
throw Error("Failed to receive message using main thread.");
|
|
41
|
+
} else {
|
|
42
|
+
parentPort.on("message", callback);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Sends a request to the main thread and waits for a response.
|
|
48
|
+
*
|
|
49
|
+
* @param {Object} payload
|
|
50
|
+
* Object or details that needs to be passed to main thread as `payload`.
|
|
51
|
+
*
|
|
52
|
+
* @returns {Promise<Object>}
|
|
53
|
+
* Response from main thread.
|
|
54
|
+
*/
|
|
55
|
+
async requestMessage(payload: any, context: any) {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
const eventId = cuid();
|
|
58
|
+
|
|
59
|
+
// Send the request to parent
|
|
60
|
+
this.postMessage({
|
|
61
|
+
payload,
|
|
62
|
+
context,
|
|
63
|
+
type: THREAD_EVENT_TYPES.REQUEST_EXECUTE as EVENT_TYPE,
|
|
64
|
+
eventId,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Receive the response from parent once ready
|
|
68
|
+
this.onMessage((message: EventRequestData) => {
|
|
69
|
+
if (
|
|
70
|
+
message.eventId === eventId &&
|
|
71
|
+
message?.type === THREAD_EVENT_TYPES.RESPONSE
|
|
72
|
+
) {
|
|
73
|
+
resolve(message?.payload);
|
|
74
|
+
} else if (
|
|
75
|
+
message.eventId === eventId &&
|
|
76
|
+
message?.type === THREAD_EVENT_TYPES.ERROR
|
|
77
|
+
) {
|
|
78
|
+
reject(message?.payload);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default WorkerThread;
|
package/dist/node.d.ts
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
1
1
|
import tmp from "tmp";
|
|
2
|
-
import {
|
|
3
|
-
import { NodeModel, RunnerConfig, ModelConfig, ResponseObject } from "./types";
|
|
4
|
-
import { CancellationToken } from
|
|
2
|
+
import type { FixedThreadPool } from "./node/threads/workerPool";
|
|
3
|
+
import type { NodeModel, RunnerConfig, ModelConfig, ResponseObject } from "./types";
|
|
4
|
+
import { CancellationToken } from "./CancellationToken";
|
|
5
5
|
export { ColumnarSerializer } from "./serializer/columnarSerializer";
|
|
6
|
-
export { CancellationToken } from
|
|
6
|
+
export { CancellationToken } from "./CancellationToken";
|
|
7
7
|
export { ModelExecuteCancelled } from "./error";
|
|
8
|
-
/**
|
|
9
|
-
* WASM runner config
|
|
10
|
-
* @typedef {Object} RunnerConfig
|
|
11
|
-
* @property {string} id - Model id or version_uuid
|
|
12
|
-
* @property {string} url - Model url
|
|
13
|
-
*/
|
|
14
|
-
/**
|
|
15
|
-
* Input object
|
|
16
|
-
* @typedef {Object} InputObject
|
|
17
|
-
* @property {Object} request_data - Request input date
|
|
18
|
-
* @property {Object} request_meta - Request input meta
|
|
19
|
-
*/
|
|
20
8
|
/**
|
|
21
9
|
* WasmRunner class, responsible for managing model and it's lifecycle
|
|
22
10
|
*
|
|
@@ -28,7 +16,8 @@ export declare class WasmRunner {
|
|
|
28
16
|
_tempModelFolder: tmp.DirResult;
|
|
29
17
|
license: string;
|
|
30
18
|
models: any[];
|
|
31
|
-
externalResolverModule:
|
|
19
|
+
externalResolverModule: any;
|
|
20
|
+
initializingModels: string[];
|
|
32
21
|
/**
|
|
33
22
|
* Create new Wasm Runner instance
|
|
34
23
|
*
|
|
@@ -42,19 +31,25 @@ export declare class WasmRunner {
|
|
|
42
31
|
* @function initialize
|
|
43
32
|
*/
|
|
44
33
|
initialize(license?: string): Promise<void>;
|
|
45
|
-
_createModelInstance(id: any, url: any, workerFolder: any, size: any): Promise<
|
|
34
|
+
_createModelInstance(id: any, url: any, workerFolder: any, size: any): Promise<FixedThreadPool<any, any>>;
|
|
46
35
|
_initializeModelInstance(m: NodeModel): Promise<void>;
|
|
47
|
-
_createModelInstanceFromURL(id: string, url: string, workerFolder: string, modelFolder: string, size: number): Promise<
|
|
36
|
+
_createModelInstanceFromURL(id: string, url: string, workerFolder: string, modelFolder: string, size: number): Promise<FixedThreadPool<any, any>>;
|
|
48
37
|
/**
|
|
49
|
-
* Append and
|
|
38
|
+
* Append and initialize model in preparation for the `execute()`.
|
|
39
|
+
*
|
|
50
40
|
* @param {ModelConfig} modelConfig
|
|
51
|
-
*
|
|
41
|
+
* Model details like version_id as `id` and zip path as `url`.
|
|
42
|
+
*
|
|
43
|
+
* @returns {Promise<void>}
|
|
52
44
|
*/
|
|
53
45
|
append(modelConfig: ModelConfig): Promise<void>;
|
|
54
46
|
/**
|
|
55
|
-
* Remove model from
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
47
|
+
* Remove model from the list of initialized model useful for GC.
|
|
48
|
+
*
|
|
49
|
+
* @param {String} id
|
|
50
|
+
* ID of the model that needs to be removed.
|
|
51
|
+
*
|
|
52
|
+
* @returns {Promise<void>}
|
|
58
53
|
*/
|
|
59
54
|
remove(id: string): Promise<void>;
|
|
60
55
|
_parseError(rawResponse: any): Object;
|
|
@@ -62,7 +57,7 @@ export declare class WasmRunner {
|
|
|
62
57
|
/**
|
|
63
58
|
* Check if model existed
|
|
64
59
|
* @param {string} id Model id
|
|
65
|
-
* @returns boolean
|
|
60
|
+
* @returns {boolean}
|
|
66
61
|
*/
|
|
67
62
|
isExist(id: string): boolean;
|
|
68
63
|
/**
|