@persistica/flux-agent 0.0.16 → 0.0.18

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/index.js CHANGED
@@ -8,18 +8,20 @@ var NetworkAuthorityNotFoundError = class _NetworkAuthorityNotFoundError extends
8
8
  };
9
9
 
10
10
  // libs/flux/shared/types/src/lib/channel.type.ts
11
+ var InvaliChannelNameError = class extends Error {
12
+ };
11
13
  var validateChannelNameOrThrow = (channelName) => {
12
14
  if (channelName.includes(":")) {
13
- throw new Error("Channel name cannot contain :");
15
+ throw new InvaliChannelNameError("Channel name cannot contain :");
14
16
  }
15
17
  if (channelName.includes("/")) {
16
- throw new Error("Channel name cannot contain /");
18
+ throw new InvaliChannelNameError("Channel name cannot contain /");
17
19
  }
18
20
  if (!/^[A-Za-z0-9-]+$/.test(channelName)) {
19
- throw new Error("Channel name can only contain letters, numbers and dashes ('-')");
21
+ throw new InvaliChannelNameError("Channel name can only contain letters, numbers and dashes ('-')");
20
22
  }
21
23
  if (channelName.length > 100) {
22
- throw new Error("Channel name cannot be longer than 100 characters");
24
+ throw new InvaliChannelNameError("Channel name cannot be longer than 100 characters");
23
25
  }
24
26
  return true;
25
27
  };
@@ -86,7 +88,9 @@ var validateNetworkIdOrThrow = (networkId) => {
86
88
  var NANOID_LENGTH = 21;
87
89
  var NANOID_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-";
88
90
  var isNanoId = (id) => {
89
- if (typeof id !== "string") return false;
91
+ if (typeof id !== "string") {
92
+ return false;
93
+ }
90
94
  const regex = new RegExp(`^[${NANOID_ALPHABET}]{${NANOID_LENGTH}}$`);
91
95
  return regex.test(id);
92
96
  };
@@ -389,7 +393,9 @@ var FluxAgentNetworkConnection = class {
389
393
  this._fluxWebSocketClientConnection,
390
394
  this._webRTCStateChange
391
395
  );
392
- console.log("RTCPeerConnection is available", !!this.iceConnection);
396
+ console.log(
397
+ this.iceConnection ? `RTCPeerConnection is available` : `RTCPeerConnection is not available`
398
+ );
393
399
  } else {
394
400
  console.warn("\u2757WebRTC is not available in this environment.");
395
401
  }
@@ -506,9 +512,9 @@ var createWSConnection = (id, ticket, stateManager, onReconnectCallback, options
506
512
  stateManager,
507
513
  ticket,
508
514
  {
515
+ domain: options.domain,
509
516
  secretKey: options?.secretKey,
510
- retries: options?.retries,
511
- port: options?.domain ? new URL(options.domain).port ? Number.parseInt(new URL(options.domain).port) : void 0 : void 0
517
+ retries: options?.retries
512
518
  }
513
519
  );
514
520
  };
@@ -520,13 +526,16 @@ var FluxWebSocketConnection = class {
520
526
  this.token = token;
521
527
  this.options = options;
522
528
  this.options = {
529
+ domain: this.options.domain,
523
530
  secretKey: this.options?.secretKey,
524
- retries: this.options?.retries ?? 1e4,
525
- port: this.options?.port ?? 5100
531
+ retries: this.options?.retries ?? 1e4
526
532
  };
533
+ const url = new URL(this.options?.domain ?? "http://localhost:5100");
534
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
535
+ url.searchParams.set("token", this.token);
527
536
  this.socket = new FluxWebSocketClientConnection(
528
537
  {
529
- url: `ws://localhost:${this.options.port}?token=${this.token}`,
538
+ url: url.toString(),
530
539
  autoReconnect: true,
531
540
  reconnectDelay: 2e3,
532
541
  retries: this.options.retries
@@ -616,14 +625,14 @@ var FluxWebSocketConnection = class {
616
625
  /**
617
626
  * Registers an authority to the network.
618
627
  */
619
- async registerAuthority(cb, authorizeNetworkChannel) {
628
+ async registerAuthority(authorizeAgentConnection, authorizeChannelAccess) {
620
629
  const webSocketClient = await this.connect();
621
- webSocketClient.registerMethod("authorize", (messageWithType) => {
630
+ webSocketClient.registerMethod("authorizeAgentConnection", (messageWithType) => {
622
631
  const dataType = messageWithType.split(":")[0];
623
632
  const message = messageWithType.substring(messageWithType.indexOf(":") + 1);
624
- return cb(dataType === "json" ? JSON.parse(message) : message);
633
+ return authorizeAgentConnection(dataType === "json" ? JSON.parse(message) : message);
625
634
  });
626
- webSocketClient.registerMethod("authorizeNetworkChannel", authorizeNetworkChannel);
635
+ webSocketClient.registerMethod("authorizeChannelAccess", authorizeChannelAccess);
627
636
  return Promise.resolve(void 0);
628
637
  }
629
638
  /**
@@ -1025,13 +1034,16 @@ var authenticateAgentOrThrow = async (networkId, domain, unknownIdentificationPa
1025
1034
  body: customPayload
1026
1035
  });
1027
1036
  if (!response.ok) {
1037
+ const textResponse = await response.text();
1028
1038
  if (response.status === 401) {
1029
- const textResponse = await response.text();
1030
1039
  if (textResponse.startsWith(NetworkAuthorityNotFoundError.message)) {
1031
1040
  throw new NetworkAuthorityNotFoundError(networkId);
1032
1041
  }
1033
1042
  }
1034
- throw new Error(`Auth failed: ${response.status}`);
1043
+ if (response.status === 404) {
1044
+ throw new Error(`Mesh server not found at ${url.origin}.`);
1045
+ }
1046
+ throw new Error(textResponse ? `Auth failed: ${response.status} - ${textResponse}` : `Auth failed: ${response.status}`);
1035
1047
  }
1036
1048
  const result = await response.text();
1037
1049
  if (!checkAuthTicketShape(result)) {
@@ -1059,6 +1071,7 @@ var FluxClientData = class {
1059
1071
  globalThis.agentLoadCount ??= 0;
1060
1072
  globalThis.agentLoadCount++;
1061
1073
  console.log(`[flux-agent] Reloaded ${globalThis.agentLoadCount} time(s)`);
1074
+ var DEFAULT_FLUX_DOMAIN = "https://mesh.persistica.io";
1062
1075
  var FluxAgent = class {
1063
1076
  constructor(networkId, options) {
1064
1077
  this.networkId = networkId;
@@ -1078,6 +1091,7 @@ var FluxAgent = class {
1078
1091
  * @returns { Promise<FluxAgentNetworkConnection> } The network connection
1079
1092
  */
1080
1093
  async connect(identification, clientUId) {
1094
+ const domain = this.options?.domain ?? DEFAULT_FLUX_DOMAIN;
1081
1095
  this.stateManager.emitNetworkState("authorizing");
1082
1096
  if (clientUId && !validateAgentUIDOrThrow(clientUId)) {
1083
1097
  throw new Error("Will never be thrown");
@@ -1087,7 +1101,7 @@ var FluxAgent = class {
1087
1101
  async () => {
1088
1102
  return authenticateAgentOrThrow(
1089
1103
  this.networkId,
1090
- this.options?.domain ?? "http://localhost:5100",
1104
+ domain,
1091
1105
  identification,
1092
1106
  {
1093
1107
  clientUId,
@@ -1114,12 +1128,16 @@ var FluxAgent = class {
1114
1128
  clientUId
1115
1129
  );
1116
1130
  },
1117
- this.options
1131
+ {
1132
+ ...this.options,
1133
+ domain
1134
+ }
1118
1135
  );
1119
1136
  this.fluxClientData.updateWsConnection(this.fluxWebSocketConnection);
1120
1137
  const fluxNetworkConnection = await this.fluxWebSocketConnection.connectToNetwork();
1121
1138
  return fluxNetworkConnection;
1122
1139
  } catch (error) {
1140
+ this.stateManager.emitNetworkState("auth-error");
1123
1141
  return Promise.reject(error);
1124
1142
  }
1125
1143
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@persistica/flux-agent",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "Persistica Flux Agent",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -1,4 +1,4 @@
1
1
  import { RPCServer } from '@flux/shared/ws';
2
- export declare class AuthorityClient extends RPCServer<'authorize'> {
3
- authorize(fn: (jwt: unknown) => boolean): void;
2
+ export declare class AuthorityClient extends RPCServer<'authorizeAgentConnection'> {
3
+ authorizeAgentConnection(fn: (jwt: unknown) => boolean): void;
4
4
  }