@zpl-toolchain/print 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.d.ts +66 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +194 -0
- package/dist/browser.js.map +1 -0
- package/dist/index.d.ts +189 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +647 -0
- package/dist/index.js.map +1 -0
- package/dist/proxy.d.ts +57 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +404 -0
- package/dist/proxy.js.map +1 -0
- package/dist/status.d.ts +14 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +108 -0
- package/dist/status.js.map +1 -0
- package/dist/types.d.ts +175 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/package.json +79 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { PrinterDevice, PrinterStatus } from "./types.js";
|
|
2
|
+
export type { PrinterDevice, PrinterStatus } from "./types.js";
|
|
3
|
+
/** Options for configuring the Zebra Browser Print client. */
|
|
4
|
+
export interface ZebraBrowserPrintOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Base URL of the Zebra Browser Print agent.
|
|
7
|
+
* Defaults to `http://127.0.0.1:9100` when served over HTTP, or
|
|
8
|
+
* `https://localhost:9101` when served over HTTPS.
|
|
9
|
+
*/
|
|
10
|
+
agentUrl?: string;
|
|
11
|
+
/** Request timeout in milliseconds (default: 5000). */
|
|
12
|
+
timeout?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Wrapper around the Zebra Browser Print local agent.
|
|
16
|
+
*
|
|
17
|
+
* Uses only `fetch()` — no external dependencies.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { ZebraBrowserPrint } from "@zpl-toolchain/print/browser";
|
|
22
|
+
*
|
|
23
|
+
* const zbp = new ZebraBrowserPrint();
|
|
24
|
+
*
|
|
25
|
+
* if (await zbp.isAvailable()) {
|
|
26
|
+
* const devices = await zbp.discover();
|
|
27
|
+
* if (devices.length > 0) {
|
|
28
|
+
* await zbp.print(devices[0], "^XA^FDHello^FS^XZ");
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare class ZebraBrowserPrint {
|
|
34
|
+
private readonly agentUrl;
|
|
35
|
+
private readonly timeout;
|
|
36
|
+
constructor(options?: ZebraBrowserPrintOptions);
|
|
37
|
+
/**
|
|
38
|
+
* Check whether the Zebra Browser Print agent is running and reachable.
|
|
39
|
+
*
|
|
40
|
+
* @returns `true` if the agent responds, `false` otherwise.
|
|
41
|
+
*/
|
|
42
|
+
isAvailable(): Promise<boolean>;
|
|
43
|
+
/**
|
|
44
|
+
* Discover Zebra printers available through the local agent.
|
|
45
|
+
*
|
|
46
|
+
* @returns An array of {@link PrinterDevice} objects.
|
|
47
|
+
*/
|
|
48
|
+
discover(): Promise<PrinterDevice[]>;
|
|
49
|
+
/**
|
|
50
|
+
* Send ZPL to a specific printer through the agent.
|
|
51
|
+
*
|
|
52
|
+
* @param device - The target printer device (from {@link discover}).
|
|
53
|
+
* @param zpl - The ZPL II command string to send.
|
|
54
|
+
*/
|
|
55
|
+
print(device: PrinterDevice, zpl: string): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Query the host status of a printer through the agent.
|
|
58
|
+
*
|
|
59
|
+
* Sends `~HS` to the device and parses the response into a
|
|
60
|
+
* {@link PrinterStatus} object.
|
|
61
|
+
*
|
|
62
|
+
* @param device - The target printer device.
|
|
63
|
+
*/
|
|
64
|
+
getStatus(device: PrinterDevice): Promise<PrinterStatus>;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=browser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAG/D,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAkB/D,8DAA8D;AAC9D,MAAM,WAAW,wBAAwB;IACvC;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAoBD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAErB,OAAO,CAAC,EAAE,wBAAwB;IAO9C;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAcrC;;;;OAIG;IACG,QAAQ,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;IAqC1C;;;;;OAKG;IACG,KAAK,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAkB9D;;;;;;;OAOG;IACG,SAAS,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;CAqC/D"}
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { parseHostStatus } from "./status.js";
|
|
2
|
+
// ─── Zebra Browser Print Agent ───────────────────────────────────────────────
|
|
3
|
+
//
|
|
4
|
+
// The Zebra Browser Print SDK provides a localhost HTTP agent that bridges
|
|
5
|
+
// the browser to USB / network-attached Zebra printers. By default it listens
|
|
6
|
+
// on http://127.0.0.1:9100 (for HTTP) and https://localhost:9101 (for HTTPS).
|
|
7
|
+
//
|
|
8
|
+
// This module wraps that agent using plain `fetch()` — zero dependencies.
|
|
9
|
+
// It is designed to run *only* in browser environments.
|
|
10
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
11
|
+
/** Default HTTP endpoint for the Zebra Browser Print agent. */
|
|
12
|
+
const DEFAULT_AGENT_HTTP = "http://127.0.0.1:9100";
|
|
13
|
+
/** Default HTTPS endpoint for the Zebra Browser Print agent. */
|
|
14
|
+
const DEFAULT_AGENT_HTTPS = "https://localhost:9101";
|
|
15
|
+
function resolveAgentUrl(options) {
|
|
16
|
+
if (options?.agentUrl)
|
|
17
|
+
return options.agentUrl;
|
|
18
|
+
// Auto-detect protocol if running in a browser context.
|
|
19
|
+
if (typeof globalThis.location !== "undefined") {
|
|
20
|
+
return globalThis.location.protocol === "https:"
|
|
21
|
+
? DEFAULT_AGENT_HTTPS
|
|
22
|
+
: DEFAULT_AGENT_HTTP;
|
|
23
|
+
}
|
|
24
|
+
return DEFAULT_AGENT_HTTP;
|
|
25
|
+
}
|
|
26
|
+
function buildAbortSignal(timeout) {
|
|
27
|
+
return AbortSignal.timeout(timeout);
|
|
28
|
+
}
|
|
29
|
+
// ─── ZebraBrowserPrint API ───────────────────────────────────────────────────
|
|
30
|
+
/**
|
|
31
|
+
* Wrapper around the Zebra Browser Print local agent.
|
|
32
|
+
*
|
|
33
|
+
* Uses only `fetch()` — no external dependencies.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { ZebraBrowserPrint } from "@zpl-toolchain/print/browser";
|
|
38
|
+
*
|
|
39
|
+
* const zbp = new ZebraBrowserPrint();
|
|
40
|
+
*
|
|
41
|
+
* if (await zbp.isAvailable()) {
|
|
42
|
+
* const devices = await zbp.discover();
|
|
43
|
+
* if (devices.length > 0) {
|
|
44
|
+
* await zbp.print(devices[0], "^XA^FDHello^FS^XZ");
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export class ZebraBrowserPrint {
|
|
50
|
+
agentUrl;
|
|
51
|
+
timeout;
|
|
52
|
+
constructor(options) {
|
|
53
|
+
this.agentUrl = resolveAgentUrl(options);
|
|
54
|
+
this.timeout = options?.timeout ?? 5_000;
|
|
55
|
+
}
|
|
56
|
+
// ── Availability ─────────────────────────────────────────────────────
|
|
57
|
+
/**
|
|
58
|
+
* Check whether the Zebra Browser Print agent is running and reachable.
|
|
59
|
+
*
|
|
60
|
+
* @returns `true` if the agent responds, `false` otherwise.
|
|
61
|
+
*/
|
|
62
|
+
async isAvailable() {
|
|
63
|
+
try {
|
|
64
|
+
const res = await fetch(`${this.agentUrl}/available`, {
|
|
65
|
+
method: "GET",
|
|
66
|
+
signal: buildAbortSignal(this.timeout),
|
|
67
|
+
});
|
|
68
|
+
return res.ok;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// ── Discovery ────────────────────────────────────────────────────────
|
|
75
|
+
/**
|
|
76
|
+
* Discover Zebra printers available through the local agent.
|
|
77
|
+
*
|
|
78
|
+
* @returns An array of {@link PrinterDevice} objects.
|
|
79
|
+
*/
|
|
80
|
+
async discover() {
|
|
81
|
+
const res = await fetch(`${this.agentUrl}/available`, {
|
|
82
|
+
method: "GET",
|
|
83
|
+
signal: buildAbortSignal(this.timeout),
|
|
84
|
+
});
|
|
85
|
+
if (!res.ok) {
|
|
86
|
+
throw new Error(`Zebra Browser Print agent returned ${res.status}: ${res.statusText}`);
|
|
87
|
+
}
|
|
88
|
+
const text = await res.text();
|
|
89
|
+
// The agent returns a newline-separated list of device records.
|
|
90
|
+
// Each record is a set of key/value pairs separated by \t within the line.
|
|
91
|
+
// Different firmware versions may return JSON or the legacy format.
|
|
92
|
+
try {
|
|
93
|
+
// Try parsing as JSON first (newer agent versions).
|
|
94
|
+
const json = JSON.parse(text);
|
|
95
|
+
if (Array.isArray(json)) {
|
|
96
|
+
return json.map(mapDevice);
|
|
97
|
+
}
|
|
98
|
+
// Single device object.
|
|
99
|
+
if (json && typeof json === "object") {
|
|
100
|
+
return [mapDevice(json)];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Fall through to legacy parsing.
|
|
105
|
+
}
|
|
106
|
+
// Legacy tab-separated format.
|
|
107
|
+
return parseLegacyDeviceList(text);
|
|
108
|
+
}
|
|
109
|
+
// ── Printing ─────────────────────────────────────────────────────────
|
|
110
|
+
/**
|
|
111
|
+
* Send ZPL to a specific printer through the agent.
|
|
112
|
+
*
|
|
113
|
+
* @param device - The target printer device (from {@link discover}).
|
|
114
|
+
* @param zpl - The ZPL II command string to send.
|
|
115
|
+
*/
|
|
116
|
+
async print(device, zpl) {
|
|
117
|
+
const res = await fetch(`${this.agentUrl}/write`, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers: { "Content-Type": "application/json" },
|
|
120
|
+
body: JSON.stringify({ device: device.uid, data: zpl }),
|
|
121
|
+
signal: buildAbortSignal(this.timeout),
|
|
122
|
+
});
|
|
123
|
+
if (!res.ok) {
|
|
124
|
+
const body = await res.text().catch(() => "");
|
|
125
|
+
throw new Error(`Print failed (${res.status}): ${body || res.statusText}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// ── Status ───────────────────────────────────────────────────────────
|
|
129
|
+
/**
|
|
130
|
+
* Query the host status of a printer through the agent.
|
|
131
|
+
*
|
|
132
|
+
* Sends `~HS` to the device and parses the response into a
|
|
133
|
+
* {@link PrinterStatus} object.
|
|
134
|
+
*
|
|
135
|
+
* @param device - The target printer device.
|
|
136
|
+
*/
|
|
137
|
+
async getStatus(device) {
|
|
138
|
+
// Send the ~HS command through the agent's read-after-write flow.
|
|
139
|
+
const writeRes = await fetch(`${this.agentUrl}/write`, {
|
|
140
|
+
method: "POST",
|
|
141
|
+
headers: { "Content-Type": "application/json" },
|
|
142
|
+
body: JSON.stringify({ device: device.uid, data: "~HS" }),
|
|
143
|
+
signal: buildAbortSignal(this.timeout),
|
|
144
|
+
});
|
|
145
|
+
if (!writeRes.ok) {
|
|
146
|
+
const body = await writeRes.text().catch(() => "");
|
|
147
|
+
throw new Error(`Status query write failed (${writeRes.status}): ${body || writeRes.statusText}`);
|
|
148
|
+
}
|
|
149
|
+
// Allow the printer time to prepare its response before reading.
|
|
150
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
151
|
+
// Read the response back from the printer.
|
|
152
|
+
const readRes = await fetch(`${this.agentUrl}/read`, {
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: { "Content-Type": "application/json" },
|
|
155
|
+
body: JSON.stringify({ device: device.uid }),
|
|
156
|
+
signal: buildAbortSignal(this.timeout),
|
|
157
|
+
});
|
|
158
|
+
if (!readRes.ok) {
|
|
159
|
+
const body = await readRes.text().catch(() => "");
|
|
160
|
+
throw new Error(`Status query read failed (${readRes.status}): ${body || readRes.statusText}`);
|
|
161
|
+
}
|
|
162
|
+
const raw = await readRes.text();
|
|
163
|
+
return parseHostStatus(raw);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// ─── Device mapping helpers ──────────────────────────────────────────────────
|
|
167
|
+
function mapDevice(obj) {
|
|
168
|
+
return {
|
|
169
|
+
name: String(obj.name ?? obj.Name ?? "Unknown"),
|
|
170
|
+
uid: String(obj.uid ?? obj.UID ?? obj.uniqueId ?? ""),
|
|
171
|
+
connection: String(obj.connection ?? obj.Connection ?? "unknown"),
|
|
172
|
+
deviceType: String(obj.deviceType ?? obj.DeviceType ?? "unknown"),
|
|
173
|
+
provider: String(obj.provider ?? obj.Provider ?? "unknown"),
|
|
174
|
+
manufacturer: String(obj.manufacturer ?? obj.Manufacturer ?? "unknown"),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function parseLegacyDeviceList(text) {
|
|
178
|
+
const lines = text
|
|
179
|
+
.split("\n")
|
|
180
|
+
.map((l) => l.trim())
|
|
181
|
+
.filter((l) => l.length > 0);
|
|
182
|
+
return lines.map((line) => {
|
|
183
|
+
const fields = line.split("\t");
|
|
184
|
+
return {
|
|
185
|
+
name: fields[0] ?? "Unknown",
|
|
186
|
+
uid: fields[1] ?? "",
|
|
187
|
+
connection: fields[2] ?? "unknown",
|
|
188
|
+
deviceType: fields[3] ?? "unknown",
|
|
189
|
+
provider: fields[4] ?? "unknown",
|
|
190
|
+
manufacturer: fields[5] ?? "unknown",
|
|
191
|
+
};
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAI9C,gFAAgF;AAChF,EAAE;AACF,2EAA2E;AAC3E,8EAA8E;AAC9E,8EAA8E;AAC9E,EAAE;AACF,0EAA0E;AAC1E,wDAAwD;AACxD,gFAAgF;AAEhF,+DAA+D;AAC/D,MAAM,kBAAkB,GAAG,uBAAuB,CAAC;AAEnD,gEAAgE;AAChE,MAAM,mBAAmB,GAAG,wBAAwB,CAAC;AAerD,SAAS,eAAe,CAAC,OAAkC;IACzD,IAAI,OAAO,EAAE,QAAQ;QAAE,OAAO,OAAO,CAAC,QAAQ,CAAC;IAE/C,wDAAwD;IACxD,IAAI,OAAO,UAAU,CAAC,QAAQ,KAAK,WAAW,EAAE,CAAC;QAC/C,OAAO,UAAU,CAAC,QAAQ,CAAC,QAAQ,KAAK,QAAQ;YAC9C,CAAC,CAAC,mBAAmB;YACrB,CAAC,CAAC,kBAAkB,CAAC;IACzB,CAAC;IACD,OAAO,kBAAkB,CAAC;AAC5B,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAe;IACvC,OAAO,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC;AAED,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,iBAAiB;IACX,QAAQ,CAAS;IACjB,OAAO,CAAS;IAEjC,YAAY,OAAkC;QAC5C,IAAI,CAAC,QAAQ,GAAG,eAAe,CAAC,OAAO,CAAC,C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C,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,SAAS;YAClC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,SAAS;YAChC,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,SAAS;SACrC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { PrinterConfig, PrintResult, PrinterStatus, ValidateOptions, BatchOptions, BatchProgress, BatchResult } from "./types.js";
|
|
2
|
+
export { parseHostStatus } from "./status.js";
|
|
3
|
+
export { PrintError, type PrinterConfig, type PrintResult, type PrinterStatus, type PrintErrorCode, type ValidateOptions, type BatchOptions, type BatchProgress, type BatchResult, } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Open a TCP socket, write `command`, read the response until the socket is
|
|
6
|
+
* idle for a short period, then close and return the response.
|
|
7
|
+
*
|
|
8
|
+
* Exported so that `proxy.ts` can re-use it instead of duplicating the logic.
|
|
9
|
+
*/
|
|
10
|
+
export declare function tcpQuery(host: string, port: number, command: string, timeout: number): Promise<string>;
|
|
11
|
+
/**
|
|
12
|
+
* Send ZPL to a printer and return the result.
|
|
13
|
+
*
|
|
14
|
+
* Automatically retries on transient network errors with exponential backoff.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { print } from "@zpl-toolchain/print";
|
|
19
|
+
*
|
|
20
|
+
* const result = await print("^XA^FO50,50^A0N,50,50^FDHello^FS^XZ", {
|
|
21
|
+
* host: "192.168.1.100",
|
|
22
|
+
* });
|
|
23
|
+
* console.log(result); // { success: true, bytesWritten: 44, duration: 12 }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function print(zpl: string, config: PrinterConfig): Promise<PrintResult>;
|
|
27
|
+
/**
|
|
28
|
+
* A persistent TCP connection to a ZPL printer.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { createPrinter } from "@zpl-toolchain/print";
|
|
33
|
+
*
|
|
34
|
+
* const printer = createPrinter({ host: "192.168.1.100" });
|
|
35
|
+
* await printer.print("^XA^FO50,50^A0N,50,50^FDHello^FS^XZ");
|
|
36
|
+
* const status = await printer.getStatus();
|
|
37
|
+
* console.log(status.ready);
|
|
38
|
+
* await printer.close();
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare class TcpPrinter {
|
|
42
|
+
private readonly config;
|
|
43
|
+
private socket;
|
|
44
|
+
private connecting;
|
|
45
|
+
private closed;
|
|
46
|
+
private writeQueue;
|
|
47
|
+
constructor(config: PrinterConfig);
|
|
48
|
+
/** Ensure a live connection exists, creating one if needed. */
|
|
49
|
+
private ensureConnected;
|
|
50
|
+
/**
|
|
51
|
+
* Send ZPL to the printer over the persistent connection.
|
|
52
|
+
*
|
|
53
|
+
* Falls back to a fresh connection if the existing one is broken.
|
|
54
|
+
*/
|
|
55
|
+
print(zpl: string): Promise<PrintResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Query the printer's host status (~HS).
|
|
58
|
+
*
|
|
59
|
+
* Sends the `~HS` command and parses the response into a
|
|
60
|
+
* {@link PrinterStatus} object.
|
|
61
|
+
*
|
|
62
|
+
* **Note:** This opens a separate short-lived TCP connection for the query
|
|
63
|
+
* rather than using the persistent connection, because the ~HS response
|
|
64
|
+
* needs an idle-timeout-based read strategy that would interfere with the
|
|
65
|
+
* persistent socket's event handlers.
|
|
66
|
+
*/
|
|
67
|
+
getStatus(): Promise<PrinterStatus>;
|
|
68
|
+
/**
|
|
69
|
+
* Send an arbitrary command and return the raw response string.
|
|
70
|
+
*
|
|
71
|
+
* Useful for querying ~HI (Host Identification), ~HM (Host Memory), etc.
|
|
72
|
+
*
|
|
73
|
+
* **Note:** This opens a separate short-lived TCP connection for each query
|
|
74
|
+
* rather than using the persistent connection, because reading a response
|
|
75
|
+
* requires an idle-timeout strategy that would interfere with the
|
|
76
|
+
* persistent socket's event handlers.
|
|
77
|
+
*/
|
|
78
|
+
query(command: string): Promise<string>;
|
|
79
|
+
/**
|
|
80
|
+
* Check whether the printer is reachable by attempting a TCP connection.
|
|
81
|
+
* Returns `true` if the connection succeeds, `false` otherwise.
|
|
82
|
+
*/
|
|
83
|
+
isReachable(): Promise<boolean>;
|
|
84
|
+
/**
|
|
85
|
+
* Send multiple labels sequentially over the persistent connection.
|
|
86
|
+
*
|
|
87
|
+
* @param labels - Array of ZPL strings, one per label.
|
|
88
|
+
* @param opts - Optional batch options (e.g., status polling interval).
|
|
89
|
+
* @param onProgress - Optional callback after each label.
|
|
90
|
+
* Return `false` (strictly) to abort the batch early.
|
|
91
|
+
* Any other return value (including `undefined`/`true`) continues normally.
|
|
92
|
+
* @returns The number of labels sent and the total.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* // Simple batch
|
|
97
|
+
* await printer.printBatch(labels);
|
|
98
|
+
*
|
|
99
|
+
* // With progress tracking
|
|
100
|
+
* await printer.printBatch(labels, {}, (p) => {
|
|
101
|
+
* console.log(`${p.sent}/${p.total}`);
|
|
102
|
+
* });
|
|
103
|
+
*
|
|
104
|
+
* // With status polling every 5 labels
|
|
105
|
+
* await printer.printBatch(labels, { statusInterval: 5 }, (p) => {
|
|
106
|
+
* if (p.status) console.log(`Labels remaining: ${p.status.labelsRemaining}`);
|
|
107
|
+
* });
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
printBatch(labels: string[], opts?: BatchOptions, onProgress?: (progress: BatchProgress) => boolean | void): Promise<BatchResult>;
|
|
111
|
+
/**
|
|
112
|
+
* Poll the printer until all queued labels have been printed.
|
|
113
|
+
*
|
|
114
|
+
* Queries `~HS` at the specified interval and resolves when
|
|
115
|
+
* `formatsInBuffer === 0` and `labelsRemaining === 0`.
|
|
116
|
+
*
|
|
117
|
+
* @param pollInterval - Milliseconds between status polls (default: 500).
|
|
118
|
+
* @param timeout - Maximum wait time in milliseconds (default: 30000).
|
|
119
|
+
* @throws {PrintError} with code `TIMEOUT` if the timeout is exceeded.
|
|
120
|
+
*/
|
|
121
|
+
waitForCompletion(pollInterval?: number, timeout?: number): Promise<void>;
|
|
122
|
+
/** Close the persistent connection. Safe to call multiple times. */
|
|
123
|
+
close(): Promise<void>;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create a {@link TcpPrinter} with a persistent TCP connection.
|
|
127
|
+
*
|
|
128
|
+
* @see {@link TcpPrinter}
|
|
129
|
+
*/
|
|
130
|
+
export declare function createPrinter(config: PrinterConfig): TcpPrinter;
|
|
131
|
+
/**
|
|
132
|
+
* Print ZPL after first validating it with `@zpl-toolchain/core` (if
|
|
133
|
+
* installed). Falls back to a plain print if the core package is not
|
|
134
|
+
* available.
|
|
135
|
+
*
|
|
136
|
+
* Supports optional printer-profile-aware validation and strict mode
|
|
137
|
+
* (treat warnings as errors).
|
|
138
|
+
*
|
|
139
|
+
* @param zpl - The ZPL source string to validate and send.
|
|
140
|
+
* @param config - Printer connection configuration.
|
|
141
|
+
* @param validateOpts - Optional validation settings (profile, strict mode).
|
|
142
|
+
*
|
|
143
|
+
* @throws {PrintError} with code `VALIDATION_FAILED` if validation finds errors
|
|
144
|
+
* (or warnings in strict mode).
|
|
145
|
+
* @throws {PrintError} with a network-related code if the print operation fails.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { printValidated } from "@zpl-toolchain/print";
|
|
150
|
+
* import { readFileSync } from "node:fs";
|
|
151
|
+
*
|
|
152
|
+
* // Basic: validate then print
|
|
153
|
+
* await printValidated("^XA^FDHello^FS^XZ", { host: "192.168.1.100" });
|
|
154
|
+
*
|
|
155
|
+
* // With printer profile for hardware-aware validation
|
|
156
|
+
* const profile = readFileSync("profiles/ZD421.json", "utf-8");
|
|
157
|
+
* await printValidated("^XA^FDHello^FS^XZ", { host: "192.168.1.100" }, {
|
|
158
|
+
* profileJson: profile,
|
|
159
|
+
* strict: true, // treat warnings as errors
|
|
160
|
+
* });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export declare function printValidated(zpl: string, config: PrinterConfig, validateOpts?: ValidateOptions): Promise<PrintResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Send multiple ZPL labels to a printer in a single session.
|
|
166
|
+
*
|
|
167
|
+
* Opens a persistent connection, sends all labels sequentially, and closes.
|
|
168
|
+
*
|
|
169
|
+
* @param labels - Array of ZPL strings, one per label.
|
|
170
|
+
* @param config - Printer connection configuration.
|
|
171
|
+
* @param opts - Optional batch options (e.g., status polling interval).
|
|
172
|
+
* @param onProgress - Optional callback after each label.
|
|
173
|
+
* Return `false` (strictly) to abort the batch early.
|
|
174
|
+
* @returns The number of labels sent and the total.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* import { printBatch } from "@zpl-toolchain/print";
|
|
179
|
+
*
|
|
180
|
+
* const labels = [
|
|
181
|
+
* "^XA^FO50,50^A0N,50,50^FDLabel 1^FS^XZ",
|
|
182
|
+
* "^XA^FO50,50^A0N,50,50^FDLabel 2^FS^XZ",
|
|
183
|
+
* ];
|
|
184
|
+
* const result = await printBatch(labels, { host: "192.168.1.100" });
|
|
185
|
+
* console.log(`Sent ${result.sent}/${result.total} labels`);
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export declare function printBatch(labels: string[], config: PrinterConfig, opts?: BatchOptions, onProgress?: (progress: BatchProgress) => boolean | void): Promise<BatchResult>;
|
|
189
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,aAAa,EAEb,eAAe,EACf,YAAY,EACZ,aAAa,EACb,WAAW,EACZ,MAAM,YAAY,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