@spacelr/sdk 0.19.5 → 0.19.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.d.mts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +116 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +116 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -796,6 +796,22 @@ async function generatePKCEChallenge() {
|
|
|
796
796
|
// libs/sdk/src/core/realtime.ts
|
|
797
797
|
import { io } from "socket.io-client";
|
|
798
798
|
var REBUILD_RETRY_DELAY_MS = 5e3;
|
|
799
|
+
var SOCKET_TOKEN_REFRESH_LEAD_MS = 45e3;
|
|
800
|
+
var SOCKET_TOKEN_REFRESH_JITTER_MS = 15e3;
|
|
801
|
+
var SOCKET_TOKEN_REFRESH_RETRY_MS = 5e3;
|
|
802
|
+
var SOCKET_TOKEN_REFRESH_RETRY_MAX_MS = 3e4;
|
|
803
|
+
var SERVER_DISCONNECT_REBUILD_DELAY_MS = 1e3;
|
|
804
|
+
function jwtExpiryMs(token) {
|
|
805
|
+
const payload = token.split(".")[1];
|
|
806
|
+
if (!payload) return null;
|
|
807
|
+
try {
|
|
808
|
+
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"));
|
|
809
|
+
const exp = JSON.parse(json).exp;
|
|
810
|
+
return typeof exp === "number" ? exp * 1e3 : null;
|
|
811
|
+
} catch {
|
|
812
|
+
return null;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
799
815
|
var PERMANENT_STREAM_ACK_ERRORS = /* @__PURE__ */ new Set([
|
|
800
816
|
"not-stream-collection",
|
|
801
817
|
"Collection not found",
|
|
@@ -986,6 +1002,18 @@ var RealtimeClient = class {
|
|
|
986
1002
|
// Scheduled retry after a failed `rebuildSocket()`; cleared when it fires
|
|
987
1003
|
// or when `disconnect()` tears down.
|
|
988
1004
|
this.rebuildRetryTimer = null;
|
|
1005
|
+
// #1078 — the access token the open socket was last authenticated with, and
|
|
1006
|
+
// the timer that replaces it before it expires.
|
|
1007
|
+
this.socketToken = null;
|
|
1008
|
+
this.socketTokenTimer = null;
|
|
1009
|
+
this.socketTokenRetries = 0;
|
|
1010
|
+
// The token of a socket the server closed. If the next connect would use the
|
|
1011
|
+
// same token again, it is renewed first (once).
|
|
1012
|
+
this.serverClosedToken = null;
|
|
1013
|
+
// `reauthenticate` messages whose `authenticated` reply is still due. Such a
|
|
1014
|
+
// reply confirms a token swap on a live socket, not a new connection, so the
|
|
1015
|
+
// subscriptions must not be sent again for it.
|
|
1016
|
+
this.pendingReauthReplies = 0;
|
|
989
1017
|
this.connectionState = "disconnected";
|
|
990
1018
|
this.connectionStateListeners = /* @__PURE__ */ new Set();
|
|
991
1019
|
this.streamSubscriptions = /* @__PURE__ */ new Map();
|
|
@@ -1229,6 +1257,7 @@ var RealtimeClient = class {
|
|
|
1229
1257
|
clearTimeout(this.rebuildRetryTimer);
|
|
1230
1258
|
this.rebuildRetryTimer = null;
|
|
1231
1259
|
}
|
|
1260
|
+
this.clearSocketTokenTimer();
|
|
1232
1261
|
if (this.unsubscribeFromTokenRefreshed) {
|
|
1233
1262
|
this.unsubscribeFromTokenRefreshed();
|
|
1234
1263
|
this.unsubscribeFromTokenRefreshed = null;
|
|
@@ -1356,10 +1385,16 @@ var RealtimeClient = class {
|
|
|
1356
1385
|
this.socket = null;
|
|
1357
1386
|
previous.disconnect();
|
|
1358
1387
|
}
|
|
1359
|
-
|
|
1388
|
+
let token = await this.config.getToken();
|
|
1389
|
+
if (token && token === this.serverClosedToken) {
|
|
1390
|
+
this.serverClosedToken = null;
|
|
1391
|
+
await this.config.refreshToken?.().catch(() => null);
|
|
1392
|
+
token = await this.config.getToken();
|
|
1393
|
+
}
|
|
1360
1394
|
if (!token) {
|
|
1361
1395
|
throw new Error("No authentication token available");
|
|
1362
1396
|
}
|
|
1397
|
+
this.socketToken = token;
|
|
1363
1398
|
const wsUrl = this.config.baseUrl.replace(/\/api\/v\d+\/?$/, "");
|
|
1364
1399
|
return new Promise((resolve, reject) => {
|
|
1365
1400
|
let initialConnect = true;
|
|
@@ -1368,6 +1403,7 @@ var RealtimeClient = class {
|
|
|
1368
1403
|
auth: async (cb) => {
|
|
1369
1404
|
try {
|
|
1370
1405
|
const freshToken = await this.config.getToken();
|
|
1406
|
+
if (freshToken) this.socketToken = freshToken;
|
|
1371
1407
|
cb({ token: freshToken });
|
|
1372
1408
|
} catch {
|
|
1373
1409
|
cb({ token: null });
|
|
@@ -1397,6 +1433,7 @@ var RealtimeClient = class {
|
|
|
1397
1433
|
if (this.disposed) return;
|
|
1398
1434
|
if (this.socket !== socket) return;
|
|
1399
1435
|
this.setConnectionState("connected");
|
|
1436
|
+
this.scheduleSocketTokenRefresh();
|
|
1400
1437
|
if (initialConnect) {
|
|
1401
1438
|
initialConnect = false;
|
|
1402
1439
|
if (this.socket) {
|
|
@@ -1429,14 +1466,14 @@ var RealtimeClient = class {
|
|
|
1429
1466
|
}
|
|
1430
1467
|
if (this.config.onTokenRefreshed && !this.unsubscribeFromTokenRefreshed) {
|
|
1431
1468
|
this.unsubscribeFromTokenRefreshed = this.config.onTokenRefreshed(
|
|
1432
|
-
(accessToken) =>
|
|
1433
|
-
this.socket?.emit("reauthenticate", { token: accessToken });
|
|
1434
|
-
}
|
|
1469
|
+
(accessToken) => this.reauthenticateSocket(accessToken)
|
|
1435
1470
|
);
|
|
1436
1471
|
}
|
|
1437
1472
|
if (this.subscriptions.size > 0) this.resubscribeAll();
|
|
1438
1473
|
if (this.streamSubscriptions.size > 0) void this.resubscribeAllStreams();
|
|
1439
1474
|
resolve();
|
|
1475
|
+
} else if (this.pendingReauthReplies > 0) {
|
|
1476
|
+
this.pendingReauthReplies -= 1;
|
|
1440
1477
|
} else {
|
|
1441
1478
|
this.resubscribeAll();
|
|
1442
1479
|
void this.resubscribeAllStreams();
|
|
@@ -1516,11 +1553,17 @@ var RealtimeClient = class {
|
|
|
1516
1553
|
}
|
|
1517
1554
|
}
|
|
1518
1555
|
});
|
|
1519
|
-
socket.on("disconnect", () => {
|
|
1556
|
+
socket.on("disconnect", (reason) => {
|
|
1557
|
+
if (this.socket === socket) this.pendingReauthReplies = 0;
|
|
1520
1558
|
if (this.disposed) return;
|
|
1521
1559
|
if (initialConnect) return;
|
|
1522
1560
|
if (this.socket !== socket) return;
|
|
1523
1561
|
this.setConnectionState("reconnecting");
|
|
1562
|
+
if (reason === "io server disconnect") {
|
|
1563
|
+
this.serverClosedToken = this.socketToken;
|
|
1564
|
+
this.clearSocketTokenTimer();
|
|
1565
|
+
this.scheduleRebuildRetry(SERVER_DISCONNECT_REBUILD_DELAY_MS);
|
|
1566
|
+
}
|
|
1524
1567
|
});
|
|
1525
1568
|
});
|
|
1526
1569
|
}
|
|
@@ -1582,7 +1625,72 @@ var RealtimeClient = class {
|
|
|
1582
1625
|
} catch {
|
|
1583
1626
|
}
|
|
1584
1627
|
}
|
|
1585
|
-
|
|
1628
|
+
/** #1078: send a new access token to the open socket, once per token. */
|
|
1629
|
+
reauthenticateSocket(accessToken) {
|
|
1630
|
+
if (accessToken === this.socketToken) return;
|
|
1631
|
+
if (!this.socket?.connected) return;
|
|
1632
|
+
this.socketToken = accessToken;
|
|
1633
|
+
this.pendingReauthReplies += 1;
|
|
1634
|
+
this.socket.emit("reauthenticate", { token: accessToken });
|
|
1635
|
+
this.scheduleSocketTokenRefresh();
|
|
1636
|
+
}
|
|
1637
|
+
clearSocketTokenTimer() {
|
|
1638
|
+
if (this.socketTokenTimer) {
|
|
1639
|
+
clearTimeout(this.socketTokenTimer);
|
|
1640
|
+
this.socketTokenTimer = null;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* #1078: keep the open socket's token fresh. A subscriber that only listens
|
|
1645
|
+
* makes no requests, so nothing else would refresh its token before the
|
|
1646
|
+
* gateway closes the socket at expiry. Shortly before expiry this asks the
|
|
1647
|
+
* token manager for a token (which refreshes it) and re-authenticates the
|
|
1648
|
+
* socket with the result. Tokens without a readable `exp` are left alone.
|
|
1649
|
+
*/
|
|
1650
|
+
scheduleSocketTokenRefresh(retryMs) {
|
|
1651
|
+
this.clearSocketTokenTimer();
|
|
1652
|
+
if (retryMs === void 0) this.socketTokenRetries = 0;
|
|
1653
|
+
if (this.disposed || !this.socketToken) return;
|
|
1654
|
+
const expiresAt = jwtExpiryMs(this.socketToken);
|
|
1655
|
+
if (expiresAt === null) return;
|
|
1656
|
+
const remaining = expiresAt - Date.now();
|
|
1657
|
+
const lead = Math.min(
|
|
1658
|
+
SOCKET_TOKEN_REFRESH_LEAD_MS + Math.random() * SOCKET_TOKEN_REFRESH_JITTER_MS,
|
|
1659
|
+
remaining / 2
|
|
1660
|
+
);
|
|
1661
|
+
const delay = retryMs ?? Math.max(1e3, remaining - lead);
|
|
1662
|
+
this.socketTokenTimer = setTimeout(() => {
|
|
1663
|
+
this.socketTokenTimer = null;
|
|
1664
|
+
void this.refreshSocketToken(expiresAt);
|
|
1665
|
+
}, delay);
|
|
1666
|
+
this.socketTokenTimer.unref?.();
|
|
1667
|
+
}
|
|
1668
|
+
async refreshSocketToken(expiresAt) {
|
|
1669
|
+
if (this.disposed || !this.socket) return;
|
|
1670
|
+
const previous = this.socketToken;
|
|
1671
|
+
let token = null;
|
|
1672
|
+
try {
|
|
1673
|
+
token = await this.config.getToken();
|
|
1674
|
+
} catch {
|
|
1675
|
+
}
|
|
1676
|
+
if (this.disposed || !this.socket) return;
|
|
1677
|
+
if (this.socketToken !== previous) return;
|
|
1678
|
+
if (token && token !== previous) {
|
|
1679
|
+
this.reauthenticateSocket(token);
|
|
1680
|
+
return;
|
|
1681
|
+
}
|
|
1682
|
+
const remaining = expiresAt - Date.now();
|
|
1683
|
+
if (remaining <= 0) return;
|
|
1684
|
+
const backoff = Math.min(
|
|
1685
|
+
SOCKET_TOKEN_REFRESH_RETRY_MS * 2 ** this.socketTokenRetries,
|
|
1686
|
+
SOCKET_TOKEN_REFRESH_RETRY_MAX_MS
|
|
1687
|
+
);
|
|
1688
|
+
this.socketTokenRetries += 1;
|
|
1689
|
+
this.scheduleSocketTokenRefresh(
|
|
1690
|
+
Math.min(backoff, Math.max(SOCKET_TOKEN_REFRESH_RETRY_MS, remaining / 2))
|
|
1691
|
+
);
|
|
1692
|
+
}
|
|
1693
|
+
scheduleRebuildRetry(delayMs = REBUILD_RETRY_DELAY_MS) {
|
|
1586
1694
|
if (this.disposed || this.rebuildRetryTimer) return;
|
|
1587
1695
|
this.rebuildRetryTimer = setTimeout(() => {
|
|
1588
1696
|
this.rebuildRetryTimer = null;
|
|
@@ -1592,7 +1700,7 @@ var RealtimeClient = class {
|
|
|
1592
1700
|
}
|
|
1593
1701
|
if (this.socket?.connected) return;
|
|
1594
1702
|
void this.rebuildSocket();
|
|
1595
|
-
},
|
|
1703
|
+
}, delayMs);
|
|
1596
1704
|
this.rebuildRetryTimer.unref?.();
|
|
1597
1705
|
}
|
|
1598
1706
|
ensureWakeListeners() {
|
|
@@ -4673,6 +4781,7 @@ function createClient(config) {
|
|
|
4673
4781
|
const realtime = new RealtimeClient({
|
|
4674
4782
|
baseUrl: config.apiUrl,
|
|
4675
4783
|
getToken: () => tokenManager.getAccessToken(),
|
|
4784
|
+
refreshToken: () => tokenManager.forceRefresh(),
|
|
4676
4785
|
onTokenRefreshed: (listener) => tokenManager.onTokenRefreshed((tokens) => listener(tokens.accessToken))
|
|
4677
4786
|
});
|
|
4678
4787
|
const notifications = new NotificationsModule(httpClient, {
|