import readline from "node:readline"; import { encodeWith, installBrowserStubs, loadVendor } from "./h5_vendor_encode.mjs"; const cookie = process.env.KS_COOKIE || ""; const exportName = process.env.KS_VENDOR_EXPORT || "f"; installBrowserStubs(cookie); const realConsole = { log: console.log, warn: console.warn, error: console.error }; if (!process.env.KS_VENDOR_DEBUG) { console.log = () => {}; console.warn = () => {}; console.error = () => {}; } const vendor = await loadVendor(); console.log = realConsole.log; console.warn = realConsole.warn; console.error = realConsole.error; const instance = vendor[exportName]; if (!instance || typeof instance.call !== "function") { throw new Error(`vendor export ${exportName} has no call()`); } const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity, }); function writeJson(value) { process.stdout.write(`${JSON.stringify(value)}\n`); } rl.on("line", async (line) => { if (!line.trim()) { return; } let req; try { req = JSON.parse(line); const signInput = String(req.signInput ?? ""); const encoded = await encodeWith(instance, signInput); writeJson({ id: req.id ?? null, ok: true, exportName, ...encoded }); } catch (error) { writeJson({ id: req && "id" in req ? req.id : null, ok: false, error: error && error.stack ? error.stack : String(error), }); } }); rl.on("close", () => { process.exit(0); });