Skip to content

Commit ac4fd2a

Browse files
committed
refactor(media): use qrcode buffer renderer
1 parent d8f705c commit ac4fd2a

7 files changed

Lines changed: 27 additions & 59 deletions

File tree

‎src/media/qr-image.test.ts‎

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import os from "node:os";
44
import path from "node:path";
55
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66

7-
const { MOCK_PNG_BASE64, MOCK_PNG_DATA_URL, toDataURL } = vi.hoisted(() => {
8-
const MOCK_PNG_BASE64Local = "ZmFrZXBuZw==";
9-
const MOCK_PNG_DATA_URLLocal = `data:image/png;base64,${MOCK_PNG_BASE64Local}`;
7+
const { MOCK_PNG_BASE64, MOCK_PNG_BUFFER, toBuffer } = vi.hoisted(() => {
8+
const MOCK_PNG_BUFFERLocal = Buffer.from("fakepng");
109
return {
11-
MOCK_PNG_BASE64: MOCK_PNG_BASE64Local,
12-
MOCK_PNG_DATA_URL: MOCK_PNG_DATA_URLLocal,
13-
toDataURL: vi.fn(async () => MOCK_PNG_DATA_URLLocal),
10+
MOCK_PNG_BASE64: MOCK_PNG_BUFFERLocal.toString("base64"),
11+
MOCK_PNG_BUFFER: MOCK_PNG_BUFFERLocal,
12+
toBuffer: vi.fn(async () => MOCK_PNG_BUFFERLocal),
1413
};
1514
});
1615

1716
vi.mock("qrcode", () => ({
1817
default: {
19-
toDataURL,
18+
toBuffer,
2019
},
2120
}));
2221

@@ -31,8 +30,8 @@ describe("renderQrPngBase64", () => {
3130
const tmpRoot = path.join(os.tmpdir(), "openclaw-qr-image-tests");
3231

3332
beforeEach(() => {
34-
toDataURL.mockClear();
35-
toDataURL.mockResolvedValue(MOCK_PNG_DATA_URL);
33+
toBuffer.mockClear();
34+
toBuffer.mockResolvedValue(MOCK_PNG_BUFFER);
3635
});
3736

3837
afterEach(async () => {
@@ -43,28 +42,25 @@ describe("renderQrPngBase64", () => {
4342
await expect(renderQrPngBase64("openclaw", { scale: 8, marginModules: 2 })).resolves.toBe(
4443
MOCK_PNG_BASE64,
4544
);
46-
expect(toDataURL).toHaveBeenCalledWith("openclaw", {
45+
expect(toBuffer).toHaveBeenCalledWith("openclaw", {
4746
margin: 2,
4847
scale: 8,
49-
type: "image/png",
5048
});
5149
});
5250

5351
it("uses the default PNG rendering options", async () => {
5452
await renderQrPngBase64("openclaw");
55-
expect(toDataURL).toHaveBeenCalledWith("openclaw", {
53+
expect(toBuffer).toHaveBeenCalledWith("openclaw", {
5654
margin: 4,
5755
scale: 6,
58-
type: "image/png",
5956
});
6057
});
6158

6259
it("floors finite PNG rendering options before delegating", async () => {
6360
await renderQrPngBase64("openclaw", { scale: 8.9, marginModules: 2.9 });
64-
expect(toDataURL).toHaveBeenCalledWith("openclaw", {
61+
expect(toBuffer).toHaveBeenCalledWith("openclaw", {
6562
margin: 2,
6663
scale: 8,
67-
type: "image/png",
6864
});
6965
});
7066

@@ -77,14 +73,7 @@ describe("renderQrPngBase64", () => {
7773
["marginModules", 6, Number.POSITIVE_INFINITY, "marginModules must be a finite number."],
7874
])("rejects invalid %s values", async (_name, scale, marginModules, message) => {
7975
await expect(renderQrPngBase64("openclaw", { scale, marginModules })).rejects.toThrow(message);
80-
expect(toDataURL).not.toHaveBeenCalled();
81-
});
82-
83-
it("rejects non-PNG qrcode data URLs", async () => {
84-
toDataURL.mockResolvedValue("data:image/svg+xml;base64,PHN2Zz4=");
85-
await expect(renderQrPngBase64("openclaw")).rejects.toThrow(
86-
"Expected qrcode to return a PNG data URL.",
87-
);
76+
expect(toBuffer).not.toHaveBeenCalled();
8877
});
8978

9079
it("formats QR PNG data URLs", async () => {
@@ -120,6 +109,6 @@ describe("renderQrPngBase64", () => {
120109
fileName: opts.fileName,
121110
}),
122111
).rejects.toThrow(`${name} must be a non-empty filename segment.`);
123-
expect(toDataURL).not.toHaveBeenCalled();
112+
expect(toBuffer).not.toHaveBeenCalled();
124113
});
125114
});

‎src/media/qr-image.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// QR image helpers generate QR code image files for media delivery.
22
import path from "node:path";
33
import { tempWorkspace } from "../infra/private-temp-workspace.js";
4-
import { loadQrCodeRuntime, normalizeQrText } from "./qr-runtime.ts";
4+
import { loadQrCodeRuntime } from "./qr-runtime.ts";
55

66
const DEFAULT_QR_PNG_SCALE = 6;
77
const DEFAULT_QR_PNG_MARGIN_MODULES = 4;
@@ -76,15 +76,11 @@ export async function renderQrPngBase64(
7676
max: MAX_QR_PNG_MARGIN_MODULES,
7777
});
7878
const qrCode = await loadQrCodeRuntime();
79-
const dataUrl = await qrCode.toDataURL(normalizeQrText(input), {
79+
const png = await qrCode.toBuffer(input, {
8080
margin: marginModules,
8181
scale,
82-
type: "image/png",
8382
});
84-
if (!dataUrl.startsWith(QR_PNG_DATA_URL_PREFIX)) {
85-
throw new Error("Expected qrcode to return a PNG data URL.");
86-
}
87-
return dataUrl.slice(QR_PNG_DATA_URL_PREFIX.length);
83+
return png.toString("base64");
8884
}
8985

9086
/** Wraps PNG base64 in the exact data URL prefix expected by chat/media callers. */

‎src/media/qr-runtime.ts‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// QR runtime helpers lazily load QR code generation and normalize QR text.
1+
// QR runtime helpers lazily load QR code generation.
22
import type QRCode from "qrcode";
33
import { createLazyImportLoader } from "../shared/lazy-promise.js";
44

@@ -12,14 +12,3 @@ const qrCodeRuntimeLoader = createLazyImportLoader<QrCodeRuntime>(() =>
1212
export async function loadQrCodeRuntime(): Promise<QrCodeRuntime> {
1313
return await qrCodeRuntimeLoader.load();
1414
}
15-
16-
/** Validates QR text before passing it to the renderer runtime. */
17-
export function normalizeQrText(text: string): string {
18-
if (typeof text !== "string") {
19-
throw new TypeError("QR text must be a string.");
20-
}
21-
if (text.length === 0) {
22-
throw new Error("QR text must not be empty.");
23-
}
24-
return text;
25-
}

‎src/media/qr-terminal.render.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ function decodeCompactQr(output: string, size: number): boolean[] {
5454
}
5555

5656
describe("renderQrTerminal (real qrcode runtime)", () => {
57+
it("rejects empty input through qrcode", async () => {
58+
await expect(renderQrTerminal("")).rejects.toThrow("No input text");
59+
});
60+
5761
it("keeps per-row ANSI sequence counts in line with typical rows", async () => {
5862
const sample = "https://wa.me/login/2@SAMPLE-TOKEN-1234567890ABCDEF";
5963
const rendered = await renderQrTerminal(sample);

‎src/media/qr-terminal.test.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,4 @@ describe("renderQrTerminal", () => {
4040
expect(create).toHaveBeenCalledWith("openclaw");
4141
expect(toString).not.toHaveBeenCalled();
4242
});
43-
44-
it("rejects empty QR text", async () => {
45-
await expect(renderQrTerminal("")).rejects.toThrow("QR text must not be empty.");
46-
expect(toString).not.toHaveBeenCalled();
47-
});
4843
});

‎src/media/qr-terminal.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// QR terminal helpers render QR codes for terminal output.
2-
import { loadQrCodeRuntime, normalizeQrText } from "./qr-runtime.ts";
2+
import { loadQrCodeRuntime } from "./qr-runtime.ts";
33

44
type QrTerminalModules = {
55
data: ArrayLike<boolean | number>;
@@ -50,13 +50,12 @@ export async function renderQrTerminal(
5050
input: string,
5151
opts: { small?: boolean } = {},
5252
): Promise<string> {
53-
const text = normalizeQrText(input);
5453
const qrCode = await loadQrCodeRuntime();
5554
if (opts.small === true) {
5655
// Avoid qrcode's small terminal mode so we control quiet-zone size and ANSI reset placement.
57-
return renderCompactTerminalQr(qrCode.create(text).modules);
56+
return renderCompactTerminalQr(qrCode.create(input).modules);
5857
}
59-
return await qrCode.toString(text, {
58+
return await qrCode.toString(input, {
6059
small: false,
6160
type: "terminal",
6261
});

‎src/types/qrcode.d.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,15 @@ declare module "qrcode" {
4242
export function toString(text: string, options?: QrCodeRenderOptions): Promise<string>;
4343
/** Render a QR code to a data URL. */
4444
export function toDataURL(text: string, options?: QrCodeRenderOptions): Promise<string>;
45-
/** Render a QR code to a file. */
46-
export function toFile(
47-
filePath: string,
48-
text: string,
49-
options?: QrCodeRenderOptions,
50-
): Promise<void>;
45+
/** Render a QR code to a PNG buffer. */
46+
export function toBuffer(text: string, options?: QrCodeRenderOptions): Promise<Buffer>;
5147

5248
/** Default qrcode export with the functions OpenClaw uses. */
5349
const qrcode: {
5450
create: typeof create;
5551
toString: typeof toString;
5652
toDataURL: typeof toDataURL;
57-
toFile: typeof toFile;
53+
toBuffer: typeof toBuffer;
5854
};
5955

6056
export default qrcode;

0 commit comments

Comments
 (0)