@atlaspack/workers 2.14.31 → 2.14.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/Handle.js +33 -0
- package/dist/Worker.js +180 -0
- package/dist/WorkerFarm.js +541 -0
- package/dist/backend.js +34 -0
- package/dist/bus.js +24 -0
- package/dist/child.js +290 -0
- package/dist/childState.js +11 -0
- package/dist/cpuCount.js +72 -0
- package/dist/index.js +44 -0
- package/dist/process/ProcessChild.js +48 -0
- package/dist/process/ProcessWorker.js +68 -0
- package/dist/threads/ThreadsChild.js +31 -0
- package/dist/threads/ThreadsWorker.js +47 -0
- package/dist/types.js +2 -0
- package/dist/web/WebChild.js +34 -0
- package/dist/web/WebWorker.js +70 -0
- package/package.json +5 -6
- package/tsconfig.json +25 -2
- package/tsconfig.tsbuildinfo +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @atlaspack/workers
|
|
2
2
|
|
|
3
|
+
## 2.14.32
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`1180103`](https://github.com/atlassian-labs/atlaspack/commit/118010351ed444f8178988afb3f77807154dd933)]:
|
|
8
|
+
- @atlaspack/utils@3.0.0
|
|
9
|
+
- @atlaspack/types-internal@2.20.2
|
|
10
|
+
- @atlaspack/logger@2.14.24
|
|
11
|
+
- @atlaspack/profiler@2.14.29
|
|
12
|
+
|
|
3
13
|
## 2.14.31
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/dist/Handle.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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 build_cache_1 = require("@atlaspack/build-cache");
|
|
7
|
+
const package_json_1 = __importDefault(require("../package.json"));
|
|
8
|
+
let HANDLE_ID = 0;
|
|
9
|
+
const handleById = new Map();
|
|
10
|
+
class Handle {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
this.id = opts.id ?? ++HANDLE_ID;
|
|
13
|
+
this.fn = opts.fn;
|
|
14
|
+
this.childId = opts.childId;
|
|
15
|
+
handleById.set(this.id, this);
|
|
16
|
+
}
|
|
17
|
+
dispose() {
|
|
18
|
+
handleById.delete(this.id);
|
|
19
|
+
}
|
|
20
|
+
serialize() {
|
|
21
|
+
return {
|
|
22
|
+
id: this.id,
|
|
23
|
+
childId: this.childId,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
static deserialize(opts) {
|
|
27
|
+
return new Handle(opts);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.default = Handle;
|
|
31
|
+
// Register the Handle as a serializable class so that it will properly be deserialized
|
|
32
|
+
// by anything that uses WorkerFarm.
|
|
33
|
+
(0, build_cache_1.registerSerializableClass)(`${package_json_1.default.version}:Handle`, Handle);
|
package/dist/Worker.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
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 nullthrows_1 = __importDefault(require("nullthrows"));
|
|
7
|
+
const events_1 = __importDefault(require("events"));
|
|
8
|
+
const diagnostic_1 = __importDefault(require("@atlaspack/diagnostic"));
|
|
9
|
+
const backend_1 = require("./backend");
|
|
10
|
+
let WORKER_ID = 0;
|
|
11
|
+
class Worker extends events_1.default {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super();
|
|
14
|
+
this.id = WORKER_ID++;
|
|
15
|
+
this.sentSharedReferences = new Set();
|
|
16
|
+
this.calls = new Map();
|
|
17
|
+
this.exitCode = null;
|
|
18
|
+
this.callId = 0;
|
|
19
|
+
this.ready = false;
|
|
20
|
+
this.stopped = false;
|
|
21
|
+
this.isStopping = false;
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
24
|
+
async fork(forkModule) {
|
|
25
|
+
let filteredArgs = [];
|
|
26
|
+
if (process.execArgv) {
|
|
27
|
+
filteredArgs = process.execArgv.filter((v) => !/^--(debug|inspect|no-opt|max-old-space-size=|max-semi-space-size=|expose-gc)/.test(v));
|
|
28
|
+
for (let i = 0; i < filteredArgs.length; i++) {
|
|
29
|
+
let arg = filteredArgs[i];
|
|
30
|
+
let isArgWithParam = ((arg === '-r' || arg === '--require') &&
|
|
31
|
+
filteredArgs[i + 1] === '@atlaspack/register') ||
|
|
32
|
+
arg === '--title';
|
|
33
|
+
if (isArgWithParam) {
|
|
34
|
+
filteredArgs.splice(i, 2);
|
|
35
|
+
i--;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Workaround for https://github.com/nodejs/node/issues/29117
|
|
40
|
+
if (process.env.NODE_OPTIONS) {
|
|
41
|
+
// arg parsing logic adapted from https://stackoverflow.com/a/46946420/2352201
|
|
42
|
+
let opts = [''];
|
|
43
|
+
let quote = false;
|
|
44
|
+
for (let c of (0, nullthrows_1.default)(process.env.NODE_OPTIONS.match(/.|^$/g))) {
|
|
45
|
+
if (c === '"') {
|
|
46
|
+
quote = !quote;
|
|
47
|
+
}
|
|
48
|
+
else if (!quote && c === ' ') {
|
|
49
|
+
opts.push('');
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
opts[opts.length - 1] += c.replace(/\\(.)/, '$1');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (let i = 0; i < opts.length; i++) {
|
|
56
|
+
let opt = opts[i];
|
|
57
|
+
if (opt === '-r' || opt === '--require') {
|
|
58
|
+
filteredArgs.push(opt, opts[i + 1]);
|
|
59
|
+
i++;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let onMessage = (data) => this.receive(data);
|
|
64
|
+
let onExit = (code) => {
|
|
65
|
+
this.exitCode = code;
|
|
66
|
+
this.emit('exit', code);
|
|
67
|
+
};
|
|
68
|
+
let onError = (err) => {
|
|
69
|
+
this.emit('error', err);
|
|
70
|
+
};
|
|
71
|
+
let WorkerBackend = (0, backend_1.getWorkerBackend)(this.options.backend);
|
|
72
|
+
this.worker = new WorkerBackend(filteredArgs, onMessage, onError, onExit);
|
|
73
|
+
await this.worker.start();
|
|
74
|
+
await new Promise((resolve, reject) => {
|
|
75
|
+
this.call({
|
|
76
|
+
method: 'childInit',
|
|
77
|
+
args: [
|
|
78
|
+
forkModule,
|
|
79
|
+
{
|
|
80
|
+
shouldPatchConsole: !!this.options.shouldPatchConsole,
|
|
81
|
+
shouldTrace: !!this.options.shouldTrace,
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
retries: 0,
|
|
85
|
+
skipReadyCheck: true,
|
|
86
|
+
resolve,
|
|
87
|
+
reject,
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
let sharedRefs = this.options.sharedReferences;
|
|
91
|
+
let refsShared = new Set();
|
|
92
|
+
// in case more refs are created while initial refs are sending
|
|
93
|
+
while (refsShared.size < sharedRefs.size) {
|
|
94
|
+
await Promise.all([...sharedRefs]
|
|
95
|
+
// @ts-expect-error TS2769
|
|
96
|
+
.filter(([ref]) => !refsShared.has(ref))
|
|
97
|
+
.map(async ([ref, value]) => {
|
|
98
|
+
await this.sendSharedReference(ref, value);
|
|
99
|
+
refsShared.add(ref);
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
this.ready = true;
|
|
103
|
+
this.emit('ready');
|
|
104
|
+
}
|
|
105
|
+
sendSharedReference(ref, value) {
|
|
106
|
+
this.sentSharedReferences.add(ref);
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
this.call({
|
|
109
|
+
method: 'createSharedReference',
|
|
110
|
+
args: [ref, value],
|
|
111
|
+
resolve,
|
|
112
|
+
reject,
|
|
113
|
+
retries: 0,
|
|
114
|
+
skipReadyCheck: true,
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
send(data) {
|
|
119
|
+
this.worker.send(data);
|
|
120
|
+
}
|
|
121
|
+
call(call) {
|
|
122
|
+
if (this.stopped || this.isStopping) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let idx = this.callId++;
|
|
126
|
+
this.calls.set(idx, call);
|
|
127
|
+
let msg = {
|
|
128
|
+
type: 'request',
|
|
129
|
+
idx: idx,
|
|
130
|
+
child: this.id,
|
|
131
|
+
handle: call.handle,
|
|
132
|
+
method: call.method,
|
|
133
|
+
args: call.args,
|
|
134
|
+
};
|
|
135
|
+
if (this.ready || call.skipReadyCheck === true) {
|
|
136
|
+
// @ts-expect-error TS2345
|
|
137
|
+
this.send(msg);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
// @ts-expect-error TS2345
|
|
141
|
+
this.once('ready', () => this.send(msg));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
receive(message) {
|
|
145
|
+
if (this.stopped || this.isStopping) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (message.type === 'request') {
|
|
149
|
+
this.emit('request', message);
|
|
150
|
+
}
|
|
151
|
+
else if (message.type === 'response') {
|
|
152
|
+
let idx = message.idx;
|
|
153
|
+
if (idx == null) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
let call = this.calls.get(idx);
|
|
157
|
+
if (!call) {
|
|
158
|
+
// Return for unknown calls, these might accur if a third party process uses workers
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (message.contentType === 'error') {
|
|
162
|
+
call.reject(new diagnostic_1.default({ diagnostic: message.content }));
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
call.resolve(message.content);
|
|
166
|
+
}
|
|
167
|
+
this.calls.delete(idx);
|
|
168
|
+
this.emit('response', message);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async stop() {
|
|
172
|
+
if (!this.stopped) {
|
|
173
|
+
this.stopped = true;
|
|
174
|
+
if (this.worker) {
|
|
175
|
+
await this.worker.stop();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.default = Worker;
|