piral-cli 0.15.0-beta.4672 → 0.15.0-beta.4699

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.
@@ -1,144 +0,0 @@
1
- const {errorUtils} = require('@parcel/utils');
2
-
3
- class Child {
4
- constructor() {
5
- if (!process.send) {
6
- throw new Error('Only create Child instances in a worker!');
7
- }
8
-
9
- this.module = undefined;
10
- this.childId = undefined;
11
-
12
- this.callQueue = [];
13
- this.responseQueue = new Map();
14
- this.responseId = 0;
15
- this.maxConcurrentCalls = 10;
16
- }
17
-
18
- messageListener(data) {
19
- if (data === 'die') {
20
- return this.end();
21
- }
22
-
23
- let type = data.type;
24
- if (type === 'response') {
25
- return this.handleResponse(data);
26
- } else if (type === 'request') {
27
- return this.handleRequest(data);
28
- }
29
- }
30
-
31
- async send(data) {
32
- process.send(data, err => {
33
- if (err && err instanceof Error) {
34
- if (err.code === 'ERR_IPC_CHANNEL_CLOSED') {
35
- // IPC connection closed
36
- // no need to keep the worker running if it can't send or receive data
37
- return this.end();
38
- }
39
- }
40
- });
41
- }
42
-
43
- childInit(module, childId) {
44
- this.module = require(module);
45
- this.childId = childId;
46
- }
47
-
48
- async handleRequest(data) {
49
- let idx = data.idx;
50
- let child = data.child;
51
- let method = data.method;
52
- let args = data.args;
53
-
54
- let result = {idx, child, type: 'response'};
55
- try {
56
- result.contentType = 'data';
57
- if (method === 'childInit') {
58
- result.content = this.childInit(...args, child);
59
- } else {
60
- result.content = await this.module[method](...args);
61
- }
62
- } catch (e) {
63
- result.contentType = 'error';
64
- result.content = errorUtils.errorToJson(e);
65
- }
66
-
67
- this.send(result);
68
- }
69
-
70
- async handleResponse(data) {
71
- let idx = data.idx;
72
- let contentType = data.contentType;
73
- let content = data.content;
74
- let call = this.responseQueue.get(idx);
75
-
76
- if (contentType === 'error') {
77
- call.reject(errorUtils.jsonToError(content));
78
- } else {
79
- call.resolve(content);
80
- }
81
-
82
- this.responseQueue.delete(idx);
83
-
84
- // Process the next call
85
- this.processQueue();
86
- }
87
-
88
- // Keep in mind to make sure responses to these calls are JSON.Stringify safe
89
- async addCall(request, awaitResponse = true) {
90
- let call = request;
91
- call.type = 'request';
92
- call.child = this.childId;
93
- call.awaitResponse = awaitResponse;
94
-
95
- let promise;
96
- if (awaitResponse) {
97
- promise = new Promise((resolve, reject) => {
98
- call.resolve = resolve;
99
- call.reject = reject;
100
- });
101
- }
102
-
103
- this.callQueue.push(call);
104
- this.processQueue();
105
-
106
- return promise;
107
- }
108
-
109
- async sendRequest(call) {
110
- let idx;
111
- if (call.awaitResponse) {
112
- idx = this.responseId++;
113
- this.responseQueue.set(idx, call);
114
- }
115
- this.send({
116
- idx: idx,
117
- child: call.child,
118
- type: call.type,
119
- location: call.location,
120
- method: call.method,
121
- args: call.args,
122
- awaitResponse: call.awaitResponse
123
- });
124
- }
125
-
126
- async processQueue() {
127
- if (!this.callQueue.length) {
128
- return;
129
- }
130
-
131
- if (this.responseQueue.size < this.maxConcurrentCalls) {
132
- this.sendRequest(this.callQueue.shift());
133
- }
134
- }
135
-
136
- end() {
137
- process.exit();
138
- }
139
- }
140
-
141
- let child = new Child();
142
- process.on('message', child.messageListener.bind(child));
143
-
144
- module.exports = child;
Binary file