@risleylima/escpos 0.2.0 → 1.0.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.
Files changed (106) hide show
  1. package/README.md +94 -766
  2. package/dist/adapter/index.d.ts +29 -0
  3. package/dist/adapter/index.d.ts.map +1 -0
  4. package/dist/adapter/index.js +25 -0
  5. package/dist/adapter/index.js.map +1 -0
  6. package/dist/index.d.ts +14 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +25 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/network-adapter/index.d.ts +24 -0
  11. package/dist/network-adapter/index.d.ts.map +1 -0
  12. package/dist/network-adapter/index.js +263 -0
  13. package/dist/network-adapter/index.js.map +1 -0
  14. package/dist/printer/commands-types.d.ts +37 -0
  15. package/dist/printer/commands-types.d.ts.map +1 -0
  16. package/dist/printer/commands-types.js +63 -0
  17. package/dist/printer/commands-types.js.map +1 -0
  18. package/dist/printer/commands.d.ts +169 -0
  19. package/dist/printer/commands.d.ts.map +1 -0
  20. package/dist/printer/commands.js +192 -0
  21. package/dist/printer/commands.js.map +1 -0
  22. package/dist/printer/image-loader.d.ts +17 -0
  23. package/dist/printer/image-loader.d.ts.map +1 -0
  24. package/dist/printer/image-loader.js +462 -0
  25. package/dist/printer/image-loader.js.map +1 -0
  26. package/dist/printer/image.d.ts +43 -0
  27. package/dist/printer/image.d.ts.map +1 -0
  28. package/dist/printer/image.js +132 -0
  29. package/dist/printer/image.js.map +1 -0
  30. package/dist/printer/index.d.ts +158 -0
  31. package/dist/printer/index.d.ts.map +1 -0
  32. package/dist/printer/index.js +703 -0
  33. package/dist/printer/index.js.map +1 -0
  34. package/dist/printer/profiles/bematech/mp4200th.d.ts +13 -0
  35. package/dist/printer/profiles/bematech/mp4200th.d.ts.map +1 -0
  36. package/dist/printer/profiles/bematech/mp4200th.js +29 -0
  37. package/dist/printer/profiles/bematech/mp4200th.js.map +1 -0
  38. package/dist/printer/profiles/custom/bematech-mp4200th.d.ts +13 -0
  39. package/dist/printer/profiles/custom/bematech-mp4200th.d.ts.map +1 -0
  40. package/dist/printer/profiles/custom/bematech-mp4200th.js +21 -0
  41. package/dist/printer/profiles/custom/bematech-mp4200th.js.map +1 -0
  42. package/dist/printer/profiles/custom/vkp80iii.d.ts +19 -0
  43. package/dist/printer/profiles/custom/vkp80iii.d.ts.map +1 -0
  44. package/dist/printer/profiles/custom/vkp80iii.js +87 -0
  45. package/dist/printer/profiles/custom/vkp80iii.js.map +1 -0
  46. package/dist/printer/profiles/default.d.ts +7 -0
  47. package/dist/printer/profiles/default.d.ts.map +1 -0
  48. package/dist/printer/profiles/default.js +15 -0
  49. package/dist/printer/profiles/default.js.map +1 -0
  50. package/dist/printer/profiles/index.d.ts +41 -0
  51. package/dist/printer/profiles/index.d.ts.map +1 -0
  52. package/dist/printer/profiles/index.js +98 -0
  53. package/dist/printer/profiles/index.js.map +1 -0
  54. package/dist/printer/profiles/merge.d.ts +7 -0
  55. package/dist/printer/profiles/merge.d.ts.map +1 -0
  56. package/dist/printer/profiles/merge.js +58 -0
  57. package/dist/printer/profiles/merge.js.map +1 -0
  58. package/dist/printer/profiles/types.d.ts +99 -0
  59. package/dist/printer/profiles/types.d.ts.map +1 -0
  60. package/dist/printer/profiles/types.js +8 -0
  61. package/dist/printer/profiles/types.js.map +1 -0
  62. package/dist/printer/utils.d.ts +9 -0
  63. package/dist/printer/utils.d.ts.map +1 -0
  64. package/dist/printer/utils.js +54 -0
  65. package/dist/printer/utils.js.map +1 -0
  66. package/dist/serial-adapter/index.d.ts +17 -0
  67. package/dist/serial-adapter/index.d.ts.map +1 -0
  68. package/dist/serial-adapter/index.js +172 -0
  69. package/dist/serial-adapter/index.js.map +1 -0
  70. package/dist/usb-adapter/index.d.ts +20 -0
  71. package/dist/usb-adapter/index.d.ts.map +1 -0
  72. package/dist/usb-adapter/index.js +264 -0
  73. package/dist/usb-adapter/index.js.map +1 -0
  74. package/package.json +42 -15
  75. package/CHANGELOG.md +0 -68
  76. package/docs/COVERAGE_ANALYSIS.md +0 -98
  77. package/docs/DEPENDENCIES_REVIEW.md +0 -127
  78. package/docs/JSDOC_REVIEW.md +0 -122
  79. package/docs/LIBRARY_OVERVIEW.md +0 -383
  80. package/docs/PRE_PUBLISH_CHECKLIST.md +0 -331
  81. package/docs/PUBLIC_API_ANALYSIS.md +0 -223
  82. package/docs/README.md +0 -34
  83. package/docs/SERIALPORT_V13_MIGRATION_COMPLETE.md +0 -127
  84. package/docs/TESTS_IMPLEMENTED.md +0 -129
  85. package/docs/USB_V2_REVIEW.md +0 -148
  86. package/docs/VERIFICATION_RESULTS.md +0 -172
  87. package/examples/printTest.js +0 -59
  88. package/index.js +0 -7
  89. package/jest.config.js +0 -16
  90. package/src/adapter/index.js +0 -75
  91. package/src/printer/commands.js +0 -199
  92. package/src/printer/image.js +0 -159
  93. package/src/printer/index.js +0 -621
  94. package/src/printer/utils.js +0 -58
  95. package/src/serial-adapter/index.js +0 -198
  96. package/src/usb-adapter/index.js +0 -283
  97. package/tests/README.md +0 -67
  98. package/tests/integration/printer-flow.test.js +0 -128
  99. package/tests/unit/adapters/adapter.test.js +0 -49
  100. package/tests/unit/adapters/serial-adapter.test.js +0 -238
  101. package/tests/unit/adapters/usb-adapter.test.js +0 -319
  102. package/tests/unit/image/image.test.js +0 -157
  103. package/tests/unit/printer/buffer.test.js +0 -60
  104. package/tests/unit/printer/commands.test.js +0 -109
  105. package/tests/unit/printer/printer.test.js +0 -405
  106. package/tests/unit/utils/utils.test.js +0 -96
@@ -0,0 +1,264 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.USB = void 0;
37
+ const os = __importStar(require("os"));
38
+ const adapter_1 = require("../adapter");
39
+ const usb_1 = require("usb");
40
+ const debug = require('debug')('escpos:usb-adapter');
41
+ const IFACE_CLASS = {
42
+ AUDIO: 0x01,
43
+ HID: 0x03,
44
+ PRINTER: 0x07,
45
+ HUB: 0x09,
46
+ };
47
+ const NOT_CONNECTED_MSG = 'Not connected. Call connect([vid], [pid]) first.';
48
+ const CHUNK_SIZE = 4096; // 4KB chunks for USB stability
49
+ class USB extends adapter_1.Adapter {
50
+ constructor() {
51
+ super(...arguments);
52
+ this.device = null;
53
+ this.endpoint = null;
54
+ this.detachHandler = null;
55
+ }
56
+ static async listUSB() {
57
+ const devices = usb_1.usb.getDeviceList().filter((device) => {
58
+ try {
59
+ const configDescriptor = device.configDescriptor;
60
+ if (!configDescriptor || !configDescriptor.interfaces)
61
+ return false;
62
+ return configDescriptor.interfaces.some((ifaceArray) => ifaceArray.some((iface) => iface.bInterfaceClass === IFACE_CLASS.PRINTER));
63
+ }
64
+ catch (e) {
65
+ debug('Error while get device info: ', e);
66
+ return false;
67
+ }
68
+ });
69
+ const getDescriptor = async (device, type) => {
70
+ try {
71
+ device.open();
72
+ const data = await new Promise((resolve, reject) => {
73
+ device.getStringDescriptor(type, (err, res) => {
74
+ if (err)
75
+ reject(err);
76
+ else
77
+ resolve(res);
78
+ });
79
+ });
80
+ device.close();
81
+ return data;
82
+ }
83
+ catch (e) {
84
+ debug('Error while read device description: ', e);
85
+ try {
86
+ device.close();
87
+ }
88
+ catch { /* ignore */ }
89
+ return false;
90
+ }
91
+ };
92
+ const retorno = [];
93
+ for (const device of devices) {
94
+ const manufacturer = await getDescriptor(device, device.deviceDescriptor.iManufacturer);
95
+ const product = await getDescriptor(device, device.deviceDescriptor.iProduct);
96
+ retorno.push({
97
+ manufacturer: manufacturer ? manufacturer : undefined,
98
+ product: product ? product : undefined,
99
+ vendorId: device.deviceDescriptor.idVendor,
100
+ productId: device.deviceDescriptor.idProduct,
101
+ });
102
+ }
103
+ return retorno;
104
+ }
105
+ async connect(vid, pid) {
106
+ return this.synchronized(async () => {
107
+ this.device = null;
108
+ this.endpoint = null;
109
+ if (this.detachHandler) {
110
+ usb_1.usb.removeListener('detach', this.detachHandler);
111
+ this.detachHandler = null;
112
+ }
113
+ if (vid != null && pid != null) {
114
+ this.device = usb_1.usb.findByIds(vid, pid);
115
+ }
116
+ else {
117
+ const fullList = usb_1.usb.getDeviceList().filter((d) => {
118
+ try {
119
+ const cd = d.configDescriptor;
120
+ if (!cd?.interfaces)
121
+ return false;
122
+ return cd.interfaces.some((arr) => arr.some((i) => i.bInterfaceClass === IFACE_CLASS.PRINTER));
123
+ }
124
+ catch {
125
+ return false;
126
+ }
127
+ });
128
+ if (fullList.length)
129
+ this.device = fullList[0];
130
+ }
131
+ if (!this.device)
132
+ throw new Error('Cannot find printer!');
133
+ this.state = 'CONNECTED';
134
+ this.emit('connect', this.device);
135
+ this.detachHandler = (device) => {
136
+ if (device === this.device) {
137
+ debug('Device Unplugged!');
138
+ this.emit('detach');
139
+ this.device = null;
140
+ this.endpoint = null;
141
+ this.state = 'DISCONNECTED';
142
+ }
143
+ };
144
+ usb_1.usb.on('detach', this.detachHandler);
145
+ debug('Device Connected!');
146
+ return true;
147
+ });
148
+ }
149
+ async open() {
150
+ return this.synchronized(async () => {
151
+ if (!this.device)
152
+ throw new Error(NOT_CONNECTED_MSG);
153
+ if (this.endpoint)
154
+ return true;
155
+ this.state = 'OPENING';
156
+ this.device.open();
157
+ const interfaces = this.device.interfaces;
158
+ if (!interfaces?.length)
159
+ throw new Error('Cannot access device interfaces');
160
+ for (const interfaceObj of interfaces) {
161
+ if (this.endpoint)
162
+ break;
163
+ const descriptor = interfaceObj.descriptor;
164
+ if (descriptor?.bInterfaceClass !== IFACE_CLASS.PRINTER)
165
+ continue;
166
+ if (os.platform() !== 'win32' && interfaceObj.isKernelDriverActive()) {
167
+ try {
168
+ interfaceObj.detachKernelDriver();
169
+ }
170
+ catch (e) {
171
+ throw new Error(`[ERROR] Could not detach kernel driver: ${e.message}`);
172
+ }
173
+ }
174
+ interfaceObj.claim();
175
+ for (const endpoint of interfaceObj.endpoints) {
176
+ if (endpoint.direction === 'out') {
177
+ this.endpoint = endpoint;
178
+ this.state = 'READY';
179
+ this.emit('open', this.device);
180
+ debug('Device Opened!');
181
+ break;
182
+ }
183
+ }
184
+ }
185
+ if (!this.endpoint)
186
+ throw new Error('Can not find endpoint from printer');
187
+ return true;
188
+ });
189
+ }
190
+ async write(data) {
191
+ return this.synchronized(async () => {
192
+ if (!this.device || !this.endpoint)
193
+ throw new Error(NOT_CONNECTED_MSG);
194
+ const prevState = this.state;
195
+ this.state = 'BUSY';
196
+ try {
197
+ for (let i = 0; i < data.length; i += CHUNK_SIZE) {
198
+ const chunk = data.subarray(i, i + CHUNK_SIZE);
199
+ await this.writeChunk(chunk);
200
+ }
201
+ return true;
202
+ }
203
+ finally {
204
+ this.state = prevState === 'BUSY' ? 'READY' : prevState;
205
+ }
206
+ });
207
+ }
208
+ writeChunk(chunk) {
209
+ return new Promise((resolve, reject) => {
210
+ this.endpoint.transfer(chunk, (err) => {
211
+ if (err) {
212
+ debug('Transfer Error: ', err);
213
+ reject(err);
214
+ }
215
+ else {
216
+ resolve();
217
+ }
218
+ });
219
+ });
220
+ }
221
+ async read() {
222
+ throw new Error('Read not supported for USB adapter yet.');
223
+ }
224
+ async close() {
225
+ return this.synchronized(async () => {
226
+ if (this.device) {
227
+ this.state = 'CLOSING';
228
+ try {
229
+ const endpoint = this.endpoint;
230
+ if (endpoint?.interface) {
231
+ await new Promise((resolve, reject) => {
232
+ endpoint.interface.release(true, (err) => {
233
+ if (err)
234
+ reject(err);
235
+ else
236
+ resolve();
237
+ });
238
+ });
239
+ }
240
+ this.device.close();
241
+ }
242
+ catch (e) {
243
+ debug('Error closing device: ', e);
244
+ }
245
+ }
246
+ const closedDevice = this.device;
247
+ this.endpoint = null;
248
+ this.device = null;
249
+ this.state = 'DISCONNECTED';
250
+ if (this.detachHandler) {
251
+ usb_1.usb.removeListener('detach', this.detachHandler);
252
+ this.detachHandler = null;
253
+ }
254
+ this.emit('close', closedDevice);
255
+ debug('Device Closed!');
256
+ return true;
257
+ });
258
+ }
259
+ async disconnect() {
260
+ return this.close();
261
+ }
262
+ }
263
+ exports.USB = USB;
264
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/usb-adapter/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAyB;AACzB,wCAAqC;AACrC,6BAA+C;AAE/C,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,oBAAoB,CAA8C,CAAC;AAElG,MAAM,WAAW,GAAG;IAClB,KAAK,EAAE,IAAI;IACX,GAAG,EAAE,IAAI;IACT,OAAO,EAAE,IAAI;IACb,GAAG,EAAE,IAAI;CACD,CA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,GAAQ,EAAE,EAAE;gCAC5C,IAAI,GAAG;oCAAE,MAAM,CAAC,GAAG,CAAC,CAAC;;oCAChB,OAAO,EAAE,CAAC;4BACjB,CAAC,CAAC,CAAC;wBACL,CAAC,CAAC,CAAC;oBACL,CAAC;oBACD,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;gBACtB,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBAAC,KAAK,CAAC,wBAAwB,EAAE,CAAC,CAAC,CAAC;gBAAC,CAAC;YACrD,CAAC;YAED,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC;YACjC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;YACrB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;YACnB,IAAI,CAAC,KAAK,GAAG,cAAc,CAAC;YAE5B,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;gBACvB,SAAG,CAAC,cAAc,CAAC,QAAQ,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;gBACjD,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;YAC5B,CAAC;YAED,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;YACjC,KAAK,CAAC,gBAAgB,CAAC,CAAC;YACxB,OAAO,IAAI,CAAC;QACd,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,UAAU;QACd,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;CACF;AA7MD,kBA6MC"}
package/package.json CHANGED
@@ -1,39 +1,54 @@
1
1
  {
2
2
  "name": "@risleylima/escpos",
3
- "version": "0.2.0",
4
- "description": "Library to deal with ESCPOS using some adapters",
5
- "main": "index.js",
3
+ "version": "1.0.0",
4
+ "description": "Node.js thermal printer library: ESC/POS, receipts, barcodes, images via USB or Serial",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "author": "Rlima Info",
7
8
  "license": "MIT",
8
9
  "private": false,
9
10
  "engines": {
10
11
  "node": ">=18.0.0"
11
12
  },
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "test": "yarn build && jest",
16
+ "test:watch": "jest --watch",
17
+ "test:coverage": "jest --coverage",
18
+ "check:ts": "tsc --noEmit",
19
+ "prepublishOnly": "npm run build"
20
+ },
12
21
  "dependencies": {
22
+ "@resvg/resvg-js": "^2.6.2",
13
23
  "debug": "^4.4.3",
14
- "get-pixels": "https://github.com/risleylima/get-pixels",
15
- "iconv-lite": "^0.7.0",
24
+ "jpeg-js": "^0.4.4",
25
+ "omggif": "^1.0.10",
16
26
  "serialport": "^13.0.0",
17
- "usb": "^2.16.0"
27
+ "usb": "^2.17.0"
18
28
  },
19
29
  "devDependencies": {
20
- "jest": "^30.2.0"
30
+ "@types/node": "^22.0.0",
31
+ "jest": "^30.2.0",
32
+ "typescript": "^5.8.0"
21
33
  },
22
34
  "directories": {
23
35
  "example": "examples"
24
36
  },
25
- "scripts": {
26
- "test": "jest",
27
- "test:watch": "jest --watch",
28
- "test:coverage": "jest --coverage"
29
- },
30
37
  "repository": {
31
38
  "type": "git",
32
39
  "url": "git+https://github.com/risleylima/escpos.git"
33
40
  },
34
41
  "keywords": [
35
- "EscPos",
36
- "USB"
42
+ "escpos",
43
+ "thermal-printer",
44
+ "receipt-printer",
45
+ "pos",
46
+ "usb",
47
+ "serial",
48
+ "nodejs",
49
+ "barcode",
50
+ "receipt",
51
+ "typescript"
37
52
  ],
38
53
  "bugs": {
39
54
  "url": "https://github.com/risleylima/escpos/issues"
@@ -41,5 +56,17 @@
41
56
  "homepage": "https://github.com/risleylima/escpos#readme",
42
57
  "publishConfig": {
43
58
  "access": "public"
44
- }
59
+ },
60
+ "exports": {
61
+ ".": {
62
+ "types": "./dist/index.d.ts",
63
+ "import": "./dist/index.js",
64
+ "require": "./dist/index.js",
65
+ "default": "./dist/index.js"
66
+ }
67
+ },
68
+ "files": [
69
+ "dist",
70
+ "README.md"
71
+ ]
45
72
  }
package/CHANGELOG.md DELETED
@@ -1,68 +0,0 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file.
4
-
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
-
8
- ## [0.2.0] - 2025-11-23
9
-
10
- ### Added
11
- - `Serial.listSerial()` method to list all available serial ports
12
- - Returns array of serial port objects with path, manufacturer, vendorId, productId, etc.
13
- - Similar functionality to `USB.listUSB()` for consistency between adapters
14
-
15
- ## [0.1.0] - 2025-11-23
16
-
17
- ### Added
18
- - Complete JSDoc documentation for all public APIs
19
- - Comprehensive test suite with 145+ tests achieving 100% coverage
20
- - Detailed documentation in `docs/` folder:
21
- - Library Overview
22
- - USB v2 Migration Review
23
- - SerialPort v13 Migration Complete
24
- - Test Coverage Analysis
25
- - JSDoc Review
26
- - Public API Analysis
27
- - Enhanced README with complete installation, usage examples, and API documentation
28
-
29
- ### Changed
30
- - **BREAKING (Internal)**: Updated `usb` from `^1.9.1` to `^2.16.0`
31
- - Migrated from callback-based to Promise-based API
32
- - Improved error handling and interface management
33
- - Note: Public API remains unchanged, no breaking changes for library users
34
- - **BREAKING (Internal)**: Updated `serialport` from `^12.0.0` to `^13.0.0`
35
- - Migrated from callback-based to Promise-based API
36
- - Improved connection handling and error management
37
- - Note: Public API remains unchanged, no breaking changes for library users
38
- - Updated `debug` from `^4.3.1` to `^4.4.3`
39
- - Updated `iconv-lite` from `^0.6.3` to `^0.7.0`
40
- - Updated `jest` from `^29.7.0` to `^30.2.0`
41
-
42
- ### Fixed
43
- - Fixed event emission consistency in USB and Serial adapters
44
- - Fixed adapter instantiation in Printer class to maintain consistency
45
- - Improved error handling in adapter close/disconnect methods
46
- - Fixed test coverage gaps in commands and printer utilities
47
-
48
- ### Improved
49
- - Architecture improvements for better event handling
50
- - Enhanced error messages and debugging information
51
- - Better interface release logic in USB adapter
52
- - Improved test isolation and reliability
53
-
54
- ### Security
55
- - Updated dependencies to latest versions with security patches
56
-
57
- ## [0.0.14] - Previous Release
58
-
59
- Initial stable release with USB and Serial adapter support.
60
-
61
- ---
62
-
63
- ## Version History
64
-
65
- - **0.2.0** - Added `Serial.listSerial()` method for listing available serial ports
66
- - **0.1.0** - Major dependency updates, 100% test coverage, complete documentation
67
- - **0.0.14** - Initial stable release
68
-
@@ -1,98 +0,0 @@
1
- # Test Coverage Analysis
2
-
3
- ## Current Coverage Status
4
-
5
- - **Statements**: 100% ✅
6
- - **Branches**: 100% ✅ (was 88.09%, improved to 100%)
7
- - **Functions**: 100% ✅
8
- - **Lines**: 100% ✅
9
-
10
- **Status**: ✅ **100% Coverage Achieved!**
11
-
12
- ## Uncovered Branches
13
-
14
- ### 1. `commands.js` - `numToHexString` function (Line 12)
15
-
16
- ```javascript
17
- if (!isNaN(num)) {
18
- // This branch is covered
19
- }
20
- // Missing: else branch when isNaN(num) === true
21
- ```
22
-
23
- **Issue**: The function doesn't test the case when `Number(value)` results in `NaN`.
24
-
25
- **Current Test**: None - this function is only tested indirectly through other functions.
26
-
27
- **Fix Needed**: Test with invalid input (e.g., `numToHexString('invalid')` or `numToHexString(NaN)`).
28
-
29
- ### 2. `commands.js` - `TXT_CUSTOM_SIZE` function (Lines 77-80)
30
-
31
- ```javascript
32
- width = width > 8 ? 8 : width; // Branch: width > 8 not tested
33
- width = width < 1 ? 1 : width; // Branch: width < 1 not tested
34
- height = height > 8 ? 8 : height; // Branch: height > 8 not tested
35
- height = height < 1 ? 1 : height; // Branch: height < 1 not tested
36
- ```
37
-
38
- **Issue**: The `size()` method is only tested with normal values (2, 2), not edge cases.
39
-
40
- **Current Test**:
41
- ```javascript
42
- it('should set text size', () => {
43
- printer.size(2, 2);
44
- // Only tests normal case
45
- });
46
- ```
47
-
48
- **Missing Tests**:
49
- - `printer.size(10, 2)` - width > 8 (should clamp to 8)
50
- - `printer.size(0, 2)` - width < 1 (should clamp to 1)
51
- - `printer.size(2, 10)` - height > 8 (should clamp to 8)
52
- - `printer.size(2, 0)` - height < 1 (should clamp to 1)
53
- - `printer.size(10, 10)` - both > 8
54
- - `printer.size(0, 0)` - both < 1
55
-
56
- ## Impact
57
-
58
- The uncovered branches are **edge case validations** that:
59
- - Clamp values to valid ranges (1-8 for width/height)
60
- - Handle invalid input gracefully
61
-
62
- While these branches are defensive code, they should be tested to ensure:
63
- 1. The clamping logic works correctly
64
- 2. Invalid inputs don't cause errors
65
- 3. The behavior is documented through tests
66
-
67
- ## ✅ Resolution
68
-
69
- Tests have been added for all uncovered branches:
70
-
71
- 1. **`numToHexString` tests** (`tests/unit/printer/commands.test.js`):
72
- - ✅ NaN input handling
73
- - ✅ Invalid input handling
74
- - ✅ Edge cases (zero, large numbers, odd-length hex)
75
-
76
- 2. **`TXT_CUSTOM_SIZE` tests** (`tests/unit/printer/commands.test.js`):
77
- - ✅ Width clamping (> 8, < 1)
78
- - ✅ Height clamping (> 8, < 1)
79
- - ✅ Both parameters out of range
80
- - ✅ Boundary values (1 and 8)
81
- - ✅ Valid range values
82
-
83
- 3. **`size()` method tests** (`tests/unit/printer/printer.test.js`):
84
- - ✅ Width > 8 clamping
85
- - ✅ Width < 1 clamping
86
- - ✅ Height > 8 clamping
87
- - ✅ Height < 1 clamping
88
- - ✅ Both out of range
89
-
90
- ## Results
91
-
92
- - **Before**: 88.09% branch coverage (50% in commands.js)
93
- - **After**: 100% branch coverage ✅
94
- - **New Tests Added**: 19 tests
95
- - **Total Tests**: 145 (was 126)
96
-
97
- All edge cases are now properly tested and documented!
98
-
@@ -1,127 +0,0 @@
1
- # Dependencies Review
2
-
3
- ## Current Dependencies Status
4
-
5
- ### Production Dependencies
6
-
7
- | Package | Current | Latest | Status | Notes |
8
- |---------|---------|--------|--------|-------|
9
- | `debug` | `^4.4.3` | `4.4.3` | ✅ Up to date | Updated from 4.3.1 |
10
- | `get-pixels` | `https://github.com/risleylima/get-pixels` | N/A | ✅ Custom | Custom fork, version not tracked |
11
- | `iconv-lite` | `^0.7.0` | `0.7.0` | ✅ Up to date | Updated from 0.6.3 |
12
- | `serialport` | `^13.0.0` | `13.0.0` | ✅ Up to date | Updated from 12.0.0 |
13
- | `usb` | `^2.16.0` | `2.16.0` | ✅ Up to date | Updated from 1.9.1 |
14
-
15
- ### Development Dependencies
16
-
17
- | Package | Current | Latest | Status | Notes |
18
- |---------|---------|--------|--------|-------|
19
- | `jest` | `^30.2.0` | `30.2.0` | ✅ Up to date | Updated from 29.7.0 |
20
-
21
- ## Detailed Analysis
22
-
23
- ### 1. `debug` (4.3.1 → 4.4.3)
24
- - **Type**: Minor update
25
- - **Risk**: Low
26
- - **Breaking Changes**: None expected
27
- - **Recommendation**: ✅ Safe to update
28
- - **Changes**: Bug fixes and minor improvements
29
-
30
- ### 2. `iconv-lite` (0.6.3 → 0.7.0)
31
- - **Type**: Major update
32
- - **Risk**: Medium
33
- - **Breaking Changes**: Possible
34
- - **Recommendation**: ⚠️ Review before updating
35
- - **Notes**:
36
- - Major version jump indicates potential breaking changes
37
- - Need to verify API compatibility
38
- - Check changelog for breaking changes
39
-
40
- ### 3. `serialport` (12.0.0 → 13.0.0)
41
- - **Type**: Major update
42
- - **Risk**: High
43
- - **Breaking Changes**: Likely
44
- - **Recommendation**: ⚠️ Review carefully before updating
45
- - **Notes**:
46
- - Major version jump (12 → 13)
47
- - Serial adapter code may need updates
48
- - Check migration guide
49
- - Test thoroughly with hardware
50
-
51
- ### 4. `jest` (29.7.0 → 30.2.0)
52
- - **Type**: Major update
53
- - **Risk**: Low to Medium
54
- - **Breaking Changes**: Possible
55
- - **Recommendation**: ⚠️ Review before updating
56
- - **Notes**:
57
- - Major version jump (29 → 30)
58
- - Test configuration may need updates
59
- - Check Jest 30 migration guide
60
- - All tests should still work, but config might need tweaks
61
-
62
- ### 5. `usb` (2.16.0)
63
- - **Status**: ✅ Already updated
64
- - **Notes**: Recently migrated from 1.9.1 to 2.16.0
65
-
66
- ## Update Recommendations
67
-
68
- ### Priority 1: Safe Updates (Low Risk)
69
- 1. **`debug`** → `^4.4.3`
70
- - Minor version update
71
- - No breaking changes expected
72
- - Quick win
73
-
74
- ### Priority 2: Review Required (Medium Risk)
75
- 2. **`iconv-lite`** → `^0.7.0`
76
- - Check changelog for breaking changes
77
- - Test encoding functionality
78
- - Verify all text encoding still works
79
-
80
- 3. **`jest`** → `^30.2.0`
81
- - Review Jest 30 release notes
82
- - Check if test configuration needs updates
83
- - Run all tests after update
84
-
85
- ### Priority 3: Careful Review (High Risk)
86
- 4. **`serialport`** → `^13.0.0`
87
- - Major version jump
88
- - Review migration guide
89
- - Update serial adapter code if needed
90
- - Test with real hardware
91
- - May require significant code changes
92
-
93
- ## Update Strategy
94
-
95
- ### Option 1: Conservative (Recommended)
96
- - Update `debug` only (safe)
97
- - Keep others at current versions
98
- - Monitor for issues
99
-
100
- ### Option 2: Moderate
101
- - Update `debug` and `iconv-lite`
102
- - Review and test `iconv-lite` changes
103
- - Keep `serialport` and `jest` for now
104
-
105
- ### Option 3: Aggressive
106
- - Update all dependencies
107
- - Requires thorough testing
108
- - May need code changes for `serialport`
109
- - Higher risk of breaking changes
110
-
111
- ## Testing Checklist (After Updates)
112
-
113
- - [ ] Run all unit tests
114
- - [ ] Run integration tests
115
- - [ ] Test USB adapter with real hardware
116
- - [ ] Test Serial adapter with real hardware
117
- - [ ] Test text encoding with various character sets
118
- - [ ] Verify all examples work
119
- - [ ] Check for deprecation warnings
120
-
121
- ## Notes
122
-
123
- - `get-pixels` is a custom fork, version tracking not applicable
124
- - All major updates should be tested thoroughly
125
- - Consider updating one at a time to isolate issues
126
- - Keep backup of working `package-lock.json` before updates
127
-