@parcel/workers 2.0.0-beta.3 → 2.0.0-dev.1510

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.
@@ -0,0 +1,85 @@
1
+ // @flow
2
+
3
+ import type {
4
+ WorkerImpl,
5
+ MessageHandler,
6
+ ErrorHandler,
7
+ ExitHandler,
8
+ WorkerMessage,
9
+ } from '../types';
10
+ import {prepareForSerialization, restoreDeserializedObject} from '@parcel/core';
11
+ import {makeDeferredWithPromise} from '@parcel/utils';
12
+
13
+ let id = 0;
14
+
15
+ export default class WebWorker implements WorkerImpl {
16
+ execArgv: Object;
17
+ onMessage: MessageHandler;
18
+ onError: ErrorHandler;
19
+ onExit: ExitHandler;
20
+ worker: Worker;
21
+ stopping: ?Promise<void>;
22
+
23
+ constructor(
24
+ execArgv: Object,
25
+ onMessage: MessageHandler,
26
+ onError: ErrorHandler,
27
+ onExit: ExitHandler,
28
+ ) {
29
+ this.execArgv = execArgv;
30
+ this.onMessage = onMessage;
31
+ this.onError = onError;
32
+ this.onExit = onExit;
33
+ }
34
+
35
+ start(): Promise<void> {
36
+ // $FlowFixMe[incompatible-call]
37
+ this.worker = new Worker(new URL('./WebChild.js', import.meta.url), {
38
+ name: `Parcel Worker ${id++}`,
39
+ type: 'module',
40
+ });
41
+
42
+ let {deferred, promise} = makeDeferredWithPromise();
43
+
44
+ this.worker.onmessage = ({data}) => {
45
+ if (data === 'online') {
46
+ deferred.resolve();
47
+ return;
48
+ }
49
+
50
+ // $FlowFixMe assume WorkerMessage as data
51
+ this.handleMessage(data);
52
+ };
53
+ this.worker.onerror = this.onError;
54
+ // Web workers can't crash or intentionally stop on their own, apart from stop() below
55
+ // this.worker.on('exit', this.onExit);
56
+
57
+ return promise;
58
+ }
59
+
60
+ stop(): Promise<void> {
61
+ if (!this.stopping) {
62
+ this.stopping = (async () => {
63
+ this.worker.postMessage('stop');
64
+ let {deferred, promise} = makeDeferredWithPromise();
65
+ this.worker.addEventListener('message', ({data}: MessageEvent) => {
66
+ if (data === 'stopped') {
67
+ deferred.resolve();
68
+ }
69
+ });
70
+ await promise;
71
+ this.worker.terminate();
72
+ this.onExit(0);
73
+ })();
74
+ }
75
+ return this.stopping;
76
+ }
77
+
78
+ handleMessage(data: WorkerMessage) {
79
+ this.onMessage(restoreDeserializedObject(data));
80
+ }
81
+
82
+ send(data: WorkerMessage) {
83
+ this.worker.postMessage(prepareForSerialization(data));
84
+ }
85
+ }
@@ -3,7 +3,7 @@ import os from 'os';
3
3
 
4
4
  import getCores, {detectRealCores} from '../src/cpuCount';
5
5
 
6
- describe('cpuCount', function() {
6
+ describe('cpuCount', function () {
7
7
  it('Should be able to detect real cpu count', () => {
8
8
  // Windows not supported as getting the cpu count takes a couple seconds...
9
9
  if (os.platform() === 'win32') return;
@@ -2,7 +2,7 @@ import Logger from '@parcel/logger';
2
2
  import assert from 'assert';
3
3
  import WorkerFarm from '../src';
4
4
 
5
- describe('WorkerFarm', function() {
5
+ describe('WorkerFarm', function () {
6
6
  this.timeout(30000);
7
7
 
8
8
  it('Should start up workers', async () => {
package/lib/Profiler.js DELETED
@@ -1,86 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- function _assert() {
9
- const data = _interopRequireDefault(require("assert"));
10
-
11
- _assert = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _diagnostic() {
19
- const data = _interopRequireDefault(require("@parcel/diagnostic"));
20
-
21
- _diagnostic = function () {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
-
30
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
31
-
32
- class Profiler {
33
- constructor() {
34
- _defineProperty(this, "session", void 0);
35
- }
36
-
37
- startProfiling() {
38
- let inspector;
39
-
40
- try {
41
- inspector = require('inspector');
42
- } catch (err) {
43
- throw new (_diagnostic().default)({
44
- diagnostic: {
45
- message: `The inspector module isn't available`,
46
- origin: '@parcel/workers',
47
- hints: ['Disable build profiling']
48
- }
49
- });
50
- }
51
-
52
- this.session = new inspector.Session();
53
- this.session.connect();
54
- return Promise.all([this.sendCommand('Profiler.setSamplingInterval', {
55
- interval: 100
56
- }), this.sendCommand('Profiler.enable'), this.sendCommand('Profiler.start')]);
57
- }
58
-
59
- sendCommand(method, params) {
60
- (0, _assert().default)(this.session != null);
61
- return new Promise((resolve, reject) => {
62
- this.session.post(method, params, (err, params) => {
63
- if (err == null) {
64
- resolve(params);
65
- } else {
66
- reject(err);
67
- }
68
- });
69
- });
70
- }
71
-
72
- destroy() {
73
- if (this.session != null) {
74
- this.session.disconnect();
75
- }
76
- }
77
-
78
- async stopProfiling() {
79
- let res = await this.sendCommand('Profiler.stop');
80
- this.destroy();
81
- return res.profile;
82
- }
83
-
84
- }
85
-
86
- exports.default = Profiler;
package/lib/Trace.js DELETED
@@ -1,139 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- function _chromeTraceEvent() {
9
- const data = require("chrome-trace-event");
10
-
11
- _chromeTraceEvent = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
-
20
- class Trace {
21
- constructor() {
22
- _defineProperty(this, "tracer", void 0);
23
-
24
- _defineProperty(this, "tid", void 0);
25
-
26
- _defineProperty(this, "eventId", void 0);
27
-
28
- this.tracer = new (_chromeTraceEvent().Tracer)();
29
- this.tid = 0;
30
- this.eventId = 0;
31
- }
32
-
33
- getEventId() {
34
- return this.eventId++;
35
- }
36
-
37
- init(ts) {
38
- this.tracer.instantEvent({
39
- name: 'TracingStartedInPage',
40
- id: this.getEventId(),
41
- ts,
42
- cat: ['disabled-by-default-devtools.timeline'],
43
- args: {
44
- data: {
45
- sessionId: '-1',
46
- page: '0xfff',
47
- frames: [{
48
- frame: '0xfff',
49
- url: 'parcel',
50
- name: ''
51
- }]
52
- }
53
- }
54
- });
55
- this.tracer.instantEvent({
56
- name: 'TracingStartedInBrowser',
57
- id: this.getEventId(),
58
- ts,
59
- cat: ['disabled-by-default-devtools.timeline'],
60
- args: {
61
- data: {
62
- sessionId: '-1'
63
- }
64
- }
65
- });
66
- }
67
-
68
- addCPUProfile(name, profile) {
69
- if (this.eventId === 0) {
70
- this.init(profile.startTime);
71
- }
72
-
73
- const trace = this.tracer;
74
- const tid = this.tid;
75
- this.tid++;
76
- const cpuStartTime = profile.startTime;
77
- const cpuEndTime = profile.endTime;
78
- trace.instantEvent({
79
- tid,
80
- id: this.getEventId(),
81
- cat: ['toplevel'],
82
- name: 'TaskQueueManager::ProcessTaskFromWorkQueue',
83
- args: {
84
- src_file: '../../ipc/ipc_moji_bootstrap.cc',
85
- src_func: 'Accept'
86
- },
87
- ts: cpuStartTime
88
- });
89
- trace.completeEvent({
90
- tid,
91
- name: 'EvaluateScript',
92
- id: this.getEventId(),
93
- cat: ['devtools.timeline'],
94
- ts: cpuStartTime,
95
- dur: cpuEndTime - cpuStartTime,
96
- args: {
97
- data: {
98
- url: 'parcel',
99
- lineNumber: 1,
100
- columnNumber: 1,
101
- frame: '0xFFF'
102
- }
103
- }
104
- });
105
- trace.instantEvent({
106
- tid,
107
- ts: 0,
108
- ph: 'M',
109
- cat: ['__metadata'],
110
- name: 'thread_name',
111
- args: {
112
- name
113
- }
114
- });
115
- trace.instantEvent({
116
- tid,
117
- name: 'CpuProfile',
118
- id: this.getEventId(),
119
- cat: ['disabled-by-default-devtools.timeline'],
120
- ts: cpuEndTime,
121
- args: {
122
- data: {
123
- cpuProfile: profile
124
- }
125
- }
126
- });
127
- }
128
-
129
- pipe(writable) {
130
- return this.tracer.pipe(writable);
131
- }
132
-
133
- flush() {
134
- this.tracer.push(null);
135
- }
136
-
137
- }
138
-
139
- exports.default = Trace;
package/src/Profiler.js DELETED
@@ -1,93 +0,0 @@
1
- // @flow
2
- import type {Session} from 'inspector';
3
- import invariant from 'assert';
4
- import ThrowableDiagnostic from '@parcel/diagnostic';
5
-
6
- // https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-Profile
7
- export type Profile = {|
8
- nodes: Array<ProfileNode>,
9
- startTime: number,
10
- endTime: number,
11
- samples?: Array<number>,
12
- timeDeltas?: Array<number>,
13
- |};
14
-
15
- // https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ProfileNode
16
- type ProfileNode = {|
17
- id: number,
18
- callFrame: CallFrame,
19
- hitCount?: number,
20
- children?: Array<number>,
21
- deoptReason?: string,
22
- positionTicks?: PositionTickInfo,
23
- |};
24
-
25
- // https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-CallFrame
26
- type CallFrame = {|
27
- functionName: string,
28
- scriptId: string,
29
- url: string,
30
- lineNumber: string,
31
- columnNumber: string,
32
- |};
33
-
34
- // https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-PositionTickInfo
35
- type PositionTickInfo = {|
36
- line: number,
37
- ticks: number,
38
- |};
39
-
40
- export default class Profiler {
41
- session: Session;
42
-
43
- startProfiling(): Promise<mixed> {
44
- let inspector;
45
- try {
46
- inspector = require('inspector');
47
- } catch (err) {
48
- throw new ThrowableDiagnostic({
49
- diagnostic: {
50
- message: `The inspector module isn't available`,
51
- origin: '@parcel/workers',
52
- hints: ['Disable build profiling'],
53
- },
54
- });
55
- }
56
-
57
- this.session = new inspector.Session();
58
- this.session.connect();
59
-
60
- return Promise.all([
61
- this.sendCommand('Profiler.setSamplingInterval', {
62
- interval: 100,
63
- }),
64
- this.sendCommand('Profiler.enable'),
65
- this.sendCommand('Profiler.start'),
66
- ]);
67
- }
68
-
69
- sendCommand(method: string, params: mixed): Promise<{profile: Profile, ...}> {
70
- invariant(this.session != null);
71
- return new Promise((resolve, reject) => {
72
- this.session.post(method, params, (err, params) => {
73
- if (err == null) {
74
- resolve(params);
75
- } else {
76
- reject(err);
77
- }
78
- });
79
- });
80
- }
81
-
82
- destroy() {
83
- if (this.session != null) {
84
- this.session.disconnect();
85
- }
86
- }
87
-
88
- async stopProfiling(): Promise<Profile> {
89
- let res = await this.sendCommand('Profiler.stop');
90
- this.destroy();
91
- return res.profile;
92
- }
93
- }
package/src/Trace.js DELETED
@@ -1,125 +0,0 @@
1
- // @flow
2
- import type {Profile} from './Profiler';
3
- import type {Writable} from 'stream';
4
- import {Tracer} from 'chrome-trace-event';
5
-
6
- export default class Trace {
7
- tracer: Tracer;
8
- tid: number;
9
- eventId: number;
10
-
11
- constructor() {
12
- this.tracer = new Tracer();
13
- this.tid = 0;
14
- this.eventId = 0;
15
- }
16
-
17
- getEventId(): number {
18
- return this.eventId++;
19
- }
20
-
21
- init(ts: number) {
22
- this.tracer.instantEvent({
23
- name: 'TracingStartedInPage',
24
- id: this.getEventId(),
25
- ts,
26
- cat: ['disabled-by-default-devtools.timeline'],
27
- args: {
28
- data: {
29
- sessionId: '-1',
30
- page: '0xfff',
31
- frames: [
32
- {
33
- frame: '0xfff',
34
- url: 'parcel',
35
- name: '',
36
- },
37
- ],
38
- },
39
- },
40
- });
41
-
42
- this.tracer.instantEvent({
43
- name: 'TracingStartedInBrowser',
44
- id: this.getEventId(),
45
- ts,
46
- cat: ['disabled-by-default-devtools.timeline'],
47
- args: {
48
- data: {
49
- sessionId: '-1',
50
- },
51
- },
52
- });
53
- }
54
-
55
- addCPUProfile(name: string, profile: Profile) {
56
- if (this.eventId === 0) {
57
- this.init(profile.startTime);
58
- }
59
- const trace = this.tracer;
60
- const tid = this.tid;
61
- this.tid++;
62
-
63
- const cpuStartTime = profile.startTime;
64
- const cpuEndTime = profile.endTime;
65
-
66
- trace.instantEvent({
67
- tid,
68
- id: this.getEventId(),
69
- cat: ['toplevel'],
70
- name: 'TaskQueueManager::ProcessTaskFromWorkQueue',
71
- args: {
72
- src_file: '../../ipc/ipc_moji_bootstrap.cc',
73
- src_func: 'Accept',
74
- },
75
- ts: cpuStartTime,
76
- });
77
-
78
- trace.completeEvent({
79
- tid,
80
- name: 'EvaluateScript',
81
- id: this.getEventId(),
82
- cat: ['devtools.timeline'],
83
- ts: cpuStartTime,
84
- dur: cpuEndTime - cpuStartTime,
85
- args: {
86
- data: {
87
- url: 'parcel',
88
- lineNumber: 1,
89
- columnNumber: 1,
90
- frame: '0xFFF',
91
- },
92
- },
93
- });
94
-
95
- trace.instantEvent({
96
- tid,
97
- ts: 0,
98
- ph: 'M',
99
- cat: ['__metadata'],
100
- name: 'thread_name',
101
- args: {name},
102
- });
103
-
104
- trace.instantEvent({
105
- tid,
106
- name: 'CpuProfile',
107
- id: this.getEventId(),
108
- cat: ['disabled-by-default-devtools.timeline'],
109
- ts: cpuEndTime,
110
- args: {
111
- data: {
112
- cpuProfile: profile,
113
- },
114
- },
115
- });
116
- }
117
-
118
- pipe(writable: Writable): stream$Writable {
119
- return this.tracer.pipe(writable);
120
- }
121
-
122
- flush() {
123
- this.tracer.push(null);
124
- }
125
- }