zerohelper-worker 11.0.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.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # zerohelper-worker
2
+
3
+ Worker thread utilities for ZeroHelper.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install zerohelper-worker
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { runAsyncTask } from 'zerohelper-worker';
15
+
16
+ // Run CPU-intensive task in worker thread
17
+ const result = await runAsyncTask(
18
+ (data) => {
19
+ // This code runs in a worker
20
+ return data.map(x => x * 2);
21
+ },
22
+ [1, 2, 3, 4, 5] // Data passed to worker
23
+ );
24
+ // Result: [2, 4, 6, 8, 10]
25
+
26
+ // Handle errors
27
+ try {
28
+ await runAsyncTask(() => {
29
+ throw new Error('Worker error');
30
+ }, []);
31
+ } catch (error) {
32
+ console.error(error.message); // 'Worker error'
33
+ }
34
+ ```
35
+
36
+ ## API
37
+
38
+ ### runAsyncTask<T, R>(workerFn: (data: T) => R, data: T): Promise<R>
39
+
40
+ Execute a function in a worker thread.
41
+
42
+ **Parameters:**
43
+ - `workerFn` - Function to run in worker (must be self-contained)
44
+ - `data` - Data to pass to worker
45
+
46
+ **Returns:** Promise with worker result
47
+
48
+ **Features:**
49
+ - Automatic worker spawning and termination
50
+ - Error propagation
51
+ - Supports complex data types
52
+ - Timeout handling
53
+
54
+ ## License
55
+
56
+ ISC
@@ -0,0 +1,5 @@
1
+ export declare function runAsyncTask(code: string, data: any): Promise<any>;
2
+ export declare const worker_module: {
3
+ runAsyncTask: typeof runAsyncTask;
4
+ };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CA0BlE;AAED,eAAO,MAAM,aAAa;;CAEzB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.worker_module = void 0;
4
+ exports.runAsyncTask = runAsyncTask;
5
+ const worker_threads_1 = require("worker_threads");
6
+ function runAsyncTask(code, data) {
7
+ return new Promise((resolve, reject) => {
8
+ const workerCode = `
9
+ const parentPort = require('worker_threads').parentPort;
10
+ parentPort.on('message', async (data) => {
11
+ try {
12
+ const fn = ${code};
13
+ const result = await fn(data);
14
+ parentPort.postMessage({ success: true, result });
15
+ } catch (error) {
16
+ parentPort.postMessage({ success: false, error: error.message });
17
+ }
18
+ });
19
+ `;
20
+ const worker = new worker_threads_1.Worker(workerCode, { eval: true });
21
+ worker.on('message', (msg) => {
22
+ if (msg.success)
23
+ resolve(msg.result);
24
+ else
25
+ reject(new Error(msg.error));
26
+ worker.terminate();
27
+ });
28
+ worker.on('error', (err) => {
29
+ reject(err);
30
+ worker.terminate();
31
+ });
32
+ worker.postMessage(data);
33
+ });
34
+ }
35
+ exports.worker_module = {
36
+ runAsyncTask,
37
+ };
38
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,oCA0BC;AA5BD,mDAAgE;AAEhE,SAAgB,YAAY,CAAC,IAAY,EAAE,IAAS;IAClD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,UAAU,GAAG;;;;uBAIA,IAAI;;;;;;;KAOtB,CAAC;QACF,MAAM,MAAM,GAAG,IAAI,uBAAM,CAAC,UAAU,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACtD,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,EAAE;YAC3B,IAAI,GAAG,CAAC,OAAO;gBAAE,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;;gBAChC,MAAM,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;YAClC,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;YACzB,MAAM,CAAC,GAAG,CAAC,CAAC;YACZ,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;AACL,CAAC;AAEY,QAAA,aAAa,GAAG;IAC3B,YAAY;CACb,CAAC"}
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "zerohelper-worker",
3
+ "version": "11.0.0",
4
+ "description": "Worker utilities for ZeroHelper",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": { "build": "tsc", "clean": "rm -rf dist" },
8
+ "keywords": ["zerohelper", "worker", "thread"],
9
+ "author": "Onure9e",
10
+ "license": "ISC",
11
+ "dependencies": {},
12
+ "devDependencies": { "@types/node": "^22.10.2", "typescript": "^5.7.2" },
13
+ "files": ["dist", "README.md"],
14
+ "publishConfig": { "access": "public" }
15
+ }