@persistica/flux-agent 0.0.23 → 0.0.25

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.
Files changed (2) hide show
  1. package/index.js +27 -31
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -87,7 +87,7 @@ var validateNetworkIdOrThrow = (networkId) => {
87
87
  // libs/flux/shared/types/src/lib/client-id.type.ts
88
88
  var NANOID_LENGTH = 21;
89
89
  var NANOID_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-";
90
- var isNanoId = (id) => {
90
+ var isClientId = (id) => {
91
91
  if (typeof id !== "string") {
92
92
  return false;
93
93
  }
@@ -198,7 +198,7 @@ var WebSocketClient = class extends RPCServer {
198
198
  );
199
199
  setTimeout(() => {
200
200
  this.reconnectAttempts++;
201
- this.connect();
201
+ void this.connect();
202
202
  }, delay);
203
203
  } else if (this.options.autoReconnect) {
204
204
  this.emit("error", new Error("Connection failed: retries exhausted"));
@@ -402,9 +402,7 @@ var FluxAgentNetworkConnection = class {
402
402
  this._fluxWebSocketClientConnection,
403
403
  this._webRTCStateChange
404
404
  );
405
- console.log(
406
- this.iceConnection ? `RTCPeerConnection is available` : `RTCPeerConnection is not available`
407
- );
405
+ console.log(`RTCPeerConnection is available`);
408
406
  } else {
409
407
  console.warn("\u2757WebRTC is not available in this environment.");
410
408
  }
@@ -442,7 +440,7 @@ var FluxAgentNetworkConnection = class {
442
440
  * Connects to a remote agent.
443
441
  */
444
442
  connectToAgent(clientId) {
445
- this._fluxWebSocketConnection.connectToAgent(clientId);
443
+ void this._fluxWebSocketConnection.connectToAgent(clientId);
446
444
  return new FluxRemoteClient(this.iceConnection);
447
445
  }
448
446
  /**
@@ -458,25 +456,25 @@ var hasOverlap = (a, b) => a && b ? [...a].some((item) => (Array.isArray(b) ? b
458
456
  var PicoLogger = /* @__PURE__ */ (() => {
459
457
  let allowScopes;
460
458
  const configure = (configuration) => {
461
- allowScopes = typeof configuration.allowScopes === "string" && configuration.allowScopes === "*" ? "*" : new Set(configuration.allowScopes);
459
+ allowScopes = configuration.allowScopes === "*" ? "*" : new Set(configuration.allowScopes);
462
460
  };
463
461
  const log = (message, scopes) => {
464
462
  if (allowScopes !== "*" && !hasOverlap(allowScopes, scopes)) {
465
463
  return;
466
464
  }
467
- console.log(`[${scopes}] ${message}`);
465
+ console.log(`[${scopes?.toString()}] ${message}`);
468
466
  };
469
467
  const warn = (message, scopes) => {
470
468
  if (allowScopes !== "*" && !hasOverlap(allowScopes, scopes)) {
471
469
  return;
472
470
  }
473
- console.warn(`[${scopes}] ${message}`);
471
+ console.warn(`[${scopes?.toString()}] ${message}`);
474
472
  };
475
473
  const error = (message, scopes) => {
476
474
  if (allowScopes !== "*" && !hasOverlap(allowScopes, scopes)) {
477
475
  return;
478
476
  }
479
- console.error(`[${scopes}] ${message}`);
477
+ console.error(`[${scopes?.toString()}] ${message}`);
480
478
  };
481
479
  return {
482
480
  configure,
@@ -529,17 +527,16 @@ var ChannelStateManager = class {
529
527
  this.connectionTimeouts.delete(channelName);
530
528
  }
531
529
  const cb = (message) => {
530
+ const receivedChannelName = message.substring(message.indexOf(":") + 1);
531
+ if (channelName !== receivedChannelName) {
532
+ return;
533
+ }
532
534
  const timeout = this.connectionTimeouts.get(channelName);
533
535
  if (timeout) {
534
536
  clearTimeout(timeout);
535
537
  this.connectionTimeouts.delete(channelName);
536
538
  }
537
539
  fluxWebSocketConnection.removePackageTypeInterceptor(SUBSCRIBED_NETWORK_CHANNEL_NAME, cb);
538
- const receivedChannelName = message.substring(message.indexOf(":") + 1);
539
- if (channelName !== receivedChannelName) {
540
- reject(new Error(`Channel name mismatch: "${channelName}" !== "${receivedChannelName}"`));
541
- return;
542
- }
543
540
  resolve(new FluxNetworkChannel(channelName, fluxWebSocketConnection));
544
541
  };
545
542
  fluxWebSocketConnection.interceptPackageTypeMessages(
@@ -580,8 +577,8 @@ var createWSConnection = (id, ticket, stateManager, onReconnectCallback, options
580
577
  ticket,
581
578
  {
582
579
  domain: options.domain,
583
- secretKey: options?.secretKey,
584
- retries: options?.retries
580
+ secretKey: options.secretKey,
581
+ retries: options.retries
585
582
  }
586
583
  );
587
584
  };
@@ -594,8 +591,8 @@ var FluxWebSocketConnection = class {
594
591
  this.options = options;
595
592
  this.options = {
596
593
  domain: this.options.domain,
597
- secretKey: this.options?.secretKey,
598
- retries: this.options?.retries ?? 1e4
594
+ secretKey: this.options.secretKey,
595
+ retries: this.options.retries ?? 1e4
599
596
  };
600
597
  const url = new URL(this.options.domain);
601
598
  url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
@@ -789,7 +786,7 @@ var FluxWebSocketConnection = class {
789
786
  * Disconnects an agent from the network.
790
787
  */
791
788
  disconnectAgent(id) {
792
- if (!isNanoId(id)) {
789
+ if (!isClientId(id)) {
793
790
  throw new Error(`Invalid agent id: '${id}'`);
794
791
  }
795
792
  if (this.webSocketClient) {
@@ -820,7 +817,7 @@ var FluxWebSocketConnection = class {
820
817
  handleSocketConnecting(retryAttempt) {
821
818
  this.stateManager.emitNetworkState("disconnected");
822
819
  if (retryAttempt > 0) {
823
- PicoLogger.log(`\u{1F504} Connecting attempt: #${retryAttempt} of ${this.options?.retries ?? "none"}`, this.fluxInstanceId);
820
+ PicoLogger.log(`\u{1F504} Connecting attempt: #${retryAttempt} of ${this.options.retries ?? "none"}`, this.fluxInstanceId);
824
821
  } else {
825
822
  PicoLogger.log(`\u{1F504} Connecting: ${this.fluxInstanceId}`);
826
823
  }
@@ -866,26 +863,26 @@ var FluxWebSocketConnection = class {
866
863
  }
867
864
  }
868
865
  }
869
- break;
866
+ return;
870
867
  }
871
868
  case RPC_REQUEST: {
872
869
  const payload = JSON.parse(message_.substring(message_.indexOf(":") + 1));
873
- this.socket.handleMessage(payload, (data) => {
870
+ void this.socket.handleMessage(payload, (data) => {
874
871
  this.socket.send(`${RPC_RESPONSE}:${JSON.stringify(data)}`);
875
872
  });
876
- break;
873
+ return;
877
874
  }
878
875
  case RPC_RESPONSE: {
879
876
  const payload = message_.substring(message_.indexOf(":") + 1);
880
877
  PicoLogger.log("\u{1F50C} Unhandled type rpc response", "ws-client");
881
878
  PicoLogger.log(`payload: ${payload}`, "ws-client");
882
- break;
879
+ return;
883
880
  }
884
881
  default:
885
882
  if (message_.startsWith(`${ERROR}:`)) {
886
883
  }
887
884
  PicoLogger.log(`\u{1F50C} Unhandled type: "${message_}"`, "ws-client");
888
- break;
885
+ return;
889
886
  }
890
887
  }
891
888
  };
@@ -1022,9 +1019,8 @@ var StateManager = class {
1022
1019
 
1023
1020
  // libs/flux/shared/utils/src/lib/promises.utils.ts
1024
1021
  var retryOrThrow = async (fn, shouldRetry, options) => {
1025
- let attempt = 0;
1026
1022
  let overrideDelay;
1027
- while (true) {
1023
+ for (let attempt = 0; attempt <= options.retries; attempt++) {
1028
1024
  try {
1029
1025
  if (attempt > 0) {
1030
1026
  if (options.onRetry) {
@@ -1033,12 +1029,11 @@ var retryOrThrow = async (fn, shouldRetry, options) => {
1033
1029
  }
1034
1030
  return await fn();
1035
1031
  } catch (err) {
1036
- attempt++;
1037
- if (attempt > options.retries || !shouldRetry(err)) {
1032
+ if (attempt >= options.retries || !shouldRetry(err)) {
1038
1033
  throw err;
1039
1034
  }
1040
1035
  if (options.delayMs > 0 || overrideDelay && overrideDelay > 0) {
1041
- let delay = overrideDelay ?? options.delayMs;
1036
+ const delay = overrideDelay ?? options.delayMs;
1042
1037
  await new Promise((res) => setTimeout(
1043
1038
  res,
1044
1039
  delay
@@ -1046,6 +1041,7 @@ var retryOrThrow = async (fn, shouldRetry, options) => {
1046
1041
  }
1047
1042
  }
1048
1043
  }
1044
+ throw new Error("Retry loop exited unexpectedly.");
1049
1045
  };
1050
1046
 
1051
1047
  // libs/flux/shared/utils/src/lib/machine-id.util.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@persistica/flux-agent",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "description": "Persistica Flux Agent",
5
5
  "type": "module",
6
6
  "keywords": [],