fix(qr): lazy load terminal ascii renderer

This commit is contained in:
Vincent Koc
2026-04-13 15:11:40 +01:00
parent 9387ec9933
commit 9763d446d9
2 changed files with 14 additions and 4 deletions

View File

@@ -7,7 +7,6 @@ import { resolveAmbientNodeProxyAgent } from "openclaw/plugin-sdk/extension-shar
import { danger, success } from "openclaw/plugin-sdk/runtime-env";
import { getChildLogger, toPinoLikeLogger } from "openclaw/plugin-sdk/runtime-env";
import { ensureDir, resolveUserPath } from "openclaw/plugin-sdk/text-runtime";
import qrcode from "qrcode-terminal";
import {
maybeRestoreCredsFromBackup,
readCredsJsonRaw,
@@ -37,6 +36,11 @@ export {
const LOGGED_OUT_STATUS = DisconnectReason?.loggedOut ?? 401;
async function loadQrTerminal() {
const mod = await import("qrcode-terminal");
return mod.default ?? mod;
}
// Per-authDir queues so multi-account creds saves don't block each other.
const credsSaveQueues = new Map<string, Promise<void>>();
const CREDS_SAVE_FLUSH_TIMEOUT_MS = 15_000;
@@ -139,13 +143,14 @@ export async function createWaSocket(
sock.ev.on("creds.update", () => enqueueSaveCreds(authDir, saveCreds, sessionLogger));
sock.ev.on(
"connection.update",
(update: Partial<import("@whiskeysockets/baileys").ConnectionState>) => {
async (update: Partial<import("@whiskeysockets/baileys").ConnectionState>) => {
try {
const { connection, lastDisconnect, qr } = update;
if (qr) {
opts.onQr?.(qr);
if (printQr) {
console.log("Scan this QR in WhatsApp (Linked Devices):");
const qrcode = await loadQrTerminal();
qrcode.generate(qr, { small: true });
}
}

View File

@@ -1,5 +1,4 @@
import type { Command } from "commander";
import qrcode from "qrcode-terminal";
import { loadConfig } from "../config/config.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { hasConfiguredSecretInput } from "../config/types.secrets.js";
@@ -24,7 +23,13 @@ type QrCliOptions = {
password?: string;
};
function renderQrAscii(data: string): Promise<string> {
async function loadQrTerminal() {
const mod = await import("qrcode-terminal");
return mod.default ?? mod;
}
async function renderQrAscii(data: string): Promise<string> {
const qrcode = await loadQrTerminal();
return new Promise((resolve) => {
qrcode.generate(data, { small: true }, (output: string) => {
resolve(output);