-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
parallel-api.js
273 lines (229 loc) · 7.28 KB
/
parallel-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
const transpiler = require('@babel/core');
const path = require('path');
const workerpool = require('workerpool');
const Promise = require('rsvp').Promise;
const debugGenerator = require('heimdalljs-logger');
const JOBS = Number(process.env.JOBS) || require('os').cpus().length;
const loggerName = 'broccoli-persistent-filter:babel:parallel-api';
const _logger = debugGenerator(loggerName);
const babelCoreVersion = getBabelVersion();
// return the version of Babel that will be used by this plugin
function getBabelVersion() {
return require('@babel/core/package.json').version;
}
function getWorkerPool() {
let pool;
let globalPoolID = 'v2/broccoli-babel-transpiler/workerpool/babel-core-' + babelCoreVersion;
let existingPool = process[globalPoolID];
if (existingPool) {
pool = existingPool;
} else {
pool = workerpool.pool(path.join(__dirname, 'worker.js'), { workerType: 'auto', maxWorkers: JOBS });
process[globalPoolID] = pool;
}
return pool;
}
function implementsParallelAPI(object) {
const type = typeof object;
const hasProperties = type === 'function' || (type === 'object' && object !== null) || Array.isArray(object);
return hasProperties &&
object._parallelBabel !== null &&
typeof object._parallelBabel === 'object' &&
typeof object._parallelBabel.requireFile === 'string';
}
function isSerializable(value) {
if (value === null) {
return true;
} else if (implementsParallelAPI(value)) {
return true;
} else if (Array.isArray(value)) {
return value.every(isSerializable);
} else {
switch (typeof value) {
case 'string':
case 'number':
case 'boolean': return true;
case 'object': return Object.keys(value).every(key => isSerializable(value[key]));
default: return false;
}
}
}
function pluginsAreParallelizable(plugins) {
const errors = [];
let isParallelizable = true;
if (Array.isArray(plugins)) {
for (let i = 0; i < plugins.length; i++) {
let plugin = plugins[i];
if (isSerializable(plugin) === false) {
isParallelizable = false;
errors.push(humanizePlugin(plugin));
}
}
}
return {
isParallelizable,
errors
};
}
function callbacksAreParallelizable(babelOptions) {
const callbacks = Object.keys(babelOptions).filter(key => typeof babelOptions[key] === 'function');
let isParallelizable = true;
const errors = [];
for (let i = 0; i < callbacks.length; i++) {
let callback = babelOptions[callbacks[i]];
if (implementsParallelAPI(callback) === false) {
isParallelizable = false;
errors.push(humanizePlugin(callback))
}
}
return { isParallelizable, errors };
}
function transformIsParallelizable(babelOptions) {
const plugins = pluginsAreParallelizable(babelOptions.plugins);
const callbacks = callbacksAreParallelizable(babelOptions);
return {
isParallelizable: isSerializable(babelOptions),
errors: [].concat(plugins.errors, callbacks.errors)
};
}
function humanizePlugin(plugin) {
let name, baseDir;
if (Array.isArray(plugin) && typeof plugin[0] === 'function') {
plugin = plugin[0];
}
if (typeof plugin === 'function' || typeof plugin === 'object' && plugin !== null) {
if (typeof plugin.name === 'string') {
name = plugin.name;
}
if (typeof plugin.baseDir === 'function') {
baseDir = plugin.baseDir();
}
}
const output = `name: ${name || 'unknown'}, location: ${baseDir || 'unknown'}`;
if (!name && typeof plugin === 'function') {
return output + `,\n↓ function source ↓ \n${plugin.toString().substr(0, 200)}\n \n`;
} else {
return output;
}
}
function buildFromParallelApiInfo(parallelApiInfo, cacheKey) {
let requiredStuff = require(parallelApiInfo.requireFile);
if (parallelApiInfo.useMethod) {
if (requiredStuff[parallelApiInfo.useMethod] === undefined) {
throw new Error("method '" + parallelApiInfo.useMethod + "' does not exist in file " + parallelApiInfo.requireFile);
}
return requiredStuff[parallelApiInfo.useMethod];
}
if (parallelApiInfo.buildUsing) {
if (typeof requiredStuff[parallelApiInfo.buildUsing] !== 'function') {
throw new Error("'" + parallelApiInfo.buildUsing + "' is not a function in file " + parallelApiInfo.requireFile);
}
return requiredStuff[parallelApiInfo.buildUsing](parallelApiInfo.params, cacheKey);
}
return requiredStuff;
}
function deserialize(data, cacheKey) {
if (data.cacheKey) {
cacheKey = data.cacheKey;
}
if (data.babel) {
data = data.babel;
}
if (Array.isArray(data)) {
const result = [];
for (let i =0; i < data.length; i++) {
let member = data[i];
if (implementsParallelAPI(member)) {
result[i] = buildFromParallelApiInfo(member._parallelBabel, cacheKey);
} else {
result[i] = deserialize(member, cacheKey);
}
}
return result;
} else if (typeof data === 'object' && data !== null) {
const result = {};
Object.keys(data).forEach(key => {
let value = data[key];
if (value === undefined || value == null) {
} else if (implementsParallelAPI(value)) {
value = buildFromParallelApiInfo(value._parallelBabel, cacheKey);
} else {
value = deserialize(value, cacheKey);
}
result[key] = value;
});
return result;
} else {
return data;
}
}
const visited = new WeakSet();
// replace callback functions with objects so they can be transferred to the worker processes
function serialize(options) {
let optionsType = typeof options;
if ((optionsType === 'object' && options !== null) ||
optionsType === 'function' ||
Array.isArray(options)) {
if (visited.has(options)) {
// a cycle, so simply reuse
return options;
} else {
if (implementsParallelAPI(options)) {
options = {
_parallelBabel: options._parallelBabel
};
}
visited.add(options);
}
} else {
return options;
}
const serialized = Array.isArray(options) ? [] : {};
Object.keys(options).forEach(key => {
let value = options[key];
if (value === options) {
throw new TypeError('Babel Plugins contain a cycle');
}
if (isSerializable(value)) {
value = serialize(value);
} else {
// leave as is
}
serialized[key] = value;
});
return serialized;
}
function transformString(string, options, buildOptions) {
const isParallelizable = transformIsParallelizable(options.babel).isParallelizable;
if (JOBS > 1 && isParallelizable) {
let pool = getWorkerPool();
_logger.info('transformString is parallelizable');
let serializedObj = { babel : serialize(options.babel), 'cacheKey': options.cacheKey };
return pool.exec('transform', [string, serializedObj]);
} else {
if (JOBS <= 1) {
_logger.info('JOBS <= 1, skipping worker, using main thread');
} else {
_logger.info('transformString is NOT parallelizable');
}
return new Promise(resolve => {
resolve(transpiler.transform(string, deserialize(options)));
});
}
}
module.exports = {
jobs: JOBS,
getBabelVersion,
implementsParallelAPI,
isSerializable,
pluginsAreParallelizable,
callbacksAreParallelizable,
transformIsParallelizable,
deserialize,
serialize,
buildFromParallelApiInfo,
transformString,
humanizePlugin,
getWorkerPool
};