@hybrid-compute/worker 0.37.3 → 0.38.1
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 +24 -0
- package/README.md +30 -8
- package/dist/errors.d.ts +38 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +37 -0
- package/dist/errors.js.map +1 -0
- package/dist/expose.d.ts +39 -0
- package/dist/expose.d.ts.map +1 -0
- package/dist/expose.js +111 -0
- package/dist/expose.js.map +1 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +276 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +93 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +26 -0
- package/dist/utils.js.map +1 -0
- package/package.json +5 -1
- package/.release-it.json +0 -83
- package/eslint.config.mjs +0 -3
- package/src/__tests__/worker.spec.ts +0 -139
- package/src/index.ts +0 -118
- package/src/types.ts +0 -53
- package/tsconfig.build.json +0 -13
- package/tsconfig.json +0 -13
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import test, { describe } from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import {
|
|
4
|
-
createThreadedCompute,
|
|
5
|
-
ThreadedCompute,
|
|
6
|
-
WorkerResultMessageInterface,
|
|
7
|
-
WorkerTaskMessageInterface
|
|
8
|
-
} from '..';
|
|
9
|
-
|
|
10
|
-
describe('ThreadedCompute', () => {
|
|
11
|
-
test('canRun returns true for registered task', () => {
|
|
12
|
-
const OriginalWorker = globalThis.Worker;
|
|
13
|
-
|
|
14
|
-
class MockWorker {
|
|
15
|
-
public onmessage: ((event: any) => void) | null = null;
|
|
16
|
-
constructor(
|
|
17
|
-
public path: string,
|
|
18
|
-
public options: object
|
|
19
|
-
) {}
|
|
20
|
-
postMessage() {}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// @ts-expect-error allow override
|
|
24
|
-
globalThis.Worker = MockWorker;
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
const threaded = new ThreadedCompute('mock-worker.js', ['foo', 'bar']);
|
|
28
|
-
assert.equal(threaded.canRun('foo'), true);
|
|
29
|
-
assert.equal(threaded.canRun('baz'), false);
|
|
30
|
-
} finally {
|
|
31
|
-
globalThis.Worker = OriginalWorker;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('runTask resolves with worker result', async () => {
|
|
36
|
-
const OriginalWorker = globalThis.Worker;
|
|
37
|
-
|
|
38
|
-
class MockWorker {
|
|
39
|
-
public onmessage:
|
|
40
|
-
| ((event: MessageEvent<WorkerResultMessageInterface>) => void)
|
|
41
|
-
| null = null;
|
|
42
|
-
public received: WorkerTaskMessageInterface | null = null;
|
|
43
|
-
|
|
44
|
-
constructor(
|
|
45
|
-
public path: string,
|
|
46
|
-
public options: object
|
|
47
|
-
) {}
|
|
48
|
-
|
|
49
|
-
postMessage(msg: WorkerTaskMessageInterface) {
|
|
50
|
-
this.received = msg;
|
|
51
|
-
|
|
52
|
-
setTimeout(() => {
|
|
53
|
-
this.onmessage?.({
|
|
54
|
-
data: {
|
|
55
|
-
id: msg.id,
|
|
56
|
-
result: `result-${msg.input}`
|
|
57
|
-
}
|
|
58
|
-
} as MessageEvent<WorkerResultMessageInterface>);
|
|
59
|
-
}, 10);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// @ts-expect-error override
|
|
64
|
-
globalThis.Worker = MockWorker;
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
const compute = new ThreadedCompute('mock-worker.js', ['echo']);
|
|
68
|
-
const result = await compute.runTask<string, string>('echo', 'ping');
|
|
69
|
-
assert.equal(result, 'result-ping');
|
|
70
|
-
} finally {
|
|
71
|
-
globalThis.Worker = OriginalWorker;
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('runTask rejects with worker error', async () => {
|
|
76
|
-
const OriginalWorker = globalThis.Worker;
|
|
77
|
-
|
|
78
|
-
class MockWorker {
|
|
79
|
-
public onmessage:
|
|
80
|
-
| ((event: MessageEvent<WorkerResultMessageInterface>) => void)
|
|
81
|
-
| null = null;
|
|
82
|
-
|
|
83
|
-
constructor(
|
|
84
|
-
public path: string,
|
|
85
|
-
public options: object
|
|
86
|
-
) {}
|
|
87
|
-
|
|
88
|
-
postMessage(msg: WorkerTaskMessageInterface) {
|
|
89
|
-
setTimeout(() => {
|
|
90
|
-
this.onmessage?.({
|
|
91
|
-
data: {
|
|
92
|
-
id: msg.id,
|
|
93
|
-
error: 'Something went wrong'
|
|
94
|
-
}
|
|
95
|
-
} as MessageEvent<WorkerResultMessageInterface>);
|
|
96
|
-
}, 10);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// @ts-expect-error override
|
|
101
|
-
globalThis.Worker = MockWorker;
|
|
102
|
-
|
|
103
|
-
try {
|
|
104
|
-
const compute = new ThreadedCompute('mock-worker.js', ['fail']);
|
|
105
|
-
await assert.rejects(
|
|
106
|
-
() => compute.runTask('fail', 42),
|
|
107
|
-
/Something went wrong/
|
|
108
|
-
);
|
|
109
|
-
} finally {
|
|
110
|
-
globalThis.Worker = OriginalWorker;
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
describe('createThreadedCompute', () => {
|
|
115
|
-
test('returns instance of ThreadedCompute', () => {
|
|
116
|
-
const OriginalWorker = globalThis.Worker;
|
|
117
|
-
|
|
118
|
-
class MockWorker {
|
|
119
|
-
public onmessage:
|
|
120
|
-
| ((event: MessageEvent<WorkerResultMessageInterface>) => void)
|
|
121
|
-
| null = null;
|
|
122
|
-
constructor(
|
|
123
|
-
public path: string,
|
|
124
|
-
public options: object
|
|
125
|
-
) {}
|
|
126
|
-
postMessage() {}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// @ts-expect-error override
|
|
130
|
-
globalThis.Worker = MockWorker;
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
const instance = createThreadedCompute('mock-worker.js', ['foo']);
|
|
134
|
-
assert.ok(instance instanceof ThreadedCompute);
|
|
135
|
-
} finally {
|
|
136
|
-
globalThis.Worker = OriginalWorker;
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
});
|
package/src/index.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { ComputeBackendInterface } from '@hybrid-compute/core';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
WorkerResultMessageInterface,
|
|
5
|
-
WorkerTaskMessageInterface
|
|
6
|
-
} from './types.js';
|
|
7
|
-
|
|
8
|
-
export * from './types.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* `ThreadedCompute` is a compute backend that runs tasks in a dedicated Web Worker.
|
|
12
|
-
*
|
|
13
|
-
* It manages communication with the worker via `postMessage`, assigns unique task
|
|
14
|
-
* request IDs, and resolves results asynchronously. Tasks are registered by name
|
|
15
|
-
* during construction and mapped internally for availability checks.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```ts
|
|
19
|
-
* const threaded = new ThreadedCompute(new URL('./worker.js', import.meta.url), ['add']);
|
|
20
|
-
*
|
|
21
|
-
* const result = await threaded.runTask<number, number>('add', 5);
|
|
22
|
-
* console.log(result); // Output from worker
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Worker
|
|
26
|
-
*/
|
|
27
|
-
export class ThreadedCompute implements ComputeBackendInterface {
|
|
28
|
-
private worker: Worker;
|
|
29
|
-
private taskMap = new Set<string>();
|
|
30
|
-
|
|
31
|
-
// Map of task response IDs to success callbacks
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
-
private callbacks = new Map<number, (value: any) => void>();
|
|
34
|
-
|
|
35
|
-
// Map of task response IDs to error callbacks
|
|
36
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
-
private errors = new Map<number, (err: any) => void>();
|
|
38
|
-
|
|
39
|
-
private nextId = 1;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Constructs a new `ThreadedCompute` backend with a specified worker and list of available task names.
|
|
43
|
-
*
|
|
44
|
-
* @param workerPath - Path to the worker script (typically a URL via `import.meta.url`).
|
|
45
|
-
* @param taskNames - A list of task identifiers this worker is capable of executing.
|
|
46
|
-
*/
|
|
47
|
-
constructor(workerPath: string, taskNames: string[]) {
|
|
48
|
-
this.worker = new Worker(workerPath, { type: 'module' });
|
|
49
|
-
this.taskMap = new Set(taskNames);
|
|
50
|
-
|
|
51
|
-
this.worker.onmessage = (e: MessageEvent<WorkerResultMessageInterface>) => {
|
|
52
|
-
const { id, result, error } = e.data;
|
|
53
|
-
|
|
54
|
-
if (error) this.errors.get(id)?.(error);
|
|
55
|
-
else this.callbacks.get(id)?.(result);
|
|
56
|
-
|
|
57
|
-
this.callbacks.delete(id);
|
|
58
|
-
this.errors.delete(id);
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Checks whether the current worker is registered to handle a given task.
|
|
64
|
-
*
|
|
65
|
-
* @param taskName - The name of the task to check.
|
|
66
|
-
* @returns `true` if the task is supported, otherwise `false`.
|
|
67
|
-
*/
|
|
68
|
-
canRun(taskName: string): boolean {
|
|
69
|
-
return this.taskMap.has(taskName);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Runs a task inside the Web Worker.
|
|
74
|
-
*
|
|
75
|
-
* The task is assigned a unique ID and sent via `postMessage`. The result or error
|
|
76
|
-
* is resolved asynchronously when the worker responds.
|
|
77
|
-
*
|
|
78
|
-
* @typeParam Input - The input type for the task.
|
|
79
|
-
* @typeParam Output - The output type returned by the task.
|
|
80
|
-
*
|
|
81
|
-
* @param taskName - The name of the task to execute.
|
|
82
|
-
* @param input - The input payload for the task.
|
|
83
|
-
* @returns A Promise resolving to the task output.
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```ts
|
|
87
|
-
* const result = await threaded.runTask('square', 9); // 81
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
runTask<Input, Output>(taskName: string, input: Input): Promise<Output> {
|
|
91
|
-
return new Promise<Output>((resolve, reject) => {
|
|
92
|
-
const id = this.nextId++;
|
|
93
|
-
|
|
94
|
-
this.callbacks.set(id, resolve);
|
|
95
|
-
this.errors.set(id, reject);
|
|
96
|
-
|
|
97
|
-
const message: WorkerTaskMessageInterface = { task: taskName, input, id };
|
|
98
|
-
|
|
99
|
-
this.worker.postMessage(message);
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Factory function to create a new `ThreadedCompute` backend.
|
|
106
|
-
*
|
|
107
|
-
* @param workerPath - The module worker file path (e.g. `new URL('./worker.js', import.meta.url)`).
|
|
108
|
-
* @param tasks - A list of supported task names this worker can execute.
|
|
109
|
-
* @returns A new `ThreadedCompute` instance.
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```ts
|
|
113
|
-
* const backend = createThreadedCompute(new URL('./worker.js', import.meta.url), ['resizeImage']);
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
export function createThreadedCompute(workerPath: string, tasks: string[]) {
|
|
117
|
-
return new ThreadedCompute(workerPath, tasks);
|
|
118
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The message format sent from the main thread to a Web Worker when executing a task.
|
|
3
|
-
*
|
|
4
|
-
* This message includes a task name, its input payload, and a unique ID used to match
|
|
5
|
-
* the response from the worker.
|
|
6
|
-
*
|
|
7
|
-
* @property task - The name of the task to be executed in the worker.
|
|
8
|
-
* @property input - The input data for the task. This can be any JSON-serializable value.
|
|
9
|
-
* @property id - A unique identifier for correlating the response message.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* const message: WorkerTaskMessageInterface = {
|
|
14
|
-
* task: 'double',
|
|
15
|
-
* input: 21,
|
|
16
|
-
* id: 1
|
|
17
|
-
* };
|
|
18
|
-
* worker.postMessage(message);
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export interface WorkerTaskMessageInterface {
|
|
22
|
-
task: string;
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
-
input: any;
|
|
25
|
-
id: number;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The message format sent from a Web Worker back to the main thread upon task completion.
|
|
30
|
-
*
|
|
31
|
-
* This message includes the result of the task execution or an error message if the task failed.
|
|
32
|
-
*
|
|
33
|
-
* @property id - The unique identifier that matches a previously sent task message.
|
|
34
|
-
* @property result - The result of the task execution (if successful).
|
|
35
|
-
* @property error - An optional error message if the task failed.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* const response: WorkerResultMessageInterface = {
|
|
40
|
-
* id: 1,
|
|
41
|
-
* result: 42
|
|
42
|
-
* };
|
|
43
|
-
* postMessage(response);
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
|
|
47
|
-
*/
|
|
48
|
-
export interface WorkerResultMessageInterface {
|
|
49
|
-
id: number;
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
-
result: any;
|
|
52
|
-
error?: string;
|
|
53
|
-
}
|
package/tsconfig.build.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "dist",
|
|
5
|
-
"rootDir": "src",
|
|
6
|
-
"composite": true,
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationMap": true
|
|
9
|
-
},
|
|
10
|
-
"include": ["src"],
|
|
11
|
-
"exclude": ["src/**/__tests__/**"],
|
|
12
|
-
"references": [{ "path": "../core/tsconfig.build.json" }]
|
|
13
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.build.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"composite": false,
|
|
5
|
-
"noEmit": true,
|
|
6
|
-
"declaration": false,
|
|
7
|
-
"declarationMap": false,
|
|
8
|
-
"types": ["node"]
|
|
9
|
-
},
|
|
10
|
-
"include": ["src"],
|
|
11
|
-
"exclude": [],
|
|
12
|
-
"references": [{ "path": "../core/tsconfig.build.json" }]
|
|
13
|
-
}
|