GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: the settings read no longer waits out the deadline behind a raw … · u8array/ZPLab@a81a1d5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/core/src/lib
    • sgd.test.ts
    • sgd.ts
  • src/lib
    • printerQuery.ts
    • printerSettings.test.ts
    • printerSettings.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/core/src/lib/sgd.test.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/lib/sgd.test.ts
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect } from 'vitest';
22
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';
44

55
const byKey = (key: string) => parseSgdDump(ZD230_ALLCV_EXCERPT).find((s) => s.key === key);
66

@@ -43,6 +43,20 @@ describe('sgdValues', () => {
4343
});
4444
});
4545

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+
4660
describe('sgdGetvar', () => {
4761
it('builds the getvar line the printer reads', () => {
4862
expect(sgdGetvar(SGD_ALL_SETTINGS)).toBe('! U1 getvar "allcv"\r\n');
Collapse file

‎packages/core/src/lib/sgd.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/lib/sgd.ts
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ export interface SgdSetting {
88
export const SGD_ALL_SETTINGS = 'allcv';
99

1010
/** 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;
1218

1319
/** The getvar command without its terminator, so a progress text can name it. */
1420
export const sgdGetvarCommand = (key: string): string => `! U1 getvar "${key}"`;
Collapse file

‎src/lib/printerQuery.ts‎

Copy file name to clipboardExpand all lines: src/lib/printerQuery.ts
+12-9Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ export interface ReplyWait {
116116
deadlineMs?: number;
117117
/** The replies the printer still owes after its first answer, which the idle gap alone would miss. */
118118
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;
121121
/** Ends the read with what came so far. */
122122
signal?: AbortSignal;
123123
}
@@ -131,13 +131,13 @@ export interface PrinterReply {
131131
}
132132

133133
/** 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 {
135135
if (end === "deadline") return { kind: "timed_out" };
136136
if (end === "lost") return { kind: "lost" };
137137
return undefined;
138138
}
139139

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>> {
141141
let body = "";
142142
let received = 0;
143143
let returned = false;
@@ -152,15 +152,15 @@ async function readReply(target: QueryTarget, zpl: string, { deadlineMs = REPLY_
152152
if (returned) return;
153153
clearTimeout(idle);
154154
idle = setTimeout(() => {
155-
if (owedReplies(body, awaited).length === 0) stop();
155+
if (whole ? whole(body) : owedReplies(body, awaited).length === 0) stop();
156156
}, REPLY_IDLE_MS);
157157
});
158158
const stop = () => {
159159
stopped = true;
160160
stream.stop();
161161
};
162162
// 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);
164164
signal?.addEventListener("abort", stop, { once: true });
165165
if (signal?.aborted) stop();
166166
const end = await stream.done;
@@ -194,11 +194,14 @@ async function readReply(target: QueryTarget, zpl: string, { deadlineMs = REPLY_
194194
// A stopped wait with nothing in still names what it waited for.
195195
if (body === "" && unanswered.length === 0) return { kind: "silent" };
196196
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;
197200
// 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.
198201
const failure = awaitsNone ? endFailure(ended) : undefined;
199202
if (failure) return failure;
200203
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;
202205
}
203206

204207
/** 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?
207210
return res.kind === "ok" ? { kind: "ok", value: res.value.body } : res;
208211
}
209212

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. */
212215
export async function readPrinterReply(target: QueryTarget, zpl: string, wait: ReplyWait): Promise<PrinterOutcome<PrinterReply>> {
213216
if (!carriesReply(target.kind, printReach)) return { kind: "error", message: "This build has no channel that reads this printer" };
214217
try {
Collapse file

‎src/lib/printerSettings.test.ts‎

Copy file name to clipboardExpand all lines: src/lib/printerSettings.test.ts
+22-3Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { STX } from "@zplab/core/lib/hostStatus.fixture";
23
import { parseSgdDump } from "@zplab/core/lib/sgd";
34
import { ZD230_ALLCV_EXCERPT } from "@zplab/core/lib/sgd.fixture";
45
import { FIRST_BYTE_MS, REPLY_DEADLINE_MS, REPLY_IDLE_MS } from "./printerQuery";
@@ -18,7 +19,6 @@ vi.mock("./printReach", () => ({ printReach: { desktop: false, server: true } })
1819
vi.mock("./serverPrint", () => ({ queryZplServer }));
1920

2021
const target = { kind: "server", id: "dock" } as const;
21-
// The ZD230 line endings and dump end, see SGD_ALL_SETTINGS_END.
2222
const DUMP = `${ZD230_ALLCV_EXCERPT.replace(/\n/g, "\r\n")}""`;
2323
const PAUSE_AT = DUMP.indexOf("zpl.label_length");
2424

@@ -119,11 +119,30 @@ describe("readPrinterSettings", () => {
119119
expect(await read).toEqual({ kind: "ok", value: parseSgdDump(ZD230_ALLCV_EXCERPT) });
120120
});
121121

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 () => {
123123
const read = readPrinterSettings(target);
124124
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);
126135
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);
127146
expect(await read).toEqual({ kind: "unparsed" });
128147
});
129148
});
Collapse file

‎src/lib/printerSettings.ts‎

Copy file name to clipboard
+4-11Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
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";
43
import type { QueryTarget } from "./printTarget";
54

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-
95
/** The printer's whole configuration in one round trip. */
106
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 });
128
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);
1710
return settings.length > 0 ? { kind: "ok", value: settings } : { kind: "unparsed" };
1811
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.