@persistica/flux-agent 0.0.37 → 0.0.39

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 +60 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -98,6 +98,14 @@ var isClientId = (id) => {
98
98
  return regex.test(id);
99
99
  };
100
100
 
101
+ // libs/flux/shared/types/src/lib/channels/channel-capacity.utils.ts
102
+ var MAX_CHANNEL_MEMBERS_BY_SUBSCRIPTION_TYPE = {
103
+ free: 25,
104
+ medium: 500,
105
+ high: 1e5
106
+ };
107
+ var MAX_CHANNEL_MEMBERS = MAX_CHANNEL_MEMBERS_BY_SUBSCRIPTION_TYPE.high;
108
+
101
109
  // libs/flux/shared/ws/src/lib/rpc/rpc-server.class.ts
102
110
  var RPCServer = class {
103
111
  methods = /* @__PURE__ */ new Map();
@@ -142,6 +150,7 @@ var WebSocketClient = class extends RPCServer {
142
150
  open: [],
143
151
  message: [],
144
152
  close: [],
153
+ "connect-failed": [],
145
154
  error: [],
146
155
  connecting: []
147
156
  };
@@ -158,6 +167,10 @@ var WebSocketClient = class extends RPCServer {
158
167
  // The server proved it answers pings on this connection
159
168
  closeHandled = false;
160
169
  // Guards against running the close path twice (heartbeat + late onclose)
170
+ // The current `connect()` promise's reject, held so the close path can settle a
171
+ // socket that dies before it opens — `onclose` cancels the connection timeout,
172
+ // so without this the promise has nothing left to settle it (see #508).
173
+ pendingConnectReject;
161
174
  constructor(options) {
162
175
  super();
163
176
  this.options = {
@@ -175,6 +188,7 @@ var WebSocketClient = class extends RPCServer {
175
188
  connect() {
176
189
  return new Promise((resolve, reject) => {
177
190
  this.closeHandled = false;
191
+ this.pendingConnectReject = reject;
178
192
  this.emit("connecting", this.reconnectAttempts);
179
193
  const socket = new WebSocket(
180
194
  this.options.url
@@ -183,6 +197,7 @@ var WebSocketClient = class extends RPCServer {
183
197
  const timeout = setTimeout(() => {
184
198
  if (socket.readyState !== WebSocket.OPEN) {
185
199
  socket.close();
200
+ this.pendingConnectReject = void 0;
186
201
  reject(new Error("Connection timeout"));
187
202
  }
188
203
  }, this.options.connectionTimeout);
@@ -193,6 +208,7 @@ var WebSocketClient = class extends RPCServer {
193
208
  }
194
209
  this.reconnectAttempts = 0;
195
210
  this.isOpen = true;
211
+ this.pendingConnectReject = void 0;
196
212
  this.startHeartbeat();
197
213
  this.emit("open");
198
214
  resolve();
@@ -234,6 +250,7 @@ var WebSocketClient = class extends RPCServer {
234
250
  }
235
251
  this.closeHandled = true;
236
252
  this.stopHeartbeat();
253
+ const neverOpened = !this.isOpen;
237
254
  if (this.isOpen) {
238
255
  this.emit(
239
256
  "close",
@@ -241,7 +258,16 @@ var WebSocketClient = class extends RPCServer {
241
258
  );
242
259
  this.isOpen = false;
243
260
  }
244
- if (this.options.autoReconnect && (this.options.retries === void 0 || this.reconnectAttempts < this.options.retries)) {
261
+ const willRetry = this.options.autoReconnect === true && (this.options.retries === void 0 || this.reconnectAttempts < this.options.retries);
262
+ if (neverOpened) {
263
+ const rejectPendingConnect = this.pendingConnectReject;
264
+ this.pendingConnectReject = void 0;
265
+ rejectPendingConnect?.(new Error("WebSocket closed before the connection was established"));
266
+ if (!willRetry) {
267
+ this.emit("connect-failed");
268
+ }
269
+ }
270
+ if (willRetry) {
245
271
  const delay = wasKicked ? RECONNECTION_DELAY_ON_KICK_MS : Math.min(
246
272
  this.options.maxReconnectDelay ?? Number.POSITIVE_INFINITY,
247
273
  (this.options.reconnectDelay ?? 0) * 2 ** this.reconnectAttempts
@@ -323,6 +349,7 @@ var WebSocketClient = class extends RPCServer {
323
349
  this.eventListeners.open = [];
324
350
  this.eventListeners.message = [];
325
351
  this.eventListeners.close = [];
352
+ this.eventListeners["connect-failed"] = [];
326
353
  this.eventListeners.connecting = [];
327
354
  this.eventListeners.error = [];
328
355
  }
@@ -727,6 +754,7 @@ var ChannelStateManager = class {
727
754
 
728
755
  // libs/flux/shared/connection/src/lib/flux-ws-connection.ts
729
756
  var RECONNECT_DELAY_MS = 2e3;
757
+ var MAX_SIGN_ON_RETRY_DELAY_MS = 3e4;
730
758
  var createWSConnection = (id, ticket, stateManager, onReconnectCallback, options) => {
731
759
  return new FluxWebSocketConnection(
732
760
  id,
@@ -793,6 +821,7 @@ var FluxWebSocketConnection = class {
793
821
  };
794
822
  socketMessageHandler = this.handleMessage.bind(this);
795
823
  socketCloseHandler = (reason) => this.handleSocketClose(reason);
824
+ socketConnectFailedHandler = () => this.handleSocketConnectFailed();
796
825
  socketConnectingHandler = (retryAttempt) => this.handleSocketConnecting(retryAttempt);
797
826
  socketErrorHandler = (error) => this.handleSocketError(error);
798
827
  /**
@@ -834,7 +863,7 @@ var FluxWebSocketConnection = class {
834
863
  this.connectPromiseResolver = resolve;
835
864
  });
836
865
  this.socket.clearEventSubscribers();
837
- this.socket.on("message", this.socketMessageHandler).on("close", this.socketCloseHandler).on("connecting", this.socketConnectingHandler).on("error", this.socketErrorHandler);
866
+ this.socket.on("message", this.socketMessageHandler).on("close", this.socketCloseHandler).on("connect-failed", this.socketConnectFailedHandler).on("connecting", this.socketConnectingHandler).on("error", this.socketErrorHandler);
838
867
  this.socket.connect().catch(this.socketErrorHandler);
839
868
  return this.connectPromise;
840
869
  }
@@ -974,6 +1003,33 @@ var FluxWebSocketConnection = class {
974
1003
  this.fluxInstanceId
975
1004
  );
976
1005
  this.stateManager.emitNetworkState(reason === "kicked" ? "kicked" : "disconnected");
1006
+ this.scheduleSignOn(reason === "kicked" ? RECONNECTION_DELAY_ON_KICK_MS : RECONNECT_DELAY_MS);
1007
+ }
1008
+ /**
1009
+ * A socket that died before it ever opened. It emits no 'close' — there was no
1010
+ * open connection to lose — and the low-level client does not re-dial it, since
1011
+ * the ticket in its URL is single-use and expiring. Signing on again is the only
1012
+ * way back, and nothing else will start it: before this, a socket that failed
1013
+ * this way left the Authority up, silent and unregistered, until a human
1014
+ * restarted the process (#508).
1015
+ */
1016
+ handleSocketConnectFailed() {
1017
+ this.webSocketClient = void 0;
1018
+ this.connectPromiseResolver = void 0;
1019
+ this.connectPromise = void 0;
1020
+ PicoLogger.log("\u{1F50C}\u{1F534} The connection failed before it was established", this.fluxInstanceId);
1021
+ this.stateManager.emitNetworkState("disconnected");
1022
+ this.scheduleSignOn(RECONNECT_DELAY_MS);
1023
+ }
1024
+ /**
1025
+ * Signs on again from scratch: new handshake, new ticket, new connection.
1026
+ * Driven by the disconnect, not by a successful reconnect — with an expired
1027
+ * ticket there is no successful reconnect to hang this off.
1028
+ *
1029
+ * `disconnect()` does not reach here: it clears the socket's event subscribers,
1030
+ * so nothing is emitted and nothing signs on again.
1031
+ */
1032
+ scheduleSignOn(delayMs) {
977
1033
  setTimeout(
978
1034
  () => {
979
1035
  Promise.resolve(this.onReconnectCallback()).catch((error) => {
@@ -981,9 +1037,10 @@ var FluxWebSocketConnection = class {
981
1037
  `Failed to sign on again: ${error instanceof Error ? error.message : String(error)}`,
982
1038
  this.fluxInstanceId
983
1039
  );
1040
+ this.scheduleSignOn(Math.min(delayMs * 2, MAX_SIGN_ON_RETRY_DELAY_MS));
984
1041
  });
985
1042
  },
986
- reason === "kicked" ? RECONNECTION_DELAY_ON_KICK_MS : RECONNECT_DELAY_MS
1043
+ delayMs
987
1044
  );
988
1045
  }
989
1046
  handleSocketConnecting(retryAttempt) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@persistica/flux-agent",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "description": "Persistica Flux Agent",
5
5
  "type": "module",
6
6
  "keywords": [],