@slastra/tsplib 0.2.0 → 0.3.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 CHANGED
@@ -17,7 +17,9 @@ The module advertises twice, as `Y43BT-XXXX` (classic Bluetooth) and `Y43BT-XXXX
17
17
 
18
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
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.
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 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](./FINDINGS.md#core-innovations-ctp800bd) has the BlueZ setting that fixes it.
21
+
22
+ Both are in the exported `PRINTERS` table, so a bare `connect()` offers either one 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:
21
23
 
22
24
  ```ts
23
25
  const link = await connect({ namePrefix: 'Shipping Printer', credits: false });
@@ -61,7 +63,7 @@ await printJob(link, [async () => job], { onProgress: (done, total) => console.l
61
63
  | `direction` | `0` | TSPL `DIRECTION`: `1` turns the label half a turn. |
62
64
  | `x`, `y` | `0` | Where the raster's top-left lands, in dots. |
63
65
 
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`.
66
+ `connect` options are `namePrefix` (default: every printer in `PRINTERS`), `maxChunk` (240), `creditTimeoutMs` (10000), `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`.
65
67
 
66
68
  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
69
 
package/dist/index.d.ts CHANGED
@@ -10,4 +10,5 @@ export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel, t
10
10
  export { THRESHOLD, lumaOverWhite, imageDataToRows, type RasterRow, type PixelSource } from './raster.js';
11
11
  export { parseFlow, CreditGate, type FlowMessage } from './flow.js';
12
12
  export { printJob, type Link, type PrintJobOptions } from './transport.js';
13
+ export { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
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;AAE3E,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js CHANGED
@@ -10,4 +10,5 @@ export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel }
10
10
  export { THRESHOLD, lumaOverWhite, imageDataToRows } from './raster.js';
11
11
  export { parseFlow, CreditGate } from './flow.js';
12
12
  export { printJob } from './transport.js';
13
+ export { PRINTERS, knownPrinter } from './printers.js';
13
14
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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"}
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;AAE3E,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAqB,MAAM,eAAe,CAAC"}
@@ -0,0 +1,21 @@
1
+ /** A printer this library has been verified on, by what its BLE side advertises. */
2
+ export interface KnownPrinter {
3
+ /** What it is sold as. */
4
+ readonly name: string;
5
+ /** Its BLE advertised name, up to the per-unit suffix. */
6
+ readonly namePrefix: string;
7
+ /**
8
+ * Whether it hands a write credit back for every write. A printer that does
9
+ * is metered by them; one that does not has to be sent to back to back.
10
+ */
11
+ readonly credits: boolean;
12
+ }
13
+ /**
14
+ * Every printer verified so far. They share one BLE module family (TSPL on
15
+ * `ff00`, flow on `ff03`) and differ in name and in whether credits come back.
16
+ * See FINDINGS.md for each.
17
+ */
18
+ export declare const PRINTERS: readonly KnownPrinter[];
19
+ /** The known printer a BLE device name belongs to, if any. */
20
+ export declare function knownPrinter(deviceName: string): KnownPrinter | undefined;
21
+ //# sourceMappingURL=printers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"printers.d.ts","sourceRoot":"","sources":["../src/printers.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,MAAM,WAAW,YAAY;IAC5B,0BAA0B;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0DAA0D;IAC1D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC1B;AAED;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,SAAS,YAAY,EAK3C,CAAC;AAEF,8DAA8D;AAC9D,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAEzE"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Every printer verified so far. They share one BLE module family (TSPL on
3
+ * `ff00`, flow on `ff03`) and differ in name and in whether credits come back.
4
+ * See FINDINGS.md for each.
5
+ */
6
+ export const PRINTERS = [
7
+ // advertises `Y43BT-XXXX` (classic) and `Y43BT-XXXX_LE`; only `_LE` has GATT
8
+ { name: 'Labeer Y43BT', namePrefix: 'Y43BT', credits: true },
9
+ // grants ten credits on subscribing and never returns another
10
+ { name: 'Core Innovations CTP800BD', namePrefix: 'Shipping Printer', credits: false }
11
+ ];
12
+ /** The known printer a BLE device name belongs to, if any. */
13
+ export function knownPrinter(deviceName) {
14
+ return PRINTERS.find((p) => deviceName.startsWith(p.namePrefix));
15
+ }
16
+ //# sourceMappingURL=printers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"printers.js","sourceRoot":"","sources":["../src/printers.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAA4B;IAChD,6EAA6E;IAC7E,EAAE,IAAI,EAAE,cAAc,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE;IAC5D,8DAA8D;IAC9D,EAAE,IAAI,EAAE,2BAA2B,EAAE,UAAU,EAAE,kBAAkB,EAAE,OAAO,EAAE,KAAK,EAAE;CACrF,CAAC;AAEF,8DAA8D;AAC9D,MAAM,UAAU,YAAY,CAAC,UAAkB;IAC9C,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;AAClE,CAAC"}
@@ -1,9 +1,13 @@
1
+ import { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
1
2
  import type { Link } from './transport.js';
3
+ export { PRINTERS, knownPrinter, type KnownPrinter };
2
4
  export interface ConnectOptions {
3
5
  /**
4
- * Device name prefix to match. The module advertises twice, `Y43BT-XXXX`
5
- * over classic Bluetooth and `Y43BT-XXXX_LE` over BLE; pick the `_LE` one
6
- * in the chooser, since that is the one with the GATT services.
6
+ * Device name prefix to match. Left out, the chooser offers every printer in
7
+ * {@link PRINTERS}, so one Connect reaches whichever is switched on.
8
+ *
9
+ * The Y43BT advertises twice, `Y43BT-XXXX` over classic Bluetooth and
10
+ * `Y43BT-XXXX_LE` over BLE; the `_LE` one is the one with the GATT services.
7
11
  */
8
12
  namePrefix?: string;
9
13
  /**
@@ -22,6 +26,9 @@ export interface ConnectOptions {
22
26
  * eleventh write. With `false` the writes go out back to back, which that
23
27
  * printer takes without loss, but a jammed or empty printer then no longer
24
28
  * fails the send: there is no fault signal left.
29
+ *
30
+ * Left out, it follows the picked printer's entry in {@link PRINTERS}, and
31
+ * is `true` for a printer not in it.
25
32
  */
26
33
  credits?: boolean;
27
34
  /** Fires if the printer drops the GATT link. */
@@ -30,6 +37,10 @@ export interface ConnectOptions {
30
37
  /** A connected printer. Extends {@link Link}, so it drives `printJob` directly. */
31
38
  export interface BluetoothLink extends Link {
32
39
  readonly deviceName: string;
40
+ /** The known printer the device turned out to be, or null for any other. */
41
+ readonly printer: KnownPrinter | null;
42
+ /** Whether writes are metered by credits on this link. */
43
+ readonly credits: boolean;
33
44
  /** The write size currently in use, in bytes. */
34
45
  readonly chunkSize: number;
35
46
  disconnect(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"web-bluetooth.d.ts","sourceRoot":"","sources":["../src/web-bluetooth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAc3C,MAAM,WAAW,cAAc;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,gDAAgD;IAChD,YAAY,CAAC,E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AAI,CAAC;IACtC,0DAA0D;IAC1D,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,iDAAiD;IACjD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,UAAU,IAAI,IAAI,CAAC;CACnB;AAED,yEAAyE;AACzE,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED,4EAA4E;AAC5E,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAED,6CAA6C;AAC7C,wBAAgB,WAAW,IAAI,OAAO,CAErC;AAED;;;;;;;;;GASG;AACH,wBAAsB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,aAAa,CAAC,CAyElF"}
@@ -1,4 +1,6 @@
1
1
  import { CreditGate, parseFlow } from './flow.js';
2
+ import { PRINTERS, knownPrinter } from './printers.js';
3
+ export { PRINTERS, knownPrinter };
2
4
  /**
3
5
  * The module advertises `0x18f0` (write `2af1`), the service most cheap BLE
4
6
  * printers carry TSPL on. On the Y43BT that one accepts writes and does
@@ -32,8 +34,11 @@ export function isSupported() {
32
34
  * Must be called from a user gesture, per the Web Bluetooth spec.
33
35
  */
34
36
  export async function connect(options = {}) {
35
- const { namePrefix = 'Y43BT', maxChunk = 240, creditTimeoutMs = 10000, credits = true, onDisconnect } = options;
36
- const device = await pickDevice(namePrefix);
37
+ const { namePrefix, maxChunk = 240, creditTimeoutMs = 10000, onDisconnect } = options;
38
+ const prefixes = namePrefix === undefined ? PRINTERS.map((p) => p.namePrefix) : [namePrefix];
39
+ const device = await pickDevice(prefixes);
40
+ const printer = knownPrinter(device.name ?? '') ?? null;
41
+ const credits = options.credits ?? printer?.credits ?? true;
37
42
  if (!device.gatt)
38
43
  throw new Error('device has no GATT server');
39
44
  device.addEventListener('gattserverdisconnected', () => onDisconnect?.());
@@ -84,7 +89,9 @@ export async function connect(options = {}) {
84
89
  }
85
90
  }
86
91
  return {
87
- deviceName: device.name ?? namePrefix,
92
+ deviceName: device.name ?? prefixes[0],
93
+ printer,
94
+ credits,
88
95
  get chunkSize() {
89
96
  return chunk;
90
97
  },
@@ -98,11 +105,15 @@ export async function connect(options = {}) {
98
105
  }
99
106
  };
100
107
  }
101
- async function pickDevice(namePrefix) {
108
+ async function pickDevice(prefixes) {
109
+ const matching = (name = '') => prefixes.some((prefix) => name.startsWith(prefix));
102
110
  if (navigator.bluetooth.getDevices) {
103
111
  try {
112
+ // With more than one printer permitted, this takes the first, whether
113
+ // it is switched on or not. A caller that sees the connect fail can
114
+ // forget() it, and the next connect opens the chooser.
104
115
  const known = await navigator.bluetooth.getDevices();
105
- const matches = known.filter((d) => (d.name ?? '').startsWith(namePrefix));
116
+ const matches = known.filter((d) => matching(d.name));
106
117
  // a dual-mode printer can leave both of its names permitted; the LE one
107
118
  // is the one with the GATT services
108
119
  const hit = matches.find((d) => d.name?.endsWith('_LE')) ?? matches[0];
@@ -114,7 +125,7 @@ async function pickDevice(namePrefix) {
114
125
  }
115
126
  }
116
127
  return navigator.bluetooth.requestDevice({
117
- filters: [{ namePrefix }],
128
+ filters: prefixes.map((namePrefix) => ({ namePrefix })),
118
129
  optionalServices: [SERVICE]
119
130
  });
120
131
  }
@@ -1 +1 @@
1
- {"version":3,"file":"web-bluetooth.js","sourceRoot":"","sources":["../src/web-bluetooth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGlD;;;;GAIG;AACH,MAAM,OAAO,GAAG,MAAM,CAAC;AACvB,MAAM,UAAU,GAAG,MAAM,CAAC;AAC1B,MAAM,SAAS,GAAG,MAAM,CAAC;AAEzB,8EAA8E;AAC9E,MAAM,UAAU,GAAG,EAAE,CAAC;AAuCtB,yEAAyE;AACzE,MAAM,UAAU,YAAY;IAC3B,OAAO,OAAO,SAAS,KAAK,WAAW,IAAI,WAAW,IAAI,SAAS,CAAC;AACrE,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,eAAe;IAC9B,OAAO,OAAO,MAAM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OAAO,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;QACvD,gBAAgB,EAAE,CAAC,OAAO,CAAC;KAC3B,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slastra/tsplib",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "TSPL raster label printing over BLE for printers that hide TSPL behind a credit-metered GATT channel (Labeer Y43BT, Core Innovations CTP800BD, and their white-label siblings). Job builder, 1-bit raster, and a Web Bluetooth transport.",
5
5
  "keywords": [
6
6
  "tspl",
package/src/index.ts CHANGED
@@ -28,3 +28,5 @@ export {
28
28
  export { parseFlow, CreditGate, type FlowMessage } from './flow.js';
29
29
 
30
30
  export { printJob, type Link, type PrintJobOptions } from './transport.js';
31
+
32
+ export { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
@@ -0,0 +1,29 @@
1
+ /** A printer this library has been verified on, by what its BLE side advertises. */
2
+ export interface KnownPrinter {
3
+ /** What it is sold as. */
4
+ readonly name: string;
5
+ /** Its BLE advertised name, up to the per-unit suffix. */
6
+ readonly namePrefix: string;
7
+ /**
8
+ * Whether it hands a write credit back for every write. A printer that does
9
+ * is metered by them; one that does not has to be sent to back to back.
10
+ */
11
+ readonly credits: boolean;
12
+ }
13
+
14
+ /**
15
+ * Every printer verified so far. They share one BLE module family (TSPL on
16
+ * `ff00`, flow on `ff03`) and differ in name and in whether credits come back.
17
+ * See FINDINGS.md for each.
18
+ */
19
+ export const PRINTERS: readonly KnownPrinter[] = [
20
+ // advertises `Y43BT-XXXX` (classic) and `Y43BT-XXXX_LE`; only `_LE` has GATT
21
+ { name: 'Labeer Y43BT', namePrefix: 'Y43BT', credits: true },
22
+ // grants ten credits on subscribing and never returns another
23
+ { name: 'Core Innovations CTP800BD', namePrefix: 'Shipping Printer', credits: false }
24
+ ];
25
+
26
+ /** The known printer a BLE device name belongs to, if any. */
27
+ export function knownPrinter(deviceName: string): KnownPrinter | undefined {
28
+ return PRINTERS.find((p) => deviceName.startsWith(p.namePrefix));
29
+ }
@@ -1,6 +1,9 @@
1
1
  import { CreditGate, parseFlow } from './flow.js';
2
+ import { PRINTERS, knownPrinter, type KnownPrinter } from './printers.js';
2
3
  import type { Link } from './transport.js';
3
4
 
5
+ export { PRINTERS, knownPrinter, type KnownPrinter };
6
+
4
7
  /**
5
8
  * The module advertises `0x18f0` (write `2af1`), the service most cheap BLE
6
9
  * printers carry TSPL on. On the Y43BT that one accepts writes and does
@@ -15,9 +18,11 @@ const SAFE_CHUNK = 20;
15
18
 
16
19
  export interface ConnectOptions {
17
20
  /**
18
- * Device name prefix to match. The module advertises twice, `Y43BT-XXXX`
19
- * over classic Bluetooth and `Y43BT-XXXX_LE` over BLE; pick the `_LE` one
20
- * in the chooser, since that is the one with the GATT services.
21
+ * Device name prefix to match. Left out, the chooser offers every printer in
22
+ * {@link PRINTERS}, so one Connect reaches whichever is switched on.
23
+ *
24
+ * The Y43BT advertises twice, `Y43BT-XXXX` over classic Bluetooth and
25
+ * `Y43BT-XXXX_LE` over BLE; the `_LE` one is the one with the GATT services.
21
26
  */
22
27
  namePrefix?: string;
23
28
  /**
@@ -36,6 +41,9 @@ export interface ConnectOptions {
36
41
  * eleventh write. With `false` the writes go out back to back, which that
37
42
  * printer takes without loss, but a jammed or empty printer then no longer
38
43
  * fails the send: there is no fault signal left.
44
+ *
45
+ * Left out, it follows the picked printer's entry in {@link PRINTERS}, and
46
+ * is `true` for a printer not in it.
39
47
  */
40
48
  credits?: boolean;
41
49
  /** Fires if the printer drops the GATT link. */
@@ -45,6 +53,10 @@ export interface ConnectOptions {
45
53
  /** A connected printer. Extends {@link Link}, so it drives `printJob` directly. */
46
54
  export interface BluetoothLink extends Link {
47
55
  readonly deviceName: string;
56
+ /** The known printer the device turned out to be, or null for any other. */
57
+ readonly printer: KnownPrinter | null;
58
+ /** Whether writes are metered by credits on this link. */
59
+ readonly credits: boolean;
48
60
  /** The write size currently in use, in bytes. */
49
61
  readonly chunkSize: number;
50
62
  disconnect(): void;
@@ -76,15 +88,12 @@ export function isSupported(): boolean {
76
88
  * Must be called from a user gesture, per the Web Bluetooth spec.
77
89
  */
78
90
  export async function connect(options: ConnectOptions = {}): Promise<BluetoothLink> {
79
- const {
80
- namePrefix = 'Y43BT',
81
- maxChunk = 240,
82
- creditTimeoutMs = 10000,
83
- credits = true,
84
- onDisconnect
85
- } = options;
86
-
87
- const device = await pickDevice(namePrefix);
91
+ const { namePrefix, maxChunk = 240, creditTimeoutMs = 10000, onDisconnect } = options;
92
+ const prefixes = namePrefix === undefined ? PRINTERS.map((p) => p.namePrefix) : [namePrefix];
93
+
94
+ const device = await pickDevice(prefixes);
95
+ const printer = knownPrinter(device.name ?? '') ?? null;
96
+ const credits = options.credits ?? printer?.credits ?? true;
88
97
  if (!device.gatt) throw new Error('device has no GATT server');
89
98
  device.addEventListener('gattserverdisconnected', () => onDisconnect?.());
90
99
 
@@ -136,7 +145,9 @@ export async function connect(options: ConnectOptions = {}): Promise<BluetoothLi
136
145
  }
137
146
 
138
147
  return {
139
- deviceName: device.name ?? namePrefix,
148
+ deviceName: device.name ?? prefixes[0],
149
+ printer,
150
+ credits,
140
151
  get chunkSize() {
141
152
  return chunk;
142
153
  },
@@ -151,11 +162,15 @@ export async function connect(options: ConnectOptions = {}): Promise<BluetoothLi
151
162
  };
152
163
  }
153
164
 
154
- async function pickDevice(namePrefix: string): Promise<BluetoothDevice> {
165
+ async function pickDevice(prefixes: string[]): Promise<BluetoothDevice> {
166
+ const matching = (name = '') => prefixes.some((prefix) => name.startsWith(prefix));
155
167
  if (navigator.bluetooth.getDevices) {
156
168
  try {
169
+ // With more than one printer permitted, this takes the first, whether
170
+ // it is switched on or not. A caller that sees the connect fail can
171
+ // forget() it, and the next connect opens the chooser.
157
172
  const known = await navigator.bluetooth.getDevices();
158
- const matches = known.filter((d) => (d.name ?? '').startsWith(namePrefix));
173
+ const matches = known.filter((d) => matching(d.name));
159
174
  // a dual-mode printer can leave both of its names permitted; the LE one
160
175
  // is the one with the GATT services
161
176
  const hit = matches.find((d) => d.name?.endsWith('_LE')) ?? matches[0];
@@ -165,7 +180,7 @@ async function pickDevice(namePrefix: string): Promise<BluetoothDevice> {
165
180
  }
166
181
  }
167
182
  return navigator.bluetooth.requestDevice({
168
- filters: [{ namePrefix }],
183
+ filters: prefixes.map((namePrefix) => ({ namePrefix })),
169
184
  optionalServices: [SERVICE]
170
185
  });
171
186
  }