@parcel/workers 2.0.0-beta.1 → 2.0.0-nightly.1002
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/index.d.ts +22 -0
- package/lib/Handle.js +14 -45
- package/lib/Profiler.js +20 -10
- package/lib/Trace.js +16 -11
- package/lib/Worker.js +63 -31
- package/lib/WorkerFarm.js +191 -82
- package/lib/bus.js +10 -2
- package/lib/child.js +111 -56
- package/lib/childState.js +1 -1
- package/lib/cpuCount.js +28 -6
- package/lib/index.js +26 -10
- package/lib/process/ProcessChild.js +21 -11
- package/lib/process/ProcessWorker.js +30 -19
- package/lib/threads/ThreadsChild.js +32 -14
- package/lib/threads/ThreadsWorker.js +28 -16
- package/package.json +18 -7
- package/src/Handle.js +10 -39
- package/src/Profiler.js +1 -1
- package/src/Trace.js +8 -4
- package/src/Worker.js +39 -16
- package/src/WorkerFarm.js +145 -29
- package/src/bus.js +1 -1
- package/src/child.js +70 -20
- package/src/cpuCount.js +9 -3
- package/src/index.js +1 -1
- package/src/process/ProcessChild.js +1 -1
- package/src/process/ProcessWorker.js +1 -1
- package/src/threads/ThreadsWorker.js +2 -2
- package/test/cpuCount.test.js +1 -1
- package/test/integration/workerfarm/console.js +1 -1
- package/test/integration/workerfarm/logging.js +1 -1
- package/test/integration/workerfarm/reverse-handle.js +2 -2
- package/test/workerfarm.js +5 -5
|
@@ -5,30 +5,42 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
function _worker_threads() {
|
|
9
|
+
const data = require("worker_threads");
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
_worker_threads = function () {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
13
17
|
|
|
14
|
-
function
|
|
18
|
+
function _path() {
|
|
19
|
+
const data = _interopRequireDefault(require("path"));
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
_path = function () {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
19
27
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
_defineProperty(this, "execArgv", void 0);
|
|
28
|
+
function _core() {
|
|
29
|
+
const data = require("@parcel/core");
|
|
23
30
|
|
|
24
|
-
|
|
31
|
+
_core = function () {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
25
34
|
|
|
26
|
-
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
27
37
|
|
|
28
|
-
|
|
38
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
39
|
|
|
30
|
-
|
|
40
|
+
const WORKER_PATH = _path().default.join(__dirname, 'ThreadsChild.js');
|
|
31
41
|
|
|
42
|
+
class ThreadsWorker {
|
|
43
|
+
constructor(execArgv, onMessage, onError, onExit) {
|
|
32
44
|
this.execArgv = execArgv;
|
|
33
45
|
this.onMessage = onMessage;
|
|
34
46
|
this.onError = onError;
|
|
@@ -36,7 +48,7 @@ class ThreadsWorker {
|
|
|
36
48
|
}
|
|
37
49
|
|
|
38
50
|
start() {
|
|
39
|
-
this.worker = new _worker_threads.Worker(WORKER_PATH, {
|
|
51
|
+
this.worker = new (_worker_threads().Worker)(WORKER_PATH, {
|
|
40
52
|
execArgv: this.execArgv,
|
|
41
53
|
env: process.env
|
|
42
54
|
});
|
|
@@ -55,11 +67,11 @@ class ThreadsWorker {
|
|
|
55
67
|
}
|
|
56
68
|
|
|
57
69
|
handleMessage(data) {
|
|
58
|
-
this.onMessage((0, _core.restoreDeserializedObject)(data));
|
|
70
|
+
this.onMessage((0, _core().restoreDeserializedObject)(data));
|
|
59
71
|
}
|
|
60
72
|
|
|
61
73
|
send(data) {
|
|
62
|
-
this.worker.postMessage((0, _core.prepareForSerialization)(data));
|
|
74
|
+
this.worker.postMessage((0, _core().prepareForSerialization)(data));
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
}
|
package/package.json
CHANGED
|
@@ -1,29 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/workers",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-nightly.1002+5530a6ef",
|
|
4
4
|
"description": "Blazing fast, zero configuration web application bundler",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
8
8
|
},
|
|
9
|
+
"funding": {
|
|
10
|
+
"type": "opencollective",
|
|
11
|
+
"url": "https://opencollective.com/parcel"
|
|
12
|
+
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
11
15
|
"url": "https://github.com/parcel-bundler/parcel.git"
|
|
12
16
|
},
|
|
13
17
|
"main": "lib/index.js",
|
|
14
18
|
"source": "src/index.js",
|
|
19
|
+
"types": "index.d.ts",
|
|
15
20
|
"engines": {
|
|
16
|
-
"node": ">=
|
|
21
|
+
"node": ">= 12.0.0"
|
|
17
22
|
},
|
|
18
23
|
"dependencies": {
|
|
19
|
-
"@parcel/diagnostic": "2.0.0-
|
|
20
|
-
"@parcel/logger": "2.0.0-
|
|
21
|
-
"@parcel/
|
|
24
|
+
"@parcel/diagnostic": "2.0.0-nightly.1002+5530a6ef",
|
|
25
|
+
"@parcel/logger": "2.0.0-nightly.1002+5530a6ef",
|
|
26
|
+
"@parcel/types": "2.0.0-nightly.1002+5530a6ef",
|
|
27
|
+
"@parcel/utils": "2.0.0-nightly.1002+5530a6ef",
|
|
22
28
|
"chrome-trace-event": "^1.0.2",
|
|
23
29
|
"nullthrows": "^1.1.1"
|
|
24
30
|
},
|
|
25
31
|
"peerDependencies": {
|
|
26
|
-
"@parcel/core": "
|
|
32
|
+
"@parcel/core": "2.0.0-nightly.1000+5530a6ef"
|
|
33
|
+
},
|
|
34
|
+
"browser": {
|
|
35
|
+
"./src/cpuCount.js": false,
|
|
36
|
+
"./src/process/ProcessWorker.js": false,
|
|
37
|
+
"./src/threads/ThreadsWorker.js": false
|
|
27
38
|
},
|
|
28
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "5530a6eff8b619873353baeb0457ae4ec591e9fa"
|
|
29
40
|
}
|
package/src/Handle.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import type {WorkerApi} from './';
|
|
4
|
-
|
|
1
|
+
// @flow strict-local
|
|
5
2
|
import {registerSerializableClass} from '@parcel/core';
|
|
6
|
-
|
|
7
|
-
import {child} from './childState';
|
|
3
|
+
// $FlowFixMe
|
|
8
4
|
import packageJson from '../package.json';
|
|
9
5
|
|
|
10
6
|
let HANDLE_ID = 0;
|
|
11
|
-
|
|
7
|
+
// $FlowFixMe
|
|
12
8
|
export type HandleFunction = (...args: Array<any>) => any;
|
|
13
9
|
|
|
14
10
|
type HandleOpts = {|
|
|
15
|
-
fn
|
|
11
|
+
fn?: HandleFunction,
|
|
16
12
|
childId?: ?number,
|
|
17
|
-
|
|
13
|
+
id?: number,
|
|
18
14
|
|};
|
|
19
15
|
|
|
20
16
|
const handleById: Map<number, Handle> = new Map();
|
|
@@ -22,14 +18,12 @@ const handleById: Map<number, Handle> = new Map();
|
|
|
22
18
|
export default class Handle {
|
|
23
19
|
id: number;
|
|
24
20
|
childId: ?number;
|
|
25
|
-
fn: HandleFunction;
|
|
26
|
-
workerApi: WorkerApi;
|
|
21
|
+
fn: ?HandleFunction;
|
|
27
22
|
|
|
28
23
|
constructor(opts: HandleOpts) {
|
|
29
|
-
this.id = ++HANDLE_ID;
|
|
24
|
+
this.id = opts.id ?? ++HANDLE_ID;
|
|
30
25
|
this.fn = opts.fn;
|
|
31
26
|
this.childId = opts.childId;
|
|
32
|
-
this.workerApi = opts.workerApi;
|
|
33
27
|
handleById.set(this.id, this);
|
|
34
28
|
}
|
|
35
29
|
|
|
@@ -37,38 +31,15 @@ export default class Handle {
|
|
|
37
31
|
handleById.delete(this.id);
|
|
38
32
|
}
|
|
39
33
|
|
|
40
|
-
serialize() {
|
|
34
|
+
serialize(): {|childId: ?number, id: number|} {
|
|
41
35
|
return {
|
|
42
36
|
id: this.id,
|
|
43
37
|
childId: this.childId,
|
|
44
38
|
};
|
|
45
39
|
}
|
|
46
40
|
|
|
47
|
-
static deserialize(opts:
|
|
48
|
-
return
|
|
49
|
-
let workerApi;
|
|
50
|
-
if (child) {
|
|
51
|
-
workerApi = child.workerApi;
|
|
52
|
-
} else {
|
|
53
|
-
let handle = handleById.get(opts.id);
|
|
54
|
-
if (!handle) {
|
|
55
|
-
throw new Error(
|
|
56
|
-
'Corresponding Handle was not found. It may have been disposed.',
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
workerApi = handle.workerApi;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (opts.childId != null && child) {
|
|
63
|
-
throw new Error('Cannot call another child from a child');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (opts.childId != null && workerApi.callChild) {
|
|
67
|
-
return workerApi.callChild(opts.childId, {handle: opts.id, args});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return workerApi.callMaster({handle: opts.id, args}, true);
|
|
71
|
-
};
|
|
41
|
+
static deserialize(opts: HandleOpts): Handle {
|
|
42
|
+
return new Handle(opts);
|
|
72
43
|
}
|
|
73
44
|
}
|
|
74
45
|
|
package/src/Profiler.js
CHANGED
package/src/Trace.js
CHANGED
|
@@ -12,17 +12,17 @@ export default class Trace {
|
|
|
12
12
|
this.tracer = new Tracer();
|
|
13
13
|
this.tid = 0;
|
|
14
14
|
this.eventId = 0;
|
|
15
|
-
this.init();
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
getEventId() {
|
|
17
|
+
getEventId(): number {
|
|
19
18
|
return this.eventId++;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
init() {
|
|
21
|
+
init(ts: number) {
|
|
23
22
|
this.tracer.instantEvent({
|
|
24
23
|
name: 'TracingStartedInPage',
|
|
25
24
|
id: this.getEventId(),
|
|
25
|
+
ts,
|
|
26
26
|
cat: ['disabled-by-default-devtools.timeline'],
|
|
27
27
|
args: {
|
|
28
28
|
data: {
|
|
@@ -42,6 +42,7 @@ export default class Trace {
|
|
|
42
42
|
this.tracer.instantEvent({
|
|
43
43
|
name: 'TracingStartedInBrowser',
|
|
44
44
|
id: this.getEventId(),
|
|
45
|
+
ts,
|
|
45
46
|
cat: ['disabled-by-default-devtools.timeline'],
|
|
46
47
|
args: {
|
|
47
48
|
data: {
|
|
@@ -52,6 +53,9 @@ export default class Trace {
|
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
addCPUProfile(name: string, profile: Profile) {
|
|
56
|
+
if (this.eventId === 0) {
|
|
57
|
+
this.init(profile.startTime);
|
|
58
|
+
}
|
|
55
59
|
const trace = this.tracer;
|
|
56
60
|
const tid = this.tid;
|
|
57
61
|
this.tid++;
|
|
@@ -111,7 +115,7 @@ export default class Trace {
|
|
|
111
115
|
});
|
|
112
116
|
}
|
|
113
117
|
|
|
114
|
-
pipe(writable: Writable) {
|
|
118
|
+
pipe(writable: Writable): stream$Writable {
|
|
115
119
|
return this.tracer.pipe(writable);
|
|
116
120
|
}
|
|
117
121
|
|
package/src/Worker.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
|
|
3
3
|
import type {FilePath} from '@parcel/types';
|
|
4
|
-
import type {
|
|
4
|
+
import type {BackendType, WorkerImpl, WorkerMessage} from './types';
|
|
5
|
+
import type {SharedReference} from './WorkerFarm';
|
|
5
6
|
|
|
7
|
+
import nullthrows from 'nullthrows';
|
|
6
8
|
import EventEmitter from 'events';
|
|
7
9
|
import ThrowableDiagnostic from '@parcel/diagnostic';
|
|
8
10
|
import {getWorkerBackend} from './backend';
|
|
@@ -12,6 +14,7 @@ export type WorkerCall = {|
|
|
|
12
14
|
handle?: number,
|
|
13
15
|
args: $ReadOnlyArray<any>,
|
|
14
16
|
retries: number,
|
|
17
|
+
skipReadyCheck?: boolean,
|
|
15
18
|
resolve: (result: Promise<any> | any) => void,
|
|
16
19
|
reject: (error: any) => void,
|
|
17
20
|
|};
|
|
@@ -19,8 +22,8 @@ export type WorkerCall = {|
|
|
|
19
22
|
type WorkerOpts = {|
|
|
20
23
|
forcedKillTime: number,
|
|
21
24
|
backend: BackendType,
|
|
22
|
-
|
|
23
|
-
sharedReferences:
|
|
25
|
+
shouldPatchConsole?: boolean,
|
|
26
|
+
sharedReferences: $ReadOnlyMap<SharedReference, mixed>,
|
|
24
27
|
|};
|
|
25
28
|
|
|
26
29
|
let WORKER_ID = 0;
|
|
@@ -28,15 +31,15 @@ export default class Worker extends EventEmitter {
|
|
|
28
31
|
+options: WorkerOpts;
|
|
29
32
|
worker: WorkerImpl;
|
|
30
33
|
id: number = WORKER_ID++;
|
|
31
|
-
sharedReferences:
|
|
34
|
+
sharedReferences: $ReadOnlyMap<SharedReference, mixed> = new Map();
|
|
32
35
|
|
|
33
36
|
calls: Map<number, WorkerCall> = new Map();
|
|
34
|
-
exitCode = null;
|
|
35
|
-
callId = 0;
|
|
37
|
+
exitCode: ?number = null;
|
|
38
|
+
callId: number = 0;
|
|
36
39
|
|
|
37
|
-
ready = false;
|
|
38
|
-
stopped = false;
|
|
39
|
-
isStopping = false;
|
|
40
|
+
ready: boolean = false;
|
|
41
|
+
stopped: boolean = false;
|
|
42
|
+
isStopping: boolean = false;
|
|
40
43
|
|
|
41
44
|
constructor(options: WorkerOpts) {
|
|
42
45
|
super();
|
|
@@ -45,7 +48,7 @@ export default class Worker extends EventEmitter {
|
|
|
45
48
|
|
|
46
49
|
async fork(forkModule: FilePath) {
|
|
47
50
|
let filteredArgs = process.execArgv.filter(
|
|
48
|
-
v => !/^--(debug|inspect)/.test(v),
|
|
51
|
+
v => !/^--(debug|inspect|max-old-space-size=)/.test(v),
|
|
49
52
|
);
|
|
50
53
|
|
|
51
54
|
for (let i = 0; i < filteredArgs.length; i++) {
|
|
@@ -61,7 +64,19 @@ export default class Worker extends EventEmitter {
|
|
|
61
64
|
|
|
62
65
|
// Workaround for https://github.com/nodejs/node/issues/29117
|
|
63
66
|
if (process.env.NODE_OPTIONS) {
|
|
64
|
-
|
|
67
|
+
// arg parsing logic adapted from https://stackoverflow.com/a/46946420/2352201
|
|
68
|
+
let opts = [''];
|
|
69
|
+
let quote = false;
|
|
70
|
+
for (let c of nullthrows(process.env.NODE_OPTIONS.match(/.|^$/g))) {
|
|
71
|
+
if (c === '"') {
|
|
72
|
+
quote = !quote;
|
|
73
|
+
} else if (!quote && c === ' ') {
|
|
74
|
+
opts.push('');
|
|
75
|
+
} else {
|
|
76
|
+
opts[opts.length - 1] += c.replace(/\\(.)/, '$1');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
65
80
|
for (let i = 0; i < opts.length; i++) {
|
|
66
81
|
let opt = opts[i];
|
|
67
82
|
if (opt === '-r' || opt === '--require') {
|
|
@@ -91,10 +106,11 @@ export default class Worker extends EventEmitter {
|
|
|
91
106
|
args: [
|
|
92
107
|
forkModule,
|
|
93
108
|
{
|
|
94
|
-
|
|
109
|
+
shouldPatchConsole: !!this.options.shouldPatchConsole,
|
|
95
110
|
},
|
|
96
111
|
],
|
|
97
112
|
retries: 0,
|
|
113
|
+
skipReadyCheck: true,
|
|
98
114
|
resolve,
|
|
99
115
|
reject,
|
|
100
116
|
});
|
|
@@ -118,14 +134,15 @@ export default class Worker extends EventEmitter {
|
|
|
118
134
|
this.emit('ready');
|
|
119
135
|
}
|
|
120
136
|
|
|
121
|
-
sendSharedReference(ref:
|
|
122
|
-
new Promise((resolve, reject) => {
|
|
137
|
+
sendSharedReference(ref: SharedReference, value: mixed): Promise<any> {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
123
139
|
this.call({
|
|
124
140
|
method: 'createSharedReference',
|
|
125
141
|
args: [ref, value],
|
|
126
142
|
resolve,
|
|
127
143
|
reject,
|
|
128
144
|
retries: 0,
|
|
145
|
+
skipReadyCheck: true,
|
|
129
146
|
});
|
|
130
147
|
});
|
|
131
148
|
}
|
|
@@ -142,14 +159,20 @@ export default class Worker extends EventEmitter {
|
|
|
142
159
|
let idx = this.callId++;
|
|
143
160
|
this.calls.set(idx, call);
|
|
144
161
|
|
|
145
|
-
|
|
162
|
+
let msg = {
|
|
146
163
|
type: 'request',
|
|
147
164
|
idx: idx,
|
|
148
165
|
child: this.id,
|
|
149
166
|
handle: call.handle,
|
|
150
167
|
method: call.method,
|
|
151
168
|
args: call.args,
|
|
152
|
-
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
if (this.ready || call.skipReadyCheck === true) {
|
|
172
|
+
this.send(msg);
|
|
173
|
+
} else {
|
|
174
|
+
this.once('ready', () => this.send(msg));
|
|
175
|
+
}
|
|
153
176
|
}
|
|
154
177
|
|
|
155
178
|
receive(message: WorkerMessage): void {
|