-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstdio_protocol.ts
374 lines (322 loc) · 8.61 KB
/
stdio_protocol.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// The JavaScript API communicates with the Go child process over stdin/stdout
// using this protocol. It's a very simple binary protocol that uses primitives
// and nested arrays and maps. It's basically JSON with UTF-8 encoding and an
// additional byte array primitive. You must send a response after receiving a
// request because the other end is blocking on the response coming back.
import * as types from "./types";
export interface BuildRequest {
command: 'build';
key: number;
flags: string[];
write: boolean;
stdinContents: string | null;
stdinResolveDir: string | null;
absWorkingDir: string;
incremental: boolean;
nodePaths: string[];
hasOnRebuild: boolean;
plugins?: BuildPlugin[];
serve?: ServeRequest;
}
export interface ServeRequest {
serveID: number;
port?: number;
host?: string;
servedir?: string;
}
export interface ServeResponse {
port: number;
host: string;
}
export interface ServeStopRequest {
command: 'serve-stop';
serveID: number;
}
export interface BuildPlugin {
name: string;
onResolve: { id: number, filter: string, namespace: string }[];
onLoad: { id: number, filter: string, namespace: string }[];
}
export interface BuildResponse {
errors: types.Message[];
warnings: types.Message[];
outputFiles: BuildOutputFile[];
metafile: string;
writeToStdout?: Uint8Array;
rebuildID?: number;
watchID?: number;
}
export interface BuildOutputFile {
path: string;
contents: Uint8Array;
}
export interface PingRequest {
command: 'ping';
}
export interface RebuildRequest {
command: 'rebuild';
rebuildID: number;
}
export interface RebuildDisposeRequest {
command: 'rebuild-dispose';
rebuildID: number;
}
export interface WatchStopRequest {
command: 'watch-stop';
watchID: number;
}
export interface OnRequestRequest {
command: 'serve-request';
serveID: number;
args: types.ServeOnRequestArgs;
}
export interface OnWaitRequest {
command: 'serve-wait';
serveID: number;
error: string | null;
}
export interface OnWatchRebuildRequest {
command: 'watch-rebuild';
watchID: number;
args: types.BuildResult;
}
export interface TransformRequest {
command: 'transform';
flags: string[];
input: string;
inputFS: boolean;
}
export interface TransformResponse {
errors: types.Message[];
warnings: types.Message[];
code: string;
codeFS: boolean;
map: string;
mapFS: boolean;
}
export interface OnResolveRequest {
command: 'resolve';
key: number;
ids: number[];
path: string;
importer: string;
namespace: string;
resolveDir: string;
kind: types.ImportKind;
pluginData: number;
}
export interface OnResolveResponse {
id?: number;
pluginName?: string;
errors?: types.PartialMessage[];
warnings?: types.PartialMessage[];
path?: string;
external?: boolean;
namespace?: string;
pluginData?: number;
}
export interface OnLoadRequest {
command: 'load';
key: number;
ids: number[];
path: string;
namespace: string;
pluginData: number;
}
export interface OnLoadResponse {
id?: number;
pluginName?: string;
errors?: types.PartialMessage[];
warnings?: types.PartialMessage[];
contents?: Uint8Array;
resolveDir?: string;
loader?: string;
pluginData?: number;
}
////////////////////////////////////////////////////////////////////////////////
export interface Packet {
id: number;
isRequest: boolean;
value: Value;
}
export type Value =
| null
| boolean
| number
| string
| Uint8Array
| Value[]
| { [key: string]: Value }
export function encodePacket(packet: Packet): Uint8Array {
let visit = (value: Value) => {
if (value === null) {
bb.write8(0);
} else if (typeof value === 'boolean') {
bb.write8(1);
bb.write8(+value);
} else if (typeof value === 'number') {
bb.write8(2);
bb.write32(value | 0);
} else if (typeof value === 'string') {
bb.write8(3);
bb.write(encodeUTF8(value));
} else if (value instanceof Uint8Array) {
bb.write8(4);
bb.write(value);
} else if (value instanceof Array) {
bb.write8(5);
bb.write32(value.length);
for (let item of value) {
visit(item);
}
} else {
let keys = Object.keys(value);
bb.write8(6);
bb.write32(keys.length);
for (let key of keys) {
bb.write(encodeUTF8(key));
visit(value[key]);
}
}
};
let bb = new ByteBuffer;
bb.write32(0); // Reserve space for the length
bb.write32((packet.id << 1) | +!packet.isRequest);
visit(packet.value);
writeUInt32LE(bb.buf, bb.len - 4, 0); // Patch the length in
return bb.buf.subarray(0, bb.len);
}
export function decodePacket(bytes: Uint8Array): Packet {
let visit = (): Value => {
switch (bb.read8()) {
case 0: // null
return null;
case 1: // boolean
return !!bb.read8();
case 2: // number
return bb.read32();
case 3: // string
return decodeUTF8(bb.read());
case 4: // Uint8Array
return bb.read();
case 5: { // Value[]
let count = bb.read32();
let value: Value[] = [];
for (let i = 0; i < count; i++) {
value.push(visit());
}
return value;
}
case 6: { // { [key: string]: Value }
let count = bb.read32();
let value: { [key: string]: Value } = {};
for (let i = 0; i < count; i++) {
value[decodeUTF8(bb.read())] = visit();
}
return value;
}
default:
throw new Error('Invalid packet');
}
};
let bb = new ByteBuffer(bytes);
let id = bb.read32();
let isRequest = (id & 1) === 0;
id >>>= 1;
let value = visit();
if (bb.ptr !== bytes.length) {
throw new Error('Invalid packet');
}
return { id, isRequest, value };
}
class ByteBuffer {
len = 0;
ptr = 0;
constructor(public buf = new Uint8Array(1024)) {
}
private _write(delta: number): number {
if (this.len + delta > this.buf.length) {
let clone = new Uint8Array((this.len + delta) * 2);
clone.set(this.buf);
this.buf = clone;
}
this.len += delta;
return this.len - delta;
}
write8(value: number): void {
let offset = this._write(1);
this.buf[offset] = value;
}
write32(value: number): void {
let offset = this._write(4);
writeUInt32LE(this.buf, value, offset);
}
write(bytes: Uint8Array): void {
let offset = this._write(4 + bytes.length);
writeUInt32LE(this.buf, bytes.length, offset);
this.buf.set(bytes, offset + 4);
}
private _read(delta: number): number {
if (this.ptr + delta > this.buf.length) {
throw new Error('Invalid packet');
}
this.ptr += delta;
return this.ptr - delta;
}
read8(): number {
return this.buf[this._read(1)];
}
read32(): number {
return readUInt32LE(this.buf, this._read(4));
}
read(): Uint8Array {
let length = this.read32();
let bytes = new Uint8Array(length);
let ptr = this._read(bytes.length);
bytes.set(this.buf.subarray(ptr, ptr + length));
return bytes;
}
}
export let encodeUTF8: (text: string) => Uint8Array
export let decodeUTF8: (bytes: Uint8Array) => string
// For the browser and node 12.x
if (typeof TextEncoder !== 'undefined' && typeof TextDecoder !== 'undefined') {
let encoder = new TextEncoder();
let decoder = new TextDecoder();
encodeUTF8 = text => encoder.encode(text);
decodeUTF8 = bytes => decoder.decode(bytes);
}
// For node 10.x
else if (typeof Buffer !== 'undefined') {
encodeUTF8 = text => {
let buffer: Uint8Array = Buffer.from(text);
// The test framework called "Jest" breaks node's Buffer API. Normally
// instances of Buffer are also instances of Uint8Array, but not when
// esbuild is run inside of whatever weird environment Jest uses. More
// info: https://github.com/facebook/jest/issues/4422.
if (!(buffer instanceof Uint8Array)) {
// Construct a new Uint8Array with the contents of the buffer to force
// it to be a Uint8Array instance. This is wasteful since it's slower
// than just using the Buffer, but this should only happen when esbuild
// is run inside of Jest.
buffer = new Uint8Array(buffer);
}
return buffer;
};
decodeUTF8 = bytes => Buffer.from(bytes).toString();
}
else {
throw new Error('No UTF-8 codec found');
}
export 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;
}