@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.d.mts
CHANGED
|
@@ -778,6 +778,13 @@ interface RealtimeConfig {
|
|
|
778
778
|
* dropping the connection.
|
|
779
779
|
*/
|
|
780
780
|
onTokenRefreshed?: (listener: (accessToken: string) => void) => () => void;
|
|
781
|
+
/**
|
|
782
|
+
* Optional forced renewal of the access token, whatever its remaining
|
|
783
|
+
* lifetime. Used once after the server closed the socket although its token
|
|
784
|
+
* had not expired (the token was revoked): renewing either yields a usable
|
|
785
|
+
* token or reports the lost session to the consumer right away.
|
|
786
|
+
*/
|
|
787
|
+
refreshToken?: () => Promise<string | null>;
|
|
781
788
|
}
|
|
782
789
|
type GapReason = 'outside-retention-window' | 'redis-unavailable' | 'replay-error' | 'replay-truncated' | 'evicted' | 'auth-unavailable' | 'log-reset';
|
|
783
790
|
interface StreamGapInfo {
|
|
@@ -842,6 +849,11 @@ declare class RealtimeClient {
|
|
|
842
849
|
private onOnline;
|
|
843
850
|
private disposed;
|
|
844
851
|
private rebuildRetryTimer;
|
|
852
|
+
private socketToken;
|
|
853
|
+
private socketTokenTimer;
|
|
854
|
+
private socketTokenRetries;
|
|
855
|
+
private serverClosedToken;
|
|
856
|
+
private pendingReauthReplies;
|
|
845
857
|
private connectionState;
|
|
846
858
|
private connectionStateListeners;
|
|
847
859
|
private streamSubscriptions;
|
|
@@ -978,6 +990,18 @@ declare class RealtimeClient {
|
|
|
978
990
|
* so a throwing consumer cannot cancel recovery or make subscribe reject.
|
|
979
991
|
*/
|
|
980
992
|
private keepRetrying;
|
|
993
|
+
/** #1078: send a new access token to the open socket, once per token. */
|
|
994
|
+
private reauthenticateSocket;
|
|
995
|
+
private clearSocketTokenTimer;
|
|
996
|
+
/**
|
|
997
|
+
* #1078: keep the open socket's token fresh. A subscriber that only listens
|
|
998
|
+
* makes no requests, so nothing else would refresh its token before the
|
|
999
|
+
* gateway closes the socket at expiry. Shortly before expiry this asks the
|
|
1000
|
+
* token manager for a token (which refreshes it) and re-authenticates the
|
|
1001
|
+
* socket with the result. Tokens without a readable `exp` are left alone.
|
|
1002
|
+
*/
|
|
1003
|
+
private scheduleSocketTokenRefresh;
|
|
1004
|
+
private refreshSocketToken;
|
|
981
1005
|
private scheduleRebuildRetry;
|
|
982
1006
|
private ensureWakeListeners;
|
|
983
1007
|
private detachWakeListeners;
|
package/dist/index.d.ts
CHANGED
|
@@ -778,6 +778,13 @@ interface RealtimeConfig {
|
|
|
778
778
|
* dropping the connection.
|
|
779
779
|
*/
|
|
780
780
|
onTokenRefreshed?: (listener: (accessToken: string) => void) => () => void;
|
|
781
|
+
/**
|
|
782
|
+
* Optional forced renewal of the access token, whatever its remaining
|
|
783
|
+
* lifetime. Used once after the server closed the socket although its token
|
|
784
|
+
* had not expired (the token was revoked): renewing either yields a usable
|
|
785
|
+
* token or reports the lost session to the consumer right away.
|
|
786
|
+
*/
|
|
787
|
+
refreshToken?: () => Promise<string | null>;
|
|
781
788
|
}
|
|
782
789
|
type GapReason = 'outside-retention-window' | 'redis-unavailable' | 'replay-error' | 'replay-truncated' | 'evicted' | 'auth-unavailable' | 'log-reset';
|
|
783
790
|
interface StreamGapInfo {
|
|
@@ -842,6 +849,11 @@ declare class RealtimeClient {
|
|
|
842
849
|
private onOnline;
|
|
843
850
|
private disposed;
|
|
844
851
|
private rebuildRetryTimer;
|
|
852
|
+
private socketToken;
|
|
853
|
+
private socketTokenTimer;
|
|
854
|
+
private socketTokenRetries;
|
|
855
|
+
private serverClosedToken;
|
|
856
|
+
private pendingReauthReplies;
|
|
845
857
|
private connectionState;
|
|
846
858
|
private connectionStateListeners;
|
|
847
859
|
private streamSubscriptions;
|
|
@@ -978,6 +990,18 @@ declare class RealtimeClient {
|
|
|
978
990
|
* so a throwing consumer cannot cancel recovery or make subscribe reject.
|
|
979
991
|
*/
|
|
980
992
|
private keepRetrying;
|
|
993
|
+
/** #1078: send a new access token to the open socket, once per token. */
|
|
994
|
+
private reauthenticateSocket;
|
|
995
|
+
private clearSocketTokenTimer;
|
|
996
|
+
/**
|
|
997
|
+
* #1078: keep the open socket's token fresh. A subscriber that only listens
|
|
998
|
+
* makes no requests, so nothing else would refresh its token before the
|
|
999
|
+
* gateway closes the socket at expiry. Shortly before expiry this asks the
|
|
1000
|
+
* token manager for a token (which refreshes it) and re-authenticates the
|
|
1001
|
+
* socket with the result. Tokens without a readable `exp` are left alone.
|
|
1002
|
+
*/
|
|
1003
|
+
private scheduleSocketTokenRefresh;
|
|
1004
|
+
private refreshSocketToken;
|
|
981
1005
|
private scheduleRebuildRetry;
|
|
982
1006
|
private ensureWakeListeners;
|
|
983
1007
|
private detachWakeListeners;
|
package/dist/index.js
CHANGED
|
@@ -843,6 +843,22 @@ async function generatePKCEChallenge() {
|
|
|
843
843
|
// libs/sdk/src/core/realtime.ts
|
|
844
844
|
var import_socket = require("socket.io-client");
|
|
845
845
|
var REBUILD_RETRY_DELAY_MS = 5e3;
|
|
846
|
+
var SOCKET_TOKEN_REFRESH_LEAD_MS = 45e3;
|
|
847
|
+
var SOCKET_TOKEN_REFRESH_JITTER_MS = 15e3;
|
|
848
|
+
var SOCKET_TOKEN_REFRESH_RETRY_MS = 5e3;
|
|
849
|
+
var SOCKET_TOKEN_REFRESH_RETRY_MAX_MS = 3e4;
|
|
850
|
+
var SERVER_DISCONNECT_REBUILD_DELAY_MS = 1e3;
|
|
851
|
+
function jwtExpiryMs(token) {
|
|
852
|
+
const payload = token.split(".")[1];
|
|
853
|
+
if (!payload) return null;
|
|
854
|
+
try {
|
|
855
|
+
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"));
|
|
856
|
+
const exp = JSON.parse(json).exp;
|
|
857
|
+
return typeof exp === "number" ? exp * 1e3 : null;
|
|
858
|
+
} catch {
|
|
859
|
+
return null;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
846
862
|
var PERMANENT_STREAM_ACK_ERRORS = /* @__PURE__ */ new Set([
|
|
847
863
|
"not-stream-collection",
|
|
848
864
|
"Collection not found",
|
|
@@ -1033,6 +1049,18 @@ var RealtimeClient = class {
|
|
|
1033
1049
|
// Scheduled retry after a failed `rebuildSocket()`; cleared when it fires
|
|
1034
1050
|
// or when `disconnect()` tears down.
|
|
1035
1051
|
this.rebuildRetryTimer = null;
|
|
1052
|
+
// #1078 — the access token the open socket was last authenticated with, and
|
|
1053
|
+
// the timer that replaces it before it expires.
|
|
1054
|
+
this.socketToken = null;
|
|
1055
|
+
this.socketTokenTimer = null;
|
|
1056
|
+
this.socketTokenRetries = 0;
|
|
1057
|
+
// The token of a socket the server closed. If the next connect would use the
|
|
1058
|
+
// same token again, it is renewed first (once).
|
|
1059
|
+
this.serverClosedToken = null;
|
|
1060
|
+
// `reauthenticate` messages whose `authenticated` reply is still due. Such a
|
|
1061
|
+
// reply confirms a token swap on a live socket, not a new connection, so the
|
|
1062
|
+
// subscriptions must not be sent again for it.
|
|
1063
|
+
this.pendingReauthReplies = 0;
|
|
1036
1064
|
this.connectionState = "disconnected";
|
|
1037
1065
|
this.connectionStateListeners = /* @__PURE__ */ new Set();
|
|
1038
1066
|
this.streamSubscriptions = /* @__PURE__ */ new Map();
|
|
@@ -1276,6 +1304,7 @@ var RealtimeClient = class {
|
|
|
1276
1304
|
clearTimeout(this.rebuildRetryTimer);
|
|
1277
1305
|
this.rebuildRetryTimer = null;
|
|
1278
1306
|
}
|
|
1307
|
+
this.clearSocketTokenTimer();
|
|
1279
1308
|
if (this.unsubscribeFromTokenRefreshed) {
|
|
1280
1309
|
this.unsubscribeFromTokenRefreshed();
|
|
1281
1310
|
this.unsubscribeFromTokenRefreshed = null;
|
|
@@ -1403,10 +1432,16 @@ var RealtimeClient = class {
|
|
|
1403
1432
|
this.socket = null;
|
|
1404
1433
|
previous.disconnect();
|
|
1405
1434
|
}
|
|
1406
|
-
|
|
1435
|
+
let token = await this.config.getToken();
|
|
1436
|
+
if (token && token === this.serverClosedToken) {
|
|
1437
|
+
this.serverClosedToken = null;
|
|
1438
|
+
await this.config.refreshToken?.().catch(() => null);
|
|
1439
|
+
token = await this.config.getToken();
|
|
1440
|
+
}
|
|
1407
1441
|
if (!token) {
|
|
1408
1442
|
throw new Error("No authentication token available");
|
|
1409
1443
|
}
|
|
1444
|
+
this.socketToken = token;
|
|
1410
1445
|
const wsUrl = this.config.baseUrl.replace(/\/api\/v\d+\/?$/, "");
|
|
1411
1446
|
return new Promise((resolve, reject) => {
|
|
1412
1447
|
let initialConnect = true;
|
|
@@ -1415,6 +1450,7 @@ var RealtimeClient = class {
|
|
|
1415
1450
|
auth: async (cb) => {
|
|
1416
1451
|
try {
|
|
1417
1452
|
const freshToken = await this.config.getToken();
|
|
1453
|
+
if (freshToken) this.socketToken = freshToken;
|
|
1418
1454
|
cb({ token: freshToken });
|
|
1419
1455
|
} catch {
|
|
1420
1456
|
cb({ token: null });
|
|
@@ -1444,6 +1480,7 @@ var RealtimeClient = class {
|
|
|
1444
1480
|
if (this.disposed) return;
|
|
1445
1481
|
if (this.socket !== socket) return;
|
|
1446
1482
|
this.setConnectionState("connected");
|
|
1483
|
+
this.scheduleSocketTokenRefresh();
|
|
1447
1484
|
if (initialConnect) {
|
|
1448
1485
|
initialConnect = false;
|
|
1449
1486
|
if (this.socket) {
|
|
@@ -1476,14 +1513,14 @@ var RealtimeClient = class {
|
|
|
1476
1513
|
}
|
|
1477
1514
|
if (this.config.onTokenRefreshed && !this.unsubscribeFromTokenRefreshed) {
|
|
1478
1515
|
this.unsubscribeFromTokenRefreshed = this.config.onTokenRefreshed(
|
|
1479
|
-
(accessToken) =>
|
|
1480
|
-
this.socket?.emit("reauthenticate", { token: accessToken });
|
|
1481
|
-
}
|
|
1516
|
+
(accessToken) => this.reauthenticateSocket(accessToken)
|
|
1482
1517
|
);
|
|
1483
1518
|
}
|
|
1484
1519
|
if (this.subscriptions.size > 0) this.resubscribeAll();
|
|
1485
1520
|
if (this.streamSubscriptions.size > 0) void this.resubscribeAllStreams();
|
|
1486
1521
|
resolve();
|
|
1522
|
+
} else if (this.pendingReauthReplies > 0) {
|
|
1523
|
+
this.pendingReauthReplies -= 1;
|
|
1487
1524
|
} else {
|
|
1488
1525
|
this.resubscribeAll();
|
|
1489
1526
|
void this.resubscribeAllStreams();
|
|
@@ -1563,11 +1600,17 @@ var RealtimeClient = class {
|
|
|
1563
1600
|
}
|
|
1564
1601
|
}
|
|
1565
1602
|
});
|
|
1566
|
-
socket.on("disconnect", () => {
|
|
1603
|
+
socket.on("disconnect", (reason) => {
|
|
1604
|
+
if (this.socket === socket) this.pendingReauthReplies = 0;
|
|
1567
1605
|
if (this.disposed) return;
|
|
1568
1606
|
if (initialConnect) return;
|
|
1569
1607
|
if (this.socket !== socket) return;
|
|
1570
1608
|
this.setConnectionState("reconnecting");
|
|
1609
|
+
if (reason === "io server disconnect") {
|
|
1610
|
+
this.serverClosedToken = this.socketToken;
|
|
1611
|
+
this.clearSocketTokenTimer();
|
|
1612
|
+
this.scheduleRebuildRetry(SERVER_DISCONNECT_REBUILD_DELAY_MS);
|
|
1613
|
+
}
|
|
1571
1614
|
});
|
|
1572
1615
|
});
|
|
1573
1616
|
}
|
|
@@ -1629,7 +1672,72 @@ var RealtimeClient = class {
|
|
|
1629
1672
|
} catch {
|
|
1630
1673
|
}
|
|
1631
1674
|
}
|
|
1632
|
-
|
|
1675
|
+
/** #1078: send a new access token to the open socket, once per token. */
|
|
1676
|
+
reauthenticateSocket(accessToken) {
|
|
1677
|
+
if (accessToken === this.socketToken) return;
|
|
1678
|
+
if (!this.socket?.connected) return;
|
|
1679
|
+
this.socketToken = accessToken;
|
|
1680
|
+
this.pendingReauthReplies += 1;
|
|
1681
|
+
this.socket.emit("reauthenticate", { token: accessToken });
|
|
1682
|
+
this.scheduleSocketTokenRefresh();
|
|
1683
|
+
}
|
|
1684
|
+
clearSocketTokenTimer() {
|
|
1685
|
+
if (this.socketTokenTimer) {
|
|
1686
|
+
clearTimeout(this.socketTokenTimer);
|
|
1687
|
+
this.socketTokenTimer = null;
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* #1078: keep the open socket's token fresh. A subscriber that only listens
|
|
1692
|
+
* makes no requests, so nothing else would refresh its token before the
|
|
1693
|
+
* gateway closes the socket at expiry. Shortly before expiry this asks the
|
|
1694
|
+
* token manager for a token (which refreshes it) and re-authenticates the
|
|
1695
|
+
* socket with the result. Tokens without a readable `exp` are left alone.
|
|
1696
|
+
*/
|
|
1697
|
+
scheduleSocketTokenRefresh(retryMs) {
|
|
1698
|
+
this.clearSocketTokenTimer();
|
|
1699
|
+
if (retryMs === void 0) this.socketTokenRetries = 0;
|
|
1700
|
+
if (this.disposed || !this.socketToken) return;
|
|
1701
|
+
const expiresAt = jwtExpiryMs(this.socketToken);
|
|
1702
|
+
if (expiresAt === null) return;
|
|
1703
|
+
const remaining = expiresAt - Date.now();
|
|
1704
|
+
const lead = Math.min(
|
|
1705
|
+
SOCKET_TOKEN_REFRESH_LEAD_MS + Math.random() * SOCKET_TOKEN_REFRESH_JITTER_MS,
|
|
1706
|
+
remaining / 2
|
|
1707
|
+
);
|
|
1708
|
+
const delay = retryMs ?? Math.max(1e3, remaining - lead);
|
|
1709
|
+
this.socketTokenTimer = setTimeout(() => {
|
|
1710
|
+
this.socketTokenTimer = null;
|
|
1711
|
+
void this.refreshSocketToken(expiresAt);
|
|
1712
|
+
}, delay);
|
|
1713
|
+
this.socketTokenTimer.unref?.();
|
|
1714
|
+
}
|
|
1715
|
+
async refreshSocketToken(expiresAt) {
|
|
1716
|
+
if (this.disposed || !this.socket) return;
|
|
1717
|
+
const previous = this.socketToken;
|
|
1718
|
+
let token = null;
|
|
1719
|
+
try {
|
|
1720
|
+
token = await this.config.getToken();
|
|
1721
|
+
} catch {
|
|
1722
|
+
}
|
|
1723
|
+
if (this.disposed || !this.socket) return;
|
|
1724
|
+
if (this.socketToken !== previous) return;
|
|
1725
|
+
if (token && token !== previous) {
|
|
1726
|
+
this.reauthenticateSocket(token);
|
|
1727
|
+
return;
|
|
1728
|
+
}
|
|
1729
|
+
const remaining = expiresAt - Date.now();
|
|
1730
|
+
if (remaining <= 0) return;
|
|
1731
|
+
const backoff = Math.min(
|
|
1732
|
+
SOCKET_TOKEN_REFRESH_RETRY_MS * 2 ** this.socketTokenRetries,
|
|
1733
|
+
SOCKET_TOKEN_REFRESH_RETRY_MAX_MS
|
|
1734
|
+
);
|
|
1735
|
+
this.socketTokenRetries += 1;
|
|
1736
|
+
this.scheduleSocketTokenRefresh(
|
|
1737
|
+
Math.min(backoff, Math.max(SOCKET_TOKEN_REFRESH_RETRY_MS, remaining / 2))
|
|
1738
|
+
);
|
|
1739
|
+
}
|
|
1740
|
+
scheduleRebuildRetry(delayMs = REBUILD_RETRY_DELAY_MS) {
|
|
1633
1741
|
if (this.disposed || this.rebuildRetryTimer) return;
|
|
1634
1742
|
this.rebuildRetryTimer = setTimeout(() => {
|
|
1635
1743
|
this.rebuildRetryTimer = null;
|
|
@@ -1639,7 +1747,7 @@ var RealtimeClient = class {
|
|
|
1639
1747
|
}
|
|
1640
1748
|
if (this.socket?.connected) return;
|
|
1641
1749
|
void this.rebuildSocket();
|
|
1642
|
-
},
|
|
1750
|
+
}, delayMs);
|
|
1643
1751
|
this.rebuildRetryTimer.unref?.();
|
|
1644
1752
|
}
|
|
1645
1753
|
ensureWakeListeners() {
|
|
@@ -4720,6 +4828,7 @@ function createClient(config) {
|
|
|
4720
4828
|
const realtime = new RealtimeClient({
|
|
4721
4829
|
baseUrl: config.apiUrl,
|
|
4722
4830
|
getToken: () => tokenManager.getAccessToken(),
|
|
4831
|
+
refreshToken: () => tokenManager.forceRefresh(),
|
|
4723
4832
|
onTokenRefreshed: (listener) => tokenManager.onTokenRefreshed((tokens) => listener(tokens.accessToken))
|
|
4724
4833
|
});
|
|
4725
4834
|
const notifications = new NotificationsModule(httpClient, {
|