@danidoble/webserial 4.4.5 → 4.4.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.
@@ -17,28 +17,28 @@ const q = { type: "error", data: "parser error" }, te = typeof Blob == "function
17
17
  e("b" + (s || ""));
18
18
  }, t.readAsDataURL(n);
19
19
  };
20
- function Q(n) {
20
+ function j(n) {
21
21
  return n instanceof Uint8Array ? n : n instanceof ArrayBuffer ? new Uint8Array(n) : new Uint8Array(n.buffer, n.byteOffset, n.byteLength);
22
22
  }
23
23
  let P;
24
- function pe(n, e) {
24
+ function de(n, e) {
25
25
  if (te && n.data instanceof Blob)
26
- return n.data.arrayBuffer().then(Q).then(e);
26
+ return n.data.arrayBuffer().then(j).then(e);
27
27
  if (se && (n.data instanceof ArrayBuffer || ne(n.data)))
28
- return e(Q(n.data));
28
+ return e(j(n.data));
29
29
  H(n, !1, (t) => {
30
30
  P || (P = new TextEncoder()), e(P.encode(t));
31
31
  });
32
32
  }
33
- const X = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", C = typeof Uint8Array > "u" ? [] : new Uint8Array(256);
34
- for (let n = 0; n < X.length; n++)
35
- C[X.charCodeAt(n)] = n;
33
+ const Q = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", C = typeof Uint8Array > "u" ? [] : new Uint8Array(256);
34
+ for (let n = 0; n < Q.length; n++)
35
+ C[Q.charCodeAt(n)] = n;
36
36
  const ye = (n) => {
37
- let e = n.length * 0.75, t = n.length, s, i = 0, r, o, l, c;
37
+ let e = n.length * 0.75, t = n.length, s, i = 0, r, o, c, l;
38
38
  n[n.length - 1] === "=" && (e--, n[n.length - 2] === "=" && e--);
39
- const f = new ArrayBuffer(e), u = new Uint8Array(f);
39
+ const f = new ArrayBuffer(e), _ = new Uint8Array(f);
40
40
  for (s = 0; s < t; s += 4)
41
- r = C[n.charCodeAt(s)], o = C[n.charCodeAt(s + 1)], l = C[n.charCodeAt(s + 2)], c = C[n.charCodeAt(s + 3)], u[i++] = r << 2 | o >> 4, u[i++] = (o & 15) << 4 | l >> 2, u[i++] = (l & 3) << 6 | c & 63;
41
+ r = C[n.charCodeAt(s)], o = C[n.charCodeAt(s + 1)], c = C[n.charCodeAt(s + 2)], l = C[n.charCodeAt(s + 3)], _[i++] = r << 2 | o >> 4, _[i++] = (o & 15) << 4 | c >> 2, _[i++] = (c & 3) << 6 | l & 63;
42
42
  return f;
43
43
  }, ge = typeof ArrayBuffer == "function", W = (n, e) => {
44
44
  if (typeof n != "string")
@@ -66,8 +66,8 @@ const ye = (n) => {
66
66
  const t = n.length, s = new Array(t);
67
67
  let i = 0;
68
68
  n.forEach((r, o) => {
69
- H(r, !1, (l) => {
70
- s[o] = l, ++i === t && e(s.join(re));
69
+ H(r, !1, (c) => {
70
+ s[o] = c, ++i === t && e(s.join(re));
71
71
  });
72
72
  });
73
73
  }, be = (n, e) => {
@@ -82,7 +82,7 @@ const ye = (n) => {
82
82
  function ve() {
83
83
  return new TransformStream({
84
84
  transform(n, e) {
85
- pe(n, (t) => {
85
+ de(n, (t) => {
86
86
  const s = t.length;
87
87
  let i;
88
88
  if (s < 126)
@@ -119,35 +119,35 @@ function Ee(n, e) {
119
119
  const t = [];
120
120
  let s = 0, i = -1, r = !1;
121
121
  return new TransformStream({
122
- transform(o, l) {
122
+ transform(o, c) {
123
123
  for (t.push(o); ; ) {
124
124
  if (s === 0) {
125
125
  if (T(t) < 1)
126
126
  break;
127
- const c = A(t, 1);
128
- r = (c[0] & 128) === 128, i = c[0] & 127, i < 126 ? s = 3 : i === 126 ? s = 1 : s = 2;
127
+ const l = A(t, 1);
128
+ r = (l[0] & 128) === 128, i = l[0] & 127, i < 126 ? s = 3 : i === 126 ? s = 1 : s = 2;
129
129
  } else if (s === 1) {
130
130
  if (T(t) < 2)
131
131
  break;
132
- const c = A(t, 2);
133
- i = new DataView(c.buffer, c.byteOffset, c.length).getUint16(0), s = 3;
132
+ const l = A(t, 2);
133
+ i = new DataView(l.buffer, l.byteOffset, l.length).getUint16(0), s = 3;
134
134
  } else if (s === 2) {
135
135
  if (T(t) < 8)
136
136
  break;
137
- const c = A(t, 8), f = new DataView(c.buffer, c.byteOffset, c.length), u = f.getUint32(0);
138
- if (u > Math.pow(2, 21) - 1) {
139
- l.enqueue(q);
137
+ const l = A(t, 8), f = new DataView(l.buffer, l.byteOffset, l.length), _ = f.getUint32(0);
138
+ if (_ > Math.pow(2, 21) - 1) {
139
+ c.enqueue(q);
140
140
  break;
141
141
  }
142
- i = u * Math.pow(2, 32) + f.getUint32(4), s = 3;
142
+ i = _ * Math.pow(2, 32) + f.getUint32(4), s = 3;
143
143
  } else {
144
144
  if (T(t) < i)
145
145
  break;
146
- const c = A(t, i);
147
- l.enqueue(W(r ? c : N.decode(c), e)), s = 0;
146
+ const l = A(t, i);
147
+ c.enqueue(W(r ? l : N.decode(l), e)), s = 0;
148
148
  }
149
149
  if (i === 0 || i > n) {
150
- l.enqueue(q);
150
+ c.enqueue(q);
151
151
  break;
152
152
  }
153
153
  }
@@ -155,24 +155,24 @@ function Ee(n, e) {
155
155
  });
156
156
  }
157
157
  const oe = 4;
158
- function _(n) {
158
+ function u(n) {
159
159
  if (n) return ke(n);
160
160
  }
161
161
  function ke(n) {
162
- for (var e in _.prototype)
163
- n[e] = _.prototype[e];
162
+ for (var e in u.prototype)
163
+ n[e] = u.prototype[e];
164
164
  return n;
165
165
  }
166
- _.prototype.on = _.prototype.addEventListener = function(n, e) {
166
+ u.prototype.on = u.prototype.addEventListener = function(n, e) {
167
167
  return this._callbacks = this._callbacks || {}, (this._callbacks["$" + n] = this._callbacks["$" + n] || []).push(e), this;
168
168
  };
169
- _.prototype.once = function(n, e) {
169
+ u.prototype.once = function(n, e) {
170
170
  function t() {
171
171
  this.off(n, t), e.apply(this, arguments);
172
172
  }
173
173
  return t.fn = e, this.on(n, t), this;
174
174
  };
175
- _.prototype.off = _.prototype.removeListener = _.prototype.removeAllListeners = _.prototype.removeEventListener = function(n, e) {
175
+ u.prototype.off = u.prototype.removeListener = u.prototype.removeAllListeners = u.prototype.removeEventListener = function(n, e) {
176
176
  if (this._callbacks = this._callbacks || {}, arguments.length == 0)
177
177
  return this._callbacks = {}, this;
178
178
  var t = this._callbacks["$" + n];
@@ -186,7 +186,7 @@ _.prototype.off = _.prototype.removeListener = _.prototype.removeAllListeners =
186
186
  }
187
187
  return t.length === 0 && delete this._callbacks["$" + n], this;
188
188
  };
189
- _.prototype.emit = function(n) {
189
+ u.prototype.emit = function(n) {
190
190
  this._callbacks = this._callbacks || {};
191
191
  for (var e = new Array(arguments.length - 1), t = this._callbacks["$" + n], s = 1; s < arguments.length; s++)
192
192
  e[s - 1] = arguments[s];
@@ -197,20 +197,20 @@ _.prototype.emit = function(n) {
197
197
  }
198
198
  return this;
199
199
  };
200
- _.prototype.emitReserved = _.prototype.emit;
201
- _.prototype.listeners = function(n) {
200
+ u.prototype.emitReserved = u.prototype.emit;
201
+ u.prototype.listeners = function(n) {
202
202
  return this._callbacks = this._callbacks || {}, this._callbacks["$" + n] || [];
203
203
  };
204
- _.prototype.hasListeners = function(n) {
204
+ u.prototype.hasListeners = function(n) {
205
205
  return !!this.listeners(n).length;
206
206
  };
207
- const O = typeof Promise == "function" && typeof Promise.resolve == "function" ? (e) => Promise.resolve().then(e) : (e, t) => t(e, 0), p = typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")(), Ce = "arraybuffer";
207
+ const O = typeof Promise == "function" && typeof Promise.resolve == "function" ? (e) => Promise.resolve().then(e) : (e, t) => t(e, 0), d = typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")(), Ce = "arraybuffer";
208
208
  function ae(n, ...e) {
209
209
  return e.reduce((t, s) => (n.hasOwnProperty(s) && (t[s] = n[s]), t), {});
210
210
  }
211
- const Te = p.setTimeout, Ae = p.clearTimeout;
211
+ const Te = d.setTimeout, Ae = d.clearTimeout;
212
212
  function L(n, e) {
213
- e.useNativeTimers ? (n.setTimeoutFn = Te.bind(p), n.clearTimeoutFn = Ae.bind(p)) : (n.setTimeoutFn = p.setTimeout.bind(p), n.clearTimeoutFn = p.clearTimeout.bind(p));
213
+ e.useNativeTimers ? (n.setTimeoutFn = Te.bind(d), n.clearTimeoutFn = Ae.bind(d)) : (n.setTimeoutFn = d.setTimeout.bind(d), n.clearTimeoutFn = d.clearTimeout.bind(d));
214
214
  }
215
215
  const Se = 1.33;
216
216
  function xe(n) {
@@ -244,7 +244,7 @@ class Le extends Error {
244
244
  super(e), this.description = t, this.context = s, this.type = "TransportError";
245
245
  }
246
246
  }
247
- class K extends _ {
247
+ class K extends u {
248
248
  /**
249
249
  * Transport abstract constructor.
250
250
  *
@@ -335,7 +335,7 @@ class K extends _ {
335
335
  return e.indexOf(":") === -1 ? e : "[" + e + "]";
336
336
  }
337
337
  _port() {
338
- return this.opts.port && (this.opts.secure && +(this.opts.port !== 443) || !this.opts.secure && Number(this.opts.port) !== 80) ? ":" + this.opts.port : "";
338
+ return this.opts.port && (this.opts.secure && Number(this.opts.port) !== 443 || !this.opts.secure && Number(this.opts.port) !== 80) ? ":" + this.opts.port : "";
339
339
  }
340
340
  _query(e) {
341
341
  const t = Be(e);
@@ -484,7 +484,7 @@ class De extends Pe {
484
484
  }), this.pollXhr = e;
485
485
  }
486
486
  }
487
- class g extends _ {
487
+ class g extends u {
488
488
  /**
489
489
  * Request constructor
490
490
  *
@@ -585,13 +585,13 @@ g.requestsCount = 0;
585
585
  g.requests = {};
586
586
  if (typeof document < "u") {
587
587
  if (typeof attachEvent == "function")
588
- attachEvent("onunload", j);
588
+ attachEvent("onunload", X);
589
589
  else if (typeof addEventListener == "function") {
590
- const n = "onpagehide" in p ? "pagehide" : "unload";
591
- addEventListener(n, j, !1);
590
+ const n = "onpagehide" in d ? "pagehide" : "unload";
591
+ addEventListener(n, X, !1);
592
592
  }
593
593
  }
594
- function j() {
594
+ function X() {
595
595
  for (let n in g.requests)
596
596
  g.requests.hasOwnProperty(n) && g.requests[n].abort();
597
597
  }
@@ -620,17 +620,17 @@ function he(n) {
620
620
  }
621
621
  if (!e)
622
622
  try {
623
- return new p[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
623
+ return new d[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
624
624
  } catch {
625
625
  }
626
626
  }
627
- const ue = typeof navigator < "u" && typeof navigator.product == "string" && navigator.product.toLowerCase() === "reactnative";
627
+ const _e = typeof navigator < "u" && typeof navigator.product == "string" && navigator.product.toLowerCase() === "reactnative";
628
628
  class Me extends K {
629
629
  get name() {
630
630
  return "websocket";
631
631
  }
632
632
  doOpen() {
633
- const e = this.uri(), t = this.opts.protocols, s = ue ? {} : ae(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");
633
+ const e = this.uri(), t = this.opts.protocols, s = _e ? {} : ae(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");
634
634
  this.opts.extraHeaders && (s.headers = this.opts.extraHeaders);
635
635
  try {
636
636
  this.ws = this.createSocket(e, t, s);
@@ -681,10 +681,10 @@ class Me extends K {
681
681
  return this.opts.timestampRequests && (t[this.opts.timestampParam] = ce()), this.supportsBinary || (t.b64 = 1), this.createUri(e, t);
682
682
  }
683
683
  }
684
- const I = p.WebSocket || p.MozWebSocket;
684
+ const I = d.WebSocket || d.MozWebSocket;
685
685
  class Fe extends Me {
686
686
  createSocket(e, t, s) {
687
- return ue ? new I(e, t, s) : t ? new I(e, t) : new I(e);
687
+ return _e ? new I(e, t, s) : t ? new I(e, t) : new I(e);
688
688
  }
689
689
  doWrite(e, t) {
690
690
  this.ws.send(t);
@@ -709,9 +709,9 @@ class $e extends K {
709
709
  const t = Ee(Number.MAX_SAFE_INTEGER, this.socket.binaryType), s = e.readable.pipeThrough(t).getReader(), i = ve();
710
710
  i.readable.pipeTo(e.writable), this._writer = i.writable.getWriter();
711
711
  const r = () => {
712
- s.read().then(({ done: l, value: c }) => {
713
- l || (this.onPacket(c), r());
714
- }).catch((l) => {
712
+ s.read().then(({ done: c, value: l }) => {
713
+ c || (this.onPacket(l), r());
714
+ }).catch((c) => {
715
715
  });
716
716
  };
717
717
  r();
@@ -780,7 +780,7 @@ const M = typeof addEventListener == "function" && typeof removeEventListener ==
780
780
  M && addEventListener("offline", () => {
781
781
  x.forEach((n) => n());
782
782
  }, !1);
783
- class b extends _ {
783
+ class b extends u {
784
784
  /**
785
785
  * Socket constructor.
786
786
  *
@@ -1070,13 +1070,13 @@ class Ye extends b {
1070
1070
  let t = this.createTransport(e), s = !1;
1071
1071
  b.priorWebsocketSuccess = !1;
1072
1072
  const i = () => {
1073
- s || (t.send([{ type: "ping", data: "probe" }]), t.once("packet", (d) => {
1073
+ s || (t.send([{ type: "ping", data: "probe" }]), t.once("packet", (p) => {
1074
1074
  if (!s)
1075
- if (d.type === "pong" && d.data === "probe") {
1075
+ if (p.type === "pong" && p.data === "probe") {
1076
1076
  if (this.upgrading = !0, this.emitReserved("upgrading", t), !t)
1077
1077
  return;
1078
1078
  b.priorWebsocketSuccess = t.name === "websocket", this.transport.pause(() => {
1079
- s || this.readyState !== "closed" && (u(), this.setTransport(t), t.send([{ type: "upgrade" }]), this.emitReserved("upgrade", t), t = null, this.upgrading = !1, this.flush());
1079
+ s || this.readyState !== "closed" && (_(), this.setTransport(t), t.send([{ type: "upgrade" }]), this.emitReserved("upgrade", t), t = null, this.upgrading = !1, this.flush());
1080
1080
  });
1081
1081
  } else {
1082
1082
  const E = new Error("probe error");
@@ -1085,25 +1085,25 @@ class Ye extends b {
1085
1085
  }));
1086
1086
  };
1087
1087
  function r() {
1088
- s || (s = !0, u(), t.close(), t = null);
1088
+ s || (s = !0, _(), t.close(), t = null);
1089
1089
  }
1090
- const o = (d) => {
1091
- const E = new Error("probe error: " + d);
1090
+ const o = (p) => {
1091
+ const E = new Error("probe error: " + p);
1092
1092
  E.transport = t.name, r(), this.emitReserved("upgradeError", E);
1093
1093
  };
1094
- function l() {
1094
+ function c() {
1095
1095
  o("transport closed");
1096
1096
  }
1097
- function c() {
1097
+ function l() {
1098
1098
  o("socket closed");
1099
1099
  }
1100
- function f(d) {
1101
- t && d.name !== t.name && r();
1100
+ function f(p) {
1101
+ t && p.name !== t.name && r();
1102
1102
  }
1103
- const u = () => {
1104
- t.removeListener("open", i), t.removeListener("error", o), t.removeListener("close", l), this.off("close", c), this.off("upgrading", f);
1103
+ const _ = () => {
1104
+ t.removeListener("open", i), t.removeListener("error", o), t.removeListener("close", c), this.off("close", l), this.off("upgrading", f);
1105
1105
  };
1106
- t.once("open", i), t.once("error", o), t.once("close", l), this.once("close", c), this.once("upgrading", f), this._upgrades.indexOf("webtransport") !== -1 && e !== "webtransport" ? this.setTimeoutFn(() => {
1106
+ t.once("open", i), t.once("error", o), t.once("close", c), this.once("close", l), this.once("upgrading", f), this._upgrades.indexOf("webtransport") !== -1 && e !== "webtransport" ? this.setTimeoutFn(() => {
1107
1107
  s || t.open();
1108
1108
  }, 200) : t.open();
1109
1109
  }
@@ -1129,15 +1129,15 @@ let Je = class extends Ye {
1129
1129
  (!s.transports || s.transports && typeof s.transports[0] == "string") && (s.transports = (s.transports || ["polling", "websocket", "webtransport"]).map((i) => Ve[i]).filter((i) => !!i)), super(e, s);
1130
1130
  }
1131
1131
  };
1132
- function Qe(n, e = "", t) {
1132
+ function je(n, e = "", t) {
1133
1133
  let s = n;
1134
1134
  t = t || typeof location < "u" && location, n == null && (n = t.protocol + "//" + t.host), typeof n == "string" && (n.charAt(0) === "/" && (n.charAt(1) === "/" ? n = t.protocol + n : n = t.host + n), /^(https?|wss?):\/\//.test(n) || (typeof t < "u" ? n = t.protocol + "//" + n : n = "https://" + n), s = U(n)), s.port || (/^(http|ws)$/.test(s.protocol) ? s.port = "80" : /^(http|ws)s$/.test(s.protocol) && (s.port = "443")), s.path = s.path || "/";
1135
1135
  const r = s.host.indexOf(":") !== -1 ? "[" + s.host + "]" : s.host;
1136
1136
  return s.id = s.protocol + "://" + r + ":" + s.port + e, s.href = s.protocol + "://" + r + (t && t.port === s.port ? "" : ":" + s.port), s;
1137
1137
  }
1138
- const Xe = typeof ArrayBuffer == "function", je = (n) => typeof ArrayBuffer.isView == "function" ? ArrayBuffer.isView(n) : n.buffer instanceof ArrayBuffer, _e = Object.prototype.toString, Ge = typeof Blob == "function" || typeof Blob < "u" && _e.call(Blob) === "[object BlobConstructor]", Ze = typeof File == "function" || typeof File < "u" && _e.call(File) === "[object FileConstructor]";
1138
+ const Qe = typeof ArrayBuffer == "function", Xe = (n) => typeof ArrayBuffer.isView == "function" ? ArrayBuffer.isView(n) : n.buffer instanceof ArrayBuffer, ue = Object.prototype.toString, Ge = typeof Blob == "function" || typeof Blob < "u" && ue.call(Blob) === "[object BlobConstructor]", Ze = typeof File == "function" || typeof File < "u" && ue.call(File) === "[object FileConstructor]";
1139
1139
  function z(n) {
1140
- return Xe && (n instanceof ArrayBuffer || je(n)) || Ge && n instanceof Blob || Ze && n instanceof File;
1140
+ return Qe && (n instanceof ArrayBuffer || Xe(n)) || Ge && n instanceof Blob || Ze && n instanceof File;
1141
1141
  }
1142
1142
  function R(n, e) {
1143
1143
  if (!n || typeof n != "object")
@@ -1200,18 +1200,23 @@ function $(n, e) {
1200
1200
  }
1201
1201
  const st = [
1202
1202
  "connect",
1203
+ // used on the client side
1203
1204
  "connect_error",
1205
+ // used on the client side
1204
1206
  "disconnect",
1207
+ // used on both sides
1205
1208
  "disconnecting",
1209
+ // used on the server side
1206
1210
  "newListener",
1211
+ // used by the Node.js EventEmitter
1207
1212
  "removeListener"
1208
1213
  // used by the Node.js EventEmitter
1209
- ], nt = 5;
1214
+ ];
1210
1215
  var h;
1211
1216
  (function(n) {
1212
1217
  n[n.CONNECT = 0] = "CONNECT", n[n.DISCONNECT = 1] = "DISCONNECT", n[n.EVENT = 2] = "EVENT", n[n.ACK = 3] = "ACK", n[n.CONNECT_ERROR = 4] = "CONNECT_ERROR", n[n.BINARY_EVENT = 5] = "BINARY_EVENT", n[n.BINARY_ACK = 6] = "BINARY_ACK";
1213
1218
  })(h || (h = {}));
1214
- class it {
1219
+ class nt {
1215
1220
  /**
1216
1221
  * Encoder constructor
1217
1222
  *
@@ -1251,17 +1256,15 @@ class it {
1251
1256
  return i.unshift(s), i;
1252
1257
  }
1253
1258
  }
1254
- function G(n) {
1255
- return Object.prototype.toString.call(n) === "[object Object]";
1256
- }
1257
- class Y extends _ {
1259
+ class Y extends u {
1258
1260
  /**
1259
1261
  * Decoder constructor
1260
- *
1261
- * @param {function} reviver - custom reviver to pass down to JSON.stringify
1262
1262
  */
1263
1263
  constructor(e) {
1264
- super(), this.reviver = e;
1264
+ super(), this.opts = Object.assign({
1265
+ reviver: void 0,
1266
+ maxAttachments: 10
1267
+ }, typeof e == "function" ? { reviver: e } : e);
1265
1268
  }
1266
1269
  /**
1267
1270
  * Decodes an encoded packet string into packet JSON.
@@ -1275,7 +1278,7 @@ class Y extends _ {
1275
1278
  throw new Error("got plaintext data when reconstructing a packet");
1276
1279
  t = this.decodeString(e);
1277
1280
  const s = t.type === h.BINARY_EVENT;
1278
- s || t.type === h.BINARY_ACK ? (t.type = s ? h.EVENT : h.ACK, this.reconstructor = new rt(t), t.attachments === 0 && super.emitReserved("decoded", t)) : super.emitReserved("decoded", t);
1281
+ s || t.type === h.BINARY_ACK ? (t.type = s ? h.EVENT : h.ACK, this.reconstructor = new it(t), t.attachments === 0 && super.emitReserved("decoded", t)) : super.emitReserved("decoded", t);
1279
1282
  } else if (z(e) || e.base64)
1280
1283
  if (this.reconstructor)
1281
1284
  t = this.reconstructor.takeBinaryData(e), t && (this.reconstructor = null, super.emitReserved("decoded", t));
@@ -1304,7 +1307,12 @@ class Y extends _ {
1304
1307
  const o = e.substring(r, t);
1305
1308
  if (o != Number(o) || e.charAt(t) !== "-")
1306
1309
  throw new Error("Illegal attachments");
1307
- s.attachments = Number(o);
1310
+ const c = Number(o);
1311
+ if (!rt(c) || c < 0)
1312
+ throw new Error("Illegal attachments");
1313
+ if (c > this.opts.maxAttachments)
1314
+ throw new Error("too many attachments");
1315
+ s.attachments = c;
1308
1316
  }
1309
1317
  if (e.charAt(t + 1) === "/") {
1310
1318
  const r = t + 1;
@@ -1338,7 +1346,7 @@ class Y extends _ {
1338
1346
  }
1339
1347
  tryParse(e) {
1340
1348
  try {
1341
- return JSON.parse(e, this.reviver);
1349
+ return JSON.parse(e, this.opts.reviver);
1342
1350
  } catch {
1343
1351
  return !1;
1344
1352
  }
@@ -1366,7 +1374,7 @@ class Y extends _ {
1366
1374
  this.reconstructor && (this.reconstructor.finishedReconstruction(), this.reconstructor = null);
1367
1375
  }
1368
1376
  }
1369
- class rt {
1377
+ class it {
1370
1378
  constructor(e) {
1371
1379
  this.packet = e, this.buffers = [], this.reconPack = e;
1372
1380
  }
@@ -1392,14 +1400,19 @@ class rt {
1392
1400
  this.reconPack = null, this.buffers = [];
1393
1401
  }
1394
1402
  }
1403
+ const rt = Number.isInteger || function(n) {
1404
+ return typeof n == "number" && isFinite(n) && Math.floor(n) === n;
1405
+ };
1406
+ function G(n) {
1407
+ return Object.prototype.toString.call(n) === "[object Object]";
1408
+ }
1395
1409
  const ot = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1396
1410
  __proto__: null,
1397
1411
  Decoder: Y,
1398
- Encoder: it,
1412
+ Encoder: nt,
1399
1413
  get PacketType() {
1400
1414
  return h;
1401
- },
1402
- protocol: nt
1415
+ }
1403
1416
  }, Symbol.toStringTag, { value: "Module" }));
1404
1417
  function y(n, e, t) {
1405
1418
  return n.on(e, t), function() {
@@ -1415,7 +1428,7 @@ const at = Object.freeze({
1415
1428
  newListener: 1,
1416
1429
  removeListener: 1
1417
1430
  });
1418
- class fe extends _ {
1431
+ class fe extends u {
1419
1432
  /**
1420
1433
  * `Socket` constructor.
1421
1434
  */
@@ -1540,11 +1553,11 @@ class fe extends _ {
1540
1553
  data: t
1541
1554
  };
1542
1555
  if (o.options = {}, o.options.compress = this.flags.compress !== !1, typeof t[t.length - 1] == "function") {
1543
- const u = this.ids++, d = t.pop();
1544
- this._registerAckCallback(u, d), o.id = u;
1556
+ const _ = this.ids++, p = t.pop();
1557
+ this._registerAckCallback(_, p), o.id = _;
1545
1558
  }
1546
- const l = (i = (s = this.io.engine) === null || s === void 0 ? void 0 : s.transport) === null || i === void 0 ? void 0 : i.writable, c = this.connected && !(!((r = this.io.engine) === null || r === void 0) && r._hasPingExpired());
1547
- return this.flags.volatile && !l || (c ? (this.notifyOutgoingListeners(o), this.packet(o)) : this.sendBuffer.push(o)), this.flags = {}, this;
1559
+ const c = (i = (s = this.io.engine) === null || s === void 0 ? void 0 : s.transport) === null || i === void 0 ? void 0 : i.writable, l = this.connected && !(!((r = this.io.engine) === null || r === void 0) && r._hasPingExpired());
1560
+ return this.flags.volatile && !c || (l ? (this.notifyOutgoingListeners(o), this.packet(o)) : this.sendBuffer.push(o)), this.flags = {}, this;
1548
1561
  }
1549
1562
  /**
1550
1563
  * @private
@@ -1558,11 +1571,11 @@ class fe extends _ {
1558
1571
  }
1559
1572
  const r = this.io.setTimeoutFn(() => {
1560
1573
  delete this.acks[e];
1561
- for (let l = 0; l < this.sendBuffer.length; l++)
1562
- this.sendBuffer[l].id === e && this.sendBuffer.splice(l, 1);
1574
+ for (let c = 0; c < this.sendBuffer.length; c++)
1575
+ this.sendBuffer[c].id === e && this.sendBuffer.splice(c, 1);
1563
1576
  t.call(this, new Error("operation has timed out"));
1564
- }, i), o = (...l) => {
1565
- this.io.clearTimeoutFn(r), t.apply(this, l);
1577
+ }, i), o = (...c) => {
1578
+ this.io.clearTimeoutFn(r), t.apply(this, c);
1566
1579
  };
1567
1580
  o.withError = !0, this.acks[e] = o;
1568
1581
  }
@@ -1584,7 +1597,7 @@ class fe extends _ {
1584
1597
  */
1585
1598
  emitWithAck(e, ...t) {
1586
1599
  return new Promise((s, i) => {
1587
- const r = (o, l) => o ? i(o) : s(l);
1600
+ const r = (o, c) => o ? i(o) : s(c);
1588
1601
  r.withError = !0, t.push(r), this.emit(e, ...t);
1589
1602
  });
1590
1603
  }
@@ -1603,7 +1616,7 @@ class fe extends _ {
1603
1616
  args: e,
1604
1617
  flags: Object.assign({ fromQueue: !0 }, this.flags)
1605
1618
  };
1606
- e.push((i, ...r) => s !== this._queue[0] ? void 0 : (i !== null ? s.tryCount > this._opts.retries && (this._queue.shift(), t && t(i)) : (this._queue.shift(), t && t(null, ...r)), s.pending = !1, this._drainQueue())), this._queue.push(s), this._drainQueue();
1619
+ e.push((i, ...r) => (this._queue[0], i !== null ? s.tryCount > this._opts.retries && (this._queue.shift(), t && t(i)) : (this._queue.shift(), t && t(null, ...r)), s.pending = !1, this._drainQueue())), this._queue.push(s), this._drainQueue();
1607
1620
  }
1608
1621
  /**
1609
1622
  * Send the first packet of the queue, and wait for an acknowledgement from the server.
@@ -1761,7 +1774,7 @@ class fe extends _ {
1761
1774
  * @private
1762
1775
  */
1763
1776
  onconnect(e, t) {
1764
- this.id = e, this.recovered = t && this._pid === t, this._pid = t, this.connected = !0, this.emitBuffered(), this.emitReserved("connect"), this._drainQueue(!0);
1777
+ this.id = e, this.recovered = t && this._pid === t, this._pid = t, this.connected = !0, this.emitBuffered(), this._drainQueue(!0), this.emitReserved("connect");
1765
1778
  }
1766
1779
  /**
1767
1780
  * Emit buffered events (received and emitted).
@@ -2030,7 +2043,7 @@ v.prototype.setMax = function(n) {
2030
2043
  v.prototype.setJitter = function(n) {
2031
2044
  this.jitter = n;
2032
2045
  };
2033
- class V extends _ {
2046
+ class V extends u {
2034
2047
  constructor(e, t) {
2035
2048
  var s;
2036
2049
  super(), this.nsps = {}, this.subs = [], e && typeof e == "object" && (t = e, e = void 0), t = t || {}, t.path = t.path || "/socket.io", this.opts = t, L(this, t), this.reconnection(t.reconnection !== !1), this.reconnectionAttempts(t.reconnectionAttempts || 1 / 0), this.reconnectionDelay(t.reconnectionDelay || 1e3), this.reconnectionDelayMax(t.reconnectionDelayMax || 5e3), this.randomizationFactor((s = t.randomizationFactor) !== null && s !== void 0 ? s : 0.5), this.backoff = new v({
@@ -2086,15 +2099,15 @@ class V extends _ {
2086
2099
  this._readyState = "opening", this.skipReconnect = !1;
2087
2100
  const i = y(t, "open", function() {
2088
2101
  s.onopen(), e && e();
2089
- }), r = (l) => {
2090
- this.cleanup(), this._readyState = "closed", this.emitReserved("error", l), e ? e(l) : this.maybeReconnectOnOpen();
2102
+ }), r = (c) => {
2103
+ this.cleanup(), this._readyState = "closed", this.emitReserved("error", c), e ? e(c) : this.maybeReconnectOnOpen();
2091
2104
  }, o = y(t, "error", r);
2092
2105
  if (this._timeout !== !1) {
2093
- const l = this._timeout, c = this.setTimeoutFn(() => {
2106
+ const c = this._timeout, l = this.setTimeoutFn(() => {
2094
2107
  i(), r(new Error("timeout")), t.close();
2095
- }, l);
2096
- this.opts.autoUnref && c.unref(), this.subs.push(() => {
2097
- this.clearTimeoutFn(c);
2108
+ }, c);
2109
+ this.opts.autoUnref && l.unref(), this.subs.push(() => {
2110
+ this.clearTimeoutFn(l);
2098
2111
  });
2099
2112
  }
2100
2113
  return this.subs.push(i), this.subs.push(o), this;
@@ -2271,9 +2284,9 @@ class V extends _ {
2271
2284
  const k = {};
2272
2285
  function B(n, e) {
2273
2286
  typeof n == "object" && (e = n, n = void 0), e = e || {};
2274
- const t = Qe(n, e.path || "/socket.io"), s = t.source, i = t.id, r = t.path, o = k[i] && r in k[i].nsps, l = e.forceNew || e["force new connection"] || e.multiplex === !1 || o;
2275
- let c;
2276
- return l ? c = new V(s, e) : (k[i] || (k[i] = new V(s, e)), c = k[i]), t.query && !e.query && (e.query = t.queryKey), c.socket(t.path, e);
2287
+ const t = je(n, e.path || "/socket.io"), s = t.source, i = t.id, r = t.path, o = k[i] && r in k[i].nsps, c = e.forceNew || e["force new connection"] || e.multiplex === !1 || o;
2288
+ let l;
2289
+ return c ? l = new V(s, e) : (k[i] || (k[i] = new V(s, e)), l = k[i]), t.query && !e.query && (e.query = t.queryKey), l.socket(t.path, e);
2277
2290
  }
2278
2291
  Object.assign(B, {
2279
2292
  Manager: V,
@@ -2286,7 +2299,7 @@ class Z extends CustomEvent {
2286
2299
  super(e, t);
2287
2300
  }
2288
2301
  }
2289
- class de extends EventTarget {
2302
+ class pe extends EventTarget {
2290
2303
  __listeners__ = {
2291
2304
  debug: !1
2292
2305
  };
@@ -2387,7 +2400,7 @@ class de extends EventTarget {
2387
2400
  this.__listeners__[e] = !1;
2388
2401
  }
2389
2402
  }
2390
- class a extends de {
2403
+ class a extends pe {
2391
2404
  static instance;
2392
2405
  static devices = {};
2393
2406
  constructor() {
@@ -2495,16 +2508,16 @@ function ee(n = 100) {
2495
2508
  );
2496
2509
  }
2497
2510
  class ct {
2498
- #s = "http://localhost:3000";
2499
- #n = {
2511
+ #n = "http://localhost:3001";
2512
+ #t = {
2500
2513
  transports: ["websocket"]
2501
2514
  };
2502
2515
  #e = null;
2503
2516
  #i = !1;
2504
- #a = !1;
2505
- #t;
2506
- constructor() {
2507
- this.#t = {
2517
+ #o = !1;
2518
+ #s;
2519
+ constructor(e, t) {
2520
+ e && (this.#n = e), t && (this.#t = { ...this.#t, ...t }), this.#s = {
2508
2521
  onResponse: this.onResponse.bind(this),
2509
2522
  onDisconnect: () => {
2510
2523
  this.#i = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
@@ -2512,8 +2525,8 @@ class ct {
2512
2525
  onConnect: () => {
2513
2526
  this.#i = !0, window.dispatchEvent(new Event("serial:socket:connected"));
2514
2527
  },
2515
- onConnectError: (e) => {
2516
- console.debug("Socket connection error", e), this.#i = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
2528
+ onConnectError: (s) => {
2529
+ console.debug("Socket connection error", s), this.#i = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
2517
2530
  }
2518
2531
  };
2519
2532
  }
@@ -2521,27 +2534,32 @@ class ct {
2521
2534
  const t = new URL(e);
2522
2535
  if (!["http:", "https:", "ws:", "wss:"].includes(t.protocol))
2523
2536
  throw new Error("URI must start with http://, https://, ws://, or wss://");
2524
- this.#s = e;
2537
+ this.#n = e;
2525
2538
  }
2526
2539
  get uri() {
2527
- return this.#s;
2540
+ return this.#n;
2528
2541
  }
2529
2542
  set options(e) {
2530
2543
  if (typeof e != "object")
2531
2544
  throw new Error("Options must be an object");
2532
- this.#n = e;
2545
+ this.#t = e;
2533
2546
  }
2534
2547
  get options() {
2535
- return this.#n;
2548
+ return this.#t;
2536
2549
  }
2537
2550
  get socketId() {
2538
2551
  return this.#e && this.#e.id ? this.#e.id : null;
2539
2552
  }
2553
+ configure(e, t) {
2554
+ if (this.#o)
2555
+ throw new Error("Cannot configure socket after it has been initialized. Call configure() before prepare().");
2556
+ e && (this.uri = e), t && (this.#t = { ...this.#t, ...t });
2557
+ }
2540
2558
  disconnect() {
2541
- this.#e && (this.#e.off("response", this.#t.onResponse), this.#e.off("disconnect", this.#t.onDisconnect), this.#e.off("connect", this.#t.onConnect), this.#e.off("connect_error", this.#t.onConnectError), this.#e.disconnect(), this.#e = null, this.#a = !1), this.#i = !1;
2559
+ this.#e && (this.#e.off("response", this.#s.onResponse), this.#e.off("disconnect", this.#s.onDisconnect), this.#e.off("connect", this.#s.onConnect), this.#e.off("connect_error", this.#s.onConnectError), this.#e.disconnect(), this.#e = null, this.#o = !1), this.#i = !1;
2542
2560
  }
2543
2561
  prepare() {
2544
- this.#i || this.#a || (this.#e = B(this.#s, this.#n), this.#a = !0, this.#e.on("disconnect", this.#t.onDisconnect), this.#e.on("response", this.#t.onResponse), this.#e.on("connect", this.#t.onConnect), this.#e.on("connect_error", this.#t.onConnectError));
2562
+ this.#i || this.#o || (this.#e = B(this.#n, this.#t), this.#o = !0, this.#e.on("disconnect", this.#s.onDisconnect), this.#e.on("response", this.#s.onResponse), this.#e.on("connect", this.#s.onConnect), this.#e.on("connect_error", this.#s.onConnectError));
2545
2563
  }
2546
2564
  connectDevice(e) {
2547
2565
  if (!this.#e)
@@ -2582,7 +2600,7 @@ const w = new ct(), D = {
2582
2600
  bufferSize: 32768,
2583
2601
  flowControl: "none"
2584
2602
  };
2585
- class ht extends de {
2603
+ class ht extends pe {
2586
2604
  __internal__ = {
2587
2605
  bypassSerialBytesConnection: !1,
2588
2606
  auto_response: !1,
@@ -2595,6 +2613,7 @@ class ht extends de {
2595
2613
  no_code: 0
2596
2614
  },
2597
2615
  serial: {
2616
+ transformStream: !1,
2598
2617
  socket: !1,
2599
2618
  portInfo: {
2600
2619
  path: null,
@@ -2636,8 +2655,11 @@ class ht extends de {
2636
2655
  auto_response: null,
2637
2656
  free_timeout_ms: 50,
2638
2657
  // In previous versions 400 was used
2639
- useRTSCTS: !1
2658
+ useRTSCTS: !1,
2640
2659
  // Use RTS/CTS flow control
2660
+ pipeAbortController: null,
2661
+ pipeDone: null,
2662
+ pumpReader: null
2641
2663
  },
2642
2664
  device: {
2643
2665
  type: "unknown",
@@ -2656,25 +2678,34 @@ class ht extends de {
2656
2678
  reconnection: 0
2657
2679
  }
2658
2680
  };
2659
- #s = null;
2681
+ #n = null;
2660
2682
  constructor({
2661
2683
  filters: e = null,
2662
2684
  config_port: t = D,
2663
2685
  no_device: s = 1,
2664
2686
  device_listen_on_channel: i = 1,
2665
2687
  bypassSerialBytesConnection: r = !1,
2666
- socket: o = !1
2688
+ socket: o = !1,
2689
+ transformStream: c = !1
2667
2690
  } = {
2668
2691
  filters: null,
2669
2692
  config_port: D,
2670
2693
  no_device: 1,
2671
2694
  device_listen_on_channel: 1,
2672
2695
  bypassSerialBytesConnection: !1,
2673
- socket: !1
2696
+ socket: !1,
2697
+ transformStream: !1
2674
2698
  }) {
2675
2699
  if (super(), !("serial" in navigator))
2676
2700
  throw new Error("Web Serial not supported");
2677
- e && (this.serialFilters = e), t && (this.serialConfigPort = t), r && (this.__internal__.bypassSerialBytesConnection = r), s && this.#b(s), i && ["number", "string"].includes(typeof i) && (this.listenOnChannel = i), this.__internal__.serial.socket = o, this.#y(), this.#g();
2701
+ if (e && (this.serialFilters = e), t && (this.serialConfigPort = t), r && (this.__internal__.bypassSerialBytesConnection = r), s && this.#v(s), i && ["number", "string"].includes(typeof i) && (this.listenOnChannel = i), this.__internal__.serial.socket = o, typeof c == "function")
2702
+ this.__internal__.serial.transformStream = c;
2703
+ else if (c) {
2704
+ const l = c;
2705
+ this.__internal__.serial.transformStream = () => l;
2706
+ } else
2707
+ this.__internal__.serial.transformStream = !1;
2708
+ this.#g(), this.#m();
2678
2709
  }
2679
2710
  set listenOnChannel(e) {
2680
2711
  if (typeof e == "string" && (e = parseInt(e)), isNaN(e) || e < 1 || e > 255)
@@ -2708,15 +2739,15 @@ class ht extends de {
2708
2739
  this.__internal__.serial.useRTSCTS = e;
2709
2740
  }
2710
2741
  get isConnected() {
2711
- const e = this.__internal__.serial.connected, t = this.#n(this.__internal__.serial.port);
2742
+ const e = this.__internal__.serial.connected, t = this.#t(this.__internal__.serial.port);
2712
2743
  return e && !t && this.#e({ error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
2713
2744
  }
2714
2745
  get isConnecting() {
2715
2746
  return this.__internal__.serial.connecting;
2716
2747
  }
2717
2748
  get isDisconnected() {
2718
- const e = this.__internal__.serial.connected, t = this.#n(this.__internal__.serial.port);
2719
- return !e && t && (this.dispatch("serial:connected"), this.#o(!1), a.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
2749
+ const e = this.__internal__.serial.connected, t = this.#t(this.__internal__.serial.port);
2750
+ return !e && t && (this.dispatch("serial:connected"), this.#a(!1), a.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
2720
2751
  }
2721
2752
  get deviceNumber() {
2722
2753
  return this.__internal__.device_number;
@@ -2889,7 +2920,7 @@ class ht extends de {
2889
2920
  }
2890
2921
  };
2891
2922
  }
2892
- #n(e) {
2923
+ #t(e) {
2893
2924
  return this.useSocket ? this.__internal__.serial.connected && w.isConnected() : !!(e && e.readable && e.writable);
2894
2925
  }
2895
2926
  async timeout(e, t) {
@@ -2910,7 +2941,7 @@ class ht extends de {
2910
2941
  }
2911
2942
  socketResponse(e) {
2912
2943
  const t = this.__internal__.serial.connected;
2913
- if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), a.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#o(!1)), e.type === "success")
2944
+ if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), a.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#a(!1)), e.type === "success")
2914
2945
  this.#r(new Uint8Array(e.data));
2915
2946
  else if (e.type === "error") {
2916
2947
  const s = new Error("The port is closed or is not readable/writable");
@@ -2920,9 +2951,9 @@ class ht extends de {
2920
2951
  }
2921
2952
  async connect() {
2922
2953
  return this.isConnected ? !0 : (this.__internal__.serial.aux_connecting = "idle", new Promise((e, t) => {
2923
- this.#s || (this.#s = this.#i.bind(this)), this.on("internal:connecting", this.#s);
2954
+ this.#n || (this.#n = this.#i.bind(this)), this.on("internal:connecting", this.#n);
2924
2955
  const s = setInterval(() => {
2925
- this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(s), this.__internal__.serial.aux_connecting = "idle", this.#s !== null && this.off("internal:connecting", this.#s), 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 }));
2956
+ this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(s), this.__internal__.serial.aux_connecting = "idle", this.#n !== null && this.off("internal:connecting", this.#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("internal:connecting", { active: !0 }), this.dispatch("serial:connecting", { active: !0 }));
2926
2957
  }, 100);
2927
2958
  this.serialConnect();
2928
2959
  }));
@@ -2932,16 +2963,30 @@ class ht extends de {
2932
2963
  if (this.useSocket)
2933
2964
  w.isConnected() && w.disconnectDevice(this.configDeviceSocket);
2934
2965
  else {
2935
- const e = this.__internal__.serial.reader, t = this.__internal__.serial.output_stream;
2936
- 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();
2966
+ this.__internal__.serial.keep_reading = !1;
2967
+ const e = this.__internal__.serial.reader;
2968
+ e && await e.cancel().catch(() => {
2969
+ }), this.__internal__.serial.pumpReader && await this.__internal__.serial.pumpReader.cancel().catch(() => {
2970
+ }), this.__internal__.serial.pipeDone && (await this.__internal__.serial.pipeDone.catch(() => {
2971
+ }), this.__internal__.serial.pipeDone = null), this.__internal__.serial.input_done && await this.__internal__.serial.input_done.catch(() => {
2972
+ });
2973
+ const t = this.__internal__.serial.output_stream;
2974
+ if (t) {
2975
+ const s = t.getWriter();
2976
+ await s.close().catch(() => {
2977
+ }), s.releaseLock(), this.__internal__.serial.output_done && await this.__internal__.serial.output_done.catch(() => {
2978
+ });
2979
+ }
2980
+ this.__internal__.serial.port && await this.__internal__.serial.port.close().catch(() => {
2981
+ });
2937
2982
  }
2938
2983
  } catch (e) {
2939
- this.serialErrors(e);
2984
+ console.error(e), this.serialErrors(e);
2940
2985
  } finally {
2941
2986
  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, a.$dispatchChange(this);
2942
2987
  }
2943
2988
  }
2944
- async #a(e) {
2989
+ async #o(e) {
2945
2990
  if (w.isDisconnected())
2946
2991
  throw this.#e({ error: "Socket is disconnected." }), new Error("The socket is disconnected");
2947
2992
  if (this.isDisconnected)
@@ -2949,9 +2994,9 @@ class ht extends de {
2949
2994
  const t = this.validateBytes(e);
2950
2995
  w.write({ config: this.configDeviceSocket, bytes: Array.from(t) });
2951
2996
  }
2952
- async #t(e) {
2997
+ async #s(e) {
2953
2998
  if (this.useSocket) {
2954
- await this.#a(e);
2999
+ await this.#o(e);
2955
3000
  return;
2956
3001
  }
2957
3002
  const t = this.__internal__.serial.port;
@@ -2975,7 +3020,7 @@ class ht extends de {
2975
3020
  #r(e = new Uint8Array([]), t = !1) {
2976
3021
  if (e && e.length > 0) {
2977
3022
  const s = this.__internal__.serial.connected;
2978
- if (this.__internal__.serial.connected = this.#n(this.__internal__.serial.port), a.$dispatchChange(this), !s && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#o(!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")
3023
+ if (this.__internal__.serial.connected = this.#t(this.__internal__.serial.port), a.$dispatchChange(this), !s && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#a(!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")
2979
3024
  t ? this.serialCorruptMessage(this.parseUint8ToHex(e)) : this.serialMessage(this.parseUint8ToHex(e));
2980
3025
  else if (this.__internal__.serial.response.as === "uint8")
2981
3026
  t ? this.serialCorruptMessage(e) : this.serialMessage(e);
@@ -3015,7 +3060,7 @@ class ht extends de {
3015
3060
  return e.length === 0 ? t : t.filter((s) => {
3016
3061
  const i = s.getInfo();
3017
3062
  return e.some((r) => i.usbProductId === r.usbProductId && i.usbVendorId === r.usbVendorId);
3018
- }).filter((s) => !this.#n(s));
3063
+ }).filter((s) => !this.#t(s));
3019
3064
  }
3020
3065
  async serialPortsSaved(e) {
3021
3066
  const t = this.serialFilters;
@@ -3082,12 +3127,15 @@ class ht extends de {
3082
3127
  s.set(t, 0), s.set(new Uint8Array(e), t.length), this.__internal__.serial.response.buffer = s;
3083
3128
  }
3084
3129
  }
3130
+ async #_() {
3131
+ 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.response.buffer && this.#r(this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
3132
+ }
3085
3133
  async #u() {
3086
3134
  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(() => {
3087
3135
  this.__internal__.serial.response.buffer && this.#r(this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
3088
3136
  }, this.__internal__.serial.free_timeout_ms || 50);
3089
3137
  }
3090
- async #_() {
3138
+ async #f() {
3091
3139
  const e = this.__internal__.serial.response.length;
3092
3140
  let t = this.__internal__.serial.response.buffer;
3093
3141
  if (this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), !(e === null || !t || t.length === 0)) {
@@ -3100,7 +3148,7 @@ class ht extends de {
3100
3148
  }, this.__internal__.serial.free_timeout_ms || 50));
3101
3149
  }
3102
3150
  }
3103
- async #f() {
3151
+ async #p() {
3104
3152
  const {
3105
3153
  limiter: e,
3106
3154
  prefixLimiter: t = !1,
@@ -3114,69 +3162,96 @@ class ht extends de {
3114
3162
  let r = new TextDecoder().decode(i);
3115
3163
  const o = [];
3116
3164
  if (typeof e == "string") {
3117
- let c;
3165
+ let l;
3118
3166
  if (t && s)
3119
- c = new RegExp(`${e}([^${e}]+)${e}`, "g");
3167
+ l = new RegExp(`${e}([^${e}]+)${e}`, "g");
3120
3168
  else if (t)
3121
- c = new RegExp(`${e}([^${e}]*)`, "g");
3169
+ l = new RegExp(`${e}([^${e}]*)`, "g");
3122
3170
  else if (s)
3123
- c = new RegExp(`([^${e}]+)${e}`, "g");
3171
+ l = new RegExp(`([^${e}]+)${e}`, "g");
3124
3172
  else
3125
3173
  return;
3126
- let f, u = 0;
3127
- for (; (f = c.exec(r)) !== null; )
3128
- o.push(new TextEncoder().encode(f[1])), u = c.lastIndex;
3129
- r = r.slice(u);
3174
+ let f, _ = 0;
3175
+ for (; (f = l.exec(r)) !== null; )
3176
+ o.push(new TextEncoder().encode(f[1])), _ = l.lastIndex;
3177
+ r = r.slice(_);
3130
3178
  } else if (e instanceof RegExp) {
3131
- let c, f = 0;
3179
+ let l, f = 0;
3132
3180
  if (t && s) {
3133
- const u = new RegExp(`${e.source}(.*?)${e.source}`, "g");
3134
- for (; (c = u.exec(r)) !== null; )
3135
- o.push(new TextEncoder().encode(c[1])), f = u.lastIndex;
3181
+ const _ = new RegExp(`${e.source}(.*?)${e.source}`, "g");
3182
+ for (; (l = _.exec(r)) !== null; )
3183
+ o.push(new TextEncoder().encode(l[1])), f = _.lastIndex;
3136
3184
  } else if (s)
3137
- for (; (c = e.exec(r)) !== null; ) {
3138
- const u = c.index, d = r.slice(f, u);
3139
- o.push(new TextEncoder().encode(d)), f = e.lastIndex;
3185
+ for (; (l = e.exec(r)) !== null; ) {
3186
+ const _ = l.index, p = r.slice(f, _);
3187
+ o.push(new TextEncoder().encode(p)), f = e.lastIndex;
3140
3188
  }
3141
3189
  else if (t) {
3142
- const u = r.split(e);
3143
- u.shift();
3144
- for (const d of u)
3145
- o.push(new TextEncoder().encode(d));
3190
+ const _ = r.split(e);
3191
+ _.shift();
3192
+ for (const p of _)
3193
+ o.push(new TextEncoder().encode(p));
3146
3194
  r = "";
3147
3195
  }
3148
3196
  r = r.slice(f);
3149
3197
  }
3150
- for (const c of o)
3151
- this.#r(c);
3152
- const l = new TextEncoder().encode(r);
3153
- this.__internal__.serial.response.buffer = l, l.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
3198
+ for (const l of o)
3199
+ this.#r(l);
3200
+ const c = new TextEncoder().encode(r);
3201
+ this.__internal__.serial.response.buffer = c, c.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
3154
3202
  this.#r(this.__internal__.serial.response.buffer, !0), this.__internal__.serial.response.buffer = new Uint8Array(0);
3155
3203
  }, this.__internal__.serial.free_timeout_ms ?? 50));
3156
3204
  }
3157
3205
  async #d() {
3158
3206
  const e = this.__internal__.serial.port;
3159
3207
  if (!e || !e.readable) throw new Error("Port is not readable");
3160
- const t = e.readable.getReader();
3161
- this.__internal__.serial.reader = t;
3208
+ const t = this.__internal__.serial.transformStream;
3209
+ let s;
3210
+ if (t) {
3211
+ const i = t(), r = e.readable.getReader();
3212
+ this.__internal__.serial.pumpReader = r;
3213
+ const o = i.writable.getWriter();
3214
+ this.__internal__.serial.pipeDone = (async () => {
3215
+ try {
3216
+ for (; ; ) {
3217
+ const { value: c, done: l } = await r.read();
3218
+ if (l) break;
3219
+ await o.write(c);
3220
+ }
3221
+ } catch {
3222
+ } finally {
3223
+ try {
3224
+ r.releaseLock();
3225
+ } catch {
3226
+ }
3227
+ try {
3228
+ o.releaseLock();
3229
+ } catch {
3230
+ }
3231
+ this.__internal__.serial.pumpReader = null;
3232
+ }
3233
+ })(), s = i.readable.getReader();
3234
+ } else
3235
+ s = e.readable.getReader();
3236
+ this.__internal__.serial.reader = s;
3162
3237
  try {
3163
3238
  for (; this.__internal__.serial.keep_reading; ) {
3164
- const { value: s, done: i } = await t.read();
3165
- if (i) break;
3166
- this.#h(s), this.__internal__.serial.response.delimited ? await this.#f() : this.__internal__.serial.response.length === null ? await this.#u() : await this.#_();
3239
+ const { value: i, done: r } = await s.read();
3240
+ if (r) break;
3241
+ this.#h(i), t ? await this.#_() : this.__internal__.serial.response.delimited ? await this.#p() : this.__internal__.serial.response.length === null ? await this.#u() : await this.#f();
3167
3242
  }
3168
- } catch (s) {
3169
- this.serialErrors(s);
3243
+ } catch (i) {
3244
+ this.serialErrors(i);
3170
3245
  } finally {
3171
- t.releaseLock(), this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
3246
+ s.releaseLock(), this.__internal__.serial.reader = null, this.__internal__.serial.keep_reading = !0;
3172
3247
  }
3173
3248
  }
3174
- #o(e) {
3249
+ #a(e) {
3175
3250
  e !== this.__internal__.serial.connecting && (this.__internal__.serial.connecting = e, this.dispatch("serial:connecting", { active: e }), this.dispatch("internal:connecting", { active: e }));
3176
3251
  }
3177
3252
  async serialConnect() {
3178
3253
  try {
3179
- if (this.#o(!0), this.useSocket) {
3254
+ if (this.#a(!0), this.useSocket) {
3180
3255
  if (w.prepare(), this.__internal__.serial.last_action = "connect", this.__internal__.timeout.until_response = setTimeout(async () => {
3181
3256
  await this.timeout(this.__internal__.serial.bytes_connection ?? [], "connection:start");
3182
3257
  }, this.__internal__.time.response_connection), w.isDisconnected())
@@ -3201,25 +3276,25 @@ class ht extends de {
3201
3276
  await t.open(this.serialConfigPort);
3202
3277
  const s = this;
3203
3278
  t.onconnect = (i) => {
3204
- s.dispatch("serial:connected", i), s.#o(!1), a.$dispatchChange(this), s.__internal__.serial.queue.length > 0 ? s.dispatch("internal:queue", {}) : s.__internal__.serial.running_queue = !1;
3279
+ s.dispatch("serial:connected", i), s.#a(!1), a.$dispatchChange(this), s.__internal__.serial.queue.length > 0 ? s.dispatch("internal:queue", {}) : s.__internal__.serial.running_queue = !1;
3205
3280
  }, t.ondisconnect = async () => {
3206
3281
  await s.disconnect();
3207
3282
  }, await ee(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
3208
3283
  await s.timeout(s.__internal__.serial.bytes_connection ?? [], "connection:start");
3209
- }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#t(this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
3284
+ }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#s(this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
3210
3285
  action: "connect",
3211
3286
  bytes: this.__internal__.serial.bytes_connection
3212
- }), this.__internal__.auto_response && this.#r(this.__internal__.serial.auto_response), await this.#d();
3287
+ }), this.__internal__.auto_response && this.#r(this.__internal__.serial.auto_response), this.__internal__.serial.input_done = this.#d(), await this.__internal__.serial.input_done;
3213
3288
  }
3214
3289
  } catch (e) {
3215
- this.#o(!1), this.serialErrors(e);
3290
+ this.#a(!1), this.serialErrors(e);
3216
3291
  }
3217
3292
  }
3218
- async #p() {
3293
+ async #y() {
3219
3294
  return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
3220
3295
  }
3221
3296
  async serialForget() {
3222
- return await this.#p();
3297
+ return await this.#y();
3223
3298
  }
3224
3299
  decToHex(e) {
3225
3300
  return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
@@ -3239,7 +3314,7 @@ class ht extends de {
3239
3314
  bytesToHex(e) {
3240
3315
  return this.add0x(Array.from(e, (t) => this.hexMaker(t)));
3241
3316
  }
3242
- #y() {
3317
+ #g() {
3243
3318
  [
3244
3319
  "serial:connected",
3245
3320
  "serial:connecting",
@@ -3260,10 +3335,10 @@ class ht extends de {
3260
3335
  this.serialRegisterAvailableListener(e);
3261
3336
  });
3262
3337
  }
3263
- #g() {
3338
+ #m() {
3264
3339
  const e = this;
3265
3340
  this.on("internal:queue", async () => {
3266
- await e.#w();
3341
+ await e.#b();
3267
3342
  });
3268
3343
  const t = () => {
3269
3344
  e.isConnected && e.#e({ error: "Socket disconnected." });
@@ -3271,19 +3346,19 @@ class ht extends de {
3271
3346
  e.isDisconnected && !e.isConnecting && e.serialConnect().catch(() => {
3272
3347
  });
3273
3348
  };
3274
- this.useSocket && (window.addEventListener("serial:socket:disconnected", t), window.addEventListener("serial:socket:connected", s)), this.#m();
3349
+ this.useSocket && (window.addEventListener("serial:socket:disconnected", t), window.addEventListener("serial:socket:connected", s)), this.#w();
3275
3350
  }
3276
- #m() {
3351
+ #w() {
3277
3352
  const e = this;
3278
3353
  navigator.serial.addEventListener("connect", async () => {
3279
3354
  e.isDisconnected && await e.serialConnect().catch(() => {
3280
3355
  });
3281
3356
  });
3282
3357
  }
3283
- async #w() {
3358
+ async #b() {
3284
3359
  if (this.useSocket && w.isDisconnected())
3285
3360
  return;
3286
- if (!this.#n(this.__internal__.serial.port)) {
3361
+ if (!this.#t(this.__internal__.serial.port)) {
3287
3362
  this.#e({ error: "Port is closed, not readable or writable." }), await this.serialConnect();
3288
3363
  return;
3289
3364
  }
@@ -3297,7 +3372,7 @@ class ht extends de {
3297
3372
  let t = this.__internal__.time.response_general;
3298
3373
  if (e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
3299
3374
  await this.timeout(e.bytes, e.action);
3300
- }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#t(e.bytes), this.dispatch("serial:sent", {
3375
+ }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#s(e.bytes), this.dispatch("serial:sent", {
3301
3376
  action: e.action,
3302
3377
  bytes: e.bytes
3303
3378
  }), this.__internal__.auto_response) {
@@ -3335,7 +3410,7 @@ class ht extends de {
3335
3410
  }
3336
3411
  this.__internal__.serial.queue.push({ bytes: s, action: t }), this.dispatch("internal:queue", {});
3337
3412
  }
3338
- #b(e = 1) {
3413
+ #v(e = 1) {
3339
3414
  this.__internal__.device_number = e, !this.__internal__.bypassSerialBytesConnection && (this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(e));
3340
3415
  }
3341
3416
  serialSetConnectionConstant(e = 1) {
@@ -3348,7 +3423,7 @@ class ht extends de {
3348
3423
  serialCorruptMessage(e) {
3349
3424
  throw console.log(e), this.dispatch("serial:corrupt-message", { code: e }), new Error("Method not implemented 'serialCorruptMessage'");
3350
3425
  }
3351
- #v() {
3426
+ #E() {
3352
3427
  this.__internal__.last_error = {
3353
3428
  message: null,
3354
3429
  action: null,
@@ -3375,7 +3450,7 @@ class ht extends de {
3375
3450
  });
3376
3451
  }
3377
3452
  softReload() {
3378
- this.#v(), this.dispatch("serial:soft-reload", {});
3453
+ this.#E(), this.dispatch("serial:soft-reload", {});
3379
3454
  }
3380
3455
  async sendConnect() {
3381
3456
  if (!this.__internal__.serial.bytes_connection)
@@ -3443,7 +3518,7 @@ class ht extends de {
3443
3518
  }
3444
3519
  }
3445
3520
  export {
3446
- ht as S,
3447
- w as c,
3448
- a as s
3521
+ w as h,
3522
+ a as s,
3523
+ ht as v
3449
3524
  };