webserial-core 1.1.4 → 1.1.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  An easy way to connect to a serial port from a web page.
4
4
 
5
- [![CI](https://github.com/danidoble/webserial-core/actions/workflows/ci.yml/badge.svg)](https://github.com/danidoble/webserial-core/actions/workflows/ci.yml)
5
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/danidoble/webserial-core)
6
6
  [![npm version](https://badge.fury.io/js/webserial-core.svg)](https://www.npmjs.com/package/webserial-core)
7
7
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
8
 
@@ -7,12 +7,13 @@ interface SocketResponseData {
7
7
  }
8
8
  declare class MySocket {
9
9
  #private;
10
- constructor();
10
+ constructor(uri?: string, options?: Partial<ManagerOptions & SocketOptions>);
11
11
  set uri(uri: string);
12
12
  get uri(): string;
13
13
  set options(options: Partial<ManagerOptions & SocketOptions>);
14
14
  get options(): Partial<ManagerOptions & SocketOptions>;
15
15
  get socketId(): string | null;
16
+ configure(uri?: string, options?: Partial<ManagerOptions & SocketOptions>): void;
16
17
  disconnect(): void;
17
18
  prepare(): void;
18
19
  connectDevice(config: object): void;
@@ -1 +1 @@
1
- {"version":3,"file":"Socket.d.ts","sourceRoot":"","sources":["../../lib/Socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAM,cAAc,EAAE,aAAa,EAA4B,MAAM,kBAAkB,CAAC;AAI/F,UAAU,kBAAkB;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AASD,cAAM,QAAQ;;;IAgCZ,IAAI,GAAG,CAAC,GAAG,EAAE,MAAM,EAOlB;IAED,IAAI,GAAG,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AAG,IAAI,CAE5B;IAED,SAAS,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,cAAc,GAAG,aAAa,CAAC,GAAG,IAAI;IAYhF,UAAU;IAcV,OAAO;IAaP,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAOnC,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAOtC,oBAAoB,IAAI,IAAI;IAO5B,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOzB,UAAU,CAAC,IAAI,EAAE,kBAAkB,GAAG,IAAI;IAW1C,WAAW,IAAI,OAAO;IAItB,cAAc,IAAI,OAAO;CAG1B;AAED,eAAO,MAAM,MAAM,UAAiB,CAAC"}
@@ -213,16 +213,16 @@ function y(o = 100) {
213
213
  );
214
214
  }
215
215
  class C {
216
- #n = "http://localhost:3000";
217
- #i = {
216
+ #i = "http://localhost:3001";
217
+ #t = {
218
218
  transports: ["websocket"]
219
219
  };
220
220
  #e = null;
221
221
  #s = !1;
222
- #a = !1;
223
- #t;
224
- constructor() {
225
- this.#t = {
222
+ #o = !1;
223
+ #n;
224
+ constructor(e, t) {
225
+ e && (this.#i = e), t && (this.#t = { ...this.#t, ...t }), this.#n = {
226
226
  onResponse: this.onResponse.bind(this),
227
227
  onDisconnect: () => {
228
228
  this.#s = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
@@ -230,8 +230,8 @@ class C {
230
230
  onConnect: () => {
231
231
  this.#s = !0, window.dispatchEvent(new Event("serial:socket:connected"));
232
232
  },
233
- onConnectError: (e) => {
234
- console.debug("Socket connection error", e), this.#s = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
233
+ onConnectError: (n) => {
234
+ console.debug("Socket connection error", n), this.#s = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
235
235
  }
236
236
  };
237
237
  }
@@ -239,27 +239,32 @@ class C {
239
239
  const t = new URL(e);
240
240
  if (!["http:", "https:", "ws:", "wss:"].includes(t.protocol))
241
241
  throw new Error("URI must start with http://, https://, ws://, or wss://");
242
- this.#n = e;
242
+ this.#i = e;
243
243
  }
244
244
  get uri() {
245
- return this.#n;
245
+ return this.#i;
246
246
  }
247
247
  set options(e) {
248
248
  if (typeof e != "object")
249
249
  throw new Error("Options must be an object");
250
- this.#i = e;
250
+ this.#t = e;
251
251
  }
252
252
  get options() {
253
- return this.#i;
253
+ return this.#t;
254
254
  }
255
255
  get socketId() {
256
256
  return this.#e && this.#e.id ? this.#e.id : null;
257
257
  }
258
+ configure(e, t) {
259
+ if (this.#o)
260
+ throw new Error("Cannot configure socket after it has been initialized. Call configure() before prepare().");
261
+ e && (this.uri = e), t && (this.#t = { ...this.#t, ...t });
262
+ }
258
263
  disconnect() {
259
- 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.#s = !1;
264
+ this.#e && (this.#e.off("response", this.#n.onResponse), this.#e.off("disconnect", this.#n.onDisconnect), this.#e.off("connect", this.#n.onConnect), this.#e.off("connect_error", this.#n.onConnectError), this.#e.disconnect(), this.#e = null, this.#o = !1), this.#s = !1;
260
265
  }
261
266
  prepare() {
262
- this.#s || this.#a || (this.#e = b(this.#n, this.#i), 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));
267
+ this.#s || this.#o || (this.#e = b(this.#i, this.#t), this.#o = !0, this.#e.on("disconnect", this.#n.onDisconnect), this.#e.on("response", this.#n.onResponse), this.#e.on("connect", this.#n.onConnect), this.#e.on("connect_error", this.#n.onConnectError));
263
268
  }
264
269
  connectDevice(e) {
265
270
  if (!this.#e)
@@ -292,7 +297,7 @@ class C {
292
297
  return !this.#s;
293
298
  }
294
299
  }
295
- const c = new C(), p = {
300
+ const c = new C(), f = {
296
301
  baudRate: 9600,
297
302
  dataBits: 8,
298
303
  stopBits: 1,
@@ -348,7 +353,7 @@ class S extends m {
348
353
  delay_first_connection: 200,
349
354
  bytes_connection: null,
350
355
  filters: [],
351
- config_port: p,
356
+ config_port: f,
352
357
  queue: [],
353
358
  running_queue: !1,
354
359
  auto_response: null,
@@ -374,17 +379,17 @@ class S extends m {
374
379
  reconnection: 0
375
380
  }
376
381
  };
377
- #n = null;
382
+ #i = null;
378
383
  constructor({
379
384
  filters: e = null,
380
- config_port: t = p,
385
+ config_port: t = f,
381
386
  no_device: n = 1,
382
387
  device_listen_on_channel: i = 1,
383
388
  bypassSerialBytesConnection: a = !1,
384
389
  socket: r = !1
385
390
  } = {
386
391
  filters: null,
387
- config_port: p,
392
+ config_port: f,
388
393
  no_device: 1,
389
394
  device_listen_on_channel: 1,
390
395
  bypassSerialBytesConnection: !1,
@@ -426,15 +431,15 @@ class S extends m {
426
431
  this.__internal__.serial.useRTSCTS = e;
427
432
  }
428
433
  get isConnected() {
429
- const e = this.__internal__.serial.connected, t = this.#i(this.__internal__.serial.port);
434
+ const e = this.__internal__.serial.connected, t = this.#t(this.__internal__.serial.port);
430
435
  return e && !t && this.#e({ error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
431
436
  }
432
437
  get isConnecting() {
433
438
  return this.__internal__.serial.connecting;
434
439
  }
435
440
  get isDisconnected() {
436
- const e = this.__internal__.serial.connected, t = this.#i(this.__internal__.serial.port);
437
- return !e && t && (this.dispatch("serial:connected"), this.#o(!1), s.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
441
+ const e = this.__internal__.serial.connected, t = this.#t(this.__internal__.serial.port);
442
+ return !e && t && (this.dispatch("serial:connected"), this.#a(!1), s.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
438
443
  }
439
444
  get deviceNumber() {
440
445
  return this.__internal__.device_number;
@@ -607,7 +612,7 @@ class S extends m {
607
612
  }
608
613
  };
609
614
  }
610
- #i(e) {
615
+ #t(e) {
611
616
  return this.useSocket ? this.__internal__.serial.connected && c.isConnected() : !!(e && e.readable && e.writable);
612
617
  }
613
618
  async timeout(e, t) {
@@ -628,7 +633,7 @@ class S extends m {
628
633
  }
629
634
  socketResponse(e) {
630
635
  const t = this.__internal__.serial.connected;
631
- if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), s.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#o(!1)), e.type === "success")
636
+ if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), s.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#a(!1)), e.type === "success")
632
637
  this.#r(new Uint8Array(e.data));
633
638
  else if (e.type === "error") {
634
639
  const n = new Error("The port is closed or is not readable/writable");
@@ -638,9 +643,9 @@ class S extends m {
638
643
  }
639
644
  async connect() {
640
645
  return this.isConnected ? !0 : (this.__internal__.serial.aux_connecting = "idle", new Promise((e, t) => {
641
- this.#n || (this.#n = this.#s.bind(this)), this.on("internal:connecting", this.#n);
646
+ this.#i || (this.#i = this.#s.bind(this)), this.on("internal:connecting", this.#i);
642
647
  const n = setInterval(() => {
643
- this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(n), 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 }));
648
+ this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(n), this.__internal__.serial.aux_connecting = "idle", this.#i !== null && this.off("internal:connecting", this.#i), 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 }));
644
649
  }, 100);
645
650
  this.serialConnect();
646
651
  }));
@@ -659,7 +664,7 @@ class S extends m {
659
664
  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, s.$dispatchChange(this);
660
665
  }
661
666
  }
662
- async #a(e) {
667
+ async #o(e) {
663
668
  if (c.isDisconnected())
664
669
  throw this.#e({ error: "Socket is disconnected." }), new Error("The socket is disconnected");
665
670
  if (this.isDisconnected)
@@ -667,9 +672,9 @@ class S extends m {
667
672
  const t = this.validateBytes(e);
668
673
  c.write({ config: this.configDeviceSocket, bytes: Array.from(t) });
669
674
  }
670
- async #t(e) {
675
+ async #n(e) {
671
676
  if (this.useSocket) {
672
- await this.#a(e);
677
+ await this.#o(e);
673
678
  return;
674
679
  }
675
680
  const t = this.__internal__.serial.port;
@@ -693,7 +698,7 @@ class S extends m {
693
698
  #r(e = new Uint8Array([]), t = !1) {
694
699
  if (e && e.length > 0) {
695
700
  const n = this.__internal__.serial.connected;
696
- if (this.__internal__.serial.connected = this.#i(this.__internal__.serial.port), s.$dispatchChange(this), !n && 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")
701
+ if (this.__internal__.serial.connected = this.#t(this.__internal__.serial.port), s.$dispatchChange(this), !n && 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")
697
702
  t ? this.serialCorruptMessage(this.parseUint8ToHex(e)) : this.serialMessage(this.parseUint8ToHex(e));
698
703
  else if (this.__internal__.serial.response.as === "uint8")
699
704
  t ? this.serialCorruptMessage(e) : this.serialMessage(e);
@@ -733,7 +738,7 @@ class S extends m {
733
738
  return e.length === 0 ? t : t.filter((i) => {
734
739
  const a = i.getInfo();
735
740
  return e.some((r) => a.usbProductId === r.usbProductId && a.usbVendorId === r.usbVendorId);
736
- }).filter((i) => !this.#i(i));
741
+ }).filter((i) => !this.#t(i));
737
742
  }
738
743
  async serialPortsSaved(e) {
739
744
  const t = this.serialFilters;
@@ -867,12 +872,12 @@ class S extends m {
867
872
  }
868
873
  for (const l of u)
869
874
  this.#r(l);
870
- const f = new TextEncoder().encode(r);
871
- this.__internal__.serial.response.buffer = f, f.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
875
+ const p = new TextEncoder().encode(r);
876
+ this.__internal__.serial.response.buffer = p, p.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
872
877
  this.#r(this.__internal__.serial.response.buffer, !0), this.__internal__.serial.response.buffer = new Uint8Array(0);
873
878
  }, this.__internal__.serial.free_timeout_ms ?? 50));
874
879
  }
875
- async #p() {
880
+ async #f() {
876
881
  const e = this.__internal__.serial.port;
877
882
  if (!e || !e.readable) throw new Error("Port is not readable");
878
883
  const t = e.readable.getReader();
@@ -889,12 +894,12 @@ class S extends m {
889
894
  t.releaseLock(), this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
890
895
  }
891
896
  }
892
- #o(e) {
897
+ #a(e) {
893
898
  e !== this.__internal__.serial.connecting && (this.__internal__.serial.connecting = e, this.dispatch("serial:connecting", { active: e }), this.dispatch("internal:connecting", { active: e }));
894
899
  }
895
900
  async serialConnect() {
896
901
  try {
897
- if (this.#o(!0), this.useSocket) {
902
+ if (this.#a(!0), this.useSocket) {
898
903
  if (c.prepare(), this.__internal__.serial.last_action = "connect", this.__internal__.timeout.until_response = setTimeout(async () => {
899
904
  await this.timeout(this.__internal__.serial.bytes_connection ?? [], "connection:start");
900
905
  }, this.__internal__.time.response_connection), c.isDisconnected())
@@ -919,25 +924,25 @@ class S extends m {
919
924
  await t.open(this.serialConfigPort);
920
925
  const n = this;
921
926
  t.onconnect = (i) => {
922
- n.dispatch("serial:connected", i), n.#o(!1), s.$dispatchChange(this), n.__internal__.serial.queue.length > 0 ? n.dispatch("internal:queue", {}) : n.__internal__.serial.running_queue = !1;
927
+ n.dispatch("serial:connected", i), n.#a(!1), s.$dispatchChange(this), n.__internal__.serial.queue.length > 0 ? n.dispatch("internal:queue", {}) : n.__internal__.serial.running_queue = !1;
923
928
  }, t.ondisconnect = async () => {
924
929
  await n.disconnect();
925
930
  }, await y(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
926
931
  await n.timeout(n.__internal__.serial.bytes_connection ?? [], "connection:start");
927
- }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#t(this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
932
+ }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#n(this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
928
933
  action: "connect",
929
934
  bytes: this.__internal__.serial.bytes_connection
930
- }), this.__internal__.auto_response && this.#r(this.__internal__.serial.auto_response), await this.#p();
935
+ }), this.__internal__.auto_response && this.#r(this.__internal__.serial.auto_response), await this.#f();
931
936
  }
932
937
  } catch (e) {
933
- this.#o(!1), this.serialErrors(e);
938
+ this.#a(!1), this.serialErrors(e);
934
939
  }
935
940
  }
936
- async #f() {
941
+ async #p() {
937
942
  return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
938
943
  }
939
944
  async serialForget() {
940
- return await this.#f();
945
+ return await this.#p();
941
946
  }
942
947
  decToHex(e) {
943
948
  return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
@@ -1001,7 +1006,7 @@ class S extends m {
1001
1006
  async #w() {
1002
1007
  if (this.useSocket && c.isDisconnected())
1003
1008
  return;
1004
- if (!this.#i(this.__internal__.serial.port)) {
1009
+ if (!this.#t(this.__internal__.serial.port)) {
1005
1010
  this.#e({ error: "Port is closed, not readable or writable." }), await this.serialConnect();
1006
1011
  return;
1007
1012
  }
@@ -1015,7 +1020,7 @@ class S extends m {
1015
1020
  let t = this.__internal__.time.response_general;
1016
1021
  if (e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
1017
1022
  await this.timeout(e.bytes, e.action);
1018
- }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#t(e.bytes), this.dispatch("serial:sent", {
1023
+ }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#n(e.bytes), this.dispatch("serial:sent", {
1019
1024
  action: e.action,
1020
1025
  bytes: e.bytes
1021
1026
  }), this.__internal__.auto_response) {