tsplib

TypeScript library for TSPL raster label printing over BLE, for printers that meter writes with a credit channel

readme · slastra/tsplib

TypeScript library for TSPL raster label printing over BLE, for printers that carry TSPL on an undocumented GATT channel, credit-metered or not.

npm license

A TSPL job builder, 1-bit raster packing, and a Web Bluetooth transport. No dependencies, and no DOM in the core.

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 for how that was worked out.

Hardware

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 drives it through this library, from Chrome.

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.

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.

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 is driven without credits. And it runs classic and LE on one public address, which on Linux makes BlueZ connect over classic and fail; FINDINGS.md has the BlueZ setting that fixes it.

Also verified on a POLONO PM220S, a 2 in battery label printer with a 384-dot (48 mm) head, on POLONO's own 50 × 30 mm roll. It has the same ff00 layout but a different personality: the flow channel says nothing at all, so it is driven without credits at a fixed 180-byte write, and it is the first printer here that answers. link.readStatus() returns the TSPL status byte and link.readConfig() its firmware and hardware versions. It shares the CTP800BD's one-address dual-mode habit, so Linux needs the same BlueZ setting. POLONO's rolls carry an RFID tag, but the printer does not insist on one: a generic roll with no tag prints just as dark, also after a power cycle. FINDINGS.md has the rest.

All three are in the exported PRINTERS table, so a bare connect() offers any of them in Chrome's chooser and picks the credit mode from the name it gets back. link.printer says which it was. To insist on one:

const link = await connect({ namePrefix: 'Shipping Printer', credits: false });

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, or none are granted at all, use credits: false.

Install

bun add @slastra/tsplib

Requirements

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).

Usage

import { buildLabel, imageDataToRows, printJob } from '@slastra/tsplib';
import { connect } from '@slastra/tsplib/web-bluetooth';

const link = await connect(); // from a click handler: shows Chrome's device chooser

const rows = imageDataToRows(ctx.getImageData(0, 0, 812, 1218)); // 4 x 6 in at 8 dots/mm
const job = buildLabel(rows, { widthMm: 101.6, heightMm: 152.4, density: 8 });

await printJob(link, [async () => job], { onProgress: (done, total) => console.log(done, total) });

buildLabel options:

Option Default
widthMm, heightMm required Label size. TSPL clips anything drawn past the width.
media 'gap' 'gap', 'mark' (black mark, sent as BLINE) or 'continuous'.
gapMm 3 Gap or mark length.
density 8 Burn darkness, 0 to 15.
copies 1 Copies of this label, at no extra cost on the wire.
direction 0 TSPL DIRECTION: 1 turns the label half a turn.
x, y 0 Where the raster's top-left lands, in dots.

connect options are namePrefix (default: every printer in PRINTERS), maxChunk (240), creditTimeoutMs (10000), replyTimeoutMs (2000), credits (default: the picked printer's entry in PRINTERS, else true; false sends without waiting for credits, for printers that never return them) and onDisconnect. The link reports printer (the PRINTERS entry, or null) and credits.

On a printer that answers queries, the link also reads its state:

import { describeStatus, isFault } from '@slastra/tsplib';

const status = await link.readStatus(); // the TSPL status byte, or null
if (status !== null && isFault(status)) console.warn(describeStatus(status)); // "out of paper"

const config = await link.readConfig(); // { dpi: 203, hardware: '0.3.0', firmware: '1.29.2' }, or null

Both resolve to null on a printer that answers nothing, and when the answer does not come in time. A query waits behind any job already being sent.

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.

How it works

  • 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.
  • 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.
  • 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.
  • 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. The PM220S announces nothing, so its size comes from the PRINTERS table.
  • Cancelling stops between labels, never inside one. A half-sent BITMAP leaves the printer reading the next job's commands as image data.
  • Status is the exception. The Y43BT and CTP800BD answered none of TSPL's or ESC/POS's status queries on any channel. For them 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 none at all. The PM220S answers ESC !? on ff01, so its state can be read before and after a job.

Development

bun install
bun test test      # job bytes, bitmap packing, credit gate, status parsing, print loop, BLE transport against a fake printer
bun run check      # tsc
bun run build      # emits dist/

Credits

The Labeer, Core Innovations and POLONO findings are original work from probing the hardware directly; see FINDINGS.md. The PM220S's CONFIG? and BATTERY? layouts follow olofj/p31s's notes on its sibling, the P31S. TSPL is TSC's printer language, documented in its public programming manual.

License

MIT © 2026 Shaun Lastra. See LICENSE.