Skip to content

Commit a5f0b05

Browse files
committed
fix: address PR review comments for stdio discovery
- Add proper error handling for non-Error objects - Define MANIFEST_PREFIX and MANIFEST_ERROR_PREFIX constants - Fix HTTP discovery retry logic regression in tests - Make regex patterns multiline-safe for manifest and error parsing - Add timeout handling for stdio discovery in tests
1 parent 0c2da1f commit a5f0b05

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

scripts/bin-test/test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ async function runHttpDiscovery(modulePath: string): Promise<DiscoveryResult> {
163163
return true;
164164
} catch (e: unknown) {
165165
const error = e as { code?: string };
166-
return error.code !== "ECONNREFUSED";
166+
if (error.code === "ECONNREFUSED") {
167+
// This is an expected error during server startup, so we should retry.
168+
return false;
169+
}
170+
// Any other error is unexpected and should fail the test immediately.
171+
throw e;
167172
}
168173
}, TIMEOUT_L);
169174

@@ -199,9 +204,15 @@ async function runStdioDiscovery(modulePath: string): Promise<DiscoveryResult> {
199204
stderr += chunk.toString("utf8");
200205
});
201206

207+
const timeoutId = setTimeout(() => {
208+
proc.kill(9);
209+
reject(new Error("Stdio discovery timed out after " + TIMEOUT_M + "ms"));
210+
}, TIMEOUT_M);
211+
202212
proc.on("close", () => {
213+
clearTimeout(timeoutId);
203214
// Try to parse manifest
204-
const manifestMatch = stderr.match(/__FIREBASE_FUNCTIONS_MANIFEST__:(.+)/);
215+
const manifestMatch = stderr.match(/__FIREBASE_FUNCTIONS_MANIFEST__:([\s\S]+)/);
205216
if (manifestMatch) {
206217
const base64 = manifestMatch[1];
207218
const manifestJson = Buffer.from(base64, "base64").toString("utf8");
@@ -211,7 +222,7 @@ async function runStdioDiscovery(modulePath: string): Promise<DiscoveryResult> {
211222
}
212223

213224
// Try to parse error
214-
const errorMatch = stderr.match(/__FIREBASE_FUNCTIONS_MANIFEST_ERROR__:(.+)/);
225+
const errorMatch = stderr.match(/__FIREBASE_FUNCTIONS_MANIFEST_ERROR__:([\s\S]+)/);
215226
if (errorMatch) {
216227
resolve({ success: false, error: errorMatch[1] });
217228
return;
@@ -221,6 +232,7 @@ async function runStdioDiscovery(modulePath: string): Promise<DiscoveryResult> {
221232
});
222233

223234
proc.on("error", (err) => {
235+
clearTimeout(timeoutId);
224236
reject(err);
225237
});
226238
});

src/bin/firebase-functions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,21 @@ if (args.length > 1) {
4949
functionsDir = args[0];
5050
}
5151

52+
const MANIFEST_PREFIX = "__FIREBASE_FUNCTIONS_MANIFEST__:";
53+
const MANIFEST_ERROR_PREFIX = "__FIREBASE_FUNCTIONS_MANIFEST_ERROR__:";
54+
5255
async function runStdioDiscovery() {
5356
try {
5457
const stack = await loadStack(functionsDir);
5558
const wireFormat = stackToWire(stack);
5659
const manifestJson = JSON.stringify(wireFormat);
5760
const base64 = Buffer.from(manifestJson).toString("base64");
58-
process.stderr.write(`__FIREBASE_FUNCTIONS_MANIFEST__:${base64}\n`);
61+
process.stderr.write(`${MANIFEST_PREFIX}${base64}\n`);
5962
process.exit(0);
6063
} catch (e) {
6164
console.error("Failed to generate manifest from function source:", e);
62-
const errorMessage = e instanceof Error ? e.message : String(e);
63-
process.stderr.write(`__FIREBASE_FUNCTIONS_MANIFEST_ERROR__:${errorMessage}\n`);
65+
const message = e instanceof Error ? e.message : String(e);
66+
process.stderr.write(`${MANIFEST_ERROR_PREFIX}${message}\n`);
6467
process.exit(1);
6568
}
6669
}

0 commit comments

Comments
 (0)