label-printer 0.8.0 → 0.9.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/dist/index.mjs CHANGED
@@ -65,7 +65,12 @@ var Command = class {
65
65
  */
66
66
  get commandBytes() {
67
67
  const encoder = new TextEncoder();
68
- return encoder.encode(this.commandString);
68
+ const encoded = encoder.encode(this.commandString);
69
+ const terminator = this.commandTerminatorBytes;
70
+ const result = new Uint8Array(encoded.length + terminator.length);
71
+ result.set(encoded, 0);
72
+ result.set(terminator, encoded.length);
73
+ return result;
69
74
  }
70
75
  print(fn) {
71
76
  fn(this.commandString);
@@ -142,14 +147,7 @@ var CommandGroup = class extends Command {
142
147
  if (this.commands.length === 0) {
143
148
  return new Uint8Array(0);
144
149
  }
145
- const newlineBytes = new Uint8Array([10]);
146
- const byteArrays = [];
147
- for (let i = 0; i < this.commands.length; i++) {
148
- byteArrays.push(this.commands[i].commandBytes);
149
- if (i < this.commands.length - 1) {
150
- byteArrays.push(newlineBytes);
151
- }
152
- }
150
+ const byteArrays = this.commands.map((c) => c.commandBytes);
153
151
  const totalLength = byteArrays.reduce((sum, arr) => sum + arr.length, 0);
154
152
  const result = new Uint8Array(totalLength);
155
153
  let offset = 0;
@@ -1154,9 +1152,11 @@ var TSPLBitmapCommand = class _TSPLBitmapCommand extends TSPLVisualCommand {
1154
1152
  get commandBytes() {
1155
1153
  const encoder = new TextEncoder();
1156
1154
  const commandBytes = encoder.encode(this.commandWithoutBytes);
1157
- const result = new Uint8Array(commandBytes.length + this.bitmap.bytes.length);
1155
+ const terminator = this.commandTerminatorBytes;
1156
+ const result = new Uint8Array(commandBytes.length + this.bitmap.bytes.length + terminator.length);
1158
1157
  result.set(commandBytes, 0);
1159
1158
  result.set(this.bitmap.bytes, commandBytes.length);
1159
+ result.set(terminator, commandBytes.length + this.bitmap.bytes.length);
1160
1160
  return result;
1161
1161
  }
1162
1162
  writeTo(device) {
@@ -1344,9 +1344,11 @@ var TSPLDownload = class extends TSPLCommand {
1344
1344
  const encoder = new TextEncoder();
1345
1345
  const commandBytes = encoder.encode(this.commandString);
1346
1346
  const dataBytes = this.data instanceof Uint8Array ? this.data : new Uint8Array(this.data);
1347
- const result = new Uint8Array(commandBytes.length + dataBytes.length);
1347
+ const terminator = this.commandTerminatorBytes;
1348
+ const result = new Uint8Array(commandBytes.length + dataBytes.length + terminator.length);
1348
1349
  result.set(commandBytes, 0);
1349
1350
  result.set(dataBytes, commandBytes.length);
1351
+ result.set(terminator, commandBytes.length + dataBytes.length);
1350
1352
  return result;
1351
1353
  }
1352
1354
  writeTo(device) {
@@ -1583,6 +1585,18 @@ var Printer = class {
1583
1585
  yield this.device.writeString(text);
1584
1586
  });
1585
1587
  }
1588
+ /**
1589
+ * Writes raw bytes to the printer. Useful when bytes were built elsewhere
1590
+ * (e.g. via Command.commandBytes) and transferred over the network to a service
1591
+ * that is responsible for the physical printing.
1592
+ * @param data Bytes to send to the printer
1593
+ */
1594
+ writeRawBytes(data) {
1595
+ return __async(this, null, function* () {
1596
+ if (!this.device.opened) yield this.device.openAndConfigure();
1597
+ yield this.device.writeData(data);
1598
+ });
1599
+ }
1586
1600
  /**
1587
1601
  * Check if the device is indeed a printer
1588
1602
  * @param device
@@ -2029,6 +2043,7 @@ var TSPLPrinter = class _TSPLPrinter extends Printer {
2029
2043
  static try(device) {
2030
2044
  return __async(this, null, function* () {
2031
2045
  if (!device.opened) yield device.openAndConfigure();
2046
+ console.log("Response: ", device.opened);
2032
2047
  const testCommand = new TSPLRawCommand("~!I");
2033
2048
  yield testCommand.writeTo(device);
2034
2049
  const response = yield device.readString(64);