GeezerVison is showing github.com — fix: the settings read no longer waits out the deadline behind a raw … · u8array/ZPLab@a81a1d5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/lib/sgd.test.ts +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, it, expect } from 'vitest'; | 2 | 2 | import { ZD230_ALLCV_EXCERPT } from './sgd.fixture'; | 3 | | -import { SGD_ALL_SETTINGS, parseSgdDump, sgdGetvar, sgdGetvarCommand, sgdValues } from './sgd'; | | 3 | +import { SGD_ALL_SETTINGS, parseSgdDump, sgdAllSettingsWhole, sgdGetvar, sgdGetvarCommand, sgdValues } from './sgd'; | 4 | 4 |
| 5 | 5 | const byKey = (key: string) => parseSgdDump(ZD230_ALLCV_EXCERPT).find((s) => s.key === key); | 6 | 6 |
| @@ -43,6 +43,20 @@ describe('sgdValues', () => { | 43 | 43 | }); | 44 | 44 | }); | 45 | 45 |
| | 46 | +describe('sgdAllSettingsWhole', () => { | | 47 | + const dump = ZD230_ALLCV_EXCERPT.replace(/\n/g, '\r\n'); | | 48 | + | | 49 | + it('calls the dump whole only at its closing line', () => { | | 50 | + expect(sgdAllSettingsWhole(dump)).toBe(false); | | 51 | + expect(sgdAllSettingsWhole(`${dump}""`)).toBe(true); | | 52 | + }); | | 53 | + | | 54 | + it('calls a lone "?" whole, an answer inside the dump not', () => { | | 55 | + expect(sgdAllSettingsWhole('"?"\r\n')).toBe(true); | | 56 | + expect(sgdAllSettingsWhole(`${dump}file.type : "?" \r\n`)).toBe(false); | | 57 | + }); | | 58 | +}); | | 59 | + | 46 | 60 | describe('sgdGetvar', () => { | 47 | 61 | it('builds the getvar line the printer reads', () => { | 48 | 62 | expect(sgdGetvar(SGD_ALL_SETTINGS)).toBe('! U1 getvar "allcv"\r\n'); | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/lib/sgd.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,13 @@ export interface SgdSetting { | 8 | 8 | export const SGD_ALL_SETTINGS = 'allcv'; | 9 | 9 |
| 10 | 10 | /** The ZD230 closes the dump on a line holding an empty quoted value. */ | 11 | | -export const SGD_ALL_SETTINGS_END = '\r\n""'; | | 11 | +const DUMP_END = '\r\n""'; | | 12 | + | | 13 | +/** The value a printer writes for a getvar it cannot answer, here file.type in the ZD230 dump. */ | | 14 | +const NO_ANSWER = '"?"'; | | 15 | + | | 16 | +/** Whether the all-settings getvar has answered whole. */ | | 17 | +export const sgdAllSettingsWhole = (reply: string): boolean => reply.includes(DUMP_END) || reply.trim() === NO_ANSWER; | 12 | 18 |
| 13 | 19 | /** The getvar command without its terminator, so a progress text can name it. */ | 14 | 20 | export const sgdGetvarCommand = (key: string): string => `! U1 getvar "${key}"`; | |
Collapse file Copy file name to clipboardExpand all lines: src/lib/printerQuery.ts +12-9Lines changed: 12 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -116,8 +116,8 @@ export interface ReplyWait { | 116 | 116 | deadlineMs?: number; | 117 | 117 | /** The replies the printer still owes after its first answer, which the idle gap alone would miss. */ | 118 | 118 | awaited?: readonly AwaitedReply[]; | 119 | | - /** The awaited reply starts at once instead of behind a print, so a printer that stays mute ends the read at the first-byte limit. */ | 120 | | - answersAtOnce?: boolean; | | 119 | + /** Whether the text proves the reply whole, for a reply that pauses longer than the idle gap. A read that ends short of it fails. */ | | 120 | + whole?: (body: string) => boolean; | 121 | 121 | /** Ends the read with what came so far. */ | 122 | 122 | signal?: AbortSignal; | 123 | 123 | } | @@ -131,13 +131,13 @@ export interface PrinterReply { | 131 | 131 | } | 132 | 132 |
| 133 | 133 | /** The failure a reply's end names, undefined for an end that is no failure. */ | 134 | | -export function endFailure(end: ReplyEnd): PrinterQueryFailure | undefined { | | 134 | +function endFailure(end: ReplyEnd): PrinterQueryFailure | undefined { | 135 | 135 | if (end === "deadline") return { kind: "timed_out" }; | 136 | 136 | if (end === "lost") return { kind: "lost" }; | 137 | 137 | return undefined; | 138 | 138 | } | 139 | 139 |
| 140 | | -async function readReply(target: QueryTarget, zpl: string, { deadlineMs = REPLY_DEADLINE_MS, awaited = [], answersAtOnce = false, signal }: ReplyWait): Promise<PrinterOutcome<PrinterReply>> { | | 140 | +async function readReply(target: QueryTarget, zpl: string, { deadlineMs = REPLY_DEADLINE_MS, awaited = [], whole, signal }: ReplyWait): Promise<PrinterOutcome<PrinterReply>> { | 141 | 141 | let body = ""; | 142 | 142 | let received = 0; | 143 | 143 | let returned = false; | @@ -152,15 +152,15 @@ async function readReply(target: QueryTarget, zpl: string, { deadlineMs = REPLY_ | 152 | 152 | if (returned) return; | 153 | 153 | clearTimeout(idle); | 154 | 154 | idle = setTimeout(() => { | 155 | | - if (owedReplies(body, awaited).length === 0) stop(); | | 155 | + if (whole ? whole(body) : owedReplies(body, awaited).length === 0) stop(); | 156 | 156 | }, REPLY_IDLE_MS); | 157 | 157 | }); | 158 | 158 | const stop = () => { | 159 | 159 | stopped = true; | 160 | 160 | stream.stop(); | 161 | 161 | }; | 162 | 162 | // Only a reply owed behind a print waits the whole deadline, every other read gives up on a mute printer at the first-byte limit. | 163 | | - if (awaitsNone || answersAtOnce) idle = setTimeout(stop, FIRST_BYTE_MS); | | 163 | + if (awaitsNone) idle = setTimeout(stop, FIRST_BYTE_MS); | 164 | 164 | signal?.addEventListener("abort", stop, { once: true }); | 165 | 165 | if (signal?.aborted) stop(); | 166 | 166 | const end = await stream.done; | @@ -194,11 +194,14 @@ async function readReply(target: QueryTarget, zpl: string, { deadlineMs = REPLY_ | 194 | 194 | // A stopped wait with nothing in still names what it waited for. | 195 | 195 | if (body === "" && unanswered.length === 0) return { kind: "silent" }; | 196 | 196 | const ended: ReplyEnd = stopped ? "stopped" : end.value.end; | | 197 | + const reply: PrinterOutcome<PrinterReply> = { kind: "ok", value: { body, unanswered, end: ended } }; | | 198 | + // A reply the text proves whole is kept, even when the link or the deadline ends the read behind it. | | 199 | + if (whole?.(body)) return reply; | 197 | 200 | // A read that awaits nothing stops itself behind the reply, so a deadline or a lost link before that stop would cut the printer off mid-reply. | 198 | 201 | const failure = awaitsNone ? endFailure(ended) : undefined; | 199 | 202 | if (failure) return failure; | 200 | 203 | if (unanswered.length === 0 && answeredText(body).kind === "silent") return { kind: "silent" }; | 201 | | - return { kind: "ok", value: { body, unanswered, end: ended } }; | | 204 | + return whole ? { kind: "incomplete" } : reply; | 202 | 205 | } | 203 | 206 |
| 204 | 207 | /** The reply of a command that answers at once, whole or not at all. */ | @@ -207,8 +210,8 @@ export async function queryPrinter(target: QueryTarget, zpl: string, deadlineMs? | 207 | 210 | return res.kind === "ok" ? { kind: "ok", value: res.value.body } : res; | 208 | 211 | } | 209 | 212 |
| 210 | | -/** Reads until every awaited reply is in and the printer has gone quiet, or until the deadline or the signal ends | 211 | | - * the wait with what came. Every failure comes back as a value, never as a throw. */ | | 213 | +/** Reads until the reply is whole and the printer has gone quiet, or until the deadline or the signal ends the wait. | | 214 | + * Every failure comes back as a value, never as a throw. */ | 212 | 215 | export async function readPrinterReply(target: QueryTarget, zpl: string, wait: ReplyWait): Promise<PrinterOutcome<PrinterReply>> { | 213 | 216 | if (!carriesReply(target.kind, printReach)) return { kind: "error", message: "This build has no channel that reads this printer" }; | 214 | 217 | try { | |
Collapse file Copy file name to clipboardExpand all lines: src/lib/printerSettings.test.ts +22-3Lines changed: 22 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | | 2 | +import { STX } from "@zplab/core/lib/hostStatus.fixture"; | 2 | 3 | import { parseSgdDump } from "@zplab/core/lib/sgd"; | 3 | 4 | import { ZD230_ALLCV_EXCERPT } from "@zplab/core/lib/sgd.fixture"; | 4 | 5 | import { FIRST_BYTE_MS, REPLY_DEADLINE_MS, REPLY_IDLE_MS } from "./printerQuery"; | @@ -18,7 +19,6 @@ vi.mock("./printReach", () => ({ printReach: { desktop: false, server: true } }) | 18 | 19 | vi.mock("./serverPrint", () => ({ queryZplServer })); | 19 | 20 |
| 20 | 21 | const target = { kind: "server", id: "dock" } as const; | 21 | | -// The ZD230 line endings and dump end, see SGD_ALL_SETTINGS_END. | 22 | 22 | const DUMP = `${ZD230_ALLCV_EXCERPT.replace(/\n/g, "\r\n")}""`; | 23 | 23 | const PAUSE_AT = DUMP.indexOf("zpl.label_length"); | 24 | 24 |
| @@ -119,11 +119,30 @@ describe("readPrinterSettings", () => { | 119 | 119 | expect(await read).toEqual({ kind: "ok", value: parseSgdDump(ZD230_ALLCV_EXCERPT) }); | 120 | 120 | }); | 121 | 121 |
| 122 | | - it("calls a whole reply that lists no setting unparsed", async () => { | | 122 | + it("keeps a whole dump whose link dropped after its end", async () => { | 123 | 123 | const read = readPrinterSettings(target); | 124 | 124 | const query = await open(); | 125 | | - query.text('"?"\r\n""'); | | 125 | + query.text(DUMP); | | 126 | + query.end("lost"); | | 127 | + expect(await read).toEqual({ kind: "ok", value: parseSgdDump(ZD230_ALLCV_EXCERPT) }); | | 128 | + }); | | 129 | + | | 130 | + it("finds the dump end behind a raw frame byte in the prefix", async () => { | | 131 | + const framed = DUMP.replace("^ (5E)", `${STX} (02)`); | | 132 | + const read = readPrinterSettings(target); | | 133 | + const query = await open(); | | 134 | + query.text(framed); | 126 | 135 | await vi.advanceTimersByTimeAsync(REPLY_IDLE_MS); | | 136 | + expect(query.signal.aborted).toBe(true); | | 137 | + expect(await read).toEqual({ kind: "ok", value: parseSgdDump(framed) }); | | 138 | + }); | | 139 | + | | 140 | + it("calls the lone value of a printer without the dump unparsed at the idle gap", async () => { | | 141 | + const read = readPrinterSettings(target); | | 142 | + const query = await open(); | | 143 | + query.text('"?"'); | | 144 | + await vi.advanceTimersByTimeAsync(REPLY_IDLE_MS); | | 145 | + expect(query.signal.aborted).toBe(true); | 127 | 146 | expect(await read).toEqual({ kind: "unparsed" }); | 128 | 147 | }); | 129 | 148 | }); |
Collapse file Copy file name to clipboard +4-11Lines changed: 4 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import type { AwaitedReply } from "@zplab/core/lib/documentReplies"; | 2 | | -import { SGD_ALL_SETTINGS, SGD_ALL_SETTINGS_END, parseSgdDump, sgdGetvar, sgdGetvarCommand, type SgdSetting } from "@zplab/core/lib/sgd"; | 3 | | -import { answeredText, endFailure, readPrinterReply, type PrinterOutcome } from "./printerQuery"; | | 1 | +import { SGD_ALL_SETTINGS, parseSgdDump, sgdAllSettingsWhole, sgdGetvar, type SgdSetting } from "@zplab/core/lib/sgd"; | | 2 | +import { readPrinterReply, type PrinterOutcome } from "./printerQuery"; | 4 | 3 | import type { QueryTarget } from "./printTarget"; | 5 | 4 |
| 6 | | -/** The printer pauses inside the dump for longer than the idle gap, so the read waits for the dump's end. */ | 7 | | -const DUMP_END: AwaitedReply = { header: "", terminator: SGD_ALL_SETTINGS_END, count: 1, commands: [sgdGetvarCommand(SGD_ALL_SETTINGS)] }; | 8 | | - | 9 | 5 | /** The printer's whole configuration in one round trip. */ | 10 | 6 | export async function readPrinterSettings(target: QueryTarget, signal?: AbortSignal): Promise<PrinterOutcome<SgdSetting[]>> { | 11 | | - const reply = await readPrinterReply(target, sgdGetvar(SGD_ALL_SETTINGS), { signal, answersAtOnce: true, awaited: [DUMP_END] }); | | 7 | + const reply = await readPrinterReply(target, sgdGetvar(SGD_ALL_SETTINGS), { signal, whole: sgdAllSettingsWhole }); | 12 | 8 | if (reply.kind !== "ok") return reply; | 13 | | - const { body, unanswered, end } = reply.value; | 14 | | - if (answeredText(body).kind === "silent") return { kind: "silent" }; | 15 | | - if (unanswered.length > 0) return endFailure(end) ?? { kind: "incomplete" }; | 16 | | - const settings = parseSgdDump(body); | | 9 | + const settings = parseSgdDump(reply.value.body); | 17 | 10 | return settings.length > 0 ? { kind: "ok", value: settings } : { kind: "unparsed" }; | 18 | 11 | } |
|