forked from evanw/esbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
1242 lines (1117 loc) · 48.7 KB
/
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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as types from "./types";
import * as protocol from "./stdio_protocol";
declare const ESBUILD_VERSION: string;
function validateTarget(target: string): string {
target += ''
if (target.indexOf(',') >= 0) throw new Error(`Invalid target: ${target}`)
return target
}
let canBeAnything = () => null;
let mustBeBoolean = (value: boolean | undefined): string | null =>
typeof value === 'boolean' ? null : 'a boolean';
let mustBeBooleanOrObject = (value: Object | boolean | undefined): string | null =>
typeof value === 'boolean' || (typeof value === 'object' && !Array.isArray(value)) ? null : 'a boolean or an object';
let mustBeString = (value: string | undefined): string | null =>
typeof value === 'string' ? null : 'a string';
let mustBeRegExp = (value: RegExp | undefined): string | null =>
value instanceof RegExp ? null : 'a RegExp object';
let mustBeInteger = (value: number | undefined): string | null =>
typeof value === 'number' && value === (value | 0) ? null : 'an integer';
let mustBeFunction = (value: Function | undefined): string | null =>
typeof value === 'function' ? null : 'a function';
let mustBeArray = <T>(value: T[] | undefined): string | null =>
Array.isArray(value) ? null : 'an array';
let mustBeObject = (value: Object | undefined): string | null =>
typeof value === 'object' && value !== null && !Array.isArray(value) ? null : 'an object';
let mustBeObjectOrNull = (value: Object | null | undefined): string | null =>
typeof value === 'object' && !Array.isArray(value) ? null : 'an object or null';
let mustBeStringOrBoolean = (value: string | boolean | undefined): string | null =>
typeof value === 'string' || typeof value === 'boolean' ? null : 'a string or a boolean';
let mustBeStringOrObject = (value: string | Object | undefined): string | null =>
typeof value === 'string' || typeof value === 'object' && value !== null && !Array.isArray(value) ? null : 'a string or an object';
let mustBeStringOrArray = (value: string | string[] | undefined): string | null =>
typeof value === 'string' || Array.isArray(value) ? null : 'a string or an array';
let mustBeStringOrUint8Array = (value: string | Uint8Array | undefined): string | null =>
typeof value === 'string' || value instanceof Uint8Array ? null : 'a string or a Uint8Array';
type OptionKeys = { [key: string]: boolean };
function getFlag<T, K extends keyof T>(object: T, keys: OptionKeys, key: K, mustBeFn: (value: T[K]) => string | null): T[K] | undefined {
let value = object[key];
keys[key + ''] = true;
if (value === undefined) return undefined;
let mustBe = mustBeFn(value);
if (mustBe !== null) throw new Error(`"${key}" must be ${mustBe}`);
return value;
}
function checkForInvalidFlags(object: Object, keys: OptionKeys, where: string): void {
for (let key in object) {
if (!(key in keys)) {
throw new Error(`Invalid option ${where}: "${key}"`);
}
}
}
export function validateInitializeOptions(options: types.InitializeOptions): types.InitializeOptions {
let keys: OptionKeys = Object.create(null);
let wasmURL = getFlag(options, keys, 'wasmURL', mustBeString);
let worker = getFlag(options, keys, 'worker', mustBeBoolean);
checkForInvalidFlags(options, keys, 'in startService() call');
return {
wasmURL,
worker,
};
}
type CommonOptions = types.BuildOptions | types.TransformOptions;
function pushLogFlags(flags: string[], options: CommonOptions, keys: OptionKeys, isTTY: boolean, logLevelDefault: types.LogLevel): void {
let color = getFlag(options, keys, 'color', mustBeBoolean);
let logLevel = getFlag(options, keys, 'logLevel', mustBeString);
let logLimit = getFlag(options, keys, 'logLimit', mustBeInteger);
if (color) flags.push(`--color=${color}`);
else if (isTTY) flags.push(`--color=true`); // This is needed to fix "execFileSync" which buffers stderr
flags.push(`--log-level=${logLevel || logLevelDefault}`);
flags.push(`--log-limit=${logLimit || 0}`);
}
function pushCommonFlags(flags: string[], options: CommonOptions, keys: OptionKeys): void {
let sourcesContent = getFlag(options, keys, 'sourcesContent', mustBeBoolean);
let target = getFlag(options, keys, 'target', mustBeStringOrArray);
let format = getFlag(options, keys, 'format', mustBeString);
let globalName = getFlag(options, keys, 'globalName', mustBeString);
let minify = getFlag(options, keys, 'minify', mustBeBoolean);
let minifySyntax = getFlag(options, keys, 'minifySyntax', mustBeBoolean);
let minifyWhitespace = getFlag(options, keys, 'minifyWhitespace', mustBeBoolean);
let minifyIdentifiers = getFlag(options, keys, 'minifyIdentifiers', mustBeBoolean);
let charset = getFlag(options, keys, 'charset', mustBeString);
let treeShaking = getFlag(options, keys, 'treeShaking', mustBeStringOrBoolean);
let jsxFactory = getFlag(options, keys, 'jsxFactory', mustBeString);
let jsxFragment = getFlag(options, keys, 'jsxFragment', mustBeString);
let define = getFlag(options, keys, 'define', mustBeObject);
let pure = getFlag(options, keys, 'pure', mustBeArray);
let keepNames = getFlag(options, keys, 'keepNames', mustBeBoolean);
if (sourcesContent !== void 0) flags.push(`--sources-content=${sourcesContent}`);
if (target) {
if (Array.isArray(target)) flags.push(`--target=${Array.from(target).map(validateTarget).join(',')}`)
else flags.push(`--target=${validateTarget(target)}`)
}
if (format) flags.push(`--format=${format}`);
if (globalName) flags.push(`--global-name=${globalName}`);
if (minify) flags.push('--minify');
if (minifySyntax) flags.push('--minify-syntax');
if (minifyWhitespace) flags.push('--minify-whitespace');
if (minifyIdentifiers) flags.push('--minify-identifiers');
if (charset) flags.push(`--charset=${charset}`);
if (treeShaking !== void 0 && treeShaking !== true) flags.push(`--tree-shaking=${treeShaking}`);
if (jsxFactory) flags.push(`--jsx-factory=${jsxFactory}`);
if (jsxFragment) flags.push(`--jsx-fragment=${jsxFragment}`);
if (define) {
for (let key in define) {
if (key.indexOf('=') >= 0) throw new Error(`Invalid define: ${key}`);
flags.push(`--define:${key}=${define[key]}`);
}
}
if (pure) for (let fn of pure) flags.push(`--pure:${fn}`);
if (keepNames) flags.push(`--keep-names`);
}
function flagsForBuildOptions(
callName: string,
options: types.BuildOptions,
isTTY: boolean,
logLevelDefault: types.LogLevel,
writeDefault: boolean,
): {
flags: string[],
write: boolean,
plugins: types.Plugin[] | undefined,
stdinContents: string | null,
stdinResolveDir: string | null,
absWorkingDir: string | undefined,
incremental: boolean,
nodePaths: string[],
watch: types.WatchMode | null,
} {
let flags: string[] = [];
let keys: OptionKeys = Object.create(null);
let stdinContents: string | null = null;
let stdinResolveDir: string | null = null;
let watchMode: types.WatchMode | null = null;
pushLogFlags(flags, options, keys, isTTY, logLevelDefault);
pushCommonFlags(flags, options, keys);
let sourcemap = getFlag(options, keys, 'sourcemap', mustBeStringOrBoolean);
let bundle = getFlag(options, keys, 'bundle', mustBeBoolean);
let watch = getFlag(options, keys, 'watch', mustBeBooleanOrObject);
let splitting = getFlag(options, keys, 'splitting', mustBeBoolean);
let preserveSymlinks = getFlag(options, keys, 'preserveSymlinks', mustBeBoolean);
let metafile = getFlag(options, keys, 'metafile', mustBeBoolean);
let outfile = getFlag(options, keys, 'outfile', mustBeString);
let outdir = getFlag(options, keys, 'outdir', mustBeString);
let outbase = getFlag(options, keys, 'outbase', mustBeString);
let platform = getFlag(options, keys, 'platform', mustBeString);
let tsconfig = getFlag(options, keys, 'tsconfig', mustBeString);
let resolveExtensions = getFlag(options, keys, 'resolveExtensions', mustBeArray);
let nodePathsInput = getFlag(options, keys, 'nodePaths', mustBeArray);
let mainFields = getFlag(options, keys, 'mainFields', mustBeArray);
let conditions = getFlag(options, keys, 'conditions', mustBeArray);
let external = getFlag(options, keys, 'external', mustBeArray);
let loader = getFlag(options, keys, 'loader', mustBeObject);
let outExtension = getFlag(options, keys, 'outExtension', mustBeObject);
let publicPath = getFlag(options, keys, 'publicPath', mustBeString);
let chunkNames = getFlag(options, keys, 'chunkNames', mustBeString);
let assetNames = getFlag(options, keys, 'assetNames', mustBeString);
let inject = getFlag(options, keys, 'inject', mustBeArray);
let banner = getFlag(options, keys, 'banner', mustBeObject);
let footer = getFlag(options, keys, 'footer', mustBeObject);
let entryPoints = getFlag(options, keys, 'entryPoints', mustBeArray);
let absWorkingDir = getFlag(options, keys, 'absWorkingDir', mustBeString);
let stdin = getFlag(options, keys, 'stdin', mustBeObject);
let write = getFlag(options, keys, 'write', mustBeBoolean) ?? writeDefault; // Default to true if not specified
let incremental = getFlag(options, keys, 'incremental', mustBeBoolean) === true;
let plugins = getFlag(options, keys, 'plugins', mustBeArray);
checkForInvalidFlags(options, keys, `in ${callName}() call`);
if (sourcemap) flags.push(`--sourcemap${sourcemap === true ? '' : `=${sourcemap}`}`);
if (bundle) flags.push('--bundle');
if (watch) {
flags.push('--watch');
if (typeof watch === 'boolean') {
watchMode = {};
} else {
let watchKeys: OptionKeys = Object.create(null);
let onRebuild = getFlag(watch, watchKeys, 'onRebuild', mustBeFunction);
checkForInvalidFlags(watch, watchKeys, `on "watch" in ${callName}() call`);
watchMode = { onRebuild };
}
}
if (splitting) flags.push('--splitting');
if (preserveSymlinks) flags.push('--preserve-symlinks');
if (metafile) flags.push(`--metafile`);
if (outfile) flags.push(`--outfile=${outfile}`);
if (outdir) flags.push(`--outdir=${outdir}`);
if (outbase) flags.push(`--outbase=${outbase}`);
if (platform) flags.push(`--platform=${platform}`);
if (tsconfig) flags.push(`--tsconfig=${tsconfig}`);
if (resolveExtensions) {
let values: string[] = [];
for (let value of resolveExtensions) {
value += '';
if (value.indexOf(',') >= 0) throw new Error(`Invalid resolve extension: ${value}`);
values.push(value);
}
flags.push(`--resolve-extensions=${values.join(',')}`);
}
if (publicPath) flags.push(`--public-path=${publicPath}`);
if (chunkNames) flags.push(`--chunk-names=${chunkNames}`);
if (assetNames) flags.push(`--asset-names=${assetNames}`);
if (mainFields) {
let values: string[] = [];
for (let value of mainFields) {
value += '';
if (value.indexOf(',') >= 0) throw new Error(`Invalid main field: ${value}`);
values.push(value);
}
flags.push(`--main-fields=${values.join(',')}`);
}
if (conditions) {
let values: string[] = [];
for (let value of conditions) {
value += '';
if (value.indexOf(',') >= 0) throw new Error(`Invalid condition: ${value}`);
values.push(value);
}
flags.push(`--conditions=${values.join(',')}`);
}
if (external) for (let name of external) flags.push(`--external:${name}`);
if (banner) {
for (let type in banner) {
if (type.indexOf('=') >= 0) throw new Error(`Invalid banner file type: ${type}`);
flags.push(`--banner:${type}=${banner[type]}`);
}
}
if (footer) {
for (let type in footer) {
if (type.indexOf('=') >= 0) throw new Error(`Invalid footer file type: ${type}`);
flags.push(`--footer:${type}=${footer[type]}`);
}
}
if (inject) for (let path of inject) flags.push(`--inject:${path}`);
if (loader) {
for (let ext in loader) {
if (ext.indexOf('=') >= 0) throw new Error(`Invalid loader extension: ${ext}`);
flags.push(`--loader:${ext}=${loader[ext]}`);
}
}
if (outExtension) {
for (let ext in outExtension) {
if (ext.indexOf('=') >= 0) throw new Error(`Invalid out extension: ${ext}`);
flags.push(`--out-extension:${ext}=${outExtension[ext]}`);
}
}
if (entryPoints) {
for (let entryPoint of entryPoints) {
entryPoint += '';
if (entryPoint.startsWith('-')) throw new Error(`Invalid entry point: ${entryPoint}`);
flags.push(entryPoint);
}
}
if (stdin) {
let stdinKeys: OptionKeys = Object.create(null);
let contents = getFlag(stdin, stdinKeys, 'contents', mustBeString);
let resolveDir = getFlag(stdin, stdinKeys, 'resolveDir', mustBeString);
let sourcefile = getFlag(stdin, stdinKeys, 'sourcefile', mustBeString);
let loader = getFlag(stdin, stdinKeys, 'loader', mustBeString);
checkForInvalidFlags(stdin, stdinKeys, 'in "stdin" object');
if (sourcefile) flags.push(`--sourcefile=${sourcefile}`);
if (loader) flags.push(`--loader=${loader}`);
if (resolveDir) stdinResolveDir = resolveDir + '';
stdinContents = contents ? contents + '' : '';
}
let nodePaths: string[] = [];
if (nodePathsInput) {
for (let value of nodePathsInput) {
value += '';
nodePaths.push(value);
}
}
return {
flags,
write,
plugins,
stdinContents,
stdinResolveDir,
absWorkingDir,
incremental,
nodePaths,
watch: watchMode,
};
}
function flagsForTransformOptions(
callName: string,
options: types.TransformOptions,
isTTY: boolean,
logLevelDefault: types.LogLevel,
): string[] {
let flags: string[] = [];
let keys: OptionKeys = Object.create(null);
pushLogFlags(flags, options, keys, isTTY, logLevelDefault);
pushCommonFlags(flags, options, keys);
let sourcemap = getFlag(options, keys, 'sourcemap', mustBeStringOrBoolean);
let tsconfigRaw = getFlag(options, keys, 'tsconfigRaw', mustBeStringOrObject);
let sourcefile = getFlag(options, keys, 'sourcefile', mustBeString);
let loader = getFlag(options, keys, 'loader', mustBeString);
let banner = getFlag(options, keys, 'banner', mustBeString);
let footer = getFlag(options, keys, 'footer', mustBeString);
checkForInvalidFlags(options, keys, `in ${callName}() call`);
if (sourcemap) flags.push(`--sourcemap=${sourcemap === true ? 'external' : sourcemap}`);
if (tsconfigRaw) flags.push(`--tsconfig-raw=${typeof tsconfigRaw === 'string' ? tsconfigRaw : JSON.stringify(tsconfigRaw)}`);
if (sourcefile) flags.push(`--sourcefile=${sourcefile}`);
if (loader) flags.push(`--loader=${loader}`);
if (banner) flags.push(`--banner=${banner}`);
if (footer) flags.push(`--footer=${footer}`);
return flags;
}
export interface StreamIn {
writeToStdin: (data: Uint8Array) => void;
readFileSync?: (path: string, encoding: 'utf8') => string;
isSync: boolean;
isBrowser: boolean;
}
export interface StreamOut {
readFromStdout: (data: Uint8Array) => void;
afterClose: () => void;
service: StreamService;
}
export interface StreamFS {
writeFile(contents: string, callback: (path: string | null) => void): void;
readFile(path: string, callback: (err: Error | null, contents: string | null) => void): void;
}
export interface Refs {
ref(): void;
unref(): void;
}
export interface StreamService {
buildOrServe(
callName: string,
refs: Refs | null,
serveOptions: types.ServeOptions | null,
options: types.BuildOptions,
isTTY: boolean,
defaultWD: string,
callback: (err: Error | null, res: types.BuildResult | types.ServeResult | null) => void,
): void;
transform(
callName: string,
refs: Refs | null,
input: string,
options: types.TransformOptions,
isTTY: boolean,
fs: StreamFS,
callback: (err: Error | null, res: types.TransformResult | null) => void,
): void;
}
// This can't use any promises in the main execution flow because it must work
// for both sync and async code. There is an exception for plugin code because
// that can't work in sync code anyway.
export function createChannel(streamIn: StreamIn): StreamOut {
type PluginCallback = (request: protocol.OnResolveRequest | protocol.OnLoadRequest) =>
Promise<protocol.OnResolveResponse | protocol.OnLoadResponse>;
type WatchCallback = (error: Error | null, response: any) => void;
interface ServeCallbacks {
onRequest: types.ServeOptions['onRequest'];
onWait: (error: string | null) => void;
}
let responseCallbacks = new Map<number, (error: string | null, response: protocol.Value) => void>();
let pluginCallbacks = new Map<number, PluginCallback>();
let watchCallbacks = new Map<number, WatchCallback>();
let serveCallbacks = new Map<number, ServeCallbacks>();
let nextServeID = 0;
let isClosed = false;
let nextRequestID = 0;
let nextBuildKey = 0;
// Use a long-lived buffer to store stdout data
let stdout = new Uint8Array(16 * 1024);
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) packets
let offset = 0;
while (offset + 4 <= stdoutUsed) {
let length = protocol.readUInt32LE(stdout, offset);
if (offset + 4 + length > stdoutUsed) {
break;
}
offset += 4;
handleIncomingPacket(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 responseCallbacks.values()) {
callback('The service was stopped', null);
}
responseCallbacks.clear();
for (let callbacks of serveCallbacks.values()) {
callbacks.onWait('The service was stopped');
}
serveCallbacks.clear();
for (let callback of watchCallbacks.values()) {
try {
callback(new Error('The service was stopped'), null);
} catch (e) {
console.error(e)
}
}
watchCallbacks.clear();
};
let sendRequest = <Req, Res>(refs: Refs | null, value: Req, callback: (error: string | null, response: Res | null) => void): void => {
if (isClosed) return callback('The service is no longer running', null);
let id = nextRequestID++;
responseCallbacks.set(id, (error, response) => {
try {
callback(error, response as any);
} finally {
if (refs) refs.unref() // Do this after the callback so the callback can extend the lifetime if needed
}
});
if (refs) refs.ref()
streamIn.writeToStdin(protocol.encodePacket({ id, isRequest: true, value: value as any }));
};
let sendResponse = (id: number, value: protocol.Value): void => {
if (isClosed) throw new Error('The service is no longer running');
streamIn.writeToStdin(protocol.encodePacket({ id, isRequest: false, value }));
};
type RequestType =
| protocol.PingRequest
| protocol.OnResolveRequest
| protocol.OnLoadRequest
| protocol.OnRequestRequest
| protocol.OnWaitRequest
| protocol.OnWatchRebuildRequest
let handleRequest = async (id: number, request: RequestType) => {
// Catch exceptions in the code below so they get passed to the caller
try {
switch (request.command) {
case 'ping': {
sendResponse(id, {});
break;
}
case 'resolve': {
let callback = pluginCallbacks.get(request.key);
if (!callback) sendResponse(id, {});
else sendResponse(id, await callback!(request) as any);
break;
}
case 'load': {
let callback = pluginCallbacks.get(request.key);
if (!callback) sendResponse(id, {});
else sendResponse(id, await callback!(request) as any);
break;
}
case 'serve-request': {
let callbacks = serveCallbacks.get(request.serveID);
if (callbacks && callbacks.onRequest) callbacks.onRequest(request.args);
sendResponse(id, {});
break;
}
case 'serve-wait': {
let callbacks = serveCallbacks.get(request.serveID);
if (callbacks) callbacks.onWait(request.error);
sendResponse(id, {});
break;
}
case 'watch-rebuild': {
let callback = watchCallbacks.get(request.watchID);
try {
if (callback) callback(null, request.args);
} catch (err) {
console.error(err);
}
sendResponse(id, {});
break;
}
default:
throw new Error(`Invalid command: ` + (request as any)!.command);
}
} catch (e) {
sendResponse(id, { errors: [extractErrorMessageV8(e, streamIn, null, void 0)] } as any);
}
};
let isFirstPacket = true;
let handleIncomingPacket = (bytes: Uint8Array): void => {
// The first packet is a version check
if (isFirstPacket) {
isFirstPacket = false;
// Validate the binary's version number to make sure esbuild was installed
// correctly. This check was added because some people have reported
// errors that appear to indicate an incorrect installation.
let binaryVersion = String.fromCharCode(...bytes);
if (binaryVersion !== ESBUILD_VERSION) {
throw new Error(`Cannot start service: Host version "${ESBUILD_VERSION}" does not match binary version ${JSON.stringify(binaryVersion)}`);
}
return;
}
let packet = protocol.decodePacket(bytes) as any;
if (packet.isRequest) {
handleRequest(packet.id, packet.value);
}
else {
let callback = responseCallbacks.get(packet.id)!;
responseCallbacks.delete(packet.id);
if (packet.value.error) callback(packet.value.error, {});
else callback(null, packet.value);
}
};
let handlePlugins = (plugins: types.Plugin[], request: protocol.BuildRequest, buildKey: number, stash: ObjectStash): Refs => {
if (streamIn.isSync) throw new Error('Cannot use plugins in synchronous API calls');
let onResolveCallbacks: {
[id: number]: {
name: string,
note: types.Note | undefined,
callback: (args: types.OnResolveArgs) =>
(types.OnResolveResult | null | undefined | Promise<types.OnResolveResult | null | undefined>),
},
} = {};
let onLoadCallbacks: {
[id: number]: {
name: string,
note: types.Note | undefined,
callback: (args: types.OnLoadArgs) =>
(types.OnLoadResult | null | undefined | Promise<types.OnLoadResult | null | undefined>),
},
} = {};
let nextCallbackID = 0;
let i = 0;
request.plugins = [];
for (let item of plugins) {
let keys: OptionKeys = {};
if (typeof item !== 'object') throw new Error(`Plugin at index ${i} must be an object`);
let name = getFlag(item, keys, 'name', mustBeString);
let setup = getFlag(item, keys, 'setup', mustBeFunction);
if (typeof name !== 'string' || name === '') throw new Error(`Plugin at index ${i} is missing a name`);
if (typeof setup !== 'function') throw new Error(`[${name}] Plugin is missing a setup function`);
checkForInvalidFlags(item, keys, `on plugin ${JSON.stringify(name)}`);
let plugin: protocol.BuildPlugin = {
name,
onResolve: [],
onLoad: [],
};
i++;
setup({
onResolve(options, callback) {
let registeredText = `This error came from the "onResolve" callback registered here`
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, 'onResolve');
let keys: OptionKeys = {};
let filter = getFlag(options, keys, 'filter', mustBeRegExp);
let namespace = getFlag(options, keys, 'namespace', mustBeString);
checkForInvalidFlags(options, keys, `in onResolve() call for plugin ${JSON.stringify(name)}`);
if (filter == null) throw new Error(`[${plugin.name}] onResolve() call is missing a filter`);
let id = nextCallbackID++;
onResolveCallbacks[id] = { name: name!, callback, note: registeredNote };
plugin.onResolve.push({ id, filter: filter.source, namespace: namespace || '' });
},
onLoad(options, callback) {
let registeredText = `This error came from the "onLoad" callback registered here`
let registeredNote = extractCallerV8(new Error(registeredText), streamIn, 'onLoad');
let keys: OptionKeys = {};
let filter = getFlag(options, keys, 'filter', mustBeRegExp);
let namespace = getFlag(options, keys, 'namespace', mustBeString);
checkForInvalidFlags(options, keys, `in onLoad() call for plugin ${JSON.stringify(name)}`);
if (filter == null) throw new Error(`[${plugin.name}] onLoad() call is missing a filter`);
let id = nextCallbackID++;
onLoadCallbacks[id] = { name: name!, callback, note: registeredNote };
plugin.onLoad.push({ id, filter: filter.source, namespace: namespace || '' });
},
});
request.plugins.push(plugin);
}
const callback: PluginCallback = async (request) => {
switch (request.command) {
case 'resolve': {
let response: protocol.OnResolveResponse = {}, name, callback, note;
for (let id of request.ids) {
try {
({ name, callback, note } = onResolveCallbacks[id]);
let result = await callback({
path: request.path,
importer: request.importer,
namespace: request.namespace,
resolveDir: request.resolveDir,
kind: request.kind,
pluginData: stash.load(request.pluginData),
});
if (result != null) {
if (typeof result !== 'object') throw new Error(`Expected onResolve() callback in plugin ${JSON.stringify(name)} to return an object`);
let keys: OptionKeys = {};
let pluginName = getFlag(result, keys, 'pluginName', mustBeString);
let path = getFlag(result, keys, 'path', mustBeString);
let namespace = getFlag(result, keys, 'namespace', mustBeString);
let external = getFlag(result, keys, 'external', mustBeBoolean);
let pluginData = getFlag(result, keys, 'pluginData', canBeAnything);
let errors = getFlag(result, keys, 'errors', mustBeArray);
let warnings = getFlag(result, keys, 'warnings', mustBeArray);
checkForInvalidFlags(result, keys, `from onResolve() callback in plugin ${JSON.stringify(name)}`);
response.id = id;
if (pluginName != null) response.pluginName = pluginName;
if (path != null) response.path = path;
if (namespace != null) response.namespace = namespace;
if (external != null) response.external = external;
if (pluginData != null) response.pluginData = stash.store(pluginData);
if (errors != null) response.errors = sanitizeMessages(errors, 'errors', stash);
if (warnings != null) response.warnings = sanitizeMessages(warnings, 'warnings', stash);
break;
}
} catch (e) {
return { id, errors: [extractErrorMessageV8(e, streamIn, stash, note)] };
}
}
return response;
}
case 'load': {
let response: protocol.OnLoadResponse = {}, name, callback, note;
for (let id of request.ids) {
try {
({ name, callback, note } = onLoadCallbacks[id]);
let result = await callback({
path: request.path,
namespace: request.namespace,
pluginData: stash.load(request.pluginData),
});
if (result != null) {
if (typeof result !== 'object') throw new Error(`Expected onLoad() callback in plugin ${JSON.stringify(name)} to return an object`);
let keys: OptionKeys = {};
let pluginName = getFlag(result, keys, 'pluginName', mustBeString);
let contents = getFlag(result, keys, 'contents', mustBeStringOrUint8Array);
let resolveDir = getFlag(result, keys, 'resolveDir', mustBeString);
let pluginData = getFlag(result, keys, 'pluginData', canBeAnything);
let loader = getFlag(result, keys, 'loader', mustBeString);
let errors = getFlag(result, keys, 'errors', mustBeArray);
let warnings = getFlag(result, keys, 'warnings', mustBeArray);
checkForInvalidFlags(result, keys, `from onLoad() callback in plugin ${JSON.stringify(name)}`);
response.id = id;
if (pluginName != null) response.pluginName = pluginName;
if (contents instanceof Uint8Array) response.contents = contents;
else if (contents != null) response.contents = protocol.encodeUTF8(contents);
if (resolveDir != null) response.resolveDir = resolveDir;
if (pluginData != null) response.pluginData = stash.store(pluginData);
if (loader != null) response.loader = loader;
if (errors != null) response.errors = sanitizeMessages(errors, 'errors', stash);
if (warnings != null) response.warnings = sanitizeMessages(warnings, 'warnings', stash);
break;
}
} catch (e) {
return { id, errors: [extractErrorMessageV8(e, streamIn, stash, note)] };
}
}
return response;
}
default:
throw new Error(`Invalid command: ` + (request as any).command);
}
}
let refCount = 0;
return {
ref() { if (++refCount === 1) pluginCallbacks.set(buildKey, callback); },
unref() { if (--refCount === 0) pluginCallbacks.delete(buildKey) },
};
};
interface ServeData {
wait: Promise<void>
stop: () => void
}
let buildServeData = (refs: Refs | null, options: types.ServeOptions, request: protocol.BuildRequest): ServeData => {
let keys: OptionKeys = {};
let port = getFlag(options, keys, 'port', mustBeInteger);
let host = getFlag(options, keys, 'host', mustBeString);
let servedir = getFlag(options, keys, 'servedir', mustBeString);
let onRequest = getFlag(options, keys, 'onRequest', mustBeFunction);
let serveID = nextServeID++;
let onWait: ServeCallbacks['onWait'];
let wait = new Promise<void>((resolve, reject) => {
onWait = error => {
serveCallbacks.delete(serveID);
if (error !== null) reject(new Error(error));
else resolve();
};
});
request.serve = { serveID };
checkForInvalidFlags(options, keys, `in serve() call`);
if (port !== void 0) request.serve.port = port;
if (host !== void 0) request.serve.host = host;
if (servedir !== void 0) request.serve.servedir = servedir;
serveCallbacks.set(serveID, {
onRequest,
onWait: onWait!,
});
return {
wait,
stop() {
sendRequest<protocol.ServeStopRequest, null>(refs, { command: 'serve-stop', serveID }, () => {
// We don't care about the result
});
},
};
};
return {
readFromStdout,
afterClose,
service: {
buildOrServe(callName, callerRefs, serveOptions, options, isTTY, defaultWD, callback) {
let pluginRefs: Refs | undefined;
const details = createObjectStash();
const logLevelDefault = 'warning';
const refs = {
ref() {
if (pluginRefs) pluginRefs.ref()
if (callerRefs) callerRefs.ref()
},
unref() {
if (pluginRefs) pluginRefs.unref()
if (callerRefs) callerRefs.unref()
},
}
try {
let key = nextBuildKey++;
let writeDefault = !streamIn.isBrowser;
let {
flags,
write,
plugins,
stdinContents,
stdinResolveDir,
absWorkingDir,
incremental,
nodePaths,
watch,
} = flagsForBuildOptions(callName, options, isTTY, logLevelDefault, writeDefault);
let request: protocol.BuildRequest = {
command: 'build',
key,
flags,
write,
stdinContents,
stdinResolveDir,
absWorkingDir: absWorkingDir || defaultWD,
incremental,
nodePaths,
hasOnRebuild: !!(watch && watch.onRebuild),
};
let serve = serveOptions && buildServeData(refs, serveOptions, request);
if (plugins && plugins.length > 0) pluginRefs = handlePlugins(plugins, request, key, details);
// Factor out response handling so it can be reused for rebuilds
let rebuild: types.BuildResult['rebuild'] | undefined;
let stop: types.BuildResult['stop'] | undefined;
let buildResponseToResult = (
response: protocol.BuildResponse | null,
callback: (error: Error | null, result: types.BuildResult | null) => void,
): void => {
let errors = replaceDetailsInMessages(response!.errors, details);
let warnings = replaceDetailsInMessages(response!.warnings, details);
if (errors.length > 0) return callback(failureErrorWithLog('Build failed', errors, warnings), null);
let result: types.BuildResult = { warnings };
if (response!.outputFiles) result.outputFiles = response!.outputFiles.map(convertOutputFiles);
if (response!.metafile) result.metafile = JSON.parse(response!.metafile);
if (response!.writeToStdout !== void 0) console.log(protocol.decodeUTF8(response!.writeToStdout).replace(/\n$/, ''));
// Handle incremental rebuilds
if (response!.rebuildID !== void 0) {
if (!rebuild) {
let isDisposed = false;
(rebuild as any) = () => new Promise<types.BuildResult>((resolve, reject) => {
if (isDisposed || isClosed) throw new Error('Cannot rebuild');
sendRequest<protocol.RebuildRequest, protocol.BuildResponse>(refs, { command: 'rebuild', rebuildID: response!.rebuildID! },
(error2, response2) => {
if (error2) return callback(new Error(error2), null);
buildResponseToResult(response2, (error3, result3) => {
if (error3) reject(error3);
else resolve(result3!);
});
});
});
refs.ref()
rebuild!.dispose = () => {
if (isDisposed) return;
isDisposed = true;
sendRequest<protocol.RebuildDisposeRequest, null>(refs, { command: 'rebuild-dispose', rebuildID: response!.rebuildID! }, () => {
// We don't care about the result
});
refs.unref() // Do this after the callback so "sendRequest" can extend the lifetime
};
}
result.rebuild = rebuild;
}
// Handle watch mode
if (response!.watchID !== void 0) {
if (!stop) {
let isStopped = false;
refs.ref()
stop = () => {
if (isStopped) return;
isStopped = true;
watchCallbacks.delete(response!.watchID!);
sendRequest<protocol.WatchStopRequest, null>(refs, { command: 'watch-stop', watchID: response!.watchID! }, () => {
// We don't care about the result
});
refs.unref() // Do this after the callback so "sendRequest" can extend the lifetime
}
if (watch && watch.onRebuild) {
watchCallbacks.set(response!.watchID, (serviceStopError, watchResponse) => {
if (serviceStopError) return watch!.onRebuild!(serviceStopError as any, null);
let errors = replaceDetailsInMessages(watchResponse.errors, details);
let warnings = replaceDetailsInMessages(watchResponse.warnings, details);
if (errors.length > 0) return watch!.onRebuild!(failureErrorWithLog('Build failed', errors, warnings), null);
let result: types.BuildResult = { warnings };
if (watchResponse.outputFiles) result.outputFiles = watchResponse.outputFiles.map(convertOutputFiles);
if (watchResponse.rebuildID !== void 0) result.rebuild = rebuild;
result.stop = stop;
watch!.onRebuild!(null, result);
});
}
}
result.stop = stop;
}
return callback(null, result);
};
if (write && streamIn.isBrowser) throw new Error(`Cannot enable "write" in the browser`);
if (incremental && streamIn.isSync) throw new Error(`Cannot use "incremental" with a synchronous build`);
sendRequest<protocol.BuildRequest, protocol.BuildResponse>(refs, request, (error, response) => {
if (error) return callback(new Error(error), null);
if (serve) {
let serveResponse = response as any as protocol.ServeResponse;
let isStopped = false
// Add a ref/unref for "stop()"
refs.ref()
let result: types.ServeResult = {
port: serveResponse.port,
host: serveResponse.host,
wait: serve.wait,
stop() {
if (isStopped) return
isStopped = true
serve!.stop();
refs.unref() // Do this after the callback so "stop" can extend the lifetime
},
};
// Add a ref/unref for "wait". This must be done independently of
// "stop()" in case the response to "stop()" comes in first before
// the request for "wait". Without this ref/unref, node may close
// the child's stdin pipe after the "stop()" but before the "wait"
// which will cause things to break. This caused a test failure.
refs.ref()
serve.wait.then(refs.unref, refs.unref)
return callback(null, result);
}
return buildResponseToResult(response!, callback);
});
} catch (e) {
let flags: string[] = [];
try { pushLogFlags(flags, options, {}, isTTY, logLevelDefault) } catch { }
const error = extractErrorMessageV8(e, streamIn, details, void 0)
sendRequest(refs, { command: 'error', flags, error }, () => {
error.detail = details.load(error.detail);
callback(failureErrorWithLog('Build failed', [error], []), null);
});
}
},
transform(callName, refs, input, options, isTTY, fs, callback) {
const details = createObjectStash();
const logLevelDefault = 'silent';
// Ideally the "transform()" API would be faster than calling "build()"
// since it doesn't need to touch the file system. However, performance
// measurements with large files on macOS indicate that sending the data
// over the stdio pipe can be 2x slower than just using a temporary file.
//
// This appears to be an OS limitation. Both the JavaScript and Go code
// are using large buffers but the pipe only writes data in 8kb chunks.
// An investigation seems to indicate that this number is hard-coded into
// the OS source code. Presumably files are faster because the OS uses
// a larger chunk size, or maybe even reads everything in one syscall.
//
// The cross-over size where this starts to be faster is around 1mb on
// my machine. In that case, this code tries to use a temporary file if
// possible but falls back to sending the data over the stdio pipe if
// that doesn't work.
let start = (inputPath: string | null) => {
try {
if (typeof input !== 'string') throw new Error('The input to "transform" must be a string');
let flags = flagsForTransformOptions(callName, options, isTTY, logLevelDefault);
let request: protocol.TransformRequest = {
command: 'transform',
flags,
inputFS: inputPath !== null,
input: inputPath !== null ? inputPath : input,
};
sendRequest<protocol.TransformRequest, protocol.TransformResponse>(refs, request, (error, response) => {
if (error) return callback(new Error(error), null);
let errors = replaceDetailsInMessages(response!.errors, details);
let warnings = replaceDetailsInMessages(response!.warnings, details);
let outstanding = 1;
let next = () => --outstanding === 0 && callback(null, { warnings, code: response!.code, map: response!.map });
if (errors.length > 0) return callback(failureErrorWithLog('Transform failed', errors, warnings), null);
// Read the JavaScript file from the file system
if (response!.codeFS) {
outstanding++;
fs.readFile(response!.code, (err, contents) => {