isomorfeus-asset-manager 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +12 -0
- data/lib/iso_opal.rb +10 -0
- data/lib/isomorfeus/asset_manager/asset.rb +67 -0
- data/lib/isomorfeus/asset_manager/config.rb +69 -0
- data/lib/isomorfeus/asset_manager/js_import.rb +59 -0
- data/lib/isomorfeus/asset_manager/portfolio.rb +46 -0
- data/lib/isomorfeus/asset_manager/rack_middleware.rb +85 -0
- data/lib/isomorfeus/asset_manager/ruby_import.rb +33 -0
- data/lib/isomorfeus/asset_manager/server_socket_processor.rb +55 -0
- data/lib/isomorfeus/asset_manager/version.rb +5 -0
- data/lib/isomorfeus/asset_manager/view_helper.rb +9 -0
- data/lib/isomorfeus/asset_manager.rb +145 -0
- data/lib/isomorfeus-asset-manager.rb +27 -0
- data/node_modules/.bin/esbuild +12 -0
- data/node_modules/.bin/esbuild.cmd +17 -0
- data/node_modules/.bin/esbuild.ps1 +28 -0
- data/node_modules/.package-lock.json +18 -0
- data/node_modules/esbuild-wasm/README.md +3 -0
- data/node_modules/esbuild-wasm/bin/esbuild +84 -0
- data/node_modules/esbuild-wasm/esbuild.wasm +0 -0
- data/node_modules/esbuild-wasm/esm/browser.d.ts +397 -0
- data/node_modules/esbuild-wasm/esm/browser.js +2341 -0
- data/node_modules/esbuild-wasm/esm/browser.min.js +8 -0
- data/node_modules/esbuild-wasm/exit0.js +11 -0
- data/node_modules/esbuild-wasm/lib/browser.d.ts +397 -0
- data/node_modules/esbuild-wasm/lib/browser.js +2371 -0
- data/node_modules/esbuild-wasm/lib/browser.min.js +10 -0
- data/node_modules/esbuild-wasm/lib/main.d.ts +397 -0
- data/node_modules/esbuild-wasm/lib/main.js +1948 -0
- data/node_modules/esbuild-wasm/package.json +16 -0
- data/node_modules/esbuild-wasm/wasm_exec.js +654 -0
- data/package.json +9 -0
- metadata +162 -0
@@ -0,0 +1,2341 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __defProps = Object.defineProperties;
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
8
|
+
var __spreadValues = (a, b) => {
|
9
|
+
for (var prop in b || (b = {}))
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
12
|
+
if (__getOwnPropSymbols)
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
14
|
+
if (__propIsEnum.call(b, prop))
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
16
|
+
}
|
17
|
+
return a;
|
18
|
+
};
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
20
|
+
|
21
|
+
// lib/shared/stdio_protocol.ts
|
22
|
+
function encodePacket(packet) {
|
23
|
+
let visit = (value) => {
|
24
|
+
if (value === null) {
|
25
|
+
bb.write8(0);
|
26
|
+
} else if (typeof value === "boolean") {
|
27
|
+
bb.write8(1);
|
28
|
+
bb.write8(+value);
|
29
|
+
} else if (typeof value === "number") {
|
30
|
+
bb.write8(2);
|
31
|
+
bb.write32(value | 0);
|
32
|
+
} else if (typeof value === "string") {
|
33
|
+
bb.write8(3);
|
34
|
+
bb.write(encodeUTF8(value));
|
35
|
+
} else if (value instanceof Uint8Array) {
|
36
|
+
bb.write8(4);
|
37
|
+
bb.write(value);
|
38
|
+
} else if (value instanceof Array) {
|
39
|
+
bb.write8(5);
|
40
|
+
bb.write32(value.length);
|
41
|
+
for (let item of value) {
|
42
|
+
visit(item);
|
43
|
+
}
|
44
|
+
} else {
|
45
|
+
let keys = Object.keys(value);
|
46
|
+
bb.write8(6);
|
47
|
+
bb.write32(keys.length);
|
48
|
+
for (let key of keys) {
|
49
|
+
bb.write(encodeUTF8(key));
|
50
|
+
visit(value[key]);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
};
|
54
|
+
let bb = new ByteBuffer();
|
55
|
+
bb.write32(0);
|
56
|
+
bb.write32(packet.id << 1 | +!packet.isRequest);
|
57
|
+
visit(packet.value);
|
58
|
+
writeUInt32LE(bb.buf, bb.len - 4, 0);
|
59
|
+
return bb.buf.subarray(0, bb.len);
|
60
|
+
}
|
61
|
+
function decodePacket(bytes) {
|
62
|
+
let visit = () => {
|
63
|
+
switch (bb.read8()) {
|
64
|
+
case 0:
|
65
|
+
return null;
|
66
|
+
case 1:
|
67
|
+
return !!bb.read8();
|
68
|
+
case 2:
|
69
|
+
return bb.read32();
|
70
|
+
case 3:
|
71
|
+
return decodeUTF8(bb.read());
|
72
|
+
case 4:
|
73
|
+
return bb.read();
|
74
|
+
case 5: {
|
75
|
+
let count = bb.read32();
|
76
|
+
let value2 = [];
|
77
|
+
for (let i = 0; i < count; i++) {
|
78
|
+
value2.push(visit());
|
79
|
+
}
|
80
|
+
return value2;
|
81
|
+
}
|
82
|
+
case 6: {
|
83
|
+
let count = bb.read32();
|
84
|
+
let value2 = {};
|
85
|
+
for (let i = 0; i < count; i++) {
|
86
|
+
value2[decodeUTF8(bb.read())] = visit();
|
87
|
+
}
|
88
|
+
return value2;
|
89
|
+
}
|
90
|
+
default:
|
91
|
+
throw new Error("Invalid packet");
|
92
|
+
}
|
93
|
+
};
|
94
|
+
let bb = new ByteBuffer(bytes);
|
95
|
+
let id = bb.read32();
|
96
|
+
let isRequest = (id & 1) === 0;
|
97
|
+
id >>>= 1;
|
98
|
+
let value = visit();
|
99
|
+
if (bb.ptr !== bytes.length) {
|
100
|
+
throw new Error("Invalid packet");
|
101
|
+
}
|
102
|
+
return { id, isRequest, value };
|
103
|
+
}
|
104
|
+
var ByteBuffer = class {
|
105
|
+
constructor(buf = new Uint8Array(1024)) {
|
106
|
+
this.buf = buf;
|
107
|
+
this.len = 0;
|
108
|
+
this.ptr = 0;
|
109
|
+
}
|
110
|
+
_write(delta) {
|
111
|
+
if (this.len + delta > this.buf.length) {
|
112
|
+
let clone = new Uint8Array((this.len + delta) * 2);
|
113
|
+
clone.set(this.buf);
|
114
|
+
this.buf = clone;
|
115
|
+
}
|
116
|
+
this.len += delta;
|
117
|
+
return this.len - delta;
|
118
|
+
}
|
119
|
+
write8(value) {
|
120
|
+
let offset = this._write(1);
|
121
|
+
this.buf[offset] = value;
|
122
|
+
}
|
123
|
+
write32(value) {
|
124
|
+
let offset = this._write(4);
|
125
|
+
writeUInt32LE(this.buf, value, offset);
|
126
|
+
}
|
127
|
+
write(bytes) {
|
128
|
+
let offset = this._write(4 + bytes.length);
|
129
|
+
writeUInt32LE(this.buf, bytes.length, offset);
|
130
|
+
this.buf.set(bytes, offset + 4);
|
131
|
+
}
|
132
|
+
_read(delta) {
|
133
|
+
if (this.ptr + delta > this.buf.length) {
|
134
|
+
throw new Error("Invalid packet");
|
135
|
+
}
|
136
|
+
this.ptr += delta;
|
137
|
+
return this.ptr - delta;
|
138
|
+
}
|
139
|
+
read8() {
|
140
|
+
return this.buf[this._read(1)];
|
141
|
+
}
|
142
|
+
read32() {
|
143
|
+
return readUInt32LE(this.buf, this._read(4));
|
144
|
+
}
|
145
|
+
read() {
|
146
|
+
let length = this.read32();
|
147
|
+
let bytes = new Uint8Array(length);
|
148
|
+
let ptr = this._read(bytes.length);
|
149
|
+
bytes.set(this.buf.subarray(ptr, ptr + length));
|
150
|
+
return bytes;
|
151
|
+
}
|
152
|
+
};
|
153
|
+
var encodeUTF8;
|
154
|
+
var decodeUTF8;
|
155
|
+
if (typeof TextEncoder !== "undefined" && typeof TextDecoder !== "undefined") {
|
156
|
+
let encoder = new TextEncoder();
|
157
|
+
let decoder = new TextDecoder();
|
158
|
+
encodeUTF8 = (text) => encoder.encode(text);
|
159
|
+
decodeUTF8 = (bytes) => decoder.decode(bytes);
|
160
|
+
} else if (typeof Buffer !== "undefined") {
|
161
|
+
encodeUTF8 = (text) => {
|
162
|
+
let buffer = Buffer.from(text);
|
163
|
+
if (!(buffer instanceof Uint8Array)) {
|
164
|
+
buffer = new Uint8Array(buffer);
|
165
|
+
}
|
166
|
+
return buffer;
|
167
|
+
};
|
168
|
+
decodeUTF8 = (bytes) => {
|
169
|
+
let { buffer, byteOffset, byteLength } = bytes;
|
170
|
+
return Buffer.from(buffer, byteOffset, byteLength).toString();
|
171
|
+
};
|
172
|
+
} else {
|
173
|
+
throw new Error("No UTF-8 codec found");
|
174
|
+
}
|
175
|
+
function readUInt32LE(buffer, offset) {
|
176
|
+
return buffer[offset++] | buffer[offset++] << 8 | buffer[offset++] << 16 | buffer[offset++] << 24;
|
177
|
+
}
|
178
|
+
function writeUInt32LE(buffer, value, offset) {
|
179
|
+
buffer[offset++] = value;
|
180
|
+
buffer[offset++] = value >> 8;
|
181
|
+
buffer[offset++] = value >> 16;
|
182
|
+
buffer[offset++] = value >> 24;
|
183
|
+
}
|
184
|
+
|
185
|
+
// lib/shared/common.ts
|
186
|
+
function validateTarget(target) {
|
187
|
+
target += "";
|
188
|
+
if (target.indexOf(",") >= 0)
|
189
|
+
throw new Error(`Invalid target: ${target}`);
|
190
|
+
return target;
|
191
|
+
}
|
192
|
+
var canBeAnything = () => null;
|
193
|
+
var mustBeBoolean = (value) => typeof value === "boolean" ? null : "a boolean";
|
194
|
+
var mustBeBooleanOrObject = (value) => typeof value === "boolean" || typeof value === "object" && !Array.isArray(value) ? null : "a boolean or an object";
|
195
|
+
var mustBeString = (value) => typeof value === "string" ? null : "a string";
|
196
|
+
var mustBeRegExp = (value) => value instanceof RegExp ? null : "a RegExp object";
|
197
|
+
var mustBeInteger = (value) => typeof value === "number" && value === (value | 0) ? null : "an integer";
|
198
|
+
var mustBeFunction = (value) => typeof value === "function" ? null : "a function";
|
199
|
+
var mustBeArray = (value) => Array.isArray(value) ? null : "an array";
|
200
|
+
var mustBeObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? null : "an object";
|
201
|
+
var mustBeArrayOrRecord = (value) => typeof value === "object" && value !== null ? null : "an array or an object";
|
202
|
+
var mustBeObjectOrNull = (value) => typeof value === "object" && !Array.isArray(value) ? null : "an object or null";
|
203
|
+
var mustBeStringOrBoolean = (value) => typeof value === "string" || typeof value === "boolean" ? null : "a string or a boolean";
|
204
|
+
var mustBeStringOrObject = (value) => typeof value === "string" || typeof value === "object" && value !== null && !Array.isArray(value) ? null : "a string or an object";
|
205
|
+
var mustBeStringOrArray = (value) => typeof value === "string" || Array.isArray(value) ? null : "a string or an array";
|
206
|
+
var mustBeStringOrUint8Array = (value) => typeof value === "string" || value instanceof Uint8Array ? null : "a string or a Uint8Array";
|
207
|
+
function getFlag(object, keys, key, mustBeFn) {
|
208
|
+
let value = object[key];
|
209
|
+
keys[key + ""] = true;
|
210
|
+
if (value === void 0)
|
211
|
+
return void 0;
|
212
|
+
let mustBe = mustBeFn(value);
|
213
|
+
if (mustBe !== null)
|
214
|
+
throw new Error(`"${key}" must be ${mustBe}`);
|
215
|
+
return value;
|
216
|
+
}
|
217
|
+
function checkForInvalidFlags(object, keys, where) {
|
218
|
+
for (let key in object) {
|
219
|
+
if (!(key in keys)) {
|
220
|
+
throw new Error(`Invalid option ${where}: "${key}"`);
|
221
|
+
}
|
222
|
+
}
|
223
|
+
}
|
224
|
+
function validateInitializeOptions(options) {
|
225
|
+
let keys = Object.create(null);
|
226
|
+
let wasmURL = getFlag(options, keys, "wasmURL", mustBeString);
|
227
|
+
let worker = getFlag(options, keys, "worker", mustBeBoolean);
|
228
|
+
checkForInvalidFlags(options, keys, "in startService() call");
|
229
|
+
return {
|
230
|
+
wasmURL,
|
231
|
+
worker
|
232
|
+
};
|
233
|
+
}
|
234
|
+
function pushLogFlags(flags, options, keys, isTTY, logLevelDefault) {
|
235
|
+
let color = getFlag(options, keys, "color", mustBeBoolean);
|
236
|
+
let logLevel = getFlag(options, keys, "logLevel", mustBeString);
|
237
|
+
let logLimit = getFlag(options, keys, "logLimit", mustBeInteger);
|
238
|
+
if (color !== void 0)
|
239
|
+
flags.push(`--color=${color}`);
|
240
|
+
else if (isTTY)
|
241
|
+
flags.push(`--color=true`);
|
242
|
+
flags.push(`--log-level=${logLevel || logLevelDefault}`);
|
243
|
+
flags.push(`--log-limit=${logLimit || 0}`);
|
244
|
+
}
|
245
|
+
function pushCommonFlags(flags, options, keys) {
|
246
|
+
let legalComments = getFlag(options, keys, "legalComments", mustBeString);
|
247
|
+
let sourceRoot = getFlag(options, keys, "sourceRoot", mustBeString);
|
248
|
+
let sourcesContent = getFlag(options, keys, "sourcesContent", mustBeBoolean);
|
249
|
+
let target = getFlag(options, keys, "target", mustBeStringOrArray);
|
250
|
+
let format = getFlag(options, keys, "format", mustBeString);
|
251
|
+
let globalName = getFlag(options, keys, "globalName", mustBeString);
|
252
|
+
let minify = getFlag(options, keys, "minify", mustBeBoolean);
|
253
|
+
let minifySyntax = getFlag(options, keys, "minifySyntax", mustBeBoolean);
|
254
|
+
let minifyWhitespace = getFlag(options, keys, "minifyWhitespace", mustBeBoolean);
|
255
|
+
let minifyIdentifiers = getFlag(options, keys, "minifyIdentifiers", mustBeBoolean);
|
256
|
+
let charset = getFlag(options, keys, "charset", mustBeString);
|
257
|
+
let treeShaking = getFlag(options, keys, "treeShaking", mustBeStringOrBoolean);
|
258
|
+
let jsx = getFlag(options, keys, "jsx", mustBeString);
|
259
|
+
let jsxFactory = getFlag(options, keys, "jsxFactory", mustBeString);
|
260
|
+
let jsxFragment = getFlag(options, keys, "jsxFragment", mustBeString);
|
261
|
+
let define = getFlag(options, keys, "define", mustBeObject);
|
262
|
+
let pure = getFlag(options, keys, "pure", mustBeArray);
|
263
|
+
let keepNames = getFlag(options, keys, "keepNames", mustBeBoolean);
|
264
|
+
if (legalComments)
|
265
|
+
flags.push(`--legal-comments=${legalComments}`);
|
266
|
+
if (sourceRoot !== void 0)
|
267
|
+
flags.push(`--source-root=${sourceRoot}`);
|
268
|
+
if (sourcesContent !== void 0)
|
269
|
+
flags.push(`--sources-content=${sourcesContent}`);
|
270
|
+
if (target) {
|
271
|
+
if (Array.isArray(target))
|
272
|
+
flags.push(`--target=${Array.from(target).map(validateTarget).join(",")}`);
|
273
|
+
else
|
274
|
+
flags.push(`--target=${validateTarget(target)}`);
|
275
|
+
}
|
276
|
+
if (format)
|
277
|
+
flags.push(`--format=${format}`);
|
278
|
+
if (globalName)
|
279
|
+
flags.push(`--global-name=${globalName}`);
|
280
|
+
if (minify)
|
281
|
+
flags.push("--minify");
|
282
|
+
if (minifySyntax)
|
283
|
+
flags.push("--minify-syntax");
|
284
|
+
if (minifyWhitespace)
|
285
|
+
flags.push("--minify-whitespace");
|
286
|
+
if (minifyIdentifiers)
|
287
|
+
flags.push("--minify-identifiers");
|
288
|
+
if (charset)
|
289
|
+
flags.push(`--charset=${charset}`);
|
290
|
+
if (treeShaking !== void 0 && treeShaking !== true)
|
291
|
+
flags.push(`--tree-shaking=${treeShaking}`);
|
292
|
+
if (jsx)
|
293
|
+
flags.push(`--jsx=${jsx}`);
|
294
|
+
if (jsxFactory)
|
295
|
+
flags.push(`--jsx-factory=${jsxFactory}`);
|
296
|
+
if (jsxFragment)
|
297
|
+
flags.push(`--jsx-fragment=${jsxFragment}`);
|
298
|
+
if (define) {
|
299
|
+
for (let key in define) {
|
300
|
+
if (key.indexOf("=") >= 0)
|
301
|
+
throw new Error(`Invalid define: ${key}`);
|
302
|
+
flags.push(`--define:${key}=${define[key]}`);
|
303
|
+
}
|
304
|
+
}
|
305
|
+
if (pure)
|
306
|
+
for (let fn of pure)
|
307
|
+
flags.push(`--pure:${fn}`);
|
308
|
+
if (keepNames)
|
309
|
+
flags.push(`--keep-names`);
|
310
|
+
}
|
311
|
+
function flagsForBuildOptions(callName, options, isTTY, logLevelDefault, writeDefault) {
|
312
|
+
var _a;
|
313
|
+
let flags = [];
|
314
|
+
let entries = [];
|
315
|
+
let keys = Object.create(null);
|
316
|
+
let stdinContents = null;
|
317
|
+
let stdinResolveDir = null;
|
318
|
+
let watchMode = null;
|
319
|
+
pushLogFlags(flags, options, keys, isTTY, logLevelDefault);
|
320
|
+
pushCommonFlags(flags, options, keys);
|
321
|
+
let sourcemap = getFlag(options, keys, "sourcemap", mustBeStringOrBoolean);
|
322
|
+
let bundle = getFlag(options, keys, "bundle", mustBeBoolean);
|
323
|
+
let watch = getFlag(options, keys, "watch", mustBeBooleanOrObject);
|
324
|
+
let splitting = getFlag(options, keys, "splitting", mustBeBoolean);
|
325
|
+
let preserveSymlinks = getFlag(options, keys, "preserveSymlinks", mustBeBoolean);
|
326
|
+
let metafile = getFlag(options, keys, "metafile", mustBeBoolean);
|
327
|
+
let outfile = getFlag(options, keys, "outfile", mustBeString);
|
328
|
+
let outdir = getFlag(options, keys, "outdir", mustBeString);
|
329
|
+
let outbase = getFlag(options, keys, "outbase", mustBeString);
|
330
|
+
let platform = getFlag(options, keys, "platform", mustBeString);
|
331
|
+
let tsconfig = getFlag(options, keys, "tsconfig", mustBeString);
|
332
|
+
let resolveExtensions = getFlag(options, keys, "resolveExtensions", mustBeArray);
|
333
|
+
let nodePathsInput = getFlag(options, keys, "nodePaths", mustBeArray);
|
334
|
+
let mainFields = getFlag(options, keys, "mainFields", mustBeArray);
|
335
|
+
let conditions = getFlag(options, keys, "conditions", mustBeArray);
|
336
|
+
let external = getFlag(options, keys, "external", mustBeArray);
|
337
|
+
let loader = getFlag(options, keys, "loader", mustBeObject);
|
338
|
+
let outExtension = getFlag(options, keys, "outExtension", mustBeObject);
|
339
|
+
let publicPath = getFlag(options, keys, "publicPath", mustBeString);
|
340
|
+
let entryNames = getFlag(options, keys, "entryNames", mustBeString);
|
341
|
+
let chunkNames = getFlag(options, keys, "chunkNames", mustBeString);
|
342
|
+
let assetNames = getFlag(options, keys, "assetNames", mustBeString);
|
343
|
+
let inject = getFlag(options, keys, "inject", mustBeArray);
|
344
|
+
let banner = getFlag(options, keys, "banner", mustBeObject);
|
345
|
+
let footer = getFlag(options, keys, "footer", mustBeObject);
|
346
|
+
let entryPoints = getFlag(options, keys, "entryPoints", mustBeArrayOrRecord);
|
347
|
+
let absWorkingDir = getFlag(options, keys, "absWorkingDir", mustBeString);
|
348
|
+
let stdin = getFlag(options, keys, "stdin", mustBeObject);
|
349
|
+
let write = (_a = getFlag(options, keys, "write", mustBeBoolean)) != null ? _a : writeDefault;
|
350
|
+
let allowOverwrite = getFlag(options, keys, "allowOverwrite", mustBeBoolean);
|
351
|
+
let incremental = getFlag(options, keys, "incremental", mustBeBoolean) === true;
|
352
|
+
keys.plugins = true;
|
353
|
+
checkForInvalidFlags(options, keys, `in ${callName}() call`);
|
354
|
+
if (sourcemap)
|
355
|
+
flags.push(`--sourcemap${sourcemap === true ? "" : `=${sourcemap}`}`);
|
356
|
+
if (bundle)
|
357
|
+
flags.push("--bundle");
|
358
|
+
if (allowOverwrite)
|
359
|
+
flags.push("--allow-overwrite");
|
360
|
+
if (watch) {
|
361
|
+
flags.push("--watch");
|
362
|
+
if (typeof watch === "boolean") {
|
363
|
+
watchMode = {};
|
364
|
+
} else {
|
365
|
+
let watchKeys = Object.create(null);
|
366
|
+
let onRebuild = getFlag(watch, watchKeys, "onRebuild", mustBeFunction);
|
367
|
+
checkForInvalidFlags(watch, watchKeys, `on "watch" in ${callName}() call`);
|
368
|
+
watchMode = { onRebuild };
|
369
|
+
}
|
370
|
+
}
|
371
|
+
if (splitting)
|
372
|
+
flags.push("--splitting");
|
373
|
+
if (preserveSymlinks)
|
374
|
+
flags.push("--preserve-symlinks");
|
375
|
+
if (metafile)
|
376
|
+
flags.push(`--metafile`);
|
377
|
+
if (outfile)
|
378
|
+
flags.push(`--outfile=${outfile}`);
|
379
|
+
if (outdir)
|
380
|
+
flags.push(`--outdir=${outdir}`);
|
381
|
+
if (outbase)
|
382
|
+
flags.push(`--outbase=${outbase}`);
|
383
|
+
if (platform)
|
384
|
+
flags.push(`--platform=${platform}`);
|
385
|
+
if (tsconfig)
|
386
|
+
flags.push(`--tsconfig=${tsconfig}`);
|
387
|
+
if (resolveExtensions) {
|
388
|
+
let values = [];
|
389
|
+
for (let value of resolveExtensions) {
|
390
|
+
value += "";
|
391
|
+
if (value.indexOf(",") >= 0)
|
392
|
+
throw new Error(`Invalid resolve extension: ${value}`);
|
393
|
+
values.push(value);
|
394
|
+
}
|
395
|
+
flags.push(`--resolve-extensions=${values.join(",")}`);
|
396
|
+
}
|
397
|
+
if (publicPath)
|
398
|
+
flags.push(`--public-path=${publicPath}`);
|
399
|
+
if (entryNames)
|
400
|
+
flags.push(`--entry-names=${entryNames}`);
|
401
|
+
if (chunkNames)
|
402
|
+
flags.push(`--chunk-names=${chunkNames}`);
|
403
|
+
if (assetNames)
|
404
|
+
flags.push(`--asset-names=${assetNames}`);
|
405
|
+
if (mainFields) {
|
406
|
+
let values = [];
|
407
|
+
for (let value of mainFields) {
|
408
|
+
value += "";
|
409
|
+
if (value.indexOf(",") >= 0)
|
410
|
+
throw new Error(`Invalid main field: ${value}`);
|
411
|
+
values.push(value);
|
412
|
+
}
|
413
|
+
flags.push(`--main-fields=${values.join(",")}`);
|
414
|
+
}
|
415
|
+
if (conditions) {
|
416
|
+
let values = [];
|
417
|
+
for (let value of conditions) {
|
418
|
+
value += "";
|
419
|
+
if (value.indexOf(",") >= 0)
|
420
|
+
throw new Error(`Invalid condition: ${value}`);
|
421
|
+
values.push(value);
|
422
|
+
}
|
423
|
+
flags.push(`--conditions=${values.join(",")}`);
|
424
|
+
}
|
425
|
+
if (external)
|
426
|
+
for (let name of external)
|
427
|
+
flags.push(`--external:${name}`);
|
428
|
+
if (banner) {
|
429
|
+
for (let type in banner) {
|
430
|
+
if (type.indexOf("=") >= 0)
|
431
|
+
throw new Error(`Invalid banner file type: ${type}`);
|
432
|
+
flags.push(`--banner:${type}=${banner[type]}`);
|
433
|
+
}
|
434
|
+
}
|
435
|
+
if (footer) {
|
436
|
+
for (let type in footer) {
|
437
|
+
if (type.indexOf("=") >= 0)
|
438
|
+
throw new Error(`Invalid footer file type: ${type}`);
|
439
|
+
flags.push(`--footer:${type}=${footer[type]}`);
|
440
|
+
}
|
441
|
+
}
|
442
|
+
if (inject)
|
443
|
+
for (let path of inject)
|
444
|
+
flags.push(`--inject:${path}`);
|
445
|
+
if (loader) {
|
446
|
+
for (let ext in loader) {
|
447
|
+
if (ext.indexOf("=") >= 0)
|
448
|
+
throw new Error(`Invalid loader extension: ${ext}`);
|
449
|
+
flags.push(`--loader:${ext}=${loader[ext]}`);
|
450
|
+
}
|
451
|
+
}
|
452
|
+
if (outExtension) {
|
453
|
+
for (let ext in outExtension) {
|
454
|
+
if (ext.indexOf("=") >= 0)
|
455
|
+
throw new Error(`Invalid out extension: ${ext}`);
|
456
|
+
flags.push(`--out-extension:${ext}=${outExtension[ext]}`);
|
457
|
+
}
|
458
|
+
}
|
459
|
+
if (entryPoints) {
|
460
|
+
if (Array.isArray(entryPoints)) {
|
461
|
+
for (let entryPoint of entryPoints) {
|
462
|
+
entries.push(["", entryPoint + ""]);
|
463
|
+
}
|
464
|
+
} else {
|
465
|
+
for (let [key, value] of Object.entries(entryPoints)) {
|
466
|
+
entries.push([key + "", value + ""]);
|
467
|
+
}
|
468
|
+
}
|
469
|
+
}
|
470
|
+
if (stdin) {
|
471
|
+
let stdinKeys = Object.create(null);
|
472
|
+
let contents = getFlag(stdin, stdinKeys, "contents", mustBeString);
|
473
|
+
let resolveDir = getFlag(stdin, stdinKeys, "resolveDir", mustBeString);
|
474
|
+
let sourcefile = getFlag(stdin, stdinKeys, "sourcefile", mustBeString);
|
475
|
+
let loader2 = getFlag(stdin, stdinKeys, "loader", mustBeString);
|
476
|
+
checkForInvalidFlags(stdin, stdinKeys, 'in "stdin" object');
|
477
|
+
if (sourcefile)
|
478
|
+
flags.push(`--sourcefile=${sourcefile}`);
|
479
|
+
if (loader2)
|
480
|
+
flags.push(`--loader=${loader2}`);
|
481
|
+
if (resolveDir)
|
482
|
+
stdinResolveDir = resolveDir + "";
|
483
|
+
stdinContents = contents ? contents + "" : "";
|
484
|
+
}
|
485
|
+
let nodePaths = [];
|
486
|
+
if (nodePathsInput) {
|
487
|
+
for (let value of nodePathsInput) {
|
488
|
+
value += "";
|
489
|
+
nodePaths.push(value);
|
490
|
+
}
|
491
|
+
}
|
492
|
+
return {
|
493
|
+
entries,
|
494
|
+
flags,
|
495
|
+
write,
|
496
|
+
stdinContents,
|
497
|
+
stdinResolveDir,
|
498
|
+
absWorkingDir,
|
499
|
+
incremental,
|
500
|
+
nodePaths,
|
501
|
+
watch: watchMode
|
502
|
+
};
|
503
|
+
}
|
504
|
+
function flagsForTransformOptions(callName, options, isTTY, logLevelDefault) {
|
505
|
+
let flags = [];
|
506
|
+
let keys = Object.create(null);
|
507
|
+
pushLogFlags(flags, options, keys, isTTY, logLevelDefault);
|
508
|
+
pushCommonFlags(flags, options, keys);
|
509
|
+
let sourcemap = getFlag(options, keys, "sourcemap", mustBeStringOrBoolean);
|
510
|
+
let tsconfigRaw = getFlag(options, keys, "tsconfigRaw", mustBeStringOrObject);
|
511
|
+
let sourcefile = getFlag(options, keys, "sourcefile", mustBeString);
|
512
|
+
let loader = getFlag(options, keys, "loader", mustBeString);
|
513
|
+
let banner = getFlag(options, keys, "banner", mustBeString);
|
514
|
+
let footer = getFlag(options, keys, "footer", mustBeString);
|
515
|
+
checkForInvalidFlags(options, keys, `in ${callName}() call`);
|
516
|
+
if (sourcemap)
|
517
|
+
flags.push(`--sourcemap=${sourcemap === true ? "external" : sourcemap}`);
|
518
|
+
if (tsconfigRaw)
|
519
|
+
flags.push(`--tsconfig-raw=${typeof tsconfigRaw === "string" ? tsconfigRaw : JSON.stringify(tsconfigRaw)}`);
|
520
|
+
if (sourcefile)
|
521
|
+
flags.push(`--sourcefile=${sourcefile}`);
|
522
|
+
if (loader)
|
523
|
+
flags.push(`--loader=${loader}`);
|
524
|
+
if (banner)
|
525
|
+
flags.push(`--banner=${banner}`);
|
526
|
+
if (footer)
|
527
|
+
flags.push(`--footer=${footer}`);
|
528
|
+
return flags;
|
529
|
+
}
|
530
|
+
function createChannel(streamIn) {
|
531
|
+
let responseCallbacks = new Map();
|
532
|
+
let pluginCallbacks = new Map();
|
533
|
+
let watchCallbacks = new Map();
|
534
|
+
let serveCallbacks = new Map();
|
535
|
+
let nextServeID = 0;
|
536
|
+
let isClosed = false;
|
537
|
+
let nextRequestID = 0;
|
538
|
+
let nextBuildKey = 0;
|
539
|
+
let stdout = new Uint8Array(16 * 1024);
|
540
|
+
let stdoutUsed = 0;
|
541
|
+
let readFromStdout = (chunk) => {
|
542
|
+
let limit = stdoutUsed + chunk.length;
|
543
|
+
if (limit > stdout.length) {
|
544
|
+
let swap = new Uint8Array(limit * 2);
|
545
|
+
swap.set(stdout);
|
546
|
+
stdout = swap;
|
547
|
+
}
|
548
|
+
stdout.set(chunk, stdoutUsed);
|
549
|
+
stdoutUsed += chunk.length;
|
550
|
+
let offset = 0;
|
551
|
+
while (offset + 4 <= stdoutUsed) {
|
552
|
+
let length = readUInt32LE(stdout, offset);
|
553
|
+
if (offset + 4 + length > stdoutUsed) {
|
554
|
+
break;
|
555
|
+
}
|
556
|
+
offset += 4;
|
557
|
+
handleIncomingPacket(stdout.subarray(offset, offset + length));
|
558
|
+
offset += length;
|
559
|
+
}
|
560
|
+
if (offset > 0) {
|
561
|
+
stdout.copyWithin(0, offset, stdoutUsed);
|
562
|
+
stdoutUsed -= offset;
|
563
|
+
}
|
564
|
+
};
|
565
|
+
let afterClose = () => {
|
566
|
+
isClosed = true;
|
567
|
+
for (let callback of responseCallbacks.values()) {
|
568
|
+
callback("The service was stopped", null);
|
569
|
+
}
|
570
|
+
responseCallbacks.clear();
|
571
|
+
for (let callbacks of serveCallbacks.values()) {
|
572
|
+
callbacks.onWait("The service was stopped");
|
573
|
+
}
|
574
|
+
serveCallbacks.clear();
|
575
|
+
for (let callback of watchCallbacks.values()) {
|
576
|
+
try {
|
577
|
+
callback(new Error("The service was stopped"), null);
|
578
|
+
} catch (e) {
|
579
|
+
console.error(e);
|
580
|
+
}
|
581
|
+
}
|
582
|
+
watchCallbacks.clear();
|
583
|
+
};
|
584
|
+
let sendRequest = (refs, value, callback) => {
|
585
|
+
if (isClosed)
|
586
|
+
return callback("The service is no longer running", null);
|
587
|
+
let id = nextRequestID++;
|
588
|
+
responseCallbacks.set(id, (error, response) => {
|
589
|
+
try {
|
590
|
+
callback(error, response);
|
591
|
+
} finally {
|
592
|
+
if (refs)
|
593
|
+
refs.unref();
|
594
|
+
}
|
595
|
+
});
|
596
|
+
if (refs)
|
597
|
+
refs.ref();
|
598
|
+
streamIn.writeToStdin(encodePacket({ id, isRequest: true, value }));
|
599
|
+
};
|
600
|
+
let sendResponse = (id, value) => {
|
601
|
+
if (isClosed)
|
602
|
+
throw new Error("The service is no longer running");
|
603
|
+
streamIn.writeToStdin(encodePacket({ id, isRequest: false, value }));
|
604
|
+
};
|
605
|
+
let handleRequest = async (id, request) => {
|
606
|
+
try {
|
607
|
+
switch (request.command) {
|
608
|
+
case "ping": {
|
609
|
+
sendResponse(id, {});
|
610
|
+
break;
|
611
|
+
}
|
612
|
+
case "start": {
|
613
|
+
let callback = pluginCallbacks.get(request.key);
|
614
|
+
if (!callback)
|
615
|
+
sendResponse(id, {});
|
616
|
+
else
|
617
|
+
sendResponse(id, await callback(request));
|
618
|
+
break;
|
619
|
+
}
|
620
|
+
case "resolve": {
|
621
|
+
let callback = pluginCallbacks.get(request.key);
|
622
|
+
if (!callback)
|
623
|
+
sendResponse(id, {});
|
624
|
+
else
|
625
|
+
sendResponse(id, await callback(request));
|
626
|
+
break;
|
627
|
+
}
|
628
|
+
case "load": {
|
629
|
+
let callback = pluginCallbacks.get(request.key);
|
630
|
+
if (!callback)
|
631
|
+
sendResponse(id, {});
|
632
|
+
else
|
633
|
+
sendResponse(id, await callback(request));
|
634
|
+
break;
|
635
|
+
}
|
636
|
+
case "serve-request": {
|
637
|
+
let callbacks = serveCallbacks.get(request.serveID);
|
638
|
+
if (callbacks && callbacks.onRequest)
|
639
|
+
callbacks.onRequest(request.args);
|
640
|
+
sendResponse(id, {});
|
641
|
+
break;
|
642
|
+
}
|
643
|
+
case "serve-wait": {
|
644
|
+
let callbacks = serveCallbacks.get(request.serveID);
|
645
|
+
if (callbacks)
|
646
|
+
callbacks.onWait(request.error);
|
647
|
+
sendResponse(id, {});
|
648
|
+
break;
|
649
|
+
}
|
650
|
+
case "watch-rebuild": {
|
651
|
+
let callback = watchCallbacks.get(request.watchID);
|
652
|
+
try {
|
653
|
+
if (callback)
|
654
|
+
callback(null, request.args);
|
655
|
+
} catch (err) {
|
656
|
+
console.error(err);
|
657
|
+
}
|
658
|
+
sendResponse(id, {});
|
659
|
+
break;
|
660
|
+
}
|
661
|
+
default:
|
662
|
+
throw new Error(`Invalid command: ` + request.command);
|
663
|
+
}
|
664
|
+
} catch (e) {
|
665
|
+
sendResponse(id, { errors: [extractErrorMessageV8(e, streamIn, null, void 0, "")] });
|
666
|
+
}
|
667
|
+
};
|
668
|
+
let isFirstPacket = true;
|
669
|
+
let handleIncomingPacket = (bytes) => {
|
670
|
+
if (isFirstPacket) {
|
671
|
+
isFirstPacket = false;
|
672
|
+
let binaryVersion = String.fromCharCode(...bytes);
|
673
|
+
if (binaryVersion !== "0.12.25") {
|
674
|
+
throw new Error(`Cannot start service: Host version "${"0.12.25"}" does not match binary version ${JSON.stringify(binaryVersion)}`);
|
675
|
+
}
|
676
|
+
return;
|
677
|
+
}
|
678
|
+
let packet = decodePacket(bytes);
|
679
|
+
if (packet.isRequest) {
|
680
|
+
handleRequest(packet.id, packet.value);
|
681
|
+
} else {
|
682
|
+
let callback = responseCallbacks.get(packet.id);
|
683
|
+
responseCallbacks.delete(packet.id);
|
684
|
+
if (packet.value.error)
|
685
|
+
callback(packet.value.error, {});
|
686
|
+
else
|
687
|
+
callback(null, packet.value);
|
688
|
+
}
|
689
|
+
};
|
690
|
+
let handlePlugins = async (initialOptions, plugins, buildKey, stash) => {
|
691
|
+
let onStartCallbacks = [];
|
692
|
+
let onEndCallbacks = [];
|
693
|
+
let onResolveCallbacks = {};
|
694
|
+
let onLoadCallbacks = {};
|
695
|
+
let nextCallbackID = 0;
|
696
|
+
let i = 0;
|
697
|
+
let requestPlugins = [];
|
698
|
+
plugins = [...plugins];
|
699
|
+
for (let item of plugins) {
|
700
|
+
let keys = {};
|
701
|
+
if (typeof item !== "object")
|
702
|
+
throw new Error(`Plugin at index ${i} must be an object`);
|
703
|
+
let name = getFlag(item, keys, "name", mustBeString);
|
704
|
+
if (typeof name !== "string" || name === "")
|
705
|
+
throw new Error(`Plugin at index ${i} is missing a name`);
|
706
|
+
try {
|
707
|
+
let setup = getFlag(item, keys, "setup", mustBeFunction);
|
708
|
+
if (typeof setup !== "function")
|
709
|
+
throw new Error(`Plugin is missing a setup function`);
|
710
|
+
checkForInvalidFlags(item, keys, `on plugin ${JSON.stringify(name)}`);
|
711
|
+
let plugin = {
|
712
|
+
name,
|
713
|
+
onResolve: [],
|
714
|
+
onLoad: []
|
715
|
+
};
|
716
|
+
i++;
|
717
|
+
let promise = setup({
|
718
|
+
initialOptions,
|
719
|
+
onStart(callback2) {
|
720
|
+
let registeredText = `This error came from the "onStart" callback registered here`;
|
721
|
+
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, "onStart");
|
722
|
+
onStartCallbacks.push({ name, callback: callback2, note: registeredNote });
|
723
|
+
},
|
724
|
+
onEnd(callback2) {
|
725
|
+
let registeredText = `This error came from the "onEnd" callback registered here`;
|
726
|
+
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, "onEnd");
|
727
|
+
onEndCallbacks.push({ name, callback: callback2, note: registeredNote });
|
728
|
+
},
|
729
|
+
onResolve(options, callback2) {
|
730
|
+
let registeredText = `This error came from the "onResolve" callback registered here`;
|
731
|
+
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, "onResolve");
|
732
|
+
let keys2 = {};
|
733
|
+
let filter = getFlag(options, keys2, "filter", mustBeRegExp);
|
734
|
+
let namespace = getFlag(options, keys2, "namespace", mustBeString);
|
735
|
+
checkForInvalidFlags(options, keys2, `in onResolve() call for plugin ${JSON.stringify(name)}`);
|
736
|
+
if (filter == null)
|
737
|
+
throw new Error(`onResolve() call is missing a filter`);
|
738
|
+
let id = nextCallbackID++;
|
739
|
+
onResolveCallbacks[id] = { name, callback: callback2, note: registeredNote };
|
740
|
+
plugin.onResolve.push({ id, filter: filter.source, namespace: namespace || "" });
|
741
|
+
},
|
742
|
+
onLoad(options, callback2) {
|
743
|
+
let registeredText = `This error came from the "onLoad" callback registered here`;
|
744
|
+
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, "onLoad");
|
745
|
+
let keys2 = {};
|
746
|
+
let filter = getFlag(options, keys2, "filter", mustBeRegExp);
|
747
|
+
let namespace = getFlag(options, keys2, "namespace", mustBeString);
|
748
|
+
checkForInvalidFlags(options, keys2, `in onLoad() call for plugin ${JSON.stringify(name)}`);
|
749
|
+
if (filter == null)
|
750
|
+
throw new Error(`onLoad() call is missing a filter`);
|
751
|
+
let id = nextCallbackID++;
|
752
|
+
onLoadCallbacks[id] = { name, callback: callback2, note: registeredNote };
|
753
|
+
plugin.onLoad.push({ id, filter: filter.source, namespace: namespace || "" });
|
754
|
+
}
|
755
|
+
});
|
756
|
+
if (promise)
|
757
|
+
await promise;
|
758
|
+
requestPlugins.push(plugin);
|
759
|
+
} catch (e) {
|
760
|
+
return { ok: false, error: e, pluginName: name };
|
761
|
+
}
|
762
|
+
}
|
763
|
+
const callback = async (request) => {
|
764
|
+
switch (request.command) {
|
765
|
+
case "start": {
|
766
|
+
let response = { errors: [], warnings: [] };
|
767
|
+
await Promise.all(onStartCallbacks.map(async ({ name, callback: callback2, note }) => {
|
768
|
+
try {
|
769
|
+
let result = await callback2();
|
770
|
+
if (result != null) {
|
771
|
+
if (typeof result !== "object")
|
772
|
+
throw new Error(`Expected onStart() callback in plugin ${JSON.stringify(name)} to return an object`);
|
773
|
+
let keys = {};
|
774
|
+
let errors = getFlag(result, keys, "errors", mustBeArray);
|
775
|
+
let warnings = getFlag(result, keys, "warnings", mustBeArray);
|
776
|
+
checkForInvalidFlags(result, keys, `from onStart() callback in plugin ${JSON.stringify(name)}`);
|
777
|
+
if (errors != null)
|
778
|
+
response.errors.push(...sanitizeMessages(errors, "errors", stash, name));
|
779
|
+
if (warnings != null)
|
780
|
+
response.warnings.push(...sanitizeMessages(warnings, "warnings", stash, name));
|
781
|
+
}
|
782
|
+
} catch (e) {
|
783
|
+
response.errors.push(extractErrorMessageV8(e, streamIn, stash, note && note(), name));
|
784
|
+
}
|
785
|
+
}));
|
786
|
+
return response;
|
787
|
+
}
|
788
|
+
case "resolve": {
|
789
|
+
let response = {}, name = "", callback2, note;
|
790
|
+
for (let id of request.ids) {
|
791
|
+
try {
|
792
|
+
({ name, callback: callback2, note } = onResolveCallbacks[id]);
|
793
|
+
let result = await callback2({
|
794
|
+
path: request.path,
|
795
|
+
importer: request.importer,
|
796
|
+
namespace: request.namespace,
|
797
|
+
resolveDir: request.resolveDir,
|
798
|
+
kind: request.kind,
|
799
|
+
pluginData: stash.load(request.pluginData)
|
800
|
+
});
|
801
|
+
if (result != null) {
|
802
|
+
if (typeof result !== "object")
|
803
|
+
throw new Error(`Expected onResolve() callback in plugin ${JSON.stringify(name)} to return an object`);
|
804
|
+
let keys = {};
|
805
|
+
let pluginName = getFlag(result, keys, "pluginName", mustBeString);
|
806
|
+
let path = getFlag(result, keys, "path", mustBeString);
|
807
|
+
let namespace = getFlag(result, keys, "namespace", mustBeString);
|
808
|
+
let external = getFlag(result, keys, "external", mustBeBoolean);
|
809
|
+
let sideEffects = getFlag(result, keys, "sideEffects", mustBeBoolean);
|
810
|
+
let pluginData = getFlag(result, keys, "pluginData", canBeAnything);
|
811
|
+
let errors = getFlag(result, keys, "errors", mustBeArray);
|
812
|
+
let warnings = getFlag(result, keys, "warnings", mustBeArray);
|
813
|
+
let watchFiles = getFlag(result, keys, "watchFiles", mustBeArray);
|
814
|
+
let watchDirs = getFlag(result, keys, "watchDirs", mustBeArray);
|
815
|
+
checkForInvalidFlags(result, keys, `from onResolve() callback in plugin ${JSON.stringify(name)}`);
|
816
|
+
response.id = id;
|
817
|
+
if (pluginName != null)
|
818
|
+
response.pluginName = pluginName;
|
819
|
+
if (path != null)
|
820
|
+
response.path = path;
|
821
|
+
if (namespace != null)
|
822
|
+
response.namespace = namespace;
|
823
|
+
if (external != null)
|
824
|
+
response.external = external;
|
825
|
+
if (sideEffects != null)
|
826
|
+
response.sideEffects = sideEffects;
|
827
|
+
if (pluginData != null)
|
828
|
+
response.pluginData = stash.store(pluginData);
|
829
|
+
if (errors != null)
|
830
|
+
response.errors = sanitizeMessages(errors, "errors", stash, name);
|
831
|
+
if (warnings != null)
|
832
|
+
response.warnings = sanitizeMessages(warnings, "warnings", stash, name);
|
833
|
+
if (watchFiles != null)
|
834
|
+
response.watchFiles = sanitizeStringArray(watchFiles, "watchFiles");
|
835
|
+
if (watchDirs != null)
|
836
|
+
response.watchDirs = sanitizeStringArray(watchDirs, "watchDirs");
|
837
|
+
break;
|
838
|
+
}
|
839
|
+
} catch (e) {
|
840
|
+
return { id, errors: [extractErrorMessageV8(e, streamIn, stash, note && note(), name)] };
|
841
|
+
}
|
842
|
+
}
|
843
|
+
return response;
|
844
|
+
}
|
845
|
+
case "load": {
|
846
|
+
let response = {}, name = "", callback2, note;
|
847
|
+
for (let id of request.ids) {
|
848
|
+
try {
|
849
|
+
({ name, callback: callback2, note } = onLoadCallbacks[id]);
|
850
|
+
let result = await callback2({
|
851
|
+
path: request.path,
|
852
|
+
namespace: request.namespace,
|
853
|
+
pluginData: stash.load(request.pluginData)
|
854
|
+
});
|
855
|
+
if (result != null) {
|
856
|
+
if (typeof result !== "object")
|
857
|
+
throw new Error(`Expected onLoad() callback in plugin ${JSON.stringify(name)} to return an object`);
|
858
|
+
let keys = {};
|
859
|
+
let pluginName = getFlag(result, keys, "pluginName", mustBeString);
|
860
|
+
let contents = getFlag(result, keys, "contents", mustBeStringOrUint8Array);
|
861
|
+
let resolveDir = getFlag(result, keys, "resolveDir", mustBeString);
|
862
|
+
let pluginData = getFlag(result, keys, "pluginData", canBeAnything);
|
863
|
+
let loader = getFlag(result, keys, "loader", mustBeString);
|
864
|
+
let errors = getFlag(result, keys, "errors", mustBeArray);
|
865
|
+
let warnings = getFlag(result, keys, "warnings", mustBeArray);
|
866
|
+
let watchFiles = getFlag(result, keys, "watchFiles", mustBeArray);
|
867
|
+
let watchDirs = getFlag(result, keys, "watchDirs", mustBeArray);
|
868
|
+
checkForInvalidFlags(result, keys, `from onLoad() callback in plugin ${JSON.stringify(name)}`);
|
869
|
+
response.id = id;
|
870
|
+
if (pluginName != null)
|
871
|
+
response.pluginName = pluginName;
|
872
|
+
if (contents instanceof Uint8Array)
|
873
|
+
response.contents = contents;
|
874
|
+
else if (contents != null)
|
875
|
+
response.contents = encodeUTF8(contents);
|
876
|
+
if (resolveDir != null)
|
877
|
+
response.resolveDir = resolveDir;
|
878
|
+
if (pluginData != null)
|
879
|
+
response.pluginData = stash.store(pluginData);
|
880
|
+
if (loader != null)
|
881
|
+
response.loader = loader;
|
882
|
+
if (errors != null)
|
883
|
+
response.errors = sanitizeMessages(errors, "errors", stash, name);
|
884
|
+
if (warnings != null)
|
885
|
+
response.warnings = sanitizeMessages(warnings, "warnings", stash, name);
|
886
|
+
if (watchFiles != null)
|
887
|
+
response.watchFiles = sanitizeStringArray(watchFiles, "watchFiles");
|
888
|
+
if (watchDirs != null)
|
889
|
+
response.watchDirs = sanitizeStringArray(watchDirs, "watchDirs");
|
890
|
+
break;
|
891
|
+
}
|
892
|
+
} catch (e) {
|
893
|
+
return { id, errors: [extractErrorMessageV8(e, streamIn, stash, note && note(), name)] };
|
894
|
+
}
|
895
|
+
}
|
896
|
+
return response;
|
897
|
+
}
|
898
|
+
default:
|
899
|
+
throw new Error(`Invalid command: ` + request.command);
|
900
|
+
}
|
901
|
+
};
|
902
|
+
let runOnEndCallbacks = (result, logPluginError, done) => done();
|
903
|
+
if (onEndCallbacks.length > 0) {
|
904
|
+
runOnEndCallbacks = (result, logPluginError, done) => {
|
905
|
+
(async () => {
|
906
|
+
for (const { name, callback: callback2, note } of onEndCallbacks) {
|
907
|
+
try {
|
908
|
+
await callback2(result);
|
909
|
+
} catch (e) {
|
910
|
+
result.errors.push(await new Promise((resolve) => logPluginError(e, name, note && note(), resolve)));
|
911
|
+
}
|
912
|
+
}
|
913
|
+
})().then(done);
|
914
|
+
};
|
915
|
+
}
|
916
|
+
let refCount = 0;
|
917
|
+
return {
|
918
|
+
ok: true,
|
919
|
+
requestPlugins,
|
920
|
+
runOnEndCallbacks,
|
921
|
+
pluginRefs: {
|
922
|
+
ref() {
|
923
|
+
if (++refCount === 1)
|
924
|
+
pluginCallbacks.set(buildKey, callback);
|
925
|
+
},
|
926
|
+
unref() {
|
927
|
+
if (--refCount === 0)
|
928
|
+
pluginCallbacks.delete(buildKey);
|
929
|
+
}
|
930
|
+
}
|
931
|
+
};
|
932
|
+
};
|
933
|
+
let buildServeData = (refs, options, request) => {
|
934
|
+
let keys = {};
|
935
|
+
let port = getFlag(options, keys, "port", mustBeInteger);
|
936
|
+
let host = getFlag(options, keys, "host", mustBeString);
|
937
|
+
let servedir = getFlag(options, keys, "servedir", mustBeString);
|
938
|
+
let onRequest = getFlag(options, keys, "onRequest", mustBeFunction);
|
939
|
+
let serveID = nextServeID++;
|
940
|
+
let onWait;
|
941
|
+
let wait = new Promise((resolve, reject) => {
|
942
|
+
onWait = (error) => {
|
943
|
+
serveCallbacks.delete(serveID);
|
944
|
+
if (error !== null)
|
945
|
+
reject(new Error(error));
|
946
|
+
else
|
947
|
+
resolve();
|
948
|
+
};
|
949
|
+
});
|
950
|
+
request.serve = { serveID };
|
951
|
+
checkForInvalidFlags(options, keys, `in serve() call`);
|
952
|
+
if (port !== void 0)
|
953
|
+
request.serve.port = port;
|
954
|
+
if (host !== void 0)
|
955
|
+
request.serve.host = host;
|
956
|
+
if (servedir !== void 0)
|
957
|
+
request.serve.servedir = servedir;
|
958
|
+
serveCallbacks.set(serveID, {
|
959
|
+
onRequest,
|
960
|
+
onWait
|
961
|
+
});
|
962
|
+
return {
|
963
|
+
wait,
|
964
|
+
stop() {
|
965
|
+
sendRequest(refs, { command: "serve-stop", serveID }, () => {
|
966
|
+
});
|
967
|
+
}
|
968
|
+
};
|
969
|
+
};
|
970
|
+
const buildLogLevelDefault = "warning";
|
971
|
+
const transformLogLevelDefault = "silent";
|
972
|
+
let buildOrServe = (args) => {
|
973
|
+
let key = nextBuildKey++;
|
974
|
+
const details = createObjectStash();
|
975
|
+
let plugins;
|
976
|
+
let { refs, options, isTTY, callback } = args;
|
977
|
+
if (typeof options === "object") {
|
978
|
+
let value = options.plugins;
|
979
|
+
if (value !== void 0) {
|
980
|
+
if (!Array.isArray(value))
|
981
|
+
throw new Error(`"plugins" must be an array`);
|
982
|
+
plugins = value;
|
983
|
+
}
|
984
|
+
}
|
985
|
+
let logPluginError = (e, pluginName, note, done) => {
|
986
|
+
let flags = [];
|
987
|
+
try {
|
988
|
+
pushLogFlags(flags, options, {}, isTTY, buildLogLevelDefault);
|
989
|
+
} catch (e2) {
|
990
|
+
}
|
991
|
+
const message = extractErrorMessageV8(e, streamIn, details, note, pluginName);
|
992
|
+
sendRequest(refs, { command: "error", flags, error: message }, () => {
|
993
|
+
message.detail = details.load(message.detail);
|
994
|
+
done(message);
|
995
|
+
});
|
996
|
+
};
|
997
|
+
let handleError = (e, pluginName) => {
|
998
|
+
logPluginError(e, pluginName, void 0, (error) => {
|
999
|
+
callback(failureErrorWithLog("Build failed", [error], []), null);
|
1000
|
+
});
|
1001
|
+
};
|
1002
|
+
if (plugins && plugins.length > 0) {
|
1003
|
+
if (streamIn.isSync)
|
1004
|
+
return handleError(new Error("Cannot use plugins in synchronous API calls"), "");
|
1005
|
+
handlePlugins(options, plugins, key, details).then((result) => {
|
1006
|
+
if (!result.ok) {
|
1007
|
+
handleError(result.error, result.pluginName);
|
1008
|
+
} else {
|
1009
|
+
try {
|
1010
|
+
buildOrServeContinue(__spreadProps(__spreadValues({}, args), {
|
1011
|
+
key,
|
1012
|
+
details,
|
1013
|
+
logPluginError,
|
1014
|
+
requestPlugins: result.requestPlugins,
|
1015
|
+
runOnEndCallbacks: result.runOnEndCallbacks,
|
1016
|
+
pluginRefs: result.pluginRefs
|
1017
|
+
}));
|
1018
|
+
} catch (e) {
|
1019
|
+
handleError(e, "");
|
1020
|
+
}
|
1021
|
+
}
|
1022
|
+
}, (e) => handleError(e, ""));
|
1023
|
+
} else {
|
1024
|
+
try {
|
1025
|
+
buildOrServeContinue(__spreadProps(__spreadValues({}, args), {
|
1026
|
+
key,
|
1027
|
+
details,
|
1028
|
+
logPluginError,
|
1029
|
+
requestPlugins: null,
|
1030
|
+
runOnEndCallbacks: (result, logPluginError2, done) => done(),
|
1031
|
+
pluginRefs: null
|
1032
|
+
}));
|
1033
|
+
} catch (e) {
|
1034
|
+
handleError(e, "");
|
1035
|
+
}
|
1036
|
+
}
|
1037
|
+
};
|
1038
|
+
let buildOrServeContinue = ({
|
1039
|
+
callName,
|
1040
|
+
refs: callerRefs,
|
1041
|
+
serveOptions,
|
1042
|
+
options,
|
1043
|
+
isTTY,
|
1044
|
+
defaultWD,
|
1045
|
+
callback,
|
1046
|
+
key,
|
1047
|
+
details,
|
1048
|
+
logPluginError,
|
1049
|
+
requestPlugins,
|
1050
|
+
runOnEndCallbacks,
|
1051
|
+
pluginRefs
|
1052
|
+
}) => {
|
1053
|
+
const refs = {
|
1054
|
+
ref() {
|
1055
|
+
if (pluginRefs)
|
1056
|
+
pluginRefs.ref();
|
1057
|
+
if (callerRefs)
|
1058
|
+
callerRefs.ref();
|
1059
|
+
},
|
1060
|
+
unref() {
|
1061
|
+
if (pluginRefs)
|
1062
|
+
pluginRefs.unref();
|
1063
|
+
if (callerRefs)
|
1064
|
+
callerRefs.unref();
|
1065
|
+
}
|
1066
|
+
};
|
1067
|
+
let writeDefault = !streamIn.isBrowser;
|
1068
|
+
let {
|
1069
|
+
entries,
|
1070
|
+
flags,
|
1071
|
+
write,
|
1072
|
+
stdinContents,
|
1073
|
+
stdinResolveDir,
|
1074
|
+
absWorkingDir,
|
1075
|
+
incremental,
|
1076
|
+
nodePaths,
|
1077
|
+
watch
|
1078
|
+
} = flagsForBuildOptions(callName, options, isTTY, buildLogLevelDefault, writeDefault);
|
1079
|
+
let request = {
|
1080
|
+
command: "build",
|
1081
|
+
key,
|
1082
|
+
entries,
|
1083
|
+
flags,
|
1084
|
+
write,
|
1085
|
+
stdinContents,
|
1086
|
+
stdinResolveDir,
|
1087
|
+
absWorkingDir: absWorkingDir || defaultWD,
|
1088
|
+
incremental,
|
1089
|
+
nodePaths
|
1090
|
+
};
|
1091
|
+
if (requestPlugins)
|
1092
|
+
request.plugins = requestPlugins;
|
1093
|
+
let serve2 = serveOptions && buildServeData(refs, serveOptions, request);
|
1094
|
+
let rebuild;
|
1095
|
+
let stop;
|
1096
|
+
let copyResponseToResult = (response, result) => {
|
1097
|
+
if (response.outputFiles)
|
1098
|
+
result.outputFiles = response.outputFiles.map(convertOutputFiles);
|
1099
|
+
if (response.metafile)
|
1100
|
+
result.metafile = JSON.parse(response.metafile);
|
1101
|
+
if (response.writeToStdout !== void 0)
|
1102
|
+
console.log(decodeUTF8(response.writeToStdout).replace(/\n$/, ""));
|
1103
|
+
};
|
1104
|
+
let buildResponseToResult = (response, callback2) => {
|
1105
|
+
let result = {
|
1106
|
+
errors: replaceDetailsInMessages(response.errors, details),
|
1107
|
+
warnings: replaceDetailsInMessages(response.warnings, details)
|
1108
|
+
};
|
1109
|
+
copyResponseToResult(response, result);
|
1110
|
+
runOnEndCallbacks(result, logPluginError, () => {
|
1111
|
+
if (result.errors.length > 0) {
|
1112
|
+
return callback2(failureErrorWithLog("Build failed", result.errors, result.warnings), null);
|
1113
|
+
}
|
1114
|
+
if (response.rebuildID !== void 0) {
|
1115
|
+
if (!rebuild) {
|
1116
|
+
let isDisposed = false;
|
1117
|
+
rebuild = () => new Promise((resolve, reject) => {
|
1118
|
+
if (isDisposed || isClosed)
|
1119
|
+
throw new Error("Cannot rebuild");
|
1120
|
+
sendRequest(refs, { command: "rebuild", rebuildID: response.rebuildID }, (error2, response2) => {
|
1121
|
+
if (error2) {
|
1122
|
+
const message = { pluginName: "", text: error2, location: null, notes: [], detail: void 0 };
|
1123
|
+
return callback2(failureErrorWithLog("Build failed", [message], []), null);
|
1124
|
+
}
|
1125
|
+
buildResponseToResult(response2, (error3, result3) => {
|
1126
|
+
if (error3)
|
1127
|
+
reject(error3);
|
1128
|
+
else
|
1129
|
+
resolve(result3);
|
1130
|
+
});
|
1131
|
+
});
|
1132
|
+
});
|
1133
|
+
refs.ref();
|
1134
|
+
rebuild.dispose = () => {
|
1135
|
+
if (isDisposed)
|
1136
|
+
return;
|
1137
|
+
isDisposed = true;
|
1138
|
+
sendRequest(refs, { command: "rebuild-dispose", rebuildID: response.rebuildID }, () => {
|
1139
|
+
});
|
1140
|
+
refs.unref();
|
1141
|
+
};
|
1142
|
+
}
|
1143
|
+
result.rebuild = rebuild;
|
1144
|
+
}
|
1145
|
+
if (response.watchID !== void 0) {
|
1146
|
+
if (!stop) {
|
1147
|
+
let isStopped = false;
|
1148
|
+
refs.ref();
|
1149
|
+
stop = () => {
|
1150
|
+
if (isStopped)
|
1151
|
+
return;
|
1152
|
+
isStopped = true;
|
1153
|
+
watchCallbacks.delete(response.watchID);
|
1154
|
+
sendRequest(refs, { command: "watch-stop", watchID: response.watchID }, () => {
|
1155
|
+
});
|
1156
|
+
refs.unref();
|
1157
|
+
};
|
1158
|
+
if (watch) {
|
1159
|
+
watchCallbacks.set(response.watchID, (serviceStopError, watchResponse) => {
|
1160
|
+
if (serviceStopError) {
|
1161
|
+
if (watch.onRebuild)
|
1162
|
+
watch.onRebuild(serviceStopError, null);
|
1163
|
+
return;
|
1164
|
+
}
|
1165
|
+
let result2 = {
|
1166
|
+
errors: replaceDetailsInMessages(watchResponse.errors, details),
|
1167
|
+
warnings: replaceDetailsInMessages(watchResponse.warnings, details)
|
1168
|
+
};
|
1169
|
+
copyResponseToResult(watchResponse, result2);
|
1170
|
+
runOnEndCallbacks(result2, logPluginError, () => {
|
1171
|
+
if (result2.errors.length > 0) {
|
1172
|
+
if (watch.onRebuild)
|
1173
|
+
watch.onRebuild(failureErrorWithLog("Build failed", result2.errors, result2.warnings), null);
|
1174
|
+
return;
|
1175
|
+
}
|
1176
|
+
if (watchResponse.rebuildID !== void 0)
|
1177
|
+
result2.rebuild = rebuild;
|
1178
|
+
result2.stop = stop;
|
1179
|
+
if (watch.onRebuild)
|
1180
|
+
watch.onRebuild(null, result2);
|
1181
|
+
});
|
1182
|
+
});
|
1183
|
+
}
|
1184
|
+
}
|
1185
|
+
result.stop = stop;
|
1186
|
+
}
|
1187
|
+
callback2(null, result);
|
1188
|
+
});
|
1189
|
+
};
|
1190
|
+
if (write && streamIn.isBrowser)
|
1191
|
+
throw new Error(`Cannot enable "write" in the browser`);
|
1192
|
+
if (incremental && streamIn.isSync)
|
1193
|
+
throw new Error(`Cannot use "incremental" with a synchronous build`);
|
1194
|
+
if (watch && streamIn.isSync)
|
1195
|
+
throw new Error(`Cannot use "watch" with a synchronous build`);
|
1196
|
+
sendRequest(refs, request, (error, response) => {
|
1197
|
+
if (error)
|
1198
|
+
return callback(new Error(error), null);
|
1199
|
+
if (serve2) {
|
1200
|
+
let serveResponse = response;
|
1201
|
+
let isStopped = false;
|
1202
|
+
refs.ref();
|
1203
|
+
let result = {
|
1204
|
+
port: serveResponse.port,
|
1205
|
+
host: serveResponse.host,
|
1206
|
+
wait: serve2.wait,
|
1207
|
+
stop() {
|
1208
|
+
if (isStopped)
|
1209
|
+
return;
|
1210
|
+
isStopped = true;
|
1211
|
+
serve2.stop();
|
1212
|
+
refs.unref();
|
1213
|
+
}
|
1214
|
+
};
|
1215
|
+
refs.ref();
|
1216
|
+
serve2.wait.then(refs.unref, refs.unref);
|
1217
|
+
return callback(null, result);
|
1218
|
+
}
|
1219
|
+
return buildResponseToResult(response, callback);
|
1220
|
+
});
|
1221
|
+
};
|
1222
|
+
let transform2 = ({ callName, refs, input, options, isTTY, fs, callback }) => {
|
1223
|
+
const details = createObjectStash();
|
1224
|
+
let start = (inputPath) => {
|
1225
|
+
try {
|
1226
|
+
if (typeof input !== "string")
|
1227
|
+
throw new Error('The input to "transform" must be a string');
|
1228
|
+
let flags = flagsForTransformOptions(callName, options, isTTY, transformLogLevelDefault);
|
1229
|
+
let request = {
|
1230
|
+
command: "transform",
|
1231
|
+
flags,
|
1232
|
+
inputFS: inputPath !== null,
|
1233
|
+
input: inputPath !== null ? inputPath : input
|
1234
|
+
};
|
1235
|
+
sendRequest(refs, request, (error, response) => {
|
1236
|
+
if (error)
|
1237
|
+
return callback(new Error(error), null);
|
1238
|
+
let errors = replaceDetailsInMessages(response.errors, details);
|
1239
|
+
let warnings = replaceDetailsInMessages(response.warnings, details);
|
1240
|
+
let outstanding = 1;
|
1241
|
+
let next = () => --outstanding === 0 && callback(null, { warnings, code: response.code, map: response.map });
|
1242
|
+
if (errors.length > 0)
|
1243
|
+
return callback(failureErrorWithLog("Transform failed", errors, warnings), null);
|
1244
|
+
if (response.codeFS) {
|
1245
|
+
outstanding++;
|
1246
|
+
fs.readFile(response.code, (err, contents) => {
|
1247
|
+
if (err !== null) {
|
1248
|
+
callback(err, null);
|
1249
|
+
} else {
|
1250
|
+
response.code = contents;
|
1251
|
+
next();
|
1252
|
+
}
|
1253
|
+
});
|
1254
|
+
}
|
1255
|
+
if (response.mapFS) {
|
1256
|
+
outstanding++;
|
1257
|
+
fs.readFile(response.map, (err, contents) => {
|
1258
|
+
if (err !== null) {
|
1259
|
+
callback(err, null);
|
1260
|
+
} else {
|
1261
|
+
response.map = contents;
|
1262
|
+
next();
|
1263
|
+
}
|
1264
|
+
});
|
1265
|
+
}
|
1266
|
+
next();
|
1267
|
+
});
|
1268
|
+
} catch (e) {
|
1269
|
+
let flags = [];
|
1270
|
+
try {
|
1271
|
+
pushLogFlags(flags, options, {}, isTTY, transformLogLevelDefault);
|
1272
|
+
} catch (e2) {
|
1273
|
+
}
|
1274
|
+
const error = extractErrorMessageV8(e, streamIn, details, void 0, "");
|
1275
|
+
sendRequest(refs, { command: "error", flags, error }, () => {
|
1276
|
+
error.detail = details.load(error.detail);
|
1277
|
+
callback(failureErrorWithLog("Transform failed", [error], []), null);
|
1278
|
+
});
|
1279
|
+
}
|
1280
|
+
};
|
1281
|
+
if (typeof input === "string" && input.length > 1024 * 1024) {
|
1282
|
+
let next = start;
|
1283
|
+
start = () => fs.writeFile(input, next);
|
1284
|
+
}
|
1285
|
+
start(null);
|
1286
|
+
};
|
1287
|
+
let formatMessages2 = ({ callName, refs, messages, options, callback }) => {
|
1288
|
+
let result = sanitizeMessages(messages, "messages", null, "");
|
1289
|
+
if (!options)
|
1290
|
+
throw new Error(`Missing second argument in ${callName}() call`);
|
1291
|
+
let keys = {};
|
1292
|
+
let kind = getFlag(options, keys, "kind", mustBeString);
|
1293
|
+
let color = getFlag(options, keys, "color", mustBeBoolean);
|
1294
|
+
let terminalWidth = getFlag(options, keys, "terminalWidth", mustBeInteger);
|
1295
|
+
checkForInvalidFlags(options, keys, `in ${callName}() call`);
|
1296
|
+
if (kind === void 0)
|
1297
|
+
throw new Error(`Missing "kind" in ${callName}() call`);
|
1298
|
+
if (kind !== "error" && kind !== "warning")
|
1299
|
+
throw new Error(`Expected "kind" to be "error" or "warning" in ${callName}() call`);
|
1300
|
+
let request = {
|
1301
|
+
command: "format-msgs",
|
1302
|
+
messages: result,
|
1303
|
+
isWarning: kind === "warning"
|
1304
|
+
};
|
1305
|
+
if (color !== void 0)
|
1306
|
+
request.color = color;
|
1307
|
+
if (terminalWidth !== void 0)
|
1308
|
+
request.terminalWidth = terminalWidth;
|
1309
|
+
sendRequest(refs, request, (error, response) => {
|
1310
|
+
if (error)
|
1311
|
+
return callback(new Error(error), null);
|
1312
|
+
callback(null, response.messages);
|
1313
|
+
});
|
1314
|
+
};
|
1315
|
+
return {
|
1316
|
+
readFromStdout,
|
1317
|
+
afterClose,
|
1318
|
+
service: {
|
1319
|
+
buildOrServe,
|
1320
|
+
transform: transform2,
|
1321
|
+
formatMessages: formatMessages2
|
1322
|
+
}
|
1323
|
+
};
|
1324
|
+
}
|
1325
|
+
function createObjectStash() {
|
1326
|
+
const map = new Map();
|
1327
|
+
let nextID = 0;
|
1328
|
+
return {
|
1329
|
+
load(id) {
|
1330
|
+
return map.get(id);
|
1331
|
+
},
|
1332
|
+
store(value) {
|
1333
|
+
if (value === void 0)
|
1334
|
+
return -1;
|
1335
|
+
const id = nextID++;
|
1336
|
+
map.set(id, value);
|
1337
|
+
return id;
|
1338
|
+
}
|
1339
|
+
};
|
1340
|
+
}
|
1341
|
+
function extractCallerV8(e, streamIn, ident) {
|
1342
|
+
let note;
|
1343
|
+
let tried = false;
|
1344
|
+
return () => {
|
1345
|
+
if (tried)
|
1346
|
+
return note;
|
1347
|
+
tried = true;
|
1348
|
+
try {
|
1349
|
+
let lines = (e.stack + "").split("\n");
|
1350
|
+
lines.splice(1, 1);
|
1351
|
+
let location = parseStackLinesV8(streamIn, lines, ident);
|
1352
|
+
if (location) {
|
1353
|
+
note = { text: e.message, location };
|
1354
|
+
return note;
|
1355
|
+
}
|
1356
|
+
} catch (e2) {
|
1357
|
+
}
|
1358
|
+
};
|
1359
|
+
}
|
1360
|
+
function extractErrorMessageV8(e, streamIn, stash, note, pluginName) {
|
1361
|
+
let text = "Internal error";
|
1362
|
+
let location = null;
|
1363
|
+
try {
|
1364
|
+
text = (e && e.message || e) + "";
|
1365
|
+
} catch (e2) {
|
1366
|
+
}
|
1367
|
+
try {
|
1368
|
+
location = parseStackLinesV8(streamIn, (e.stack + "").split("\n"), "");
|
1369
|
+
} catch (e2) {
|
1370
|
+
}
|
1371
|
+
return { pluginName, text, location, notes: note ? [note] : [], detail: stash ? stash.store(e) : -1 };
|
1372
|
+
}
|
1373
|
+
function parseStackLinesV8(streamIn, lines, ident) {
|
1374
|
+
let at = " at ";
|
1375
|
+
if (streamIn.readFileSync && !lines[0].startsWith(at) && lines[1].startsWith(at)) {
|
1376
|
+
for (let i = 1; i < lines.length; i++) {
|
1377
|
+
let line = lines[i];
|
1378
|
+
if (!line.startsWith(at))
|
1379
|
+
continue;
|
1380
|
+
line = line.slice(at.length);
|
1381
|
+
while (true) {
|
1382
|
+
let match = /^(?:new |async )?\S+ \((.*)\)$/.exec(line);
|
1383
|
+
if (match) {
|
1384
|
+
line = match[1];
|
1385
|
+
continue;
|
1386
|
+
}
|
1387
|
+
match = /^eval at \S+ \((.*)\)(?:, \S+:\d+:\d+)?$/.exec(line);
|
1388
|
+
if (match) {
|
1389
|
+
line = match[1];
|
1390
|
+
continue;
|
1391
|
+
}
|
1392
|
+
match = /^(\S+):(\d+):(\d+)$/.exec(line);
|
1393
|
+
if (match) {
|
1394
|
+
let contents;
|
1395
|
+
try {
|
1396
|
+
contents = streamIn.readFileSync(match[1], "utf8");
|
1397
|
+
} catch (e) {
|
1398
|
+
break;
|
1399
|
+
}
|
1400
|
+
let lineText = contents.split(/\r\n|\r|\n|\u2028|\u2029/)[+match[2] - 1] || "";
|
1401
|
+
let column = +match[3] - 1;
|
1402
|
+
let length = lineText.slice(column, column + ident.length) === ident ? ident.length : 0;
|
1403
|
+
return {
|
1404
|
+
file: match[1],
|
1405
|
+
namespace: "file",
|
1406
|
+
line: +match[2],
|
1407
|
+
column: encodeUTF8(lineText.slice(0, column)).length,
|
1408
|
+
length: encodeUTF8(lineText.slice(column, column + length)).length,
|
1409
|
+
lineText: lineText + "\n" + lines.slice(1).join("\n"),
|
1410
|
+
suggestion: ""
|
1411
|
+
};
|
1412
|
+
}
|
1413
|
+
break;
|
1414
|
+
}
|
1415
|
+
}
|
1416
|
+
}
|
1417
|
+
return null;
|
1418
|
+
}
|
1419
|
+
function failureErrorWithLog(text, errors, warnings) {
|
1420
|
+
let limit = 5;
|
1421
|
+
let summary = errors.length < 1 ? "" : ` with ${errors.length} error${errors.length < 2 ? "" : "s"}:` + errors.slice(0, limit + 1).map((e, i) => {
|
1422
|
+
if (i === limit)
|
1423
|
+
return "\n...";
|
1424
|
+
if (!e.location)
|
1425
|
+
return `
|
1426
|
+
error: ${e.text}`;
|
1427
|
+
let { file, line, column } = e.location;
|
1428
|
+
let pluginText = e.pluginName ? `[plugin: ${e.pluginName}] ` : "";
|
1429
|
+
return `
|
1430
|
+
${file}:${line}:${column}: error: ${pluginText}${e.text}`;
|
1431
|
+
}).join("");
|
1432
|
+
let error = new Error(`${text}${summary}`);
|
1433
|
+
error.errors = errors;
|
1434
|
+
error.warnings = warnings;
|
1435
|
+
return error;
|
1436
|
+
}
|
1437
|
+
function replaceDetailsInMessages(messages, stash) {
|
1438
|
+
for (const message of messages) {
|
1439
|
+
message.detail = stash.load(message.detail);
|
1440
|
+
}
|
1441
|
+
return messages;
|
1442
|
+
}
|
1443
|
+
function sanitizeLocation(location, where) {
|
1444
|
+
if (location == null)
|
1445
|
+
return null;
|
1446
|
+
let keys = {};
|
1447
|
+
let file = getFlag(location, keys, "file", mustBeString);
|
1448
|
+
let namespace = getFlag(location, keys, "namespace", mustBeString);
|
1449
|
+
let line = getFlag(location, keys, "line", mustBeInteger);
|
1450
|
+
let column = getFlag(location, keys, "column", mustBeInteger);
|
1451
|
+
let length = getFlag(location, keys, "length", mustBeInteger);
|
1452
|
+
let lineText = getFlag(location, keys, "lineText", mustBeString);
|
1453
|
+
let suggestion = getFlag(location, keys, "suggestion", mustBeString);
|
1454
|
+
checkForInvalidFlags(location, keys, where);
|
1455
|
+
return {
|
1456
|
+
file: file || "",
|
1457
|
+
namespace: namespace || "",
|
1458
|
+
line: line || 0,
|
1459
|
+
column: column || 0,
|
1460
|
+
length: length || 0,
|
1461
|
+
lineText: lineText || "",
|
1462
|
+
suggestion: suggestion || ""
|
1463
|
+
};
|
1464
|
+
}
|
1465
|
+
function sanitizeMessages(messages, property, stash, fallbackPluginName) {
|
1466
|
+
let messagesClone = [];
|
1467
|
+
let index = 0;
|
1468
|
+
for (const message of messages) {
|
1469
|
+
let keys = {};
|
1470
|
+
let pluginName = getFlag(message, keys, "pluginName", mustBeString);
|
1471
|
+
let text = getFlag(message, keys, "text", mustBeString);
|
1472
|
+
let location = getFlag(message, keys, "location", mustBeObjectOrNull);
|
1473
|
+
let notes = getFlag(message, keys, "notes", mustBeArray);
|
1474
|
+
let detail = getFlag(message, keys, "detail", canBeAnything);
|
1475
|
+
let where = `in element ${index} of "${property}"`;
|
1476
|
+
checkForInvalidFlags(message, keys, where);
|
1477
|
+
let notesClone = [];
|
1478
|
+
if (notes) {
|
1479
|
+
for (const note of notes) {
|
1480
|
+
let noteKeys = {};
|
1481
|
+
let noteText = getFlag(note, noteKeys, "text", mustBeString);
|
1482
|
+
let noteLocation = getFlag(note, noteKeys, "location", mustBeObjectOrNull);
|
1483
|
+
checkForInvalidFlags(note, noteKeys, where);
|
1484
|
+
notesClone.push({
|
1485
|
+
text: noteText || "",
|
1486
|
+
location: sanitizeLocation(noteLocation, where)
|
1487
|
+
});
|
1488
|
+
}
|
1489
|
+
}
|
1490
|
+
messagesClone.push({
|
1491
|
+
pluginName: pluginName || fallbackPluginName,
|
1492
|
+
text: text || "",
|
1493
|
+
location: sanitizeLocation(location, where),
|
1494
|
+
notes: notesClone,
|
1495
|
+
detail: stash ? stash.store(detail) : -1
|
1496
|
+
});
|
1497
|
+
index++;
|
1498
|
+
}
|
1499
|
+
return messagesClone;
|
1500
|
+
}
|
1501
|
+
function sanitizeStringArray(values, property) {
|
1502
|
+
const result = [];
|
1503
|
+
for (const value of values) {
|
1504
|
+
if (typeof value !== "string")
|
1505
|
+
throw new Error(`${JSON.stringify(property)} must be an array of strings`);
|
1506
|
+
result.push(value);
|
1507
|
+
}
|
1508
|
+
return result;
|
1509
|
+
}
|
1510
|
+
function convertOutputFiles({ path, contents }) {
|
1511
|
+
let text = null;
|
1512
|
+
return {
|
1513
|
+
path,
|
1514
|
+
contents,
|
1515
|
+
get text() {
|
1516
|
+
if (text === null)
|
1517
|
+
text = decodeUTF8(contents);
|
1518
|
+
return text;
|
1519
|
+
}
|
1520
|
+
};
|
1521
|
+
}
|
1522
|
+
|
1523
|
+
// lib/npm/browser.ts
|
1524
|
+
var version = "0.12.25";
|
1525
|
+
var build = (options) => ensureServiceIsRunning().build(options);
|
1526
|
+
var serve = () => {
|
1527
|
+
throw new Error(`The "serve" API only works in node`);
|
1528
|
+
};
|
1529
|
+
var transform = (input, options) => ensureServiceIsRunning().transform(input, options);
|
1530
|
+
var formatMessages = (messages, options) => ensureServiceIsRunning().formatMessages(messages, options);
|
1531
|
+
var buildSync = () => {
|
1532
|
+
throw new Error(`The "buildSync" API only works in node`);
|
1533
|
+
};
|
1534
|
+
var transformSync = () => {
|
1535
|
+
throw new Error(`The "transformSync" API only works in node`);
|
1536
|
+
};
|
1537
|
+
var formatMessagesSync = () => {
|
1538
|
+
throw new Error(`The "formatMessagesSync" API only works in node`);
|
1539
|
+
};
|
1540
|
+
var initializePromise;
|
1541
|
+
var longLivedService;
|
1542
|
+
var ensureServiceIsRunning = () => {
|
1543
|
+
if (longLivedService)
|
1544
|
+
return longLivedService;
|
1545
|
+
if (initializePromise)
|
1546
|
+
throw new Error('You need to wait for the promise returned from "initialize" to be resolved before calling this');
|
1547
|
+
throw new Error('You need to call "initialize" before calling this');
|
1548
|
+
};
|
1549
|
+
var initialize = (options) => {
|
1550
|
+
options = validateInitializeOptions(options || {});
|
1551
|
+
let wasmURL = options.wasmURL;
|
1552
|
+
let useWorker = options.worker !== false;
|
1553
|
+
if (!wasmURL)
|
1554
|
+
throw new Error('Must provide the "wasmURL" option');
|
1555
|
+
wasmURL += "";
|
1556
|
+
if (initializePromise)
|
1557
|
+
throw new Error('Cannot call "initialize" more than once');
|
1558
|
+
initializePromise = startRunningService(wasmURL, useWorker);
|
1559
|
+
initializePromise.catch(() => {
|
1560
|
+
initializePromise = void 0;
|
1561
|
+
});
|
1562
|
+
return initializePromise;
|
1563
|
+
};
|
1564
|
+
var startRunningService = async (wasmURL, useWorker) => {
|
1565
|
+
let res = await fetch(wasmURL);
|
1566
|
+
if (!res.ok)
|
1567
|
+
throw new Error(`Failed to download ${JSON.stringify(wasmURL)}`);
|
1568
|
+
let wasm = await res.arrayBuffer();
|
1569
|
+
let code = `{let global={};for(let o=self;o;o=Object.getPrototypeOf(o))for(let k of Object.getOwnPropertyNames(o))if(!(k in global))Object.defineProperty(global,k,{get:()=>self[k]});// Copyright 2018 The Go Authors. All rights reserved.
|
1570
|
+
// Use of this source code is governed by a BSD-style
|
1571
|
+
// license that can be found in the LICENSE file.
|
1572
|
+
|
1573
|
+
(() => {
|
1574
|
+
// Map multiple JavaScript environments to a single common API,
|
1575
|
+
// preferring web standards over Node.js API.
|
1576
|
+
//
|
1577
|
+
// Environments considered:
|
1578
|
+
// - Browsers
|
1579
|
+
// - Node.js
|
1580
|
+
// - Electron
|
1581
|
+
// - Parcel
|
1582
|
+
// - Webpack
|
1583
|
+
|
1584
|
+
if (typeof global !== "undefined") {
|
1585
|
+
// global already exists
|
1586
|
+
} else if (typeof window !== "undefined") {
|
1587
|
+
window.global = window;
|
1588
|
+
} else if (typeof self !== "undefined") {
|
1589
|
+
self.global = self;
|
1590
|
+
} else {
|
1591
|
+
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
1592
|
+
}
|
1593
|
+
|
1594
|
+
if (!global.require && typeof require !== "undefined") {
|
1595
|
+
global.require = require;
|
1596
|
+
}
|
1597
|
+
|
1598
|
+
if (!global.fs && global.require) {
|
1599
|
+
const fs = require("fs");
|
1600
|
+
if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) {
|
1601
|
+
|
1602
|
+
global.fs = Object.assign({}, fs, {
|
1603
|
+
// Hack around a Unicode bug in node: https://github.com/nodejs/node/issues/24550
|
1604
|
+
write(fd, buf, offset, length, position, callback) {
|
1605
|
+
if (offset === 0 && length === buf.length && position === null) {
|
1606
|
+
if (fd === process.stdout.fd) {
|
1607
|
+
try {
|
1608
|
+
process.stdout.write(buf, err => err ? callback(err, 0, null) : callback(null, length, buf));
|
1609
|
+
} catch (err) {
|
1610
|
+
callback(err, 0, null);
|
1611
|
+
}
|
1612
|
+
return;
|
1613
|
+
}
|
1614
|
+
if (fd === process.stderr.fd) {
|
1615
|
+
try {
|
1616
|
+
process.stderr.write(buf, err => err ? callback(err, 0, null) : callback(null, length, buf));
|
1617
|
+
} catch (err) {
|
1618
|
+
callback(err, 0, null);
|
1619
|
+
}
|
1620
|
+
return;
|
1621
|
+
}
|
1622
|
+
}
|
1623
|
+
fs.write(fd, buf, offset, length, position, callback);
|
1624
|
+
},
|
1625
|
+
});
|
1626
|
+
|
1627
|
+
}
|
1628
|
+
}
|
1629
|
+
|
1630
|
+
const enosys = () => {
|
1631
|
+
const err = new Error("not implemented");
|
1632
|
+
err.code = "ENOSYS";
|
1633
|
+
return err;
|
1634
|
+
};
|
1635
|
+
|
1636
|
+
if (!global.fs) {
|
1637
|
+
let outputBuf = "";
|
1638
|
+
global.fs = {
|
1639
|
+
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
1640
|
+
writeSync(fd, buf) {
|
1641
|
+
outputBuf += decoder.decode(buf);
|
1642
|
+
const nl = outputBuf.lastIndexOf("\\n");
|
1643
|
+
if (nl != -1) {
|
1644
|
+
console.log(outputBuf.substr(0, nl));
|
1645
|
+
outputBuf = outputBuf.substr(nl + 1);
|
1646
|
+
}
|
1647
|
+
return buf.length;
|
1648
|
+
},
|
1649
|
+
write(fd, buf, offset, length, position, callback) {
|
1650
|
+
if (offset !== 0 || length !== buf.length || position !== null) {
|
1651
|
+
callback(enosys());
|
1652
|
+
return;
|
1653
|
+
}
|
1654
|
+
const n = this.writeSync(fd, buf);
|
1655
|
+
callback(null, n);
|
1656
|
+
},
|
1657
|
+
chmod(path, mode, callback) { callback(enosys()); },
|
1658
|
+
chown(path, uid, gid, callback) { callback(enosys()); },
|
1659
|
+
close(fd, callback) { callback(enosys()); },
|
1660
|
+
fchmod(fd, mode, callback) { callback(enosys()); },
|
1661
|
+
fchown(fd, uid, gid, callback) { callback(enosys()); },
|
1662
|
+
fstat(fd, callback) { callback(enosys()); },
|
1663
|
+
fsync(fd, callback) { callback(null); },
|
1664
|
+
ftruncate(fd, length, callback) { callback(enosys()); },
|
1665
|
+
lchown(path, uid, gid, callback) { callback(enosys()); },
|
1666
|
+
link(path, link, callback) { callback(enosys()); },
|
1667
|
+
lstat(path, callback) { callback(enosys()); },
|
1668
|
+
mkdir(path, perm, callback) { callback(enosys()); },
|
1669
|
+
open(path, flags, mode, callback) { callback(enosys()); },
|
1670
|
+
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
|
1671
|
+
readdir(path, callback) { callback(enosys()); },
|
1672
|
+
readlink(path, callback) { callback(enosys()); },
|
1673
|
+
rename(from, to, callback) { callback(enosys()); },
|
1674
|
+
rmdir(path, callback) { callback(enosys()); },
|
1675
|
+
stat(path, callback) { callback(enosys()); },
|
1676
|
+
symlink(path, link, callback) { callback(enosys()); },
|
1677
|
+
truncate(path, length, callback) { callback(enosys()); },
|
1678
|
+
unlink(path, callback) { callback(enosys()); },
|
1679
|
+
utimes(path, atime, mtime, callback) { callback(enosys()); },
|
1680
|
+
};
|
1681
|
+
}
|
1682
|
+
|
1683
|
+
if (!global.process) {
|
1684
|
+
global.process = {
|
1685
|
+
getuid() { return -1; },
|
1686
|
+
getgid() { return -1; },
|
1687
|
+
geteuid() { return -1; },
|
1688
|
+
getegid() { return -1; },
|
1689
|
+
getgroups() { throw enosys(); },
|
1690
|
+
pid: -1,
|
1691
|
+
ppid: -1,
|
1692
|
+
umask() { throw enosys(); },
|
1693
|
+
cwd() { throw enosys(); },
|
1694
|
+
chdir() { throw enosys(); },
|
1695
|
+
}
|
1696
|
+
}
|
1697
|
+
|
1698
|
+
if (!global.crypto && global.require) {
|
1699
|
+
const nodeCrypto = require("crypto");
|
1700
|
+
global.crypto = {
|
1701
|
+
getRandomValues(b) {
|
1702
|
+
nodeCrypto.randomFillSync(b);
|
1703
|
+
},
|
1704
|
+
};
|
1705
|
+
}
|
1706
|
+
if (!global.crypto) {
|
1707
|
+
throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
|
1708
|
+
}
|
1709
|
+
|
1710
|
+
if (!global.performance) {
|
1711
|
+
global.performance = {
|
1712
|
+
now() {
|
1713
|
+
const [sec, nsec] = process.hrtime();
|
1714
|
+
return sec * 1000 + nsec / 1000000;
|
1715
|
+
},
|
1716
|
+
};
|
1717
|
+
}
|
1718
|
+
|
1719
|
+
if (!global.TextEncoder && global.require) {
|
1720
|
+
global.TextEncoder = require("util").TextEncoder;
|
1721
|
+
}
|
1722
|
+
if (!global.TextEncoder) {
|
1723
|
+
throw new Error("global.TextEncoder is not available, polyfill required");
|
1724
|
+
}
|
1725
|
+
|
1726
|
+
if (!global.TextDecoder && global.require) {
|
1727
|
+
global.TextDecoder = require("util").TextDecoder;
|
1728
|
+
}
|
1729
|
+
if (!global.TextDecoder) {
|
1730
|
+
throw new Error("global.TextDecoder is not available, polyfill required");
|
1731
|
+
}
|
1732
|
+
|
1733
|
+
// End of polyfills for common API.
|
1734
|
+
|
1735
|
+
const encoder = new TextEncoder("utf-8");
|
1736
|
+
const decoder = new TextDecoder("utf-8");
|
1737
|
+
|
1738
|
+
global.Go = class {
|
1739
|
+
constructor() {
|
1740
|
+
this.argv = ["js"];
|
1741
|
+
this.env = {};
|
1742
|
+
this.exit = (code) => {
|
1743
|
+
if (code !== 0) {
|
1744
|
+
console.warn("exit code:", code);
|
1745
|
+
}
|
1746
|
+
};
|
1747
|
+
this._exitPromise = new Promise((resolve) => {
|
1748
|
+
this._resolveExitPromise = resolve;
|
1749
|
+
});
|
1750
|
+
this._pendingEvent = null;
|
1751
|
+
this._scheduledTimeouts = new Map();
|
1752
|
+
this._nextCallbackTimeoutID = 1;
|
1753
|
+
|
1754
|
+
const setInt64 = (addr, v) => {
|
1755
|
+
this.mem.setUint32(addr + 0, v, true);
|
1756
|
+
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
1757
|
+
}
|
1758
|
+
|
1759
|
+
const getInt64 = (addr) => {
|
1760
|
+
const low = this.mem.getUint32(addr + 0, true);
|
1761
|
+
const high = this.mem.getInt32(addr + 4, true);
|
1762
|
+
return low + high * 4294967296;
|
1763
|
+
}
|
1764
|
+
|
1765
|
+
const loadValue = (addr) => {
|
1766
|
+
const f = this.mem.getFloat64(addr, true);
|
1767
|
+
if (f === 0) {
|
1768
|
+
return undefined;
|
1769
|
+
}
|
1770
|
+
if (!isNaN(f)) {
|
1771
|
+
return f;
|
1772
|
+
}
|
1773
|
+
|
1774
|
+
const id = this.mem.getUint32(addr, true);
|
1775
|
+
return this._values[id];
|
1776
|
+
}
|
1777
|
+
|
1778
|
+
const storeValue = (addr, v) => {
|
1779
|
+
const nanHead = 0x7FF80000;
|
1780
|
+
|
1781
|
+
if (typeof v === "number" && v !== 0) {
|
1782
|
+
if (isNaN(v)) {
|
1783
|
+
this.mem.setUint32(addr + 4, nanHead, true);
|
1784
|
+
this.mem.setUint32(addr, 0, true);
|
1785
|
+
return;
|
1786
|
+
}
|
1787
|
+
this.mem.setFloat64(addr, v, true);
|
1788
|
+
return;
|
1789
|
+
}
|
1790
|
+
|
1791
|
+
if (v === undefined) {
|
1792
|
+
this.mem.setFloat64(addr, 0, true);
|
1793
|
+
return;
|
1794
|
+
}
|
1795
|
+
|
1796
|
+
let id = this._ids.get(v);
|
1797
|
+
if (id === undefined) {
|
1798
|
+
id = this._idPool.pop();
|
1799
|
+
if (id === undefined) {
|
1800
|
+
id = this._values.length;
|
1801
|
+
}
|
1802
|
+
this._values[id] = v;
|
1803
|
+
this._goRefCounts[id] = 0;
|
1804
|
+
this._ids.set(v, id);
|
1805
|
+
}
|
1806
|
+
this._goRefCounts[id]++;
|
1807
|
+
let typeFlag = 0;
|
1808
|
+
switch (typeof v) {
|
1809
|
+
case "object":
|
1810
|
+
if (v !== null) {
|
1811
|
+
typeFlag = 1;
|
1812
|
+
}
|
1813
|
+
break;
|
1814
|
+
case "string":
|
1815
|
+
typeFlag = 2;
|
1816
|
+
break;
|
1817
|
+
case "symbol":
|
1818
|
+
typeFlag = 3;
|
1819
|
+
break;
|
1820
|
+
case "function":
|
1821
|
+
typeFlag = 4;
|
1822
|
+
break;
|
1823
|
+
}
|
1824
|
+
this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
|
1825
|
+
this.mem.setUint32(addr, id, true);
|
1826
|
+
}
|
1827
|
+
|
1828
|
+
const loadSlice = (addr) => {
|
1829
|
+
const array = getInt64(addr + 0);
|
1830
|
+
const len = getInt64(addr + 8);
|
1831
|
+
return new Uint8Array(this._inst.exports.mem.buffer, array, len);
|
1832
|
+
}
|
1833
|
+
|
1834
|
+
const loadSliceOfValues = (addr) => {
|
1835
|
+
const array = getInt64(addr + 0);
|
1836
|
+
const len = getInt64(addr + 8);
|
1837
|
+
const a = new Array(len);
|
1838
|
+
for (let i = 0; i < len; i++) {
|
1839
|
+
a[i] = loadValue(array + i * 8);
|
1840
|
+
}
|
1841
|
+
return a;
|
1842
|
+
}
|
1843
|
+
|
1844
|
+
const loadString = (addr) => {
|
1845
|
+
const saddr = getInt64(addr + 0);
|
1846
|
+
const len = getInt64(addr + 8);
|
1847
|
+
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
|
1848
|
+
}
|
1849
|
+
|
1850
|
+
const timeOrigin = Date.now() - performance.now();
|
1851
|
+
this.importObject = {
|
1852
|
+
go: {
|
1853
|
+
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
|
1854
|
+
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
|
1855
|
+
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
|
1856
|
+
// This changes the SP, thus we have to update the SP used by the imported function.
|
1857
|
+
|
1858
|
+
// func wasmExit(code int32)
|
1859
|
+
"runtime.wasmExit": (sp) => {
|
1860
|
+
sp >>>= 0;
|
1861
|
+
const code = this.mem.getInt32(sp + 8, true);
|
1862
|
+
this.exited = true;
|
1863
|
+
delete this._inst;
|
1864
|
+
delete this._values;
|
1865
|
+
delete this._goRefCounts;
|
1866
|
+
delete this._ids;
|
1867
|
+
delete this._idPool;
|
1868
|
+
this.exit(code);
|
1869
|
+
},
|
1870
|
+
|
1871
|
+
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
|
1872
|
+
"runtime.wasmWrite": (sp) => {
|
1873
|
+
sp >>>= 0;
|
1874
|
+
const fd = getInt64(sp + 8);
|
1875
|
+
const p = getInt64(sp + 16);
|
1876
|
+
const n = this.mem.getInt32(sp + 24, true);
|
1877
|
+
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
|
1878
|
+
},
|
1879
|
+
|
1880
|
+
// func resetMemoryDataView()
|
1881
|
+
"runtime.resetMemoryDataView": (sp) => {
|
1882
|
+
sp >>>= 0;
|
1883
|
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
1884
|
+
},
|
1885
|
+
|
1886
|
+
// func nanotime1() int64
|
1887
|
+
"runtime.nanotime1": (sp) => {
|
1888
|
+
sp >>>= 0;
|
1889
|
+
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
|
1890
|
+
},
|
1891
|
+
|
1892
|
+
// func walltime() (sec int64, nsec int32)
|
1893
|
+
"runtime.walltime": (sp) => {
|
1894
|
+
sp >>>= 0;
|
1895
|
+
const msec = (new Date).getTime();
|
1896
|
+
setInt64(sp + 8, msec / 1000);
|
1897
|
+
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
1898
|
+
},
|
1899
|
+
|
1900
|
+
// func scheduleTimeoutEvent(delay int64) int32
|
1901
|
+
"runtime.scheduleTimeoutEvent": (sp) => {
|
1902
|
+
sp >>>= 0;
|
1903
|
+
const id = this._nextCallbackTimeoutID;
|
1904
|
+
this._nextCallbackTimeoutID++;
|
1905
|
+
this._scheduledTimeouts.set(id, setTimeout(
|
1906
|
+
() => {
|
1907
|
+
this._resume();
|
1908
|
+
while (this._scheduledTimeouts.has(id)) {
|
1909
|
+
// for some reason Go failed to register the timeout event, log and try again
|
1910
|
+
// (temporary workaround for https://github.com/golang/go/issues/28975)
|
1911
|
+
console.warn("scheduleTimeoutEvent: missed timeout event");
|
1912
|
+
this._resume();
|
1913
|
+
}
|
1914
|
+
},
|
1915
|
+
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
1916
|
+
));
|
1917
|
+
this.mem.setInt32(sp + 16, id, true);
|
1918
|
+
},
|
1919
|
+
|
1920
|
+
// func clearTimeoutEvent(id int32)
|
1921
|
+
"runtime.clearTimeoutEvent": (sp) => {
|
1922
|
+
sp >>>= 0;
|
1923
|
+
const id = this.mem.getInt32(sp + 8, true);
|
1924
|
+
clearTimeout(this._scheduledTimeouts.get(id));
|
1925
|
+
this._scheduledTimeouts.delete(id);
|
1926
|
+
},
|
1927
|
+
|
1928
|
+
// func getRandomData(r []byte)
|
1929
|
+
"runtime.getRandomData": (sp) => {
|
1930
|
+
sp >>>= 0;
|
1931
|
+
crypto.getRandomValues(loadSlice(sp + 8));
|
1932
|
+
},
|
1933
|
+
|
1934
|
+
// func finalizeRef(v ref)
|
1935
|
+
"syscall/js.finalizeRef": (sp) => {
|
1936
|
+
sp >>>= 0;
|
1937
|
+
const id = this.mem.getUint32(sp + 8, true);
|
1938
|
+
this._goRefCounts[id]--;
|
1939
|
+
if (this._goRefCounts[id] === 0) {
|
1940
|
+
const v = this._values[id];
|
1941
|
+
this._values[id] = null;
|
1942
|
+
this._ids.delete(v);
|
1943
|
+
this._idPool.push(id);
|
1944
|
+
}
|
1945
|
+
},
|
1946
|
+
|
1947
|
+
// func stringVal(value string) ref
|
1948
|
+
"syscall/js.stringVal": (sp) => {
|
1949
|
+
sp >>>= 0;
|
1950
|
+
storeValue(sp + 24, loadString(sp + 8));
|
1951
|
+
},
|
1952
|
+
|
1953
|
+
// func valueGet(v ref, p string) ref
|
1954
|
+
"syscall/js.valueGet": (sp) => {
|
1955
|
+
sp >>>= 0;
|
1956
|
+
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
|
1957
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
1958
|
+
storeValue(sp + 32, result);
|
1959
|
+
},
|
1960
|
+
|
1961
|
+
// func valueSet(v ref, p string, x ref)
|
1962
|
+
"syscall/js.valueSet": (sp) => {
|
1963
|
+
sp >>>= 0;
|
1964
|
+
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
1965
|
+
},
|
1966
|
+
|
1967
|
+
// func valueDelete(v ref, p string)
|
1968
|
+
"syscall/js.valueDelete": (sp) => {
|
1969
|
+
sp >>>= 0;
|
1970
|
+
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
|
1971
|
+
},
|
1972
|
+
|
1973
|
+
// func valueIndex(v ref, i int) ref
|
1974
|
+
"syscall/js.valueIndex": (sp) => {
|
1975
|
+
sp >>>= 0;
|
1976
|
+
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
1977
|
+
},
|
1978
|
+
|
1979
|
+
// valueSetIndex(v ref, i int, x ref)
|
1980
|
+
"syscall/js.valueSetIndex": (sp) => {
|
1981
|
+
sp >>>= 0;
|
1982
|
+
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
1983
|
+
},
|
1984
|
+
|
1985
|
+
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
1986
|
+
"syscall/js.valueCall": (sp) => {
|
1987
|
+
sp >>>= 0;
|
1988
|
+
try {
|
1989
|
+
const v = loadValue(sp + 8);
|
1990
|
+
const m = Reflect.get(v, loadString(sp + 16));
|
1991
|
+
const args = loadSliceOfValues(sp + 32);
|
1992
|
+
const result = Reflect.apply(m, v, args);
|
1993
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
1994
|
+
storeValue(sp + 56, result);
|
1995
|
+
this.mem.setUint8(sp + 64, 1);
|
1996
|
+
} catch (err) {
|
1997
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
1998
|
+
storeValue(sp + 56, err);
|
1999
|
+
this.mem.setUint8(sp + 64, 0);
|
2000
|
+
}
|
2001
|
+
},
|
2002
|
+
|
2003
|
+
// func valueInvoke(v ref, args []ref) (ref, bool)
|
2004
|
+
"syscall/js.valueInvoke": (sp) => {
|
2005
|
+
sp >>>= 0;
|
2006
|
+
try {
|
2007
|
+
const v = loadValue(sp + 8);
|
2008
|
+
const args = loadSliceOfValues(sp + 16);
|
2009
|
+
const result = Reflect.apply(v, undefined, args);
|
2010
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
2011
|
+
storeValue(sp + 40, result);
|
2012
|
+
this.mem.setUint8(sp + 48, 1);
|
2013
|
+
} catch (err) {
|
2014
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
2015
|
+
storeValue(sp + 40, err);
|
2016
|
+
this.mem.setUint8(sp + 48, 0);
|
2017
|
+
}
|
2018
|
+
},
|
2019
|
+
|
2020
|
+
// func valueNew(v ref, args []ref) (ref, bool)
|
2021
|
+
"syscall/js.valueNew": (sp) => {
|
2022
|
+
sp >>>= 0;
|
2023
|
+
try {
|
2024
|
+
const v = loadValue(sp + 8);
|
2025
|
+
const args = loadSliceOfValues(sp + 16);
|
2026
|
+
const result = Reflect.construct(v, args);
|
2027
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
2028
|
+
storeValue(sp + 40, result);
|
2029
|
+
this.mem.setUint8(sp + 48, 1);
|
2030
|
+
} catch (err) {
|
2031
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
2032
|
+
storeValue(sp + 40, err);
|
2033
|
+
this.mem.setUint8(sp + 48, 0);
|
2034
|
+
}
|
2035
|
+
},
|
2036
|
+
|
2037
|
+
// func valueLength(v ref) int
|
2038
|
+
"syscall/js.valueLength": (sp) => {
|
2039
|
+
sp >>>= 0;
|
2040
|
+
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
2041
|
+
},
|
2042
|
+
|
2043
|
+
// valuePrepareString(v ref) (ref, int)
|
2044
|
+
"syscall/js.valuePrepareString": (sp) => {
|
2045
|
+
sp >>>= 0;
|
2046
|
+
const str = encoder.encode(String(loadValue(sp + 8)));
|
2047
|
+
storeValue(sp + 16, str);
|
2048
|
+
setInt64(sp + 24, str.length);
|
2049
|
+
},
|
2050
|
+
|
2051
|
+
// valueLoadString(v ref, b []byte)
|
2052
|
+
"syscall/js.valueLoadString": (sp) => {
|
2053
|
+
sp >>>= 0;
|
2054
|
+
const str = loadValue(sp + 8);
|
2055
|
+
loadSlice(sp + 16).set(str);
|
2056
|
+
},
|
2057
|
+
|
2058
|
+
// func valueInstanceOf(v ref, t ref) bool
|
2059
|
+
"syscall/js.valueInstanceOf": (sp) => {
|
2060
|
+
sp >>>= 0;
|
2061
|
+
this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
|
2062
|
+
},
|
2063
|
+
|
2064
|
+
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
2065
|
+
"syscall/js.copyBytesToGo": (sp) => {
|
2066
|
+
sp >>>= 0;
|
2067
|
+
const dst = loadSlice(sp + 8);
|
2068
|
+
const src = loadValue(sp + 32);
|
2069
|
+
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
2070
|
+
this.mem.setUint8(sp + 48, 0);
|
2071
|
+
return;
|
2072
|
+
}
|
2073
|
+
const toCopy = src.subarray(0, dst.length);
|
2074
|
+
dst.set(toCopy);
|
2075
|
+
setInt64(sp + 40, toCopy.length);
|
2076
|
+
this.mem.setUint8(sp + 48, 1);
|
2077
|
+
},
|
2078
|
+
|
2079
|
+
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
2080
|
+
"syscall/js.copyBytesToJS": (sp) => {
|
2081
|
+
sp >>>= 0;
|
2082
|
+
const dst = loadValue(sp + 8);
|
2083
|
+
const src = loadSlice(sp + 16);
|
2084
|
+
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
2085
|
+
this.mem.setUint8(sp + 48, 0);
|
2086
|
+
return;
|
2087
|
+
}
|
2088
|
+
const toCopy = src.subarray(0, dst.length);
|
2089
|
+
dst.set(toCopy);
|
2090
|
+
setInt64(sp + 40, toCopy.length);
|
2091
|
+
this.mem.setUint8(sp + 48, 1);
|
2092
|
+
},
|
2093
|
+
|
2094
|
+
"debug": (value) => {
|
2095
|
+
console.log(value);
|
2096
|
+
},
|
2097
|
+
}
|
2098
|
+
};
|
2099
|
+
}
|
2100
|
+
|
2101
|
+
async run(instance) {
|
2102
|
+
if (!(instance instanceof WebAssembly.Instance)) {
|
2103
|
+
throw new Error("Go.run: WebAssembly.Instance expected");
|
2104
|
+
}
|
2105
|
+
this._inst = instance;
|
2106
|
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
2107
|
+
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
2108
|
+
NaN,
|
2109
|
+
0,
|
2110
|
+
null,
|
2111
|
+
true,
|
2112
|
+
false,
|
2113
|
+
global,
|
2114
|
+
this,
|
2115
|
+
];
|
2116
|
+
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
|
2117
|
+
this._ids = new Map([ // mapping from JS values to reference ids
|
2118
|
+
[0, 1],
|
2119
|
+
[null, 2],
|
2120
|
+
[true, 3],
|
2121
|
+
[false, 4],
|
2122
|
+
[global, 5],
|
2123
|
+
[this, 6],
|
2124
|
+
]);
|
2125
|
+
this._idPool = []; // unused ids that have been garbage collected
|
2126
|
+
this.exited = false; // whether the Go program has exited
|
2127
|
+
|
2128
|
+
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
|
2129
|
+
let offset = 4096;
|
2130
|
+
|
2131
|
+
const strPtr = (str) => {
|
2132
|
+
const ptr = offset;
|
2133
|
+
const bytes = encoder.encode(str + "\\0");
|
2134
|
+
new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
|
2135
|
+
offset += bytes.length;
|
2136
|
+
if (offset % 8 !== 0) {
|
2137
|
+
offset += 8 - (offset % 8);
|
2138
|
+
}
|
2139
|
+
return ptr;
|
2140
|
+
};
|
2141
|
+
|
2142
|
+
const argc = this.argv.length;
|
2143
|
+
|
2144
|
+
const argvPtrs = [];
|
2145
|
+
this.argv.forEach((arg) => {
|
2146
|
+
argvPtrs.push(strPtr(arg));
|
2147
|
+
});
|
2148
|
+
argvPtrs.push(0);
|
2149
|
+
|
2150
|
+
const keys = Object.keys(this.env).sort();
|
2151
|
+
keys.forEach((key) => {
|
2152
|
+
argvPtrs.push(strPtr(\`\${key}=\${this.env[key]}\`));
|
2153
|
+
});
|
2154
|
+
argvPtrs.push(0);
|
2155
|
+
|
2156
|
+
const argv = offset;
|
2157
|
+
argvPtrs.forEach((ptr) => {
|
2158
|
+
this.mem.setUint32(offset, ptr, true);
|
2159
|
+
this.mem.setUint32(offset + 4, 0, true);
|
2160
|
+
offset += 8;
|
2161
|
+
});
|
2162
|
+
|
2163
|
+
this._inst.exports.run(argc, argv);
|
2164
|
+
if (this.exited) {
|
2165
|
+
this._resolveExitPromise();
|
2166
|
+
}
|
2167
|
+
await this._exitPromise;
|
2168
|
+
}
|
2169
|
+
|
2170
|
+
_resume() {
|
2171
|
+
if (this.exited) {
|
2172
|
+
throw new Error("Go program has already exited");
|
2173
|
+
}
|
2174
|
+
this._inst.exports.resume();
|
2175
|
+
if (this.exited) {
|
2176
|
+
this._resolveExitPromise();
|
2177
|
+
}
|
2178
|
+
}
|
2179
|
+
|
2180
|
+
_makeFuncWrapper(id) {
|
2181
|
+
const go = this;
|
2182
|
+
return function () {
|
2183
|
+
const event = { id: id, this: this, args: arguments };
|
2184
|
+
go._pendingEvent = event;
|
2185
|
+
go._resume();
|
2186
|
+
return event.result;
|
2187
|
+
};
|
2188
|
+
}
|
2189
|
+
}
|
2190
|
+
|
2191
|
+
if (
|
2192
|
+
typeof module !== "undefined" &&
|
2193
|
+
global.require &&
|
2194
|
+
global.require.main === module &&
|
2195
|
+
global.process &&
|
2196
|
+
global.process.versions &&
|
2197
|
+
!global.process.versions.electron
|
2198
|
+
) {
|
2199
|
+
if (process.argv.length < 3) {
|
2200
|
+
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
2201
|
+
process.exit(1);
|
2202
|
+
}
|
2203
|
+
|
2204
|
+
const go = new Go();
|
2205
|
+
go.argv = process.argv.slice(2);
|
2206
|
+
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
|
2207
|
+
go.exit = process.exit;
|
2208
|
+
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
|
2209
|
+
process.on("exit", (code) => { // Node.js exits if no event handler is pending
|
2210
|
+
if (code === 0 && !go.exited) {
|
2211
|
+
// deadlock, make Go print error and stack traces
|
2212
|
+
go._pendingEvent = { id: 0 };
|
2213
|
+
go._resume();
|
2214
|
+
}
|
2215
|
+
});
|
2216
|
+
return go.run(result.instance);
|
2217
|
+
}).catch((err) => {
|
2218
|
+
console.error(err);
|
2219
|
+
process.exit(1);
|
2220
|
+
});
|
2221
|
+
}
|
2222
|
+
})();
|
2223
|
+
onmessage = ({ data: wasm }) => {
|
2224
|
+
let decoder = new TextDecoder();
|
2225
|
+
let fs = global.fs;
|
2226
|
+
let stderr = "";
|
2227
|
+
fs.writeSync = (fd, buffer) => {
|
2228
|
+
if (fd === 1) {
|
2229
|
+
postMessage(buffer);
|
2230
|
+
} else if (fd === 2) {
|
2231
|
+
stderr += decoder.decode(buffer);
|
2232
|
+
let parts = stderr.split("\\n");
|
2233
|
+
if (parts.length > 1)
|
2234
|
+
console.log(parts.slice(0, -1).join("\\n"));
|
2235
|
+
stderr = parts[parts.length - 1];
|
2236
|
+
} else {
|
2237
|
+
throw new Error("Bad write");
|
2238
|
+
}
|
2239
|
+
return buffer.length;
|
2240
|
+
};
|
2241
|
+
let stdin = [];
|
2242
|
+
let resumeStdin;
|
2243
|
+
let stdinPos = 0;
|
2244
|
+
onmessage = ({ data }) => {
|
2245
|
+
if (data.length > 0) {
|
2246
|
+
stdin.push(data);
|
2247
|
+
if (resumeStdin)
|
2248
|
+
resumeStdin();
|
2249
|
+
}
|
2250
|
+
};
|
2251
|
+
fs.read = (fd, buffer, offset, length, position, callback) => {
|
2252
|
+
if (fd !== 0 || offset !== 0 || length !== buffer.length || position !== null) {
|
2253
|
+
throw new Error("Bad read");
|
2254
|
+
}
|
2255
|
+
if (stdin.length === 0) {
|
2256
|
+
resumeStdin = () => fs.read(fd, buffer, offset, length, position, callback);
|
2257
|
+
return;
|
2258
|
+
}
|
2259
|
+
let first = stdin[0];
|
2260
|
+
let count = Math.max(0, Math.min(length, first.length - stdinPos));
|
2261
|
+
buffer.set(first.subarray(stdinPos, stdinPos + count), offset);
|
2262
|
+
stdinPos += count;
|
2263
|
+
if (stdinPos === first.length) {
|
2264
|
+
stdin.shift();
|
2265
|
+
stdinPos = 0;
|
2266
|
+
}
|
2267
|
+
callback(null, count);
|
2268
|
+
};
|
2269
|
+
let go = new global.Go();
|
2270
|
+
go.argv = ["", \`--service=\${"0.12.25"}\`];
|
2271
|
+
WebAssembly.instantiate(wasm, go.importObject).then(({ instance }) => go.run(instance));
|
2272
|
+
};}`;
|
2273
|
+
let worker;
|
2274
|
+
if (useWorker) {
|
2275
|
+
let blob = new Blob([code], { type: "text/javascript" });
|
2276
|
+
worker = new Worker(URL.createObjectURL(blob));
|
2277
|
+
} else {
|
2278
|
+
let fn = new Function("postMessage", code + `var onmessage; return m => onmessage(m)`);
|
2279
|
+
let onmessage = fn((data) => worker.onmessage({ data }));
|
2280
|
+
worker = {
|
2281
|
+
onmessage: null,
|
2282
|
+
postMessage: (data) => onmessage({ data }),
|
2283
|
+
terminate() {
|
2284
|
+
}
|
2285
|
+
};
|
2286
|
+
}
|
2287
|
+
worker.postMessage(wasm);
|
2288
|
+
worker.onmessage = ({ data }) => readFromStdout(data);
|
2289
|
+
let { readFromStdout, service } = createChannel({
|
2290
|
+
writeToStdin(bytes) {
|
2291
|
+
worker.postMessage(bytes);
|
2292
|
+
},
|
2293
|
+
isSync: false,
|
2294
|
+
isBrowser: true
|
2295
|
+
});
|
2296
|
+
longLivedService = {
|
2297
|
+
build: (options) => new Promise((resolve, reject) => service.buildOrServe({
|
2298
|
+
callName: "build",
|
2299
|
+
refs: null,
|
2300
|
+
serveOptions: null,
|
2301
|
+
options,
|
2302
|
+
isTTY: false,
|
2303
|
+
defaultWD: "/",
|
2304
|
+
callback: (err, res2) => err ? reject(err) : resolve(res2)
|
2305
|
+
})),
|
2306
|
+
transform: (input, options) => new Promise((resolve, reject) => service.transform({
|
2307
|
+
callName: "transform",
|
2308
|
+
refs: null,
|
2309
|
+
input,
|
2310
|
+
options: options || {},
|
2311
|
+
isTTY: false,
|
2312
|
+
fs: {
|
2313
|
+
readFile(_, callback) {
|
2314
|
+
callback(new Error("Internal error"), null);
|
2315
|
+
},
|
2316
|
+
writeFile(_, callback) {
|
2317
|
+
callback(null);
|
2318
|
+
}
|
2319
|
+
},
|
2320
|
+
callback: (err, res2) => err ? reject(err) : resolve(res2)
|
2321
|
+
})),
|
2322
|
+
formatMessages: (messages, options) => new Promise((resolve, reject) => service.formatMessages({
|
2323
|
+
callName: "formatMessages",
|
2324
|
+
refs: null,
|
2325
|
+
messages,
|
2326
|
+
options,
|
2327
|
+
callback: (err, res2) => err ? reject(err) : resolve(res2)
|
2328
|
+
}))
|
2329
|
+
};
|
2330
|
+
};
|
2331
|
+
export {
|
2332
|
+
build,
|
2333
|
+
buildSync,
|
2334
|
+
formatMessages,
|
2335
|
+
formatMessagesSync,
|
2336
|
+
initialize,
|
2337
|
+
serve,
|
2338
|
+
transform,
|
2339
|
+
transformSync,
|
2340
|
+
version
|
2341
|
+
};
|