/
api-common.ts
310 lines (272 loc) · 11.1 KB
/
api-common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import * as types from "./api-types";
function pushCommonFlags(flags: string[], options: types.CommonOptions, isTTY: boolean, logLevelDefault: types.LogLevel): void {
if (options.target) flags.push(`--target=${options.target}`);
if (options.strict === true) flags.push(`--strict`);
else if (options.strict) for (let key of options.strict) flags.push(`--strict:${key}`);
if (options.minify) flags.push('--minify');
if (options.minifySyntax) flags.push('--minify-syntax');
if (options.minifyWhitespace) flags.push('--minify-whitespace');
if (options.minifyIdentifiers) flags.push('--minify-identifiers');
if (options.jsxFactory) flags.push(`--jsx-factory=${options.jsxFactory}`);
if (options.jsxFragment) flags.push(`--jsx-fragment=${options.jsxFragment}`);
if (options.define) for (let key in options.define) flags.push(`--define:${key}=${options.define[key]}`);
if (options.pure) for (let fn of options.pure) flags.push(`--pure:${fn}`);
if (options.color) flags.push(`--color=${options.color}`);
else if (isTTY) flags.push(`--color=true`); // This is needed to fix "execFileSync" which buffers stderr
flags.push(`--log-level=${options.logLevel || logLevelDefault}`);
flags.push(`--error-limit=${options.errorLimit || 0}`);
}
function flagsForBuildOptions(options: types.BuildOptions, isTTY: boolean): string[] {
let flags: string[] = [];
pushCommonFlags(flags, options, isTTY, 'info');
if (options.sourcemap) flags.push(`--sourcemap${options.sourcemap === true ? '' : `=${options.sourcemap}`}`);
if (options.globalName) flags.push(`--global-name=${options.globalName}`);
if (options.bundle) flags.push('--bundle');
if (options.splitting) flags.push('--splitting');
if (options.metafile) flags.push(`--metafile=${options.metafile}`);
if (options.outfile) flags.push(`--outfile=${options.outfile}`);
if (options.outdir) flags.push(`--outdir=${options.outdir}`);
if (options.platform) flags.push(`--platform=${options.platform}`);
if (options.format) flags.push(`--format=${options.format}`);
if (options.resolveExtensions) flags.push(`--resolve-extensions=${options.resolveExtensions.join(',')}`);
if (options.external) for (let name of options.external) flags.push(`--external:${name}`);
if (options.loader) for (let ext in options.loader) flags.push(`--loader:${ext}=${options.loader[ext]}`);
if (options.write === false) flags.push(`--write=false`);
for (let entryPoint of options.entryPoints) {
if (entryPoint.startsWith('-')) throw new Error(`Invalid entry point: ${entryPoint}`);
flags.push(entryPoint);
}
return flags;
}
function flagsForTransformOptions(options: types.TransformOptions, isTTY: boolean): string[] {
let flags: string[] = [];
pushCommonFlags(flags, options, isTTY, 'silent');
if (options.sourcemap) flags.push(`--sourcemap=${options.sourcemap === true ? 'external' : options.sourcemap}`);
if (options.sourcefile) flags.push(`--sourcefile=${options.sourcefile}`);
if (options.loader) flags.push(`--loader=${options.loader}`);
return flags;
}
type Request = string[];
type Response = { [key: string]: Uint8Array };
type ResponseCallback = (err: string | null, res: Response) => void;
export interface StreamIn {
writeToStdin: (data: Uint8Array) => void,
}
export interface StreamOut {
readFromStdout: (data: Uint8Array) => void;
afterClose: () => void;
service: StreamService;
}
export interface StreamService {
build(options: types.BuildOptions, isTTY: boolean, callback: (err: Error | null, res: types.BuildResult | null) => void): void;
transform(input: string, options: types.TransformOptions, isTTY: boolean, callback: (err: Error | null, res: types.TransformResult | null) => void): void;
}
interface TextCodec {
encode(text: string): Uint8Array
decode(bytes: Uint8Array): string
}
function textCodec(): TextCodec {
// For the browser and node 12.x
if (typeof TextEncoder !== 'undefined' && typeof TextDecoder !== 'undefined') {
let encoder = new TextEncoder();
let decoder = new TextDecoder();
return {
encode: text => encoder.encode(text),
decode: bytes => decoder.decode(bytes),
}
}
// For node 10.x
if (typeof Buffer !== 'undefined') {
return {
encode: text => Buffer.from(text),
decode: bytes => Buffer.from(bytes).toString(),
}
}
throw new Error('No UTF-8 codec found');
}
// This can't use any promises because it must work for both sync and async code
export function createChannel(options: StreamIn): StreamOut {
let requests = new Map<string, ResponseCallback>();
let codec = textCodec();
let isClosed = false;
let nextID = 0;
// Use a long-lived buffer to store stdout data
let stdout = new Uint8Array(4096);
let stdoutUsed = 0;
let readFromStdout = (chunk: Uint8Array) => {
// Append the chunk to the stdout buffer, growing it as necessary
let limit = stdoutUsed + chunk.length;
if (limit > stdout.length) {
let swap = new Uint8Array(limit * 2);
swap.set(stdout);
stdout = swap;
}
stdout.set(chunk, stdoutUsed);
stdoutUsed += chunk.length;
// Process all complete (i.e. not partial) responses
let offset = 0;
while (offset + 4 <= stdoutUsed) {
let length = readUInt32LE(stdout, offset);
if (offset + 4 + length > stdoutUsed) {
break;
}
offset += 4;
handleResponse(stdout.slice(offset, offset + length));
offset += length;
}
if (offset > 0) {
stdout.set(stdout.slice(offset));
stdoutUsed -= offset;
}
};
let afterClose = () => {
// When the process is closed, fail all pending requests
isClosed = true;
for (let callback of requests.values()) {
callback('The service was stopped', {});
}
requests.clear();
};
let sendRequest = (request: Request, callback: ResponseCallback): void => {
if (isClosed) return callback('The service is no longer running', {});
// Allocate an id for this request
let id = (nextID++).toString();
requests.set(id, callback);
// Figure out how long the request will be
let argBuffers = [codec.encode(id)];
let length = 4 + 4 + 4 + argBuffers[0].length;
for (let arg of request) {
let argBuffer = codec.encode(arg);
argBuffers.push(argBuffer);
length += 4 + argBuffer.length;
}
// Write out the request message
let bytes = new Uint8Array(length);
let offset = 0;
let writeUint32 = (value: number) => {
writeUInt32LE(bytes, value, offset);
offset += 4;
};
writeUint32(length - 4);
writeUint32(argBuffers.length);
for (let argBuffer of argBuffers) {
writeUint32(argBuffer.length);
bytes.set(argBuffer, offset);
offset += argBuffer.length;
}
options.writeToStdin(bytes);
};
let handleResponse = (bytes: Uint8Array): void => {
let offset = 0;
let eat = (n: number) => {
offset += n;
if (offset > bytes.length) throw new Error('Invalid message');
return offset - n;
};
let count = readUInt32LE(bytes, eat(4));
let response: Response = {};
let id;
// Parse the response into a map
for (let i = 0; i < count; i++) {
let keyLength = readUInt32LE(bytes, eat(4));
let key = codec.decode(bytes.slice(offset, eat(keyLength) + keyLength));
let valueLength = readUInt32LE(bytes, eat(4));
let value = bytes.slice(offset, eat(valueLength) + valueLength);
if (key === 'id') id = codec.decode(value);
else response[key] = value;
}
// Dispatch the response
if (!id) throw new Error('Invalid message');
let callback = requests.get(id)!;
requests.delete(id);
if (response.error) callback(codec.decode(response.error), {});
else callback(null, response);
};
return {
readFromStdout,
afterClose,
service: {
build(options, isTTY, callback) {
let flags = flagsForBuildOptions(options, isTTY);
sendRequest(['build'].concat(flags), (error, response) => {
if (error) return callback(new Error(error), null);
let errors = jsonToMessages(codec.decode(response.errors));
let warnings = jsonToMessages(codec.decode(response.warnings));
if (errors.length > 0) return callback(failureErrorWithLog('Build failed', errors, warnings), null);
let result: types.BuildResult = { warnings };
if (options.write === false) result.outputFiles = decodeOutputFiles(codec, response.outputFiles);
callback(null, result);
});
},
transform(input, options, isTTY, callback) {
let flags = flagsForTransformOptions(options, isTTY);
sendRequest(['transform', input].concat(flags), (error, response) => {
if (error) return callback(new Error(error), null);
let errors = jsonToMessages(codec.decode(response.errors));
let warnings = jsonToMessages(codec.decode(response.warnings));
if (errors.length > 0) return callback(failureErrorWithLog('Transform failed', errors, warnings), null);
callback(null, { warnings, js: codec.decode(response.js), jsSourceMap: codec.decode(response.jsSourceMap) });
});
},
},
};
}
function readUInt32LE(buffer: Uint8Array, offset: number): number {
return buffer[offset++] |
(buffer[offset++] << 8) |
(buffer[offset++] << 16) |
(buffer[offset++] << 24);
}
function writeUInt32LE(buffer: Uint8Array, value: number, offset: number): void {
buffer[offset++] = value;
buffer[offset++] = value >> 8;
buffer[offset++] = value >> 16;
buffer[offset++] = value >> 24;
}
function jsonToMessages(json: string): types.Message[] {
let parts = JSON.parse(json);
let messages: types.Message[] = [];
for (let i = 0; i < parts.length; i += 6) {
messages.push({
text: parts[i],
location: parts[i + 1] < 0 ? null : {
length: parts[i + 1],
file: parts[i + 2],
line: parts[i + 3],
column: parts[i + 4],
lineText: parts[i + 5],
},
});
}
return messages;
}
function failureErrorWithLog(text: string, errors: types.Message[], warnings: types.Message[]): Error {
let limit = 5
let summary = errors.length < 1 ? '' : ` with ${errors.length} error${errors.length < 2 ? '' : 's'}:` +
errors.slice(0, limit + 1).map((e, i) => {
if (i === limit) return '\n...';
if (!e.location) return `\nerror: ${e.text}`;
let { file, line, column } = e.location;
return `\n${file}:${line}:${column}: error: ${e.text}`;
}).join('');
let error: any = new Error(`${text}${summary}`);
error.errors = errors;
error.warnings = warnings;
return error;
}
function decodeOutputFiles(codec: TextCodec, bytes: Uint8Array): types.OutputFile[] {
let outputFiles: types.OutputFile[] = [];
let offset = 0;
let count = readUInt32LE(bytes, offset);
offset += 4;
for (let i = 0; i < count; i++) {
let pathLength = readUInt32LE(bytes, offset);
let path = codec.decode(bytes.slice(offset + 4, offset + 4 + pathLength));
offset += 4 + pathLength;
let contentsLength = readUInt32LE(bytes, offset);
let contents = new Uint8Array(bytes.slice(offset + 4, offset + 4 + contentsLength));
offset += 4 + contentsLength;
outputFiles.push({ path, contents });
}
return outputFiles;
}