libniimai 0.4.0 → 0.6.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 +10 -7
- package/dist/bluetooth.d.ts +2 -43
- package/dist/catalog.d.ts +21 -1
- package/dist/client.d.ts +47 -4
- package/dist/index.d.ts +8 -5
- package/dist/libniimai.js +354 -81
- package/dist/libniimai.js.map +1 -1
- package/dist/routed.d.ts +52 -0
- package/dist/thermal.d.ts +5 -1
- package/dist/usb.d.ts +39 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# libniimai
|
|
2
2
|
|
|
3
|
-
Print from the browser on four families of thermal printers: **NIIMBOT**, **TSPL label printers**, **ESC/POS receipt printers** and **cat printers**. One API connects, detects the printer and prints any canvas, over Web Bluetooth
|
|
3
|
+
Print from the browser on four families of thermal printers: **NIIMBOT**, **TSPL label printers**, **ESC/POS receipt printers** and **cat printers**. One API connects, detects the printer and prints any canvas, over Web Bluetooth, Web Serial or WebUSB. No server, no drivers, no app.
|
|
4
4
|
|
|
5
5
|
libniimai powers [niimai](https://niimai.app), the label designer.
|
|
6
6
|
|
|
@@ -9,8 +9,8 @@ libniimai powers [niimai](https://niimai.app), the label designer.
|
|
|
9
9
|
| Family | Paper | Print width | Connection | Models |
|
|
10
10
|
|---|---|---|---|---|
|
|
11
11
|
| **NIIMBOT** | Labels with gaps, black mark, continuous, transparent | 12 mm and up, by model | Bluetooth, USB | D11, D110, D101, B1, B18, B21, B3S and more |
|
|
12
|
-
| **TSPL label printers** | Labels with gaps, black mark, continuous | Up to 80 mm | Bluetooth | QR380A (sold as CC390), Gprinter GP-M322 |
|
|
13
|
-
| **ESC/POS receipt printers** | 58 or 80 mm, continuous or stickers with gaps | 48 or 72 mm | Bluetooth | MP583, MPT-II, Xprinter XP-58IIL, PT-210, RPP02, MPT-3, RPP300 and more |
|
|
12
|
+
| **TSPL label printers** | Labels with gaps, black mark, continuous | Up to 80 mm | Bluetooth, USB | QR380A (sold as CC390), Gprinter GP-M322 |
|
|
13
|
+
| **ESC/POS receipt printers** | 58 or 80 mm, continuous or stickers with gaps | 48 or 72 mm | Bluetooth, USB | MP583, MPT-II, Xprinter XP-58IIL, PT-210, RPP02, MPT-3, RPP300 and more |
|
|
14
14
|
| **Cat printers** | 58 mm continuous, stickers with gaps | 48 mm | Bluetooth | GB01, GB02, GB03, MX05, MX06, MX10 and more |
|
|
15
15
|
|
|
16
16
|
The family is found from the device the user picks: by its Bluetooth name in `PRINTER_CATALOG`, then checked against the services it offers. A name that matches no family is treated as a NIIMBOT, unless a type from `PRINTER_TYPES` is set for unrecognised printers. Every family gets the same treatment:
|
|
@@ -90,7 +90,7 @@ Returns a `PrinterService`, which owns one printer connection.
|
|
|
90
90
|
|
|
91
91
|
| Member | What it does |
|
|
92
92
|
| --- | --- |
|
|
93
|
-
| `connect(kind)` | Opens the browser's device picker and connects. `kind` is `'bluetooth'` (every family), `'serial'` (NIIMBOT over USB) or `'demo'` (a printer in memory, for trying things out). |
|
|
93
|
+
| `connect(kind)` | Opens the browser's device picker and connects. `kind` is `'bluetooth'` (every family), `'serial'` (NIIMBOT over USB, as a serial port), `'usb'` (label and receipt printers that show up as standard USB printers) or `'demo'` (a printer in memory, for trying things out). |
|
|
94
94
|
| `disconnect()` | Closes the connection. |
|
|
95
95
|
| `print(source, options)` | Prints a canvas, or a `{ count, page(i) }` source for a batch of different labels. |
|
|
96
96
|
| `getStatus()` | The current `PrinterStatus`. |
|
|
@@ -159,12 +159,15 @@ Every piece is exported, for building your own flow:
|
|
|
159
159
|
- `createClient(kind)` returns a raw client for one connection.
|
|
160
160
|
- `TsplPrinterClient`, `EscPosPrinterClient` and `CatPrinterClient` take any `ThermalTransport`, so they also work over other links.
|
|
161
161
|
- `tsplJob`, `escposJob` and `catJob` build the bytes for one page; `escposFeed` and `catFeed` the bytes that move the paper.
|
|
162
|
-
- `identifyPrinter(name)` matches a Bluetooth name to a known model across every family, from `PRINTER_CATALOG`; `tsplModelFor`, `escposModelFor` and `catModelFor` look in one family.
|
|
163
|
-
- `
|
|
162
|
+
- `identifyPrinter(name)` matches a Bluetooth name to a known model across every family, from `PRINTER_CATALOG`; `tsplModelFor`, `escposModelFor` and `catModelFor` look in one family. `identifyUsbPrinter(device)` does the same for a USB device, by vendor and product ID, then product name.
|
|
163
|
+
- `UsbPrinter` connects over WebUSB to a printer-class device (`USB_PRINTER_CLASS`, 7). The family comes from the catalog, then from a type set with `unrecognisedAs` when the printer speaks it, then from the printer's IEEE 1284 device ID (`familiesFromDeviceId`). A printer the site already uses connects without the chooser when it is the only one plugged in; `rememberedUsbPrinters()` lists them. `usbTransport(device)` gives the raw `ThermalTransport`.
|
|
164
|
+
- `supportIssue(kind)` says why a connection type cannot be used in the current browser, as `{ reason, topic }`, or null when it can. `topic` names what fixes it: `linux` (a Chrome flag), `brave` (Brave flags), `ios`, `https`, `usb` (Windows and Linux drivers holding a USB printer) or `browsers`. `unsupportedReason(kind)` returns the reason alone.
|
|
165
|
+
- `detectBrowser()` returns the system, the browser and whether it is built on Chromium.
|
|
166
|
+
- `connectErrorTopic(err)` maps a connect error from a browser that has Bluetooth or USB but cannot use it (adapter off, API turned off, a system driver holding the printer) to the same topics.
|
|
164
167
|
|
|
165
168
|
## Browsers
|
|
166
169
|
|
|
167
|
-
Needs Web Bluetooth
|
|
170
|
+
Needs Web Bluetooth, Web Serial or WebUSB: Chrome or Edge on desktop, or Chrome on Android. On iPhone and iPad, the Bluefy browser adds Web Bluetooth. On Linux, Chrome needs `chrome://flags/#enable-web-bluetooth` turned on; Brave keeps Bluetooth and serial behind `brave://flags`. For USB printers, Windows needs the WinUSB driver (Zadig installs it), and Linux needs the `usblp` module unloaded and a udev rule granting access.
|
|
168
171
|
|
|
169
172
|
## Feedback
|
|
170
173
|
|
package/dist/bluetooth.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { RoutedClient } from './routed.js';
|
|
2
2
|
type Lib = typeof import('@mmote/niimbluelib');
|
|
3
3
|
/**
|
|
4
4
|
* The Bluetooth printer behind the Connect button. One chooser lists NIIMBOT
|
|
@@ -9,55 +9,14 @@ type Lib = typeof import('@mmote/niimbluelib');
|
|
|
9
9
|
* the TSPL client; and everything else the NIIMBOT client exactly as before.
|
|
10
10
|
* Known names never overlap, so each printer matches one client at most.
|
|
11
11
|
*/
|
|
12
|
-
export declare class BluetoothPrinter
|
|
12
|
+
export declare class BluetoothPrinter extends RoutedClient {
|
|
13
13
|
private lib;
|
|
14
|
-
private inner;
|
|
15
14
|
constructor(lib: () => Promise<Lib>);
|
|
16
|
-
private get client();
|
|
17
15
|
/** The kind of printer to treat an unrecognised one as; null leaves it to NIIMBOT. */
|
|
18
16
|
private fallback;
|
|
19
17
|
unrecognisedAs(typeId: string | null): void;
|
|
20
18
|
connect(): Promise<{
|
|
21
19
|
deviceName?: string;
|
|
22
20
|
}>;
|
|
23
|
-
disconnect(): Promise<void>;
|
|
24
|
-
isConnected(): boolean;
|
|
25
|
-
on(event: string, fn: (e: any) => void): unknown;
|
|
26
|
-
off(event: string, fn: (e: any) => void): unknown;
|
|
27
|
-
getPrinterInfo(): {
|
|
28
|
-
batteryPercents?: number;
|
|
29
|
-
modelId?: number;
|
|
30
|
-
printheadWidth?: number;
|
|
31
|
-
protocolVersion?: number;
|
|
32
|
-
};
|
|
33
|
-
getModelMetadata(): import("@mmote/niimbluelib").PrinterModelMeta | undefined;
|
|
34
|
-
getPrintTaskType(): "B1" | "B21_L2B" | "B21_V1" | "D110" | "D110M_V4" | "D11_V1" | "H1S" | undefined;
|
|
35
|
-
getRfidInfo(): {
|
|
36
|
-
labelRfidInfo?: {
|
|
37
|
-
tagPresent?: boolean;
|
|
38
|
-
barCode?: string;
|
|
39
|
-
allPaper?: number;
|
|
40
|
-
usedPaper?: number;
|
|
41
|
-
consumablesType?: number;
|
|
42
|
-
};
|
|
43
|
-
paperInfo?: {
|
|
44
|
-
valid?: boolean;
|
|
45
|
-
paperWidth?: number;
|
|
46
|
-
paperHeight?: number;
|
|
47
|
-
paperType?: number;
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
fetchRfidInfo(): Promise<unknown>;
|
|
51
|
-
startHeartbeat(): void;
|
|
52
|
-
stopHeartbeat(): void;
|
|
53
|
-
isHeartbeatStarted(): boolean;
|
|
54
|
-
get protocol(): {
|
|
55
|
-
newPrintTask(name: import("@mmote/niimbluelib").PrintTaskName, options: Record<string, unknown>): import("./client.js").PrintTaskLike;
|
|
56
|
-
printEnd(): Promise<boolean>;
|
|
57
|
-
};
|
|
58
|
-
get encodePage(): ((canvas: HTMLCanvasElement) => unknown) | undefined;
|
|
59
|
-
get rollKey(): (() => string) | undefined;
|
|
60
|
-
get feedPaper(): ((dots: number) => Promise<void>) | undefined;
|
|
61
|
-
get feedsBack(): boolean | undefined;
|
|
62
21
|
}
|
|
63
22
|
export {};
|
package/dist/catalog.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Every printer outside NIIMBOT that libniimai knows, by Bluetooth name, in
|
|
3
|
+
* one lookup across families. A name matches one family at most, and a name
|
|
4
|
+
* that matches none is known to be unknown before anything is sent to it.
|
|
5
|
+
* The models themselves live with their family: ESCPOS_MODELS, TSPL_MODELS
|
|
6
|
+
* and CAT_MODELS.
|
|
7
|
+
*/
|
|
8
|
+
import type { ClientLike } from './client.js';
|
|
9
|
+
import { type Pace, type ThermalModel, type ThermalTransport } from './thermal.js';
|
|
2
10
|
export type PrinterFamily = 'escpos' | 'tspl' | 'cat';
|
|
3
11
|
export interface CatalogEntry {
|
|
4
12
|
family: PrinterFamily;
|
|
@@ -7,6 +15,18 @@ export interface CatalogEntry {
|
|
|
7
15
|
export declare const PRINTER_CATALOG: CatalogEntry[];
|
|
8
16
|
/** The known printer a Bluetooth name belongs to, longest prefix first across every family. */
|
|
9
17
|
export declare function identifyPrinter(name: string | undefined): CatalogEntry | null;
|
|
18
|
+
/** The known printer behind a USB device: by its vendor and product IDs, then by its product name. */
|
|
19
|
+
export declare function identifyUsbPrinter({ vendorId, productId, productName }: {
|
|
20
|
+
vendorId: number;
|
|
21
|
+
productId: number;
|
|
22
|
+
productName?: string;
|
|
23
|
+
}): CatalogEntry | null;
|
|
24
|
+
/**
|
|
25
|
+
* The client for a thermal printer over a cable link. Cat printers are
|
|
26
|
+
* Bluetooth only, so they get null. `ackTimeoutMs`: how long a label printer
|
|
27
|
+
* waits for its "OK" before asking for its status.
|
|
28
|
+
*/
|
|
29
|
+
export declare function cableClient({ family, model }: CatalogEntry, transport: ThermalTransport, pace: Pace, ackTimeoutMs: number): ClientLike | null;
|
|
10
30
|
/** A kind of printer to pick for one niimai does not recognise. */
|
|
11
31
|
export interface PrinterType extends CatalogEntry {
|
|
12
32
|
id: string;
|
package/dist/client.d.ts
CHANGED
|
@@ -67,14 +67,57 @@ export interface ClientLike {
|
|
|
67
67
|
*/
|
|
68
68
|
unrecognisedAs?(typeId: string | null): void;
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
/** `serial`: a printer on a USB cable that shows up as a serial port, as NIIMBOT printers do. `usb`: one that shows up as a standard USB printer. */
|
|
71
|
+
export type ConnectionKind = 'bluetooth' | 'serial' | 'usb' | 'demo';
|
|
71
72
|
export declare const supportsBluetooth: () => boolean;
|
|
72
73
|
export declare const supportsSerial: () => boolean;
|
|
74
|
+
export declare const supportsUsb: () => boolean;
|
|
73
75
|
export type Platform = 'ios' | 'android' | 'desktop';
|
|
74
76
|
export declare function detectPlatform(ua?: string, touchPoints?: number): Platform;
|
|
77
|
+
export type OperatingSystem = 'ios' | 'android' | 'chromeos' | 'linux' | 'mac' | 'windows' | 'other';
|
|
78
|
+
export type BrowserName = 'chrome' | 'edge' | 'opera' | 'brave' | 'samsung' | 'bluefy' | 'firefox' | 'safari' | 'other';
|
|
79
|
+
export interface BrowserInfo {
|
|
80
|
+
os: OperatingSystem;
|
|
81
|
+
browser: BrowserName;
|
|
82
|
+
/** Built on Chromium, so Web Bluetooth, Web Serial and WebUSB can be there. */
|
|
83
|
+
chromium: boolean;
|
|
84
|
+
/** Served over https or from localhost; browsers hide both APIs elsewhere. */
|
|
85
|
+
secure: boolean;
|
|
86
|
+
}
|
|
87
|
+
/** What the browser tells about itself. Defaults to the running browser. */
|
|
88
|
+
export interface BrowserEnv {
|
|
89
|
+
ua: string;
|
|
90
|
+
touchPoints: number;
|
|
91
|
+
/** Brave hides itself in the user agent; it has `navigator.brave` instead. */
|
|
92
|
+
brave: boolean;
|
|
93
|
+
secure: boolean;
|
|
94
|
+
}
|
|
95
|
+
export declare function detectBrowser(env?: Partial<BrowserEnv>): BrowserInfo;
|
|
75
96
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
97
|
+
* A Help topic that shows how to get a connection type working:
|
|
98
|
+
* - `linux`: Chrome and other Chromium browsers on Linux need a flag for Bluetooth.
|
|
99
|
+
* - `brave`: Brave keeps Bluetooth and USB behind flags.
|
|
100
|
+
* - `ios`: iPhone and iPad browsers, and Bluefy.
|
|
101
|
+
* - `https`: the page has to be served over https.
|
|
102
|
+
* - `browsers`: which browsers work.
|
|
103
|
+
* - `usb`: Windows and Linux hold on to USB printers until told to let the browser have them.
|
|
79
104
|
*/
|
|
105
|
+
export type SetupTopic = 'linux' | 'brave' | 'ios' | 'https' | 'browsers' | 'usb';
|
|
106
|
+
export interface SupportIssue {
|
|
107
|
+
/** In a few words, what is missing or what to do. */
|
|
108
|
+
reason: string;
|
|
109
|
+
topic: SetupTopic;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Why a connection type cannot be used in this browser, and where to read how
|
|
113
|
+
* to fix it, or null when it can be used.
|
|
114
|
+
*/
|
|
115
|
+
export declare function supportIssue(kind: ConnectionKind, env?: Partial<BrowserEnv>): SupportIssue | null;
|
|
116
|
+
/** supportIssue's reason alone. */
|
|
80
117
|
export declare function unsupportedReason(kind: ConnectionKind, platform?: Platform): string | null;
|
|
118
|
+
/**
|
|
119
|
+
* Errors browsers raise when Bluetooth or USB is there but cannot be used:
|
|
120
|
+
* the adapter is off or missing, the browser turned the API off, or the
|
|
121
|
+
* system keeps a USB printer for itself. The Help topic to point to, or null.
|
|
122
|
+
*/
|
|
123
|
+
export declare function connectErrorTopic(err: unknown): SetupTopic | null;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/// <reference types="web-bluetooth" preserve="true" />
|
|
2
2
|
/// <reference types="w3c-web-serial" preserve="true" />
|
|
3
|
+
/// <reference types="w3c-web-usb" preserve="true" />
|
|
3
4
|
import type { ClientLike, ConnectionKind } from './client.js';
|
|
4
5
|
import { PrinterService, type ClientFactory } from './service.js';
|
|
5
6
|
/**
|
|
6
7
|
* A client for one kind of connection. Bluetooth finds the printer family
|
|
7
|
-
* from the device the user picks; serial is NIIMBOT only;
|
|
8
|
-
*
|
|
8
|
+
* from the device the user picks; serial is NIIMBOT only; usb is for label
|
|
9
|
+
* and receipt printers that show up as standard USB printers; demo is a
|
|
10
|
+
* printer in memory.
|
|
9
11
|
*/
|
|
10
12
|
export declare function createClient(kind: ConnectionKind): Promise<ClientLike>;
|
|
11
13
|
export interface CreatePrinterOptions {
|
|
@@ -19,15 +21,16 @@ export interface CreatePrinterOptions {
|
|
|
19
21
|
export declare function createPrinter(options?: CreatePrinterOptions): PrinterService;
|
|
20
22
|
export { PrinterService, DEMO_NAME, friendlyError, fitToPrinthead, rotateCanvas, shiftCanvas } from './service.js';
|
|
21
23
|
export type { ClientFactory, PageSource, PrinterState, PrinterStatus, PrintOffset, PrintOptions, Rotation } from './service.js';
|
|
22
|
-
export { detectPlatform, supportsBluetooth, supportsSerial, unsupportedReason } from './client.js';
|
|
23
|
-
export type { ClientLike, ConnectionKind, Platform, PrintTaskLike } from './client.js';
|
|
24
|
+
export { connectErrorTopic, detectBrowser, detectPlatform, supportIssue, supportsBluetooth, supportsSerial, supportsUsb, unsupportedReason } from './client.js';
|
|
25
|
+
export type { BrowserEnv, BrowserInfo, BrowserName, ClientLike, ConnectionKind, OperatingSystem, Platform, PrintTaskLike, SetupTopic, SupportIssue } from './client.js';
|
|
24
26
|
export { correctTask, findManualProfile, inkOutsideHead, MANUAL_PROFILES, printheadCrop, printheadMm, resolveProfile } from './profiles.js';
|
|
25
27
|
export type { LabelSize, PrintDirection, PrinterProfile } from './profiles.js';
|
|
26
28
|
export { effectivePaper, labelTypeOf, PAPER_TYPES, paperFromLabelType, paperName } from './paper.js';
|
|
27
29
|
export type { PaperType } from './paper.js';
|
|
28
|
-
export { findPrinterType, identifyPrinter, PRINTER_CATALOG, PRINTER_TYPES } from './catalog.js';
|
|
30
|
+
export { findPrinterType, identifyPrinter, identifyUsbPrinter, PRINTER_CATALOG, PRINTER_TYPES } from './catalog.js';
|
|
29
31
|
export type { CatalogEntry, PrinterFamily, PrinterType } from './catalog.js';
|
|
30
32
|
export { BluetoothPrinter } from './bluetooth.js';
|
|
33
|
+
export { familiesFromDeviceId, parseDeviceId, rememberedUsbPrinters, UsbPrinter, usbTransport, USB_PRINTER_CLASS } from './usb.js';
|
|
31
34
|
export { describeBleError } from './ble.js';
|
|
32
35
|
export { DEMO_META, MockClient } from './mock.js';
|
|
33
36
|
export { modelFor, THERMAL_DEFAULT_SIZE, THERMAL_HEAD_DOTS, THERMAL_HEAD_MM, THERMAL_SIZES, ThermalPrinterClient, webTransport } from './thermal.js';
|