retail-print-kit 0.1.0 → 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.
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.localSubnets = localSubnets;
7
+ exports.discoverNetworkPrinters = discoverNetworkPrinters;
8
+ const net_1 = __importDefault(require("net"));
9
+ const os_1 = __importDefault(require("os"));
10
+ const net_2 = require("./net");
11
+ const identify_1 = require("../core/identify");
12
+ function isPrivate(ip) {
13
+ return /^10\./.test(ip) || /^192\.168\./.test(ip) || /^172\.(1[6-9]|2\d|3[01])\./.test(ip);
14
+ }
15
+ function localSubnets() {
16
+ const out = new Set();
17
+ for (const list of Object.values(os_1.default.networkInterfaces())) {
18
+ for (const a of list ?? []) {
19
+ if (a.family === 'IPv4' && !a.internal && isPrivate(a.address))
20
+ out.add(a.address.split('.').slice(0, 3).join('.'));
21
+ }
22
+ }
23
+ return [...out];
24
+ }
25
+ function probe(host, port, timeout) {
26
+ return new Promise((resolve) => {
27
+ const s = new net_1.default.Socket();
28
+ const done = (ok) => {
29
+ s.destroy();
30
+ resolve(ok);
31
+ };
32
+ s.setTimeout(timeout, () => done(false));
33
+ s.once('error', () => done(false));
34
+ s.connect(port, host, () => done(true));
35
+ });
36
+ }
37
+ /** Sweeps the local /24 networks for hosts that accept raw printing connections. */
38
+ async function discoverNetworkPrinters(options = {}) {
39
+ const port = options.port ?? 9100;
40
+ const timeout = options.timeout ?? 400;
41
+ const subnets = options.subnets ?? localSubnets();
42
+ const hosts = [];
43
+ for (const s of subnets)
44
+ for (let i = 1; i < 255; i++)
45
+ hosts.push(`${s}.${i}`);
46
+ const found = [];
47
+ let next = 0;
48
+ const worker = async () => {
49
+ while (next < hosts.length) {
50
+ const host = hosts[next++];
51
+ if (await probe(host, port, timeout))
52
+ found.push({ host, port });
53
+ }
54
+ };
55
+ await Promise.all(Array.from({ length: Math.min(options.concurrency ?? 64, hosts.length) }, worker));
56
+ found.sort((a, b) => a.host.localeCompare(b.host, undefined, { numeric: true }));
57
+ if (options.identify) {
58
+ for (const p of found) {
59
+ const t = new net_2.NetTransport(p.host, { port: p.port, timeout: 2000 });
60
+ try {
61
+ await t.open();
62
+ p.identity = await (0, identify_1.identify)(t, 800);
63
+ }
64
+ catch {
65
+ /* leave unidentified */
66
+ }
67
+ finally {
68
+ await t.close().catch(() => undefined);
69
+ }
70
+ }
71
+ }
72
+ return found;
73
+ }
@@ -1 +1,2 @@
1
1
  export declare function toBase64(bytes: Uint8Array): string;
2
+ export declare function fromBase64(text: string): Uint8Array;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toBase64 = toBase64;
4
+ exports.fromBase64 = fromBase64;
4
5
  const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
5
6
  function toBase64(bytes) {
6
7
  if (typeof Buffer !== 'undefined')
@@ -17,3 +18,21 @@ function toBase64(bytes) {
17
18
  }
18
19
  return out;
19
20
  }
21
+ function fromBase64(text) {
22
+ if (typeof Buffer !== 'undefined') {
23
+ const b = Buffer.from(text, 'base64');
24
+ return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
25
+ }
26
+ const clean = text.replace(/[^A-Za-z0-9+/]/g, '');
27
+ const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
28
+ let o = 0;
29
+ for (let i = 0; i < clean.length; i += 4) {
30
+ const n = (ALPHABET.indexOf(clean[i]) << 18) | (ALPHABET.indexOf(clean[i + 1]) << 12) | ((ALPHABET.indexOf(clean[i + 2]) & 63) << 6) | (ALPHABET.indexOf(clean[i + 3]) & 63);
31
+ out[o++] = (n >> 16) & 0xff;
32
+ if (i + 2 < clean.length)
33
+ out[o++] = (n >> 8) & 0xff;
34
+ if (i + 3 < clean.length)
35
+ out[o++] = n & 0xff;
36
+ }
37
+ return out.subarray(0, o);
38
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "retail-print-kit",
3
- "version": "0.1.0",
4
- "description": "Receipt printer and cash drawer driver for Node and Electron: ESC/POS and Star, USB, network and serial, printer profiles, no native dependency in the core.",
3
+ "version": "0.2.0",
4
+ "description": "Receipt printers, cash drawers and label printers for Node.js and Electron: ESC/POS (Epson and compatibles), Star, ZPL, TSPL, EZPL; USB, network and serial; templates, previews, a visual designer and a CLI.",
5
5
  "author": "Arkaprova Majumder <arka162@gmail.com>",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -78,24 +78,58 @@
78
78
  "bin": {
79
79
  "retail-print-kit": "./bin/retail-print-kit.js"
80
80
  },
81
- "homepage": "https://github.com/arka162/thermal-print#readme",
81
+ "homepage": "https://arka162.github.io/thermal-print/",
82
82
  "bugs": {
83
83
  "url": "https://github.com/arka162/thermal-print/issues"
84
84
  },
85
85
  "keywords": [
86
86
  "escpos",
87
+ "esc-pos",
87
88
  "esc/pos",
89
+ "thermal-printer",
88
90
  "thermal",
91
+ "receipt-printer",
89
92
  "receipt",
90
- "printer",
93
+ "pos-printer",
94
+ "pos",
95
+ "point-of-sale",
96
+ "retail",
97
+ "cash-drawer",
91
98
  "cash drawer",
99
+ "printer",
100
+ "print",
101
+ "epson",
102
+ "tm-t88",
103
+ "tm-t20",
104
+ "tm-m30",
105
+ "star-micronics",
92
106
  "star",
93
107
  "tsp100",
108
+ "tsp143",
109
+ "tsp650",
110
+ "mc-print",
111
+ "bixolon",
112
+ "citizen",
113
+ "xprinter",
114
+ "rongta",
115
+ "zebra",
94
116
  "zpl",
95
117
  "tspl",
118
+ "tsc",
96
119
  "ezpl",
120
+ "godex",
121
+ "label-printer",
97
122
  "label",
98
- "pos",
99
- "epson"
123
+ "barcode",
124
+ "qrcode",
125
+ "qr",
126
+ "receipt-template",
127
+ "receipt-designer",
128
+ "usb",
129
+ "serial",
130
+ "network",
131
+ "electron",
132
+ "nodejs",
133
+ "typescript"
100
134
  ]
101
135
  }