@miguelmorales13/nestkit 0.30.0 → 0.31.0
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/README.md +1 -0
- package/dist/printing/escpos.d.ts +26 -0
- package/dist/printing/index.cjs +302 -0
- package/dist/printing/index.d.ts +7 -0
- package/dist/printing/index.js +302 -0
- package/dist/printing/memory.store.d.ts +58 -0
- package/dist/printing/print-queue.d.ts +28 -0
- package/dist/printing/printer-status.d.ts +10 -0
- package/dist/printing/printing.types.d.ts +96 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -2191,6 +2191,7 @@ trae los datos.
|
|
|
2191
2191
|
| `@miguelmorales13/nestkit/i18n` | `I18nModule`, `t`, `translate`, `currentLanguage`, `I18nInterceptor`, `TranslatorPort`, `NestI18nTranslator`, `translateOr` |
|
|
2192
2192
|
| `@miguelmorales13/nestkit/bootstrap` | `applyNestKitDefaults` |
|
|
2193
2193
|
| `@miguelmorales13/nestkit/security` | `signState`/`readState` — CSRF de un flujo OAuth "conectar cuenta" firmado, sin cookie ni sesión |
|
|
2194
|
+
| `@miguelmorales13/nestkit/printing` | `PrintQueue`, `PrintJobStorePort`, `MemoryPrintJobStore`, `printerStatus`, `pickPrinter`, `disconnectedUses`, `EscPosBuilder` — impresoras compartidas por el comercio, cola de trabajos y estación que imprime (sin peer dependencies) |
|
|
2194
2195
|
|
|
2195
2196
|
## Cambios recientes
|
|
2196
2197
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal ESC/POS builder: enough for a receipt and the cash drawer. Chainable; `bytes()` returns what to send.
|
|
3
|
+
*/
|
|
4
|
+
export declare class EscPosBuilder {
|
|
5
|
+
private readonly out;
|
|
6
|
+
/** Reset and select CP858 so accents print. */
|
|
7
|
+
init(): this;
|
|
8
|
+
text(value: string): this;
|
|
9
|
+
line(value?: string): this;
|
|
10
|
+
feed(lines?: number): this;
|
|
11
|
+
align(to: 'left' | 'center' | 'right'): this;
|
|
12
|
+
bold(on: boolean): this;
|
|
13
|
+
/** 1 = normal, 2 = double width and height. */
|
|
14
|
+
size(multiplier: 1 | 2): this;
|
|
15
|
+
/** Feed and cut (partial). */
|
|
16
|
+
cut(): this;
|
|
17
|
+
/**
|
|
18
|
+
* Pulse the cash drawer through the printer's RJ11 (the drawer has no connection of its own). Both kick pins are
|
|
19
|
+
* pulsed: drawers are wired to pin 2 or to pin 5 depending on the model, and a pulse on the wrong one does nothing.
|
|
20
|
+
*/
|
|
21
|
+
openDrawer(): this;
|
|
22
|
+
raw(bytes: ArrayLike<number>): this;
|
|
23
|
+
bytes(): Uint8Array;
|
|
24
|
+
/** What `PrintQueue.enqueue` takes as `payload`. */
|
|
25
|
+
toBase64(): string;
|
|
26
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }require('../chunk-LRX7AMSS.cjs');
|
|
2
|
+
|
|
3
|
+
// src/printing/print-queue.ts
|
|
4
|
+
var _crypto = require('crypto');
|
|
5
|
+
|
|
6
|
+
// src/printing/printing.types.ts
|
|
7
|
+
var DEFAULT_CONNECTION_TTL_MS = 45e3;
|
|
8
|
+
var DEFAULT_JOB_TTL_MS = 15 * 6e4;
|
|
9
|
+
var DEFAULT_CLAIM_TTL_MS = 6e4;
|
|
10
|
+
var DEFAULT_MAX_ATTEMPTS = 3;
|
|
11
|
+
var DEFAULT_MAX_PAYLOAD_CHARS = Math.ceil(256 * 1024 * 4 / 3);
|
|
12
|
+
|
|
13
|
+
// src/printing/print-queue.ts
|
|
14
|
+
var PrintQueue = class {
|
|
15
|
+
constructor(store, options = {}) {
|
|
16
|
+
this.store = store;
|
|
17
|
+
this.jobTtl = _nullishCoalesce(options.jobTtlMs, () => ( DEFAULT_JOB_TTL_MS));
|
|
18
|
+
this.claimTtl = _nullishCoalesce(options.claimTtlMs, () => ( DEFAULT_CLAIM_TTL_MS));
|
|
19
|
+
this.maxAttempts = _nullishCoalesce(options.maxAttempts, () => ( DEFAULT_MAX_ATTEMPTS));
|
|
20
|
+
this.maxPayload = _nullishCoalesce(options.maxPayloadChars, () => ( DEFAULT_MAX_PAYLOAD_CHARS));
|
|
21
|
+
this.now = _nullishCoalesce(options.now, () => ( Date.now));
|
|
22
|
+
}
|
|
23
|
+
/** Same `idempotencyKey` returns the job already queued (`duplicate: true`) instead of queuing a second one. */
|
|
24
|
+
async enqueue(input) {
|
|
25
|
+
if (!input.payload || input.payload.length > this.maxPayload || !/^[A-Za-z0-9+/]+={0,2}$/.test(input.payload)) {
|
|
26
|
+
throw new Error("invalid print payload");
|
|
27
|
+
}
|
|
28
|
+
const key = _nullishCoalesce(input.idempotencyKey, () => ( null));
|
|
29
|
+
if (key) {
|
|
30
|
+
const prior = await this.store.findByKey(input.tenant, key);
|
|
31
|
+
if (prior) return { job: prior, duplicate: true };
|
|
32
|
+
}
|
|
33
|
+
const now = this.now();
|
|
34
|
+
const job = {
|
|
35
|
+
id: _crypto.randomUUID.call(void 0, ),
|
|
36
|
+
tenant: input.tenant,
|
|
37
|
+
printerId: input.printerId,
|
|
38
|
+
use: input.use,
|
|
39
|
+
payload: input.payload,
|
|
40
|
+
status: "pending",
|
|
41
|
+
attempts: 0,
|
|
42
|
+
idempotencyKey: key,
|
|
43
|
+
error: null,
|
|
44
|
+
createdAt: now,
|
|
45
|
+
claimedAt: null,
|
|
46
|
+
claimedBy: null,
|
|
47
|
+
finishedAt: null,
|
|
48
|
+
expiresAt: now + this.jobTtl
|
|
49
|
+
};
|
|
50
|
+
await this.store.insert(job);
|
|
51
|
+
return { job, duplicate: false };
|
|
52
|
+
}
|
|
53
|
+
/** What a station asks for: the jobs of the printers it holds. Never hands the same job to two stations. */
|
|
54
|
+
async claim(tenant, stationId, printerIds, limit = 5) {
|
|
55
|
+
if (printerIds.length === 0) return [];
|
|
56
|
+
const now = this.now();
|
|
57
|
+
await this.store.expire(tenant, now);
|
|
58
|
+
return this.store.claim(tenant, printerIds, stationId, now, this.claimTtl, limit);
|
|
59
|
+
}
|
|
60
|
+
/** The station printed it. */
|
|
61
|
+
async done(tenant, jobId, stationId) {
|
|
62
|
+
return this.store.finish(tenant, jobId, stationId, { status: "done", error: null, finishedAt: this.now() });
|
|
63
|
+
}
|
|
64
|
+
/** The station could not print it: back to the queue until `maxAttempts`, then `failed`. Returns the final status. */
|
|
65
|
+
async fail(tenant, jobId, stationId, error) {
|
|
66
|
+
const job = await this.store.get(tenant, jobId);
|
|
67
|
+
if (!job) return null;
|
|
68
|
+
if (job.attempts >= this.maxAttempts) {
|
|
69
|
+
const ok = await this.store.finish(tenant, jobId, stationId, { status: "failed", error, finishedAt: this.now() });
|
|
70
|
+
return ok ? "failed" : null;
|
|
71
|
+
}
|
|
72
|
+
return await this.store.release(tenant, jobId, stationId, error) ? "pending" : null;
|
|
73
|
+
}
|
|
74
|
+
/** Jobs still waiting or being printed, oldest first (the «n en cola» of the screen). */
|
|
75
|
+
open(tenant) {
|
|
76
|
+
return this.store.listOpen(tenant);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/printing/memory.store.ts
|
|
81
|
+
var MemoryPrintJobStore = class {
|
|
82
|
+
constructor() {
|
|
83
|
+
this.rows = /* @__PURE__ */ new Map();
|
|
84
|
+
}
|
|
85
|
+
async insert(job) {
|
|
86
|
+
this.rows.set(job.id, { ...job });
|
|
87
|
+
}
|
|
88
|
+
async findByKey(tenant, key) {
|
|
89
|
+
for (const j of this.rows.values()) if (j.tenant === tenant && j.idempotencyKey === key) return { ...j };
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
async get(tenant, id) {
|
|
93
|
+
const j = this.rows.get(id);
|
|
94
|
+
return j && j.tenant === tenant ? { ...j } : null;
|
|
95
|
+
}
|
|
96
|
+
async claim(tenant, printerIds, by, now, claimTtlMs, limit) {
|
|
97
|
+
const taken = [];
|
|
98
|
+
const open = [...this.rows.values()].filter(
|
|
99
|
+
(j) => j.tenant === tenant && printerIds.includes(j.printerId) && (j.status === "pending" || j.status === "claimed" && now - (_nullishCoalesce(j.claimedAt, () => ( 0))) > claimTtlMs)
|
|
100
|
+
).sort((a, b) => a.createdAt - b.createdAt).slice(0, limit);
|
|
101
|
+
for (const j of open) {
|
|
102
|
+
j.status = "claimed";
|
|
103
|
+
j.claimedAt = now;
|
|
104
|
+
j.claimedBy = by;
|
|
105
|
+
j.attempts += 1;
|
|
106
|
+
taken.push({ ...j });
|
|
107
|
+
}
|
|
108
|
+
return taken;
|
|
109
|
+
}
|
|
110
|
+
async finish(tenant, id, by, patch) {
|
|
111
|
+
const j = this.rows.get(id);
|
|
112
|
+
if (!j || j.tenant !== tenant || j.status !== "claimed" || j.claimedBy !== by) return false;
|
|
113
|
+
Object.assign(j, patch);
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
async release(tenant, id, by, error) {
|
|
117
|
+
const j = this.rows.get(id);
|
|
118
|
+
if (!j || j.tenant !== tenant || j.status !== "claimed" || j.claimedBy !== by) return false;
|
|
119
|
+
j.status = "pending";
|
|
120
|
+
j.claimedAt = null;
|
|
121
|
+
j.claimedBy = null;
|
|
122
|
+
j.error = error;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
async listOpen(tenant) {
|
|
126
|
+
return [...this.rows.values()].filter((j) => j.tenant === tenant && (j.status === "pending" || j.status === "claimed")).sort((a, b) => a.createdAt - b.createdAt).map((j) => ({ ...j }));
|
|
127
|
+
}
|
|
128
|
+
async expire(tenant, now) {
|
|
129
|
+
let n = 0;
|
|
130
|
+
for (const [id, j] of this.rows) {
|
|
131
|
+
if (j.tenant === tenant && (j.status === "pending" || j.status === "claimed") && j.expiresAt <= now) {
|
|
132
|
+
this.rows.delete(id);
|
|
133
|
+
n += 1;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return n;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/printing/printer-status.ts
|
|
141
|
+
function printerStatus(printer, now, ttlMs = DEFAULT_CONNECTION_TTL_MS) {
|
|
142
|
+
if (!printer.station) return "never";
|
|
143
|
+
return now - printer.station.at <= ttlMs ? "connected" : "disconnected";
|
|
144
|
+
}
|
|
145
|
+
function pickPrinter(printers, use, now, ttlMs = DEFAULT_CONNECTION_TTL_MS) {
|
|
146
|
+
const serving = printers.filter((p) => p.uses.includes(use));
|
|
147
|
+
return _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(serving.find((p) => printerStatus(p, now, ttlMs) === "connected"), () => ( serving.find((p) => p.station))), () => ( serving[0])), () => ( null));
|
|
148
|
+
}
|
|
149
|
+
function disconnectedUses(printers, now, ttlMs = DEFAULT_CONNECTION_TTL_MS) {
|
|
150
|
+
const uses = new Set(printers.flatMap((p) => p.uses));
|
|
151
|
+
return [...uses].filter((u) => printers.every((p) => !p.uses.includes(u) || printerStatus(p, now, ttlMs) !== "connected"));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/printing/escpos.ts
|
|
155
|
+
var ESC = 27;
|
|
156
|
+
var GS = 29;
|
|
157
|
+
var LF = 10;
|
|
158
|
+
var CP858 = {
|
|
159
|
+
\u00C7: 128,
|
|
160
|
+
\u00FC: 129,
|
|
161
|
+
\u00E9: 130,
|
|
162
|
+
\u00E2: 131,
|
|
163
|
+
\u00E4: 132,
|
|
164
|
+
\u00E0: 133,
|
|
165
|
+
\u00E5: 134,
|
|
166
|
+
\u00E7: 135,
|
|
167
|
+
\u00EA: 136,
|
|
168
|
+
\u00EB: 137,
|
|
169
|
+
\u00E8: 138,
|
|
170
|
+
\u00EF: 139,
|
|
171
|
+
\u00EE: 140,
|
|
172
|
+
\u00EC: 141,
|
|
173
|
+
\u00C4: 142,
|
|
174
|
+
\u00C5: 143,
|
|
175
|
+
\u00C9: 144,
|
|
176
|
+
\u00E6: 145,
|
|
177
|
+
\u00C6: 146,
|
|
178
|
+
\u00F4: 147,
|
|
179
|
+
\u00F6: 148,
|
|
180
|
+
\u00F2: 149,
|
|
181
|
+
\u00FB: 150,
|
|
182
|
+
\u00F9: 151,
|
|
183
|
+
\u00FF: 152,
|
|
184
|
+
\u00D6: 153,
|
|
185
|
+
\u00DC: 154,
|
|
186
|
+
\u00F8: 155,
|
|
187
|
+
"\xA3": 156,
|
|
188
|
+
\u00D8: 157,
|
|
189
|
+
"\xD7": 158,
|
|
190
|
+
\u00E1: 160,
|
|
191
|
+
\u00ED: 161,
|
|
192
|
+
\u00F3: 162,
|
|
193
|
+
\u00FA: 163,
|
|
194
|
+
\u00F1: 164,
|
|
195
|
+
\u00D1: 165,
|
|
196
|
+
\u00AA: 166,
|
|
197
|
+
\u00BA: 167,
|
|
198
|
+
"\xBF": 168,
|
|
199
|
+
"\xAE": 169,
|
|
200
|
+
"\xAC": 170,
|
|
201
|
+
"\xBD": 171,
|
|
202
|
+
"\xBC": 172,
|
|
203
|
+
"\xA1": 173,
|
|
204
|
+
"\xAB": 174,
|
|
205
|
+
"\xBB": 175,
|
|
206
|
+
\u00C1: 181,
|
|
207
|
+
\u00C2: 182,
|
|
208
|
+
\u00C0: 183,
|
|
209
|
+
"\xA9": 184,
|
|
210
|
+
\u00C3: 198,
|
|
211
|
+
\u00E3: 199,
|
|
212
|
+
\u00CA: 210,
|
|
213
|
+
\u00CB: 211,
|
|
214
|
+
\u00C8: 212,
|
|
215
|
+
\u00CD: 214,
|
|
216
|
+
\u00CE: 215,
|
|
217
|
+
\u00CF: 216,
|
|
218
|
+
\u00CC: 222,
|
|
219
|
+
\u00D3: 224,
|
|
220
|
+
\u00D4: 226,
|
|
221
|
+
\u00D2: 227,
|
|
222
|
+
\u00D5: 228,
|
|
223
|
+
\u00F5: 229,
|
|
224
|
+
\u00DA: 233,
|
|
225
|
+
\u00DB: 234,
|
|
226
|
+
\u00D9: 235,
|
|
227
|
+
"\u20AC": 213
|
|
228
|
+
};
|
|
229
|
+
var EscPosBuilder = class {
|
|
230
|
+
constructor() {
|
|
231
|
+
this.out = [];
|
|
232
|
+
}
|
|
233
|
+
/** Reset and select CP858 so accents print. */
|
|
234
|
+
init() {
|
|
235
|
+
this.out.push(ESC, 64, ESC, 116, 19);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
text(value) {
|
|
239
|
+
for (const ch of value) {
|
|
240
|
+
const code = ch.codePointAt(0);
|
|
241
|
+
this.out.push(code < 128 ? code : _nullishCoalesce(CP858[ch], () => ( 63)));
|
|
242
|
+
}
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
line(value = "") {
|
|
246
|
+
return this.text(value).feed(1);
|
|
247
|
+
}
|
|
248
|
+
feed(lines = 1) {
|
|
249
|
+
for (let i = 0; i < lines; i += 1) this.out.push(LF);
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
align(to) {
|
|
253
|
+
this.out.push(ESC, 97, to === "left" ? 0 : to === "center" ? 1 : 2);
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
bold(on) {
|
|
257
|
+
this.out.push(ESC, 69, on ? 1 : 0);
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
/** 1 = normal, 2 = double width and height. */
|
|
261
|
+
size(multiplier) {
|
|
262
|
+
this.out.push(GS, 33, multiplier === 2 ? 17 : 0);
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
/** Feed and cut (partial). */
|
|
266
|
+
cut() {
|
|
267
|
+
this.out.push(GS, 86, 66, 3);
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Pulse the cash drawer through the printer's RJ11 (the drawer has no connection of its own). Both kick pins are
|
|
272
|
+
* pulsed: drawers are wired to pin 2 or to pin 5 depending on the model, and a pulse on the wrong one does nothing.
|
|
273
|
+
*/
|
|
274
|
+
openDrawer() {
|
|
275
|
+
this.out.push(ESC, 112, 0, 50, 200, ESC, 112, 1, 50, 200);
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
278
|
+
raw(bytes) {
|
|
279
|
+
for (let i = 0; i < bytes.length; i += 1) this.out.push(bytes[i] & 255);
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
bytes() {
|
|
283
|
+
return Uint8Array.from(this.out);
|
|
284
|
+
}
|
|
285
|
+
/** What `PrintQueue.enqueue` takes as `payload`. */
|
|
286
|
+
toBase64() {
|
|
287
|
+
return Buffer.from(this.bytes()).toString("base64");
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
exports.DEFAULT_CLAIM_TTL_MS = DEFAULT_CLAIM_TTL_MS; exports.DEFAULT_CONNECTION_TTL_MS = DEFAULT_CONNECTION_TTL_MS; exports.DEFAULT_JOB_TTL_MS = DEFAULT_JOB_TTL_MS; exports.DEFAULT_MAX_ATTEMPTS = DEFAULT_MAX_ATTEMPTS; exports.DEFAULT_MAX_PAYLOAD_CHARS = DEFAULT_MAX_PAYLOAD_CHARS; exports.EscPosBuilder = EscPosBuilder; exports.MemoryPrintJobStore = MemoryPrintJobStore; exports.PrintQueue = PrintQueue; exports.disconnectedUses = disconnectedUses; exports.pickPrinter = pickPrinter; exports.printerStatus = printerStatus;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { PrintQueue } from './print-queue.js';
|
|
2
|
+
export type { EnqueueResult } from './print-queue.js';
|
|
3
|
+
export { MemoryPrintJobStore } from './memory.store.js';
|
|
4
|
+
export { printerStatus, pickPrinter, disconnectedUses } from './printer-status.js';
|
|
5
|
+
export { EscPosBuilder } from './escpos.js';
|
|
6
|
+
export { DEFAULT_CLAIM_TTL_MS, DEFAULT_CONNECTION_TTL_MS, DEFAULT_JOB_TTL_MS, DEFAULT_MAX_ATTEMPTS, DEFAULT_MAX_PAYLOAD_CHARS, } from './printing.types.js';
|
|
7
|
+
export type { EnqueueInput, PrintJob, PrintJobStatus, PrintJobStorePort, PrintQueueOptions, PrinterRecord, PrinterStation, PrinterStatus, PrinterTransport, } from './printing.types.js';
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import "../chunk-D3DDJWUD.js";
|
|
2
|
+
|
|
3
|
+
// src/printing/print-queue.ts
|
|
4
|
+
import { randomUUID } from "crypto";
|
|
5
|
+
|
|
6
|
+
// src/printing/printing.types.ts
|
|
7
|
+
var DEFAULT_CONNECTION_TTL_MS = 45e3;
|
|
8
|
+
var DEFAULT_JOB_TTL_MS = 15 * 6e4;
|
|
9
|
+
var DEFAULT_CLAIM_TTL_MS = 6e4;
|
|
10
|
+
var DEFAULT_MAX_ATTEMPTS = 3;
|
|
11
|
+
var DEFAULT_MAX_PAYLOAD_CHARS = Math.ceil(256 * 1024 * 4 / 3);
|
|
12
|
+
|
|
13
|
+
// src/printing/print-queue.ts
|
|
14
|
+
var PrintQueue = class {
|
|
15
|
+
constructor(store, options = {}) {
|
|
16
|
+
this.store = store;
|
|
17
|
+
this.jobTtl = options.jobTtlMs ?? DEFAULT_JOB_TTL_MS;
|
|
18
|
+
this.claimTtl = options.claimTtlMs ?? DEFAULT_CLAIM_TTL_MS;
|
|
19
|
+
this.maxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
|
|
20
|
+
this.maxPayload = options.maxPayloadChars ?? DEFAULT_MAX_PAYLOAD_CHARS;
|
|
21
|
+
this.now = options.now ?? Date.now;
|
|
22
|
+
}
|
|
23
|
+
/** Same `idempotencyKey` returns the job already queued (`duplicate: true`) instead of queuing a second one. */
|
|
24
|
+
async enqueue(input) {
|
|
25
|
+
if (!input.payload || input.payload.length > this.maxPayload || !/^[A-Za-z0-9+/]+={0,2}$/.test(input.payload)) {
|
|
26
|
+
throw new Error("invalid print payload");
|
|
27
|
+
}
|
|
28
|
+
const key = input.idempotencyKey ?? null;
|
|
29
|
+
if (key) {
|
|
30
|
+
const prior = await this.store.findByKey(input.tenant, key);
|
|
31
|
+
if (prior) return { job: prior, duplicate: true };
|
|
32
|
+
}
|
|
33
|
+
const now = this.now();
|
|
34
|
+
const job = {
|
|
35
|
+
id: randomUUID(),
|
|
36
|
+
tenant: input.tenant,
|
|
37
|
+
printerId: input.printerId,
|
|
38
|
+
use: input.use,
|
|
39
|
+
payload: input.payload,
|
|
40
|
+
status: "pending",
|
|
41
|
+
attempts: 0,
|
|
42
|
+
idempotencyKey: key,
|
|
43
|
+
error: null,
|
|
44
|
+
createdAt: now,
|
|
45
|
+
claimedAt: null,
|
|
46
|
+
claimedBy: null,
|
|
47
|
+
finishedAt: null,
|
|
48
|
+
expiresAt: now + this.jobTtl
|
|
49
|
+
};
|
|
50
|
+
await this.store.insert(job);
|
|
51
|
+
return { job, duplicate: false };
|
|
52
|
+
}
|
|
53
|
+
/** What a station asks for: the jobs of the printers it holds. Never hands the same job to two stations. */
|
|
54
|
+
async claim(tenant, stationId, printerIds, limit = 5) {
|
|
55
|
+
if (printerIds.length === 0) return [];
|
|
56
|
+
const now = this.now();
|
|
57
|
+
await this.store.expire(tenant, now);
|
|
58
|
+
return this.store.claim(tenant, printerIds, stationId, now, this.claimTtl, limit);
|
|
59
|
+
}
|
|
60
|
+
/** The station printed it. */
|
|
61
|
+
async done(tenant, jobId, stationId) {
|
|
62
|
+
return this.store.finish(tenant, jobId, stationId, { status: "done", error: null, finishedAt: this.now() });
|
|
63
|
+
}
|
|
64
|
+
/** The station could not print it: back to the queue until `maxAttempts`, then `failed`. Returns the final status. */
|
|
65
|
+
async fail(tenant, jobId, stationId, error) {
|
|
66
|
+
const job = await this.store.get(tenant, jobId);
|
|
67
|
+
if (!job) return null;
|
|
68
|
+
if (job.attempts >= this.maxAttempts) {
|
|
69
|
+
const ok = await this.store.finish(tenant, jobId, stationId, { status: "failed", error, finishedAt: this.now() });
|
|
70
|
+
return ok ? "failed" : null;
|
|
71
|
+
}
|
|
72
|
+
return await this.store.release(tenant, jobId, stationId, error) ? "pending" : null;
|
|
73
|
+
}
|
|
74
|
+
/** Jobs still waiting or being printed, oldest first (the «n en cola» of the screen). */
|
|
75
|
+
open(tenant) {
|
|
76
|
+
return this.store.listOpen(tenant);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/printing/memory.store.ts
|
|
81
|
+
var MemoryPrintJobStore = class {
|
|
82
|
+
constructor() {
|
|
83
|
+
this.rows = /* @__PURE__ */ new Map();
|
|
84
|
+
}
|
|
85
|
+
async insert(job) {
|
|
86
|
+
this.rows.set(job.id, { ...job });
|
|
87
|
+
}
|
|
88
|
+
async findByKey(tenant, key) {
|
|
89
|
+
for (const j of this.rows.values()) if (j.tenant === tenant && j.idempotencyKey === key) return { ...j };
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
async get(tenant, id) {
|
|
93
|
+
const j = this.rows.get(id);
|
|
94
|
+
return j && j.tenant === tenant ? { ...j } : null;
|
|
95
|
+
}
|
|
96
|
+
async claim(tenant, printerIds, by, now, claimTtlMs, limit) {
|
|
97
|
+
const taken = [];
|
|
98
|
+
const open = [...this.rows.values()].filter(
|
|
99
|
+
(j) => j.tenant === tenant && printerIds.includes(j.printerId) && (j.status === "pending" || j.status === "claimed" && now - (j.claimedAt ?? 0) > claimTtlMs)
|
|
100
|
+
).sort((a, b) => a.createdAt - b.createdAt).slice(0, limit);
|
|
101
|
+
for (const j of open) {
|
|
102
|
+
j.status = "claimed";
|
|
103
|
+
j.claimedAt = now;
|
|
104
|
+
j.claimedBy = by;
|
|
105
|
+
j.attempts += 1;
|
|
106
|
+
taken.push({ ...j });
|
|
107
|
+
}
|
|
108
|
+
return taken;
|
|
109
|
+
}
|
|
110
|
+
async finish(tenant, id, by, patch) {
|
|
111
|
+
const j = this.rows.get(id);
|
|
112
|
+
if (!j || j.tenant !== tenant || j.status !== "claimed" || j.claimedBy !== by) return false;
|
|
113
|
+
Object.assign(j, patch);
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
async release(tenant, id, by, error) {
|
|
117
|
+
const j = this.rows.get(id);
|
|
118
|
+
if (!j || j.tenant !== tenant || j.status !== "claimed" || j.claimedBy !== by) return false;
|
|
119
|
+
j.status = "pending";
|
|
120
|
+
j.claimedAt = null;
|
|
121
|
+
j.claimedBy = null;
|
|
122
|
+
j.error = error;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
async listOpen(tenant) {
|
|
126
|
+
return [...this.rows.values()].filter((j) => j.tenant === tenant && (j.status === "pending" || j.status === "claimed")).sort((a, b) => a.createdAt - b.createdAt).map((j) => ({ ...j }));
|
|
127
|
+
}
|
|
128
|
+
async expire(tenant, now) {
|
|
129
|
+
let n = 0;
|
|
130
|
+
for (const [id, j] of this.rows) {
|
|
131
|
+
if (j.tenant === tenant && (j.status === "pending" || j.status === "claimed") && j.expiresAt <= now) {
|
|
132
|
+
this.rows.delete(id);
|
|
133
|
+
n += 1;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return n;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/printing/printer-status.ts
|
|
141
|
+
function printerStatus(printer, now, ttlMs = DEFAULT_CONNECTION_TTL_MS) {
|
|
142
|
+
if (!printer.station) return "never";
|
|
143
|
+
return now - printer.station.at <= ttlMs ? "connected" : "disconnected";
|
|
144
|
+
}
|
|
145
|
+
function pickPrinter(printers, use, now, ttlMs = DEFAULT_CONNECTION_TTL_MS) {
|
|
146
|
+
const serving = printers.filter((p) => p.uses.includes(use));
|
|
147
|
+
return serving.find((p) => printerStatus(p, now, ttlMs) === "connected") ?? serving.find((p) => p.station) ?? serving[0] ?? null;
|
|
148
|
+
}
|
|
149
|
+
function disconnectedUses(printers, now, ttlMs = DEFAULT_CONNECTION_TTL_MS) {
|
|
150
|
+
const uses = new Set(printers.flatMap((p) => p.uses));
|
|
151
|
+
return [...uses].filter((u) => printers.every((p) => !p.uses.includes(u) || printerStatus(p, now, ttlMs) !== "connected"));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/printing/escpos.ts
|
|
155
|
+
var ESC = 27;
|
|
156
|
+
var GS = 29;
|
|
157
|
+
var LF = 10;
|
|
158
|
+
var CP858 = {
|
|
159
|
+
\u00C7: 128,
|
|
160
|
+
\u00FC: 129,
|
|
161
|
+
\u00E9: 130,
|
|
162
|
+
\u00E2: 131,
|
|
163
|
+
\u00E4: 132,
|
|
164
|
+
\u00E0: 133,
|
|
165
|
+
\u00E5: 134,
|
|
166
|
+
\u00E7: 135,
|
|
167
|
+
\u00EA: 136,
|
|
168
|
+
\u00EB: 137,
|
|
169
|
+
\u00E8: 138,
|
|
170
|
+
\u00EF: 139,
|
|
171
|
+
\u00EE: 140,
|
|
172
|
+
\u00EC: 141,
|
|
173
|
+
\u00C4: 142,
|
|
174
|
+
\u00C5: 143,
|
|
175
|
+
\u00C9: 144,
|
|
176
|
+
\u00E6: 145,
|
|
177
|
+
\u00C6: 146,
|
|
178
|
+
\u00F4: 147,
|
|
179
|
+
\u00F6: 148,
|
|
180
|
+
\u00F2: 149,
|
|
181
|
+
\u00FB: 150,
|
|
182
|
+
\u00F9: 151,
|
|
183
|
+
\u00FF: 152,
|
|
184
|
+
\u00D6: 153,
|
|
185
|
+
\u00DC: 154,
|
|
186
|
+
\u00F8: 155,
|
|
187
|
+
"\xA3": 156,
|
|
188
|
+
\u00D8: 157,
|
|
189
|
+
"\xD7": 158,
|
|
190
|
+
\u00E1: 160,
|
|
191
|
+
\u00ED: 161,
|
|
192
|
+
\u00F3: 162,
|
|
193
|
+
\u00FA: 163,
|
|
194
|
+
\u00F1: 164,
|
|
195
|
+
\u00D1: 165,
|
|
196
|
+
\u00AA: 166,
|
|
197
|
+
\u00BA: 167,
|
|
198
|
+
"\xBF": 168,
|
|
199
|
+
"\xAE": 169,
|
|
200
|
+
"\xAC": 170,
|
|
201
|
+
"\xBD": 171,
|
|
202
|
+
"\xBC": 172,
|
|
203
|
+
"\xA1": 173,
|
|
204
|
+
"\xAB": 174,
|
|
205
|
+
"\xBB": 175,
|
|
206
|
+
\u00C1: 181,
|
|
207
|
+
\u00C2: 182,
|
|
208
|
+
\u00C0: 183,
|
|
209
|
+
"\xA9": 184,
|
|
210
|
+
\u00C3: 198,
|
|
211
|
+
\u00E3: 199,
|
|
212
|
+
\u00CA: 210,
|
|
213
|
+
\u00CB: 211,
|
|
214
|
+
\u00C8: 212,
|
|
215
|
+
\u00CD: 214,
|
|
216
|
+
\u00CE: 215,
|
|
217
|
+
\u00CF: 216,
|
|
218
|
+
\u00CC: 222,
|
|
219
|
+
\u00D3: 224,
|
|
220
|
+
\u00D4: 226,
|
|
221
|
+
\u00D2: 227,
|
|
222
|
+
\u00D5: 228,
|
|
223
|
+
\u00F5: 229,
|
|
224
|
+
\u00DA: 233,
|
|
225
|
+
\u00DB: 234,
|
|
226
|
+
\u00D9: 235,
|
|
227
|
+
"\u20AC": 213
|
|
228
|
+
};
|
|
229
|
+
var EscPosBuilder = class {
|
|
230
|
+
constructor() {
|
|
231
|
+
this.out = [];
|
|
232
|
+
}
|
|
233
|
+
/** Reset and select CP858 so accents print. */
|
|
234
|
+
init() {
|
|
235
|
+
this.out.push(ESC, 64, ESC, 116, 19);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
text(value) {
|
|
239
|
+
for (const ch of value) {
|
|
240
|
+
const code = ch.codePointAt(0);
|
|
241
|
+
this.out.push(code < 128 ? code : CP858[ch] ?? 63);
|
|
242
|
+
}
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
line(value = "") {
|
|
246
|
+
return this.text(value).feed(1);
|
|
247
|
+
}
|
|
248
|
+
feed(lines = 1) {
|
|
249
|
+
for (let i = 0; i < lines; i += 1) this.out.push(LF);
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
align(to) {
|
|
253
|
+
this.out.push(ESC, 97, to === "left" ? 0 : to === "center" ? 1 : 2);
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
bold(on) {
|
|
257
|
+
this.out.push(ESC, 69, on ? 1 : 0);
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
/** 1 = normal, 2 = double width and height. */
|
|
261
|
+
size(multiplier) {
|
|
262
|
+
this.out.push(GS, 33, multiplier === 2 ? 17 : 0);
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
/** Feed and cut (partial). */
|
|
266
|
+
cut() {
|
|
267
|
+
this.out.push(GS, 86, 66, 3);
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Pulse the cash drawer through the printer's RJ11 (the drawer has no connection of its own). Both kick pins are
|
|
272
|
+
* pulsed: drawers are wired to pin 2 or to pin 5 depending on the model, and a pulse on the wrong one does nothing.
|
|
273
|
+
*/
|
|
274
|
+
openDrawer() {
|
|
275
|
+
this.out.push(ESC, 112, 0, 50, 200, ESC, 112, 1, 50, 200);
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
278
|
+
raw(bytes) {
|
|
279
|
+
for (let i = 0; i < bytes.length; i += 1) this.out.push(bytes[i] & 255);
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
bytes() {
|
|
283
|
+
return Uint8Array.from(this.out);
|
|
284
|
+
}
|
|
285
|
+
/** What `PrintQueue.enqueue` takes as `payload`. */
|
|
286
|
+
toBase64() {
|
|
287
|
+
return Buffer.from(this.bytes()).toString("base64");
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
export {
|
|
291
|
+
DEFAULT_CLAIM_TTL_MS,
|
|
292
|
+
DEFAULT_CONNECTION_TTL_MS,
|
|
293
|
+
DEFAULT_JOB_TTL_MS,
|
|
294
|
+
DEFAULT_MAX_ATTEMPTS,
|
|
295
|
+
DEFAULT_MAX_PAYLOAD_CHARS,
|
|
296
|
+
EscPosBuilder,
|
|
297
|
+
MemoryPrintJobStore,
|
|
298
|
+
PrintQueue,
|
|
299
|
+
disconnectedUses,
|
|
300
|
+
pickPrinter,
|
|
301
|
+
printerStatus
|
|
302
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { PrintJob, PrintJobStorePort } from './printing.types.js';
|
|
2
|
+
/** In-process store: tests and a single instance. Atomic by being synchronous. */
|
|
3
|
+
export declare class MemoryPrintJobStore implements PrintJobStorePort {
|
|
4
|
+
private readonly rows;
|
|
5
|
+
insert(job: PrintJob): Promise<void>;
|
|
6
|
+
findByKey(tenant: string, key: string): Promise<{
|
|
7
|
+
id: string;
|
|
8
|
+
tenant: string;
|
|
9
|
+
printerId: string;
|
|
10
|
+
use: string;
|
|
11
|
+
payload: string;
|
|
12
|
+
status: import("./printing.types.js").PrintJobStatus;
|
|
13
|
+
attempts: number;
|
|
14
|
+
idempotencyKey: string | null;
|
|
15
|
+
error: string | null;
|
|
16
|
+
createdAt: number;
|
|
17
|
+
claimedAt: number | null;
|
|
18
|
+
claimedBy: string | null;
|
|
19
|
+
finishedAt: number | null;
|
|
20
|
+
expiresAt: number;
|
|
21
|
+
} | null>;
|
|
22
|
+
get(tenant: string, id: string): Promise<{
|
|
23
|
+
id: string;
|
|
24
|
+
tenant: string;
|
|
25
|
+
printerId: string;
|
|
26
|
+
use: string;
|
|
27
|
+
payload: string;
|
|
28
|
+
status: import("./printing.types.js").PrintJobStatus;
|
|
29
|
+
attempts: number;
|
|
30
|
+
idempotencyKey: string | null;
|
|
31
|
+
error: string | null;
|
|
32
|
+
createdAt: number;
|
|
33
|
+
claimedAt: number | null;
|
|
34
|
+
claimedBy: string | null;
|
|
35
|
+
finishedAt: number | null;
|
|
36
|
+
expiresAt: number;
|
|
37
|
+
} | null>;
|
|
38
|
+
claim(tenant: string, printerIds: string[], by: string, now: number, claimTtlMs: number, limit: number): Promise<PrintJob[]>;
|
|
39
|
+
finish(tenant: string, id: string, by: string, patch: Pick<PrintJob, 'status' | 'error' | 'finishedAt'>): Promise<boolean>;
|
|
40
|
+
release(tenant: string, id: string, by: string, error: string | null): Promise<boolean>;
|
|
41
|
+
listOpen(tenant: string): Promise<{
|
|
42
|
+
id: string;
|
|
43
|
+
tenant: string;
|
|
44
|
+
printerId: string;
|
|
45
|
+
use: string;
|
|
46
|
+
payload: string;
|
|
47
|
+
status: import("./printing.types.js").PrintJobStatus;
|
|
48
|
+
attempts: number;
|
|
49
|
+
idempotencyKey: string | null;
|
|
50
|
+
error: string | null;
|
|
51
|
+
createdAt: number;
|
|
52
|
+
claimedAt: number | null;
|
|
53
|
+
claimedBy: string | null;
|
|
54
|
+
finishedAt: number | null;
|
|
55
|
+
expiresAt: number;
|
|
56
|
+
}[]>;
|
|
57
|
+
expire(tenant: string, now: number): Promise<number>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type EnqueueInput, type PrintJob, type PrintJobStorePort, type PrintQueueOptions } from './printing.types.js';
|
|
2
|
+
export type EnqueueResult = {
|
|
3
|
+
job: PrintJob;
|
|
4
|
+
duplicate: boolean;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Print jobs between the devices that sell and the station that holds the printer. Framework-free: the app wraps it
|
|
8
|
+
* in its own controller, tenancy and permissions.
|
|
9
|
+
*/
|
|
10
|
+
export declare class PrintQueue {
|
|
11
|
+
private readonly store;
|
|
12
|
+
private readonly jobTtl;
|
|
13
|
+
private readonly claimTtl;
|
|
14
|
+
private readonly maxAttempts;
|
|
15
|
+
private readonly maxPayload;
|
|
16
|
+
private readonly now;
|
|
17
|
+
constructor(store: PrintJobStorePort, options?: PrintQueueOptions);
|
|
18
|
+
/** Same `idempotencyKey` returns the job already queued (`duplicate: true`) instead of queuing a second one. */
|
|
19
|
+
enqueue(input: EnqueueInput): Promise<EnqueueResult>;
|
|
20
|
+
/** What a station asks for: the jobs of the printers it holds. Never hands the same job to two stations. */
|
|
21
|
+
claim(tenant: string, stationId: string, printerIds: string[], limit?: number): Promise<PrintJob[]>;
|
|
22
|
+
/** The station printed it. */
|
|
23
|
+
done(tenant: string, jobId: string, stationId: string): Promise<boolean>;
|
|
24
|
+
/** The station could not print it: back to the queue until `maxAttempts`, then `failed`. Returns the final status. */
|
|
25
|
+
fail(tenant: string, jobId: string, stationId: string, error: string): Promise<'pending' | 'failed' | null>;
|
|
26
|
+
/** Jobs still waiting or being printed, oldest first (the «n en cola» of the screen). */
|
|
27
|
+
open(tenant: string): Promise<PrintJob[]>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type PrinterRecord, type PrinterStatus } from './printing.types.js';
|
|
2
|
+
/** Connected only if a station said so recently: a station that closed the app never says «disconnected». */
|
|
3
|
+
export declare function printerStatus(printer: Pick<PrinterRecord, 'station'>, now: number, ttlMs?: number): PrinterStatus;
|
|
4
|
+
/**
|
|
5
|
+
* Printer that should serve a `use`: a connected one first, else a registered one that is down (its job waits in the
|
|
6
|
+
* queue), else null (nothing registered for that use: the caller decides what to do, e.g. print locally).
|
|
7
|
+
*/
|
|
8
|
+
export declare function pickPrinter<P extends Pick<PrinterRecord, 'uses' | 'station'>>(printers: readonly P[], use: string, now: number, ttlMs?: number): P | null;
|
|
9
|
+
/** Uses that have a registered printer but none connected: what the register screen warns about. */
|
|
10
|
+
export declare function disconnectedUses(printers: readonly Pick<PrinterRecord, 'uses' | 'station'>[], now: number, ttlMs?: number): string[];
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/** How the printer is reached by the device that holds it. */
|
|
2
|
+
export type PrinterTransport = 'usb' | 'bluetooth' | 'serial' | 'network';
|
|
3
|
+
/**
|
|
4
|
+
* A printer shared by the whole tenant. `uses` is whatever the app defines (`receipt`, `order`, `label`…): the
|
|
5
|
+
* library never interprets it, it only routes by it. One printer can serve several uses.
|
|
6
|
+
*/
|
|
7
|
+
export interface PrinterRecord {
|
|
8
|
+
id: string;
|
|
9
|
+
tenant: string;
|
|
10
|
+
name: string;
|
|
11
|
+
transport: PrinterTransport;
|
|
12
|
+
paperMm: number;
|
|
13
|
+
uses: string[];
|
|
14
|
+
/** Identity of the physical printer as its host device sees it (USB ids, Bluetooth address…). Lets a station match it. */
|
|
15
|
+
localKey: string | null;
|
|
16
|
+
/** Last device that reported the printer connected. Kept after it drops, so the UI can say who had it last. */
|
|
17
|
+
station: PrinterStation | null;
|
|
18
|
+
}
|
|
19
|
+
export interface PrinterStation {
|
|
20
|
+
deviceId: string;
|
|
21
|
+
deviceName: string;
|
|
22
|
+
userId: string | null;
|
|
23
|
+
userName: string | null;
|
|
24
|
+
/** Epoch ms of the last heartbeat that said «connected». */
|
|
25
|
+
at: number;
|
|
26
|
+
}
|
|
27
|
+
/** `never`: no station ever reported it. */
|
|
28
|
+
export type PrinterStatus = 'connected' | 'disconnected' | 'never';
|
|
29
|
+
export type PrintJobStatus = 'pending' | 'claimed' | 'done' | 'failed';
|
|
30
|
+
export interface PrintJob {
|
|
31
|
+
id: string;
|
|
32
|
+
tenant: string;
|
|
33
|
+
printerId: string;
|
|
34
|
+
/** One of the printer's `uses`. */
|
|
35
|
+
use: string;
|
|
36
|
+
/** Base64 of the bytes to send as they are (ESC/POS, TSPL…). The library never looks inside. */
|
|
37
|
+
payload: string;
|
|
38
|
+
status: PrintJobStatus;
|
|
39
|
+
attempts: number;
|
|
40
|
+
/** Same key, same job: a client that retries after a timeout does not print twice. */
|
|
41
|
+
idempotencyKey: string | null;
|
|
42
|
+
error: string | null;
|
|
43
|
+
createdAt: number;
|
|
44
|
+
claimedAt: number | null;
|
|
45
|
+
claimedBy: string | null;
|
|
46
|
+
finishedAt: number | null;
|
|
47
|
+
/** A job nobody printed by then is dropped: a receipt printed an hour late is worse than none. */
|
|
48
|
+
expiresAt: number;
|
|
49
|
+
}
|
|
50
|
+
export interface EnqueueInput {
|
|
51
|
+
tenant: string;
|
|
52
|
+
printerId: string;
|
|
53
|
+
use: string;
|
|
54
|
+
payload: string;
|
|
55
|
+
idempotencyKey?: string | null;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Persistence. `claim` MUST be atomic (two stations asking at once must not get the same job): in SQL,
|
|
59
|
+
* `UPDATE … WHERE id IN (SELECT … FOR UPDATE SKIP LOCKED) RETURNING *`.
|
|
60
|
+
*/
|
|
61
|
+
export interface PrintJobStorePort {
|
|
62
|
+
insert(job: PrintJob): Promise<void>;
|
|
63
|
+
findByKey(tenant: string, key: string): Promise<PrintJob | null>;
|
|
64
|
+
get(tenant: string, id: string): Promise<PrintJob | null>;
|
|
65
|
+
/**
|
|
66
|
+
* Pending jobs of those printers (plus claimed ones whose claim went stale), oldest first, marked claimed by `by`
|
|
67
|
+
* with `attempts` incremented (the count lives in the store so it survives a restart and works across instances).
|
|
68
|
+
*/
|
|
69
|
+
claim(tenant: string, printerIds: string[], by: string, now: number, claimTtlMs: number, limit: number): Promise<PrintJob[]>;
|
|
70
|
+
/** Only the device that claimed it can finish it. True if it changed. */
|
|
71
|
+
finish(tenant: string, id: string, by: string, patch: Pick<PrintJob, 'status' | 'error' | 'finishedAt'>): Promise<boolean>;
|
|
72
|
+
/** Back to pending (a failed attempt that may be retried). True if it changed. */
|
|
73
|
+
release(tenant: string, id: string, by: string, error: string | null): Promise<boolean>;
|
|
74
|
+
/** Pending/claimed jobs of the tenant, oldest first. */
|
|
75
|
+
listOpen(tenant: string): Promise<PrintJob[]>;
|
|
76
|
+
/** Drop the tenant's pending/claimed jobs past `expiresAt`; returns how many. */
|
|
77
|
+
expire(tenant: string, now: number): Promise<number>;
|
|
78
|
+
}
|
|
79
|
+
export interface PrintQueueOptions {
|
|
80
|
+
/** A station that has not reported for this long is «disconnected». Default 45 s (a heartbeat every 15–20 s). */
|
|
81
|
+
connectionTtlMs?: number;
|
|
82
|
+
/** A job lives this long before it is dropped. Default 15 min. */
|
|
83
|
+
jobTtlMs?: number;
|
|
84
|
+
/** A claim not finished after this long goes back to the queue. Default 60 s. */
|
|
85
|
+
claimTtlMs?: number;
|
|
86
|
+
/** Failed attempts before the job is marked `failed`. Default 3. */
|
|
87
|
+
maxAttempts?: number;
|
|
88
|
+
/** Largest payload (base64 chars) accepted. Default 256 KiB of bytes. */
|
|
89
|
+
maxPayloadChars?: number;
|
|
90
|
+
now?: () => number;
|
|
91
|
+
}
|
|
92
|
+
export declare const DEFAULT_CONNECTION_TTL_MS = 45000;
|
|
93
|
+
export declare const DEFAULT_JOB_TTL_MS: number;
|
|
94
|
+
export declare const DEFAULT_CLAIM_TTL_MS = 60000;
|
|
95
|
+
export declare const DEFAULT_MAX_ATTEMPTS = 3;
|
|
96
|
+
export declare const DEFAULT_MAX_PAYLOAD_CHARS: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miguelmorales13/nestkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -156,6 +156,11 @@
|
|
|
156
156
|
"import": "./dist/payments/index.js",
|
|
157
157
|
"require": "./dist/payments/index.cjs"
|
|
158
158
|
},
|
|
159
|
+
"./printing": {
|
|
160
|
+
"types": "./dist/printing/index.d.ts",
|
|
161
|
+
"import": "./dist/printing/index.js",
|
|
162
|
+
"require": "./dist/printing/index.cjs"
|
|
163
|
+
},
|
|
159
164
|
"./security": {
|
|
160
165
|
"types": "./dist/security/index.d.ts",
|
|
161
166
|
"import": "./dist/security/index.js",
|