Skip to content

Commit 41551c7

Browse files
authored
fix(cli): read caption images through checked file descriptors (#3724)
1 parent cfdaccb commit 41551c7

2 files changed

Lines changed: 124 additions & 4 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import * as fs from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { captionImagesWithGemini } from "./contentExtractor.js";
6+
7+
const hooks = vi.hoisted(() => ({
8+
afterStat: () => {},
9+
beforeRead: () => {},
10+
descriptor: -1,
11+
}));
12+
13+
vi.mock("node:fs", async (importOriginal) => {
14+
const actual = await importOriginal<typeof fs>();
15+
return {
16+
...actual,
17+
statSync: (path: fs.PathLike) => {
18+
const stat = actual.statSync(path);
19+
hooks.afterStat();
20+
return stat;
21+
},
22+
fstatSync: (fd: number) => {
23+
hooks.descriptor = fd;
24+
const stat = actual.fstatSync(fd);
25+
hooks.afterStat();
26+
return stat;
27+
},
28+
readFileSync: (path: fs.PathOrFileDescriptor) => {
29+
hooks.beforeRead();
30+
return actual.readFileSync(path);
31+
},
32+
};
33+
});
34+
35+
describe("caption image file reads", () => {
36+
let dir: string;
37+
let file: string;
38+
let sentBody: string;
39+
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
40+
sentBody = typeof init?.body === "string" ? init.body : "";
41+
return new Response(JSON.stringify({ choices: [{ message: { content: "A hero." } }] }));
42+
});
43+
44+
beforeEach(() => {
45+
dir = fs.mkdtempSync(join(tmpdir(), "hf-caption-read-"));
46+
fs.mkdirSync(join(dir, "assets"));
47+
file = join(dir, "assets", "hero.png");
48+
fs.writeFileSync(file, "original image");
49+
hooks.afterStat = () => {};
50+
hooks.beforeRead = () => {};
51+
hooks.descriptor = -1;
52+
sentBody = "";
53+
fetchMock.mockClear();
54+
vi.stubEnv("OPENROUTER_API_KEY", "test-key");
55+
vi.stubGlobal("fetch", fetchMock);
56+
});
57+
58+
afterEach(() => {
59+
hooks.afterStat = () => {};
60+
hooks.beforeRead = () => {};
61+
vi.unstubAllGlobals();
62+
vi.unstubAllEnvs();
63+
fs.rmSync(dir, { recursive: true, force: true });
64+
});
65+
66+
async function expectClosed() {
67+
const actual = await vi.importActual<typeof fs>("node:fs");
68+
expect(hooks.descriptor).toBeGreaterThanOrEqual(0);
69+
expect(() => actual.fstatSync(hooks.descriptor)).toThrow(/EBADF/);
70+
}
71+
72+
it("reads the checked image when its directory entry is replaced", async () => {
73+
hooks.afterStat = () => {
74+
fs.renameSync(file, join(dir, "original.png"));
75+
fs.writeFileSync(file, "replacement image");
76+
};
77+
expect(await captionImagesWithGemini(dir, () => {}, [])).toEqual({ "hero.png": "A hero." });
78+
expect(sentBody).toContain(Buffer.from("original image").toString("base64"));
79+
expect(sentBody).not.toContain(Buffer.from("replacement image").toString("base64"));
80+
await expectClosed();
81+
});
82+
83+
it.each([4_000_000, 4_000_001])("preserves the inline size limit for %i bytes", async (size) => {
84+
fs.writeFileSync(file, Buffer.alloc(size));
85+
const read = vi.fn();
86+
hooks.beforeRead = read;
87+
const captions = await captionImagesWithGemini(dir, () => {}, []);
88+
expect(captions).toEqual(size === 4_000_000 ? { "hero.png": "A hero." } : {});
89+
expect(fetchMock).toHaveBeenCalledTimes(size === 4_000_000 ? 1 : 0);
90+
expect(read).toHaveBeenCalledTimes(size === 4_000_000 ? 1 : 0);
91+
await expectClosed();
92+
});
93+
94+
it.each(["stat", "read"])("closes the image and reports a failed %s", async (step) => {
95+
const fail = () => {
96+
throw new Error("Injected file failure");
97+
};
98+
if (step === "stat") hooks.afterStat = fail;
99+
else hooks.beforeRead = fail;
100+
const warnings: string[] = [];
101+
expect(await captionImagesWithGemini(dir, () => {}, warnings)).toEqual({});
102+
expect(warnings).toEqual(["OpenRouter vision failed for 1 asset(s); captions omitted."]);
103+
expect(fetchMock).not.toHaveBeenCalled();
104+
await expectClosed();
105+
});
106+
});

‎packages/cli/src/capture/contentExtractor.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
*/
1010

1111
import type { Page } from "puppeteer-core";
12-
import { existsSync, readdirSync, statSync, readFileSync } from "node:fs";
12+
import {
13+
existsSync,
14+
readdirSync,
15+
statSync,
16+
readFileSync,
17+
openSync,
18+
fstatSync,
19+
closeSync,
20+
} from "node:fs";
1321
import { basename, join } from "node:path";
1422
import type sharpType from "sharp";
1523
import type { CatalogedAsset } from "./assetCataloger.js";
@@ -446,9 +454,15 @@ export async function captionImagesWithGemini(
446454
const results = await Promise.allSettled(
447455
batch.map(async (file: string) => {
448456
const filePath = join(outputDir, "assets", file);
449-
const stat = statSync(filePath);
450-
if (stat.size > 4_000_000) return { file, caption: "" }; // skip images > 4 MB (provider inline limit)
451-
const buffer = readFileSync(filePath);
457+
const fd = openSync(filePath, "r");
458+
let buffer: Buffer;
459+
try {
460+
const stat = fstatSync(fd);
461+
if (stat.size > 4_000_000) return { file, caption: "" }; // skip images > 4 MB (provider inline limit)
462+
buffer = readFileSync(fd);
463+
} finally {
464+
closeSync(fd);
465+
}
452466
const base64 = buffer.toString("base64");
453467
const ext = file.split(".").pop()?.toLowerCase() || "png";
454468
const mimeType = ext === "jpg" ? "image/jpeg" : `image/${ext}`;

0 commit comments

Comments
 (0)