Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions scripts/lib/orchestrator-gateway.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export async function verifyOrchestratorHealthz(healthzUrl, options = {}) {
let raw;
const reader = response.body?.getReader?.();
if (reader) {
const chunks = [];
const body = Buffer.allocUnsafe(HEALTH_BODY_LIMIT_BYTES);
let totalBytes = 0;
let completed = false;
try {
Expand All @@ -357,12 +357,12 @@ export async function verifyOrchestratorHealthz(healthzUrl, options = {}) {
completed = true;
break;
}
const chunk = Buffer.from(result.value ?? new Uint8Array());
totalBytes += chunk.length;
if (totalBytes > HEALTH_BODY_LIMIT_BYTES) {
const chunk = result.value ?? new Uint8Array();
if (totalBytes + chunk.byteLength > HEALTH_BODY_LIMIT_BYTES) {
throw new Error("contextual-orchestrator health response is too large");
}
chunks.push(chunk);
body.set(chunk, totalBytes);
totalBytes += chunk.byteLength;
}
} finally {
if (!completed) {
Expand All @@ -374,7 +374,7 @@ export async function verifyOrchestratorHealthz(healthzUrl, options = {}) {
}
reader.releaseLock();
}
raw = Buffer.concat(chunks, totalBytes);
raw = body.subarray(0, totalBytes);
} else {
raw = Buffer.from(
await Promise.race([response.arrayBuffer(), timeoutPromise]),
Expand Down
44 changes: 43 additions & 1 deletion test/orchestrator-gateway-stream-bound.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";

import { verifyOrchestratorHealthz } from "../scripts/lib/orchestrator-gateway.mjs";

Expand Down Expand Up @@ -48,4 +48,46 @@ describe("contextual-orchestrator streamed health response", () => {
expect(released).toBe(true);
expect(arrayBufferCalled).toBe(false);
});

it("does not retain fragmented chunks for a second concatenation allocation", async () => {
const payload = new TextEncoder().encode(
JSON.stringify({ status: "ok", service: "contextual-orchestrator" }),
);
let offset = 0;
let released = false;
const reader = {
async read() {
if (offset >= payload.length) return { done: true, value: undefined };
const value = payload.slice(offset, offset + 1);
offset += 1;
return { done: false, value };
},
async cancel() {},
releaseLock() {
released = true;
},
};
const response = {
ok: true,
status: 200,
headers: { get: () => null },
body: { getReader: () => reader },
async arrayBuffer() {
throw new Error("streaming response must not fall back to arrayBuffer");
},
} as unknown as Response;
const concatSpy = vi.spyOn(Buffer, "concat");

try {
await expect(
verifyOrchestratorHealthz("https://orchestrator.example/healthz", {
fetchImpl: (async () => response) as typeof fetch,
}),
).resolves.toEqual({ status: "ok", service: "contextual-orchestrator" });
expect(concatSpy).not.toHaveBeenCalled();
expect(released).toBe(true);
} finally {
concatSpy.mockRestore();
}
});
});
Loading