@spacelr/sdk 0.18.1 → 0.18.3
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 +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +103 -45
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +103 -45
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -931,7 +931,7 @@ var RealtimeClient = class {
|
|
|
931
931
|
if (this.connectionState === "disconnected") {
|
|
932
932
|
this.setConnectionState("reconnecting");
|
|
933
933
|
}
|
|
934
|
-
await this.
|
|
934
|
+
const connectError = await this.tryEnsureConnected();
|
|
935
935
|
const room = this.buildRoomKey(projectId, collectionName, where);
|
|
936
936
|
if (!this.subscriptions.has(room)) {
|
|
937
937
|
this.subscriptions.set(room, /* @__PURE__ */ new Set());
|
|
@@ -956,16 +956,19 @@ var RealtimeClient = class {
|
|
|
956
956
|
if (where && Object.keys(where).length > 0) {
|
|
957
957
|
payload["where"] = where;
|
|
958
958
|
}
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
959
|
+
if (!connectError) {
|
|
960
|
+
this.socket?.emit(
|
|
961
|
+
"subscribe",
|
|
962
|
+
payload,
|
|
963
|
+
(response) => {
|
|
964
|
+
if (response.error && this.roomGeneration.get(room) === generation) {
|
|
965
|
+
this.notifyRoomError(room, this.toSubscribeError(response));
|
|
966
|
+
}
|
|
965
967
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
+
);
|
|
969
|
+
}
|
|
968
970
|
}
|
|
971
|
+
if (connectError) this.keepRetrying(connectError, onError);
|
|
969
972
|
return () => {
|
|
970
973
|
const callbacks2 = this.subscriptions.get(room);
|
|
971
974
|
if (callbacks2) {
|
|
@@ -996,7 +999,7 @@ var RealtimeClient = class {
|
|
|
996
999
|
* event rather than the originally-subscribed `sinceId`.
|
|
997
1000
|
*/
|
|
998
1001
|
async subscribeWithCursor(options) {
|
|
999
|
-
const { unsubscribe } = await this._subscribeInternal(options);
|
|
1002
|
+
const { unsubscribe } = await this._subscribeInternal(options, true);
|
|
1000
1003
|
return unsubscribe;
|
|
1001
1004
|
}
|
|
1002
1005
|
/**
|
|
@@ -1057,12 +1060,17 @@ var RealtimeClient = class {
|
|
|
1057
1060
|
* only — `emitSubscribeEvents` itself always resolves, so this
|
|
1058
1061
|
* catch should never fire on the success path.
|
|
1059
1062
|
*/
|
|
1060
|
-
async _subscribeInternal(options) {
|
|
1063
|
+
async _subscribeInternal(options, keepOnConnectFailure = false) {
|
|
1061
1064
|
this.ensureWakeListeners();
|
|
1062
1065
|
if (this.connectionState === "disconnected") {
|
|
1063
1066
|
this.setConnectionState("reconnecting");
|
|
1064
1067
|
}
|
|
1065
|
-
|
|
1068
|
+
let connectError = null;
|
|
1069
|
+
if (keepOnConnectFailure) {
|
|
1070
|
+
connectError = await this.tryEnsureConnected();
|
|
1071
|
+
} else {
|
|
1072
|
+
await this.ensureConnected();
|
|
1073
|
+
}
|
|
1066
1074
|
const streamKey = `stream:${options.projectId}:${options.collectionName}`;
|
|
1067
1075
|
const subscriptionKey = this.streamSubscriptionKey(
|
|
1068
1076
|
streamKey,
|
|
@@ -1088,14 +1096,21 @@ var RealtimeClient = class {
|
|
|
1088
1096
|
subscriptionKey,
|
|
1089
1097
|
dispatchQueue: Promise.resolve()
|
|
1090
1098
|
};
|
|
1099
|
+
const syntheticAck = {
|
|
1100
|
+
subscribed: streamKey,
|
|
1101
|
+
realtimeMode: "stream",
|
|
1102
|
+
latestStreamId: null
|
|
1103
|
+
};
|
|
1104
|
+
if (connectError) {
|
|
1105
|
+
const kept = this.streamSubscriptions.get(subscriptionKey) ?? /* @__PURE__ */ new Set();
|
|
1106
|
+
kept.add(state);
|
|
1107
|
+
this.streamSubscriptions.set(subscriptionKey, kept);
|
|
1108
|
+
this.keepRetrying(connectError, options.onError);
|
|
1109
|
+
return { unsubscribe: () => this.unsubscribeStream(state), ack: syntheticAck };
|
|
1110
|
+
}
|
|
1091
1111
|
const existing = this.streamSubscriptions.get(subscriptionKey);
|
|
1092
1112
|
if (existing && existing.size > 0) {
|
|
1093
1113
|
existing.add(state);
|
|
1094
|
-
const syntheticAck = {
|
|
1095
|
-
subscribed: streamKey,
|
|
1096
|
-
realtimeMode: "stream",
|
|
1097
|
-
latestStreamId: null
|
|
1098
|
-
};
|
|
1099
1114
|
return {
|
|
1100
1115
|
unsubscribe: () => this.unsubscribeStream(state),
|
|
1101
1116
|
ack: syntheticAck
|
|
@@ -1245,7 +1260,25 @@ var RealtimeClient = class {
|
|
|
1245
1260
|
this.connecting = null;
|
|
1246
1261
|
}
|
|
1247
1262
|
}
|
|
1263
|
+
/**
|
|
1264
|
+
* `ensureConnected` that returns the failure instead of throwing (#983).
|
|
1265
|
+
* A disposed client still throws: nothing may be kept or retried on it.
|
|
1266
|
+
*/
|
|
1267
|
+
async tryEnsureConnected() {
|
|
1268
|
+
try {
|
|
1269
|
+
await this.ensureConnected();
|
|
1270
|
+
return null;
|
|
1271
|
+
} catch (err) {
|
|
1272
|
+
if (this.disposed) throw err;
|
|
1273
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1248
1276
|
async connect() {
|
|
1277
|
+
const previous = this.socket;
|
|
1278
|
+
if (previous) {
|
|
1279
|
+
this.socket = null;
|
|
1280
|
+
previous.disconnect();
|
|
1281
|
+
}
|
|
1249
1282
|
const token = await this.config.getToken();
|
|
1250
1283
|
if (!token) {
|
|
1251
1284
|
throw new Error("No authentication token available");
|
|
@@ -1254,7 +1287,7 @@ var RealtimeClient = class {
|
|
|
1254
1287
|
return new Promise((resolve, reject) => {
|
|
1255
1288
|
let initialConnect = true;
|
|
1256
1289
|
let onInitialDisconnect = null;
|
|
1257
|
-
|
|
1290
|
+
const socket = io(`${wsUrl}/database`, {
|
|
1258
1291
|
auth: async (cb) => {
|
|
1259
1292
|
try {
|
|
1260
1293
|
const freshToken = await this.config.getToken();
|
|
@@ -1270,8 +1303,18 @@ var RealtimeClient = class {
|
|
|
1270
1303
|
// setters (see authenticated handler).
|
|
1271
1304
|
reconnection: false
|
|
1272
1305
|
});
|
|
1273
|
-
this.socket
|
|
1306
|
+
this.socket = socket;
|
|
1307
|
+
const failInitialConnect = (message) => {
|
|
1308
|
+
socket.disconnect();
|
|
1309
|
+
if (this.socket === socket) {
|
|
1310
|
+
this.socket = null;
|
|
1311
|
+
this.setConnectionState("disconnected");
|
|
1312
|
+
}
|
|
1313
|
+
reject(new Error(message));
|
|
1314
|
+
};
|
|
1315
|
+
socket.on("authenticated", () => {
|
|
1274
1316
|
if (this.disposed) return;
|
|
1317
|
+
if (this.socket !== socket) return;
|
|
1275
1318
|
this.setConnectionState("connected");
|
|
1276
1319
|
if (initialConnect) {
|
|
1277
1320
|
initialConnect = false;
|
|
@@ -1310,29 +1353,29 @@ var RealtimeClient = class {
|
|
|
1310
1353
|
}
|
|
1311
1354
|
);
|
|
1312
1355
|
}
|
|
1356
|
+
if (this.subscriptions.size > 0) this.resubscribeAll();
|
|
1357
|
+
if (this.streamSubscriptions.size > 0) void this.resubscribeAllStreams();
|
|
1313
1358
|
resolve();
|
|
1314
1359
|
} else {
|
|
1315
1360
|
this.resubscribeAll();
|
|
1316
1361
|
void this.resubscribeAllStreams();
|
|
1317
1362
|
}
|
|
1318
1363
|
});
|
|
1319
|
-
|
|
1364
|
+
socket.on("connect_error", (err) => {
|
|
1320
1365
|
if (initialConnect) {
|
|
1321
|
-
|
|
1366
|
+
failInitialConnect(`WebSocket connection failed: ${err.message}`);
|
|
1322
1367
|
}
|
|
1323
1368
|
});
|
|
1324
1369
|
onInitialDisconnect = (reason) => {
|
|
1325
1370
|
if (!initialConnect) return;
|
|
1326
|
-
|
|
1327
|
-
this.socket = null;
|
|
1328
|
-
this.setConnectionState("disconnected");
|
|
1329
|
-
reject(new Error(`WebSocket disconnected during handshake: ${reason}`));
|
|
1371
|
+
failInitialConnect(`WebSocket disconnected during handshake: ${reason}`);
|
|
1330
1372
|
};
|
|
1331
|
-
|
|
1332
|
-
|
|
1373
|
+
socket.on("disconnect", onInitialDisconnect);
|
|
1374
|
+
socket.io.on("reconnect_failed", () => {
|
|
1375
|
+
if (this.socket !== socket) return;
|
|
1333
1376
|
void this.rebuildSocket();
|
|
1334
1377
|
});
|
|
1335
|
-
|
|
1378
|
+
socket.on("db:event", (event) => {
|
|
1336
1379
|
const base = `db:${event.projectId}:${event.collectionName}`;
|
|
1337
1380
|
const baseCallbacks = this.subscriptions.get(base);
|
|
1338
1381
|
if (baseCallbacks) {
|
|
@@ -1354,7 +1397,7 @@ var RealtimeClient = class {
|
|
|
1354
1397
|
}
|
|
1355
1398
|
}
|
|
1356
1399
|
});
|
|
1357
|
-
|
|
1400
|
+
socket.on(
|
|
1358
1401
|
"subscription-invalidated",
|
|
1359
1402
|
(payload) => {
|
|
1360
1403
|
if (!payload?.projectId || !payload?.collectionName) return;
|
|
@@ -1366,10 +1409,10 @@ var RealtimeClient = class {
|
|
|
1366
1409
|
}
|
|
1367
1410
|
}
|
|
1368
1411
|
);
|
|
1369
|
-
|
|
1412
|
+
socket.on("event", (payload) => {
|
|
1370
1413
|
this.dispatchStreamEvent(payload).catch(() => void 0);
|
|
1371
1414
|
});
|
|
1372
|
-
|
|
1415
|
+
socket.on("event-gap", (info) => {
|
|
1373
1416
|
const streamKey = `stream:${info.projectId}:${info.collectionName}`;
|
|
1374
1417
|
for (const set of this.streamSetsForStreamKey(streamKey)) {
|
|
1375
1418
|
for (const state of set) {
|
|
@@ -1377,7 +1420,7 @@ var RealtimeClient = class {
|
|
|
1377
1420
|
}
|
|
1378
1421
|
}
|
|
1379
1422
|
});
|
|
1380
|
-
|
|
1423
|
+
socket.on("subscription-reset", (info) => {
|
|
1381
1424
|
if (!info || typeof info.projectId !== "string") return;
|
|
1382
1425
|
for (const set of this.streamSubscriptions.values()) {
|
|
1383
1426
|
for (const state of set) {
|
|
@@ -1392,10 +1435,10 @@ var RealtimeClient = class {
|
|
|
1392
1435
|
}
|
|
1393
1436
|
}
|
|
1394
1437
|
});
|
|
1395
|
-
|
|
1438
|
+
socket.on("disconnect", () => {
|
|
1396
1439
|
if (this.disposed) return;
|
|
1397
1440
|
if (initialConnect) return;
|
|
1398
|
-
if (
|
|
1441
|
+
if (this.socket !== socket) return;
|
|
1399
1442
|
this.setConnectionState("reconnecting");
|
|
1400
1443
|
});
|
|
1401
1444
|
});
|
|
@@ -1443,17 +1486,30 @@ var RealtimeClient = class {
|
|
|
1443
1486
|
newSocket?.disconnect();
|
|
1444
1487
|
return;
|
|
1445
1488
|
}
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1489
|
+
}
|
|
1490
|
+
/**
|
|
1491
|
+
* A subscription was kept after a failed connect (#983): schedule the retry,
|
|
1492
|
+
* report 'reconnecting' (one is now pending; 'disconnected' means nothing
|
|
1493
|
+
* will retry), then tell the caller once. The handler runs last and guarded,
|
|
1494
|
+
* so a throwing consumer cannot cancel recovery or make subscribe reject.
|
|
1495
|
+
*/
|
|
1496
|
+
keepRetrying(error, onError) {
|
|
1497
|
+
this.scheduleRebuildRetry();
|
|
1498
|
+
this.setConnectionState("reconnecting");
|
|
1499
|
+
try {
|
|
1500
|
+
onError?.(error);
|
|
1501
|
+
} catch {
|
|
1451
1502
|
}
|
|
1452
1503
|
}
|
|
1453
1504
|
scheduleRebuildRetry() {
|
|
1454
1505
|
if (this.disposed || this.rebuildRetryTimer) return;
|
|
1455
1506
|
this.rebuildRetryTimer = setTimeout(() => {
|
|
1456
1507
|
this.rebuildRetryTimer = null;
|
|
1508
|
+
if (this.connecting) {
|
|
1509
|
+
this.scheduleRebuildRetry();
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
if (this.socket?.connected) return;
|
|
1457
1513
|
void this.rebuildSocket();
|
|
1458
1514
|
}, REBUILD_RETRY_DELAY_MS);
|
|
1459
1515
|
this.rebuildRetryTimer.unref?.();
|
|
@@ -1592,6 +1648,7 @@ var RealtimeClient = class {
|
|
|
1592
1648
|
captureDurableHandshake(state, ack) {
|
|
1593
1649
|
if (ack.generation !== void 0) state.generation = ack.generation;
|
|
1594
1650
|
if (ack.session !== void 0) state.session = ack.session;
|
|
1651
|
+
this.scheduleDurableAck(state);
|
|
1595
1652
|
}
|
|
1596
1653
|
/**
|
|
1597
1654
|
* #768 — schedule a coalesced durable ack. Only durable subscriptions (a
|
|
@@ -1714,6 +1771,10 @@ var RealtimeClient = class {
|
|
|
1714
1771
|
}
|
|
1715
1772
|
}
|
|
1716
1773
|
async resubscribeOne(primary, set) {
|
|
1774
|
+
for (const s of set) {
|
|
1775
|
+
s.generation = void 0;
|
|
1776
|
+
s.session = void 0;
|
|
1777
|
+
}
|
|
1717
1778
|
try {
|
|
1718
1779
|
const ack = await this.emitSubscribeEvents(primary);
|
|
1719
1780
|
if (ack.error) {
|
|
@@ -1726,12 +1787,9 @@ var RealtimeClient = class {
|
|
|
1726
1787
|
}
|
|
1727
1788
|
return;
|
|
1728
1789
|
}
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
}
|
|
1733
|
-
this.captureDurableHandshake(primary, ack);
|
|
1734
|
-
if (!this.streamSubscriptions.has(primary.subscriptionKey)) {
|
|
1790
|
+
if (this.streamSubscriptions.has(primary.subscriptionKey)) {
|
|
1791
|
+
this.captureDurableHandshake(primary, ack);
|
|
1792
|
+
} else {
|
|
1735
1793
|
const payload = {
|
|
1736
1794
|
projectId: primary.options.projectId,
|
|
1737
1795
|
collectionName: primary.options.collectionName
|