@danidoble/webserial 4.3.3 → 4.3.5

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,32 +1,32 @@
1
- var j = Object.defineProperty, B = (i) => {
2
- throw TypeError(i);
3
- }, F = (i, e, t) => e in i ? j(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t, g = (i, e, t) => F(i, typeof e != "symbol" ? e + "" : e, t), W = (i, e, t) => e.has(i) || B("Cannot " + t), Q = (i, e, t) => e.has(i) ? B("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(i) : e.set(i, t), l = (i, e, t) => (W(i, e, "access private method"), t);
4
- class x extends CustomEvent {
1
+ var V = Object.defineProperty, R = (n) => {
2
+ throw TypeError(n);
3
+ }, z = (n, e, t) => e in n ? V(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t, y = (n, e, t) => z(n, typeof e != "symbol" ? e + "" : e, t), T = (n, e, t) => e.has(n) || R("Cannot " + t), b = (n, e, t) => (T(n, e, "read from private field"), e.get(n)), S = (n, e, t) => e.has(n) ? R("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(n) : e.set(n, t), J = (n, e, t, i) => (T(n, e, "write to private field"), e.set(n, t), t), l = (n, e, t) => (T(n, e, "access private method"), t);
4
+ class A extends CustomEvent {
5
5
  constructor(e, t) {
6
6
  super(e, t);
7
7
  }
8
8
  }
9
- class P extends EventTarget {
9
+ class I extends EventTarget {
10
10
  constructor() {
11
- super(...arguments), g(this, "__listeners__", {
11
+ super(...arguments), y(this, "__listeners__", {
12
12
  debug: !1
13
- }), g(this, "__debug__", !1), g(this, "__listenersCallbacks__", []);
13
+ }), y(this, "__debug__", !1), y(this, "__listenersCallbacks__", []);
14
14
  }
15
15
  dispatch(e, t = null) {
16
- const n = new x(e, { detail: t });
17
- this.dispatchEvent(n), this.__debug__ && this.dispatchEvent(new x("debug", { detail: { type: e, data: t } }));
16
+ const i = new A(e, { detail: t });
17
+ this.dispatchEvent(i), this.__debug__ && this.dispatchEvent(new A("debug", { detail: { type: e, data: t } }));
18
18
  }
19
- dispatchAsync(e, t = null, n = 100) {
19
+ dispatchAsync(e, t = null, i = 100) {
20
20
  const s = this;
21
21
  setTimeout(() => {
22
22
  s.dispatch(e, t);
23
- }, n);
23
+ }, i);
24
24
  }
25
25
  on(e, t) {
26
26
  typeof this.__listeners__[e] < "u" && !this.__listeners__[e] && (this.__listeners__[e] = !0), this.__listenersCallbacks__.push({ key: e, callback: t }), this.addEventListener(e, t);
27
27
  }
28
28
  off(e, t) {
29
- this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((n) => !(n.key === e && n.callback === t)), this.removeEventListener(e, t);
29
+ this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((i) => !(i.key === e && i.callback === t)), this.removeEventListener(e, t);
30
30
  }
31
31
  serialRegisterAvailableListener(e) {
32
32
  this.__listeners__[e] || (this.__listeners__[e] = !1);
@@ -39,13 +39,12 @@ class P extends EventTarget {
39
39
  }
40
40
  removeAllListeners() {
41
41
  for (const e of this.__listenersCallbacks__)
42
- this.removeEventListener(e.key, e.callback);
43
- this.__listenersCallbacks__ = [], this.__listeners__ = {
44
- debug: !1
45
- };
42
+ ["internal:queue"].includes(e.key) || (this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((t) => !(t.key === e.key && t.callback === e.callback)), this.removeEventListener(e.key, e.callback));
43
+ for (const e of Object.keys(this.__listeners__))
44
+ this.__listeners__[e] = !1;
46
45
  }
47
46
  }
48
- const w = class a extends P {
47
+ const C = class a extends I {
49
48
  constructor() {
50
49
  super(), ["change"].forEach((e) => {
51
50
  this.serialRegisterAvailableListener(e);
@@ -64,10 +63,10 @@ const w = class a extends P {
64
63
  static add(e) {
65
64
  const t = e.typeDevice;
66
65
  typeof a.devices[t] > "u" && a.registerType(t);
67
- const n = e.uuid;
68
- if (typeof a.devices[t] > "u" && a.typeError(t), a.devices[t][n])
69
- throw new Error(`Device with id ${n} already exists`);
70
- return a.devices[t][n] = e, a.$dispatchChange(e), Object.keys(a.devices[t]).indexOf(n);
66
+ const i = e.uuid;
67
+ if (typeof a.devices[t] > "u" && a.typeError(t), a.devices[t][i])
68
+ throw new Error(`Device with id ${i} already exists`);
69
+ return a.devices[t][i] = e, a.$dispatchChange(e), Object.keys(a.devices[t]).indexOf(i);
71
70
  }
72
71
  static get(e, t) {
73
72
  return typeof a.devices[e] > "u" && a.registerType(e), typeof a.devices[e] > "u" && a.typeError(e), a.devices[e][t];
@@ -79,10 +78,10 @@ const w = class a extends P {
79
78
  return Object.values(a.devices).map((e) => Object.values(e)).flat();
80
79
  }
81
80
  static getByNumber(e, t) {
82
- return typeof a.devices[e] > "u" && a.typeError(e), Object.values(a.devices[e]).find((n) => n.deviceNumber === t) ?? null;
81
+ return typeof a.devices[e] > "u" && a.typeError(e), Object.values(a.devices[e]).find((i) => i.deviceNumber === t) ?? null;
83
82
  }
84
83
  static getCustom(e, t = 1) {
85
- return typeof a.devices[e] > "u" && a.typeError(e), Object.values(a.devices[e]).find((n) => n.deviceNumber === t) ?? null;
84
+ return typeof a.devices[e] > "u" && a.typeError(e), Object.values(a.devices[e]).find((i) => i.deviceNumber === t) ?? null;
86
85
  }
87
86
  static async connectToAll() {
88
87
  const e = a.getList();
@@ -117,18 +116,18 @@ const w = class a extends P {
117
116
  return Promise.resolve(e.filter((t) => t.isDisconnected));
118
117
  }
119
118
  };
120
- g(w, "instance"), g(w, "devices", {});
121
- let c = w;
119
+ y(C, "instance"), y(C, "devices", {});
120
+ let c = C;
122
121
  c.instance || (c.instance = new c());
123
- function U(i = 100) {
122
+ function M(n = 100) {
124
123
  return new Promise(
125
- (e) => setTimeout(() => e(), i)
124
+ (e) => setTimeout(() => e(), n)
126
125
  );
127
126
  }
128
- function V() {
127
+ function G() {
129
128
  return "serial" in navigator;
130
129
  }
131
- const b = {
130
+ const v = {
132
131
  baudRate: 9600,
133
132
  dataBits: 8,
134
133
  stopBits: 1,
@@ -136,22 +135,22 @@ const b = {
136
135
  bufferSize: 32768,
137
136
  flowControl: "none"
138
137
  };
139
- var r, p, y, $, v, T, u, R, E, S, A, k, I, f, M, q, N, L, D, O, H;
140
- class z extends P {
138
+ var p, r, f, m, q, x, k, u, N, L, D, B, P, O, g, H, j, F, U, $, W, Q;
139
+ class K extends I {
141
140
  constructor({
142
141
  filters: e = null,
143
- config_port: t = b,
144
- no_device: n = 1,
142
+ config_port: t = v,
143
+ no_device: i = 1,
145
144
  device_listen_on_channel: s = 1,
146
145
  bypassSerialBytesConnection: o = !1
147
146
  } = {
148
147
  filters: null,
149
- config_port: b,
148
+ config_port: v,
150
149
  no_device: 1,
151
150
  device_listen_on_channel: 1,
152
151
  bypassSerialBytesConnection: !1
153
152
  }) {
154
- if (super(), Q(this, r), g(this, "__internal__", {
153
+ if (super(), S(this, r), y(this, "__internal__", {
155
154
  bypassSerialBytesConnection: !1,
156
155
  auto_response: !1,
157
156
  device_number: 1,
@@ -188,7 +187,7 @@ class z extends P {
188
187
  delay_first_connection: 200,
189
188
  bytes_connection: null,
190
189
  filters: [],
191
- config_port: b,
190
+ config_port: v,
192
191
  queue: [],
193
192
  auto_response: ["DD", "DD"],
194
193
  free_timeout_ms: 50,
@@ -211,9 +210,9 @@ class z extends P {
211
210
  interval: {
212
211
  reconnection: 0
213
212
  }
214
- }), !("serial" in navigator))
213
+ }), S(this, p, null), !("serial" in navigator))
215
214
  throw new Error("Web Serial not supported");
216
- e && (this.serialFilters = e), t && (this.serialConfigPort = t), o && (this.__internal__.bypassSerialBytesConnection = o), n && l(this, r, O).call(this, n), s && ["number", "string"].includes(typeof s) && (this.listenOnChannel = s), l(this, r, q).call(this), l(this, r, N).call(this);
215
+ e && (this.serialFilters = e), t && (this.serialConfigPort = t), o && (this.__internal__.bypassSerialBytesConnection = o), i && l(this, r, W).call(this, i), s && ["number", "string"].includes(typeof s) && (this.listenOnChannel = s), l(this, r, j).call(this), l(this, r, F).call(this);
217
216
  }
218
217
  set listenOnChannel(e) {
219
218
  if (typeof e == "string" && (e = parseInt(e)), isNaN(e) || e < 1 || e > 255)
@@ -247,15 +246,15 @@ class z extends P {
247
246
  this.__internal__.serial.useRTSCTS = e;
248
247
  }
249
248
  get isConnected() {
250
- const e = this.__internal__.serial.connected, t = l(this, r, p).call(this, this.__internal__.serial.port);
251
- return e && !t && l(this, r, y).call(this, { error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
249
+ const e = this.__internal__.serial.connected, t = l(this, r, f).call(this, this.__internal__.serial.port);
250
+ return e && !t && l(this, r, m).call(this, { error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
252
251
  }
253
252
  get isConnecting() {
254
253
  return this.__internal__.serial.connecting;
255
254
  }
256
255
  get isDisconnected() {
257
- const e = this.__internal__.serial.connected, t = l(this, r, p).call(this, this.__internal__.serial.port);
258
- return !e && t && (this.dispatch("serial:connected"), l(this, r, f).call(this, !1), c.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
256
+ const e = this.__internal__.serial.connected, t = l(this, r, f).call(this, this.__internal__.serial.port);
257
+ return !e && t && (this.dispatch("serial:connected"), l(this, r, g).call(this, !1), c.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
259
258
  }
260
259
  get deviceNumber() {
261
260
  return this.__internal__.device_number;
@@ -333,15 +332,13 @@ class z extends P {
333
332
  });
334
333
  }
335
334
  async disconnect(e = null) {
336
- await this.serialDisconnect(), l(this, r, y).call(this, e);
335
+ await this.serialDisconnect(), l(this, r, m).call(this, e);
337
336
  }
338
337
  async connect() {
339
338
  return this.isConnected ? !0 : (this.__internal__.serial.aux_connecting = "idle", new Promise((e, t) => {
340
- V() || t("Web Serial not supported");
341
- const n = l(this, r, $).bind(this);
342
- this.on("serial:connecting", n);
343
- const s = setInterval(() => {
344
- this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(s), this.__internal__.serial.aux_connecting = "idle", this.off("serial:connecting", n), this.isConnected ? e(!0) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`)) : this.__internal__.serial.aux_connecting === "connecting" && (this.__internal__.serial.aux_connecting = "idle", this.dispatch("serial:connecting", { active: !0 }));
339
+ G() || t("Web Serial not supported"), b(this, p) || J(this, p, l(this, r, q).bind(this)), this.on("internal:connecting", b(this, p));
340
+ const i = setInterval(() => {
341
+ this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(i), this.__internal__.serial.aux_connecting = "idle", b(this, p) !== null && this.off("internal:connecting", b(this, p)), this.isConnected ? e(!0) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`)) : this.__internal__.serial.aux_connecting === "connecting" && (this.__internal__.serial.aux_connecting = "idle", this.dispatch("internal:connecting", { active: !0 }), this.dispatch("serial:connecting", { active: !0 }));
345
342
  }, 100);
346
343
  this.serialConnect();
347
344
  }));
@@ -349,7 +346,7 @@ class z extends P {
349
346
  async serialDisconnect() {
350
347
  try {
351
348
  const e = this.__internal__.serial.reader, t = this.__internal__.serial.output_stream;
352
- e && (await e.cancel().catch((n) => this.serialErrors(n)), 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();
349
+ e && (await e.cancel().catch((i) => this.serialErrors(i)), 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();
353
350
  } catch (e) {
354
351
  this.serialErrors(e);
355
352
  } finally {
@@ -371,8 +368,8 @@ class z extends P {
371
368
  async serialPortsSaved(e) {
372
369
  const t = this.serialFilters;
373
370
  if (this.__internal__.aux_port_connector < e.length) {
374
- const n = this.__internal__.aux_port_connector;
375
- this.__internal__.serial.port = e[n];
371
+ const i = this.__internal__.aux_port_connector;
372
+ this.__internal__.serial.port = e[i];
376
373
  } else
377
374
  this.__internal__.aux_port_connector = 0, this.__internal__.serial.port = await navigator.serial.requestPort({
378
375
  filters: t
@@ -429,8 +426,8 @@ class z extends P {
429
426
  }
430
427
  async serialConnect() {
431
428
  try {
432
- l(this, r, f).call(this, !0);
433
- const e = await l(this, r, R).call(this);
429
+ l(this, r, g).call(this, !0);
430
+ const e = await l(this, r, N).call(this);
434
431
  if (e.length > 0)
435
432
  await this.serialPortsSaved(e);
436
433
  else {
@@ -443,24 +440,24 @@ class z extends P {
443
440
  if (!t)
444
441
  throw new Error("No port selected by the user");
445
442
  await t.open(this.serialConfigPort);
446
- const n = this;
443
+ const i = this;
447
444
  t.onconnect = (s) => {
448
445
  var o;
449
- n.dispatch("serial:connected", s), l(o = n, r, f).call(o, !1), c.$dispatchChange(this), n.__internal__.serial.queue.length > 0 && n.dispatch("internal:queue", {});
446
+ i.dispatch("serial:connected", s), l(o = i, r, g).call(o, !1), c.$dispatchChange(this), i.__internal__.serial.queue.length > 0 && i.dispatch("internal:queue", {});
450
447
  }, t.ondisconnect = async () => {
451
- await n.disconnect();
452
- }, await U(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
453
- await n.timeout(n.__internal__.serial.bytes_connection ?? [], "connection:start");
454
- }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await l(this, r, v).call(this, this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
448
+ await i.disconnect();
449
+ }, await M(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
450
+ await i.timeout(i.__internal__.serial.bytes_connection ?? [], "connection:start");
451
+ }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await l(this, r, x).call(this, this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
455
452
  action: "connect",
456
453
  bytes: this.__internal__.serial.bytes_connection
457
- }), this.__internal__.auto_response && l(this, r, u).call(this, this.__internal__.serial.auto_response), await l(this, r, I).call(this);
454
+ }), this.__internal__.auto_response && l(this, r, u).call(this, this.__internal__.serial.auto_response), await l(this, r, O).call(this);
458
455
  } catch (e) {
459
- l(this, r, f).call(this, !1), this.serialErrors(e);
456
+ l(this, r, g).call(this, !1), this.serialErrors(e);
460
457
  }
461
458
  }
462
459
  async serialForget() {
463
- return await l(this, r, M).call(this);
460
+ return await l(this, r, H).call(this);
464
461
  }
465
462
  decToHex(e) {
466
463
  return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
@@ -473,8 +470,8 @@ class z extends P {
473
470
  }
474
471
  add0x(e) {
475
472
  const t = [];
476
- return e.forEach((n, s) => {
477
- t[s] = "0x" + n;
473
+ return e.forEach((i, s) => {
474
+ t[s] = "0x" + i;
478
475
  }), t;
479
476
  }
480
477
  bytesToHex(e) {
@@ -495,13 +492,13 @@ class z extends P {
495
492
  return t;
496
493
  }
497
494
  async appendToQueue(e, t) {
498
- const n = this.validateBytes(e);
495
+ const i = this.validateBytes(e);
499
496
  if (["connect", "connection:start"].includes(t)) {
500
497
  if (this.__internal__.serial.connected) return;
501
498
  await this.serialConnect();
502
499
  return;
503
500
  }
504
- this.__internal__.serial.queue.push({ bytes: n, action: t }), this.dispatch("internal:queue", {});
501
+ this.__internal__.serial.queue.push({ bytes: i, action: t }), this.dispatch("internal:queue", {});
505
502
  }
506
503
  serialSetConnectionConstant(e = 1) {
507
504
  if (this.__internal__.bypassSerialBytesConnection) return this.__internal__.serial.bytes_connection;
@@ -518,8 +515,8 @@ class z extends P {
518
515
  }
519
516
  sumHex(e) {
520
517
  let t = 0;
521
- return e.forEach((n) => {
522
- t += parseInt(n, 16);
518
+ return e.forEach((i) => {
519
+ t += parseInt(i, 16);
523
520
  }), t.toString(16);
524
521
  }
525
522
  toString() {
@@ -532,7 +529,7 @@ class z extends P {
532
529
  });
533
530
  }
534
531
  softReload() {
535
- l(this, r, H).call(this), this.dispatch("serial:soft-reload", {});
532
+ l(this, r, Q).call(this), this.dispatch("serial:soft-reload", {});
536
533
  }
537
534
  async sendConnect() {
538
535
  if (!this.__internal__.serial.bytes_connection)
@@ -553,13 +550,13 @@ class z extends P {
553
550
  }
554
551
  parseStringToTextEncoder(e = "", t = `
555
552
  `) {
556
- const n = new TextEncoder();
557
- return e += t, n.encode(e);
553
+ const i = new TextEncoder();
554
+ return e += t, i.encode(e);
558
555
  }
559
556
  parseStringToBytes(e = "", t = `
560
557
  `) {
561
- const n = this.parseStringToTextEncoder(e, t);
562
- return Array.from(n).map((s) => s.toString(16));
558
+ const i = this.parseStringToTextEncoder(e, t);
559
+ return Array.from(i).map((s) => s.toString(16));
563
560
  }
564
561
  parseUint8ToHex(e) {
565
562
  return Array.from(e).map((t) => t.toString(16));
@@ -569,28 +566,28 @@ class z extends P {
569
566
  }
570
567
  stringArrayToUint8Array(e) {
571
568
  const t = [];
572
- return typeof e == "string" ? this.parseStringToTextEncoder(e).buffer : (e.forEach((n) => {
573
- const s = n.replace("0x", "");
569
+ return typeof e == "string" ? this.parseStringToTextEncoder(e).buffer : (e.forEach((i) => {
570
+ const s = i.replace("0x", "");
574
571
  t.push(parseInt(s, 16));
575
572
  }), new Uint8Array(t));
576
573
  }
577
574
  parseUint8ArrayToString(e) {
578
575
  let t = new Uint8Array(0);
579
576
  e instanceof Uint8Array ? t = e : t = this.stringArrayToUint8Array(e), e = this.parseUint8ToHex(t);
580
- const n = e.map((s) => parseInt(s, 16));
581
- return this.__internal__.serial.response.replacer ? String.fromCharCode(...n).replace(this.__internal__.serial.response.replacer, "") : String.fromCharCode(...n);
577
+ const i = e.map((s) => parseInt(s, 16));
578
+ return this.__internal__.serial.response.replacer ? String.fromCharCode(...i).replace(this.__internal__.serial.response.replacer, "") : String.fromCharCode(...i);
582
579
  }
583
580
  hexToAscii(e) {
584
581
  const t = e.toString();
585
- let n = "";
582
+ let i = "";
586
583
  for (let s = 0; s < t.length; s += 2)
587
- n += String.fromCharCode(parseInt(t.substring(s, 2), 16));
588
- return n;
584
+ i += String.fromCharCode(parseInt(t.substring(s, 2), 16));
585
+ return i;
589
586
  }
590
587
  asciiToHex(e) {
591
588
  const t = [];
592
- for (let n = 0, s = e.length; n < s; n++) {
593
- const o = Number(e.charCodeAt(n)).toString(16);
589
+ for (let i = 0, s = e.length; i < s; i++) {
590
+ const o = Number(e.charCodeAt(i)).toString(16);
594
591
  t.push(o);
595
592
  }
596
593
  return t.join("");
@@ -599,151 +596,151 @@ class z extends P {
599
596
  return this.isConnected;
600
597
  }
601
598
  }
602
- r = /* @__PURE__ */ new WeakSet(), p = function(i) {
603
- return !!(i && i.readable && i.writable);
604
- }, y = function(i = null) {
605
- this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", i), c.$dispatchChange(this);
606
- }, $ = function(i) {
607
- this.__internal__.serial.aux_connecting = i.detail.active ? "connecting" : "finished";
608
- }, v = async function(i) {
599
+ p = /* @__PURE__ */ new WeakMap(), r = /* @__PURE__ */ new WeakSet(), f = function(n) {
600
+ return !!(n && n.readable && n.writable);
601
+ }, m = function(n = null) {
602
+ this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", n), c.$dispatchChange(this);
603
+ }, q = function(n) {
604
+ this.__internal__.serial.aux_connecting = n.detail.active ? "connecting" : "finished";
605
+ }, x = async function(n) {
609
606
  const e = this.__internal__.serial.port;
610
607
  if (!e || e && (!e.readable || !e.writable))
611
- throw l(this, r, y).call(this, { error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
612
- const t = this.validateBytes(i);
613
- if (this.useRTSCTS && await l(this, r, T).call(this, e, 5e3), e.writable === null) return;
614
- const n = e.writable.getWriter();
615
- await n.write(t), n.releaseLock();
616
- }, T = async function(i, e = 5e3) {
608
+ throw l(this, r, m).call(this, { error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
609
+ const t = this.validateBytes(n);
610
+ if (this.useRTSCTS && await l(this, r, k).call(this, e, 5e3), e.writable === null) return;
611
+ const i = e.writable.getWriter();
612
+ await i.write(t), i.releaseLock();
613
+ }, k = async function(n, e = 5e3) {
617
614
  const t = Date.now();
618
615
  for (; ; ) {
619
616
  if (Date.now() - t > e)
620
617
  throw new Error("Timeout waiting for clearToSend signal");
621
- const { clearToSend: n } = await i.getSignals();
622
- if (n) return;
623
- await U(100);
618
+ const { clearToSend: i } = await n.getSignals();
619
+ if (i) return;
620
+ await M(100);
624
621
  }
625
- }, u = function(i = new Uint8Array([]), e = !1) {
626
- if (i && i.length > 0) {
622
+ }, u = function(n = new Uint8Array([]), e = !1) {
623
+ if (n && n.length > 0) {
627
624
  const t = this.__internal__.serial.connected;
628
- if (this.__internal__.serial.connected = l(this, r, p).call(this, this.__internal__.serial.port), c.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), l(this, r, f).call(this, !1)), 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")
629
- e ? this.serialCorruptMessage(this.parseUint8ToHex(i)) : this.serialMessage(this.parseUint8ToHex(i));
625
+ if (this.__internal__.serial.connected = l(this, r, f).call(this, this.__internal__.serial.port), c.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), l(this, r, g).call(this, !1)), 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")
626
+ e ? this.serialCorruptMessage(this.parseUint8ToHex(n)) : this.serialMessage(this.parseUint8ToHex(n));
630
627
  else if (this.__internal__.serial.response.as === "uint8")
631
- e ? this.serialCorruptMessage(i) : this.serialMessage(i);
628
+ e ? this.serialCorruptMessage(n) : this.serialMessage(n);
632
629
  else if (this.__internal__.serial.response.as === "string") {
633
- const n = this.parseUint8ArrayToString(i);
630
+ const i = this.parseUint8ArrayToString(n);
634
631
  if (this.__internal__.serial.response.limiter !== null) {
635
- const s = n.split(this.__internal__.serial.response.limiter);
632
+ const s = i.split(this.__internal__.serial.response.limiter);
636
633
  for (const o in s)
637
634
  s[o] && (e ? this.serialCorruptMessage(s[o]) : this.serialMessage(s[o]));
638
635
  } else
639
- e ? this.serialCorruptMessage(n) : this.serialMessage(n);
636
+ e ? this.serialCorruptMessage(i) : this.serialMessage(i);
640
637
  } else {
641
- const n = this.stringToArrayBuffer(this.parseUint8ArrayToString(i));
642
- e ? this.serialCorruptMessage(n) : this.serialMessage(n);
638
+ const i = this.stringToArrayBuffer(this.parseUint8ArrayToString(n));
639
+ e ? this.serialCorruptMessage(i) : this.serialMessage(i);
643
640
  }
644
641
  }
645
642
  this.__internal__.serial.queue.length !== 0 && this.dispatch("internal:queue", {});
646
- }, R = async function() {
647
- const i = this.serialFilters, e = await navigator.serial.getPorts({ filters: i });
648
- return i.length === 0 ? e : e.filter((t) => {
649
- const n = t.getInfo();
650
- return i.some((s) => n.usbProductId === s.usbProductId && n.usbVendorId === s.usbVendorId);
651
- }).filter((t) => !l(this, r, p).call(this, t));
652
- }, E = function(i) {
653
- if (i) {
654
- const e = this.__internal__.serial.response.buffer, t = new Uint8Array(e.length + i.byteLength);
655
- t.set(e, 0), t.set(new Uint8Array(i), e.length), this.__internal__.serial.response.buffer = t;
656
- }
657
- }, S = async function() {
643
+ }, N = async function() {
644
+ const n = this.serialFilters, e = await navigator.serial.getPorts({ filters: n });
645
+ return n.length === 0 ? e : e.filter((t) => {
646
+ const i = t.getInfo();
647
+ return n.some((s) => i.usbProductId === s.usbProductId && i.usbVendorId === s.usbVendorId);
648
+ }).filter((t) => !l(this, r, f).call(this, t));
649
+ }, L = function(n) {
650
+ if (n) {
651
+ const e = this.__internal__.serial.response.buffer, t = new Uint8Array(e.length + n.byteLength);
652
+ t.set(e, 0), t.set(new Uint8Array(n), e.length), this.__internal__.serial.response.buffer = t;
653
+ }
654
+ }, D = async function() {
658
655
  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(() => {
659
656
  this.__internal__.serial.response.buffer && l(this, r, u).call(this, this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
660
657
  }, this.__internal__.serial.free_timeout_ms || 50);
661
- }, A = async function() {
662
- const i = this.__internal__.serial.response.length;
658
+ }, B = async function() {
659
+ const n = this.__internal__.serial.response.length;
663
660
  let e = this.__internal__.serial.response.buffer;
664
- if (this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), !(i === null || !e || e.length === 0)) {
665
- for (; e.length >= i; ) {
666
- const t = e.slice(0, i);
667
- l(this, r, u).call(this, t), e = e.slice(i);
661
+ if (this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), !(n === null || !e || e.length === 0)) {
662
+ for (; e.length >= n; ) {
663
+ const t = e.slice(0, n);
664
+ l(this, r, u).call(this, t), e = e.slice(n);
668
665
  }
669
666
  this.__internal__.serial.response.buffer = e, e.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
670
667
  l(this, r, u).call(this, this.__internal__.serial.response.buffer, !0);
671
668
  }, this.__internal__.serial.free_timeout_ms || 50));
672
669
  }
673
- }, k = async function() {
670
+ }, P = async function() {
674
671
  const {
675
- limiter: i,
672
+ limiter: n,
676
673
  prefixLimiter: e = !1,
677
674
  sufixLimiter: t = !0
678
675
  } = this.__internal__.serial.response;
679
- if (!i)
676
+ if (!n)
680
677
  throw new Error("No limiter defined for delimited serial response");
681
- const n = this.__internal__.serial.response.buffer;
682
- if (!i || !n || n.length === 0) return;
678
+ const i = this.__internal__.serial.response.buffer;
679
+ if (!n || !i || i.length === 0) return;
683
680
  this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0);
684
- let s = new TextDecoder().decode(n);
681
+ let s = new TextDecoder().decode(i);
685
682
  const o = [];
686
- if (typeof i == "string") {
683
+ if (typeof n == "string") {
687
684
  let _;
688
685
  if (e && t)
689
- _ = new RegExp(`${i}([^${i}]+)${i}`, "g");
686
+ _ = new RegExp(`${n}([^${n}]+)${n}`, "g");
690
687
  else if (e)
691
- _ = new RegExp(`${i}([^${i}]*)`, "g");
688
+ _ = new RegExp(`${n}([^${n}]*)`, "g");
692
689
  else if (t)
693
- _ = new RegExp(`([^${i}]+)${i}`, "g");
690
+ _ = new RegExp(`([^${n}]+)${n}`, "g");
694
691
  else
695
692
  return;
696
693
  let d, h = 0;
697
694
  for (; (d = _.exec(s)) !== null; )
698
695
  o.push(new TextEncoder().encode(d[1])), h = _.lastIndex;
699
696
  s = s.slice(h);
700
- } else if (i instanceof RegExp) {
697
+ } else if (n instanceof RegExp) {
701
698
  let _, d = 0;
702
699
  if (e && t) {
703
- const h = new RegExp(`${i.source}(.*?)${i.source}`, "g");
700
+ const h = new RegExp(`${n.source}(.*?)${n.source}`, "g");
704
701
  for (; (_ = h.exec(s)) !== null; )
705
702
  o.push(new TextEncoder().encode(_[1])), d = h.lastIndex;
706
703
  } else if (t)
707
- for (; (_ = i.exec(s)) !== null; ) {
708
- const h = _.index, m = s.slice(d, h);
709
- o.push(new TextEncoder().encode(m)), d = i.lastIndex;
704
+ for (; (_ = n.exec(s)) !== null; ) {
705
+ const h = _.index, w = s.slice(d, h);
706
+ o.push(new TextEncoder().encode(w)), d = n.lastIndex;
710
707
  }
711
708
  else if (e) {
712
- const h = s.split(i);
709
+ const h = s.split(n);
713
710
  h.shift();
714
- for (const m of h)
715
- o.push(new TextEncoder().encode(m));
711
+ for (const w of h)
712
+ o.push(new TextEncoder().encode(w));
716
713
  s = "";
717
714
  }
718
715
  s = s.slice(d);
719
716
  }
720
717
  for (const _ of o)
721
718
  l(this, r, u).call(this, _);
722
- const C = new TextEncoder().encode(s);
723
- this.__internal__.serial.response.buffer = C, C.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
719
+ const E = new TextEncoder().encode(s);
720
+ this.__internal__.serial.response.buffer = E, E.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
724
721
  l(this, r, u).call(this, this.__internal__.serial.response.buffer, !0), this.__internal__.serial.response.buffer = new Uint8Array(0);
725
722
  }, this.__internal__.serial.free_timeout_ms ?? 50));
726
- }, I = async function() {
727
- const i = this.__internal__.serial.port;
728
- if (!i || !i.readable) throw new Error("Port is not readable");
729
- const e = i.readable.getReader();
723
+ }, O = async function() {
724
+ const n = this.__internal__.serial.port;
725
+ if (!n || !n.readable) throw new Error("Port is not readable");
726
+ const e = n.readable.getReader();
730
727
  this.__internal__.serial.reader = e;
731
728
  try {
732
729
  for (; this.__internal__.serial.keep_reading; ) {
733
- const { value: t, done: n } = await e.read();
734
- if (n) break;
735
- l(this, r, E).call(this, t), this.__internal__.serial.response.delimited ? await l(this, r, k).call(this) : this.__internal__.serial.response.length === null ? await l(this, r, S).call(this) : await l(this, r, A).call(this);
730
+ const { value: t, done: i } = await e.read();
731
+ if (i) break;
732
+ l(this, r, L).call(this, t), this.__internal__.serial.response.delimited ? await l(this, r, P).call(this) : this.__internal__.serial.response.length === null ? await l(this, r, D).call(this) : await l(this, r, B).call(this);
736
733
  }
737
734
  } catch (t) {
738
735
  this.serialErrors(t);
739
736
  } finally {
740
737
  e.releaseLock(), this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
741
738
  }
742
- }, f = function(i) {
743
- i !== this.__internal__.serial.connecting && (this.__internal__.serial.connecting = i, i ? this.dispatch("serial:connecting", { active: !0 }) : this.dispatch("serial:connecting", { active: !1 }));
744
- }, M = async function() {
739
+ }, g = function(n) {
740
+ n !== this.__internal__.serial.connecting && (this.__internal__.serial.connecting = n, this.dispatch("serial:connecting", { active: n }), this.dispatch("internal:connecting", { active: n }));
741
+ }, H = async function() {
745
742
  return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
746
- }, q = function() {
743
+ }, j = function() {
747
744
  [
748
745
  "serial:connected",
749
746
  "serial:connecting",
@@ -760,48 +757,48 @@ r = /* @__PURE__ */ new WeakSet(), p = function(i) {
760
757
  "serial:unsupported",
761
758
  "serial:error",
762
759
  "debug"
763
- ].forEach((i) => {
764
- this.serialRegisterAvailableListener(i);
760
+ ].forEach((n) => {
761
+ this.serialRegisterAvailableListener(n);
765
762
  });
766
- }, N = function() {
767
- const i = this;
763
+ }, F = function() {
764
+ const n = this;
768
765
  this.on("internal:queue", async () => {
769
766
  var e;
770
- await l(e = i, r, D).call(e);
771
- }), l(this, r, L).call(this);
772
- }, L = function() {
773
- const i = this;
767
+ await l(e = n, r, $).call(e);
768
+ }), l(this, r, U).call(this);
769
+ }, U = function() {
770
+ const n = this;
774
771
  navigator.serial.addEventListener("connect", async () => {
775
- i.isDisconnected && await i.serialConnect().catch(() => {
772
+ n.isDisconnected && await n.serialConnect().catch(() => {
776
773
  });
777
774
  });
778
- }, D = async function() {
779
- if (!l(this, r, p).call(this, this.__internal__.serial.port)) {
780
- l(this, r, y).call(this, { error: "Port is closed, not readable or writable." }), await this.serialConnect();
775
+ }, $ = async function() {
776
+ if (!l(this, r, f).call(this, this.__internal__.serial.port)) {
777
+ l(this, r, m).call(this, { error: "Port is closed, not readable or writable." }), await this.serialConnect();
781
778
  return;
782
779
  }
783
780
  if (this.__internal__.timeout.until_response || this.__internal__.serial.queue.length === 0) return;
784
- const i = this.__internal__.serial.queue[0];
781
+ const n = this.__internal__.serial.queue[0];
785
782
  let e = this.__internal__.time.response_general;
786
- if (i.action === "connect" && (e = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
787
- await this.timeout(i.bytes, i.action);
788
- }, e), this.__internal__.serial.last_action = i.action ?? "unknown", await l(this, r, v).call(this, i.bytes), this.dispatch("serial:sent", {
789
- action: i.action,
790
- bytes: i.bytes
783
+ if (n.action === "connect" && (e = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
784
+ await this.timeout(n.bytes, n.action);
785
+ }, e), this.__internal__.serial.last_action = n.action ?? "unknown", await l(this, r, x).call(this, n.bytes), this.dispatch("serial:sent", {
786
+ action: n.action,
787
+ bytes: n.bytes
791
788
  }), this.__internal__.auto_response) {
792
- let n = new Uint8Array(0);
789
+ let i = new Uint8Array(0);
793
790
  try {
794
- n = this.validateBytes(this.__internal__.serial.auto_response);
791
+ i = this.validateBytes(this.__internal__.serial.auto_response);
795
792
  } catch (s) {
796
793
  this.serialErrors(s);
797
794
  }
798
- l(this, r, u).call(this, n);
795
+ l(this, r, u).call(this, i);
799
796
  }
800
797
  const t = [...this.__internal__.serial.queue];
801
798
  this.__internal__.serial.queue = t.splice(1);
802
- }, O = function(i = 1) {
803
- this.__internal__.device_number = i, !this.__internal__.bypassSerialBytesConnection && (this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(i));
804
- }, H = function() {
799
+ }, W = function(n = 1) {
800
+ this.__internal__.device_number = n, !this.__internal__.bypassSerialBytesConnection && (this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(n));
801
+ }, Q = function() {
805
802
  this.__internal__.last_error = {
806
803
  message: null,
807
804
  action: null,
@@ -810,6 +807,6 @@ r = /* @__PURE__ */ new WeakSet(), p = function(i) {
810
807
  };
811
808
  };
812
809
  export {
813
- z as K,
810
+ K as Z,
814
811
  c as h
815
812
  };