@slastra/tsplib 0.0.0-stage → 0.2.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/FINDINGS.md ADDED
@@ -0,0 +1,89 @@
1
+ # Findings: Labeer Y43BT and Core Innovations CTP800BD over BLE
2
+
3
+ How the Y43BT's BLE interface was mapped, on 2026-11-03, with a Labeer Y43BT on the bench and [bleak](https://github.com/hbldh/bleak) on Linux (BlueZ). Every claim below was checked on paper or on the wire.
4
+
5
+ ## The advertisement
6
+
7
+ The printer is dual-mode and shows up twice:
8
+
9
+ | Name | Address | Transport |
10
+ | --------------- | ---------------------------- | ----------------- |
11
+ | `Y43BT-69B2` | `28:D4:1E:FA:69:B2` | classic Bluetooth |
12
+ | `Y43BT-69B2_LE` | `E8:D4:1E:FA:69:B2` (random) | BLE |
13
+
14
+ The LE advertisement lists service UUIDs `18f0` and `fee7` (the Tencent/WeChat service), and manufacturer data carrying the classic MAC.
15
+
16
+ ## The GATT table
17
+
18
+ | Service | Characteristics | What it turned out to be |
19
+ | ---------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- |
20
+ | `18f0` | `2af1` write, `2af0` notify | The usual home of TSPL on cheap BLE printers. **Accepts writes and does nothing.** No reply, no motor, no print. |
21
+ | `ff00` | `ff02` write, `ff01` notify, `ff03` notify | **The printer.** TSPL on `ff02`, credit flow on `ff03`. |
22
+ | `ff80` | `ff82` write, `ff81` notify | The BT module's AT command channel: anything else gets `\r\nERROR\r\n`. |
23
+ | `49535343-fe7d-…` | ISSC transparent UART | Answers every write with `00 00 00 f7 01`, whatever was sent. |
24
+ | `fff0`, `ff10`, `fee7` | assorted | Silent to everything tried. |
25
+
26
+ ## Finding the channel
27
+
28
+ Status queries first, since they print nothing: TSPL `~!T` (model name), `ESC !?` (status byte) and `~!I`, `~!F`, plus ESC/POS `DLE EOT 1` and `GS I C`, sent to every writable characteristic in turn with every notify characteristic subscribed. **None got a printer reply on any channel.** That left a test print as the only way to tell.
29
+
30
+ A minimal TSPL job (`SIZE`, `GAP`, `DIRECTION`, `DENSITY`, `CLS`, `BOX`, `TEXT`, `BAR`, `PRINT`) on `2af1` did nothing at all. The same job on `ff02` started the motor, and on a clean paper path printed exactly as drawn.
31
+
32
+ ## The credit channel (`ff03`)
33
+
34
+ On subscribing, `ff03` notifies two messages at once:
35
+
36
+ ```
37
+ 01 07 7 credits
38
+ 02 f4 00 244, little-endian: the largest write the module takes
39
+ ```
40
+
41
+ Every write to `ff02` is then answered with `01 01`: one credit back. A job sent spending one credit per write, and waiting when none were left, never stalled. One credit covers one write of any size up to the limit, so a 240-byte write costs no more than a 20-byte one.
42
+
43
+ ## Rasters
44
+
45
+ `BITMAP x,y,widthBytes,height,0,<data>` with mode 0 (overwrite) prints correctly with MSB-first packing and **1 = white**, the polarity already used by working USB TSPL code for a different printer.
46
+
47
+ An 800 × 600 raster (60 KB) with a millimetre ruler, large text, and 1-, 2- and 4-dot checkerboards printed flawlessly. The 1-dot checker came out as an even grey, which confirms the advertised 203 dpi (8 dots/mm). At a 247-byte ATT MTU with 240-byte writes, the whole job transferred in 2.7 s.
48
+
49
+ ## Units and width
50
+
51
+ `SIZE 101.6 mm, 152.4 mm` and `GAP 3 mm, 0 mm` are accepted, so millimetres work as well as inches.
52
+
53
+ A 112 mm ruler printed under that `SIZE` stopped right after its 101 mm tick. That is the 813-dot label width, not the head: TSPL clips at `SIZE`. So the head prints at least 101.6 mm, and its real width is still unmeasured.
54
+
55
+ ## Media
56
+
57
+ 4 × 6 in gap stock prints cleanly. 2 × 1 in stock first printed the start of a label and then jammed, twice, on adhesive left in the paper path. Once that was cleaned out it printed without issue from printrow, placed correctly with the raster at `x = 0`, so the printer positions a narrow label from `SIZE` itself.
58
+
59
+ ## Still open
60
+
61
+ - The true head width, which needs a `SIZE` wider than 4 in.
62
+ - Any status query this printer answers, if one exists. The vendor app (Flashlabel Pro) would show it in an HCI snoop.
63
+
64
+ ## Core Innovations CTP800BD
65
+
66
+ Probed on 2026-11-04, with raw LE ATT sockets on Linux (bluetoothd bypassed, for the reason below) and then from Chrome.
67
+
68
+ ### Same module, different name
69
+
70
+ It advertises as `Shipping Printer-B863` on a **public** address, `02:3F:05:B8:B8:63`, with service UUIDs `e7810a71-73ae-499d-8c15-faa9aef0c3f2` and the classic SPP `1101`. The GATT table matches the Y43BT's where it matters: `ff00` with `ff02` write, `ff01` and `ff03` notify, and `18f0`/`2af1` again. It also carries `e7810a71-…` (characteristic `bef8d6c9-…`) and the ISSC UART service, but no `ff80` AT channel.
71
+
72
+ TSPL on `ff02` prints: first a hand-written `TEXT`/`BOX`/`BAR` job, then a full 4 × 6 in raster from `buildLabel` (816 × 1216, 124,133 bytes) with an edge border, a millimetre ruler and 1-, 2- and 4-dot checkerboards. It came out complete and clean, and the ruler burned past its 100 mm tick. The product page gives the print width as 4.01 in. None of `ESC !?`, `~!T`, `~!I`, `~!F`, `~HS`, `ESC !S` or `~!@` got a reply on any channel.
73
+
74
+ ### It never returns credits
75
+
76
+ On subscribing, `ff03` notifies:
77
+
78
+ ```
79
+ 01 0a 10 credits
80
+ 02 b6 00 182: the largest write the module takes
81
+ ```
82
+
83
+ After that it never sent anything else on `ff03`: no credit back after any write, during a job or in the seconds after it. The full raster went out as 683 back-to-back 182-byte writes with no gating at all, in 1.6 s from the host's side, and printed without loss. Metering it by credits would stall on the eleventh write, so `connect` takes `credits: false` for it.
84
+
85
+ ### Linux: BlueZ connects over classic
86
+
87
+ The printer runs classic and LE on the same public address, and its advertising flags (`0x02`) do not mark BR/EDR as unsupported. For a device like that, BlueZ's `Device1.Connect` picks BR/EDR: a classic bond always wins, and otherwise the "last seen" tie between the two bearers breaks toward classic. A `btmon` capture of Chrome's `gatt.connect()` shows HCI `Create Connection` (the BR/EDR page, `0x01|0x0005`), then SDP on PSM 1, then a disconnect about five seconds later, because there is no GATT over classic. Chrome reports `NetworkError: Unknown error when connecting to the device`.
88
+
89
+ BlueZ 5.87 has a per-device `PreferredBearer` property, but only with `Experimental = true` in `/etc/bluetooth/main.conf`. With that on and the property set to `le` (and `Trusted` set to true), Chrome connected over LE and printed. A classic pairing from the desktop's Bluetooth settings also forces BR/EDR, so the printer should not be paired there.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Shaun Lastra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,92 @@
1
- # Temporary Holding Version
1
+ # tsplib
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ TypeScript library for TSPL raster label printing over BLE, for printers that meter writes with a credit channel.
4
+
5
+ [![npm](https://img.shields.io/npm/v/@slastra/tsplib)](https://www.npmjs.com/package/@slastra/tsplib)
6
+ [![license](https://img.shields.io/npm/l/@slastra/tsplib)](./LICENSE)
7
+
8
+ A TSPL job builder, 1-bit raster packing, and a Web Bluetooth transport. No dependencies, and no DOM in the core.
9
+
10
+ TSPL itself is an open, documented language, and plenty of code already speaks it over USB. What this library adds is the part that is not written down anywhere: the cheap 4×6 shipping printers sold as **Labeer** (and Joysea, Forc, and others) carry a BLE module that hides TSPL behind a credit-metered channel, and advertises a different service that silently swallows everything you send it. See [FINDINGS.md](./FINDINGS.md) for how that was worked out.
11
+
12
+ ## Hardware
13
+
14
+ Verified on a **Labeer Y43BT** over BLE, at 203 dpi (8 dots/mm), on 4 × 6 in and 2 × 1 in gap stock: text, rasters, and a 1-dot checkerboard all print clean. A 60 KB full-width raster transfers in under 3 seconds. [printrow](https://printrow.lastra.us) drives it through this library, from Chrome.
15
+
16
+ The module advertises twice, as `Y43BT-XXXX` (classic Bluetooth) and `Y43BT-XXXX_LE`. Only the `_LE` one has the GATT services, so that is the one to pick in Chrome's chooser.
17
+
18
+ The head prints at least the full 4 in (101.6 mm); its true width has not been measured past that. A narrow label needs no `x` offset: the printer places it under the head from `SIZE` alone.
19
+
20
+ Also verified on a **Core Innovations CTP800BD**, the same module family under the name `Shipping Printer-XXXX`, on 4 × 6 in gap stock with a full-width 124 KB raster. Two things differ. It grants ten credits on subscribing and never returns another, so it has to be driven with `credits: false`. And it runs classic and LE on one public address, which on Linux makes BlueZ connect over classic and fail; [FINDINGS.md](./FINDINGS.md#core-innovations-ctp800bd) has the BlueZ setting that fixes it.
21
+
22
+ ```ts
23
+ const link = await connect({ namePrefix: 'Shipping Printer', credits: false });
24
+ ```
25
+
26
+ **How to tell if your printer is this one.** Connect over BLE and look for service `ff00` with characteristics `ff02` (write) and `ff03` (notify). If subscribing to `ff03` immediately notifies a credit grant (`01 nn`) and a write limit (`02 lo hi`), it is this module, whatever the case says. If the credits never come back after writes, use `credits: false`.
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ bun add @slastra/tsplib
32
+ ```
33
+
34
+ ### Requirements
35
+
36
+ Web Bluetooth is Chromium-only. On Linux it also needs `chrome://flags/#enable-web-bluetooth`, and it only works on a secure origin (https, or localhost).
37
+
38
+ ## Usage
39
+
40
+ ```ts
41
+ import { buildLabel, imageDataToRows, printJob } from '@slastra/tsplib';
42
+ import { connect } from '@slastra/tsplib/web-bluetooth';
43
+
44
+ const link = await connect(); // from a click handler: shows Chrome's device chooser
45
+
46
+ const rows = imageDataToRows(ctx.getImageData(0, 0, 812, 1218)); // 4 x 6 in at 8 dots/mm
47
+ const job = buildLabel(rows, { widthMm: 101.6, heightMm: 152.4, density: 8 });
48
+
49
+ await printJob(link, [async () => job], { onProgress: (done, total) => console.log(done, total) });
50
+ ```
51
+
52
+ `buildLabel` options:
53
+
54
+ | Option | Default | |
55
+ | --------------------- | -------- | ------------------------------------------------------------------ |
56
+ | `widthMm`, `heightMm` | required | Label size. TSPL clips anything drawn past the width. |
57
+ | `media` | `'gap'` | `'gap'`, `'mark'` (black mark, sent as `BLINE`) or `'continuous'`. |
58
+ | `gapMm` | `3` | Gap or mark length. |
59
+ | `density` | `8` | Burn darkness, 0 to 15. |
60
+ | `copies` | `1` | Copies of this label, at no extra cost on the wire. |
61
+ | `direction` | `0` | TSPL `DIRECTION`: `1` turns the label half a turn. |
62
+ | `x`, `y` | `0` | Where the raster's top-left lands, in dots. |
63
+
64
+ `connect` options are `namePrefix` (default `'Y43BT'`), `maxChunk` (240), `creditTimeoutMs` (10000), `credits` (`true`; `false` sends without waiting for credits, for printers that never return them) and `onDisconnect`.
65
+
66
+ Any transport with a `send(bytes)` that resolves once the bytes are accepted can drive `printJob`, so a USB `/dev/usb/lpN` writer works as well as Bluetooth.
67
+
68
+ ## How it works
69
+
70
+ - **Only `BITMAP` draws.** The printer's own fonts and barcodes are left unused, so the design is rendered host-side and the preview can be trusted to match the paper.
71
+ - **In TSPL bitmaps, 1 is white.** That is the inverse of most raster formats, and getting it backwards prints a solid black slab with the design cut out of it. Padding bits are white for the same reason.
72
+ - **Credits are the backpressure.** Every GATT write costs one credit and earns one back, so writes go out exactly as fast as the printer takes them, with no pacing timer to tune.
73
+ - **The write size follows the printer.** It announces its limit (244 bytes) on the flow channel. If the browser's link never negotiated an MTU that big, the first large write fails, its credit is refunded, and the link drops to 20-byte writes for good.
74
+ - **Cancelling stops between labels, never inside one.** A half-sent `BITMAP` leaves the printer reading the next job's commands as image data.
75
+ - **There is no status.** Neither printer answered any of TSPL's or ESC/POS's status queries on any channel. A credit that never comes back (10 s by default) is the only fault signal, and it fails the send rather than hanging it. With `credits: false` there is no fault signal at all.
76
+
77
+ ## Development
78
+
79
+ ```bash
80
+ bun install
81
+ bun test test # job bytes, bitmap packing, credit gate, print loop, BLE transport against a fake printer
82
+ bun run check # tsc
83
+ bun run build # emits dist/
84
+ ```
85
+
86
+ ## Credits
87
+
88
+ The Labeer protocol findings are original work from probing the hardware directly; see [FINDINGS.md](./FINDINGS.md). TSPL is TSC's printer language, documented in its public programming manual.
89
+
90
+ ## License
91
+
92
+ MIT © 2026 Shaun Lastra. See [LICENSE](LICENSE).
package/dist/flow.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The credit channel that meters writes into the printer.
3
+ *
4
+ * The Y43BT's BLE module exposes TSPL only on service `ff00`: bytes go in on
5
+ * `ff02`, and `ff03` notifies flow-control messages back. Two kinds have been
6
+ * seen, both on subscribing and throughout a job:
7
+ *
8
+ * 01 nn grant nn more writes (7 on subscribe, then 1 back per write)
9
+ * 02 lo hi the largest write the module takes, little-endian (f4 00 = 244)
10
+ *
11
+ * One credit buys one GATT write of any size up to that limit, NOT one byte:
12
+ * a 20-byte write and a 240-byte write each cost one and each earn one back.
13
+ * Spending credits only as they arrive is the whole of the backpressure, so
14
+ * there is no pacing timer to tune and no buffer depth to guess.
15
+ */
16
+ export type FlowMessage = {
17
+ kind: 'credit';
18
+ count: number;
19
+ } | {
20
+ kind: 'mtu';
21
+ bytes: number;
22
+ } | {
23
+ kind: 'unknown';
24
+ raw: Uint8Array;
25
+ };
26
+ export declare function parseFlow(raw: Uint8Array): FlowMessage;
27
+ /**
28
+ * Counts credits and parks writers until one is free. Writers are served in
29
+ * the order they asked, so a job's chunks cannot overtake each other.
30
+ */
31
+ export declare class CreditGate {
32
+ private credits;
33
+ private waiters;
34
+ get available(): number;
35
+ grant(count: number): void;
36
+ /**
37
+ * Spend one credit, waiting up to `timeoutMs` for the printer to return
38
+ * one. No status query has been found that this printer answers, so a
39
+ * stalled credit is the only sign a job gets that something is wrong.
40
+ *
41
+ * There is deliberately no abort here. Stopping between two chunks of a
42
+ * label leaves the TSPL parser owed BITMAP bytes, and it will then read the
43
+ * next job's commands as image data. Cancel between labels instead.
44
+ */
45
+ take(timeoutMs: number): Promise<void>;
46
+ }
47
+ //# sourceMappingURL=flow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow.d.ts","sourceRoot":"","sources":["../src/flow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,WAAW,GACpB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,GAAG,EAAE,UAAU,CAAA;CAAE,CAAC;AAExC,wBAAgB,SAAS,CAAC,GAAG,EAAE,UAAU,GAAG,WAAW,CAItD;AAED;;;GAGG;AACH,qBAAa,UAAU;IACtB,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,OAAO,CAAsB;IAErC,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAQ1B;;;;;;;;OAQG;IACH,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAiBtC"}
package/dist/flow.js ADDED
@@ -0,0 +1,52 @@
1
+ export function parseFlow(raw) {
2
+ if (raw[0] === 0x01 && raw.length >= 2)
3
+ return { kind: 'credit', count: raw[1] };
4
+ if (raw[0] === 0x02 && raw.length >= 3)
5
+ return { kind: 'mtu', bytes: raw[1] | (raw[2] << 8) };
6
+ return { kind: 'unknown', raw };
7
+ }
8
+ /**
9
+ * Counts credits and parks writers until one is free. Writers are served in
10
+ * the order they asked, so a job's chunks cannot overtake each other.
11
+ */
12
+ export class CreditGate {
13
+ credits = 0;
14
+ waiters = [];
15
+ get available() {
16
+ return this.credits;
17
+ }
18
+ grant(count) {
19
+ this.credits += count;
20
+ while (this.credits > 0 && this.waiters.length) {
21
+ this.credits--;
22
+ this.waiters.shift()();
23
+ }
24
+ }
25
+ /**
26
+ * Spend one credit, waiting up to `timeoutMs` for the printer to return
27
+ * one. No status query has been found that this printer answers, so a
28
+ * stalled credit is the only sign a job gets that something is wrong.
29
+ *
30
+ * There is deliberately no abort here. Stopping between two chunks of a
31
+ * label leaves the TSPL parser owed BITMAP bytes, and it will then read the
32
+ * next job's commands as image data. Cancel between labels instead.
33
+ */
34
+ take(timeoutMs) {
35
+ if (this.credits > 0 && this.waiters.length === 0) {
36
+ this.credits--;
37
+ return Promise.resolve();
38
+ }
39
+ return new Promise((resolve, reject) => {
40
+ const done = () => {
41
+ clearTimeout(timer);
42
+ resolve();
43
+ };
44
+ const timer = setTimeout(() => {
45
+ this.waiters = this.waiters.filter((w) => w !== done);
46
+ reject(new Error('printer stopped taking data (out of paper, lid open, or jammed?)'));
47
+ }, timeoutMs);
48
+ this.waiters.push(done);
49
+ });
50
+ }
51
+ }
52
+ //# sourceMappingURL=flow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow.js","sourceRoot":"","sources":["../src/flow.ts"],"names":[],"mappings":"AAoBA,MAAM,UAAU,SAAS,CAAC,GAAe;IACxC,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;IACjF,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;IAC9F,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;AACjC,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,UAAU;IACd,OAAO,GAAG,CAAC,CAAC;IACZ,OAAO,GAAmB,EAAE,CAAC;IAErC,IAAI,SAAS;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,KAAa;QAClB,IAAI,CAAC,OAAO,IAAI,KAAK,CAAC;QACtB,OAAO,IAAI,CAAC,OAAO,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAChD,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,IAAI,CAAC,OAAO,CAAC,KAAK,EAAG,EAAE,CAAC;QACzB,CAAC;IACF,CAAC;IAED;;;;;;;;OAQG;IACH,IAAI,CAAC,SAAiB;QACrB,IAAI,IAAI,CAAC,OAAO,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnD,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;QAC1B,CAAC;QACD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACtC,MAAM,IAAI,GAAG,GAAG,EAAE;gBACjB,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,OAAO,EAAE,CAAC;YACX,CAAC,CAAC;YACF,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC7B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;gBACtD,MAAM,CAAC,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC,CAAC;YACvF,CAAC,EAAE,SAAS,CAAC,CAAC;YACd,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC,CAAC,CAAC;IACJ,CAAC;CACD"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * tsplib — TSPL raster label printing, for printers that hide TSPL behind a
3
+ * credit-metered BLE channel.
4
+ *
5
+ * This entry point is pure: no DOM, no Node APIs, no dependencies. The Web
6
+ * Bluetooth transport lives behind `tsplib/web-bluetooth` so importing the job
7
+ * builder never drags browser types in.
8
+ */
9
+ export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel, type MediaType, type LabelOptions } from './tspl.js';
10
+ export { THRESHOLD, lumaOverWhite, imageDataToRows, type RasterRow, type PixelSource } from './raster.js';
11
+ export { parseFlow, CreditGate, type FlowMessage } from './flow.js';
12
+ export { printJob, type Link, type PrintJobOptions } from './transport.js';
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EACV,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EACf,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAE,KAAK,IAAI,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * tsplib — TSPL raster label printing, for printers that hide TSPL behind a
3
+ * credit-metered BLE channel.
4
+ *
5
+ * This entry point is pure: no DOM, no Node APIs, no dependencies. The Web
6
+ * Bluetooth transport lives behind `tsplib/web-bluetooth` so importing the job
7
+ * builder never drags browser types in.
8
+ */
9
+ export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel } from './tspl.js';
10
+ export { THRESHOLD, lumaOverWhite, imageDataToRows } from './raster.js';
11
+ export { parseFlow, CreditGate } from './flow.js';
12
+ export { printJob } from './transport.js';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EAGV,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EAGf,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAmC,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * One row of the label, one entry per dot: 1 where the head fires, 0 where the
3
+ * stock stays bare. The same shape yplib and nblib use, so one renderer can
4
+ * feed all three.
5
+ */
6
+ export type RasterRow = Uint8Array;
7
+ /**
8
+ * 1-bit cutoff. A pixel at or above this luma leaves the stock bare, below it
9
+ * the head fires.
10
+ */
11
+ export declare const THRESHOLD = 128;
12
+ /** The subset of ImageData this module needs, so Node callers need no DOM. */
13
+ export interface PixelSource {
14
+ data: Uint8ClampedArray;
15
+ width: number;
16
+ height: number;
17
+ }
18
+ /**
19
+ * BT.601 luma composited over white paper. Alpha is composited against white
20
+ * because unburned stock is what shows through, so a half-transparent grey is
21
+ * lighter on paper than it looks on a dark screen.
22
+ *
23
+ * @param data RGBA bytes
24
+ * @param i byte offset of the pixel (4 bytes per pixel)
25
+ */
26
+ export declare function lumaOverWhite(data: Uint8ClampedArray, i: number): number;
27
+ /**
28
+ * Reduce RGBA pixels to rows of dots. Pure over plain arrays: pass an ImageData
29
+ * in the browser, or any `{data, width, height}` from sharp or node-canvas.
30
+ */
31
+ export declare function imageDataToRows(src: PixelSource, threshold?: number): RasterRow[];
32
+ //# sourceMappingURL=raster.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"raster.d.ts","sourceRoot":"","sources":["../src/raster.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC;AAEnC;;;GAGG;AACH,eAAO,MAAM,SAAS,MAAM,CAAC;AAE7B,8EAA8E;AAC9E,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,iBAAiB,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,iBAAiB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAGxE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,WAAW,EAAE,SAAS,SAAY,GAAG,SAAS,EAAE,CAWpF"}
package/dist/raster.js ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * 1-bit cutoff. A pixel at or above this luma leaves the stock bare, below it
3
+ * the head fires.
4
+ */
5
+ export const THRESHOLD = 128;
6
+ /**
7
+ * BT.601 luma composited over white paper. Alpha is composited against white
8
+ * because unburned stock is what shows through, so a half-transparent grey is
9
+ * lighter on paper than it looks on a dark screen.
10
+ *
11
+ * @param data RGBA bytes
12
+ * @param i byte offset of the pixel (4 bytes per pixel)
13
+ */
14
+ export function lumaOverWhite(data, i) {
15
+ const a = data[i + 3] / 255;
16
+ return (0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]) * a + 255 * (1 - a);
17
+ }
18
+ /**
19
+ * Reduce RGBA pixels to rows of dots. Pure over plain arrays: pass an ImageData
20
+ * in the browser, or any `{data, width, height}` from sharp or node-canvas.
21
+ */
22
+ export function imageDataToRows(src, threshold = THRESHOLD) {
23
+ const { data, width, height } = src;
24
+ const rows = [];
25
+ for (let y = 0; y < height; y++) {
26
+ const row = new Uint8Array(width);
27
+ for (let x = 0; x < width; x++) {
28
+ row[x] = lumaOverWhite(data, (y * width + x) * 4) < threshold ? 1 : 0;
29
+ }
30
+ rows.push(row);
31
+ }
32
+ return rows;
33
+ }
34
+ //# sourceMappingURL=raster.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"raster.js","sourceRoot":"","sources":["../src/raster.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,CAAC;AAS7B;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,IAAuB,EAAE,CAAS;IAC/D,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC;IAC5B,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1F,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,GAAgB,EAAE,SAAS,GAAG,SAAS;IACtE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACpC,MAAM,IAAI,GAAgB,EAAE,CAAC;IAC7B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;QAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAChC,GAAG,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACvE,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAChB,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The minimum a transport must provide: push bytes at the printer, resolving
3
+ * once they have all been accepted. Web Bluetooth is the one shipped here, but
4
+ * TSPL itself is transport-agnostic, so a USB `/dev/usb/lpN` writer or a
5
+ * classic SPP socket drives `printJob` just as well.
6
+ *
7
+ * There is no status read. The Y43BT answered none of TSPL's (`~!T`, `ESC !?`)
8
+ * or ESC/POS's status queries on any of its channels, so the link's own flow
9
+ * control is the only fault signal there is.
10
+ */
11
+ export interface Link {
12
+ send(bytes: Uint8Array): Promise<void>;
13
+ }
14
+ export interface PrintJobOptions {
15
+ /** Called after each label has been handed to the printer. */
16
+ onProgress?: (done: number, total: number) => void;
17
+ /**
18
+ * Abort between labels. A job stops cleanly; it never tears a label in
19
+ * half, because a half-sent BITMAP leaves the printer reading the next
20
+ * job's commands as image data.
21
+ */
22
+ signal?: AbortSignal;
23
+ }
24
+ /**
25
+ * Run a print job. Each entry builds one label's TSPL job lazily, so a
26
+ * thousand-row batch does not rasterize a thousand labels up front, and label
27
+ * i+1 is built while label i is on the wire.
28
+ *
29
+ * Returns the number of labels sent, which is less than `builds.length` if the
30
+ * signal aborted. "Sent" is as far as anyone can know: the printer reports
31
+ * nothing back once a label has gone in.
32
+ */
33
+ export declare function printJob(link: Link, builds: (() => Promise<Uint8Array>)[], { onProgress, signal }?: PrintJobOptions): Promise<number>;
34
+ //# sourceMappingURL=transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,WAAW,IAAI;IACpB,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAED,MAAM,WAAW,eAAe;IAC/B,8DAA8D;IAC9D,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,CAC7B,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,CAAC,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC,EAAE,EACrC,EAAE,UAAU,EAAE,MAAM,EAAE,GAAE,eAAoB,GAC1C,OAAO,CAAC,MAAM,CAAC,CAkBjB"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Run a print job. Each entry builds one label's TSPL job lazily, so a
3
+ * thousand-row batch does not rasterize a thousand labels up front, and label
4
+ * i+1 is built while label i is on the wire.
5
+ *
6
+ * Returns the number of labels sent, which is less than `builds.length` if the
7
+ * signal aborted. "Sent" is as far as anyone can know: the printer reports
8
+ * nothing back once a label has gone in.
9
+ */
10
+ export async function printJob(link, builds, { onProgress, signal } = {}) {
11
+ let done = 0;
12
+ let next = builds[0]?.();
13
+ try {
14
+ for (let i = 0; i < builds.length; i++) {
15
+ if (signal?.aborted)
16
+ break;
17
+ const job = await next;
18
+ next = builds[i + 1]?.();
19
+ await link.send(job);
20
+ done = i + 1;
21
+ onProgress?.(done, builds.length);
22
+ }
23
+ }
24
+ finally {
25
+ // a build already started for a label we are not printing must not
26
+ // surface as an unhandled rejection after the caller has moved on
27
+ next?.catch(() => { });
28
+ }
29
+ return done;
30
+ }
31
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAyBA;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC7B,IAAU,EACV,MAAqC,EACrC,EAAE,UAAU,EAAE,MAAM,KAAsB,EAAE;IAE5C,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,IAAI,GAAoC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IAC1D,IAAI,CAAC;QACJ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACxC,IAAI,MAAM,EAAE,OAAO;gBAAE,MAAM;YAC3B,MAAM,GAAG,GAAG,MAAM,IAAK,CAAC;YACxB,IAAI,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC;YACzB,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACrB,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YACb,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QACnC,CAAC;IACF,CAAC;YAAS,CAAC;QACV,mEAAmE;QACnE,kEAAkE;QAClE,IAAI,EAAE,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACvB,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}
package/dist/tspl.d.ts ADDED
@@ -0,0 +1,62 @@
1
+ import type { RasterRow } from './raster.js';
2
+ /** Dots per millimetre at 203 dpi, the resolution of every printer verified here. */
3
+ export declare const DOTS_PER_MM = 8;
4
+ /**
5
+ * How the printer finds the start of each label.
6
+ *
7
+ * - `gap`: die-cut labels on a liner, found by the see-through gap between them.
8
+ * - `mark`: continuous stock with a black mark printed on the back.
9
+ * - `continuous`: no marks at all; the label is exactly as long as `heightMm`.
10
+ */
11
+ export type MediaType = 'gap' | 'mark' | 'continuous';
12
+ export interface LabelOptions {
13
+ /** Label width across the head. TSPL clips anything drawn past it. */
14
+ widthMm: number;
15
+ /** Label length along the feed. */
16
+ heightMm: number;
17
+ media?: MediaType;
18
+ /** Gap or mark length. Ignored for continuous stock. */
19
+ gapMm?: number;
20
+ /** Burn darkness, 0–15. */
21
+ density?: number;
22
+ /** Copies of this label. They cost nothing extra on the wire. */
23
+ copies?: number;
24
+ /**
25
+ * TSPL `DIRECTION`: 0 prints the raster as drawn with its top leaving the
26
+ * printer first, 1 turns it half a turn. Not a quarter turn: rotating a
27
+ * design to fit the stock is the caller's job.
28
+ */
29
+ direction?: 0 | 1;
30
+ /** Where the raster's top-left corner lands on the label, in dots. */
31
+ x?: number;
32
+ y?: number;
33
+ }
34
+ export declare const DEFAULT_DENSITY = 8;
35
+ export declare const DEFAULT_GAP_MM = 3;
36
+ /**
37
+ * Pack rows of dots into TSPL `BITMAP` data: MSB first, rows padded to whole
38
+ * bytes, and **1 is white**. That last part is the inverse of the row
39
+ * convention above and of most raster formats, so it is the bug to expect:
40
+ * get it backwards and the label comes out a solid black slab with the design
41
+ * cut out of it. Padding is white for the same reason.
42
+ *
43
+ * Every row must be the same width. A ragged row would not hang the printer
44
+ * the way it does under YPL, but it would shear everything below it sideways,
45
+ * so it is refused rather than sent.
46
+ */
47
+ export declare function packBitmap(rows: RasterRow[]): {
48
+ widthBytes: number;
49
+ data: Uint8Array;
50
+ };
51
+ /**
52
+ * One complete TSPL job for a single raster label: page setup, the bitmap,
53
+ * and the print command. The whole thing is self-contained, so a batch is
54
+ * just these laid end to end and a dropped job never leaves state behind for
55
+ * the next one.
56
+ *
57
+ * Only `BITMAP` draws. The printer's own fonts and barcode symbologies are
58
+ * deliberately unused: a host-side raster is the only way the preview can be
59
+ * trusted to match the paper.
60
+ */
61
+ export declare function buildLabel(rows: RasterRow[], options: LabelOptions): Uint8Array;
62
+ //# sourceMappingURL=tspl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tspl.d.ts","sourceRoot":"","sources":["../src/tspl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,qFAAqF;AACrF,eAAO,MAAM,WAAW,IAAI,CAAC;AAE7B;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,MAAM,GAAG,YAAY,CAAC;AAEtD,MAAM,WAAW,YAAY;IAC5B,sEAAsE;IACtE,OAAO,EAAE,MAAM,CAAC;IAChB,mCAAmC;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,2BAA2B;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,SAAS,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IAClB,sEAAsE;IACtE,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;CACX;AAED,eAAO,MAAM,eAAe,IAAI,CAAC;AACjC,eAAO,MAAM,cAAc,IAAI,CAAC;AAgBhC;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CActF;AAYD;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,YAAY,GAAG,UAAU,CAmC/E"}