@korajs/sync 0.1.2 → 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
@@ -61,7 +61,8 @@ function isErrorMessage(value) {
61
61
 
62
62
  // src/protocol/serializer.ts
63
63
  import { SyncError } from "@korajs/core";
64
- import { Reader, Writer } from "protobufjs/minimal";
64
+ import protobuf from "protobufjs/minimal";
65
+ var { Reader, Writer } = protobuf;
65
66
  function versionVectorToWire(vector) {
66
67
  const wire = {};
67
68
  for (const [nodeId, seq] of vector) {
@@ -590,8 +591,10 @@ var WebSocketTransport = class {
590
591
  errorHandler = null;
591
592
  serializer;
592
593
  WebSocketImpl;
594
+ connectTimeout;
593
595
  constructor(options) {
594
596
  this.serializer = options?.serializer ?? new JsonMessageSerializer();
597
+ this.connectTimeout = options?.connectTimeout ?? 1e4;
595
598
  if (options?.WebSocketImpl) {
596
599
  this.WebSocketImpl = options.WebSocketImpl;
597
600
  } else if (typeof globalThis.WebSocket !== "undefined") {
@@ -607,12 +610,38 @@ var WebSocketTransport = class {
607
610
  });
608
611
  }
609
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);
610
639
  try {
611
640
  const connectUrl = options?.authToken ? `${url}${url.includes("?") ? "&" : "?"}token=${encodeURIComponent(options.authToken)}` : url;
612
641
  const ws = new this.WebSocketImpl(connectUrl);
613
642
  this.ws = ws;
614
643
  ws.onopen = () => {
615
- resolve();
644
+ settle(() => resolve());
616
645
  };
617
646
  ws.onmessage = (event) => {
618
647
  try {
@@ -636,15 +665,17 @@ var WebSocketTransport = class {
636
665
  this.errorHandler?.(err);
637
666
  if (!this.isConnected()) {
638
667
  this.ws = null;
639
- reject(err);
668
+ settle(() => reject(err));
640
669
  }
641
670
  };
642
671
  } catch (err) {
643
- reject(
644
- err instanceof SyncError2 ? err : new SyncError2("Failed to create WebSocket", {
645
- url,
646
- error: String(err)
647
- })
672
+ settle(
673
+ () => reject(
674
+ err instanceof SyncError2 ? err : new SyncError2("Failed to create WebSocket", {
675
+ url,
676
+ error: String(err)
677
+ })
678
+ )
648
679
  );
649
680
  }
650
681
  });
@@ -1186,8 +1217,7 @@ var SyncEngine = class {
1186
1217
  };
1187
1218
  this.transport.send(handshake);
1188
1219
  } catch (err) {
1189
- this.transitionTo("error");
1190
- this.transitionTo("disconnected");
1220
+ this.ensureDisconnected();
1191
1221
  throw err;
1192
1222
  }
1193
1223
  }
@@ -1549,6 +1579,7 @@ var ReconnectionManager = class {
1549
1579
  attempt = 0;
1550
1580
  timer = null;
1551
1581
  stopped = false;
1582
+ running = false;
1552
1583
  waitResolve = null;
1553
1584
  constructor(config) {
1554
1585
  this.initialDelay = config?.initialDelay ?? 1e3;
@@ -1566,25 +1597,30 @@ var ReconnectionManager = class {
1566
1597
  */
1567
1598
  async start(onReconnect) {
1568
1599
  this.stopped = false;
1600
+ this.running = true;
1569
1601
  this.attempt = 0;
1570
- while (!this.stopped) {
1571
- if (this.maxAttempts > 0 && this.attempt >= this.maxAttempts) {
1572
- return false;
1573
- }
1574
- const delay = this.getNextDelay();
1575
- this.attempt++;
1576
- await this.wait(delay);
1577
- if (this.stopped) return false;
1578
- try {
1579
- const success = await onReconnect();
1580
- if (success) {
1581
- this.reset();
1582
- 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 {
1583
1618
  }
1584
- } catch {
1585
1619
  }
1620
+ return false;
1621
+ } finally {
1622
+ this.running = false;
1586
1623
  }
1587
- return false;
1588
1624
  }
1589
1625
  /**
1590
1626
  * Stop any pending reconnection attempt.
@@ -1617,6 +1653,12 @@ var ReconnectionManager = class {
1617
1653
  const jitterOffset = (this.random() - 0.5) * 2 * jitterRange;
1618
1654
  return Math.max(0, Math.round(baseDelay + jitterOffset));
1619
1655
  }
1656
+ /**
1657
+ * Whether the reconnection loop is currently running.
1658
+ */
1659
+ isRunning() {
1660
+ return this.running;
1661
+ }
1620
1662
  /**
1621
1663
  * Current attempt number (for testing).
1622
1664
  */