Skip to content

Commit 8075cba

Browse files
committed
fix(google): cancel unread response body on video download error
1 parent e2e394e commit 8075cba

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎extensions/google/video-generation-provider.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,46 @@ describe("google video generation provider", () => {
368368
).rejects.toThrow("Google generated video download exceeds 1 bytes");
369369
});
370370

371+
it("cancels response body on non-OK direct URI download before throwing", async () => {
372+
vi.spyOn(providerAuthRuntime, "resolveApiKeyForProvider").mockResolvedValue({
373+
apiKey: "google-key",
374+
source: "env",
375+
mode: "api-key",
376+
});
377+
generateVideosMock.mockResolvedValue({
378+
done: true,
379+
response: {
380+
generatedVideos: [
381+
{
382+
video: {
383+
uri: "https://generativelanguage.googleapis.com/v1beta/files/generated-video:download?alt=media",
384+
mimeType: "video/mp4",
385+
},
386+
},
387+
],
388+
},
389+
});
390+
const cancel = vi.fn();
391+
const body = new ReadableStream<Uint8Array>({ cancel });
392+
vi.stubGlobal(
393+
"fetch",
394+
vi.fn(async () => new Response(body, { status: 503, statusText: "Service Unavailable" })),
395+
);
396+
397+
const provider = buildGoogleVideoGenerationProvider();
398+
await expect(
399+
provider.generateVideo({
400+
provider: "google",
401+
model: "veo-3.1-fast-generate-preview",
402+
prompt: "A tiny robot watering a windowsill garden",
403+
cfg: {},
404+
durationSeconds: 3,
405+
}),
406+
).rejects.toThrow("Failed to download Google generated video: 503 Service Unavailable");
407+
408+
expect(cancel).toHaveBeenCalledOnce();
409+
});
410+
371411
it("downloads SDK file handles through the bounded REST media endpoint", async () => {
372412
vi.spyOn(providerAuthRuntime, "resolveApiKeyForProvider").mockResolvedValue({
373413
apiKey: "google-key",

‎extensions/google/video-generation-provider.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ async function downloadGeneratedVideoFromUri(params: {
245245
});
246246
try {
247247
if (!response.ok) {
248+
await response.body?.cancel().catch(() => undefined);
248249
throw new Error(
249250
`Failed to download Google generated video: ${response.status} ${response.statusText}`,
250251
);

0 commit comments

Comments
 (0)