Support async worker functions in the tsc_wrapped/worker.ts helper library.
PiperOrigin-RevId: 265120467
diff --git a/internal/tsc_wrapped/package.json b/internal/tsc_wrapped/package.json
index 64940e1..1f78bd4 100644
--- a/internal/tsc_wrapped/package.json
+++ b/internal/tsc_wrapped/package.json
@@ -2,7 +2,7 @@
"description": "legacy build-time dependencies to compile and run tsc_wrapped",
"devDependencies": {
"@types/jasmine": "2.8.2",
- "@types/node": "7.0.18",
+ "@types/node": "10.12.20",
"@types/tmp": "0.0.33",
"protobufjs": "5.0.3",
"tmp": "0.0.33",
diff --git a/internal/tsc_wrapped/tsconfig.json b/internal/tsc_wrapped/tsconfig.json
index 830ed18..8c22290 100644
--- a/internal/tsc_wrapped/tsconfig.json
+++ b/internal/tsc_wrapped/tsconfig.json
@@ -16,7 +16,10 @@
"es2015.core",
"es2015.collection",
"es2015.iterable",
- "es2015.promise"
+ "es2015.promise",
+ // This will need to become es2018.asynciterable when
+ // bumping the version of TypeScript in rules_typescript/package.json
+ "esnext.asynciterable"
]
}
}
diff --git a/internal/tsc_wrapped/worker.ts b/internal/tsc_wrapped/worker.ts
index dc50055..0bdb04c 100644
--- a/internal/tsc_wrapped/worker.ts
+++ b/internal/tsc_wrapped/worker.ts
@@ -115,12 +115,15 @@
* data, and dispatches into `runOneBuild` for the actual compilation to happen.
*
* The compilation handler is parameterized so that this code can be used by
- * different compiler entry points (currently TypeScript compilation and Angular
- * compilation).
+ * different compiler entry points (currently TypeScript compilation, Angular
+ * compilation, and the contrib vulcanize worker).
+ *
+ * It's also exposed publicly as an npm package:
+ * https://www.npmjs.com/package/@bazel/worker
*/
-export function runWorkerLoop(
+export async function runWorkerLoop(
runOneBuild: (args: string[], inputs?: {[path: string]: string}) =>
- boolean) {
+ boolean | Promise<boolean>) {
// Hook all output to stderr and write it to a buffer, then include
// that buffer's in the worker protcol proto's textual output. This
// means you can log via console.error() and it will appear to the
@@ -142,10 +145,8 @@
// it exits and waits for more input. If a message has been read, it strips
// its data of this buffer.
let buf: Buffer = Buffer.alloc(0);
- process.stdin.on('readable', () => {
- const chunk = process.stdin.read() as Buffer;
- if (!chunk) return;
- buf = Buffer.concat([buf, chunk]);
+ stdinLoop: for await (const chunk of process.stdin) {
+ buf = Buffer.concat([buf, chunk as Buffer]);
try {
const reader = new protobufjs.Reader(buf);
// Read all requests that have accumulated in the buffer.
@@ -154,7 +155,7 @@
const msgLength: number = reader.uint32();
// chunk might be an incomplete read from stdin. If there are not enough
// bytes for the next full message, wait for more input.
- if ((reader.len - reader.pos) < msgLength) return;
+ if ((reader.len - reader.pos) < msgLength) continue stdinLoop;
const req = workerpb.WorkRequest.decode(reader, msgLength) as
workerProto.WorkRequest;
@@ -170,7 +171,7 @@
inputs[input.path] = input.digest.toString('hex');
}
debug('Compiling with:\n\t' + args.join('\n\t'));
- const exitCode = runOneBuild(args, inputs) ? 0 : 1;
+ const exitCode = (await runOneBuild(args, inputs)) ? 0 : 1;
process.stdout.write((workerpb.WorkResponse.encodeDelimited({
exitCode,
output: consoleOutput,
@@ -196,5 +197,5 @@
// Clear buffer so the next build won't read an incomplete request.
buf = Buffer.alloc(0);
}
- });
+ }
}
diff --git a/package.json b/package.json
index 42bdbd7..9a86e26 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,7 @@
"@bazel/typescript": "^0.32.0",
"@types/jasmine": "^2.8.2",
"@types/long": "^4.0.0",
- "@types/node": "7.0.18",
+ "@types/node": "10.12.20",
"@types/source-map": "^0.5.1",
"@types/tmp": "^0.0.33",
"clang-format": "1.0.49",