@korajs/sync 0.1.3 → 0.1.4

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/dist/index.d.ts CHANGED
@@ -121,6 +121,8 @@ interface WebSocketTransportOptions {
121
121
  serializer?: MessageSerializer;
122
122
  /** Injectable WebSocket constructor for testing. Defaults to globalThis.WebSocket. */
123
123
  WebSocketImpl?: WebSocketConstructor;
124
+ /** Connection timeout in ms. Defaults to 10000 (10s). */
125
+ connectTimeout?: number;
124
126
  }
125
127
  /**
126
128
  * WebSocket-based sync transport implementation.
@@ -132,6 +134,7 @@ declare class WebSocketTransport implements SyncTransport {
132
134
  private errorHandler;
133
135
  private readonly serializer;
134
136
  private readonly WebSocketImpl;
137
+ private readonly connectTimeout;
135
138
  constructor(options?: WebSocketTransportOptions);
136
139
  connect(url: string, options?: TransportOptions): Promise<void>;
137
140
  disconnect(): Promise<void>;
@@ -473,6 +476,7 @@ declare class ReconnectionManager {
473
476
  private attempt;
474
477
  private timer;
475
478
  private stopped;
479
+ private running;
476
480
  private waitResolve;
477
481
  constructor(config?: ReconnectionConfig);
478
482
  /**
@@ -495,6 +499,10 @@ declare class ReconnectionManager {
495
499
  * Exposed for testing purposes.
496
500
  */
497
501
  getNextDelay(): number;
502
+ /**
503
+ * Whether the reconnection loop is currently running.
504
+ */
505
+ isRunning(): boolean;
498
506
  /**
499
507
  * Current attempt number (for testing).
500
508
  */
package/dist/index.js CHANGED
@@ -591,8 +591,10 @@ var WebSocketTransport = class {
591
591
  errorHandler = null;
592
592
  serializer;
593
593
  WebSocketImpl;
594
+ connectTimeout;
594
595
  constructor(options) {
595
596
  this.serializer = options?.serializer ?? new JsonMessageSerializer();
597
+ this.connectTimeout = options?.connectTimeout ?? 1e4;
596
598
  if (options?.WebSocketImpl) {
597
599
  this.WebSocketImpl = options.WebSocketImpl;
598
600
  } else if (typeof globalThis.WebSocket !== "undefined") {
@@ -608,12 +610,38 @@ var WebSocketTransport = class {
608
610
  });
609
611
  }
610
612
  return new Promise((resolve, reject) => {
613
+ let settled = false;
614
+ const settle = (fn) => {
615
+ if (settled) return;
616
+ settled = true;
617
+ clearTimeout(timer);
618
+ fn();
619
+ };
620
+ const timer = setTimeout(() => {
621
+ settle(() => {
622
+ const err = new SyncError2("WebSocket connection timed out", {
623
+ url,
624
+ timeout: this.connectTimeout
625
+ });
626
+ if (this.ws) {
627
+ try {
628
+ this.ws.onclose = null;
629
+ this.ws.onerror = null;
630
+ this.ws.close();
631
+ } catch {
632
+ }
633
+ this.ws = null;
634
+ }
635
+ this.errorHandler?.(err);
636
+ reject(err);
637
+ });
638
+ }, this.connectTimeout);
611
639
  try {
612
640
  const connectUrl = options?.authToken ? `${url}${url.includes("?") ? "&" : "?"}token=${encodeURIComponent(options.authToken)}` : url;
613
641
  const ws = new this.WebSocketImpl(connectUrl);
614
642
  this.ws = ws;
615
643
  ws.onopen = () => {
616
- resolve();
644
+ settle(() => resolve());
617
645
  };
618
646
  ws.onmessage = (event) => {
619
647
  try {
@@ -637,15 +665,17 @@ var WebSocketTransport = class {
637
665
  this.errorHandler?.(err);
638
666
  if (!this.isConnected()) {
639
667
  this.ws = null;
640
- reject(err);
668
+ settle(() => reject(err));
641
669
  }
642
670
  };
643
671
  } catch (err) {
644
- reject(
645
- err instanceof SyncError2 ? err : new SyncError2("Failed to create WebSocket", {
646
- url,
647
- error: String(err)
648
- })
672
+ settle(
673
+ () => reject(
674
+ err instanceof SyncError2 ? err : new SyncError2("Failed to create WebSocket", {
675
+ url,
676
+ error: String(err)
677
+ })
678
+ )
649
679
  );
650
680
  }
651
681
  });
@@ -1187,8 +1217,7 @@ var SyncEngine = class {
1187
1217
  };
1188
1218
  this.transport.send(handshake);
1189
1219
  } catch (err) {
1190
- this.transitionTo("error");
1191
- this.transitionTo("disconnected");
1220
+ this.ensureDisconnected();
1192
1221
  throw err;
1193
1222
  }
1194
1223
  }
@@ -1550,6 +1579,7 @@ var ReconnectionManager = class {
1550
1579
  attempt = 0;
1551
1580
  timer = null;
1552
1581
  stopped = false;
1582
+ running = false;
1553
1583
  waitResolve = null;
1554
1584
  constructor(config) {
1555
1585
  this.initialDelay = config?.initialDelay ?? 1e3;
@@ -1567,25 +1597,30 @@ var ReconnectionManager = class {
1567
1597
  */
1568
1598
  async start(onReconnect) {
1569
1599
  this.stopped = false;
1600
+ this.running = true;
1570
1601
  this.attempt = 0;
1571
- while (!this.stopped) {
1572
- if (this.maxAttempts > 0 && this.attempt >= this.maxAttempts) {
1573
- return false;
1574
- }
1575
- const delay = this.getNextDelay();
1576
- this.attempt++;
1577
- await this.wait(delay);
1578
- if (this.stopped) return false;
1579
- try {
1580
- const success = await onReconnect();
1581
- if (success) {
1582
- this.reset();
1583
- return true;
1602
+ try {
1603
+ while (!this.stopped) {
1604
+ if (this.maxAttempts > 0 && this.attempt >= this.maxAttempts) {
1605
+ return false;
1606
+ }
1607
+ const delay = this.getNextDelay();
1608
+ this.attempt++;
1609
+ await this.wait(delay);
1610
+ if (this.stopped) return false;
1611
+ try {
1612
+ const success = await onReconnect();
1613
+ if (success) {
1614
+ this.reset();
1615
+ return true;
1616
+ }
1617
+ } catch {
1584
1618
  }
1585
- } catch {
1586
1619
  }
1620
+ return false;
1621
+ } finally {
1622
+ this.running = false;
1587
1623
  }
1588
- return false;
1589
1624
  }
1590
1625
  /**
1591
1626
  * Stop any pending reconnection attempt.
@@ -1618,6 +1653,12 @@ var ReconnectionManager = class {
1618
1653
  const jitterOffset = (this.random() - 0.5) * 2 * jitterRange;
1619
1654
  return Math.max(0, Math.round(baseDelay + jitterOffset));
1620
1655
  }
1656
+ /**
1657
+ * Whether the reconnection loop is currently running.
1658
+ */
1659
+ isRunning() {
1660
+ return this.running;
1661
+ }
1621
1662
  /**
1622
1663
  * Current attempt number (for testing).
1623
1664
  */