@parcel/workers 2.0.0-nightly.97 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- 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 +96 -26
- package/lib/WorkerFarm.js +197 -95
- package/lib/backend.js +6 -0
- package/lib/bus.js +10 -2
- package/lib/child.js +114 -63
- package/lib/cpuCount.js +27 -6
- package/lib/index.js +24 -8
- 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 +74 -11
- package/src/WorkerFarm.js +150 -40
- package/src/backend.js +6 -0
- package/src/bus.js +1 -1
- package/src/child.js +71 -20
- package/src/cpuCount.js +9 -4
- 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 +6 -6
@@ -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.
|
3
|
+
"version": "2.1.0",
|
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.
|
20
|
-
"@parcel/logger": "2.
|
21
|
-
"@parcel/
|
24
|
+
"@parcel/diagnostic": "^2.1.0",
|
25
|
+
"@parcel/logger": "^2.1.0",
|
26
|
+
"@parcel/types": "^2.1.0",
|
27
|
+
"@parcel/utils": "^2.1.0",
|
22
28
|
"chrome-trace-event": "^1.0.2",
|
23
29
|
"nullthrows": "^1.1.1"
|
24
30
|
},
|
25
31
|
"peerDependencies": {
|
26
|
-
"@parcel/core": "^2.
|
32
|
+
"@parcel/core": "^2.1.0"
|
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": "5a80fbe4af8797d0aab248a66f5254f42f00e83e"
|
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,7 +22,8 @@ export type WorkerCall = {|
|
|
19
22
|
type WorkerOpts = {|
|
20
23
|
forcedKillTime: number,
|
21
24
|
backend: BackendType,
|
22
|
-
|
25
|
+
shouldPatchConsole?: boolean,
|
26
|
+
sharedReferences: $ReadOnlyMap<SharedReference, mixed>,
|
23
27
|
|};
|
24
28
|
|
25
29
|
let WORKER_ID = 0;
|
@@ -27,14 +31,15 @@ export default class Worker extends EventEmitter {
|
|
27
31
|
+options: WorkerOpts;
|
28
32
|
worker: WorkerImpl;
|
29
33
|
id: number = WORKER_ID++;
|
34
|
+
sharedReferences: $ReadOnlyMap<SharedReference, mixed> = new Map();
|
30
35
|
|
31
36
|
calls: Map<number, WorkerCall> = new Map();
|
32
|
-
exitCode = null;
|
33
|
-
callId = 0;
|
37
|
+
exitCode: ?number = null;
|
38
|
+
callId: number = 0;
|
34
39
|
|
35
|
-
ready = false;
|
36
|
-
stopped = false;
|
37
|
-
isStopping = false;
|
40
|
+
ready: boolean = false;
|
41
|
+
stopped: boolean = false;
|
42
|
+
isStopping: boolean = false;
|
38
43
|
|
39
44
|
constructor(options: WorkerOpts) {
|
40
45
|
super();
|
@@ -43,7 +48,7 @@ export default class Worker extends EventEmitter {
|
|
43
48
|
|
44
49
|
async fork(forkModule: FilePath) {
|
45
50
|
let filteredArgs = process.execArgv.filter(
|
46
|
-
v => !/^--(debug|inspect)/.test(v),
|
51
|
+
v => !/^--(debug|inspect|max-old-space-size=)/.test(v),
|
47
52
|
);
|
48
53
|
|
49
54
|
for (let i = 0; i < filteredArgs.length; i++) {
|
@@ -57,6 +62,30 @@ export default class Worker extends EventEmitter {
|
|
57
62
|
}
|
58
63
|
}
|
59
64
|
|
65
|
+
// Workaround for https://github.com/nodejs/node/issues/29117
|
66
|
+
if (process.env.NODE_OPTIONS) {
|
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
|
+
|
80
|
+
for (let i = 0; i < opts.length; i++) {
|
81
|
+
let opt = opts[i];
|
82
|
+
if (opt === '-r' || opt === '--require') {
|
83
|
+
filteredArgs.push(opt, opts[i + 1]);
|
84
|
+
i++;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
60
89
|
let onMessage = data => this.receive(data);
|
61
90
|
let onExit = code => {
|
62
91
|
this.exitCode = code;
|
@@ -77,19 +106,47 @@ export default class Worker extends EventEmitter {
|
|
77
106
|
args: [
|
78
107
|
forkModule,
|
79
108
|
{
|
80
|
-
|
109
|
+
shouldPatchConsole: !!this.options.shouldPatchConsole,
|
81
110
|
},
|
82
111
|
],
|
83
112
|
retries: 0,
|
113
|
+
skipReadyCheck: true,
|
84
114
|
resolve,
|
85
115
|
reject,
|
86
116
|
});
|
87
117
|
});
|
88
118
|
|
119
|
+
let sharedRefs = this.options.sharedReferences;
|
120
|
+
let refsShared = new Set();
|
121
|
+
// in case more refs are created while initial refs are sending
|
122
|
+
while (refsShared.size < sharedRefs.size) {
|
123
|
+
await Promise.all(
|
124
|
+
[...sharedRefs]
|
125
|
+
.filter(([ref]) => !refsShared.has(ref))
|
126
|
+
.map(async ([ref, value]) => {
|
127
|
+
await this.sendSharedReference(ref, value);
|
128
|
+
refsShared.add(ref);
|
129
|
+
}),
|
130
|
+
);
|
131
|
+
}
|
132
|
+
|
89
133
|
this.ready = true;
|
90
134
|
this.emit('ready');
|
91
135
|
}
|
92
136
|
|
137
|
+
sendSharedReference(ref: SharedReference, value: mixed): Promise<any> {
|
138
|
+
return new Promise((resolve, reject) => {
|
139
|
+
this.call({
|
140
|
+
method: 'createSharedReference',
|
141
|
+
args: [ref, value],
|
142
|
+
resolve,
|
143
|
+
reject,
|
144
|
+
retries: 0,
|
145
|
+
skipReadyCheck: true,
|
146
|
+
});
|
147
|
+
});
|
148
|
+
}
|
149
|
+
|
93
150
|
send(data: WorkerMessage): void {
|
94
151
|
this.worker.send(data);
|
95
152
|
}
|
@@ -102,14 +159,20 @@ export default class Worker extends EventEmitter {
|
|
102
159
|
let idx = this.callId++;
|
103
160
|
this.calls.set(idx, call);
|
104
161
|
|
105
|
-
|
162
|
+
let msg = {
|
106
163
|
type: 'request',
|
107
164
|
idx: idx,
|
108
165
|
child: this.id,
|
109
166
|
handle: call.handle,
|
110
167
|
method: call.method,
|
111
168
|
args: call.args,
|
112
|
-
}
|
169
|
+
};
|
170
|
+
|
171
|
+
if (this.ready || call.skipReadyCheck === true) {
|
172
|
+
this.send(msg);
|
173
|
+
} else {
|
174
|
+
this.once('ready', () => this.send(msg));
|
175
|
+
}
|
113
176
|
}
|
114
177
|
|
115
178
|
receive(message: WorkerMessage): void {
|