@emeryld/rrroutes-client 2.6.4 → 2.6.6

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.cjs CHANGED
@@ -1375,63 +1375,93 @@ var SocketClient = class {
1375
1375
  });
1376
1376
  this.logSocketConfigSnapshot("constructor");
1377
1377
  }
1378
- this.onConnect = async () => {
1378
+ this.onConnect = () => {
1379
1379
  if (!this.socket) {
1380
1380
  this.dbg({
1381
1381
  type: "connection",
1382
1382
  phase: "connect_event",
1383
1383
  err: "Socket is null"
1384
1384
  });
1385
- throw new Error("Socket is null in onConnect handler");
1385
+ return;
1386
1386
  }
1387
+ const socket = this.socket;
1387
1388
  this.dbg({
1388
1389
  type: "connection",
1389
1390
  phase: "connect_event",
1390
- id: this.socket.id,
1391
+ id: socket.id,
1391
1392
  details: {
1392
- nsp: this.getNamespace(this.socket)
1393
+ nsp: this.getNamespace(socket)
1393
1394
  }
1394
1395
  });
1395
1396
  this.logSocketConfigSnapshot("connect_event");
1396
- await this.getSysEvent("sys:connect")({
1397
- socket: this.socket,
1398
- client: this
1397
+ void Promise.resolve(
1398
+ this.getSysEvent("sys:connect")({
1399
+ socket,
1400
+ client: this
1401
+ })
1402
+ ).catch((error) => {
1403
+ this.dbg({
1404
+ type: "connection",
1405
+ phase: "connect_event",
1406
+ id: socket.id,
1407
+ err: `sys:connect handler failed: ${this.formatError(error)}`,
1408
+ details: this.getVerboseDetails({
1409
+ nsp: this.getNamespace(socket),
1410
+ rawError: error
1411
+ })
1412
+ });
1399
1413
  });
1400
1414
  };
1401
- this.onReconnect = async (attempt) => {
1415
+ this.onReconnect = (attempt) => {
1402
1416
  if (!this.socket) {
1403
1417
  this.dbg({
1404
1418
  type: "connection",
1405
1419
  phase: "reconnect_event",
1406
1420
  err: "Socket is null"
1407
1421
  });
1408
- throw new Error("Socket is null in onReconnect handler");
1422
+ return;
1409
1423
  }
1424
+ const socket = this.socket;
1410
1425
  this.dbg({
1411
1426
  type: "connection",
1412
1427
  phase: "reconnect_event",
1413
1428
  attempt,
1414
- id: this.socket.id,
1429
+ id: socket.id,
1415
1430
  details: {
1416
- nsp: this.getNamespace(this.socket)
1431
+ nsp: this.getNamespace(socket)
1417
1432
  }
1418
1433
  });
1419
1434
  this.logSocketConfigSnapshot("reconnect_event");
1420
- await this.getSysEvent("sys:reconnect")({
1421
- attempt,
1422
- socket: this.socket,
1423
- client: this
1435
+ void Promise.resolve(
1436
+ this.getSysEvent("sys:reconnect")({
1437
+ attempt,
1438
+ socket,
1439
+ client: this
1440
+ })
1441
+ ).catch((error) => {
1442
+ this.dbg({
1443
+ type: "connection",
1444
+ phase: "reconnect_event",
1445
+ attempt,
1446
+ id: socket.id,
1447
+ err: `sys:reconnect handler failed: ${this.formatError(error)}`,
1448
+ details: this.getVerboseDetails({
1449
+ nsp: this.getNamespace(socket),
1450
+ rawError: error
1451
+ })
1452
+ });
1424
1453
  });
1425
1454
  };
1426
- this.onDisconnect = async (reason) => {
1455
+ this.onDisconnect = (reason) => {
1427
1456
  if (!this.socket) {
1428
1457
  this.dbg({
1429
1458
  type: "connection",
1430
1459
  phase: "disconnect_event",
1431
1460
  err: "Socket is null"
1432
1461
  });
1433
- throw new Error("Socket is null in onDisconnect handler");
1462
+ return;
1434
1463
  }
1464
+ const socket = this.socket;
1435
1465
  this.dbg({
1436
1466
  type: "connection",
1437
1467
  phase: "disconnect_event",
@@ -1441,21 +1471,36 @@ var SocketClient = class {
1441
1471
  }
1442
1472
  });
1443
1473
  this.logSocketConfigSnapshot("disconnect_event");
1444
- await this.getSysEvent("sys:disconnect")({
1445
- reason: String(reason),
1446
- socket: this.socket,
1447
- client: this
1474
+ void Promise.resolve(
1475
+ this.getSysEvent("sys:disconnect")({
1476
+ reason: String(reason),
1477
+ socket,
1478
+ client: this
1479
+ })
1480
+ ).catch((error) => {
1481
+ this.dbg({
1482
+ type: "connection",
1483
+ phase: "disconnect_event",
1484
+ reason: String(reason),
1485
+ id: socket.id,
1486
+ err: `sys:disconnect handler failed: ${this.formatError(error)}`,
1487
+ details: this.getVerboseDetails({
1488
+ roomsTracked: this.roomCounts.size,
1489
+ rawError: error
1490
+ })
1491
+ });
1448
1492
  });
1449
1493
  };
1450
- this.onConnectError = async (err) => {
1494
+ this.onConnectError = (err) => {
1451
1495
  if (!this.socket) {
1452
1496
  this.dbg({
1453
1497
  type: "connection",
1454
1498
  phase: "connect_error_event",
1455
1499
  err: "Socket is null"
1456
1500
  });
1457
- throw new Error("Socket is null in onConnectError handler");
1501
+ return;
1458
1502
  }
1503
+ const socket = this.socket;
1459
1504
  this.dbg({
1460
1505
  type: "connection",
1461
1506
  phase: "connect_error_event",
@@ -1463,21 +1508,34 @@ var SocketClient = class {
1463
1508
  details: this.getVerboseDetails({ rawError: err })
1464
1509
  });
1465
1510
  this.logSocketConfigSnapshot("connect_error_event");
1466
- await this.getSysEvent("sys:connect_error")({
1467
- error: String(err),
1468
- socket: this.socket,
1469
- client: this
1511
+ void Promise.resolve(
1512
+ this.getSysEvent("sys:connect_error")({
1513
+ error: String(err),
1514
+ socket,
1515
+ client: this
1516
+ })
1517
+ ).catch((error) => {
1518
+ this.dbg({
1519
+ type: "connection",
1520
+ phase: "connect_error_event",
1521
+ id: socket.id,
1522
+ err: `sys:connect_error handler failed: ${this.formatError(error)}`,
1523
+ details: this.getVerboseDetails({
1524
+ rawError: error
1525
+ })
1526
+ });
1470
1527
  });
1471
1528
  };
1472
- this.onPong = async (raw) => {
1529
+ this.onPong = (raw) => {
1473
1530
  if (!this.socket) {
1474
1531
  this.dbg({
1475
1532
  type: "heartbeat",
1476
1533
  phase: "pong_recv",
1477
1534
  err: "Socket is null"
1478
1535
  });
1479
- throw new Error("Socket is null in onPong handler");
1536
+ return;
1480
1537
  }
1538
+ const socket = this.socket;
1481
1539
  const parsed = this.config.pongPayload.safeParse(raw);
1482
1540
  if (!parsed.success) {
1483
1541
  this.dbg({
@@ -1494,10 +1552,21 @@ var SocketClient = class {
1494
1552
  phase: "pong_recv",
1495
1553
  payload: validated
1496
1554
  });
1497
- await this.getSysEvent("sys:pong")({
1498
- socket: this.socket,
1499
- payload: validated,
1500
- client: this
1555
+ void Promise.resolve(
1556
+ this.getSysEvent("sys:pong")({
1557
+ socket,
1558
+ payload: validated,
1559
+ client: this
1560
+ })
1561
+ ).catch((error) => {
1562
+ this.dbg({
1563
+ type: "heartbeat",
1564
+ phase: "pong_recv",
1565
+ err: `sys:pong handler failed: ${this.formatError(error)}`,
1566
+ details: this.getVerboseDetails({
1567
+ rawError: error
1568
+ })
1569
+ });
1501
1570
  });
1502
1571
  };
1503
1572
  if (this.socket) {
@@ -1558,6 +1627,15 @@ var SocketClient = class {
1558
1627
  if (!this.debug.verbose) return void 0;
1559
1628
  return details;
1560
1629
  }
1630
+ formatError(error) {
1631
+ if (error instanceof Error) return error.message;
1632
+ if (typeof error === "string") return error;
1633
+ try {
1634
+ return JSON.stringify(error);
1635
+ } catch {
1636
+ return String(error);
1637
+ }
1638
+ }
1561
1639
  getNamespace(socket) {
1562
1640
  if (!socket) return void 0;
1563
1641
  const nsp = socket.nsp;
@@ -1571,7 +1649,13 @@ var SocketClient = class {
1571
1649
  if (!d.logger) return;
1572
1650
  if (!d[e.type]) return;
1573
1651
  if (d.only && "event" in e && !d.only.includes(e.event)) return;
1574
- d.logger(e);
1652
+ try {
1653
+ d.logger(e);
1654
+ } catch (error) {
1655
+ if (this.environment === "development" && typeof console !== "undefined" && typeof console.warn === "function") {
1656
+ console.warn("[socket] debug logger threw", error);
1657
+ }
1658
+ }
1575
1659
  }
1576
1660
  /** internal stats snapshot */
1577
1661
  stats() {
@@ -1670,8 +1754,20 @@ var SocketClient = class {
1670
1754
  payload: dataToSend
1671
1755
  });
1672
1756
  };
1673
- this.hbTimer = setInterval(tick, this.hb.intervalMs);
1674
- tick();
1757
+ const runTick = () => {
1758
+ void tick().catch((error) => {
1759
+ this.dbg({
1760
+ type: "heartbeat",
1761
+ phase: "ping_emit",
1762
+ err: `sys:ping handler failed: ${this.formatError(error)}`,
1763
+ details: this.getVerboseDetails({
1764
+ rawError: error
1765
+ })
1766
+ });
1767
+ });
1768
+ };
1769
+ this.hbTimer = setInterval(runTick, this.hb.intervalMs);
1770
+ runTick();
1675
1771
  }
1676
1772
  /**
1677
1773
  * Public: stop the heartbeat loop.
@@ -1885,7 +1981,18 @@ var SocketClient = class {
1885
1981
  metadata: meta.envelope.metadata
1886
1982
  } : void 0
1887
1983
  });
1888
- handler(data, meta);
1984
+ try {
1985
+ handler(data, meta);
1986
+ } catch (error) {
1987
+ this.dbg({
1988
+ type: "receive",
1989
+ event,
1990
+ err: `handler_failed: ${this.formatError(error)}`,
1991
+ details: this.getVerboseDetails({
1992
+ rawError: error
1993
+ })
1994
+ });
1995
+ }
1889
1996
  };
1890
1997
  const wrappedDispatcher = (envelopeOrRaw) => {
1891
1998
  if (typeof envelopeOrRaw === "object" && envelopeOrRaw !== null && "eventName" in envelopeOrRaw && "sentAt" in envelopeOrRaw && "sentTo" in envelopeOrRaw && "data" in envelopeOrRaw) {
@@ -1896,7 +2003,18 @@ var SocketClient = class {
1896
2003
  event,
1897
2004
  envelope: void 0
1898
2005
  });
1899
- handler(envelopeOrRaw, void 0);
2006
+ try {
2007
+ handler(envelopeOrRaw, void 0);
2008
+ } catch (error) {
2009
+ this.dbg({
2010
+ type: "receive",
2011
+ event,
2012
+ err: `handler_failed: ${this.formatError(error)}`,
2013
+ details: this.getVerboseDetails({
2014
+ rawError: error
2015
+ })
2016
+ });
2017
+ }
1900
2018
  }
1901
2019
  };
1902
2020
  const errorWrapped = (e) => {
@@ -2045,26 +2163,48 @@ function useSocketConnection(args) {
2045
2163
  () => rooms == null ? [] : Array.isArray(rooms) ? rooms : [rooms],
2046
2164
  [rooms]
2047
2165
  );
2166
+ const reportAsyncError = React2.useCallback(
2167
+ (phase, error) => {
2168
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
2169
+ console.warn(`[socket] useSocketConnection ${phase} failed`, error);
2170
+ }
2171
+ },
2172
+ []
2173
+ );
2048
2174
  React2.useEffect(() => {
2049
2175
  if (autoJoin && normalizedRooms.length > 0)
2050
- client.joinRooms(normalizedRooms, args.joinMeta);
2176
+ void client.joinRooms(normalizedRooms, args.joinMeta).catch((error) => reportAsyncError("joinRooms", error));
2051
2177
  const unsubscribe = client.on(event, (payload, meta) => {
2052
2178
  onMessage(payload, meta);
2053
2179
  });
2054
2180
  return () => {
2055
2181
  unsubscribe();
2056
2182
  if (autoLeave && normalizedRooms.length > 0)
2057
- client.leaveRooms(normalizedRooms, args.leaveMeta);
2183
+ void client.leaveRooms(normalizedRooms, args.leaveMeta).catch((error) => reportAsyncError("leaveRooms", error));
2058
2184
  if (onCleanup) onCleanup();
2059
2185
  };
2060
- }, [client, event, onMessage, autoJoin, autoLeave, ...normalizedRooms]);
2186
+ }, [
2187
+ client,
2188
+ event,
2189
+ onMessage,
2190
+ autoJoin,
2191
+ autoLeave,
2192
+ reportAsyncError,
2193
+ ...normalizedRooms
2194
+ ]);
2061
2195
  }
2062
2196
 
2063
2197
  // src/sockets/socket.client.context.debug.ts
2064
2198
  function dbg(dbgOpts, e) {
2065
2199
  if (!dbgOpts?.logger) return;
2066
2200
  if (!dbgOpts[e.type]) return;
2067
- dbgOpts.logger(e);
2201
+ try {
2202
+ dbgOpts.logger(e);
2203
+ } catch (error) {
2204
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
2205
+ console.warn("[socket] provider debug logger threw", error);
2206
+ }
2207
+ }
2068
2208
  }
2069
2209
  function isProbablySocket(value) {
2070
2210
  if (!value || typeof value !== "object") return false;
@@ -2281,8 +2421,15 @@ function SocketProvider(props) {
2281
2421
  });
2282
2422
  return () => {
2283
2423
  if (client) {
2284
- client.destroy(destroyLeaveMetaRef.current);
2285
- dbg(providerDebugRef.current, { type: "client", phase: "destroy" });
2424
+ void Promise.resolve(client.destroy(destroyLeaveMetaRef.current)).then(() => {
2425
+ dbg(providerDebugRef.current, { type: "client", phase: "destroy" });
2426
+ }).catch((error) => {
2427
+ dbg(providerDebugRef.current, {
2428
+ type: "resolve",
2429
+ phase: "error",
2430
+ err: `client.destroy failed: ${String(error)}`
2431
+ });
2432
+ });
2286
2433
  }
2287
2434
  };
2288
2435
  }, [client]);