Repository navigation
fix(emulators:functions): buffer stdout/stderr independently and preserve multibyte UTF-8 across chunks - #11525
Conversation
…erve multibyte UTF-8 across chunks Fixes firebase#11522 Signed-off-by: Anjali Mishra <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request updates the functions emulator runtime worker to buffer stdout and stderr independently and correctly decode multi-byte UTF-8 characters split across chunks using StringDecoder. It also ensures that any remaining buffered data is flushed upon stream end and process exit, supported by new unit tests. The review feedback recommends removing unnecessary 'any' type assertions in both the source code and tests, and suggests extracting a unified helper function to reduce duplication in stream setup and buffer flushing.
| import * as http from "http"; | ||
| import { randomUUID } from "crypto"; | ||
| import { StringDecoder } from "node:string_decoder"; |
There was a problem hiding this comment.
Import Readable from node:stream to provide proper typing for the stream setup helper, avoiding any or Serializable for stream data.
| import * as http from "http"; | |
| import { randomUUID } from "crypto"; | |
| import { StringDecoder } from "node:string_decoder"; | |
| import * as http from "http"; | |
| import { randomUUID } from "crypto"; | |
| import { StringDecoder } from "node:string_decoder"; | |
| import { Readable } from "node:stream"; |
References
- Never use
anyorunknownas an escape hatch. Define proper interfaces/types or use type guards. (link)
| let stdoutBuffer = ""; | ||
| if (childProc.stdout) { | ||
| childProc.stdout.on("data", (data) => { | ||
| stdBuffer = this.processStream(data, stdBuffer); | ||
| const stdoutDecoder = new StringDecoder("utf8"); | ||
| childProc.stdout.on("data", (data: Serializable) => { | ||
| const text = Buffer.isBuffer(data) ? stdoutDecoder.write(data) : data; | ||
| stdoutBuffer = this.processStream(text, stdoutBuffer); | ||
| }); | ||
| childProc.stdout.on("end", () => { | ||
| const remaining = stdoutDecoder.end(); | ||
| if (remaining) { | ||
| stdoutBuffer = this.processStream(remaining, stdoutBuffer); | ||
| } | ||
| if (stdoutBuffer) { | ||
| this.flushBuffer(stdoutBuffer); | ||
| stdoutBuffer = ""; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| let stderrBuffer = ""; | ||
| if (childProc.stderr) { | ||
| childProc.stderr.on("data", (data) => { | ||
| stdBuffer = this.processStream(data, stdBuffer); | ||
| const stderrDecoder = new StringDecoder("utf8"); | ||
| childProc.stderr.on("data", (data: Serializable) => { | ||
| const text = Buffer.isBuffer(data) ? stderrDecoder.write(data) : data; | ||
| stderrBuffer = this.processStream(text, stderrBuffer); | ||
| }); | ||
| childProc.stderr.on("end", () => { | ||
| const remaining = stderrDecoder.end(); | ||
| if (remaining) { | ||
| stderrBuffer = this.processStream(remaining, stderrBuffer); | ||
| } | ||
| if (stderrBuffer) { | ||
| this.flushBuffer(stderrBuffer); | ||
| stderrBuffer = ""; | ||
| } | ||
| }); | ||
| } |
There was a problem hiding this comment.
The logic for handling stdout and stderr streams is almost identical. We can extract a local helper function setupStream and a unified flushAndClear helper to reduce duplication, improve maintainability, and ensure correct typing of stream data as Buffer | string instead of Serializable.
let stdoutBuffer = "";
let stderrBuffer = "";
const flushAndClear = (getBuffer: () => string, setBuffer: (val: string) => void) => {
const buf = getBuffer();
if (buf) {
this.flushBuffer(buf);
setBuffer("");
}
};
const setupStream = (
stream: Readable,
getBuffer: () => string,
setBuffer: (val: string) => void,
) => {
const decoder = new StringDecoder("utf8");
stream.on("data", (data: Buffer | string) => {
const text = Buffer.isBuffer(data) ? decoder.write(data) : data;
setBuffer(this.processStream(text, getBuffer()));
});
stream.on("end", () => {
const remaining = decoder.end();
if (remaining) {
setBuffer(this.processStream(remaining, getBuffer()));
}
flushAndClear(getBuffer, setBuffer);
});
};
if (childProc.stdout) {
setupStream(childProc.stdout, () => stdoutBuffer, (val) => { stdoutBuffer = val; });
}
if (childProc.stderr) {
setupStream(childProc.stderr, () => stderrBuffer, (val) => { stderrBuffer = val; });
}References
- Survey prior art & harmonize utilities: Extract and unify shared helpers when logic is duplicated across modules or warrants a new common utility. (link)
| if (stdoutBuffer) { | ||
| this.flushBuffer(stdoutBuffer); | ||
| stdoutBuffer = ""; | ||
| } | ||
| if (stderrBuffer) { | ||
| this.flushBuffer(stderrBuffer); | ||
| stderrBuffer = ""; | ||
| } | ||
| if (msgBuffer) { | ||
| this.flushBuffer(msgBuffer); | ||
| msgBuffer = ""; | ||
| } |
There was a problem hiding this comment.
Use the unified flushAndClear helper to clean up and flush the buffers on process exit, reducing duplication and manual state clearing.
flushAndClear(() => stdoutBuffer, (val) => { stdoutBuffer = val; });
flushAndClear(() => stderrBuffer, (val) => { stderrBuffer = val; });
flushAndClear(() => msgBuffer, (val) => { msgBuffer = val; });References
- Survey prior art & harmonize utilities: Extract and unify shared helpers when logic is duplicated across modules or warrants a new common utility. (link)
| runtime.process.stdout = stdout as any; | ||
| runtime.process.stderr = stderr as any; |
There was a problem hiding this comment.
Avoid using as any as an escape hatch. Since PassThrough is a subclass of Readable and Writable, it is fully compatible with ChildProcess.stdout and ChildProcess.stderr types, making the type assertion unnecessary.
| runtime.process.stdout = stdout as any; | |
| runtime.process.stderr = stderr as any; | |
| runtime.process.stdout = stdout; | |
| runtime.process.stderr = stderr; |
References
- Never use
anyorunknownas an escape hatch. Define proper interfaces/types or use type guards. (link)
| it("decodes multi-byte UTF-8 split across chunks without replacement characters", () => { | ||
| const runtime = new MockRuntimeInstance(); | ||
| const stdout = new PassThrough(); | ||
| runtime.process.stdout = stdout as any; |
There was a problem hiding this comment.
Avoid using as any as an escape hatch. PassThrough is compatible with ChildProcess.stdout without type assertions.
| runtime.process.stdout = stdout as any; | |
| runtime.process.stdout = stdout; |
References
- Never use
anyorunknownas an escape hatch. Define proper interfaces/types or use type guards. (link)
| it("flushes remaining buffer on stream end and process exit", async () => { | ||
| const runtime = new MockRuntimeInstance(); | ||
| const stdout = new PassThrough(); | ||
| runtime.process.stdout = stdout as any; |
There was a problem hiding this comment.
Avoid using as any as an escape hatch. PassThrough is compatible with ChildProcess.stdout without type assertions.
| runtime.process.stdout = stdout as any; | |
| runtime.process.stdout = stdout; |
References
- Never use
anyorunknownas an escape hatch. Define proper interfaces/types or use type guards. (link)
Description
Fixes #11522
In
src/emulator/functionsRuntimeWorker.ts,RuntimeWorkerpreviously passed bothchildProc.stdoutandchildProc.stderrchunks into a single sharedstdBuffer, decoding each buffer chunk independently withs.toString(). This led to two issues:\uFFFDreplacement characters because decoder state was not preserved across chunks.stdoutwrites without trailing newlines were concatenated withstderroutput into the shared buffer, merging separate log streams.This PR:
stdBufferinto distinctstdoutBufferandstderrBufferper worker.StringDecoder('utf8')per stream to buffer incomplete UTF-8 byte sequences until following chunks arrive.endand child processexit.Scenarios Tested
src/emulator/functionsRuntimeWorker.spec.ts:buffers stdout and stderr independentlydecodes multi-byte UTF-8 split across chunks without replacement charactersflushes remaining buffer on stream end and process exitsrc/emulator/functionsRuntimeWorker.spec.tspass.