webserial-core 1.0.5 → 1.0.7

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.
@@ -1,60 +1,33 @@
1
- var O = Object.defineProperty;
2
- var v = (s) => {
3
- throw TypeError(s);
1
+ var I = Object.defineProperty;
2
+ var b = (o) => {
3
+ throw TypeError(o);
4
4
  };
5
- var H = (s, i, e) => i in s ? O(s, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[i] = e;
6
- var p = (s, i, e) => H(s, typeof i != "symbol" ? i + "" : i, e), j = (s, i, e) => i.has(s) || v("Cannot " + e);
7
- var C = (s, i, e) => i.has(s) ? v("Cannot add the same private member more than once") : i instanceof WeakSet ? i.add(s) : i.set(s, e);
8
- var l = (s, i, e) => (j(s, i, "access private method"), e);
9
- const _ = [];
10
- for (let s = 0; s < 256; ++s)
11
- _.push((s + 256).toString(16).slice(1));
12
- function B(s, i = 0) {
13
- return (_[s[i + 0]] + _[s[i + 1]] + _[s[i + 2]] + _[s[i + 3]] + "-" + _[s[i + 4]] + _[s[i + 5]] + "-" + _[s[i + 6]] + _[s[i + 7]] + "-" + _[s[i + 8]] + _[s[i + 9]] + "-" + _[s[i + 10]] + _[s[i + 11]] + _[s[i + 12]] + _[s[i + 13]] + _[s[i + 14]] + _[s[i + 15]]).toLowerCase();
14
- }
15
- let b;
16
- const F = new Uint8Array(16);
17
- function V() {
18
- if (!b) {
19
- if (typeof crypto > "u" || !crypto.getRandomValues)
20
- throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
21
- b = crypto.getRandomValues.bind(crypto);
22
- }
23
- return b(F);
24
- }
25
- const W = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), x = { randomUUID: W };
26
- function Q(s, i, e) {
27
- var n;
28
- if (x.randomUUID && !s)
29
- return x.randomUUID();
30
- s = s || {};
31
- const t = s.random ?? ((n = s.rng) == null ? void 0 : n.call(s)) ?? V();
32
- if (t.length < 16)
33
- throw new Error("Random bytes length must be >= 16");
34
- return t[6] = t[6] & 15 | 64, t[8] = t[8] & 63 | 128, B(t);
35
- }
36
- class S extends CustomEvent {
37
- constructor(i, e) {
38
- super(i, e);
5
+ var M = (o, n, e) => n in o ? I(o, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[n] = e;
6
+ var u = (o, n, e) => M(o, typeof n != "symbol" ? n + "" : n, e), N = (o, n, e) => n.has(o) || b("Cannot " + e);
7
+ var w = (o, n, e) => n.has(o) ? b("Cannot add the same private member more than once") : n instanceof WeakSet ? n.add(o) : n.set(o, e);
8
+ var l = (o, n, e) => (N(o, n, "access private method"), e);
9
+ class m extends CustomEvent {
10
+ constructor(n, e) {
11
+ super(n, e);
39
12
  }
40
13
  }
41
- class E extends EventTarget {
14
+ class C extends EventTarget {
42
15
  constructor() {
43
16
  super(...arguments);
44
- p(this, "__listeners__", {
17
+ u(this, "__listeners__", {
45
18
  debug: !1
46
19
  });
47
- p(this, "__debug__", !1);
20
+ u(this, "__debug__", !1);
48
21
  }
49
22
  dispatch(e, t = null) {
50
- const n = new S(e, { detail: t });
51
- this.dispatchEvent(n), this.__debug__ && this.dispatchEvent(new S("debug", { detail: { type: e, data: t } }));
23
+ const i = new m(e, { detail: t });
24
+ this.dispatchEvent(i), this.__debug__ && this.dispatchEvent(new m("debug", { detail: { type: e, data: t } }));
52
25
  }
53
- dispatchAsync(e, t = null, n = 100) {
54
- const a = this;
26
+ dispatchAsync(e, t = null, i = 100) {
27
+ const s = this;
55
28
  setTimeout(() => {
56
- a.dispatch(e, t);
57
- }, n);
29
+ s.dispatch(e, t);
30
+ }, i);
58
31
  }
59
32
  on(e, t) {
60
33
  typeof this.__listeners__[e] < "u" && !this.__listeners__[e] && (this.__listeners__[e] = !0), this.addEventListener(e, t);
@@ -72,58 +45,58 @@ class E extends EventTarget {
72
45
  }));
73
46
  }
74
47
  }
75
- const o = class o extends E {
48
+ const a = class a extends C {
76
49
  constructor() {
77
50
  super(), ["change"].forEach((e) => {
78
51
  this.serialRegisterAvailableListener(e);
79
52
  });
80
53
  }
81
- static $dispatchChange(i = null) {
82
- i && i.$checkAndDispatchConnection(), o.instance.dispatch("change", { devices: o.devices, dispatcher: i });
54
+ static $dispatchChange(n = null) {
55
+ n && n.$checkAndDispatchConnection(), a.instance.dispatch("change", { devices: a.devices, dispatcher: n });
83
56
  }
84
- static typeError(i) {
57
+ static typeError(n) {
85
58
  const e = new Error();
86
- throw e.message = `Type ${i} is not supported`, e.name = "DeviceTypeError", e;
59
+ throw e.message = `Type ${n} is not supported`, e.name = "DeviceTypeError", e;
87
60
  }
88
- static registerType(i) {
89
- typeof o.devices[i] > "u" && (o.devices[i] = {});
61
+ static registerType(n) {
62
+ typeof a.devices[n] > "u" && (a.devices[n] = {});
90
63
  }
91
- static add(i) {
92
- const e = i.typeDevice;
93
- typeof o.devices[e] > "u" && (o.devices[e] = {});
94
- const t = i.uuid;
95
- if (typeof o.devices[e] > "u" && o.typeError(e), o.devices[e][t])
64
+ static add(n) {
65
+ const e = n.typeDevice;
66
+ typeof a.devices[e] > "u" && (a.devices[e] = {});
67
+ const t = n.uuid;
68
+ if (typeof a.devices[e] > "u" && a.typeError(e), a.devices[e][t])
96
69
  throw new Error(`Device with id ${t} already exists`);
97
- return o.devices[e][t] = i, o.$dispatchChange(i), Object.keys(o.devices[e]).indexOf(t);
70
+ return a.devices[e][t] = n, a.$dispatchChange(n), Object.keys(a.devices[e]).indexOf(t);
98
71
  }
99
- static get(i, e) {
100
- return typeof o.devices[i] > "u" && (o.devices[i] = {}), typeof o.devices[i] > "u" && o.typeError(i), o.devices[i][e];
72
+ static get(n, e) {
73
+ return typeof a.devices[n] > "u" && (a.devices[n] = {}), typeof a.devices[n] > "u" && a.typeError(n), a.devices[n][e];
101
74
  }
102
- static getAll(i = null) {
103
- return i === null ? o.devices : (typeof o.devices[i] > "u" && o.typeError(i), o.devices[i]);
75
+ static getAll(n = null) {
76
+ return n === null ? a.devices : (typeof a.devices[n] > "u" && a.typeError(n), a.devices[n]);
104
77
  }
105
78
  static getList() {
106
- return Object.values(o.devices).map((e) => Object.values(e)).flat();
79
+ return Object.values(a.devices).map((e) => Object.values(e)).flat();
107
80
  }
108
- static getByNumber(i, e) {
109
- return typeof o.devices[i] > "u" && o.typeError(i), Object.values(o.devices[i]).find((n) => n.deviceNumber === e) ?? null;
81
+ static getByNumber(n, e) {
82
+ return typeof a.devices[n] > "u" && a.typeError(n), Object.values(a.devices[n]).find((i) => i.deviceNumber === e) ?? null;
110
83
  }
111
- static getCustom(i, e = 1) {
112
- return typeof o.devices[i] > "u" && o.typeError(i), Object.values(o.devices[i]).find((n) => n.deviceNumber === e) ?? null;
84
+ static getCustom(n, e = 1) {
85
+ return typeof a.devices[n] > "u" && a.typeError(n), Object.values(a.devices[n]).find((i) => i.deviceNumber === e) ?? null;
113
86
  }
114
87
  };
115
- p(o, "instance"), p(o, "devices", {});
116
- let c = o;
117
- c.instance || (c.instance = new c());
118
- function A(s = 100) {
88
+ u(a, "instance"), u(a, "devices", {});
89
+ let _ = a;
90
+ _.instance || (_.instance = new _());
91
+ function v(o = 100) {
119
92
  return new Promise(
120
- (i) => setTimeout(() => i(), s)
93
+ (n) => setTimeout(() => n(), o)
121
94
  );
122
95
  }
123
- function z() {
96
+ function B() {
124
97
  return "serial" in navigator;
125
98
  }
126
- const y = {
99
+ const g = {
127
100
  baudRate: 9600,
128
101
  dataBits: 8,
129
102
  stopBits: 1,
@@ -131,22 +104,22 @@ const y = {
131
104
  bufferSize: 32768,
132
105
  flowControl: "none"
133
106
  };
134
- var r, f, g, w, u, T, U, k, D, L, P, I, $, q, R, M, N;
135
- class J extends E {
107
+ var r, d, f, y, h, A, E, x, T, S, U, k, P, L, D, $, q;
108
+ class R extends C {
136
109
  constructor({
137
110
  filters: e = null,
138
- config_port: t = y,
139
- no_device: n = 1,
140
- device_listen_on_channel: a = 1
111
+ config_port: t = g,
112
+ no_device: i = 1,
113
+ device_listen_on_channel: s = 1
141
114
  } = {
142
115
  filters: null,
143
- config_port: y,
116
+ config_port: g,
144
117
  no_device: 1,
145
118
  device_listen_on_channel: 1
146
119
  }) {
147
120
  super();
148
- C(this, r);
149
- p(this, "__internal__", {
121
+ w(this, r);
122
+ u(this, "__internal__", {
150
123
  auto_response: !1,
151
124
  device_number: 1,
152
125
  aux_port_connector: 0,
@@ -163,7 +136,7 @@ class J extends E {
163
136
  response: {
164
137
  length: null,
165
138
  buffer: new Uint8Array([]),
166
- as: "hex",
139
+ as: "uint8",
167
140
  replacer: /[\n\r]+/g,
168
141
  limiter: null
169
142
  },
@@ -177,13 +150,13 @@ class J extends E {
177
150
  delay_first_connection: 200,
178
151
  bytes_connection: null,
179
152
  filters: [],
180
- config_port: y,
153
+ config_port: g,
181
154
  queue: [],
182
155
  auto_response: ["DD", "DD"]
183
156
  },
184
157
  device: {
185
158
  type: "unknown",
186
- id: Q(),
159
+ id: window.crypto.randomUUID(),
187
160
  listen_on_port: null
188
161
  },
189
162
  time: {
@@ -199,7 +172,7 @@ class J extends E {
199
172
  });
200
173
  if (!("serial" in navigator))
201
174
  throw new Error("Web Serial not supported");
202
- e && (this.serialFilters = e), t && (this.serialConfigPort = t), n && l(this, r, M).call(this, n), a && ["number", "string"].includes(typeof a) && (this.listenOnChannel = a), l(this, r, I).call(this), l(this, r, $).call(this);
175
+ e && (this.serialFilters = e), t && (this.serialConfigPort = t), i && l(this, r, $).call(this, i), s && ["number", "string"].includes(typeof s) && (this.listenOnChannel = s), l(this, r, k).call(this), l(this, r, P).call(this);
203
176
  }
204
177
  set listenOnChannel(e) {
205
178
  if (typeof e == "string" && (e = parseInt(e)), isNaN(e) || e < 1 || e > 255)
@@ -225,12 +198,12 @@ class J extends E {
225
198
  return this.__internal__.serial.config_port;
226
199
  }
227
200
  get isConnected() {
228
- const e = this.__internal__.serial.connected, t = l(this, r, f).call(this, this.__internal__.serial.port);
229
- return e && !t && l(this, r, g).call(this, { error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
201
+ const e = this.__internal__.serial.connected, t = l(this, r, d).call(this, this.__internal__.serial.port);
202
+ return e && !t && l(this, r, f).call(this, { error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
230
203
  }
231
204
  get isDisconnected() {
232
- const e = this.__internal__.serial.connected, t = l(this, r, f).call(this, this.__internal__.serial.port);
233
- return !e && t && (this.dispatch("serial:connected"), c.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
205
+ const e = this.__internal__.serial.connected, t = l(this, r, d).call(this, this.__internal__.serial.port);
206
+ return !e && t && (this.dispatch("serial:connected"), _.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
234
207
  }
235
208
  get deviceNumber() {
236
209
  return this.__internal__.device_number;
@@ -245,30 +218,30 @@ class J extends E {
245
218
  return this.__internal__.serial.queue;
246
219
  }
247
220
  async timeout(e, t) {
248
- this.__internal__.last_error.message = "Operation response timed out.", this.__internal__.last_error.action = t, this.__internal__.last_error.code = e, this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), t === "connect" ? (this.__internal__.serial.connected = !1, this.dispatch("serial:reconnect", {}), c.$dispatchChange(this)) : t === "connection:start" && (await this.serialDisconnect(), this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector += 1, c.$dispatchChange(this), await this.serialConnect()), this.dispatch("serial:timeout", {
221
+ this.__internal__.last_error.message = "Operation response timed out.", this.__internal__.last_error.action = t, this.__internal__.last_error.code = e, this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), t === "connect" ? (this.__internal__.serial.connected = !1, this.dispatch("serial:reconnect", {}), _.$dispatchChange(this)) : t === "connection:start" && (await this.serialDisconnect(), this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector += 1, _.$dispatchChange(this), await this.serialConnect()), this.dispatch("serial:timeout", {
249
222
  ...this.__internal__.last_error,
250
223
  bytes: e,
251
224
  action: t
252
225
  });
253
226
  }
254
227
  async disconnect(e = null) {
255
- await this.serialDisconnect(), l(this, r, g).call(this, e);
228
+ await this.serialDisconnect(), l(this, r, f).call(this, e);
256
229
  }
257
230
  async connect() {
258
231
  return new Promise((e, t) => {
259
- z() || t("Web Serial not supported"), setTimeout(async () => {
260
- await A(499), await this.serialConnect(), this.isConnected ? e(`${this.typeDevice} device ${this.deviceNumber} connected`) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`);
232
+ B() || t("Web Serial not supported"), setTimeout(async () => {
233
+ await v(499), await this.serialConnect(), this.isConnected ? e(`${this.typeDevice} device ${this.deviceNumber} connected`) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`);
261
234
  }, 1);
262
235
  });
263
236
  }
264
237
  async serialDisconnect() {
265
238
  try {
266
239
  const e = this.__internal__.serial.reader, t = this.__internal__.serial.output_stream;
267
- e && (await e.cancel().catch((a) => this.serialErrors(a)), await this.__internal__.serial.input_done), t && (await t.getWriter().close(), await this.__internal__.serial.output_done), this.__internal__.serial.connected && this.__internal__.serial && this.__internal__.serial.port && await this.__internal__.serial.port.close();
240
+ e && (await e.cancel().catch((s) => this.serialErrors(s)), await this.__internal__.serial.input_done), t && (await t.getWriter().close(), await this.__internal__.serial.output_done), this.__internal__.serial.connected && this.__internal__.serial && this.__internal__.serial.port && await this.__internal__.serial.port.close();
268
241
  } catch (e) {
269
242
  this.serialErrors(e);
270
243
  } finally {
271
- this.__internal__.serial.reader = null, this.__internal__.serial.input_done = null, this.__internal__.serial.output_stream = null, this.__internal__.serial.output_done = null, this.__internal__.serial.connected = !1, this.__internal__.serial.port = null, c.$dispatchChange(this);
244
+ this.__internal__.serial.reader = null, this.__internal__.serial.input_done = null, this.__internal__.serial.output_stream = null, this.__internal__.serial.output_done = null, this.__internal__.serial.connected = !1, this.__internal__.serial.port = null, _.$dispatchChange(this);
272
245
  }
273
246
  }
274
247
  getResponseAsArrayBuffer() {
@@ -286,8 +259,8 @@ class J extends E {
286
259
  async serialPortsSaved(e) {
287
260
  const t = this.serialFilters;
288
261
  if (this.__internal__.aux_port_connector < e.length) {
289
- const n = this.__internal__.aux_port_connector;
290
- this.__internal__.serial.port = e[n];
262
+ const i = this.__internal__.aux_port_connector;
263
+ this.__internal__.serial.port = e[i];
291
264
  } else
292
265
  this.__internal__.aux_port_connector = 0, this.__internal__.serial.port = await navigator.serial.requestPort({
293
266
  filters: t
@@ -308,7 +281,7 @@ class J extends E {
308
281
  case t.includes(
309
282
  "this readable stream reader has been released and cannot be used to cancel its previous owner stream"
310
283
  ):
311
- this.dispatch("serial:need-permission", {}), c.$dispatchChange(this);
284
+ this.dispatch("serial:need-permission", {}), _.$dispatchChange(this);
312
285
  break;
313
286
  case t.includes("the port is already open."):
314
287
  case t.includes("failed to open serial port"):
@@ -331,7 +304,7 @@ class J extends E {
331
304
  case t.includes("the port is already closed."):
332
305
  break;
333
306
  case t.includes("the device has been lost"):
334
- this.dispatch("serial:lost", {}), c.$dispatchChange(this);
307
+ this.dispatch("serial:lost", {}), _.$dispatchChange(this);
335
308
  break;
336
309
  case t.includes("navigator.serial is undefined"):
337
310
  this.dispatch("serial:unsupported", {});
@@ -345,36 +318,36 @@ class J extends E {
345
318
  async serialConnect() {
346
319
  try {
347
320
  this.dispatch("serial:connecting", {});
348
- const e = await l(this, r, T).call(this);
321
+ const e = await l(this, r, A).call(this);
349
322
  if (e.length > 0)
350
323
  await this.serialPortsSaved(e);
351
324
  else {
352
- const a = this.serialFilters;
325
+ const s = this.serialFilters;
353
326
  this.__internal__.serial.port = await navigator.serial.requestPort({
354
- filters: a
327
+ filters: s
355
328
  });
356
329
  }
357
330
  const t = this.__internal__.serial.port;
358
331
  if (!t)
359
332
  throw new Error("No port selected by the user");
360
333
  await t.open(this.serialConfigPort);
361
- const n = this;
362
- t.onconnect = (a) => {
363
- console.log(a), n.dispatch("serial:connected", a), c.$dispatchChange(this), n.__internal__.serial.queue.length > 0 && n.dispatch("internal:queue", {});
334
+ const i = this;
335
+ t.onconnect = (s) => {
336
+ console.log(s), i.dispatch("serial:connected", s), _.$dispatchChange(this), i.__internal__.serial.queue.length > 0 && i.dispatch("internal:queue", {});
364
337
  }, t.ondisconnect = async () => {
365
- await n.disconnect();
366
- }, await A(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
367
- await n.timeout(n.__internal__.serial.bytes_connection ?? [], "connection:start");
368
- }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await l(this, r, w).call(this, this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
338
+ await i.disconnect();
339
+ }, await v(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
340
+ await i.timeout(i.__internal__.serial.bytes_connection ?? [], "connection:start");
341
+ }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await l(this, r, y).call(this, this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
369
342
  action: "connect",
370
343
  bytes: this.__internal__.serial.bytes_connection
371
- }), this.__internal__.auto_response && l(this, r, u).call(this, this.__internal__.serial.auto_response, null), await l(this, r, L).call(this);
344
+ }), this.__internal__.auto_response && l(this, r, h).call(this, this.__internal__.serial.auto_response, null), await l(this, r, S).call(this);
372
345
  } catch (e) {
373
346
  this.serialErrors(e);
374
347
  }
375
348
  }
376
349
  async serialForget() {
377
- return await l(this, r, P).call(this);
350
+ return await l(this, r, U).call(this);
378
351
  }
379
352
  decToHex(e) {
380
353
  return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
@@ -387,21 +360,35 @@ class J extends E {
387
360
  }
388
361
  add0x(e) {
389
362
  const t = [];
390
- return e.forEach((n, a) => {
391
- t[a] = "0x" + n;
363
+ return e.forEach((i, s) => {
364
+ t[s] = "0x" + i;
392
365
  }), t;
393
366
  }
394
367
  bytesToHex(e) {
395
368
  return this.add0x(Array.from(e, (t) => this.hexMaker(t)));
396
369
  }
370
+ validateBytes(e) {
371
+ let t = new Uint8Array(0);
372
+ if (e instanceof Uint8Array)
373
+ t = e;
374
+ else if (typeof e == "string")
375
+ t = this.parseStringToTextEncoder(e);
376
+ else if (Array.isArray(e) && typeof e[0] == "string")
377
+ t = this.stringArrayToUint8Array(e);
378
+ else if (Array.isArray(e) && typeof e[0] == "number")
379
+ t = new Uint8Array(e);
380
+ else
381
+ throw new Error("Invalid data type");
382
+ return t;
383
+ }
397
384
  async appendToQueue(e, t) {
398
- const n = this.bytesToHex(e);
385
+ const i = this.validateBytes(e);
399
386
  if (["connect", "connection:start"].includes(t)) {
400
387
  if (this.__internal__.serial.connected) return;
401
388
  await this.serialConnect();
402
389
  return;
403
390
  }
404
- this.__internal__.serial.queue.push({ bytes: n, action: t }), this.dispatch("internal:queue", {});
391
+ this.__internal__.serial.queue.push({ bytes: i, action: t }), this.dispatch("internal:queue", {});
405
392
  }
406
393
  serialSetConnectionConstant(e = 1) {
407
394
  throw new Error(`Method not implemented 'serialSetConnectionConstant' to listen on channel ${e}`);
@@ -417,8 +404,8 @@ class J extends E {
417
404
  }
418
405
  sumHex(e) {
419
406
  let t = 0;
420
- return e.forEach((n) => {
421
- t += parseInt(n, 16);
407
+ return e.forEach((i) => {
408
+ t += parseInt(i, 16);
422
409
  }), t.toString(16);
423
410
  }
424
411
  toString() {
@@ -431,7 +418,7 @@ class J extends E {
431
418
  });
432
419
  }
433
420
  softReload() {
434
- l(this, r, N).call(this), this.dispatch("serial:soft-reload", {});
421
+ l(this, r, q).call(this), this.dispatch("serial:soft-reload", {});
435
422
  }
436
423
  async sendConnect() {
437
424
  if (!this.__internal__.serial.bytes_connection)
@@ -453,13 +440,13 @@ class J extends E {
453
440
  }
454
441
  parseStringToTextEncoder(e = "", t = `
455
442
  `) {
456
- const n = new TextEncoder();
457
- return e += t, n.encode(e);
443
+ const i = new TextEncoder();
444
+ return e += t, i.encode(e);
458
445
  }
459
446
  parseStringToBytes(e = "", t = `
460
447
  `) {
461
- const n = this.parseStringToTextEncoder(e, t);
462
- return Array.from(n).map((a) => a.toString(16));
448
+ const i = this.parseStringToTextEncoder(e, t);
449
+ return Array.from(i).map((s) => s.toString(16));
463
450
  }
464
451
  parseUint8ToHex(e) {
465
452
  return Array.from(e).map((t) => t.toString(16));
@@ -469,29 +456,29 @@ class J extends E {
469
456
  }
470
457
  stringArrayToUint8Array(e) {
471
458
  const t = [];
472
- return e.forEach((n) => {
473
- const a = n.replace("0x", "");
474
- t.push(parseInt(a, 16));
459
+ return e.forEach((i) => {
460
+ const s = i.replace("0x", "");
461
+ t.push(parseInt(s, 16));
475
462
  }), new Uint8Array(t);
476
463
  }
477
464
  parseUint8ArrayToString(e) {
478
- const t = this.stringArrayToUint8Array(e);
479
- e = this.parseUint8ToHex(t);
480
- const n = e.map((a) => parseInt(a, 16));
481
- return this.__internal__.serial.response.replacer ? String.fromCharCode(...n).replace(this.__internal__.serial.response.replacer, "") : String.fromCharCode(...n);
465
+ let t = new Uint8Array(0);
466
+ e instanceof Uint8Array ? t = e : t = this.stringArrayToUint8Array(e), e = this.parseUint8ToHex(t);
467
+ const i = e.map((s) => parseInt(s, 16));
468
+ return this.__internal__.serial.response.replacer ? String.fromCharCode(...i).replace(this.__internal__.serial.response.replacer, "") : String.fromCharCode(...i);
482
469
  }
483
470
  hexToAscii(e) {
484
471
  const t = e.toString();
485
- let n = "";
486
- for (let a = 0; a < t.length; a += 2)
487
- n += String.fromCharCode(parseInt(t.substring(a, 2), 16));
488
- return n;
472
+ let i = "";
473
+ for (let s = 0; s < t.length; s += 2)
474
+ i += String.fromCharCode(parseInt(t.substring(s, 2), 16));
475
+ return i;
489
476
  }
490
477
  asciiToHex(e) {
491
478
  const t = [];
492
- for (let n = 0, a = e.length; n < a; n++) {
493
- const h = Number(e.charCodeAt(n)).toString(16);
494
- t.push(h);
479
+ for (let i = 0, s = e.length; i < s; i++) {
480
+ const c = Number(e.charCodeAt(i)).toString(16);
481
+ t.push(c);
495
482
  }
496
483
  return t.join("");
497
484
  }
@@ -499,121 +486,102 @@ class J extends E {
499
486
  return this.isConnected;
500
487
  }
501
488
  }
502
- r = new WeakSet(), f = function(e) {
489
+ r = new WeakSet(), d = function(e) {
503
490
  return !!(e && e.readable && e.writable);
504
- }, g = function(e = null) {
505
- this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", e), c.$dispatchChange(this);
506
- }, w = async function(e) {
491
+ }, f = function(e = null) {
492
+ this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", e), _.$dispatchChange(this);
493
+ }, y = async function(e) {
507
494
  const t = this.__internal__.serial.port;
508
495
  if (!t || t && (!t.readable || !t.writable))
509
- throw l(this, r, g).call(this, { error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
510
- const n = this.stringArrayToUint8Array(e);
496
+ throw l(this, r, f).call(this, { error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
497
+ const i = this.validateBytes(e);
511
498
  if (t.writable === null) return;
512
- const a = t.writable.getWriter();
513
- await a.write(n), a.releaseLock();
514
- }, u = function(e = [], t = null) {
499
+ const s = t.writable.getWriter();
500
+ await s.write(i), s.releaseLock();
501
+ }, h = function(e = new Uint8Array([]), t = null) {
515
502
  if (e && e.length > 0) {
516
- const n = this.__internal__.serial.connected;
517
- this.__internal__.serial.connected = l(this, r, f).call(this, this.__internal__.serial.port), c.$dispatchChange(this), !n && this.__internal__.serial.connected && this.dispatch("serial:connected"), this.__internal__.interval.reconnection && (clearInterval(this.__internal__.interval.reconnection), this.__internal__.interval.reconnection = 0), this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0);
518
- const a = [];
519
- for (const h in e)
520
- a.push(e[h].toString().padStart(2, "0").toLowerCase());
521
- if (this.__internal__.serial.response.as === "hex")
522
- this.serialMessage(a);
503
+ const i = this.__internal__.serial.connected;
504
+ if (this.__internal__.serial.connected = l(this, r, d).call(this, this.__internal__.serial.port), _.$dispatchChange(this), !i && this.__internal__.serial.connected && this.dispatch("serial:connected"), this.__internal__.interval.reconnection && (clearInterval(this.__internal__.interval.reconnection), this.__internal__.interval.reconnection = 0), this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), this.__internal__.serial.response.as === "hex")
505
+ this.serialMessage(this.parseUint8ToHex(e));
523
506
  else if (this.__internal__.serial.response.as === "uint8")
524
- this.serialMessage(this.parseHexToUint8(this.add0x(a)));
525
- else if (this.__internal__.serial.response.as === "string")
507
+ this.serialMessage(e);
508
+ else if (this.__internal__.serial.response.as === "string") {
509
+ const s = this.parseUint8ArrayToString(e);
526
510
  if (this.__internal__.serial.response.limiter !== null) {
527
- const d = this.parseUint8ArrayToString(this.add0x(a)).split(this.__internal__.serial.response.limiter);
528
- for (const m in d)
529
- d[m] && this.serialMessage(d[m]);
511
+ const c = s.split(this.__internal__.serial.response.limiter);
512
+ for (const p in c)
513
+ c[p] && this.serialMessage(c[p]);
530
514
  } else
531
- this.serialMessage(this.parseUint8ArrayToString(this.add0x(a)));
532
- else {
533
- const h = this.stringToArrayBuffer(
534
- this.parseUint8ArrayToString(this.add0x(a))
535
- );
536
- this.serialMessage(h);
515
+ this.serialMessage(s);
516
+ } else {
517
+ const s = this.stringToArrayBuffer(this.parseUint8ArrayToString(e));
518
+ this.serialMessage(s);
537
519
  }
538
520
  } else
539
521
  this.serialCorruptMessage(e, t);
540
522
  this.__internal__.serial.queue.length !== 0 && this.dispatch("internal:queue", {});
541
- }, T = async function() {
523
+ }, A = async function() {
542
524
  const e = this.serialFilters, t = await navigator.serial.getPorts({ filters: e });
543
- return e.length === 0 ? t : t.filter((a) => {
544
- const h = a.getInfo();
545
- return e.some((d) => h.usbProductId === d.usbProductId && h.usbVendorId === d.usbVendorId);
546
- }).filter((a) => !l(this, r, f).call(this, a));
547
- }, U = function(e) {
525
+ return e.length === 0 ? t : t.filter((s) => {
526
+ const c = s.getInfo();
527
+ return e.some((p) => c.usbProductId === p.usbProductId && c.usbVendorId === p.usbVendorId);
528
+ }).filter((s) => !l(this, r, d).call(this, s));
529
+ }, E = function(e) {
548
530
  if (e) {
549
- const t = this.__internal__.serial.response.buffer, n = new Uint8Array(t.length + e.byteLength);
550
- n.set(t, 0), n.set(new Uint8Array(e), t.length), this.__internal__.serial.response.buffer = n;
531
+ const t = this.__internal__.serial.response.buffer, i = new Uint8Array(t.length + e.byteLength);
532
+ i.set(t, 0), i.set(new Uint8Array(e), t.length), this.__internal__.serial.response.buffer = i;
551
533
  }
552
- }, k = async function() {
534
+ }, x = async function() {
553
535
  this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
554
- const e = [];
555
- for (const t in this.__internal__.serial.response.buffer)
556
- e.push(this.__internal__.serial.response.buffer[t].toString(16));
557
- this.__internal__.serial.response.buffer && l(this, r, u).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0);
536
+ this.__internal__.serial.response.buffer && l(this, r, h).call(this, this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
558
537
  }, 400);
559
- }, D = async function() {
538
+ }, T = async function() {
560
539
  if (this.__internal__.serial.response.length !== null) {
561
- if (this.__internal__.serial.response.length === this.__internal__.serial.response.buffer.length) {
562
- const e = [];
563
- for (const t in this.__internal__.serial.response.buffer)
564
- e.push(this.__internal__.serial.response.buffer[t].toString(16));
565
- l(this, r, u).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0);
566
- } else if (this.__internal__.serial.response.length < this.__internal__.serial.response.buffer.length) {
540
+ if (this.__internal__.serial.response.length === this.__internal__.serial.response.buffer.length)
541
+ l(this, r, h).call(this, this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
542
+ else if (this.__internal__.serial.response.length < this.__internal__.serial.response.buffer.length) {
567
543
  let e = new Uint8Array(0);
568
- for (let n = 0; n < this.__internal__.serial.response.length; n++)
569
- e[n] = this.__internal__.serial.response.buffer[n];
544
+ for (let i = 0; i < this.__internal__.serial.response.length; i++)
545
+ e[i] = this.__internal__.serial.response.buffer[i];
570
546
  if (e.length === this.__internal__.serial.response.length) {
571
- const n = [];
572
- for (const a in e)
573
- n.push(e[a].toString(16));
574
- l(this, r, u).call(this, n), this.__internal__.serial.response.buffer = new Uint8Array(0);
547
+ l(this, r, h).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0);
575
548
  return;
576
549
  }
577
550
  e = new Uint8Array(0);
578
551
  const t = this.__internal__.serial.response.length * 2;
579
552
  if (this.__internal__.serial.response.buffer.length === t) {
580
- for (let n = 14; n < t; n++)
581
- e[n - this.__internal__.serial.response.length] = this.__internal__.serial.response.buffer[n];
582
- if (e.length === this.__internal__.serial.response.length) {
583
- const n = [];
584
- for (const a in e)
585
- n.push(e[a].toString(16));
586
- l(this, r, u).call(this, n), this.__internal__.serial.response.buffer = new Uint8Array(0);
587
- }
553
+ for (let i = 14; i < t; i++)
554
+ e[i - this.__internal__.serial.response.length] = this.__internal__.serial.response.buffer[i];
555
+ e.length === this.__internal__.serial.response.length && (l(this, r, h).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0));
588
556
  }
589
557
  }
590
558
  }
591
- }, L = async function() {
559
+ }, S = async function() {
592
560
  const e = this.__internal__.serial.port;
593
561
  if (!e || !e.readable) throw new Error("Port is not readable");
594
562
  for (; e.readable && this.__internal__.serial.keep_reading; ) {
595
563
  const t = e.readable.getReader();
596
564
  this.__internal__.serial.reader = t;
597
565
  try {
598
- let n = !0;
599
- for (; n; ) {
600
- const { value: a, done: h } = await t.read();
601
- if (h) {
602
- t.releaseLock(), this.__internal__.serial.keep_reading = !1, n = !1;
566
+ let i = !0;
567
+ for (; i; ) {
568
+ const { value: s, done: c } = await t.read();
569
+ if (c) {
570
+ t.releaseLock(), this.__internal__.serial.keep_reading = !1, i = !1;
603
571
  break;
604
572
  }
605
- l(this, r, U).call(this, a), this.__internal__.serial.response.length === null ? await l(this, r, k).call(this) : await l(this, r, D).call(this);
573
+ l(this, r, E).call(this, s), this.__internal__.serial.response.length === null ? await l(this, r, x).call(this) : await l(this, r, T).call(this);
606
574
  }
607
- } catch (n) {
608
- this.serialErrors(n);
575
+ } catch (i) {
576
+ this.serialErrors(i);
609
577
  } finally {
610
578
  t.releaseLock();
611
579
  }
612
580
  }
613
581
  this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
614
- }, P = async function() {
582
+ }, U = async function() {
615
583
  return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
616
- }, I = function() {
584
+ }, k = function() {
617
585
  [
618
586
  "serial:connected",
619
587
  "serial:connecting",
@@ -632,37 +600,45 @@ r = new WeakSet(), f = function(e) {
632
600
  ].forEach((t) => {
633
601
  this.serialRegisterAvailableListener(t);
634
602
  });
635
- }, $ = function() {
603
+ }, P = function() {
636
604
  const e = this;
637
605
  this.on("internal:queue", async () => {
638
606
  var t;
639
- await l(t = e, r, R).call(t);
640
- }), l(this, r, q).call(this);
641
- }, q = function() {
607
+ await l(t = e, r, D).call(t);
608
+ }), l(this, r, L).call(this);
609
+ }, L = function() {
642
610
  const e = this;
643
611
  navigator.serial.addEventListener("connect", async () => {
644
612
  e.isDisconnected && await e.serialConnect().catch(() => {
645
613
  });
646
614
  });
647
- }, R = async function() {
648
- if (!l(this, r, f).call(this, this.__internal__.serial.port)) {
649
- l(this, r, g).call(this, { error: "Port is closed, not readable or writable." }), await this.serialConnect();
615
+ }, D = async function() {
616
+ if (!l(this, r, d).call(this, this.__internal__.serial.port)) {
617
+ l(this, r, f).call(this, { error: "Port is closed, not readable or writable." }), await this.serialConnect();
650
618
  return;
651
619
  }
652
620
  if (this.__internal__.timeout.until_response || this.__internal__.serial.queue.length === 0) return;
653
621
  const e = this.__internal__.serial.queue[0];
654
622
  let t = this.__internal__.time.response_general;
655
- e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
623
+ if (e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
656
624
  await this.timeout(e.bytes, e.action);
657
- }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await l(this, r, w).call(this, e.bytes), this.dispatch("serial:sent", {
625
+ }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await l(this, r, y).call(this, e.bytes), this.dispatch("serial:sent", {
658
626
  action: e.action,
659
627
  bytes: e.bytes
660
- }), this.__internal__.auto_response && l(this, r, u).call(this, this.__internal__.serial.auto_response, null);
661
- const n = [...this.__internal__.serial.queue];
662
- this.__internal__.serial.queue = n.splice(1);
663
- }, M = function(e = 1) {
628
+ }), this.__internal__.auto_response) {
629
+ let s = new Uint8Array(0);
630
+ try {
631
+ s = this.validateBytes(this.__internal__.serial.auto_response);
632
+ } catch (c) {
633
+ this.serialErrors(c);
634
+ }
635
+ l(this, r, h).call(this, s, null);
636
+ }
637
+ const i = [...this.__internal__.serial.queue];
638
+ this.__internal__.serial.queue = i.splice(1);
639
+ }, $ = function(e = 1) {
664
640
  this.__internal__.device_number = e, this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(e);
665
- }, N = function() {
641
+ }, q = function() {
666
642
  this.__internal__.last_error = {
667
643
  message: null,
668
644
  action: null,
@@ -671,7 +647,7 @@ r = new WeakSet(), f = function(e) {
671
647
  };
672
648
  };
673
649
  export {
674
- J as Core,
675
- c as Devices,
676
- E as Dispatcher
650
+ R as Core,
651
+ _ as Devices,
652
+ C as Dispatcher
677
653
  };