phonic 0.32.13 → 0.32.14

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.
@@ -27,6 +27,11 @@ type EventHandlers = {
27
27
  * with a terminal close code (4800 session expired, 4801 invalid state),
28
28
  * the safety cap is reached, or the user calls close().
29
29
  *
30
+ * A reconnectable close is not surfaced to the user's close handler while the
31
+ * reconnect is in flight. The close is emitted only when reconnection has
32
+ * failed, meaning the conversation is closing. Closes that are not
33
+ * reconnectable (1000, 4000, etc) are not affected and will pass through.
34
+ *
30
35
  * Uses composition rather than inheritance to avoid coupling to the parent's
31
36
  * private event handler registration or ReconnectingWebSocket internals.
32
37
  */
@@ -41,6 +46,8 @@ export declare class ReconnectableConversationsSocket {
41
46
  private _cleanupWireListeners;
42
47
  private _pendingReconnect;
43
48
  private _pendingReplacement;
49
+ private _lastSuppressedClose;
50
+ private _gaveUp;
44
51
  constructor(args: ReconnectableConversationsSocketArgs);
45
52
  /** The conversation ID captured from the server's conversation_created message. */
46
53
  get conversationId(): string | null;
@@ -67,9 +74,18 @@ export declare class ReconnectableConversationsSocket {
67
74
  connect(): never;
68
75
  close(): void;
69
76
  waitForOpen(): Promise<core.ReconnectingWebSocket>;
77
+ private _willReconnectAfter;
78
+ private _giveUp;
70
79
  private _getReconnectDelay;
71
80
  /** Schedule a reconnection attempt after backoff delay. */
72
81
  private _scheduleReconnect;
82
+ /** Settle as soon as the abort signal fires rather than waiting on the
83
+ * factory, whose promise is caller-supplied (it awaits fresh auth) and
84
+ * may never settle — the suppressed close would stay hidden forever.
85
+ * Resolves null when aborted; a socket arriving afterwards is closed
86
+ * rather than leaked. The listener is removed once the factory settles,
87
+ * so a long-lived conversation does not accumulate them on the signal. */
88
+ private _createSocketOrAbort;
73
89
  /** Perform the actual reconnection attempt. */
74
90
  private _doReconnect;
75
91
  private _wireInner;
@@ -80,6 +80,11 @@ const MAX_RECONNECT_ATTEMPTS = 10;
80
80
  * with a terminal close code (4800 session expired, 4801 invalid state),
81
81
  * the safety cap is reached, or the user calls close().
82
82
  *
83
+ * A reconnectable close is not surfaced to the user's close handler while the
84
+ * reconnect is in flight. The close is emitted only when reconnection has
85
+ * failed, meaning the conversation is closing. Closes that are not
86
+ * reconnectable (1000, 4000, etc) are not affected and will pass through.
87
+ *
83
88
  * Uses composition rather than inheritance to avoid coupling to the parent's
84
89
  * private event handler registration or ReconnectingWebSocket internals.
85
90
  */
@@ -92,6 +97,8 @@ class ReconnectableConversationsSocket {
92
97
  this._cleanupWireListeners = null;
93
98
  this._pendingReconnect = null;
94
99
  this._pendingReplacement = false;
100
+ this._lastSuppressedClose = null;
101
+ this._gaveUp = false;
95
102
  this._createReconnectSocket = args.createReconnectSocket;
96
103
  this._abortSignal = args.abortSignal != undefined ? args.abortSignal : null;
97
104
  this._inner = new Socket_js_1.ConversationsSocket({ socket: args.socket });
@@ -170,15 +177,36 @@ class ReconnectableConversationsSocket {
170
177
  clearTimeout(this._pendingReconnect);
171
178
  this._pendingReconnect = null;
172
179
  }
180
+ // Close before detaching: ConversationsSocket.close() synthesizes a
181
+ // 1000 close event, and a user-initiated close should still surface one.
182
+ this._inner.close();
173
183
  (_a = this._cleanupWireListeners) === null || _a === void 0 ? void 0 : _a.call(this);
174
184
  this._cleanupWireListeners = null;
175
- this._inner.close();
176
185
  }
177
186
  waitForOpen() {
178
187
  return __awaiter(this, void 0, void 0, function* () {
179
188
  return this._inner.waitForOpen();
180
189
  });
181
190
  }
191
+ _willReconnectAfter(event) {
192
+ return (!this._isClosed
193
+ && !this._gaveUp
194
+ && this._conversationId !== null
195
+ && isReconnectableClose(event.code, event.reason));
196
+ }
197
+ _giveUp() {
198
+ var _a, _b;
199
+ this._pendingReplacement = false;
200
+ if (this._gaveUp || this._isClosed) {
201
+ return;
202
+ }
203
+ this._gaveUp = true;
204
+ const suppressed = this._lastSuppressedClose;
205
+ this._lastSuppressedClose = null;
206
+ if (suppressed !== null) {
207
+ (_b = (_a = this._handlers).close) === null || _b === void 0 ? void 0 : _b.call(_a, suppressed);
208
+ }
209
+ }
182
210
  _getReconnectDelay() {
183
211
  // Exponential backoff: 500ms, 1s, 2s, 4s, capped at 5s
184
212
  const delay = BASE_RECONNECT_DELAY_MS * Math.pow(2, this._reconnectAttempts - 1);
@@ -187,12 +215,18 @@ class ReconnectableConversationsSocket {
187
215
  /** Schedule a reconnection attempt after backoff delay. */
188
216
  _scheduleReconnect() {
189
217
  var _a, _b, _c;
190
- if (this._isClosed || this._conversationId === null || ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted)) {
218
+ // With no conversation the close was never suppressed, so unlike the
219
+ // branches below there is nothing to emit.
220
+ if (this._isClosed || this._conversationId === null) {
221
+ return;
222
+ }
223
+ if ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) {
224
+ this._giveUp();
191
225
  return;
192
226
  }
193
227
  if (this._reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
194
- this._pendingReplacement = false;
195
228
  (_c = (_b = this._handlers).error) === null || _c === void 0 ? void 0 : _c.call(_b, new Error("Max reconnect attempts reached"));
229
+ this._giveUp();
196
230
  return;
197
231
  }
198
232
  // Clear any existing timer to prevent orphaned timeouts
@@ -203,24 +237,70 @@ class ReconnectableConversationsSocket {
203
237
  const delay = this._getReconnectDelay();
204
238
  this._pendingReplacement = true;
205
239
  this._pendingReconnect = setTimeout(() => {
240
+ var _a;
206
241
  this._pendingReconnect = null;
207
242
  if (this._isClosed) {
208
243
  this._pendingReplacement = false;
209
244
  return;
210
245
  }
246
+ if ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) {
247
+ this._giveUp();
248
+ return;
249
+ }
211
250
  void this._doReconnect();
212
251
  }, delay);
213
252
  }
253
+ /** Settle as soon as the abort signal fires rather than waiting on the
254
+ * factory, whose promise is caller-supplied (it awaits fresh auth) and
255
+ * may never settle — the suppressed close would stay hidden forever.
256
+ * Resolves null when aborted; a socket arriving afterwards is closed
257
+ * rather than leaked. The listener is removed once the factory settles,
258
+ * so a long-lived conversation does not accumulate them on the signal. */
259
+ _createSocketOrAbort(created) {
260
+ const signal = this._abortSignal;
261
+ if (signal === null) {
262
+ return created;
263
+ }
264
+ const discardLate = () => {
265
+ void created.then((socket) => socket.close()).catch(() => undefined);
266
+ };
267
+ if (signal.aborted) {
268
+ discardLate();
269
+ return Promise.resolve(null);
270
+ }
271
+ return new Promise((resolve, reject) => {
272
+ const onAbort = () => {
273
+ discardLate();
274
+ resolve(null);
275
+ };
276
+ signal.addEventListener("abort", onAbort, { once: true });
277
+ created.then((socket) => {
278
+ signal.removeEventListener("abort", onAbort);
279
+ resolve(socket);
280
+ }, (error) => {
281
+ signal.removeEventListener("abort", onAbort);
282
+ reject(error);
283
+ });
284
+ });
285
+ }
214
286
  /** Perform the actual reconnection attempt. */
215
287
  _doReconnect() {
216
288
  return __awaiter(this, void 0, void 0, function* () {
217
289
  var _a, _b;
218
290
  try {
219
291
  const created = this._createReconnectSocket(this._conversationId);
220
- const newRawSocket = created instanceof Promise ? yield created : created;
292
+ const newRawSocket = created instanceof Promise ? yield this._createSocketOrAbort(created) : created;
293
+ if (newRawSocket === null) {
294
+ // Aborted while the factory was still in flight.
295
+ this._giveUp();
296
+ return;
297
+ }
221
298
  if (this._isClosed || ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted)) {
222
299
  this._pendingReplacement = false;
223
300
  newRawSocket.close();
301
+ if (!this._isClosed) {
302
+ this._giveUp();
303
+ }
224
304
  return;
225
305
  }
226
306
  // Clean up the old socket: remove our custom listeners and close
@@ -233,7 +313,7 @@ class ReconnectableConversationsSocket {
233
313
  catch ( /* already closed from 1006 */_c) { /* already closed from 1006 */ }
234
314
  const newInner = new Socket_js_1.ConversationsSocket({ socket: newRawSocket });
235
315
  this._inner = newInner;
236
- this._wireInner(newInner, newRawSocket);
316
+ this._wireInner(newInner, newRawSocket, true);
237
317
  }
238
318
  catch (_d) {
239
319
  this._pendingReplacement = false;
@@ -244,17 +324,41 @@ class ReconnectableConversationsSocket {
244
324
  }
245
325
  });
246
326
  }
247
- _wireInner(inner, rawSocket) {
248
- // Forward events from the inner ConversationsSocket to user handlers.
249
- // Clear _pendingReplacement before calling the user's open handler
250
- // so that sends inside the handler are not silently dropped.
327
+ _wireInner(inner, rawSocket, isReplacement = false) {
328
+ // A replacement that closes before ever opening failed at the transport
329
+ // level, and its code says nothing about why: ReconnectingWebSocket
330
+ // synthesizes 1000 for connect errors and timeouts (_handleError ->
331
+ // _disconnect). Read it as a failed attempt rather than as a close.
332
+ // _isClosed excluded: close() also reaches this via the synthesized
333
+ // 1000, and a user-initiated close is a close, not a failed attempt.
334
+ let opened = false;
335
+ const isFailedAttempt = () => isReplacement && !opened && !this._isClosed;
336
+ // Clearing _pendingReplacement before the user's open handler keeps
337
+ // sends made inside that handler from being dropped.
251
338
  inner.on("open", () => {
252
339
  var _a, _b;
340
+ opened = true;
253
341
  this._pendingReplacement = false;
342
+ this._lastSuppressedClose = null;
254
343
  (_b = (_a = this._handlers).open) === null || _b === void 0 ? void 0 : _b.call(_a);
255
344
  });
256
345
  inner.on("message", (msg) => { var _a, _b; return (_b = (_a = this._handlers).message) === null || _b === void 0 ? void 0 : _b.call(_a, msg); });
257
- inner.on("close", (ev) => { var _a, _b; return (_b = (_a = this._handlers).close) === null || _b === void 0 ? void 0 : _b.call(_a, ev); });
346
+ // ConversationsSocket registers this in its constructor, so it runs
347
+ // before the rawSocket close listener below that schedules the
348
+ // reconnect — the decision cannot be read off reconnect state here.
349
+ inner.on("close", (ev) => {
350
+ var _a, _b;
351
+ // Leave _lastSuppressedClose holding the original drop, so _giveUp
352
+ // reports what went wrong rather than this attempt's code.
353
+ if (isFailedAttempt()) {
354
+ return;
355
+ }
356
+ if (this._willReconnectAfter(ev)) {
357
+ this._lastSuppressedClose = ev;
358
+ return;
359
+ }
360
+ (_b = (_a = this._handlers).close) === null || _b === void 0 ? void 0 : _b.call(_a, ev);
361
+ });
258
362
  inner.on("error", (err) => { var _a, _b; return (_b = (_a = this._handlers).error) === null || _b === void 0 ? void 0 : _b.call(_a, err); });
259
363
  // Intercept raw messages to capture conversation_id and reset reconnect counter
260
364
  const onMessage = (event) => {
@@ -274,8 +378,15 @@ class ReconnectableConversationsSocket {
274
378
  // ignore — inner socket handles parse errors
275
379
  }
276
380
  };
381
+ // After giving up this is a plain pass-through: the close was already
382
+ // forwarded above, and retrying would repeat the give-up error.
277
383
  const onClose = (event) => {
278
- if (this._isClosed) {
384
+ if (this._isClosed || this._gaveUp) {
385
+ return;
386
+ }
387
+ if (isFailedAttempt()) {
388
+ rawSocket.close();
389
+ this._scheduleReconnect();
279
390
  return;
280
391
  }
281
392
  if (isReconnectableClose(event.code, event.reason) && this._conversationId !== null) {
@@ -294,6 +405,11 @@ class ReconnectableConversationsSocket {
294
405
  rawSocket.addEventListener("message", onMessage);
295
406
  rawSocket.addEventListener("close", onClose);
296
407
  this._cleanupWireListeners = () => {
408
+ // ConversationsSocket.close() synthesizes a 1000 close event, so a
409
+ // discarded socket would otherwise report a spurious normal closure.
410
+ for (const event of ["open", "message", "close", "error"]) {
411
+ inner.on(event, undefined);
412
+ }
297
413
  rawSocket.removeEventListener("message", onMessage);
298
414
  rawSocket.removeEventListener("close", onClose);
299
415
  };
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.32.13";
1
+ export declare const SDK_VERSION = "0.32.14";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "0.32.13";
4
+ exports.SDK_VERSION = "0.32.14";
@@ -27,6 +27,11 @@ type EventHandlers = {
27
27
  * with a terminal close code (4800 session expired, 4801 invalid state),
28
28
  * the safety cap is reached, or the user calls close().
29
29
  *
30
+ * A reconnectable close is not surfaced to the user's close handler while the
31
+ * reconnect is in flight. The close is emitted only when reconnection has
32
+ * failed, meaning the conversation is closing. Closes that are not
33
+ * reconnectable (1000, 4000, etc) are not affected and will pass through.
34
+ *
30
35
  * Uses composition rather than inheritance to avoid coupling to the parent's
31
36
  * private event handler registration or ReconnectingWebSocket internals.
32
37
  */
@@ -41,6 +46,8 @@ export declare class ReconnectableConversationsSocket {
41
46
  private _cleanupWireListeners;
42
47
  private _pendingReconnect;
43
48
  private _pendingReplacement;
49
+ private _lastSuppressedClose;
50
+ private _gaveUp;
44
51
  constructor(args: ReconnectableConversationsSocketArgs);
45
52
  /** The conversation ID captured from the server's conversation_created message. */
46
53
  get conversationId(): string | null;
@@ -67,9 +74,18 @@ export declare class ReconnectableConversationsSocket {
67
74
  connect(): never;
68
75
  close(): void;
69
76
  waitForOpen(): Promise<core.ReconnectingWebSocket>;
77
+ private _willReconnectAfter;
78
+ private _giveUp;
70
79
  private _getReconnectDelay;
71
80
  /** Schedule a reconnection attempt after backoff delay. */
72
81
  private _scheduleReconnect;
82
+ /** Settle as soon as the abort signal fires rather than waiting on the
83
+ * factory, whose promise is caller-supplied (it awaits fresh auth) and
84
+ * may never settle — the suppressed close would stay hidden forever.
85
+ * Resolves null when aborted; a socket arriving afterwards is closed
86
+ * rather than leaked. The listener is removed once the factory settles,
87
+ * so a long-lived conversation does not accumulate them on the signal. */
88
+ private _createSocketOrAbort;
73
89
  /** Perform the actual reconnection attempt. */
74
90
  private _doReconnect;
75
91
  private _wireInner;
@@ -44,6 +44,11 @@ const MAX_RECONNECT_ATTEMPTS = 10;
44
44
  * with a terminal close code (4800 session expired, 4801 invalid state),
45
45
  * the safety cap is reached, or the user calls close().
46
46
  *
47
+ * A reconnectable close is not surfaced to the user's close handler while the
48
+ * reconnect is in flight. The close is emitted only when reconnection has
49
+ * failed, meaning the conversation is closing. Closes that are not
50
+ * reconnectable (1000, 4000, etc) are not affected and will pass through.
51
+ *
47
52
  * Uses composition rather than inheritance to avoid coupling to the parent's
48
53
  * private event handler registration or ReconnectingWebSocket internals.
49
54
  */
@@ -56,6 +61,8 @@ export class ReconnectableConversationsSocket {
56
61
  this._cleanupWireListeners = null;
57
62
  this._pendingReconnect = null;
58
63
  this._pendingReplacement = false;
64
+ this._lastSuppressedClose = null;
65
+ this._gaveUp = false;
59
66
  this._createReconnectSocket = args.createReconnectSocket;
60
67
  this._abortSignal = args.abortSignal != undefined ? args.abortSignal : null;
61
68
  this._inner = new ConversationsSocket({ socket: args.socket });
@@ -134,15 +141,36 @@ export class ReconnectableConversationsSocket {
134
141
  clearTimeout(this._pendingReconnect);
135
142
  this._pendingReconnect = null;
136
143
  }
144
+ // Close before detaching: ConversationsSocket.close() synthesizes a
145
+ // 1000 close event, and a user-initiated close should still surface one.
146
+ this._inner.close();
137
147
  (_a = this._cleanupWireListeners) === null || _a === void 0 ? void 0 : _a.call(this);
138
148
  this._cleanupWireListeners = null;
139
- this._inner.close();
140
149
  }
141
150
  waitForOpen() {
142
151
  return __awaiter(this, void 0, void 0, function* () {
143
152
  return this._inner.waitForOpen();
144
153
  });
145
154
  }
155
+ _willReconnectAfter(event) {
156
+ return (!this._isClosed
157
+ && !this._gaveUp
158
+ && this._conversationId !== null
159
+ && isReconnectableClose(event.code, event.reason));
160
+ }
161
+ _giveUp() {
162
+ var _a, _b;
163
+ this._pendingReplacement = false;
164
+ if (this._gaveUp || this._isClosed) {
165
+ return;
166
+ }
167
+ this._gaveUp = true;
168
+ const suppressed = this._lastSuppressedClose;
169
+ this._lastSuppressedClose = null;
170
+ if (suppressed !== null) {
171
+ (_b = (_a = this._handlers).close) === null || _b === void 0 ? void 0 : _b.call(_a, suppressed);
172
+ }
173
+ }
146
174
  _getReconnectDelay() {
147
175
  // Exponential backoff: 500ms, 1s, 2s, 4s, capped at 5s
148
176
  const delay = BASE_RECONNECT_DELAY_MS * Math.pow(2, this._reconnectAttempts - 1);
@@ -151,12 +179,18 @@ export class ReconnectableConversationsSocket {
151
179
  /** Schedule a reconnection attempt after backoff delay. */
152
180
  _scheduleReconnect() {
153
181
  var _a, _b, _c;
154
- if (this._isClosed || this._conversationId === null || ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted)) {
182
+ // With no conversation the close was never suppressed, so unlike the
183
+ // branches below there is nothing to emit.
184
+ if (this._isClosed || this._conversationId === null) {
185
+ return;
186
+ }
187
+ if ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) {
188
+ this._giveUp();
155
189
  return;
156
190
  }
157
191
  if (this._reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
158
- this._pendingReplacement = false;
159
192
  (_c = (_b = this._handlers).error) === null || _c === void 0 ? void 0 : _c.call(_b, new Error("Max reconnect attempts reached"));
193
+ this._giveUp();
160
194
  return;
161
195
  }
162
196
  // Clear any existing timer to prevent orphaned timeouts
@@ -167,24 +201,70 @@ export class ReconnectableConversationsSocket {
167
201
  const delay = this._getReconnectDelay();
168
202
  this._pendingReplacement = true;
169
203
  this._pendingReconnect = setTimeout(() => {
204
+ var _a;
170
205
  this._pendingReconnect = null;
171
206
  if (this._isClosed) {
172
207
  this._pendingReplacement = false;
173
208
  return;
174
209
  }
210
+ if ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) {
211
+ this._giveUp();
212
+ return;
213
+ }
175
214
  void this._doReconnect();
176
215
  }, delay);
177
216
  }
217
+ /** Settle as soon as the abort signal fires rather than waiting on the
218
+ * factory, whose promise is caller-supplied (it awaits fresh auth) and
219
+ * may never settle — the suppressed close would stay hidden forever.
220
+ * Resolves null when aborted; a socket arriving afterwards is closed
221
+ * rather than leaked. The listener is removed once the factory settles,
222
+ * so a long-lived conversation does not accumulate them on the signal. */
223
+ _createSocketOrAbort(created) {
224
+ const signal = this._abortSignal;
225
+ if (signal === null) {
226
+ return created;
227
+ }
228
+ const discardLate = () => {
229
+ void created.then((socket) => socket.close()).catch(() => undefined);
230
+ };
231
+ if (signal.aborted) {
232
+ discardLate();
233
+ return Promise.resolve(null);
234
+ }
235
+ return new Promise((resolve, reject) => {
236
+ const onAbort = () => {
237
+ discardLate();
238
+ resolve(null);
239
+ };
240
+ signal.addEventListener("abort", onAbort, { once: true });
241
+ created.then((socket) => {
242
+ signal.removeEventListener("abort", onAbort);
243
+ resolve(socket);
244
+ }, (error) => {
245
+ signal.removeEventListener("abort", onAbort);
246
+ reject(error);
247
+ });
248
+ });
249
+ }
178
250
  /** Perform the actual reconnection attempt. */
179
251
  _doReconnect() {
180
252
  return __awaiter(this, void 0, void 0, function* () {
181
253
  var _a, _b;
182
254
  try {
183
255
  const created = this._createReconnectSocket(this._conversationId);
184
- const newRawSocket = created instanceof Promise ? yield created : created;
256
+ const newRawSocket = created instanceof Promise ? yield this._createSocketOrAbort(created) : created;
257
+ if (newRawSocket === null) {
258
+ // Aborted while the factory was still in flight.
259
+ this._giveUp();
260
+ return;
261
+ }
185
262
  if (this._isClosed || ((_a = this._abortSignal) === null || _a === void 0 ? void 0 : _a.aborted)) {
186
263
  this._pendingReplacement = false;
187
264
  newRawSocket.close();
265
+ if (!this._isClosed) {
266
+ this._giveUp();
267
+ }
188
268
  return;
189
269
  }
190
270
  // Clean up the old socket: remove our custom listeners and close
@@ -197,7 +277,7 @@ export class ReconnectableConversationsSocket {
197
277
  catch ( /* already closed from 1006 */_c) { /* already closed from 1006 */ }
198
278
  const newInner = new ConversationsSocket({ socket: newRawSocket });
199
279
  this._inner = newInner;
200
- this._wireInner(newInner, newRawSocket);
280
+ this._wireInner(newInner, newRawSocket, true);
201
281
  }
202
282
  catch (_d) {
203
283
  this._pendingReplacement = false;
@@ -208,17 +288,41 @@ export class ReconnectableConversationsSocket {
208
288
  }
209
289
  });
210
290
  }
211
- _wireInner(inner, rawSocket) {
212
- // Forward events from the inner ConversationsSocket to user handlers.
213
- // Clear _pendingReplacement before calling the user's open handler
214
- // so that sends inside the handler are not silently dropped.
291
+ _wireInner(inner, rawSocket, isReplacement = false) {
292
+ // A replacement that closes before ever opening failed at the transport
293
+ // level, and its code says nothing about why: ReconnectingWebSocket
294
+ // synthesizes 1000 for connect errors and timeouts (_handleError ->
295
+ // _disconnect). Read it as a failed attempt rather than as a close.
296
+ // _isClosed excluded: close() also reaches this via the synthesized
297
+ // 1000, and a user-initiated close is a close, not a failed attempt.
298
+ let opened = false;
299
+ const isFailedAttempt = () => isReplacement && !opened && !this._isClosed;
300
+ // Clearing _pendingReplacement before the user's open handler keeps
301
+ // sends made inside that handler from being dropped.
215
302
  inner.on("open", () => {
216
303
  var _a, _b;
304
+ opened = true;
217
305
  this._pendingReplacement = false;
306
+ this._lastSuppressedClose = null;
218
307
  (_b = (_a = this._handlers).open) === null || _b === void 0 ? void 0 : _b.call(_a);
219
308
  });
220
309
  inner.on("message", (msg) => { var _a, _b; return (_b = (_a = this._handlers).message) === null || _b === void 0 ? void 0 : _b.call(_a, msg); });
221
- inner.on("close", (ev) => { var _a, _b; return (_b = (_a = this._handlers).close) === null || _b === void 0 ? void 0 : _b.call(_a, ev); });
310
+ // ConversationsSocket registers this in its constructor, so it runs
311
+ // before the rawSocket close listener below that schedules the
312
+ // reconnect — the decision cannot be read off reconnect state here.
313
+ inner.on("close", (ev) => {
314
+ var _a, _b;
315
+ // Leave _lastSuppressedClose holding the original drop, so _giveUp
316
+ // reports what went wrong rather than this attempt's code.
317
+ if (isFailedAttempt()) {
318
+ return;
319
+ }
320
+ if (this._willReconnectAfter(ev)) {
321
+ this._lastSuppressedClose = ev;
322
+ return;
323
+ }
324
+ (_b = (_a = this._handlers).close) === null || _b === void 0 ? void 0 : _b.call(_a, ev);
325
+ });
222
326
  inner.on("error", (err) => { var _a, _b; return (_b = (_a = this._handlers).error) === null || _b === void 0 ? void 0 : _b.call(_a, err); });
223
327
  // Intercept raw messages to capture conversation_id and reset reconnect counter
224
328
  const onMessage = (event) => {
@@ -238,8 +342,15 @@ export class ReconnectableConversationsSocket {
238
342
  // ignore — inner socket handles parse errors
239
343
  }
240
344
  };
345
+ // After giving up this is a plain pass-through: the close was already
346
+ // forwarded above, and retrying would repeat the give-up error.
241
347
  const onClose = (event) => {
242
- if (this._isClosed) {
348
+ if (this._isClosed || this._gaveUp) {
349
+ return;
350
+ }
351
+ if (isFailedAttempt()) {
352
+ rawSocket.close();
353
+ this._scheduleReconnect();
243
354
  return;
244
355
  }
245
356
  if (isReconnectableClose(event.code, event.reason) && this._conversationId !== null) {
@@ -258,6 +369,11 @@ export class ReconnectableConversationsSocket {
258
369
  rawSocket.addEventListener("message", onMessage);
259
370
  rawSocket.addEventListener("close", onClose);
260
371
  this._cleanupWireListeners = () => {
372
+ // ConversationsSocket.close() synthesizes a 1000 close event, so a
373
+ // discarded socket would otherwise report a spurious normal closure.
374
+ for (const event of ["open", "message", "close", "error"]) {
375
+ inner.on(event, undefined);
376
+ }
261
377
  rawSocket.removeEventListener("message", onMessage);
262
378
  rawSocket.removeEventListener("close", onClose);
263
379
  };
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.32.13";
1
+ export declare const SDK_VERSION = "0.32.14";
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.32.13";
1
+ export const SDK_VERSION = "0.32.14";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phonic",
3
- "version": "0.32.13",
3
+ "version": "0.32.14",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",