Skip to content

Commit 38f8f92

Browse files
committed
feat(cli): expose deterministic font localization
1 parent 744146e commit 38f8f92

6 files changed

Lines changed: 148 additions & 1 deletion

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env node
2+
3+
import { runtimeVersionError } from "../dist/runtimeVersion.js";
4+
5+
const error = runtimeVersionError(process.versions.node);
6+
if (error) {
7+
console.error(error);
8+
process.exitCode = 1;
9+
} else {
10+
await import("../dist/fontLocalizeCli.js");
11+
}

‎packages/cli/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"directory": "packages/cli"
1010
},
1111
"bin": {
12-
"hyperframes": "./bin/hyperframes.mjs"
12+
"hyperframes": "./bin/hyperframes.mjs",
13+
"hyperframes-localize-fonts": "./bin/hyperframes-localize-fonts.mjs"
1314
},
1415
"files": [
1516
"bin",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { runFontLocalize, type FontLocalizeIo } from "./fontLocalize.js";
3+
4+
function makeIo(input: string): {
5+
io: FontLocalizeIo;
6+
output: string[];
7+
errors: string[];
8+
} {
9+
const output: string[] = [];
10+
const errors: string[] = [];
11+
return {
12+
io: {
13+
readInput: async () => input,
14+
writeOutput: (value) => output.push(value),
15+
writeError: (value) => errors.push(value),
16+
},
17+
output,
18+
errors,
19+
};
20+
}
21+
22+
describe("runFontLocalize", () => {
23+
it("writes only the localized document to stdout", async () => {
24+
const harness = makeIo("<html>source</html>");
25+
const localize = vi.fn(async () => "<html>localized</html>");
26+
27+
const exitCode = await runFontLocalize(harness.io, localize);
28+
29+
expect(exitCode).toBe(0);
30+
expect(localize).toHaveBeenCalledWith("<html>source</html>");
31+
expect(harness.output).toEqual(["<html>localized</html>"]);
32+
expect(harness.errors).toEqual([]);
33+
});
34+
35+
it("rejects blank input without calling the resolver", async () => {
36+
const harness = makeIo(" \n");
37+
const localize = vi.fn(async (html: string) => html);
38+
39+
const exitCode = await runFontLocalize(harness.io, localize);
40+
41+
expect(exitCode).toBe(2);
42+
expect(localize).not.toHaveBeenCalled();
43+
expect(harness.output).toEqual([]);
44+
expect(harness.errors.join(" ")).toContain("input is empty");
45+
});
46+
47+
it("fails without echoing source HTML or resolver details", async () => {
48+
const source = '<html><img src="https://signed.example/secret"></html>';
49+
const harness = makeIo(source);
50+
const localize = vi.fn(async () => {
51+
throw new Error(`fetch failed for ${source}`);
52+
});
53+
54+
const exitCode = await runFontLocalize(harness.io, localize);
55+
56+
expect(exitCode).toBe(1);
57+
expect(harness.output).toEqual([]);
58+
expect(harness.errors.join(" ")).toContain("font localization failed (Error)");
59+
expect(harness.errors.join(" ")).not.toContain("signed.example");
60+
expect(harness.errors.join(" ")).not.toContain("<html>");
61+
});
62+
63+
it("fails closed when the resolver returns an empty document", async () => {
64+
const harness = makeIo("<html>source</html>");
65+
66+
const exitCode = await runFontLocalize(harness.io, async () => "\n");
67+
68+
expect(exitCode).toBe(1);
69+
expect(harness.output).toEqual([]);
70+
expect(harness.errors.join(" ")).toContain("empty output");
71+
});
72+
});

‎packages/cli/src/fontLocalize.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export interface FontLocalizeIo {
2+
readInput(): Promise<string>;
3+
writeOutput(value: string): void;
4+
writeError(value: string): void;
5+
}
6+
7+
function safeErrorName(error: unknown): string {
8+
const name = error instanceof Error ? error.name : "UnknownError";
9+
return /^[A-Za-z][A-Za-z0-9]*$/.test(name) ? name : "Error";
10+
}
11+
12+
/**
13+
* Machine-only stdin/stdout boundary for deterministic font localization.
14+
* Source HTML and resolver messages can contain signed URLs, so failures emit
15+
* only a fixed category plus a sanitized error class.
16+
*/
17+
export async function runFontLocalize(
18+
io: FontLocalizeIo,
19+
localize: (html: string) => Promise<string>,
20+
): Promise<number> {
21+
const html = await io.readInput();
22+
if (!html.trim()) {
23+
io.writeError("font localization input is empty\n");
24+
return 2;
25+
}
26+
27+
try {
28+
const localized = await localize(html);
29+
if (!localized.trim()) {
30+
io.writeError("font localization failed (Error): empty output\n");
31+
return 1;
32+
}
33+
io.writeOutput(localized);
34+
return 0;
35+
} catch (error) {
36+
io.writeError(`font localization failed (${safeErrorName(error)})\n`);
37+
return 1;
38+
}
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { injectDeterministicFontFaces } from "@hyperframes/producer";
2+
import { runFontLocalize } from "./fontLocalize.js";
3+
4+
async function readStdin(): Promise<string> {
5+
const chunks: Buffer[] = [];
6+
for await (const chunk of process.stdin) {
7+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
8+
}
9+
return Buffer.concat(chunks).toString("utf8");
10+
}
11+
12+
process.exitCode = await runFontLocalize(
13+
{
14+
readInput: readStdin,
15+
writeOutput: (value) => process.stdout.write(value),
16+
writeError: (value) => process.stderr.write(value),
17+
},
18+
(html) =>
19+
injectDeterministicFontFaces(html, {
20+
failClosedFontFetch: true,
21+
allowSystemFontCapture: false,
22+
}),
23+
);

‎packages/cli/tsup.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url),
1010
export default defineConfig({
1111
entry: {
1212
cli: "src/cli.ts",
13+
fontLocalizeCli: "src/fontLocalizeCli.ts",
1314
runtimeVersion: "src/runtimeVersion.ts",
1415
shaderTransitionWorker: "../producer/src/services/shaderTransitionWorker.ts",
1516
},

0 commit comments

Comments
 (0)