Skip to content

Commit 765a5ae

Browse files
committed
fix(cli): bound capture runtime stages
1 parent 1738b5a commit 765a5ae

12 files changed

Lines changed: 548 additions & 113 deletions

‎packages/cli/src/capture/assetDownloader.test.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2-
import { isPrivateUrl, safeFetch, toStandaloneSvg } from "./assetDownloader.js";
2+
import { mkdtempSync, rmSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import {
6+
downloadAndRewriteFonts,
7+
isPrivateUrl,
8+
safeFetch,
9+
toStandaloneSvg,
10+
} from "./assetDownloader.js";
311

412
describe("isPrivateUrl — SSRF denylist (security: F-003)", () => {
513
it("blocks loopback, private, and metadata IPv4", () => {
@@ -127,3 +135,52 @@ describe("toStandaloneSvg — scraped inline SVGs must survive as .svg files", (
127135
expect(toStandaloneSvg("<div>not an svg</div>")).toBe("<div>not an svg</div>");
128136
});
129137
});
138+
139+
describe("downloadAndRewriteFonts — attempt caps", () => {
140+
afterEach(() => vi.unstubAllGlobals());
141+
142+
async function expectFailedFontAttempts(css: string, expectedAttempts: number): Promise<void> {
143+
const dir = mkdtempSync(join(tmpdir(), "hf-font-attempts-"));
144+
const fetchMock = vi.fn(async () => new Response("failed", { status: 503 }));
145+
vi.stubGlobal("fetch", fetchMock);
146+
147+
try {
148+
await downloadAndRewriteFonts(css, dir);
149+
expect(fetchMock).toHaveBeenCalledTimes(expectedAttempts);
150+
} finally {
151+
rmSync(dir, { recursive: true, force: true });
152+
}
153+
}
154+
155+
it("counts failed requests toward the global 30-font cap", async () => {
156+
const css = Array.from(
157+
{ length: 35 },
158+
(_, i) =>
159+
`@font-face { font-family: Family${i}; src: url(https://fonts${i}.example/font-${i}.woff2); }`,
160+
).join("\n");
161+
await expectFailedFontAttempts(css, 30);
162+
});
163+
164+
it("counts failed requests toward the six-attempt per-family cap", async () => {
165+
const css = Array.from(
166+
{ length: 10 },
167+
(_, i) =>
168+
`@font-face { font-family: Shared; src: url(https://fonts.example/font-${i}.woff2); }`,
169+
).join("\n");
170+
await expectFailedFontAttempts(css, 6);
171+
});
172+
173+
it("does not start a font request after the capture budget is exhausted", async () => {
174+
const dir = mkdtempSync(join(tmpdir(), "hf-font-budget-"));
175+
const css = "@font-face { font-family: Budget; src: url(https://fonts.example/budget.woff2); }";
176+
const fetchMock = vi.fn();
177+
vi.stubGlobal("fetch", fetchMock);
178+
179+
try {
180+
await downloadAndRewriteFonts(css, dir, { remainingMs: () => 0 });
181+
expect(fetchMock).not.toHaveBeenCalled();
182+
} finally {
183+
rmSync(dir, { recursive: true, force: true });
184+
}
185+
});
186+
});

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import { createHash } from "node:crypto";
1111
import type { DesignTokens, DownloadedAsset } from "./types.js";
1212
import type { CatalogedAsset } from "./assetCataloger.js";
1313

14+
interface DownloadBudgetOptions {
15+
remainingMs?: () => number;
16+
}
17+
1418
// SVGs: hash-of-bytes filename so it can't drift from content; label-derived names mis-assigned brands.
1519
function svgContentHashSlug(svgSource: string | Buffer, isLogo: boolean): string {
1620
const hash = createHash("sha1").update(svgSource).digest("hex").slice(0, 8);
@@ -43,11 +47,13 @@ export function toStandaloneSvg(outerHTML: string): string {
4347
return outerHTML.replace(original, tag);
4448
}
4549

50+
// fallow-ignore-next-line complexity
4651
export async function downloadAssets(
4752
tokens: DesignTokens,
4853
outputDir: string,
4954
catalogedAssets?: CatalogedAsset[],
5055
faviconLinks?: Array<{ rel: string; href: string }>,
56+
options: DownloadBudgetOptions = {},
5157
): Promise<DownloadedAsset[]> {
5258
const assetsDir = join(outputDir, "assets");
5359
mkdirSync(assetsDir, { recursive: true });
@@ -82,12 +88,14 @@ export async function downloadAssets(
8288

8389
// 2. Favicon
8490
for (const icon of faviconLinks || []) {
91+
const remainingMs = options.remainingMs?.() ?? 10_000;
92+
if (remainingMs <= 0) break;
8593
if (!icon.href) continue;
8694
try {
8795
const ext = extname(new URL(icon.href).pathname) || ".ico";
8896
const name = `favicon${ext}`;
8997
const localPath = `assets/${name}`;
90-
const buffer = await fetchBuffer(icon.href);
98+
const buffer = await fetchBuffer(icon.href, Math.min(10_000, remainingMs));
9199
if (buffer) {
92100
writeFileSync(join(outputDir, localPath), buffer);
93101
assets.push({ url: icon.href, localPath, type: "favicon" });
@@ -149,13 +157,15 @@ export async function downloadAssets(
149157
let imgIdx = 0;
150158
const usedNames = new Set<string>();
151159
for (let i = 0; i < toDownload.length; i += BATCH_SIZE) {
160+
const remainingMs = options.remainingMs?.() ?? 10_000;
161+
if (remainingMs <= 0) break;
152162
const batch = toDownload.slice(i, i + BATCH_SIZE);
153163
const results = await Promise.allSettled(
154164
batch.map(async ({ url, isPoster, catalog }) => {
155165
const parsedUrl = new URL(url);
156166
const pathExt = extname(parsedUrl.pathname);
157167
const ext = pathExt && pathExt.length <= 5 ? pathExt : ".jpg";
158-
const buffer = await fetchBuffer(url);
168+
const buffer = await fetchBuffer(url, Math.min(10_000, remainingMs));
159169
if (!buffer) return null;
160170
const isSvg = ext === ".svg" || url.includes(".svg");
161171
const minSize = isSvg ? 200 : 10000;
@@ -198,10 +208,12 @@ export async function downloadAssets(
198208

199209
// 4. OG image (if not already downloaded)
200210
if (tokens.ogImage && !downloadedUrls.has(normalizeUrl(tokens.ogImage))) {
211+
const remainingMs = options.remainingMs?.() ?? 10_000;
201212
try {
202213
const ext = extname(new URL(tokens.ogImage).pathname) || ".jpg";
203214
const localPath = `assets/og-image${ext}`;
204-
const buffer = await fetchBuffer(tokens.ogImage);
215+
const buffer =
216+
remainingMs > 0 ? await fetchBuffer(tokens.ogImage, Math.min(10_000, remainingMs)) : null;
205217
if (buffer && buffer.length > 5000) {
206218
writeFileSync(join(outputDir, localPath), buffer);
207219
assets.push({ url: tokens.ogImage, localPath, type: "image" });
@@ -234,7 +246,12 @@ function normalizeUrl(u: string): string {
234246
* Download fonts referenced in CSS and rewrite URLs to local paths.
235247
* Returns the modified CSS string with local font paths.
236248
*/
237-
export async function downloadAndRewriteFonts(css: string, outputDir: string): Promise<string> {
249+
// fallow-ignore-next-line complexity
250+
export async function downloadAndRewriteFonts(
251+
css: string,
252+
outputDir: string,
253+
options: DownloadBudgetOptions = {},
254+
): Promise<string> {
238255
const assetsDir = join(outputDir, "assets", "fonts");
239256
mkdirSync(assetsDir, { recursive: true });
240257

@@ -275,23 +292,25 @@ export async function downloadAndRewriteFonts(css: string, outputDir: string): P
275292
let count = 0;
276293

277294
for (const fontUrl of sortedUrls) {
295+
const remainingMs = options.remainingMs?.() ?? 10_000;
296+
if (remainingMs <= 0) break;
278297
if (count >= MAX_TOTAL_FONTS) break;
279298
const family = getFamilyForUrl(fontUrl);
280299
const familyCount = familyCounts.get(family) || 0;
281300
if (familyCount >= MAX_FONTS_PER_FAMILY) continue;
301+
familyCounts.set(family, familyCount + 1);
302+
count++;
282303

283304
try {
284305
const urlObj = new URL(fontUrl);
285306
const filename = urlObj.pathname.split("/").pop() || `font-${count}.woff2`;
286307
const localPath = join(assetsDir, filename);
287308
const relativePath = `assets/fonts/${filename}`;
288309

289-
const buffer = await fetchBuffer(fontUrl);
310+
const buffer = await fetchBuffer(fontUrl, Math.min(10_000, remainingMs));
290311
if (buffer) {
291312
writeFileSync(localPath, buffer);
292313
rewritten = rewritten.split(fontUrl).join(relativePath);
293-
familyCounts.set(family, familyCount + 1);
294-
count++;
295314
}
296315
} catch {
297316
/* skip */
@@ -391,10 +410,10 @@ export async function safeFetch(url: string, init?: RequestInit): Promise<Respon
391410
return null; // too many redirects
392411
}
393412

394-
async function fetchBuffer(url: string): Promise<Buffer | null> {
413+
async function fetchBuffer(url: string, timeoutMs = 10_000): Promise<Buffer | null> {
395414
try {
396415
const res = await safeFetch(url, {
397-
signal: AbortSignal.timeout(10000),
416+
signal: AbortSignal.timeout(timeoutMs),
398417
headers: { "User-Agent": "HyperFrames/1.0" },
399418
});
400419
if (!res || !res.ok) return null;

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface ContactSheetOptions {
1818
quality?: number;
1919
/** Target width per cell in pixels (default: 600) */
2020
cellWidth?: number;
21+
/** Cooperative boundary checked before starting each native Sharp page. */
22+
remainingMs?: () => number;
2123
}
2224

2325
/**
@@ -39,6 +41,8 @@ export async function createContactSheet(
3941
cellWidth = 600,
4042
} = opts;
4143

44+
if ((opts.remainingMs?.() ?? 1) <= 0) return null;
45+
4246
const files = imagePaths.slice(0, maxImages);
4347
if (files.length === 0) return null;
4448

@@ -125,6 +129,7 @@ function escapeXml(s: string): string {
125129
* Output files: basePath → base-1.jpg, base-2.jpg, ...
126130
* Returns the list of written file paths (empty if no images).
127131
*/
132+
// fallow-ignore-next-line complexity
128133
async function createContactSheetPages(
129134
imagePaths: string[],
130135
outputBasePath: string,
@@ -133,14 +138,15 @@ async function createContactSheetPages(
133138
customLabels?: string[],
134139
): Promise<string[]> {
135140
if (imagePaths.length === 0) return [];
136-
const { pageSize = imagePaths.length, ...sheetOpts } = opts;
141+
const { pageSize = imagePaths.length, remainingMs, ...sheetOpts } = opts;
137142
const ext = outputBasePath.match(/\.[^.]+$/)?.[0] ?? ".jpg";
138143
const base = outputBasePath.slice(0, -ext.length);
139144

140145
const pages = Math.ceil(imagePaths.length / pageSize);
141146
const results: string[] = [];
142147

143148
for (let p = 0; p < pages; p++) {
149+
if ((remainingMs?.() ?? 1) <= 0) break;
144150
const chunk = imagePaths.slice(p * pageSize, (p + 1) * pageSize);
145151
const chunkLabels = customLabels?.slice(p * pageSize, (p + 1) * pageSize);
146152
const outPath = pages === 1 ? outputBasePath : `${base}-${p + 1}${ext}`;
@@ -170,6 +176,7 @@ async function createContactSheetPages(
170176
export async function createScrollContactSheet(
171177
screenshotsDir: string,
172178
outputPath: string,
179+
budget: Pick<ContactSheetOptions, "remainingMs"> = {},
173180
): Promise<string[]> {
174181
if (!existsSync(screenshotsDir)) return [];
175182

@@ -189,7 +196,7 @@ export async function createScrollContactSheet(
189196
return createContactSheetPages(
190197
paths,
191198
outputPath,
192-
{ cols: 3, cellWidth: 600, pageSize: 9 },
199+
{ cols: 3, cellWidth: 600, pageSize: 9, ...budget },
193200
0,
194201
labels,
195202
);
@@ -203,6 +210,7 @@ export async function createScrollContactSheet(
203210
export async function createSnapshotContactSheet(
204211
snapshotsDir: string,
205212
outputPath: string,
213+
budget: Pick<ContactSheetOptions, "remainingMs"> = {},
206214
): Promise<string[]> {
207215
if (!existsSync(snapshotsDir)) return [];
208216

@@ -222,7 +230,7 @@ export async function createSnapshotContactSheet(
222230
return createContactSheetPages(
223231
paths,
224232
outputPath,
225-
{ cols: 3, cellWidth: 600, pageSize: 9 },
233+
{ cols: 3, cellWidth: 600, pageSize: 9, ...budget },
226234
0,
227235
labels,
228236
);
@@ -236,6 +244,7 @@ export async function createSnapshotContactSheet(
236244
export async function createAssetContactSheet(
237245
assetsDir: string,
238246
outputPath: string,
247+
budget: Pick<ContactSheetOptions, "remainingMs"> = {},
239248
): Promise<string[]> {
240249
if (!existsSync(assetsDir)) return [];
241250

@@ -254,6 +263,7 @@ export async function createAssetContactSheet(
254263
cellWidth: 480,
255264
labelMode: "filename",
256265
pageSize: 12,
266+
...budget,
257267
});
258268
}
259269

@@ -271,6 +281,7 @@ export async function createSvgContactSheet(
271281
svgsDir: string,
272282
outputPath: string,
273283
assetsRootDir?: string,
284+
budget: Pick<ContactSheetOptions, "remainingMs"> = {},
274285
): Promise<string[]> {
275286
const dirsToScan = [svgsDir, assetsRootDir].filter(
276287
(d): d is string => d !== undefined && existsSync(d),
@@ -302,6 +313,7 @@ export async function createSvgContactSheet(
302313
const labels: string[] = [];
303314

304315
for (let i = 0; i < svgPaths.length; i++) {
316+
if ((budget.remainingMs?.() ?? 1) <= 0) break;
305317
const svgPath = svgPaths[i]!;
306318
const tmpPath = join(tmpDir, `.thumb-${i}.png`);
307319
try {
@@ -334,6 +346,7 @@ export async function createSvgContactSheet(
334346
cols: 5,
335347
cellWidth: thumbSize,
336348
pageSize: 15,
349+
...budget,
337350
},
338351
0,
339352
labels,

0 commit comments

Comments
 (0)