mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-20 16:23:55 +02:00
fix(qr): lazy load terminal runtime modules
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import QRCodeModule from "qrcode-terminal/vendor/QRCode/index.js";
|
||||
import QRErrorCorrectLevelModule from "qrcode-terminal/vendor/QRCode/QRErrorCorrectLevel.js";
|
||||
import { encodePngRgba, fillPixel } from "./png-encode.ts";
|
||||
|
||||
type QRCodeConstructor = new (
|
||||
@@ -12,10 +10,26 @@ type QRCodeConstructor = new (
|
||||
isDark: (row: number, col: number) => boolean;
|
||||
};
|
||||
|
||||
const QRCode = QRCodeModule as QRCodeConstructor;
|
||||
const QRErrorCorrectLevel = QRErrorCorrectLevelModule;
|
||||
let qrCodeRuntimePromise: Promise<{
|
||||
QRCode: QRCodeConstructor;
|
||||
QRErrorCorrectLevel: Record<string, unknown>;
|
||||
}> | null = null;
|
||||
|
||||
function createQrMatrix(input: string) {
|
||||
async function loadQrCodeRuntime() {
|
||||
if (!qrCodeRuntimePromise) {
|
||||
qrCodeRuntimePromise = Promise.all([
|
||||
import("qrcode-terminal/vendor/QRCode/index.js"),
|
||||
import("qrcode-terminal/vendor/QRCode/QRErrorCorrectLevel.js"),
|
||||
]).then(([qrCodeModule, errorCorrectLevelModule]) => ({
|
||||
QRCode: qrCodeModule.default as QRCodeConstructor,
|
||||
QRErrorCorrectLevel: errorCorrectLevelModule.default,
|
||||
}));
|
||||
}
|
||||
return await qrCodeRuntimePromise;
|
||||
}
|
||||
|
||||
async function createQrMatrix(input: string) {
|
||||
const { QRCode, QRErrorCorrectLevel } = await loadQrCodeRuntime();
|
||||
const qr = new QRCode(-1, QRErrorCorrectLevel.L);
|
||||
qr.addData(input);
|
||||
qr.make();
|
||||
@@ -27,7 +41,7 @@ export async function renderQrPngBase64(
|
||||
opts: { scale?: number; marginModules?: number } = {},
|
||||
): Promise<string> {
|
||||
const { scale = 6, marginModules = 4 } = opts;
|
||||
const qr = createQrMatrix(input);
|
||||
const qr = await createQrMatrix(input);
|
||||
const modules = qr.getModuleCount();
|
||||
const size = (modules + marginModules * 2) * scale;
|
||||
|
||||
|
||||
13
src/types/qrcode-terminal.d.ts
vendored
13
src/types/qrcode-terminal.d.ts
vendored
@@ -1,3 +1,16 @@
|
||||
declare module "qrcode-terminal" {
|
||||
type GenerateOptions = {
|
||||
small?: boolean;
|
||||
};
|
||||
|
||||
type QrCodeTerminal = {
|
||||
generate: (input: string, options?: GenerateOptions, cb?: (output: string) => void) => void;
|
||||
};
|
||||
|
||||
const qrcode: QrCodeTerminal;
|
||||
export default qrcode;
|
||||
}
|
||||
|
||||
declare module "qrcode-terminal/vendor/QRCode/index.js" {
|
||||
const QRCode: unknown;
|
||||
export default QRCode;
|
||||
|
||||
Reference in New Issue
Block a user