@coherentglobal/wasm-runner 0.1.19 → 0.3.2

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 (50) hide show
  1. package/README.md +16 -0
  2. package/dist/CancellationToken.js +2 -0
  3. package/dist/CancellationToken.js.map +1 -1
  4. package/dist/browser/logger.js +6 -2
  5. package/dist/browser/logger.js.map +1 -1
  6. package/dist/browser/template/worker.template.js +113 -105
  7. package/dist/browser/template/worker.template.js.map +1 -1
  8. package/dist/browser/template.js +1 -1
  9. package/dist/browser.d.ts +10 -3
  10. package/dist/browser.js +255 -257
  11. package/dist/browser.js.map +1 -1
  12. package/dist/constants.d.ts +1 -0
  13. package/dist/constants.js +2 -0
  14. package/dist/constants.js.map +1 -1
  15. package/dist/defaultExternalResolver.js +6 -15
  16. package/dist/defaultExternalResolver.js.map +1 -1
  17. package/dist/error.js +10 -2
  18. package/dist/error.js.map +1 -1
  19. package/dist/node/logger.d.ts +2 -1
  20. package/dist/node/logger.js +1 -1
  21. package/dist/node/logger.js.map +1 -1
  22. package/dist/node/logger.ts +1 -1
  23. package/dist/node/mockLogger.d.ts +2 -1
  24. package/dist/node/template/main.template.ejs +60 -29
  25. package/dist/node/threads/mockWorkerThread.d.ts +1 -0
  26. package/dist/node/threads/mockWorkerThread.js +10 -3
  27. package/dist/node/threads/mockWorkerThread.js.map +1 -1
  28. package/dist/node/threads/workerPool.d.ts +2 -1
  29. package/dist/node/threads/workerPool.js +7 -6
  30. package/dist/node/threads/workerPool.js.map +1 -1
  31. package/dist/node/threads/workerPool.ts +10 -7
  32. package/dist/node/threads/workerThread.d.ts +1 -2
  33. package/dist/node/threads/workerThread.js +35 -42
  34. package/dist/node/threads/workerThread.js.map +1 -1
  35. package/dist/node/threads/workerThread.ts +8 -12
  36. package/dist/node.d.ts +26 -8
  37. package/dist/node.js +596 -437
  38. package/dist/node.js.map +1 -1
  39. package/dist/responseTimeMetric.d.ts +7 -3
  40. package/dist/responseTimeMetric.js +131 -28
  41. package/dist/responseTimeMetric.js.map +1 -1
  42. package/dist/serializer/columnarSerializer.d.ts +11 -3
  43. package/dist/serializer/columnarSerializer.js +49 -29
  44. package/dist/serializer/columnarSerializer.js.map +1 -1
  45. package/dist/types.d.ts +4 -1
  46. package/dist/types.js.map +1 -1
  47. package/dist/utils.d.ts +3 -2
  48. package/dist/utils.js +38 -48
  49. package/dist/utils.js.map +1 -1
  50. package/package.json +70 -59
@@ -1,31 +1,10 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  const worker_threads_1 = require("worker_threads");
13
4
  const poolifier_1 = require("poolifier");
14
5
  const constants_1 = require("../../constants");
15
6
  const activeListeners = new Map();
16
7
  class WorkerThread extends poolifier_1.ThreadWorker {
17
- constructor(taskFunctions, opts) {
18
- super(taskFunctions, opts);
19
- this.cleanupListener = (eventId) => {
20
- setTimeout(() => {
21
- const listener = activeListeners.get(eventId);
22
- if (listener) {
23
- worker_threads_1.parentPort.off("message", listener);
24
- activeListeners.delete(eventId);
25
- }
26
- });
27
- };
28
- }
29
8
  /**
30
9
  * Sends message back to the main thread.
31
10
  *
@@ -53,6 +32,15 @@ class WorkerThread extends poolifier_1.ThreadWorker {
53
32
  worker_threads_1.parentPort.on("message", callback);
54
33
  }
55
34
  }
35
+ cleanupListener = (eventId) => {
36
+ setTimeout(() => {
37
+ const listener = activeListeners.get(eventId);
38
+ if (listener) {
39
+ worker_threads_1.parentPort.off("message", listener);
40
+ activeListeners.delete(eventId);
41
+ }
42
+ });
43
+ };
56
44
  /**
57
45
  * Sends a request to the main thread and waits for a response.
58
46
  *
@@ -62,28 +50,33 @@ class WorkerThread extends poolifier_1.ThreadWorker {
62
50
  * @returns {Promise<Object>}
63
51
  * Response from main thread.
64
52
  */
65
- requestMessage(payload, context) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- return new Promise((resolve, reject) => {
68
- // Send the request to parent
69
- this.postMessage({
70
- payload,
71
- context,
72
- type: constants_1.THREAD_EVENT_TYPES.REQUEST_EXECUTE,
73
- threadId: worker_threads_1.threadId,
74
- });
75
- // Receive the response from parent once ready
76
- const messageListener = (message) => {
77
- worker_threads_1.parentPort.off("message", messageListener);
78
- if ((message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.RESPONSE) {
79
- resolve(message === null || message === void 0 ? void 0 : message.payload);
80
- }
81
- else if ((message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.ERROR) {
82
- reject(message === null || message === void 0 ? void 0 : message.payload);
83
- }
84
- };
85
- this.onMessage(messageListener);
53
+ async requestMessage(payload, context) {
54
+ return new Promise((resolve, reject) => {
55
+ // Send the request to parent
56
+ this.postMessage({
57
+ payload,
58
+ context,
59
+ type: constants_1.THREAD_EVENT_TYPES.REQUEST_EXECUTE,
60
+ threadId: worker_threads_1.threadId,
86
61
  });
62
+ // Receive the response from parent once ready
63
+ const messageListener = (message) => {
64
+ worker_threads_1.parentPort.off("message", messageListener);
65
+ if (message?.type === constants_1.THREAD_EVENT_TYPES.RESPONSE) {
66
+ resolve(message?.payload);
67
+ }
68
+ else if (message?.type === constants_1.THREAD_EVENT_TYPES.ERROR) {
69
+ reject(message?.payload);
70
+ }
71
+ };
72
+ this.onMessage(messageListener);
73
+ });
74
+ }
75
+ reportStats(payload) {
76
+ this.postMessage({
77
+ payload,
78
+ type: constants_1.THREAD_EVENT_TYPES.STATS_REPORT,
79
+ threadId: worker_threads_1.threadId,
87
80
  });
88
81
  }
89
82
  }
@@ -1 +1 @@
1
- {"version":3,"file":"workerThread.js","sourceRoot":"","sources":["../../../src/node/threads/workerThread.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mDAAoE;AAEpE,yCAAyC;AACzC,+CAAqD;AAKrD,MAAM,eAAe,GAAG,IAAI,GAAG,EAAE,CAAC;AAElC,MAAM,YAAa,SAAQ,wBAAY;IACrC,YACE,aAEoC,EACpC,IAAoB;QAEpB,KAAK,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;QA8BrB,oBAAe,GAAG,CAAC,OAAO,EAAE,EAAE;YACpC,UAAU,CAAC,GAAG,EAAE;gBACd,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;gBAC9C,IAAI,QAAQ,EAAE,CAAC;oBACb,2BAAU,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;oBACpC,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IArCF,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,CAAC,OAAO,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,QAA+B;QACvC,IAAI,6BAAY,EAAE,CAAC;YACjB,MAAM,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAC9D,CAAC;aAAM,CAAC;YACN,2BAAU,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IAYD;;;;;;;;OAQG;IACG,cAAc,CAAC,OAAY,EAAE,OAAY;;YAC7C,OAAO,IAAI,O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AAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;gBAC3B,CAAC;YACH,CAAC,CAAC;YACF,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;IAED,WAAW,CAAC,OAAY;QACtB,IAAI,CAAC,WAAW,CAAC;YACf,OAAO;YACP,IAAI,EAAE,8BAAkB,CAAC,YAA0B;YACnD,QAAQ,EAAR,yBAAQ;SACT,CAAC,CAAC;IACL,CAAC;CACF;AAED,kBAAe,YAAY,CAAC"}
@@ -1,23 +1,12 @@
1
1
  import { isMainThread, parentPort, threadId } from "worker_threads";
2
- import { createId as cuid } from "@paralleldrive/cuid2";
3
2
  import { ThreadWorker } from "poolifier";
4
3
  import { THREAD_EVENT_TYPES } from "../../constants";
5
4
 
6
- import type { TaskFunctions, TaskFunction, WorkerOptions } from "poolifier";
7
5
  import type { EVENT_TYPE, EventRequestData } from "../../types";
8
6
 
9
7
  const activeListeners = new Map();
10
8
 
11
9
  class WorkerThread extends ThreadWorker {
12
- constructor(
13
- taskFunctions:
14
- | TaskFunction<unknown, Response>
15
- | TaskFunctions<unknown, Response>,
16
- opts?: WorkerOptions
17
- ) {
18
- super(taskFunctions, opts);
19
- }
20
-
21
10
  /**
22
11
  * Sends message back to the main thread.
23
12
  *
@@ -82,11 +71,18 @@ class WorkerThread extends ThreadWorker {
82
71
  } else if (message?.type === THREAD_EVENT_TYPES.ERROR) {
83
72
  reject(message?.payload);
84
73
  }
85
-
86
74
  };
87
75
  this.onMessage(messageListener);
88
76
  });
89
77
  }
78
+
79
+ reportStats(payload: any) {
80
+ this.postMessage({
81
+ payload,
82
+ type: THREAD_EVENT_TYPES.STATS_REPORT as EVENT_TYPE,
83
+ threadId,
84
+ });
85
+ }
90
86
  }
91
87
 
92
88
  export default WorkerThread;
package/dist/node.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import tmp from "tmp";
2
+ import EventEmitter from "events";
3
+ import { BaseLogger } from "pino";
2
4
  import { NodeModel, RunnerConfig, ModelConfig, ResponseObject, Compatibility, WasmOptions, ModelInstance } from "./types";
3
5
  import { CancellationToken } from "./CancellationToken";
4
6
  export { ColumnarSerializer } from "./serializer/columnarSerializer";
@@ -10,15 +12,16 @@ export { ModelExecuteCancelled } from "./error";
10
12
  * @class
11
13
  * @classdesc WasmRunner class, responsible for managing model and it's lifecycle
12
14
  */
13
- export declare class WasmRunner {
15
+ export declare class WasmRunner extends EventEmitter {
14
16
  _tempWorkerFolder: tmp.DirResult;
15
17
  _tempModelFolder: tmp.DirResult;
16
18
  license: string;
17
- models: any[];
19
+ models: Map<string, NodeModel>;
18
20
  externalResolverModule: any;
19
- initializingModels: string[];
21
+ initializingModels: Set<string>;
20
22
  options: WasmOptions;
21
23
  safeCompiler: string;
24
+ logger: BaseLogger;
22
25
  /**
23
26
  * Create new Wasm Runner instance
24
27
  *
@@ -53,8 +56,9 @@ export declare class WasmRunner {
53
56
  * @returns {Promise<void>}
54
57
  */
55
58
  remove(id: string): Promise<void>;
56
- _parseError(rawResponse: any): Object;
59
+ _parseError(rawResponse: any): object;
57
60
  _getModel(id: string): NodeModel;
61
+ getModel(id: string): NodeModel;
58
62
  getModelSize(id: string): number;
59
63
  getModelCompilerVersion(id: any): string;
60
64
  /**
@@ -65,10 +69,17 @@ export declare class WasmRunner {
65
69
  isExist(id: string): boolean;
66
70
  isModelCompatible(model: any, input: any): boolean;
67
71
  getModelsStats(): {
68
- stats: any;
69
- busy: any;
70
- size: any;
71
- id: any;
72
+ thread_stats: any;
73
+ memory_usage_mb: any;
74
+ uptime_ms: number;
75
+ min_time_ms: number;
76
+ mean_time_ms: number;
77
+ p95_time_ms: number;
78
+ p99_time_ms: number;
79
+ max_time_ms: number;
80
+ busy: number;
81
+ size: number;
82
+ id: string;
72
83
  }[];
73
84
  private getPartiallyCompatibleMessage;
74
85
  private getIncompatibleMessage;
@@ -82,4 +93,11 @@ export declare class WasmRunner {
82
93
  getNeuronCompilerVersion(logFile: any): Promise<any>;
83
94
  checkCompilerVersionCompatibility(modelCompilerVersion: string): Promise<Compatibility>;
84
95
  getSafeCompilerVersion(runnerVersion: string): any;
96
+ /**
97
+ * Dispose all resources: terminate all running models and clean up temp files.
98
+ *
99
+ * @async
100
+ * @returns {Promise<void>}
101
+ */
102
+ dispose(): Promise<void>;
85
103
  }