fix(devices): USB printing dropped the job tail — chunked write loop

Field bug (ICS XP-K200L over USB): text printed, barcode + cut missing;
same bytes over TCP fine. sendRawUsb did ONE write() on an O_NONBLOCK
usblp fd and never checked bytesWritten — the kernel accepts only what
fits the printer's ~8 KB USB buffer and returns a short write, so the
tail of any job bigger than one buffer (the barcode mid-payload, the cut
at the end) was silently discarded. The regular-file test stand-in can't
short-write, which is why tests never caught it.

writeAllUsb now pushes 4 KB chunks until every byte is accepted,
continues after partial writes, retries EAGAIN/zero-byte with a short
pause, and fails at the deadline with an (N/M bytes) diagnostic. Driven
by fake-handle tests (short writes, EAGAIN interleave, wedged-printer
timeout, non-EAGAIN passthrough).

Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
2026-07-06 12:36:14 +02:00
parent 7ef332999e
commit 81bc2e357c
3 changed files with 140 additions and 6 deletions
@@ -10,6 +10,7 @@ import {
probeUsb,
sendRawUsb,
transportFromConfig,
writeAllUsb,
stamp,
} from "./printer-escpos.js";
@@ -183,3 +184,63 @@ describe("stamp (Albanian date format)", () => {
expect(stamp("not-a-date")).toBe("not-a-date");
});
});
describe("writeAllUsb — the char device's partial writes / EAGAIN (field bug 2026-07-06)", () => {
// A NONBLOCK usblp fd accepts only what fits the printer's USB buffer per write.
// The old single-write path dropped everything past the first buffer — the ICS
// XP-K200L printed the ticket's text head but lost the barcode and the cut. A
// regular file can't reproduce that, so these drive the loop with a fake handle.
/** Accepts at most `cap` bytes per call; records everything accepted in order. */
function slowHandle(cap: number) {
const chunks: Buffer[] = [];
return {
chunks,
write(buffer: Buffer, offset: number, length: number) {
const n = Math.min(cap, length);
chunks.push(Buffer.from(buffer.subarray(offset, offset + n)));
return Promise.resolve({ bytesWritten: n });
},
};
}
it("delivers the WHOLE payload across many short writes (barcode + cut included)", async () => {
const payload = renderTicket({ ticketId: "12345678901", issuedAt: "2026-06-21T10:00:00.000Z" });
const h = slowHandle(100); // way smaller than the job → many partial writes
await writeAllUsb(h, payload, Date.now() + 2000);
expect(Buffer.concat(h.chunks).equals(payload)).toBe(true);
});
it("retries EAGAIN (buffer full) until the kernel accepts the rest", async () => {
const payload = Buffer.from("x".repeat(300));
let calls = 0;
const accepted: Buffer[] = [];
const h = {
write(buffer: Buffer, offset: number, length: number) {
calls++;
if (calls % 2 === 0) {
const err = new Error("EAGAIN") as NodeJS.ErrnoException;
err.code = "EAGAIN";
return Promise.reject(err);
}
const n = Math.min(120, length);
accepted.push(Buffer.from(buffer.subarray(offset, offset + n)));
return Promise.resolve({ bytesWritten: n });
},
};
await writeAllUsb(h, payload, Date.now() + 2000);
expect(Buffer.concat(accepted).equals(payload)).toBe(true);
});
it("a wedged printer (never accepts a byte) fails at the deadline instead of hanging", async () => {
const h = { write: () => Promise.resolve({ bytesWritten: 0 }) };
await expect(writeAllUsb(h, Buffer.from("job"), Date.now() + 60)).rejects.toThrow(/usb write timeout/);
});
it("a non-EAGAIN error surfaces immediately", async () => {
const err = new Error("EIO") as NodeJS.ErrnoException;
err.code = "EIO";
const h = { write: () => Promise.reject(err) };
await expect(writeAllUsb(h, Buffer.from("job"), Date.now() + 1000)).rejects.toThrow("EIO");
});
});