@persistica/flux-agent 0.0.16 → 0.0.17

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
  };
@@ -506,9 +510,9 @@ var createWSConnection = (id, ticket, stateManager, onReconnectCallback, options
506
510
  stateManager,
507
511
  ticket,
508
512
  {
513
+ domain: options?.domain,
509
514
  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
515
+ retries: options?.retries
512
516
  }
513
517
  );
514
518
  };
@@ -520,13 +524,16 @@ var FluxWebSocketConnection = class {
520
524
  this.token = token;
521
525
  this.options = options;
522
526
  this.options = {
527
+ domain: this.options?.domain,
523
528
  secretKey: this.options?.secretKey,
524
- retries: this.options?.retries ?? 1e4,
525
- port: this.options?.port ?? 5100
529
+ retries: this.options?.retries ?? 1e4
526
530
  };
531
+ const url = new URL(this.options?.domain ?? "http://localhost:5100");
532
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
533
+ url.searchParams.set("token", this.token);
527
534
  this.socket = new FluxWebSocketClientConnection(
528
535
  {
529
- url: `ws://localhost:${this.options.port}?token=${this.token}`,
536
+ url: url.toString(),
530
537
  autoReconnect: true,
531
538
  reconnectDelay: 2e3,
532
539
  retries: this.options.retries
@@ -616,14 +623,14 @@ var FluxWebSocketConnection = class {
616
623
  /**
617
624
  * Registers an authority to the network.
618
625
  */
619
- async registerAuthority(cb, authorizeNetworkChannel) {
626
+ async registerAuthority(authorizeAgentConnection, authorizeChannelAccess) {
620
627
  const webSocketClient = await this.connect();
621
- webSocketClient.registerMethod("authorize", (messageWithType) => {
628
+ webSocketClient.registerMethod("authorizeAgentConnection", (messageWithType) => {
622
629
  const dataType = messageWithType.split(":")[0];
623
630
  const message = messageWithType.substring(messageWithType.indexOf(":") + 1);
624
- return cb(dataType === "json" ? JSON.parse(message) : message);
631
+ return authorizeAgentConnection(dataType === "json" ? JSON.parse(message) : message);
625
632
  });
626
- webSocketClient.registerMethod("authorizeNetworkChannel", authorizeNetworkChannel);
633
+ webSocketClient.registerMethod("authorizeChannelAccess", authorizeChannelAccess);
627
634
  return Promise.resolve(void 0);
628
635
  }
629
636
  /**
@@ -1025,13 +1032,16 @@ var authenticateAgentOrThrow = async (networkId, domain, unknownIdentificationPa
1025
1032
  body: customPayload
1026
1033
  });
1027
1034
  if (!response.ok) {
1035
+ const textResponse = await response.text();
1028
1036
  if (response.status === 401) {
1029
- const textResponse = await response.text();
1030
1037
  if (textResponse.startsWith(NetworkAuthorityNotFoundError.message)) {
1031
1038
  throw new NetworkAuthorityNotFoundError(networkId);
1032
1039
  }
1033
1040
  }
1034
- throw new Error(`Auth failed: ${response.status}`);
1041
+ if (response.status === 404) {
1042
+ throw new Error(`Mesh server not found at ${url.origin}.`);
1043
+ }
1044
+ throw new Error(textResponse ? `Auth failed: ${response.status} - ${textResponse}` : `Auth failed: ${response.status}`);
1035
1045
  }
1036
1046
  const result = await response.text();
1037
1047
  if (!checkAuthTicketShape(result)) {
@@ -1059,6 +1069,7 @@ var FluxClientData = class {
1059
1069
  globalThis.agentLoadCount ??= 0;
1060
1070
  globalThis.agentLoadCount++;
1061
1071
  console.log(`[flux-agent] Reloaded ${globalThis.agentLoadCount} time(s)`);
1072
+ var DEFAULT_FLUX_DOMAIN = "https://mesh.persistica.io";
1062
1073
  var FluxAgent = class {
1063
1074
  constructor(networkId, options) {
1064
1075
  this.networkId = networkId;
@@ -1078,6 +1089,7 @@ var FluxAgent = class {
1078
1089
  * @returns { Promise<FluxAgentNetworkConnection> } The network connection
1079
1090
  */
1080
1091
  async connect(identification, clientUId) {
1092
+ const domain = this.options?.domain ?? DEFAULT_FLUX_DOMAIN;
1081
1093
  this.stateManager.emitNetworkState("authorizing");
1082
1094
  if (clientUId && !validateAgentUIDOrThrow(clientUId)) {
1083
1095
  throw new Error("Will never be thrown");
@@ -1087,7 +1099,7 @@ var FluxAgent = class {
1087
1099
  async () => {
1088
1100
  return authenticateAgentOrThrow(
1089
1101
  this.networkId,
1090
- this.options?.domain ?? "http://localhost:5100",
1102
+ domain,
1091
1103
  identification,
1092
1104
  {
1093
1105
  clientUId,
@@ -1120,6 +1132,7 @@ var FluxAgent = class {
1120
1132
  const fluxNetworkConnection = await this.fluxWebSocketConnection.connectToNetwork();
1121
1133
  return fluxNetworkConnection;
1122
1134
  } catch (error) {
1135
+ this.stateManager.emitNetworkState("auth-error");
1123
1136
  return Promise.reject(error);
1124
1137
  }
1125
1138
  }
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.17",
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
  }