@anonaddy/omni-worker 0.1.0 → 0.2.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 +22 -14
- package/dist/cjs/helpers/builder.js +22 -0
- package/dist/cjs/helpers/builder.js.map +1 -0
- package/dist/cjs/helpers/pool.js +9 -0
- package/dist/cjs/helpers/pool.js.map +1 -0
- package/dist/cjs/node/builder/helpers.js +14 -28
- package/dist/cjs/node/builder/helpers.js.map +1 -1
- package/dist/cjs/node/builder/index.js +2 -1
- package/dist/cjs/node/builder/index.js.map +1 -1
- package/dist/cjs/node/index.js +110 -51
- package/dist/cjs/node/index.js.map +1 -1
- package/dist/cjs/node/pool.js +131 -73
- package/dist/cjs/node/pool.js.map +1 -1
- package/dist/cjs/node.js +6 -0
- package/dist/cjs/node.js.map +1 -0
- package/dist/cjs/types/helpers.js +11 -0
- package/dist/cjs/types/helpers.js.map +1 -0
- package/dist/cjs/types/node-omni-worker.js +3 -0
- package/dist/cjs/types/node-omni-worker.js.map +1 -0
- package/dist/cjs/types/web-omni-worker.js +3 -0
- package/dist/cjs/types/web-omni-worker.js.map +1 -0
- package/dist/cjs/web/builder/helpers.js +9 -0
- package/dist/cjs/web/builder/helpers.js.map +1 -0
- package/dist/cjs/{node/builder/node.js → web/builder/index.js} +10 -17
- package/dist/cjs/web/builder/index.js.map +1 -0
- package/dist/cjs/web/builder/model.js +3 -0
- package/dist/cjs/web/builder/model.js.map +1 -0
- package/dist/cjs/web/index.js +156 -0
- package/dist/cjs/web/index.js.map +1 -0
- package/dist/cjs/web/pool.js +143 -0
- package/dist/cjs/web/pool.js.map +1 -0
- package/dist/cjs/web/provider/helpers.js +9 -0
- package/dist/cjs/web/provider/helpers.js.map +1 -0
- package/dist/cjs/web/provider/index.js +69 -0
- package/dist/cjs/web/provider/index.js.map +1 -0
- package/dist/cjs/web/provider/model.js +3 -0
- package/dist/cjs/web/provider/model.js.map +1 -0
- package/dist/cjs/web.js +8 -0
- package/dist/cjs/web.js.map +1 -0
- package/dist/mjs/helpers/builder.js +16 -0
- package/dist/mjs/helpers/builder.js.map +1 -0
- package/dist/mjs/helpers/pool.js +6 -0
- package/dist/mjs/helpers/pool.js.map +1 -0
- package/dist/mjs/node/builder/helpers.js +14 -27
- package/dist/mjs/node/builder/helpers.js.map +1 -1
- package/dist/mjs/node/builder/index.js +4 -3
- package/dist/mjs/node/builder/index.js.map +1 -1
- package/dist/mjs/node/index.js +111 -51
- package/dist/mjs/node/index.js.map +1 -1
- package/dist/mjs/node/pool.js +132 -73
- package/dist/mjs/node/pool.js.map +1 -1
- package/dist/mjs/node.js +2 -0
- package/dist/mjs/node.js.map +1 -0
- package/dist/mjs/types/helpers.js +8 -0
- package/dist/mjs/types/helpers.js.map +1 -0
- package/dist/mjs/types/node-omni-worker.js +2 -0
- package/dist/mjs/types/node-omni-worker.js.map +1 -0
- package/dist/mjs/types/web-omni-worker.js +2 -0
- package/dist/mjs/types/web-omni-worker.js.map +1 -0
- package/dist/mjs/web/builder/helpers.js +6 -0
- package/dist/mjs/web/builder/helpers.js.map +1 -0
- package/dist/mjs/web/builder/index.js +10 -0
- package/dist/mjs/web/builder/index.js.map +1 -0
- package/dist/mjs/web/builder/model.js +2 -0
- package/dist/mjs/web/builder/model.js.map +1 -0
- package/dist/mjs/web/index.js +120 -0
- package/dist/mjs/web/index.js.map +1 -0
- package/dist/mjs/web/pool.js +140 -0
- package/dist/mjs/web/pool.js.map +1 -0
- package/dist/mjs/web/provider/helpers.js +6 -0
- package/dist/mjs/web/provider/helpers.js.map +1 -0
- package/dist/mjs/web/provider/index.js +32 -0
- package/dist/mjs/web/provider/index.js.map +1 -0
- package/dist/mjs/web/provider/model.js +2 -0
- package/dist/mjs/web/provider/model.js.map +1 -0
- package/dist/mjs/web.js +3 -0
- package/dist/mjs/web.js.map +1 -0
- package/dist/types/helpers/builder.d.ts +2 -0
- package/dist/types/helpers/builder.d.ts.map +1 -0
- package/dist/types/helpers/pool.d.ts +2 -0
- package/dist/types/helpers/pool.d.ts.map +1 -0
- package/dist/types/node/builder/helpers.d.ts +0 -1
- package/dist/types/node/builder/helpers.d.ts.map +1 -1
- package/dist/types/node/builder/index.d.ts +2 -2
- package/dist/types/node/builder/index.d.ts.map +1 -1
- package/dist/types/node/index.d.ts +15 -13
- package/dist/types/node/index.d.ts.map +1 -1
- package/dist/types/node/pool.d.ts +4 -3
- package/dist/types/node/pool.d.ts.map +1 -1
- package/dist/types/node.d.ts +3 -0
- package/dist/types/node.d.ts.map +1 -0
- package/dist/types/types/helpers.d.ts +6 -0
- package/dist/types/types/helpers.d.ts.map +1 -0
- package/dist/types/types/node-omni-worker.d.ts +90 -0
- package/dist/types/types/node-omni-worker.d.ts.map +1 -0
- package/dist/types/types/web-omni-worker.d.ts +103 -0
- package/dist/types/types/web-omni-worker.d.ts.map +1 -0
- package/dist/types/web/builder/helpers.d.ts +3 -0
- package/dist/types/web/builder/helpers.d.ts.map +1 -0
- package/dist/types/web/builder/index.d.ts +8 -0
- package/dist/types/web/builder/index.d.ts.map +1 -0
- package/dist/types/web/builder/model.d.ts +11 -0
- package/dist/types/web/builder/model.d.ts.map +1 -0
- package/dist/types/web/index.d.ts +35 -0
- package/dist/types/web/index.d.ts.map +1 -0
- package/dist/types/web/pool.d.ts +31 -0
- package/dist/types/web/pool.d.ts.map +1 -0
- package/dist/types/web/provider/helpers.d.ts +2 -0
- package/dist/types/web/provider/helpers.d.ts.map +1 -0
- package/dist/types/web/provider/index.d.ts +16 -0
- package/dist/types/web/provider/index.d.ts.map +1 -0
- package/dist/types/web/provider/model.d.ts +7 -0
- package/dist/types/web/provider/model.d.ts.map +1 -0
- package/dist/types/web.d.ts +4 -0
- package/dist/types/web.d.ts.map +1 -0
- package/package.json +23 -9
- package/dist/cjs/index.js +0 -8
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/node/builder/node.js.map +0 -1
- package/dist/mjs/index.js +0 -3
- package/dist/mjs/index.js.map +0 -1
- package/dist/mjs/node/builder/node.js +0 -14
- package/dist/mjs/node/builder/node.js.map +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/node/builder/node.d.ts +0 -7
- package/dist/types/node/builder/node.d.ts.map +0 -1
package/README.md
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# 👷 Omni Worker - A Versatile Worker for Typescript
|
2
|
-
Webworkers (browser) and thread workers (NodeJS) are usually
|
2
|
+
Webworkers (browser) and thread workers (NodeJS) are usually hard to implement, since the code that runs on a worker needs to be serializable and have complex interfaces to transfer the data into the workers. 3rd party modules are usually hard to get working or don't work at all giving you a serious headache when implementing them...
|
3
3
|
|
4
|
-
Enter the stage, OmniWorkers! These workers allow you to simply declare
|
4
|
+
Enter the stage, OmniWorkers! These workers allow you to simply declare an interface with functions (currently with the exception of callbacks) that will actually run inside a worker. Whether it be on the web (webworker) or on NodeJS (thread worker), these workers will run your code inside a worker, period.
|
5
5
|
|
6
6
|
## How does it Work?
|
7
|
-
In the case of using typescript for front-end projects, a UI will simply block at certain tasks if they're too heavy.
|
7
|
+
In the case of using typescript for front-end projects, a UI will simply block at certain tasks if they're too heavy. You could use yielding functions, but that will slow your code down as a result. Similarly, in case of NodeJS, load times can significantly increase since the JavaScript V8 engine only runs one event loop thread. Even when using asynchronous code, the event loop is still just running on one thread.
|
8
8
|
|
9
9
|
### Workers
|
10
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 (event loop). The advantage of this is that we now have more than one event loop thread to work with.
|
11
11
|
|
12
|
-
The drawback here, is that all code executed inside these (contexed) workers needs to be serializable, since your code is basically shipped into the worker for execution. This is done through the Worker communication interface. Should your code depend on other 3rd party libraries, your code
|
12
|
+
The drawback here, is that all code executed inside these (contexed) workers needs to be serializable, since your code is basically shipped into the worker for execution. This is done through the Worker communication interface. Should your code depend on other 3rd party libraries, your code would have most-likely not executed, since the code inside the worker context is closed off from the main thread context.
|
13
13
|
|
14
14
|
### 👷 OmniWorkers
|
15
|
-
So
|
15
|
+
So what makes OmniWorkers to special? First off, their simplicity. Simply declare your code and expose it to the main event loop. Then build the OmniWorker by simply referencing you file. Now, the OmniWorker will expose the object with functions or a class 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 first look at an example for NodeJS:
|
16
16
|
|
17
17
|
#### `👷♀️ workers/worker.ts`
|
18
18
|
```javascript
|
@@ -33,9 +33,9 @@ const fnObj: IMyOmniWorkerFunctions = {
|
|
33
33
|
// In this case, diamond interface can even be omitted, since inferred
|
34
34
|
NodeOmniWorker.expose<IMyOmniWorkerFunctions>(fnObj);
|
35
35
|
```
|
36
|
-
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
|
36
|
+
The worker.ts file contains the logic you would like to run inside the container. In our example above we created our own function inside a object containing functions, but notice how we included a 3rd party module (lodash). This demonstrates that an OmniWorker can just as well use any other 3rd party module as well as logic defined by you.
|
37
37
|
|
38
|
-
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
|
38
|
+
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, for you to access it in the rest of your code:
|
39
39
|
|
40
40
|
#### `🏭 index.ts`
|
41
41
|
```javascript
|
@@ -46,7 +46,7 @@ import { IMyOmniWorkerFunctions } from "./worker-model";
|
|
46
46
|
const WORKER_DIR = "./src/workers";
|
47
47
|
|
48
48
|
// Placing anonymous async function on the event loop
|
49
|
-
|
49
|
+
(async () => {
|
50
50
|
|
51
51
|
// Build the worker from the file you specified and the
|
52
52
|
// functions that were exposed
|
@@ -64,7 +64,7 @@ setTimeout(async () => {
|
|
64
64
|
|
65
65
|
// Release resources once you're done!
|
66
66
|
await worker.destroy();
|
67
|
-
}
|
67
|
+
})();
|
68
68
|
```
|
69
69
|
Your code can now just **asynchronously** call the logic you have declared and exposed inside your worker.ts. Simply call the `worker.use()` function and you'll have access to all the functions defined by you inside your `IMyOwnWorkerFunctions` interface.
|
70
70
|
|
@@ -78,7 +78,7 @@ Sometimes, having one worker just isn't enough. Worker pools are capable of inst
|
|
78
78
|
import { NodeOmniWorkerPool } from '@anonaddy/omni-worker';
|
79
79
|
import { IMyOmniWorkerFunctions } from "./worker-model";
|
80
80
|
|
81
|
-
|
81
|
+
(async () => {
|
82
82
|
|
83
83
|
const pool = await NodeOmniWorkerPool
|
84
84
|
.buildAndLaunch<IMyOmniWorkerFunctions>(
|
@@ -104,10 +104,17 @@ setTimeout(async () => {
|
|
104
104
|
// Release resources once you're done!
|
105
105
|
await pool.destroy();
|
106
106
|
|
107
|
-
}
|
107
|
+
})();
|
108
108
|
|
109
109
|
```
|
110
|
-
The code example above will create a worker pool of 4 workers with the code you specified. All work will be distributed round-robin across the OmniWorkers.
|
110
|
+
The code example above will create a worker pool of 4 workers with the code you specified. All work will be distributed round-robin across the OmniWorkers. We recommend using as many workers inside the pool as you have threads on the processor running the code. This will make sure that performance is optimal. Too many workers can AND WILL degrade performance in JavaScript.
|
111
|
+
|
112
|
+
### 🕸️ WebOmniWorkers
|
113
|
+
When you're building a module for the web, you can't use Node's module resolution. Instead, you will use a bundler (e.g. Webpack, Rollup) to bundle your code, usually in a format like ESM. This use case is covered by WebOmniWorkers.
|
114
|
+
|
115
|
+
The only difference when instantiating a WebOmniWorker is that instead of a project file path, a URL object needs to be parsed into the build function. Also, when using any 3rd party modules, these need to be statically linked inside your `worker.ts` file and can't be linked using Node's module resolution.
|
116
|
+
|
117
|
+
Please see the [demos](https://github.com/avluent/omni-worker-demos) project to see how a WebOmniWorker(pool) can be instantiated on a JavaScript module project.
|
111
118
|
|
112
119
|
### Under the Hood
|
113
120
|
This modules leverages [Webpack](https://www.npmjs.com/package/webpack) to parse your container code and build it. From there, it's wired up to the main event loop using a great module called [Comlink](https://github.com/GoogleChromeLabs/comlink). This module abstracts away the communication between the main event loop and Workers.
|
@@ -129,14 +136,15 @@ rm -rf node_modules
|
|
129
136
|
mv package-lock.json package-lock.json.backup # make sure to make a backup!
|
130
137
|
npm i
|
131
138
|
```
|
132
|
-
Should you not be able to solve your issue, please drop me an email so we can
|
139
|
+
Should you not be able to solve your issue, please drop me an email so we can have a look at your case. We're constantly attempt at improving compatitiblity for these types of use cases. Should you have an idea of you own, also feel free to provide a pull request.
|
133
140
|
|
134
141
|
# Project Status
|
135
|
-
|
142
|
+
We're happy to announce that `WebOmniWorker`s along with their pools are now available. In case you have any questions, make sure to drop me an [email](mailto:7ebr7fa0@anonaddy.com) and I will get back to you asap.
|
136
143
|
|
137
144
|
## Change Log
|
138
145
|
|Version|Description|
|
139
146
|
|:-:|-|
|
147
|
+
|**v0.2.0**|Introduced the WebOmniWorker and the WebOmniWorker pool. Split the project for Node and Web|
|
140
148
|
|**v0.1.0**|Switched from ESBuild to Webpack for NodeJS and introduced the NodeOmniWorkerPool|
|
141
149
|
|**v0.0.1**|Basic of single NodeOmniWorker with code having native dependencies still crashing|
|
142
150
|
|
@@ -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=builder.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../src/helpers/builder.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,mDAAmD;QACnD,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../../src/helpers/pool.ts"],"names":[],"mappings":";;AAAA,0BAIC;AAJD,SAAgB,OAAO;IACrB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.getCallerDir = getCallerDir;
|
7
6
|
exports.buildWorkerCode = buildWorkerCode;
|
8
7
|
const webpack_node_externals_1 = __importDefault(require("webpack-node-externals"));
|
9
8
|
const path_1 = __importDefault(require("path"));
|
@@ -13,27 +12,13 @@ const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin")
|
|
13
12
|
const BUILD_PATH = path_1.default.join(__dirname, '.out');
|
14
13
|
const BUILD_FILENAME = 'bundle.js';
|
15
14
|
const BUILD_FILE_PATH = path_1.default.join(BUILD_PATH, BUILD_FILENAME);
|
16
|
-
function getCallerDir() {
|
17
|
-
try {
|
18
|
-
const stack = new Error().stack;
|
19
|
-
const line = stack.split('\n')[1].trim();
|
20
|
-
const segment = line.split(' ')[1];
|
21
|
-
const part = segment.split(':')[0];
|
22
|
-
const dirName = path_1.default.dirname(part);
|
23
|
-
return dirName;
|
24
|
-
}
|
25
|
-
catch (e) {
|
26
|
-
// console.error('building caller path failed', e);
|
27
|
-
return './';
|
28
|
-
}
|
29
|
-
}
|
30
15
|
function buildWorkerCode(entryFile) {
|
16
|
+
if (!fs_1.default.existsSync(BUILD_PATH)) {
|
17
|
+
fs_1.default.mkdirSync(BUILD_PATH, { recursive: true });
|
18
|
+
}
|
19
|
+
const config = getWebpackConfig(entryFile);
|
20
|
+
const compiler = (0, webpack_1.webpack)(config);
|
31
21
|
return new Promise((resolve, reject) => {
|
32
|
-
if (!fs_1.default.existsSync(BUILD_PATH)) {
|
33
|
-
fs_1.default.mkdirSync(BUILD_PATH, { recursive: true });
|
34
|
-
}
|
35
|
-
const config = getWebpackConfig(entryFile);
|
36
|
-
const compiler = (0, webpack_1.webpack)(config);
|
37
22
|
compiler.run((err, stats) => {
|
38
23
|
if (err) {
|
39
24
|
reject(err);
|
@@ -47,21 +32,16 @@ function buildWorkerCode(entryFile) {
|
|
47
32
|
*/
|
48
33
|
const bundledCode = fs_1.default
|
49
34
|
.readFileSync(BUILD_FILE_PATH, 'utf8');
|
50
|
-
resolve(bundledCode);
|
51
35
|
// clean up the build folder
|
52
|
-
fs_1.default.rm(BUILD_PATH, { recursive: true, force:
|
36
|
+
fs_1.default.rm(BUILD_PATH, { recursive: true, force: false }, (err) => {
|
53
37
|
if (err) {
|
54
38
|
reject(err);
|
55
39
|
}
|
56
40
|
});
|
41
|
+
resolve(bundledCode);
|
57
42
|
});
|
58
43
|
});
|
59
44
|
}
|
60
|
-
const resolveNodeModules = (packageName) => require.resolve(packageName, {
|
61
|
-
paths: [
|
62
|
-
path_1.default.resolve(__dirname, 'node_modules')
|
63
|
-
]
|
64
|
-
});
|
65
45
|
const getWebpackConfig = (entry) => ({
|
66
46
|
entry,
|
67
47
|
output: {
|
@@ -139,7 +119,8 @@ const getWebpackConfig = (entry) => ({
|
|
139
119
|
resolveNodeModules('@babel/preset-typescript')
|
140
120
|
],
|
141
121
|
plugins: [
|
142
|
-
resolveNodeModules('@babel/plugin-transform-modules-commonjs')
|
122
|
+
resolveNodeModules('@babel/plugin-transform-modules-commonjs'),
|
123
|
+
[resolveNodeModules('@babel/plugin-proposal-decorators'), { "legacy": true }],
|
143
124
|
]
|
144
125
|
},
|
145
126
|
}
|
@@ -155,4 +136,9 @@ const getWebpackConfig = (entry) => ({
|
|
155
136
|
})],
|
156
137
|
cache: false
|
157
138
|
});
|
139
|
+
const resolveNodeModules = (packageName) => require.resolve(packageName, {
|
140
|
+
paths: [
|
141
|
+
path_1.default.resolve(__dirname, 'node_modules')
|
142
|
+
]
|
143
|
+
});
|
158
144
|
//# sourceMappingURL=helpers.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../src/node/builder/helpers.ts"],"names":[],"mappings":";;;;;AAWA,
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../src/node/builder/helpers.ts"],"names":[],"mappings":";;;;;AAWA,0CAoCC;AA/CD,oFAAmD;AACnD,gDAAwB;AAExB,4CAAoB;AACpB,qCAAkC;AAClC,kFAAiD;AAEjD,MAAM,UAAU,GAAG,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AAChD,MAAM,cAAc,GAAG,WAA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KAAK,EAAE;YACL;gBACE,IAAI,EAAE,OAAO;gBACb,GAAG,EAAG;oBACJ;wBACE,MAAM,EAAE,kBAAkB,CAAC,cAAc,CAAC;wBAC1C,OAAO,EAAE;4BACP,OAAO,EAAE;gCACP,kBAAkB,CAAC,mBAAmB,CAAC;gCACvC,kBAAkB,CAAC,0BAA0B,CAAC;6BAC/C;4BACD,OAAO,EAAE;gCACP,kBAAkB,CAAC,0CAA0C,CAAC;gCAC9D,CAAC,kBAAkB,CAAC,mCAAmC,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;6BAC9E;yBACF;qBACF;iBACF;gBACD,OAAO,EAAE,cAAc;aACxB;SACF;KACF;IACD,OAAO,EAAE,YAAY;IACrB,KAAK,EAAE,SAAS;IAChB,SAAS,EAAE,CAAC,IAAA,gCAAa,EAAC;YACxB,UAAU,EAAE,UAAU;SACvB,CAAC,CAAC;IACH,KAAK,EAAE,KAAK;CACb,CAAC,CAAC;AAEH,MAAM,kBAAkB,GAAG,CAAC,WAAmB,EAAE,EAAE,CACjD,OAAO,CAAC,OAAO,CAAC,WAAW,EACzB;IACE,KAAK,EAAE;QACL,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,cAAc,CAAC;KACxC;CACF,CAAC,CAAC"}
|
@@ -9,8 +9,9 @@ const worker_threads_1 = require("worker_threads");
|
|
9
9
|
const node_adapter_js_1 = __importDefault(require("comlink/dist/umd/node-adapter.js"));
|
10
10
|
const helpers_1 = require("./helpers");
|
11
11
|
const path_1 = __importDefault(require("path"));
|
12
|
+
const builder_1 = require("../../helpers/builder");
|
12
13
|
const genWorkerCodeFromFile = async (workerPath) => {
|
13
|
-
const callerDir = (0,
|
14
|
+
const callerDir = (0, builder_1.getCallerDir)();
|
14
15
|
const resolvedPath = path_1.default.resolve(callerDir, workerPath);
|
15
16
|
const code = await (0, helpers_1.buildWorkerCode)(resolvedPath);
|
16
17
|
return code;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/node/builder/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAC9B,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/node/builder/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAC9B,mDAAwD;AACxD,uFAA4D;AAC5D,uCAA4C;AAC5C,gDAAwB;AACxB,mDAAqD;AAE9C,MAAM,qBAAqB,GAAG,KAAK,EACxC,UAAkB,EACD,EAAE;IACnB,MAAM,SAAS,GAAG,IAAA,sBAAY,GAAE,CAAC;IACjC,MAAM,YAAY,GAAG,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;IACzD,MAAM,IAAI,GAAG,MAAM,IAAA,yBAAe,EAAC,YAAY,CAAC,CAAC;IACjD,OAAO,IAAI,CAAC;AACd,CAAC,CAAA;AAPY,QAAA,qBAAqB,yBAOjC;AAEM,MAAM,6BAA6B,GAAG,CAC3C,IAAY,EAC4C,EAAE;IAE1D,MAAM,MAAM,GAAG,IAAI,uBAAY,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IACtD,MAAM,GAAG,GAAG,iBAAO,CAAC,IAAI,CAAI,IAAA,yBAAY,EAAC,MAAM,CAAC,CAAC,CAAC;IAElD,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AACzB,CAAC,CAAA;AARY,QAAA,6BAA6B,iCAQzC"}
|
package/dist/cjs/node/index.js
CHANGED
@@ -1,4 +1,42 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
7
|
+
var _, done = false;
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
9
|
+
var context = {};
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
14
|
+
if (kind === "accessor") {
|
15
|
+
if (result === void 0) continue;
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
20
|
+
}
|
21
|
+
else if (_ = accept(result)) {
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
23
|
+
else descriptor[key] = _;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
27
|
+
done = true;
|
28
|
+
};
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
30
|
+
var useValue = arguments.length > 2;
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
33
|
+
}
|
34
|
+
return useValue ? value : void 0;
|
35
|
+
};
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
39
|
+
};
|
2
40
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
41
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
42
|
};
|
@@ -8,59 +46,80 @@ const worker_threads_1 = require("worker_threads");
|
|
8
46
|
const comlink_1 = __importDefault(require("comlink"));
|
9
47
|
const node_adapter_1 = __importDefault(require("comlink/dist/umd/node-adapter"));
|
10
48
|
const builder_1 = require("./builder");
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
49
|
+
const helpers_1 = require("../types/helpers");
|
50
|
+
/**
|
51
|
+
* OmniWorker for NodeJS
|
52
|
+
*/
|
53
|
+
let NodeOmniWorker = (() => {
|
54
|
+
let _classDecorators = [(0, helpers_1.staticImplements)(), (0, helpers_1.staticImplements)()];
|
55
|
+
let _classDescriptor;
|
56
|
+
let _classExtraInitializers = [];
|
57
|
+
let _classThis;
|
58
|
+
var NodeOmniWorker = _classThis = class {
|
59
|
+
constructor(code, worker, api) {
|
60
|
+
this.isInitialized = () => (this._api !== undefined);
|
61
|
+
this.use = () => {
|
62
|
+
const isInitialized = this.isInitialized();
|
63
|
+
if (isInitialized) {
|
64
|
+
return this._api;
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
throw Error('worker is not yet initialized. make sure to call the build() function, first');
|
68
|
+
}
|
69
|
+
};
|
70
|
+
this.clone = (numOfTimes) => {
|
71
|
+
const workers = [];
|
26
72
|
const code = this._code;
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
73
|
+
for (let i = 0; i <= numOfTimes; i++) {
|
74
|
+
const { worker, api } = (0, builder_1.buildNodeApiAndWorkerFromCode)(code);
|
75
|
+
workers.push(new NodeOmniWorker(code, worker, api));
|
76
|
+
}
|
77
|
+
return workers;
|
78
|
+
};
|
79
|
+
this.destroy = async () => {
|
80
|
+
await this._worker.terminate();
|
81
|
+
};
|
82
|
+
this._code = code;
|
83
|
+
this._worker = worker;
|
84
|
+
this._api = api;
|
85
|
+
return this;
|
86
|
+
}
|
87
|
+
/**
|
88
|
+
* Handles the building of the necessary resources for an OmniWorker to function.
|
89
|
+
* This usually means, building from the consumer's code, creating the comlink
|
90
|
+
* interface between the worker and the main thread as well as the worker itself.
|
91
|
+
*
|
92
|
+
* @param path Relative path FROM YOUR PROJECT's ROOT to the file to be the worker
|
93
|
+
* @returns An OmniWorker
|
94
|
+
*/
|
95
|
+
static async build(path) {
|
96
|
+
const code = await (0, builder_1.genWorkerCodeFromFile)(path);
|
97
|
+
const { worker, api } = (0, builder_1.buildNodeApiAndWorkerFromCode)(code);
|
98
|
+
return new NodeOmniWorker(code, worker, api);
|
99
|
+
}
|
100
|
+
};
|
101
|
+
__setFunctionName(_classThis, "NodeOmniWorker");
|
102
|
+
(() => {
|
103
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
104
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
105
|
+
NodeOmniWorker = _classThis = _classDescriptor.value;
|
106
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
107
|
+
})();
|
40
108
|
/**
|
41
|
-
*
|
42
|
-
*
|
43
|
-
*
|
44
|
-
*
|
45
|
-
* @param path The relative path to where the worker module is located
|
46
|
-
* @returns A new Node OmniWorker
|
109
|
+
* Expose the functions inside the worker to the rest of the application.
|
110
|
+
* After having exposed the functions, the build step can be initiated.
|
111
|
+
* @param exposable A class, object with functions or a single function to be
|
112
|
+
* exposed to the main thread.
|
47
113
|
*/
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
}
|
53
|
-
|
114
|
+
_classThis.expose = (exposable) => {
|
115
|
+
if (worker_threads_1.parentPort) {
|
116
|
+
comlink_1.default.expose(exposable, (0, node_adapter_1.default)(worker_threads_1.parentPort));
|
117
|
+
}
|
118
|
+
};
|
119
|
+
(() => {
|
120
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
121
|
+
})();
|
122
|
+
return NodeOmniWorker = _classThis;
|
123
|
+
})();
|
54
124
|
exports.NodeOmniWorker = NodeOmniWorker;
|
55
|
-
/**
|
56
|
-
* Expose the functions inside the worker to the rest of the application.
|
57
|
-
* After having exposed the functions, the build step can be initiated.
|
58
|
-
* @param functions An object with functions inside the worker to be exposed to
|
59
|
-
* the rest of the application.
|
60
|
-
*/
|
61
|
-
NodeOmniWorker.expose = (functions) => {
|
62
|
-
if (worker_threads_1.parentPort) {
|
63
|
-
comlink_1.default.expose(functions, (0, node_adapter_1.default)(worker_threads_1.parentPort));
|
64
|
-
}
|
65
|
-
};
|
66
125
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,mDAAoE;AACpE,sDAA8B;AAC9B,iFAAyD;AACzD,uCAAiF;AACjF,8CAAoD;AAEpD;;GAEG;IAGU,cAAc;4BAF1B,IAAA,0BAAgB,GAAc,EAC9B,IAAA,0BAAgB,GAAc;;;;;QAO7B,YACE,IAAY,EACZ,MAAoB,EACpB,GAA4B;YAoCvB,kBAAa,GAAG,GAAY,EAAE,CAAC,CACpC,IAAI,CAAC,IAAI,KAAK,SAAS,CACxB,CAAC;YAEK,QAAG,GAAG,GAAG,EAAE;gBAChB,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;gBAC3C,IAAI,aAAa,EAAE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