/** * A stand-in gateway that stays connected and answers printer provisioning, * for scripts/e2e-provision.sh. * * With TELEMETRY_AFTER_PROVISION=1 it also reports each provisioned printer * (running, then failed a few seconds later), so the console has something to * alert on. * * Needs FARM_ID in the environment (the hello must name the token's farm). * * Usage: bun scripts/fake-gateway-serve.ts [refuse-access-code] * * Every provision_printer / remove_printer frame it receives is appended to * as one JSON line, so the script can prove the credential * arrived here. A provision whose access_code equals is * answered as a failure, like a printer that rejected the login. */ import { appendFileSync } from "node:fs"; const [url, jwt, seconds, framesFile, refuseCode] = process.argv.slice(2); if (!url || !jwt || !seconds || !framesFile) { console.error("usage: fake-gateway-serve.ts [refuse-code]"); process.exit(2); } const ws = new WebSocket(url, { headers: { authorization: `Bearer ${jwt}` } } as never); ws.onerror = () => { console.error("socket error"); process.exit(1); }; ws.onopen = () => { // farm_id is required by the hello schema; the backend checks it against the token's farm. ws.send(JSON.stringify({ type: "hello", gateway_id: "fake-gw", farm_id: process.env.FARM_ID })); console.log("ready"); }; ws.onmessage = (event) => { const message = JSON.parse(String(event.data)); if (message.type !== "provision_printer" && message.type !== "remove_printer") return; appendFileSync(framesFile, JSON.stringify(message) + "\n"); if (message.type === "provision_printer" && process.env.TELEMETRY_AFTER_PROVISION === "1") { const id = message.printer.id; const report = (state: string) => ws.send(JSON.stringify({ type: "telemetry", printers: [{ printer_id: id, name: message.printer.name, state, percent: 40, print_error: 0 }] })); setTimeout(() => report("RUNNING"), 1500); setTimeout(() => report("RUNNING"), 3000); setTimeout(() => report("FAILED"), 4500); } const refused = message.type === "provision_printer" && refuseCode && message.printer.access_code === refuseCode; ws.send( JSON.stringify({ type: "command_result", command_id: message.request_id, ok: !refused, ...(refused ? { error: "could not connect to the printer: the access code was rejected" } : {}), }), ); }; setTimeout(() => process.exit(0), Number(seconds) * 1000);