@anonaddy/omni-worker 0.0.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/README.md +87 -0
- package/dist/cjs/builder/helpers.js +22 -0
- package/dist/cjs/builder/helpers.js.map +1 -0
- package/dist/cjs/builder/node.js +80 -0
- package/dist/cjs/builder/node.js.map +1 -0
- package/dist/cjs/builder/plugins/external-imports.js +44 -0
- package/dist/cjs/builder/plugins/external-imports.js.map +1 -0
- package/dist/cjs/builder/plugins/native-module.js +18 -0
- package/dist/cjs/builder/plugins/native-module.js.map +1 -0
- package/dist/cjs/index.js +84 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types/omni-worker.js +3 -0
- package/dist/cjs/types/omni-worker.js.map +1 -0
- package/dist/mjs/builder/helpers.js +16 -0
- package/dist/mjs/builder/helpers.js.map +1 -0
- package/dist/mjs/builder/node.js +40 -0
- package/dist/mjs/builder/node.js.map +1 -0
- package/dist/mjs/builder/plugins/external-imports.js +39 -0
- package/dist/mjs/builder/plugins/external-imports.js.map +1 -0
- package/dist/mjs/builder/plugins/native-module.js +15 -0
- package/dist/mjs/builder/plugins/native-module.js.map +1 -0
- package/dist/mjs/index.js +44 -0
- package/dist/mjs/index.js.map +1 -0
- package/dist/mjs/types/omni-worker.js +2 -0
- package/dist/mjs/types/omni-worker.js.map +1 -0
- package/dist/types/builder/helpers.d.ts +2 -0
- package/dist/types/builder/helpers.d.ts.map +1 -0
- package/dist/types/builder/node.d.ts +3 -0
- package/dist/types/builder/node.d.ts.map +1 -0
- package/dist/types/builder/plugins/external-imports.d.ts +4 -0
- package/dist/types/builder/plugins/external-imports.d.ts.map +1 -0
- package/dist/types/builder/plugins/native-module.d.ts +3 -0
- package/dist/types/builder/plugins/native-module.d.ts.map +1 -0
- package/dist/types/index.d.ts +26 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types/omni-worker.d.ts +21 -0
- package/dist/types/types/omni-worker.d.ts.map +1 -0
- package/package.json +51 -0
package/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# 👷 Omni Worker - A Versatile Worker for Typescript
|
2
|
+
Webworkers (browser) and thread workers (NodeJS) are usually limiting in their application, since the code that runs on a worker needs to be serializable. Therefore interfaces aren't fully supported, and also 3rd party libraries usually don't work.
|
3
|
+
|
4
|
+
Enter the stage, OmniWorkers! These workers allow you to simply declare any interface with functions that will actually run inside a worker. Whether it be on the web (webworker) or as a thread worker in NodeJS, these workers will run your code, no matter what.
|
5
|
+
|
6
|
+
## How does it Work?
|
7
|
+
In the case of jsDOM, the UI will block at certain tasks if it's too heavy. Alternatively, in case of Node, load times will take longer since the JavaScript V8 engine only runs one event loop thread. Even when using asynchronous code, the event loop can easily get overloaded.
|
8
|
+
|
9
|
+
### Workers
|
10
|
+
The [Webworker API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) for the web and [Worker Threads](https://nodejs.org/api/worker_threads.html) for NodeJS allow us to declare code inside a seperate file, which will run in its own context. The advantage, is that we now have more than one event loop thread to work on. The drawback of course, is that all code executed inside these (contexed) workers needs to be serializable, since your code is basically shipped into the worker for execution. If you have code that depends on other 3rd party libraries, your code will most-likely not build, since the code inside the worker context is closed off.
|
11
|
+
|
12
|
+
### 👷 OmniWorkers
|
13
|
+
So how do OmniWorkers stand out from normal workers? First off, the simplicity. Simply declare your code and expose it to the main event loop. Then, build the OmniWorker by referencing you file. Now, the OmniWorker will expose the object with functions you've injected into the worker container. You can use your own functions with primitive types, functions directly taken from 3rd party modules or a hybrid mix of both. Let's look at an example:
|
14
|
+
|
15
|
+
#### `👷♀️ workers/worker.ts`
|
16
|
+
```javascript
|
17
|
+
import { NodeOmniWorker } from "@anonaddy/omni-worker";
|
18
|
+
import { IMyOmniWorkerFunctions } from "./worker-model";
|
19
|
+
import { capitalize } from "lodash";
|
20
|
+
|
21
|
+
// Declare your functions using an interface
|
22
|
+
const fnObj: IMyOmniWorkerFunctions = {
|
23
|
+
add: (a: number, b: number) => a + b,
|
24
|
+
capitalize
|
25
|
+
}
|
26
|
+
|
27
|
+
// In this case, diamond interface can even be omitted, since inferred
|
28
|
+
NodeOmniWorker.expose<IMyOmniWorkerFunctions>(fnObj);
|
29
|
+
```
|
30
|
+
The worker.ts file contains the logic you would like to run inside the container. In our example above we created our own function, but we also included a 3rd party module (lodash) to demonstrate that an OmniWorker can just as well use any other 3rd party module.
|
31
|
+
|
32
|
+
What's important, is to expose your logic using the expose function. This will make sure that the container exposes (or shares) your code logic with the main event loop thread, for you to access it in your code.
|
33
|
+
|
34
|
+
#### `🏭 index.ts`
|
35
|
+
```javascript
|
36
|
+
import { NodeOmniWorker } from "@anonaddy/omni-worker";
|
37
|
+
import { IMyOmniWorkerFunctions } from "./worker-model";
|
38
|
+
|
39
|
+
// Use the static path from your project's root
|
40
|
+
const WORKER_DIR = "src/workers";
|
41
|
+
|
42
|
+
// Build the worker from the file you specified and the
|
43
|
+
// functions that were exposed
|
44
|
+
const worker = await NodeOmniWorker
|
45
|
+
.build<IMyOmniWorkerFunctions>(`${WORKER_DIR}/worker`);
|
46
|
+
|
47
|
+
// Placing anonymous async function on the event loop
|
48
|
+
setTimeout(async () => {
|
49
|
+
const sum = await worker.use().add(1, 2);
|
50
|
+
const str = await worker.use().capitalize("hello");
|
51
|
+
|
52
|
+
console.log(`
|
53
|
+
${str}, my friend!
|
54
|
+
The result of my sum is: ${sum}!
|
55
|
+
Aren't Omniworkers just awesome?!
|
56
|
+
`);
|
57
|
+
}, 0);
|
58
|
+
```
|
59
|
+
Your code can now just asynchronously call the logic you have declared and exposed inside your worker. Simply call the `worker.use()` function and you'll have access to all the functions defined by you inside your `IMyOwnWorkerFunctions` interface.
|
60
|
+
|
61
|
+
### Under the Hood
|
62
|
+
This modules leverages [Comlink](https://github.com/GoogleChromeLabs/comlink) and [Esbuild](https://github.com/evanw/esbuild) to parse your container code, build it and wire it up to the main event loop using Comlink.
|
63
|
+
|
64
|
+
### Tests
|
65
|
+
Tests are included inside this library. If you would like to learn how to build and instantiate your workers, you can find examples inside the test code, for node inside `__tests_node__` and for web `__tests_dom__` respectively.
|
66
|
+
|
67
|
+
# Potential Issues
|
68
|
+
There's cases where building the worker could lead to errors. Especially when working with packages that use bindings for other languages such as C or C++ for example and that are built to `*.node` binary files using node-gyp. One could experience situations like:
|
69
|
+
```
|
70
|
+
❌ Module did not self-register: /path/to/node-gyp/built/binary.node
|
71
|
+
```
|
72
|
+
The root cause is assumed to be a mismatch on either the build or the initialization stage of the binary. Oftentimes, performing these steps, helps:
|
73
|
+
``` bash
|
74
|
+
# first, retry the rebuild
|
75
|
+
npm rebuild <your lib>
|
76
|
+
|
77
|
+
# if that didn't work
|
78
|
+
rm -rf node_modules
|
79
|
+
mv package-lock.json package-lock.json.backup # make sure to make a backup!
|
80
|
+
npm i
|
81
|
+
```
|
82
|
+
Should you run into the issue above, or similar other issues, please drop me an email on the address mentioned at the bottom of this doc.
|
83
|
+
|
84
|
+
# Project Status
|
85
|
+
Currently, only `NodeOmniWorker`s are available. The `WebOmniWorker`s will also be available soon. Please stay tuned! In case you have any questions, make sure to drop me an [email](mailto:7ebr7fa0@anonaddy.com) and I will try to get back to you asap.
|
86
|
+
|
87
|
+
🏗️ **Happy (Omni)Working!!** 🏗️
|
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.getCallerDir = getCallerDir;
|
7
|
+
const path_1 = __importDefault(require("path"));
|
8
|
+
function getCallerDir() {
|
9
|
+
try {
|
10
|
+
const stack = new Error().stack;
|
11
|
+
const line = stack.split('\n')[1].trim();
|
12
|
+
const segment = line.split(' ')[1];
|
13
|
+
const part = segment.split(':')[0];
|
14
|
+
const dirName = path_1.default.dirname(part);
|
15
|
+
return dirName;
|
16
|
+
}
|
17
|
+
catch (e) {
|
18
|
+
console.error('building caller path failed', e);
|
19
|
+
return './';
|
20
|
+
}
|
21
|
+
}
|
22
|
+
//# sourceMappingURL=helpers.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/builder/helpers.ts"],"names":[],"mappings":";;;;;AAEA,oCAaC;AAfD,gDAAwB;AAExB,SAAgB,YAAY;IAC1B,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC,KAAM,CAAC;QACjC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACzC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnC,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnC,MAAM,OAAO,GAAG,cAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAEnC,OAAO,OAAO,CAAC;IACjB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,CAAC,CAAC,CAAC;QAChD,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
@@ -0,0 +1,80 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
exports.buildApiNode = void 0;
|
40
|
+
const Comlink = __importStar(require("comlink"));
|
41
|
+
const esbuild = __importStar(require("esbuild"));
|
42
|
+
const external_imports_1 = __importDefault(require("./plugins/external-imports"));
|
43
|
+
const worker_threads_1 = require("worker_threads");
|
44
|
+
const node_adapter_js_1 = __importDefault(require("comlink/dist/umd/node-adapter.js"));
|
45
|
+
const _path = __importStar(require("path"));
|
46
|
+
const native_module_1 = require("../builder/plugins/native-module");
|
47
|
+
const helpers_1 = require("./helpers");
|
48
|
+
const buildApiNode = async (path) => {
|
49
|
+
const callerDir = (0, helpers_1.getCallerDir)();
|
50
|
+
const resolvedPath = _path.resolve(callerDir, path);
|
51
|
+
const result = await esbuild.build({
|
52
|
+
entryPoints: [resolvedPath],
|
53
|
+
loader: {
|
54
|
+
".ts": "ts",
|
55
|
+
".js": "js",
|
56
|
+
".node": "binary"
|
57
|
+
},
|
58
|
+
format: "cjs",
|
59
|
+
bundle: true,
|
60
|
+
minify: true,
|
61
|
+
write: false,
|
62
|
+
plugins: [
|
63
|
+
native_module_1.nativeModulePlugin,
|
64
|
+
external_imports_1.default,
|
65
|
+
]
|
66
|
+
});
|
67
|
+
const outputFiles = result.outputFiles;
|
68
|
+
if (!outputFiles ||
|
69
|
+
result.outputFiles.length < 1 ||
|
70
|
+
result.outputFiles[0].text === undefined ||
|
71
|
+
result.outputFiles[0].text === "") {
|
72
|
+
throw Error("no build output for worker");
|
73
|
+
}
|
74
|
+
const scriptData = result.outputFiles[0].text.trim();
|
75
|
+
const worker = new worker_threads_1.Worker(scriptData, { eval: true });
|
76
|
+
const api = Comlink.wrap((0, node_adapter_js_1.default)(worker));
|
77
|
+
return api;
|
78
|
+
};
|
79
|
+
exports.buildApiNode = buildApiNode;
|
80
|
+
//# sourceMappingURL=node.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../../src/builder/node.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAmC;AACnC,iDAAmC;AACnC,kFAA+D;AAC/D,mDAAwC;AACxC,uFAA4D;AAC5D,4CAA8B;AAC9B,oEAAsE;AACtE,uCAAyC;AAElC,MAAM,YAAY,GAAG,KAAK,EAAK,IAAY,EAA8B,EAAE;IAEhF,MAAM,SAAS,GAAG,IAAA,sBAAY,GAAE,CAAC;IACjC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAEpD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC;QACjC,WAAW,EAAE,CAAC,YAAY,CAAC;QAC3B,MAAM,EAAE;YACN,KAAK,EAAE,IAAI;YACX,KAAK,EAAE,IAAI;YACX,OAAO,EAAE,QAAQ;SAClB;QACD,MAAM,EAAE,KAAK;QACb,MAAM,EAAE,IAAI;QACZ,MAAM,EAAE,IAAI;QACZ,KAAK,EAAE,KAAK;QACZ,OAAO,EAAE;YACP,kCAAkB;YAClB,0BAAqB;SACtB;KACF,CAAC,CAAC;IAEH,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IAEvC,IACE,CAAC,WAAW;QACZ,MAAM,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC;QAC7B,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,SAAS;QACxC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,EAAE,EACjC,CAAC;QACD,MAAM,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACrD,MAAM,MAAM,GAAG,IAAI,uBAAM,CAAC,UAAU,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IACtD,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAI,IAAA,yBAAY,EAAC,MAAM,CAAC,CAAC,CAAC;IAElD,OAAO,GAAG,CAAC;AACb,CAAC,CAAA;AAtCY,QAAA,YAAY,gBAsCxB"}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
7
|
+
const path_1 = __importDefault(require("path"));
|
8
|
+
const externalImportsPlugin = ({
|
9
|
+
name: 'external-imports-plugin',
|
10
|
+
setup(build) {
|
11
|
+
const externalModules = new Set();
|
12
|
+
build.onLoad({ filter: /\.ts$|\.js$/ }, async (args) => {
|
13
|
+
// Read the file contents
|
14
|
+
const code = fs_1.default.readFileSync(args.path, "utf8");
|
15
|
+
// Collect imported module names
|
16
|
+
const importPattern = /import\s+(?:[\s\w{},*]+from\s+)?["']([^"']+)["']/g;
|
17
|
+
;
|
18
|
+
let match;
|
19
|
+
while ((match = importPattern.exec(code)) !== null) {
|
20
|
+
let importPath = match[1];
|
21
|
+
// Convert relative paths to absolute paths
|
22
|
+
if (importPath.startsWith(".")) {
|
23
|
+
importPath = path_1.default.resolve(path_1.default.dirname(args.path), importPath);
|
24
|
+
// Strip project root to avoid absolute paths
|
25
|
+
importPath = path_1.default.relative(process.cwd(), importPath);
|
26
|
+
}
|
27
|
+
externalModules.add(importPath);
|
28
|
+
}
|
29
|
+
});
|
30
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
31
|
+
if (externalModules.has(args.path)) {
|
32
|
+
return { path: args.path, external: true }; // Mark it external
|
33
|
+
}
|
34
|
+
});
|
35
|
+
build.onEnd(() => {
|
36
|
+
build.initialOptions.external = [
|
37
|
+
...(build.initialOptions.external || []),
|
38
|
+
...Array.from(externalModules),
|
39
|
+
];
|
40
|
+
});
|
41
|
+
},
|
42
|
+
});
|
43
|
+
exports.default = externalImportsPlugin;
|
44
|
+
//# sourceMappingURL=external-imports.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"external-imports.js","sourceRoot":"","sources":["../../../../src/builder/plugins/external-imports.ts"],"names":[],"mappings":";;;;;AACA,4CAAoB;AACpB,gDAAwB;AAExB,MAAM,qBAAqB,GAAW,CAAC;IACrC,IAAI,EAAE,yBAAyB;IAC/B,KAAK,CAAC,KAAK;QACT,MAAM,eAAe,GAAgB,IAAI,GAAG,EAAE,CAAC;QAE/C,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,EAAE,KAAK,EAAE,IAAgB,EAAgB,EAAE;YAC/E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AC,eAAe,CAAC;aAC/B,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAC,CAAC;AAEH,kBAAe,qBAAqB,CAAC"}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.nativeModulePlugin = void 0;
|
4
|
+
exports.nativeModulePlugin = {
|
5
|
+
name: 'native-module',
|
6
|
+
setup(build) {
|
7
|
+
build.onResolve({ filter: /\.node$/ }, args => {
|
8
|
+
return { path: args.path, namespace: 'native', external: true };
|
9
|
+
});
|
10
|
+
build.onLoad({ filter: /\.node$/, namespace: 'native' }, async (args) => {
|
11
|
+
const fs = require('fs');
|
12
|
+
const path = require('path');
|
13
|
+
const binary = fs.readFileSync(path.resolve(args.pluginData.resolveDir, args.path));
|
14
|
+
return { contents: binary, loader: 'binary' };
|
15
|
+
});
|
16
|
+
},
|
17
|
+
};
|
18
|
+
//# sourceMappingURL=native-module.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"native-module.js","sourceRoot":"","sources":["../../../../src/builder/plugins/native-module.ts"],"names":[],"mappings":";;;AAEa,QAAA,kBAAkB,GAAW;IACxC,IAAI,EAAE,eAAe;IACrB,KAAK,CAAC,KAAK;QACT,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,CAAC,EAAE;YAC5C,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;QAClE,CAAC,CAAC,CAAC;QACH,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAC,IAAI,EAAC,EAAE;YACpE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;YACzB,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;YAC7B,MAAM,MAAM,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;YACpF,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;QAChD,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA"}
|
@@ -0,0 +1,84 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
exports.NodeOmniWorker = void 0;
|
40
|
+
const worker_threads_1 = require("worker_threads");
|
41
|
+
const Comlink = __importStar(require("comlink"));
|
42
|
+
const node_adapter_js_1 = __importDefault(require("comlink/dist/umd/node-adapter.js"));
|
43
|
+
const node_1 = require("./builder/node");
|
44
|
+
class NodeOmniWorker {
|
45
|
+
constructor(api) {
|
46
|
+
this.isInitialized = () => (this._api !== undefined);
|
47
|
+
this.use = () => {
|
48
|
+
const isInitialized = this.isInitialized();
|
49
|
+
if (isInitialized) {
|
50
|
+
return this._api;
|
51
|
+
}
|
52
|
+
else {
|
53
|
+
throw Error('worker is not yet initialized. make sure to call the .set() function, first');
|
54
|
+
}
|
55
|
+
};
|
56
|
+
this._api = api;
|
57
|
+
return this;
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Build the OmniWorker from a worker file. Please note that the functions
|
61
|
+
* on the worker file need to first be exposed using the expose({ <functions> })
|
62
|
+
* functions, in order to start this build step.
|
63
|
+
*
|
64
|
+
* @param path The relative path to where the worker module is located
|
65
|
+
* @returns A new Node OmniWorker
|
66
|
+
*/
|
67
|
+
static async build(path) {
|
68
|
+
const api = await (0, node_1.buildApiNode)(path);
|
69
|
+
return new NodeOmniWorker(api);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
exports.NodeOmniWorker = NodeOmniWorker;
|
73
|
+
/**
|
74
|
+
* Expose the functions inside the worker to the rest of the application.
|
75
|
+
* After having exposed the functions, the build step can be initiated.
|
76
|
+
* @param functions An object with functions inside the worker to be exposed to
|
77
|
+
* the rest of the application.
|
78
|
+
*/
|
79
|
+
NodeOmniWorker.expose = (functions) => {
|
80
|
+
if (worker_threads_1.parentPort) {
|
81
|
+
Comlink.expose(functions, (0, node_adapter_js_1.default)(worker_threads_1.parentPort));
|
82
|
+
}
|
83
|
+
};
|
84
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,mDAA4C;AAC5C,iDAAmC;AACnC,uFAA4D;AAC5D,yCAA8C;AAE9C,MAAa,cAAc;IAGzB,YAAoB,GAAsB;QAgCnC,kBAAa,GAAG,GAAY,EAAE,CAAC,CACpC,IAAI,CAAC,IAAI,KAAK,SAAS,CACxB,CAAC;QAEK,QAAG,GAAG,GAAG,EAAE;YAChB,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;YAC3C,IAAI,aAAa,EAAE,CAAC;gBAClB,OAAO,IAAI,CAAC,IAAK,CAAC;YACpB,CAAC;iBAAM,CAAC;gBACN,MAAM,KAAK,CAAC,6EAA6E,CAAC,CAAC;YAC7F,CAAC;QACH,CAAC,CAAA;QA1CC,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,OAAO,IAAI,CAAC;IACd,CAAC;IAcD;;;;;;;OAOG;IACI,MAAM,CAAC,KAAK,CAAC,KAAK,CACvB,IAAY;QAEZ,MAAM,GAAG,GAAG,MAAM,IAAA,mBAAY,EAAI,IAAI,CAAC,CAAC;QACxC,OAAO,IAAI,cAAc,CAAI,GAAG,CAAC,CAAC;IACpC,CAAC;;AAjCH,wCA+CC;AAvCC;;;;;GAKG;AACW,qBAAM,GAAG,CAAI,SAAY,EAAE,EAAE;IACzC,IAAI,2BAAU,EAAE,CAAC;QACf,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,IAAA,yBAAY,EAAC,2BAAU,CAAC,CAAC,CAAC;IACtD,CAAC;AACH,CAAC,AAJmB,CAInB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"omni-worker.js","sourceRoot":"","sources":["../../../src/types/omni-worker.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
export function getCallerDir() {
|
3
|
+
try {
|
4
|
+
const stack = new Error().stack;
|
5
|
+
const line = stack.split('\n')[1].trim();
|
6
|
+
const segment = line.split(' ')[1];
|
7
|
+
const part = segment.split(':')[0];
|
8
|
+
const dirName = path.dirname(part);
|
9
|
+
return dirName;
|
10
|
+
}
|
11
|
+
catch (e) {
|
12
|
+
console.error('building caller path failed', e);
|
13
|
+
return './';
|
14
|
+
}
|
15
|
+
}
|
16
|
+
//# sourceMappingURL=helpers.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/builder/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,UAAU,YAAY;IAC1B,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC,KAAM,CAAC;QACjC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACzC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnC,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAEnC,OAAO,OAAO,CAAC;IACjB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,CAAC,CAAC,CAAC;QAChD,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import * as Comlink from 'comlink';
|
2
|
+
import * as esbuild from 'esbuild';
|
3
|
+
import externalImportsPlugin from './plugins/external-imports';
|
4
|
+
import { Worker } from 'worker_threads';
|
5
|
+
import nodeEndpoint from 'comlink/dist/umd/node-adapter.js';
|
6
|
+
import * as _path from 'path';
|
7
|
+
import { nativeModulePlugin } from '../builder/plugins/native-module';
|
8
|
+
import { getCallerDir } from './helpers';
|
9
|
+
export const buildApiNode = async (path) => {
|
10
|
+
const callerDir = getCallerDir();
|
11
|
+
const resolvedPath = _path.resolve(callerDir, path);
|
12
|
+
const result = await esbuild.build({
|
13
|
+
entryPoints: [resolvedPath],
|
14
|
+
loader: {
|
15
|
+
".ts": "ts",
|
16
|
+
".js": "js",
|
17
|
+
".node": "binary"
|
18
|
+
},
|
19
|
+
format: "cjs",
|
20
|
+
bundle: true,
|
21
|
+
minify: true,
|
22
|
+
write: false,
|
23
|
+
plugins: [
|
24
|
+
nativeModulePlugin,
|
25
|
+
externalImportsPlugin,
|
26
|
+
]
|
27
|
+
});
|
28
|
+
const outputFiles = result.outputFiles;
|
29
|
+
if (!outputFiles ||
|
30
|
+
result.outputFiles.length < 1 ||
|
31
|
+
result.outputFiles[0].text === undefined ||
|
32
|
+
result.outputFiles[0].text === "") {
|
33
|
+
throw Error("no build output for worker");
|
34
|
+
}
|
35
|
+
const scriptData = result.outputFiles[0].text.trim();
|
36
|
+
const worker = new Worker(scriptData, { eval: true });
|
37
|
+
const api = Comlink.wrap(nodeEndpoint(worker));
|
38
|
+
return api;
|
39
|
+
};
|
40
|
+
//# sourceMappingURL=node.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../../src/builder/node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,qBAAqB,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,MAAM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I,EAAE,IAAI,EAAE,CAAC,CAAC;IACtD,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAI,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;IAElD,OAAO,GAAG,CAAC;AACb,CAAC,CAAA"}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
const externalImportsPlugin = ({
|
4
|
+
name: 'external-imports-plugin',
|
5
|
+
setup(build) {
|
6
|
+
const externalModules = new Set();
|
7
|
+
build.onLoad({ filter: /\.ts$|\.js$/ }, async (args) => {
|
8
|
+
// Read the file contents
|
9
|
+
const code = fs.readFileSync(args.path, "utf8");
|
10
|
+
// Collect imported module names
|
11
|
+
const importPattern = /import\s+(?:[\s\w{},*]+from\s+)?["']([^"']+)["']/g;
|
12
|
+
;
|
13
|
+
let match;
|
14
|
+
while ((match = importPattern.exec(code)) !== null) {
|
15
|
+
let importPath = match[1];
|
16
|
+
// Convert relative paths to absolute paths
|
17
|
+
if (importPath.startsWith(".")) {
|
18
|
+
importPath = path.resolve(path.dirname(args.path), importPath);
|
19
|
+
// Strip project root to avoid absolute paths
|
20
|
+
importPath = path.relative(process.cwd(), importPath);
|
21
|
+
}
|
22
|
+
externalModules.add(importPath);
|
23
|
+
}
|
24
|
+
});
|
25
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
26
|
+
if (externalModules.has(args.path)) {
|
27
|
+
return { path: args.path, external: true }; // Mark it external
|
28
|
+
}
|
29
|
+
});
|
30
|
+
build.onEnd(() => {
|
31
|
+
build.initialOptions.external = [
|
32
|
+
...(build.initialOptions.external || []),
|
33
|
+
...Array.from(externalModules),
|
34
|
+
];
|
35
|
+
});
|
36
|
+
},
|
37
|
+
});
|
38
|
+
export default externalImportsPlugin;
|
39
|
+
//# sourceMappingURL=external-imports.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"external-imports.js","sourceRoot":"","sources":["../../../../src/builder/plugins/external-imports.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C;gBACnC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,mBAAmB;YACjE,CAAC;QACD,CAAC,CAAC,CAAC;QAEH,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE;YACf,KAAK,CAAC,cAAc,CAAC,QAAQ,GAAG;gBAC9B,GAAG,CAAC,KAAK,CAAC,cAAc,CAAC,QAAQ,IAAI,EAAE,CAAC;gBACxC,GAAG,KAAK,CAAC,IAAI,CAAC,eAAe,CAAC;aAC/B,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAC,CAAC;AAEH,eAAe,qBAAqB,CAAC"}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export const nativeModulePlugin = {
|
2
|
+
name: 'native-module',
|
3
|
+
setup(build) {
|
4
|
+
build.onResolve({ filter: /\.node$/ }, args => {
|
5
|
+
return { path: args.path, namespace: 'native', external: true };
|
6
|
+
});
|
7
|
+
build.onLoad({ filter: /\.node$/, namespace: 'native' }, async (args) => {
|
8
|
+
const fs = require('fs');
|
9
|
+
const path = require('path');
|
10
|
+
const binary = fs.readFileSync(path.resolve(args.pluginData.resolveDir, args.path));
|
11
|
+
return { contents: binary, loader: 'binary' };
|
12
|
+
});
|
13
|
+
},
|
14
|
+
};
|
15
|
+
//# sourceMappingURL=native-module.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"native-module.js","sourceRoot":"","sources":["../../../../src/builder/plugins/native-module.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,kBAAkB,GAAW;IACxC,IAAI,EAAE,eAAe;IACrB,KAAK,CAAC,KAAK;QACT,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,CAAC,EAAE;YAC5C,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;QAClE,CAAC,CAAC,CAAC;QACH,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAC,IAAI,EAAC,EAAE;YACpE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;YACzB,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;YAC7B,MAAM,MAAM,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;YACpF,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;QAChD,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA"}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { parentPort } from 'worker_threads';
|
2
|
+
import * as Comlink from 'comlink';
|
3
|
+
import nodeEndpoint from 'comlink/dist/umd/node-adapter.js';
|
4
|
+
import { buildApiNode } from './builder/node';
|
5
|
+
export class NodeOmniWorker {
|
6
|
+
constructor(api) {
|
7
|
+
this.isInitialized = () => (this._api !== undefined);
|
8
|
+
this.use = () => {
|
9
|
+
const isInitialized = this.isInitialized();
|
10
|
+
if (isInitialized) {
|
11
|
+
return this._api;
|
12
|
+
}
|
13
|
+
else {
|
14
|
+
throw Error('worker is not yet initialized. make sure to call the .set() function, first');
|
15
|
+
}
|
16
|
+
};
|
17
|
+
this._api = api;
|
18
|
+
return this;
|
19
|
+
}
|
20
|
+
/**
|
21
|
+
* Build the OmniWorker from a worker file. Please note that the functions
|
22
|
+
* on the worker file need to first be exposed using the expose({ <functions> })
|
23
|
+
* functions, in order to start this build step.
|
24
|
+
*
|
25
|
+
* @param path The relative path to where the worker module is located
|
26
|
+
* @returns A new Node OmniWorker
|
27
|
+
*/
|
28
|
+
static async build(path) {
|
29
|
+
const api = await buildApiNode(path);
|
30
|
+
return new NodeOmniWorker(api);
|
31
|
+
}
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Expose the functions inside the worker to the rest of the application.
|
35
|
+
* After having exposed the functions, the build step can be initiated.
|
36
|
+
* @param functions An object with functions inside the worker to be exposed to
|
37
|
+
* the rest of the application.
|
38
|
+
*/
|
39
|
+
NodeOmniWorker.expose = (functions) => {
|
40
|
+
if (parentPort) {
|
41
|
+
Comlink.expose(functions, nodeEndpoint(parentPort));
|
42
|
+
}
|
43
|
+
};
|
44
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,YAAY,MAAM,kCAAkC,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,OAAO,cAAc;IAGzB,YAAoB,GAAsB;QAgCnC,kBAAa,GAAG,GAAY,EAAE,CAAC,CACpC,IAAI,CAAC,IAAI,KAAK,SAAS,CACxB,CAAC;QAEK,QAAG,GAAG,GAAG,EAAE;YAChB,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;YAC3C,IAAI,aAAa,EAAE,CAAC;gBAClB,OAAO,IAAI,CAAC,IAAK,CAAC;YACpB,CAAC;iBAAM,CAAC;gBACN,MAAM,KAAK,CAAC,6EAA6E,CAAC,CAAC;YAC7F,CAAC;QACH,CAAC,CAAA;QA1CC,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,OAAO,IAAI,CAAC;IACd,CAAC;IAcD;;;;;;;OAOG;IACI,MAAM,CAAC,KAAK,CAAC,KAAK,CACvB,IAAY;QAEZ,MAAM,GAAG,GAAG,MAAM,YAAY,CAAI,IAAI,CAAC,CAAC;QACxC,OAAO,IAAI,cAAc,CAAI,GAAG,CAAC,CAAC;IACpC,CAAC;;AAzBD;;;;;GAKG;AACW,qBAAM,GAAG,CAAI,SAAY,EAAE,EAAE;IACzC,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC;IACtD,CAAC;AACH,CAAC,AAJmB,CAInB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"omni-worker.js","sourceRoot":"","sources":["../../../src/types/omni-worker.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/builder/helpers.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,WAa3B"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../src/builder/node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AASnC,eAAO,MAAM,YAAY,GAAU,CAAC,QAAQ,MAAM,KAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAsC7E,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"external-imports.d.ts","sourceRoot":"","sources":["../../../../src/builder/plugins/external-imports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,MAAM,EAAE,MAAM,SAAS,CAAC;AAI7C,QAAA,MAAM,qBAAqB,EAAE,MAwC3B,CAAC;AAEH,eAAe,qBAAqB,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"native-module.d.ts","sourceRoot":"","sources":["../../../../src/builder/plugins/native-module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,eAAO,MAAM,kBAAkB,EAAE,MAahC,CAAA"}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { IOmniWorker } from './types/omni-worker';
|
2
|
+
export { IOmniWorker } from './types/omni-worker';
|
3
|
+
import * as Comlink from 'comlink';
|
4
|
+
export declare class NodeOmniWorker<T> implements IOmniWorker<T> {
|
5
|
+
private _api;
|
6
|
+
private constructor();
|
7
|
+
/**
|
8
|
+
* Expose the functions inside the worker to the rest of the application.
|
9
|
+
* After having exposed the functions, the build step can be initiated.
|
10
|
+
* @param functions An object with functions inside the worker to be exposed to
|
11
|
+
* the rest of the application.
|
12
|
+
*/
|
13
|
+
static expose: <T_1>(functions: T_1) => void;
|
14
|
+
/**
|
15
|
+
* Build the OmniWorker from a worker file. Please note that the functions
|
16
|
+
* on the worker file need to first be exposed using the expose({ <functions> })
|
17
|
+
* functions, in order to start this build step.
|
18
|
+
*
|
19
|
+
* @param path The relative path to where the worker module is located
|
20
|
+
* @returns A new Node OmniWorker
|
21
|
+
*/
|
22
|
+
static build<T extends object>(path: string): Promise<NodeOmniWorker<T>>;
|
23
|
+
isInitialized: () => boolean;
|
24
|
+
use: () => Comlink.Remote<T>;
|
25
|
+
}
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGlD,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAInC,qBAAa,cAAc,CAAC,CAAC,CAAE,YAAW,WAAW,CAAC,CAAC,CAAC;IACtD,OAAO,CAAC,IAAI,CAAoB;IAEhC,OAAO;IAKP;;;;;OAKG;IACH,OAAc,MAAM,mBAAkB,GAAC,UAItC;IAED;;;;;;;OAOG;WACiB,KAAK,CAAC,CAAC,SAAS,MAAM,EACxC,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAKtB,aAAa,QAAO,OAAO,CAEhC;IAEK,GAAG,0BAOT;CACF"}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import * as Comlink from 'comlink';
|
2
|
+
/**
|
3
|
+
* The base Interface for all workers
|
4
|
+
*/
|
5
|
+
export interface IOmniWorker<T> {
|
6
|
+
/**
|
7
|
+
* Use this function to see if the worker was successfully initialized
|
8
|
+
*
|
9
|
+
* @returns A boolean on whether or not the OmniWorker was successfully initialized
|
10
|
+
*/
|
11
|
+
isInitialized: () => boolean;
|
12
|
+
/**
|
13
|
+
* Will let you use the worker functions that were exposed from inside the worker.
|
14
|
+
* Please not that since using the worker, all your functions will now return
|
15
|
+
* asynchronously (Promise<>).
|
16
|
+
*
|
17
|
+
* @returns An object with the worker functions that were exposed from the worker
|
18
|
+
*/
|
19
|
+
use: () => Comlink.Remote<T>;
|
20
|
+
}
|
21
|
+
//# sourceMappingURL=omni-worker.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"omni-worker.d.ts","sourceRoot":"","sources":["../../../src/types/omni-worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC;;EAEE;AACF,MAAM,WAAW,WAAW,CAAC,CAAC;IAE5B;;;;OAIG;IACH,aAAa,EAAE,MAAM,OAAO,CAAA;IAE5B;;;;;;OAMG;IACH,GAAG,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;CAC7B"}
|
package/package.json
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
{
|
2
|
+
"name": "@anonaddy/omni-worker",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"description": "Run your code including all imports inside workers on either the web or on NodeJS!",
|
5
|
+
"main": "dist/cjs/index.js",
|
6
|
+
"module": "dist/mjs/index.js",
|
7
|
+
"exports": {
|
8
|
+
".": {
|
9
|
+
"require": "./dist/cjs/index.js",
|
10
|
+
"import": "./dist/mjs/index.js"
|
11
|
+
},
|
12
|
+
"./package.json": "./package.json"
|
13
|
+
},
|
14
|
+
"types": "dist/types/index.d.ts",
|
15
|
+
"scripts": {
|
16
|
+
"build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.mjs.json",
|
17
|
+
"test:node": "jest --config jest.node.config.ts --detectOpenHandles",
|
18
|
+
"test:dom": "jest --config jest.dom.config.ts",
|
19
|
+
"prepare": "npm run build"
|
20
|
+
},
|
21
|
+
"files": [
|
22
|
+
"dist"
|
23
|
+
],
|
24
|
+
"repository": {
|
25
|
+
"type": "git",
|
26
|
+
"url": "https://github.com/avluent/omni-worker"
|
27
|
+
},
|
28
|
+
"keywords": [
|
29
|
+
"worker",
|
30
|
+
"multithreading",
|
31
|
+
"web worker",
|
32
|
+
"worker_threads",
|
33
|
+
"omni",
|
34
|
+
"typescript"
|
35
|
+
],
|
36
|
+
"author": "anonaddy",
|
37
|
+
"license": "MIT",
|
38
|
+
"devDependencies": {
|
39
|
+
"@types/jest": "^29.5.14",
|
40
|
+
"@types/lodash": "^4.17.15",
|
41
|
+
"jest": "^29.0.0",
|
42
|
+
"jest-environment-jsdom": "^29.7.0",
|
43
|
+
"lodash": "^4.17.21",
|
44
|
+
"ts-jest": "^29.0.0",
|
45
|
+
"typescript": "^5.0.0"
|
46
|
+
},
|
47
|
+
"dependencies": {
|
48
|
+
"comlink": "^4.4.2",
|
49
|
+
"esbuild": "^0.24.2"
|
50
|
+
}
|
51
|
+
}
|