node-thread-decorator 1.0.4 → 1.0.5
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/LICENSE +21 -0
- package/README.md +284 -31
- package/package.json +54 -11
- package/dist/thread.d.ts +0 -1
- package/dist/thread.decorator.d.ts +0 -23
- package/dist/thread.decorator.js +0 -55
- package/dist/thread.decorator.js.map +0 -1
- package/dist/thread.js +0 -16
- package/dist/thread.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/thread.decorator.d.ts +0 -12
- package/src/thread.decorator.ts +0 -67
- package/src/thread.ts +0 -16
- package/tsconfig.json +0 -29
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Mike Lima
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,67 +1,320 @@
|
|
|
1
1
|
# Node Thread Decorator
|
|
2
2
|
|
|
3
3
|
[![node version][node-image]][node-url]
|
|
4
|
+
[](https://www.npmjs.com/package/node-thread-decorator)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
6
|
|
|
5
7
|
[node-image]: https://img.shields.io/badge/node.js-%3E=_18.0-green.svg?style=flat-square
|
|
6
8
|
[node-url]: http://nodejs.org/download/
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
A TypeScript decorator that executes class methods in separate Node.js worker threads, preventing CPU-intensive operations from blocking the main event loop.
|
|
9
11
|
|
|
12
|
+
## Table of Contents
|
|
13
|
+
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Quick Start](#quick-start)
|
|
16
|
+
- [API Reference](#api-reference)
|
|
17
|
+
- [Usage Examples](#usage-examples)
|
|
18
|
+
- [Important Limitations](#important-limitations)
|
|
19
|
+
- [Using External Dependencies](#using-external-dependencies)
|
|
20
|
+
- [Best Practices](#best-practices)
|
|
21
|
+
- [License](#license)
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install node-thread-decorator
|
|
10
27
|
```
|
|
11
|
-
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { RunInNewThread } from 'node-thread-decorator';
|
|
33
|
+
|
|
34
|
+
class Calculator {
|
|
35
|
+
@RunInNewThread()
|
|
36
|
+
async heavyComputation(iterations: number): Promise<number> {
|
|
37
|
+
let result = 0;
|
|
38
|
+
for (let i = 0; i < iterations; i++) {
|
|
39
|
+
result += Math.sqrt(i) * Math.sin(i);
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const calc = new Calculator();
|
|
46
|
+
const result = await calc.heavyComputation(10000000);
|
|
12
47
|
```
|
|
13
48
|
|
|
14
|
-
|
|
49
|
+
## API Reference
|
|
50
|
+
|
|
51
|
+
### `@RunInNewThread(timeout?: number)`
|
|
52
|
+
|
|
53
|
+
A method decorator that executes the decorated method in a separate worker thread.
|
|
15
54
|
|
|
16
|
-
|
|
55
|
+
| Parameter | Type | Required | Description |
|
|
56
|
+
|-----------|------|----------|-------------|
|
|
57
|
+
| `timeout` | `number` | No | Maximum execution time in milliseconds. If exceeded, the worker is terminated and an error is thrown. |
|
|
17
58
|
|
|
18
|
-
|
|
19
|
-
|
|
59
|
+
**Returns:** `Promise<T>` - The decorated method always returns a Promise, even if the original method was synchronous.
|
|
60
|
+
|
|
61
|
+
## Usage Examples
|
|
62
|
+
|
|
63
|
+
### Basic Usage
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { RunInNewThread } from 'node-thread-decorator';
|
|
67
|
+
|
|
68
|
+
class MyService {
|
|
69
|
+
@RunInNewThread()
|
|
70
|
+
async processData(data: number[]): Promise<number> {
|
|
71
|
+
return data.reduce((sum, n) => sum + n, 0);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### With Timeout
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
class MyService {
|
|
80
|
+
@RunInNewThread(5000) // 5 second timeout
|
|
81
|
+
async longRunningTask(): Promise<string> {
|
|
82
|
+
// If this takes more than 5 seconds, it will throw an error
|
|
83
|
+
return 'completed';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
20
86
|
```
|
|
21
|
-
- Usage
|
|
22
|
-
```ts
|
|
23
|
-
import { Controller, Get } from '@nestjs/common';
|
|
24
87
|
|
|
88
|
+
### NestJS Integration
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { Controller, Get } from '@nestjs/common';
|
|
25
92
|
import { RunInNewThread } from 'node-thread-decorator';
|
|
26
93
|
|
|
27
94
|
@Controller()
|
|
28
95
|
export class HealthController {
|
|
29
|
-
/**
|
|
30
|
-
* Blocks the main thread for a specified duration in milliseconds.
|
|
31
|
-
* This function runs in a new process due to the decorator `@RunInNewThread`.
|
|
32
|
-
*
|
|
33
|
-
* @param {number} milliseconds - The time to block the thread in milliseconds.
|
|
34
|
-
*/
|
|
35
96
|
@RunInNewThread()
|
|
36
|
-
|
|
97
|
+
heavyOperation(milliseconds: number): void {
|
|
37
98
|
const start = Date.now();
|
|
38
99
|
while (Date.now() - start < milliseconds) {
|
|
39
|
-
//
|
|
100
|
+
// CPU-intensive work
|
|
40
101
|
}
|
|
41
102
|
}
|
|
42
103
|
|
|
43
|
-
|
|
44
|
-
* Endpoint that returns health information of the service.
|
|
45
|
-
* It will block the main thread for 10 seconds to simulate a heavy operation.
|
|
46
|
-
*
|
|
47
|
-
* @returns {Promise<string>} - A promise that resolves when the health check is completed.
|
|
48
|
-
*/
|
|
49
|
-
@Get(['/health', '/'])
|
|
104
|
+
@Get('/health')
|
|
50
105
|
async getHealth(): Promise<string> {
|
|
51
|
-
this.
|
|
52
|
-
|
|
53
|
-
|
|
106
|
+
await this.heavyOperation(10000); // Won't block the main thread
|
|
107
|
+
return 'OK';
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Important Limitations
|
|
113
|
+
|
|
114
|
+
### 1. Class Methods Only
|
|
115
|
+
|
|
116
|
+
The decorator **only works with class methods**. Standalone functions are not supported.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// ✅ Supported - Class method
|
|
120
|
+
class MyClass {
|
|
121
|
+
@RunInNewThread()
|
|
122
|
+
async myMethod() { /* ... */ }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ❌ Not Supported - Standalone function
|
|
126
|
+
@RunInNewThread() // This won't work
|
|
127
|
+
function myFunction() { /* ... */ }
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 2. No Access to External Scope (Closures)
|
|
131
|
+
|
|
132
|
+
Methods executed in worker threads **cannot access variables, imports, or closures** from the original scope. Each worker runs in an isolated context.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { someHelper } from './helpers';
|
|
136
|
+
|
|
137
|
+
const CONFIG = { maxRetries: 3 };
|
|
138
|
+
|
|
139
|
+
class MyService {
|
|
140
|
+
@RunInNewThread()
|
|
141
|
+
async process(): Promise<void> {
|
|
142
|
+
// ❌ This will fail - 'someHelper' is not defined in worker
|
|
143
|
+
someHelper();
|
|
144
|
+
|
|
145
|
+
// ❌ This will fail - 'CONFIG' is not defined in worker
|
|
146
|
+
console.log(CONFIG.maxRetries);
|
|
54
147
|
}
|
|
55
148
|
}
|
|
56
149
|
```
|
|
57
150
|
|
|
58
|
-
|
|
151
|
+
### 3. No Access to `this` Context
|
|
152
|
+
|
|
153
|
+
The `this` keyword inside decorated methods does **not** refer to the class instance. Instance properties and other methods are not accessible.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
class MyService {
|
|
157
|
+
private value = 42;
|
|
158
|
+
|
|
159
|
+
@RunInNewThread()
|
|
160
|
+
async getValue(): Promise<number> {
|
|
161
|
+
// ❌ This will fail - 'this.value' is undefined in worker
|
|
162
|
+
return this.value;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
@RunInNewThread()
|
|
166
|
+
async callOther(): Promise<void> {
|
|
167
|
+
// ❌ This will fail - 'this.otherMethod' is not a function in worker
|
|
168
|
+
this.otherMethod();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
otherMethod() { /* ... */ }
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### 4. Arguments Must Be Serializable
|
|
176
|
+
|
|
177
|
+
All arguments passed to decorated methods must be serializable (transferable via `postMessage`). Functions, class instances, and circular references cannot be passed.
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
class MyService {
|
|
181
|
+
@RunInNewThread()
|
|
182
|
+
async process(data: unknown): Promise<void> {
|
|
183
|
+
// ...
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ✅ Supported types
|
|
188
|
+
await service.process({ name: 'John', age: 30 }); // Plain objects
|
|
189
|
+
await service.process([1, 2, 3]); // Arrays
|
|
190
|
+
await service.process('hello'); // Primitives
|
|
191
|
+
await service.process(null); // null/undefined
|
|
192
|
+
|
|
193
|
+
// ❌ Not supported
|
|
194
|
+
await service.process(() => {}); // Functions
|
|
195
|
+
await service.process(new MyClass()); // Class instances
|
|
196
|
+
await service.process(circularRef); // Circular references
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Using External Dependencies
|
|
200
|
+
|
|
201
|
+
To use external modules inside a decorated method, you must use `require()` **inside** the method body with the **absolute path** to the module.
|
|
202
|
+
|
|
203
|
+
### Pattern for External Dependencies
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
import { RunInNewThread } from 'node-thread-decorator';
|
|
207
|
+
import path from 'path';
|
|
208
|
+
|
|
209
|
+
class MyService {
|
|
210
|
+
@RunInNewThread()
|
|
211
|
+
async processWithExternal(servicePath: string, data: number[]): Promise<number> {
|
|
212
|
+
// ✅ Correct - require() inside the method with absolute path
|
|
213
|
+
const { Calculator } = require(servicePath);
|
|
214
|
+
const calc = new Calculator();
|
|
215
|
+
return calc.sum(data);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Usage - pass the absolute path as argument
|
|
220
|
+
const servicePath = path.resolve(__dirname, './services/calculator');
|
|
221
|
+
const result = await service.processWithExternal(servicePath, [1, 2, 3]);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Using Node.js Built-in Modules
|
|
225
|
+
|
|
226
|
+
Built-in modules can be required directly by name:
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
class MyService {
|
|
230
|
+
@RunInNewThread()
|
|
231
|
+
async readFileInThread(filePath: string): Promise<string> {
|
|
232
|
+
const fs = require('fs');
|
|
233
|
+
const path = require('path');
|
|
234
|
+
return fs.readFileSync(filePath, 'utf-8');
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
@RunInNewThread()
|
|
238
|
+
async getSystemInfo(): Promise<object> {
|
|
239
|
+
const os = require('os');
|
|
240
|
+
return {
|
|
241
|
+
cpus: os.cpus().length,
|
|
242
|
+
memory: os.totalmem(),
|
|
243
|
+
platform: os.platform()
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Complete Example with External Service
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
// services/calculator.ts
|
|
253
|
+
export class Calculator {
|
|
254
|
+
sum(numbers: number[]): number {
|
|
255
|
+
return numbers.reduce((a, b) => a + b, 0);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
multiply(numbers: number[]): number {
|
|
259
|
+
return numbers.reduce((a, b) => a * b, 1);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// my-service.ts
|
|
264
|
+
import { RunInNewThread } from 'node-thread-decorator';
|
|
265
|
+
import path from 'path';
|
|
266
|
+
|
|
267
|
+
class MyService {
|
|
268
|
+
private readonly calculatorPath = path.resolve(__dirname, './services/calculator');
|
|
269
|
+
|
|
270
|
+
@RunInNewThread()
|
|
271
|
+
async calculate(calculatorPath: string, numbers: number[]): Promise<number> {
|
|
272
|
+
const { Calculator } = require(calculatorPath);
|
|
273
|
+
const calc = new Calculator();
|
|
274
|
+
return calc.sum(numbers);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async run(): Promise<number> {
|
|
278
|
+
// Pass the absolute path as an argument
|
|
279
|
+
return this.calculate(this.calculatorPath, [1, 2, 3, 4, 5]);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Best Practices
|
|
285
|
+
|
|
286
|
+
### ✅ Do
|
|
287
|
+
|
|
288
|
+
- Use for CPU-intensive operations (cryptography, data processing, complex calculations)
|
|
289
|
+
- Pass all required data as arguments
|
|
290
|
+
- Use `require()` inside the method for external dependencies
|
|
291
|
+
- Always use absolute paths when requiring local modules
|
|
292
|
+
- Handle errors with try/catch blocks
|
|
293
|
+
- Set appropriate timeouts for long-running operations
|
|
294
|
+
|
|
295
|
+
### ❌ Don't
|
|
296
|
+
|
|
297
|
+
- Don't use for I/O-bound operations (the event loop handles these efficiently)
|
|
298
|
+
- Don't try to access external scope variables
|
|
299
|
+
- Don't use `this` to access instance properties
|
|
300
|
+
- Don't pass non-serializable data as arguments
|
|
301
|
+
- Don't use relative paths with `require()` inside workers
|
|
302
|
+
|
|
303
|
+
### When to Use
|
|
304
|
+
|
|
305
|
+
| Use Case | Recommendation |
|
|
306
|
+
|----------|----------------|
|
|
307
|
+
| Heavy mathematical computations | ✅ Use decorator |
|
|
308
|
+
| Image/video processing | ✅ Use decorator |
|
|
309
|
+
| Data encryption/hashing | ✅ Use decorator |
|
|
310
|
+
| Database queries | ❌ Use async/await |
|
|
311
|
+
| HTTP requests | ❌ Use async/await |
|
|
312
|
+
| File I/O | ❌ Use async/await |
|
|
59
313
|
|
|
60
|
-
|
|
314
|
+
## Contributors
|
|
61
315
|
|
|
62
316
|
[<img alt="mikemajesty" src="https://avatars1.githubusercontent.com/u/11630212?s=460&v=4&s=117" width="117">](https://github.com/mikemajesty)
|
|
63
317
|
|
|
64
318
|
## License
|
|
65
319
|
|
|
66
|
-
|
|
67
|
-
[License](https://opensource.org/licenses/mit-license.php)
|
|
320
|
+
MIT License - see [LICENSE](https://opensource.org/licenses/mit-license.php) for details
|
package/package.json
CHANGED
|
@@ -1,20 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-thread-decorator",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "dist/
|
|
6
|
-
"
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "A TypeScript decorator that executes class methods in separate Node.js worker threads",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
7
20
|
"scripts": {
|
|
8
|
-
"build": "tsc"
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"prebuild": "rm -rf dist",
|
|
23
|
+
"test": "jest",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"typescript",
|
|
28
|
+
"decorator",
|
|
29
|
+
"thread",
|
|
30
|
+
"worker",
|
|
31
|
+
"worker-threads",
|
|
32
|
+
"nodejs",
|
|
33
|
+
"multithreading",
|
|
34
|
+
"async",
|
|
35
|
+
"parallel",
|
|
36
|
+
"cpu-intensive",
|
|
37
|
+
"nestjs"
|
|
38
|
+
],
|
|
39
|
+
"author": "Mike Lima <mikemajesty>",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/mikemajesty/node-thread-decorator.git"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/mikemajesty/node-thread-decorator/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/mikemajesty/node-thread-decorator#readme",
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=18.0.0"
|
|
9
51
|
},
|
|
10
|
-
"keywords": [],
|
|
11
|
-
"author": "",
|
|
12
|
-
"license": "ISC",
|
|
13
52
|
"dependencies": {
|
|
14
|
-
"colorette": "^2.0.20"
|
|
15
|
-
"typescript": "^5.7.3"
|
|
53
|
+
"colorette": "^2.0.20"
|
|
16
54
|
},
|
|
17
55
|
"devDependencies": {
|
|
18
|
-
"@types/
|
|
56
|
+
"@types/jest": "^30.0.0",
|
|
57
|
+
"@types/node": "^22.13.7",
|
|
58
|
+
"jest": "^30.2.0",
|
|
59
|
+
"ts-jest": "^29.4.6",
|
|
60
|
+
"ts-node": "^10.9.2",
|
|
61
|
+
"typescript": "^5.7.3"
|
|
19
62
|
}
|
|
20
63
|
}
|
package/dist/thread.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Decorator that executes a method in a new thread.
|
|
3
|
-
*
|
|
4
|
-
* This decorator utilizes `worker_threads` to execute the target method in a separate thread,
|
|
5
|
-
* improving performance for CPU-intensive tasks without blocking the main event loop.
|
|
6
|
-
* Optionally, a timeout can be specified to terminate the thread if it exceeds the given time.
|
|
7
|
-
*
|
|
8
|
-
* @param timeout - (Optional) The maximum execution time in milliseconds before the thread is terminated.
|
|
9
|
-
* If not provided, the thread runs without a timeout.
|
|
10
|
-
* @returns A method decorator that modifies the method to execute in a new thread.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* class TaskProcessor {
|
|
15
|
-
* @RunInNewThread(5000) // Runs in a new thread with a 5-second timeout
|
|
16
|
-
* heavyComputation() {
|
|
17
|
-
* // CPU-intensive task
|
|
18
|
-
* }
|
|
19
|
-
* }
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
export declare function RunInNewThread(timeout?: number):
|
|
23
|
-
(target: object, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
package/dist/thread.decorator.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunInNewThread = RunInNewThread;
|
|
4
|
-
const colorette_1 = require("colorette");
|
|
5
|
-
const worker_threads_1 = require("worker_threads");
|
|
6
|
-
function RunInNewThread(timeout) {
|
|
7
|
-
return function (target, key, descriptor) {
|
|
8
|
-
const originalMethod = descriptor.value;
|
|
9
|
-
descriptor.value = function (...args) {
|
|
10
|
-
const fnCode = originalMethod.toString();
|
|
11
|
-
const worker = new worker_threads_1.Worker(`${__dirname}/thread.js`);
|
|
12
|
-
let timeoutId = null;
|
|
13
|
-
worker.postMessage([fnCode, args]);
|
|
14
|
-
if (timeout) {
|
|
15
|
-
timeoutId = setTimeout(() => {
|
|
16
|
-
const className = target.constructor.name;
|
|
17
|
-
const methodName = key;
|
|
18
|
-
const error = new Error('worker execution timed out.');
|
|
19
|
-
Object.assign(error, { context: `${className}/${methodName}` });
|
|
20
|
-
console.error(error);
|
|
21
|
-
worker.terminate();
|
|
22
|
-
}, timeout);
|
|
23
|
-
}
|
|
24
|
-
worker.on('message', (value) => {
|
|
25
|
-
if (timeoutId)
|
|
26
|
-
clearTimeout(timeoutId);
|
|
27
|
-
if (value.error) {
|
|
28
|
-
return Promise.reject(value.error);
|
|
29
|
-
}
|
|
30
|
-
if (value.success) {
|
|
31
|
-
return Promise.resolve(value.success);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
worker.on('error', (err) => {
|
|
35
|
-
var _a;
|
|
36
|
-
if (timeoutId)
|
|
37
|
-
clearTimeout(timeoutId);
|
|
38
|
-
if (err.name === 'ReferenceError') {
|
|
39
|
-
console.error((0, colorette_1.red)('worker error '), err);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
console.error((0, colorette_1.red)('worker error '), (_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : err);
|
|
43
|
-
});
|
|
44
|
-
worker.on('exit', (code) => {
|
|
45
|
-
if (timeoutId)
|
|
46
|
-
clearTimeout(timeoutId);
|
|
47
|
-
if (code !== 0) {
|
|
48
|
-
console.error((0, colorette_1.red)(`worker stopped with exit code ${code}`));
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
return descriptor;
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
//# sourceMappingURL=thread.decorator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"thread.decorator.js","sourceRoot":"","sources":["../src/thread.decorator.ts"],"names":[],"mappings":";;AAGA,wCA+DC;AAlED,yCAAgC;AAChC,mDAAwC;AAExC,SAAgB,cAAc,CAAC,OAAgB;IAC7C,OAAO,UAAU,MAAc,EAAE,GAAW,EAAE,UAA8B;QAC1E,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAGxC,UAAU,CAAC,KAAK,GAAG,UAAU,GAAG,IAAe;YAE7C,MAAM,MAAM,GAAG,cAAc,CAAC,QAAQ,EAAE,CAAC;YAGzC,MAAM,MAAM,GAAG,IAAI,uBAAM,CAAC,GAAG,SAAS,YAAY,CAAC,CAAC;YAEpD,IAAI,SAAS,GAA0B,IAAI,CAAC;YAG5C,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;YAGnC,IAAI,OAAO,EAAE,CAAC;gBACZ,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;oBAC1B,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;oBAC1C,MAAM,UAAU,GAAG,GAAG,CAAC;oBACvB,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;oBACvD,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,GAAG,SAAS,IAAI,UAAU,EAAE,EAAE,CAAC,CAAC;oBAEhE,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;oBACrB,MAAM,CAAC,SAAS,EAAE,CAAC;gBACrB,CAAC,EAAE,OAAO,CAAC,CAAC;YACd,CAAC;YAGD,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,KAAyC,EAAE,EAAE;gBACjE,IAAI,SAAS;oBAAE,YAAY,CAAC,SAAS,CAAC,CAAC;gBACvC,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;oBAChB,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBACrC,CAAC;gBAED,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;oBAClB,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBACxC,CAAC;YACH,CAAC,CAAC,CAAC;YAGH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAU,EAAE,EAAE;;gBAChC,IAAI,SAAS;oBAAE,YAAY,CAAC,SAAS,CAAC,CAAC;gBACvC,IAAI,GAAG,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;oBAClC,OAAO,CAAC,KAAK,CAAC,IAAA,eAAG,EAAC,eAAe,CAAC,EAAE,GAAG,CAAC,CAAC;oBACzC,OAAO;gBACT,CAAC;gBACD,OAAO,CAAC,KAAK,CAAC,IAAA,eAAG,EAAC,eAAe,CAAC,EAAE,MAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,OAAO,mCAAI,GAAG,CAAC,CAAC;YAC3D,CAAC,CAAC,CAAC;YAGH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAY,EAAE,EAAE;gBACjC,IAAI,SAAS;oBAAE,YAAY,CAAC,SAAS,CAAC,CAAC;gBACvC,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;oBACf,OAAO,CAAC,KAAK,CAAC,IAAA,eAAG,EAAC,iCAAiC,IAAI,EAAE,CAAC,CAAC,CAAC;gBAC9D,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/thread.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const worker_threads_1 = require("worker_threads");
|
|
4
|
-
worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.on('message', async (data) => {
|
|
5
|
-
try {
|
|
6
|
-
const [fnCode, args] = data;
|
|
7
|
-
const removeAsync = fnCode.replace('async', '');
|
|
8
|
-
const fn = new Function(`return async function ${removeAsync}`)();
|
|
9
|
-
const result = await fn(...args);
|
|
10
|
-
worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.postMessage({ success: result });
|
|
11
|
-
}
|
|
12
|
-
catch (error) {
|
|
13
|
-
worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.postMessage({ error });
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
//# sourceMappingURL=thread.js.map
|
package/dist/thread.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"thread.js","sourceRoot":"","sources":["../src/thread.ts"],"names":[],"mappings":";;AAAA,mDAA4C;AAE5C,2BAAU,aAAV,2BAAU,uBAAV,2BAAU,CAAE,EAAE,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;IACvC,IAAI,CAAC;QACH,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,IAAI,CAAC;QAE5B,MAAM,WAAW,GAAI,MAAiB,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QAE5D,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,yBAAyB,WAAW,EAAE,CAAC,EAAE,CAAC;QAClE,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;QAEjC,2BAAU,aAAV,2BAAU,uBAAV,2BAAU,CAAE,WAAW,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,2BAAU,aAAV,2BAAU,uBAAV,2BAAU,CAAE,WAAW,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IACrC,CAAC;AACH,CAAC,CAAC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"fileNames":["../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.es2016.d.ts","../node_modules/typescript/lib/lib.es2017.d.ts","../node_modules/typescript/lib/lib.es2018.d.ts","../node_modules/typescript/lib/lib.es2019.d.ts","../node_modules/typescript/lib/lib.es2020.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.dom.iterable.d.ts","../node_modules/typescript/lib/lib.webworker.importscripts.d.ts","../node_modules/typescript/lib/lib.scripthost.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../node_modules/typescript/lib/lib.es2016.intl.d.ts","../node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts","../node_modules/typescript/lib/lib.es2017.date.d.ts","../node_modules/typescript/lib/lib.es2017.object.d.ts","../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2017.string.d.ts","../node_modules/typescript/lib/lib.es2017.intl.d.ts","../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../node_modules/typescript/lib/lib.es2018.intl.d.ts","../node_modules/typescript/lib/lib.es2018.promise.d.ts","../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../node_modules/typescript/lib/lib.es2019.array.d.ts","../node_modules/typescript/lib/lib.es2019.object.d.ts","../node_modules/typescript/lib/lib.es2019.string.d.ts","../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../node_modules/typescript/lib/lib.es2019.intl.d.ts","../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../node_modules/typescript/lib/lib.es2020.date.d.ts","../node_modules/typescript/lib/lib.es2020.promise.d.ts","../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2020.string.d.ts","../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2020.intl.d.ts","../node_modules/typescript/lib/lib.es2020.number.d.ts","../node_modules/typescript/lib/lib.decorators.d.ts","../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../node_modules/typescript/lib/lib.es2017.full.d.ts","../node_modules/colorette/index.d.ts","../src/thread.decorator.ts","../src/thread.ts","../node_modules/@types/node/compatibility/disposable.d.ts","../node_modules/@types/node/compatibility/indexable.d.ts","../node_modules/@types/node/compatibility/iterators.d.ts","../node_modules/@types/node/compatibility/index.d.ts","../node_modules/@types/node/globals.typedarray.d.ts","../node_modules/@types/node/buffer.buffer.d.ts","../node_modules/undici-types/header.d.ts","../node_modules/undici-types/readable.d.ts","../node_modules/undici-types/file.d.ts","../node_modules/undici-types/fetch.d.ts","../node_modules/undici-types/formdata.d.ts","../node_modules/undici-types/connector.d.ts","../node_modules/undici-types/client.d.ts","../node_modules/undici-types/errors.d.ts","../node_modules/undici-types/dispatcher.d.ts","../node_modules/undici-types/global-dispatcher.d.ts","../node_modules/undici-types/global-origin.d.ts","../node_modules/undici-types/pool-stats.d.ts","../node_modules/undici-types/pool.d.ts","../node_modules/undici-types/handlers.d.ts","../node_modules/undici-types/balanced-pool.d.ts","../node_modules/undici-types/agent.d.ts","../node_modules/undici-types/mock-interceptor.d.ts","../node_modules/undici-types/mock-agent.d.ts","../node_modules/undici-types/mock-client.d.ts","../node_modules/undici-types/mock-pool.d.ts","../node_modules/undici-types/mock-errors.d.ts","../node_modules/undici-types/proxy-agent.d.ts","../node_modules/undici-types/env-http-proxy-agent.d.ts","../node_modules/undici-types/retry-handler.d.ts","../node_modules/undici-types/retry-agent.d.ts","../node_modules/undici-types/api.d.ts","../node_modules/undici-types/interceptors.d.ts","../node_modules/undici-types/util.d.ts","../node_modules/undici-types/cookies.d.ts","../node_modules/undici-types/patch.d.ts","../node_modules/undici-types/websocket.d.ts","../node_modules/undici-types/eventsource.d.ts","../node_modules/undici-types/filereader.d.ts","../node_modules/undici-types/diagnostics-channel.d.ts","../node_modules/undici-types/content-type.d.ts","../node_modules/undici-types/cache.d.ts","../node_modules/undici-types/index.d.ts","../node_modules/@types/node/globals.d.ts","../node_modules/@types/node/assert.d.ts","../node_modules/@types/node/assert/strict.d.ts","../node_modules/@types/node/async_hooks.d.ts","../node_modules/@types/node/buffer.d.ts","../node_modules/@types/node/child_process.d.ts","../node_modules/@types/node/cluster.d.ts","../node_modules/@types/node/console.d.ts","../node_modules/@types/node/constants.d.ts","../node_modules/@types/node/crypto.d.ts","../node_modules/@types/node/dgram.d.ts","../node_modules/@types/node/diagnostics_channel.d.ts","../node_modules/@types/node/dns.d.ts","../node_modules/@types/node/dns/promises.d.ts","../node_modules/@types/node/domain.d.ts","../node_modules/@types/node/dom-events.d.ts","../node_modules/@types/node/events.d.ts","../node_modules/@types/node/fs.d.ts","../node_modules/@types/node/fs/promises.d.ts","../node_modules/@types/node/http.d.ts","../node_modules/@types/node/http2.d.ts","../node_modules/@types/node/https.d.ts","../node_modules/@types/node/inspector.d.ts","../node_modules/@types/node/module.d.ts","../node_modules/@types/node/net.d.ts","../node_modules/@types/node/os.d.ts","../node_modules/@types/node/path.d.ts","../node_modules/@types/node/perf_hooks.d.ts","../node_modules/@types/node/process.d.ts","../node_modules/@types/node/punycode.d.ts","../node_modules/@types/node/querystring.d.ts","../node_modules/@types/node/readline.d.ts","../node_modules/@types/node/readline/promises.d.ts","../node_modules/@types/node/repl.d.ts","../node_modules/@types/node/sea.d.ts","../node_modules/@types/node/sqlite.d.ts","../node_modules/@types/node/stream.d.ts","../node_modules/@types/node/stream/promises.d.ts","../node_modules/@types/node/stream/consumers.d.ts","../node_modules/@types/node/stream/web.d.ts","../node_modules/@types/node/string_decoder.d.ts","../node_modules/@types/node/test.d.ts","../node_modules/@types/node/timers.d.ts","../node_modules/@types/node/timers/promises.d.ts","../node_modules/@types/node/tls.d.ts","../node_modules/@types/node/trace_events.d.ts","../node_modules/@types/node/tty.d.ts","../node_modules/@types/node/url.d.ts","../node_modules/@types/node/util.d.ts","../node_modules/@types/node/v8.d.ts","../node_modules/@types/node/vm.d.ts","../node_modules/@types/node/wasi.d.ts","../node_modules/@types/node/worker_threads.d.ts","../node_modules/@types/node/zlib.d.ts","../node_modules/@types/node/index.d.ts"],"fileIdsList":[[59,98,101],[59,100,101],[101],[59,101,106,136],[59,101,102,107,113,114,121,133,144],[59,101,102,103,113,121],[59,101],[54,55,56,59,101],[59,101,104,145],[59,101,105,106,114,122],[59,101,106,133,141],[59,101,107,109,113,121],[59,100,101,108],[59,101,109,110],[59,101,113],[59,101,111,113],[59,100,101,113],[59,101,113,114,115,133,144],[59,101,113,114,115,128,133,136],[59,96,101,149],[59,96,101,109,113,116,121,133,144],[59,101,113,114,116,117,121,133,141,144],[59,101,116,118,133,141,144],[57,58,59,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150],[59,101,113,119],[59,101,120,144],[59,101,109,113,121,133],[59,101,122],[59,101,123],[59,100,101,124],[59,98,99,100,101,102,103,104,105,106,107,108,109,110,111,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150],[59,101,126],[59,101,127],[59,101,113,128,129],[59,101,128,130,145,147],[59,101,113,133,134,136],[59,101,133,135],[59,101,133,134],[59,101,136],[59,101,137],[59,98,101,133],[59,101,113,139,140],[59,101,139,140],[59,101,106,121,133,141],[59,101,142],[59,101,121,143],[59,101,116,127,144],[59,101,106,145],[59,101,133,146],[59,101,120,147],[59,101,148],[59,101,106,113,115,124,133,144,147,149],[59,101,133,150],[59,68,72,101,144],[59,68,101,133,144],[59,63,101],[59,65,68,101,141,144],[59,101,121,141],[59,101,151],[59,63,101,151],[59,65,68,101,121,144],[59,60,61,64,67,101,113,133,144],[59,68,75,101],[59,60,66,101],[59,68,89,90,101],[59,64,68,101,136,144,151],[59,89,101,151],[59,62,63,101,151],[59,68,101],[59,62,63,64,65,66,67,68,69,70,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,90,91,92,93,94,95,101],[59,68,83,101],[59,68,75,76,101],[59,66,68,76,77,101],[59,67,101],[59,60,63,68,101],[59,68,72,76,77,101],[59,72,101],[59,66,68,71,101,144],[59,60,65,68,75,101],[59,101,133],[59,63,68,89,101,149,151],[51,59,101,149],[59,101,149]],"fileInfos":[{"version":"69684132aeb9b5642cbcd9e22dff7818ff0ee1aa831728af0ecf97d3364d5546","affectsGlobalScope":true,"impliedFormat":1},{"version":"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","impliedFormat":1},{"version":"3facaf05f0c5fc569c5649dd359892c98a85557e3e0c847964caeb67076f4d75","impliedFormat":1},{"version":"e44bb8bbac7f10ecc786703fe0a6a4b952189f908707980ba8f3c8975a760962","impliedFormat":1},{"version":"5e1c4c362065a6b95ff952c0eab010f04dcd2c3494e813b493ecfd4fcb9fc0d8","impliedFormat":1},{"version":"68d73b4a11549f9c0b7d352d10e91e5dca8faa3322bfb77b661839c42b1ddec7","impliedFormat":1},{"version":"5efce4fc3c29ea84e8928f97adec086e3dc876365e0982cc8479a07954a3efd4","impliedFormat":1},{"version":"092c2bfe125ce69dbb1223c85d68d4d2397d7d8411867b5cc03cec902c233763","affectsGlobalScope":true,"impliedFormat":1},{"version":"07f073f19d67f74d732b1adea08e1dc66b1b58d77cb5b43931dee3d798a2fd53","affectsGlobalScope":true,"impliedFormat":1},{"version":"80e18897e5884b6723488d4f5652167e7bb5024f946743134ecc4aa4ee731f89","affectsGlobalScope":true,"impliedFormat":1},{"version":"cd034f499c6cdca722b60c04b5b1b78e058487a7085a8e0d6fb50809947ee573","affectsGlobalScope":true,"impliedFormat":1},{"version":"c57796738e7f83dbc4b8e65132f11a377649c00dd3eee333f672b8f0a6bea671","affectsGlobalScope":true,"impliedFormat":1},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true,"impliedFormat":1},{"version":"515d0b7b9bea2e31ea4ec968e9edd2c39d3eebf4a2d5cbd04e88639819ae3b71","affectsGlobalScope":true,"impliedFormat":1},{"version":"0559b1f683ac7505ae451f9a96ce4c3c92bdc71411651ca6ddb0e88baaaad6a3","affectsGlobalScope":true,"impliedFormat":1},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true,"impliedFormat":1},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true,"impliedFormat":1},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true,"impliedFormat":1},{"version":"936e80ad36a2ee83fc3caf008e7c4c5afe45b3cf3d5c24408f039c1d47bdc1df","affectsGlobalScope":true,"impliedFormat":1},{"version":"d15bea3d62cbbdb9797079416b8ac375ae99162a7fba5de2c6c505446486ac0a","affectsGlobalScope":true,"impliedFormat":1},{"version":"68d18b664c9d32a7336a70235958b8997ebc1c3b8505f4f1ae2b7e7753b87618","affectsGlobalScope":true,"impliedFormat":1},{"version":"eb3d66c8327153d8fa7dd03f9c58d351107fe824c79e9b56b462935176cdf12a","affectsGlobalScope":true,"impliedFormat":1},{"version":"38f0219c9e23c915ef9790ab1d680440d95419ad264816fa15009a8851e79119","affectsGlobalScope":true,"impliedFormat":1},{"version":"69ab18c3b76cd9b1be3d188eaf8bba06112ebbe2f47f6c322b5105a6fbc45a2e","affectsGlobalScope":true,"impliedFormat":1},{"version":"fef8cfad2e2dc5f5b3d97a6f4f2e92848eb1b88e897bb7318cef0e2820bceaab","affectsGlobalScope":true,"impliedFormat":1},{"version":"2f11ff796926e0832f9ae148008138ad583bd181899ab7dd768a2666700b1893","affectsGlobalScope":true,"impliedFormat":1},{"version":"4de680d5bb41c17f7f68e0419412ca23c98d5749dcaaea1896172f06435891fc","affectsGlobalScope":true,"impliedFormat":1},{"version":"954296b30da6d508a104a3a0b5d96b76495c709785c1d11610908e63481ee667","affectsGlobalScope":true,"impliedFormat":1},{"version":"ac9538681b19688c8eae65811b329d3744af679e0bdfa5d842d0e32524c73e1c","affectsGlobalScope":true,"impliedFormat":1},{"version":"0a969edff4bd52585473d24995c5ef223f6652d6ef46193309b3921d65dd4376","affectsGlobalScope":true,"impliedFormat":1},{"version":"9e9fbd7030c440b33d021da145d3232984c8bb7916f277e8ffd3dc2e3eae2bdb","affectsGlobalScope":true,"impliedFormat":1},{"version":"811ec78f7fefcabbda4bfa93b3eb67d9ae166ef95f9bff989d964061cbf81a0c","affectsGlobalScope":true,"impliedFormat":1},{"version":"717937616a17072082152a2ef351cb51f98802fb4b2fdabd32399843875974ca","affectsGlobalScope":true,"impliedFormat":1},{"version":"d7e7d9b7b50e5f22c915b525acc5a49a7a6584cf8f62d0569e557c5cfc4b2ac2","affectsGlobalScope":true,"impliedFormat":1},{"version":"71c37f4c9543f31dfced6c7840e068c5a5aacb7b89111a4364b1d5276b852557","affectsGlobalScope":true,"impliedFormat":1},{"version":"576711e016cf4f1804676043e6a0a5414252560eb57de9faceee34d79798c850","affectsGlobalScope":true,"impliedFormat":1},{"version":"89c1b1281ba7b8a96efc676b11b264de7a8374c5ea1e6617f11880a13fc56dc6","affectsGlobalScope":true,"impliedFormat":1},{"version":"74f7fa2d027d5b33eb0471c8e82a6c87216223181ec31247c357a3e8e2fddc5b","affectsGlobalScope":true,"impliedFormat":1},{"version":"d6d7ae4d1f1f3772e2a3cde568ed08991a8ae34a080ff1151af28b7f798e22ca","affectsGlobalScope":true,"impliedFormat":1},{"version":"063600664504610fe3e99b717a1223f8b1900087fab0b4cad1496a114744f8df","affectsGlobalScope":true,"impliedFormat":1},{"version":"934019d7e3c81950f9a8426d093458b65d5aff2c7c1511233c0fd5b941e608ab","affectsGlobalScope":true,"impliedFormat":1},{"version":"52ada8e0b6e0482b728070b7639ee42e83a9b1c22d205992756fe020fd9f4a47","affectsGlobalScope":true,"impliedFormat":1},{"version":"3bdefe1bfd4d6dee0e26f928f93ccc128f1b64d5d501ff4a8cf3c6371200e5e6","affectsGlobalScope":true,"impliedFormat":1},{"version":"59fb2c069260b4ba00b5643b907ef5d5341b167e7d1dbf58dfd895658bda2867","affectsGlobalScope":true,"impliedFormat":1},{"version":"639e512c0dfc3fad96a84caad71b8834d66329a1f28dc95e3946c9b58176c73a","affectsGlobalScope":true,"impliedFormat":1},{"version":"368af93f74c9c932edd84c58883e736c9e3d53cec1fe24c0b0ff451f529ceab1","affectsGlobalScope":true,"impliedFormat":1},{"version":"8e7f8264d0fb4c5339605a15daadb037bf238c10b654bb3eee14208f860a32ea","affectsGlobalScope":true,"impliedFormat":1},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true,"impliedFormat":1},{"version":"1d242d5c24cf285c88bc4fb93c5ff903de8319064e282986edeb6247ba028d5e","impliedFormat":1},{"version":"c8adda9f45d2f7ec9fb28c59859db32da6c2835f1fec96433e2729e5805fa46f","impliedFormat":99},{"version":"e843062cb9091bddec205e2dd039b4bb3e16bbc78eedf4cd05b35b760680e411","signature":"f04d7e1316bde53cfa4491d14f8b85c6b88830848ff70685e88caa6088cd160e"},{"version":"fc9e10e8d62fe97f0bea7683ba4d8cfa1e54fef21e44a50aee484ef3fd9240a7","signature":"8e609bb71c20b858c77f0e9f90bb1319db8477b13f9f965f1a1e18524bf50881"},{"version":"70521b6ab0dcba37539e5303104f29b721bfb2940b2776da4cc818c07e1fefc1","affectsGlobalScope":true,"impliedFormat":1},{"version":"030e350db2525514580ed054f712ffb22d273e6bc7eddc1bb7eda1e0ba5d395e","affectsGlobalScope":true,"impliedFormat":1},{"version":"d153a11543fd884b596587ccd97aebbeed950b26933ee000f94009f1ab142848","affectsGlobalScope":true,"impliedFormat":1},{"version":"21d819c173c0cf7cc3ce57c3276e77fd9a8a01d35a06ad87158781515c9a438a","impliedFormat":1},{"version":"a79e62f1e20467e11a904399b8b18b18c0c6eea6b50c1168bf215356d5bebfaf","affectsGlobalScope":true,"impliedFormat":1},{"version":"8fa51737611c21ba3a5ac02c4e1535741d58bec67c9bdf94b1837a31c97a2263","affectsGlobalScope":true,"impliedFormat":1},{"version":"5929864ce17fba74232584d90cb721a89b7ad277220627cc97054ba15a98ea8f","impliedFormat":1},{"version":"24bd580b5743dc56402c440dc7f9a4f5d592ad7a419f25414d37a7bfe11e342b","impliedFormat":1},{"version":"25c8056edf4314820382a5fdb4bb7816999acdcb929c8f75e3f39473b87e85bc","impliedFormat":1},{"version":"c464d66b20788266e5353b48dc4aa6bc0dc4a707276df1e7152ab0c9ae21fad8","impliedFormat":1},{"version":"78d0d27c130d35c60b5e5566c9f1e5be77caf39804636bc1a40133919a949f21","impliedFormat":1},{"version":"c6fd2c5a395f2432786c9cb8deb870b9b0e8ff7e22c029954fabdd692bff6195","impliedFormat":1},{"version":"1d6e127068ea8e104a912e42fc0a110e2aa5a66a356a917a163e8cf9a65e4a75","impliedFormat":1},{"version":"5ded6427296cdf3b9542de4471d2aa8d3983671d4cac0f4bf9c637208d1ced43","impliedFormat":1},{"version":"6bdc71028db658243775263e93a7db2fd2abfce3ca569c3cca5aee6ed5eb186d","impliedFormat":1},{"version":"cadc8aced301244057c4e7e73fbcae534b0f5b12a37b150d80e5a45aa4bebcbd","impliedFormat":1},{"version":"385aab901643aa54e1c36f5ef3107913b10d1b5bb8cbcd933d4263b80a0d7f20","impliedFormat":1},{"version":"9670d44354bab9d9982eca21945686b5c24a3f893db73c0dae0fd74217a4c219","impliedFormat":1},{"version":"0b8a9268adaf4da35e7fa830c8981cfa22adbbe5b3f6f5ab91f6658899e657a7","impliedFormat":1},{"version":"11396ed8a44c02ab9798b7dca436009f866e8dae3c9c25e8c1fbc396880bf1bb","impliedFormat":1},{"version":"ba7bc87d01492633cb5a0e5da8a4a42a1c86270e7b3d2dea5d156828a84e4882","impliedFormat":1},{"version":"4893a895ea92c85345017a04ed427cbd6a1710453338df26881a6019432febdd","impliedFormat":1},{"version":"c21dc52e277bcfc75fac0436ccb75c204f9e1b3fa5e12729670910639f27343e","impliedFormat":1},{"version":"13f6f39e12b1518c6650bbb220c8985999020fe0f21d818e28f512b7771d00f9","impliedFormat":1},{"version":"9b5369969f6e7175740bf51223112ff209f94ba43ecd3bb09eefff9fd675624a","impliedFormat":1},{"version":"4fe9e626e7164748e8769bbf74b538e09607f07ed17c2f20af8d680ee49fc1da","impliedFormat":1},{"version":"24515859bc0b836719105bb6cc3d68255042a9f02a6022b3187948b204946bd2","impliedFormat":1},{"version":"ea0148f897b45a76544ae179784c95af1bd6721b8610af9ffa467a518a086a43","impliedFormat":1},{"version":"24c6a117721e606c9984335f71711877293a9651e44f59f3d21c1ea0856f9cc9","impliedFormat":1},{"version":"dd3273ead9fbde62a72949c97dbec2247ea08e0c6952e701a483d74ef92d6a17","impliedFormat":1},{"version":"405822be75ad3e4d162e07439bac80c6bcc6dbae1929e179cf467ec0b9ee4e2e","impliedFormat":1},{"version":"0db18c6e78ea846316c012478888f33c11ffadab9efd1cc8bcc12daded7a60b6","impliedFormat":1},{"version":"e61be3f894b41b7baa1fbd6a66893f2579bfad01d208b4ff61daef21493ef0a8","impliedFormat":1},{"version":"bd0532fd6556073727d28da0edfd1736417a3f9f394877b6d5ef6ad88fba1d1a","impliedFormat":1},{"version":"89167d696a849fce5ca508032aabfe901c0868f833a8625d5a9c6e861ef935d2","impliedFormat":1},{"version":"615ba88d0128ed16bf83ef8ccbb6aff05c3ee2db1cc0f89ab50a4939bfc1943f","impliedFormat":1},{"version":"a4d551dbf8746780194d550c88f26cf937caf8d56f102969a110cfaed4b06656","impliedFormat":1},{"version":"8bd86b8e8f6a6aa6c49b71e14c4ffe1211a0e97c80f08d2c8cc98838006e4b88","impliedFormat":1},{"version":"317e63deeb21ac07f3992f5b50cdca8338f10acd4fbb7257ebf56735bf52ab00","impliedFormat":1},{"version":"4732aec92b20fb28c5fe9ad99521fb59974289ed1e45aecb282616202184064f","impliedFormat":1},{"version":"2e85db9e6fd73cfa3d7f28e0ab6b55417ea18931423bd47b409a96e4a169e8e6","impliedFormat":1},{"version":"c46e079fe54c76f95c67fb89081b3e399da2c7d109e7dca8e4b58d83e332e605","impliedFormat":1},{"version":"bf67d53d168abc1298888693338cb82854bdb2e69ef83f8a0092093c2d562107","impliedFormat":1},{"version":"d2bc987ae352271d0d615a420dcf98cc886aa16b87fb2b569358c1fe0ca0773d","affectsGlobalScope":true,"impliedFormat":1},{"version":"4f0539c58717cbc8b73acb29f9e992ab5ff20adba5f9b57130691c7f9b186a4d","impliedFormat":1},{"version":"7394959e5a741b185456e1ef5d64599c36c60a323207450991e7a42e08911419","impliedFormat":1},{"version":"76103716ba397bbb61f9fa9c9090dca59f39f9047cb1352b2179c5d8e7f4e8d0","impliedFormat":1},{"version":"f9677e434b7a3b14f0a9367f9dfa1227dfe3ee661792d0085523c3191ae6a1a4","affectsGlobalScope":true,"impliedFormat":1},{"version":"4314c7a11517e221f7296b46547dbc4df047115b182f544d072bdccffa57fc72","impliedFormat":1},{"version":"115971d64632ea4742b5b115fb64ed04bcaae2c3c342f13d9ba7e3f9ee39c4e7","impliedFormat":1},{"version":"c2510f124c0293ab80b1777c44d80f812b75612f297b9857406468c0f4dafe29","affectsGlobalScope":true,"impliedFormat":1},{"version":"a40826e8476694e90da94aa008283a7de50d1dafd37beada623863f1901cb7fb","impliedFormat":1},{"version":"86956cc2eb9dd371d6fab493d326a574afedebf76eef3fa7833b8e0d9b52d6f1","affectsGlobalScope":true,"impliedFormat":1},{"version":"19d5f8d3930e9f99aa2c36258bf95abbe5adf7e889e6181872d1cdba7c9a7dd5","impliedFormat":1},{"version":"e6f5a38687bebe43a4cef426b69d34373ef68be9a6b1538ec0a371e69f309354","impliedFormat":1},{"version":"a6bf63d17324010ca1fbf0389cab83f93389bb0b9a01dc8a346d092f65b3605f","impliedFormat":1},{"version":"e009777bef4b023a999b2e5b9a136ff2cde37dc3f77c744a02840f05b18be8ff","impliedFormat":1},{"version":"1e0d1f8b0adfa0b0330e028c7941b5a98c08b600efe7f14d2d2a00854fb2f393","impliedFormat":1},{"version":"ee1ee365d88c4c6c0c0a5a5701d66ebc27ccd0bcfcfaa482c6e2e7fe7b98edf7","affectsGlobalScope":true,"impliedFormat":1},{"version":"88bc59b32d0d5b4e5d9632ac38edea23454057e643684c3c0b94511296f2998c","affectsGlobalScope":true,"impliedFormat":1},{"version":"1ff5a53a58e756d2661b73ba60ffe274231a4432d21f7a2d0d9e4f6aa99f4283","impliedFormat":1},{"version":"1e289f30a48126935a5d408a91129a13a59c9b0f8c007a816f9f16ef821e144e","impliedFormat":1},{"version":"2ea254f944dfe131df1264d1fb96e4b1f7d110195b21f1f5dbb68fdd394e5518","impliedFormat":1},{"version":"5135bdd72cc05a8192bd2e92f0914d7fc43ee077d1293dc622a049b7035a0afb","impliedFormat":1},{"version":"4f80de3a11c0d2f1329a72e92c7416b2f7eab14f67e92cac63bb4e8d01c6edc8","impliedFormat":1},{"version":"6d386bc0d7f3afa1d401afc3e00ed6b09205a354a9795196caed937494a713e6","impliedFormat":1},{"version":"b11cb909327c874a4e81bfb390bf0d231e5bf9439052689ab80ba8afa50da17b","affectsGlobalScope":true,"impliedFormat":1},{"version":"23459c1915878a7c1e86e8bdb9c187cddd3aea105b8b1dfce512f093c969bc7e","impliedFormat":1},{"version":"b1b6ee0d012aeebe11d776a155d8979730440082797695fc8e2a5c326285678f","impliedFormat":1},{"version":"45875bcae57270aeb3ebc73a5e3fb4c7b9d91d6b045f107c1d8513c28ece71c0","impliedFormat":1},{"version":"1dc73f8854e5c4506131c4d95b3a6c24d0c80336d3758e95110f4c7b5cb16397","affectsGlobalScope":true,"impliedFormat":1},{"version":"5f6f1d54779d0b9ed152b0516b0958cd34889764c1190434bbf18e7a8bb884cd","affectsGlobalScope":true,"impliedFormat":1},{"version":"3f16a7e4deafa527ed9995a772bb380eb7d3c2c0fd4ae178c5263ed18394db2c","impliedFormat":1},{"version":"c6b4e0a02545304935ecbf7de7a8e056a31bb50939b5b321c9d50a405b5a0bba","impliedFormat":1},{"version":"fab29e6d649aa074a6b91e3bdf2bff484934a46067f6ee97a30fcd9762ae2213","impliedFormat":1},{"version":"8145e07aad6da5f23f2fcd8c8e4c5c13fb26ee986a79d03b0829b8fce152d8b2","impliedFormat":1},{"version":"e1120271ebbc9952fdc7b2dd3e145560e52e06956345e6fdf91d70ca4886464f","impliedFormat":1},{"version":"814118df420c4e38fe5ae1b9a3bafb6e9c2aa40838e528cde908381867be6466","impliedFormat":1},{"version":"f7b1df115dbd1b8522cba4f404a9f4fdcd5169e2137129187ffeee9d287e4fd1","impliedFormat":1},{"version":"c878f74b6d10b267f6075c51ac1d8becd15b4aa6a58f79c0cfe3b24908357f60","impliedFormat":1},{"version":"37ba7b45141a45ce6e80e66f2a96c8a5ab1bcef0fc2d0f56bb58df96ec67e972","impliedFormat":1},{"version":"93452d394fdd1dc551ec62f5042366f011a00d342d36d50793b3529bfc9bd633","impliedFormat":1},{"version":"745c4240220559bd340c8aeb6e3c5270a709d3565e934dc22a69c304703956bc","affectsGlobalScope":true,"impliedFormat":1},{"version":"2754d8221d77c7b382096651925eb476f1066b3348da4b73fe71ced7801edada","impliedFormat":1},{"version":"993985beef40c7d113f6dd8f0ba26eed63028b691fbfeb6a5b63f26408dd2c6d","affectsGlobalScope":true,"impliedFormat":1},{"version":"bef91efa0baea5d0e0f0f27b574a8bc100ce62a6d7e70220a0d58af6acab5e89","affectsGlobalScope":true,"impliedFormat":1},{"version":"282fd2a1268a25345b830497b4b7bf5037a5e04f6a9c44c840cb605e19fea841","impliedFormat":1},{"version":"5360a27d3ebca11b224d7d3e38e3e2c63f8290cb1fcf6c3610401898f8e68bc3","impliedFormat":1},{"version":"66ba1b2c3e3a3644a1011cd530fb444a96b1b2dfe2f5e837a002d41a1a799e60","impliedFormat":1},{"version":"7e514f5b852fdbc166b539fdd1f4e9114f29911592a5eb10a94bb3a13ccac3c4","impliedFormat":1},{"version":"7d6ff413e198d25639f9f01f16673e7df4e4bd2875a42455afd4ecc02ef156da","affectsGlobalScope":true,"impliedFormat":1},{"version":"cb094bb347d7df3380299eb69836c2c8758626ecf45917577707c03cf816b6f4","affectsGlobalScope":true,"impliedFormat":1},{"version":"f689c4237b70ae6be5f0e4180e8833f34ace40529d1acc0676ab8fb8f70457d7","impliedFormat":1},{"version":"b02784111b3fc9c38590cd4339ff8718f9329a6f4d3fd66e9744a1dcd1d7e191","impliedFormat":1},{"version":"ac5ed35e649cdd8143131964336ab9076937fa91802ec760b3ea63b59175c10a","impliedFormat":1},{"version":"52a8e7e8a1454b6d1b5ad428efae3870ffc56f2c02d923467f2940c454aa9aec","affectsGlobalScope":true,"impliedFormat":1},{"version":"78dc0513cc4f1642906b74dda42146bcbd9df7401717d6e89ea6d72d12ecb539","impliedFormat":1},{"version":"ad90122e1cb599b3bc06a11710eb5489101be678f2920f2322b0ac3e195af78d","impliedFormat":1}],"root":[52,53],"options":{"allowSyntheticDefaultImports":true,"declaration":true,"emitDecoratorMetadata":true,"esModuleInterop":true,"experimentalDecorators":true,"module":1,"noFallthroughCasesInSwitch":false,"noImplicitAny":true,"outDir":"./","removeComments":true,"skipLibCheck":true,"sourceMap":true,"strict":true,"strictBindCallApply":false,"strictNullChecks":true,"target":4},"referencedMap":[[98,1],[99,1],[100,2],[59,3],[101,4],[102,5],[103,6],[54,7],[57,8],[55,7],[56,7],[104,9],[105,10],[106,11],[107,12],[108,13],[109,14],[110,14],[112,15],[111,16],[113,17],[114,18],[115,19],[97,20],[58,7],[116,21],[117,22],[118,23],[151,24],[119,25],[120,26],[121,27],[122,28],[123,29],[124,30],[125,31],[126,32],[127,33],[128,34],[129,34],[130,35],[131,7],[132,7],[133,36],[135,37],[134,38],[136,39],[137,40],[138,41],[139,42],[140,43],[141,44],[142,45],[143,46],[144,47],[145,48],[146,49],[147,50],[148,51],[149,52],[150,53],[51,7],[48,7],[49,7],[8,7],[9,7],[13,7],[12,7],[2,7],[14,7],[15,7],[16,7],[17,7],[18,7],[19,7],[20,7],[21,7],[3,7],[22,7],[23,7],[4,7],[24,7],[50,7],[28,7],[25,7],[26,7],[27,7],[29,7],[30,7],[31,7],[5,7],[32,7],[33,7],[34,7],[35,7],[6,7],[39,7],[36,7],[37,7],[38,7],[40,7],[7,7],[41,7],[46,7],[47,7],[42,7],[43,7],[44,7],[45,7],[1,7],[11,7],[10,7],[75,54],[85,55],[74,54],[95,56],[66,57],[65,58],[94,59],[88,60],[93,61],[68,62],[82,63],[67,64],[91,65],[63,66],[62,59],[92,67],[64,68],[69,69],[70,7],[73,69],[60,7],[96,70],[86,71],[77,72],[78,73],[80,74],[76,75],[79,76],[89,59],[71,77],[72,78],[81,79],[61,80],[84,71],[83,69],[87,7],[90,81],[52,82],[53,83]],"version":"5.8.2"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module providing decorators for execution in separate threads.
|
|
3
|
-
*/
|
|
4
|
-
declare module 'node-thread-decorator' {
|
|
5
|
-
/**
|
|
6
|
-
* Executes the decorated method in a separate thread.
|
|
7
|
-
*
|
|
8
|
-
* @param timeout - Maximum time in milliseconds for the thread execution. If not provided, there is no timeout.
|
|
9
|
-
* @returns A method decorator that executes the method in a separate thread.
|
|
10
|
-
*/
|
|
11
|
-
export function RunInNewThread(timeout?: number): MethodDecorator;
|
|
12
|
-
}
|
package/src/thread.decorator.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { red } from 'colorette';
|
|
2
|
-
import { Worker } from 'worker_threads';
|
|
3
|
-
|
|
4
|
-
export function RunInNewThread(timeout?: number) {
|
|
5
|
-
return function (target: object, key: string, descriptor: PropertyDescriptor): PropertyDescriptor {
|
|
6
|
-
const originalMethod = descriptor.value;
|
|
7
|
-
|
|
8
|
-
// Modifying the descriptor to replace the original method with one that runs in a worker thread
|
|
9
|
-
descriptor.value = function (...args: unknown[]) {
|
|
10
|
-
// Convert the method code to string to send to the worker
|
|
11
|
-
const fnCode = originalMethod.toString();
|
|
12
|
-
|
|
13
|
-
// Create a new Worker instance to run the method in a separate thread
|
|
14
|
-
const worker = new Worker(`${__dirname}/thread.js`);
|
|
15
|
-
|
|
16
|
-
let timeoutId: NodeJS.Timeout | null = null;
|
|
17
|
-
|
|
18
|
-
// Send the method code and arguments to the worker
|
|
19
|
-
worker.postMessage([fnCode, args]);
|
|
20
|
-
|
|
21
|
-
// Set a timeout if specified
|
|
22
|
-
if (timeout) {
|
|
23
|
-
timeoutId = setTimeout(() => {
|
|
24
|
-
const className = target.constructor.name;
|
|
25
|
-
const methodName = key;
|
|
26
|
-
const error = new Error('worker execution timed out.');
|
|
27
|
-
Object.assign(error, { context: `${className}/${methodName}` });
|
|
28
|
-
|
|
29
|
-
console.error(error);
|
|
30
|
-
worker.terminate(); // Terminate the worker if it times out
|
|
31
|
-
}, timeout);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Handle messages received from the worker (either success or error)
|
|
35
|
-
worker.on('message', (value: { error: Error; success: unknown }) => {
|
|
36
|
-
if (timeoutId) clearTimeout(timeoutId); // Clear the timeout if the worker finishes early
|
|
37
|
-
if (value.error) {
|
|
38
|
-
return Promise.reject(value.error); // Reject if there's an error
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (value.success) {
|
|
42
|
-
return Promise.resolve(value.success); // Resolve with the worker's result
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// Handle errors emitted by the worker thread
|
|
47
|
-
worker.on('error', (err: Error) => {
|
|
48
|
-
if (timeoutId) clearTimeout(timeoutId); // Clear timeout on error
|
|
49
|
-
if (err.name === 'ReferenceError') {
|
|
50
|
-
console.error(red('worker error '), err);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
console.error(red('worker error '), err?.message ?? err);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// Handle worker exit (whether successful or with an error code)
|
|
57
|
-
worker.on('exit', (code: number) => {
|
|
58
|
-
if (timeoutId) clearTimeout(timeoutId); // Clear timeout on worker exit
|
|
59
|
-
if (code !== 0) {
|
|
60
|
-
console.error(red(`worker stopped with exit code ${code}`)); // Log if the worker exits with a non-zero code
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
return descriptor;
|
|
66
|
-
};
|
|
67
|
-
}
|
package/src/thread.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { parentPort } from 'worker_threads';
|
|
2
|
-
|
|
3
|
-
parentPort?.on('message', async (data) => {
|
|
4
|
-
try {
|
|
5
|
-
const [fnCode, args] = data;
|
|
6
|
-
|
|
7
|
-
const removeAsync = (fnCode as string).replace('async', '');
|
|
8
|
-
|
|
9
|
-
const fn = new Function(`return async function ${removeAsync}`)();
|
|
10
|
-
const result = await fn(...args);
|
|
11
|
-
|
|
12
|
-
parentPort?.postMessage({ success: result });
|
|
13
|
-
} catch (error) {
|
|
14
|
-
parentPort?.postMessage({ error });
|
|
15
|
-
}
|
|
16
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "commonjs",
|
|
4
|
-
"declaration": true,
|
|
5
|
-
"removeComments": true,
|
|
6
|
-
"emitDecoratorMetadata": true,
|
|
7
|
-
"experimentalDecorators": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"allowSyntheticDefaultImports": true,
|
|
10
|
-
"target": "es2017",
|
|
11
|
-
"resolveJsonModule": true,
|
|
12
|
-
"sourceMap": true,
|
|
13
|
-
"outDir": "./dist",
|
|
14
|
-
"baseUrl": "./",
|
|
15
|
-
"incremental": true,
|
|
16
|
-
"strict": true,
|
|
17
|
-
"skipLibCheck": true,
|
|
18
|
-
"strictNullChecks": true,
|
|
19
|
-
"noImplicitAny": true,
|
|
20
|
-
"strictBindCallApply": false,
|
|
21
|
-
"forceConsistentCasingInFileNames": false,
|
|
22
|
-
"noFallthroughCasesInSwitch": false,
|
|
23
|
-
"paths": {
|
|
24
|
-
"@/*": [
|
|
25
|
-
"./src/*"
|
|
26
|
-
]
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|