@spacelr/sdk 0.18.2 → 0.18.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.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +100 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +100 -40
- 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();
|
|
@@ -1268,10 +1301,24 @@ var RealtimeClient = class {
|
|
|
1268
1301
|
// rejects on connect_error and they decide whether to retry. Once the
|
|
1269
1302
|
// first authentication succeeds we re-enable it via the Manager's
|
|
1270
1303
|
// setters (see authenticated handler).
|
|
1271
|
-
reconnection: false
|
|
1304
|
+
reconnection: false,
|
|
1305
|
+
// #1000 — socket.io's 20 s default left a connect that hangs without
|
|
1306
|
+
// a close (browser offline mid-handshake) pending for 20 s before the
|
|
1307
|
+
// retry could start. Also applies to the later auto-reconnects.
|
|
1308
|
+
timeout: 5e3
|
|
1272
1309
|
});
|
|
1273
|
-
this.socket
|
|
1310
|
+
this.socket = socket;
|
|
1311
|
+
const failInitialConnect = (message) => {
|
|
1312
|
+
socket.disconnect();
|
|
1313
|
+
if (this.socket === socket) {
|
|
1314
|
+
this.socket = null;
|
|
1315
|
+
this.setConnectionState("disconnected");
|
|
1316
|
+
}
|
|
1317
|
+
reject(new Error(message));
|
|
1318
|
+
};
|
|
1319
|
+
socket.on("authenticated", () => {
|
|
1274
1320
|
if (this.disposed) return;
|
|
1321
|
+
if (this.socket !== socket) return;
|
|
1275
1322
|
this.setConnectionState("connected");
|
|
1276
1323
|
if (initialConnect) {
|
|
1277
1324
|
initialConnect = false;
|
|
@@ -1310,29 +1357,29 @@ var RealtimeClient = class {
|
|
|
1310
1357
|
}
|
|
1311
1358
|
);
|
|
1312
1359
|
}
|
|
1360
|
+
if (this.subscriptions.size > 0) this.resubscribeAll();
|
|
1361
|
+
if (this.streamSubscriptions.size > 0) void this.resubscribeAllStreams();
|
|
1313
1362
|
resolve();
|
|
1314
1363
|
} else {
|
|
1315
1364
|
this.resubscribeAll();
|
|
1316
1365
|
void this.resubscribeAllStreams();
|
|
1317
1366
|
}
|
|
1318
1367
|
});
|
|
1319
|
-
|
|
1368
|
+
socket.on("connect_error", (err) => {
|
|
1320
1369
|
if (initialConnect) {
|
|
1321
|
-
|
|
1370
|
+
failInitialConnect(`WebSocket connection failed: ${err.message}`);
|
|
1322
1371
|
}
|
|
1323
1372
|
});
|
|
1324
1373
|
onInitialDisconnect = (reason) => {
|
|
1325
1374
|
if (!initialConnect) return;
|
|
1326
|
-
|
|
1327
|
-
this.socket = null;
|
|
1328
|
-
this.setConnectionState("disconnected");
|
|
1329
|
-
reject(new Error(`WebSocket disconnected during handshake: ${reason}`));
|
|
1375
|
+
failInitialConnect(`WebSocket disconnected during handshake: ${reason}`);
|
|
1330
1376
|
};
|
|
1331
|
-
|
|
1332
|
-
|
|
1377
|
+
socket.on("disconnect", onInitialDisconnect);
|
|
1378
|
+
socket.io.on("reconnect_failed", () => {
|
|
1379
|
+
if (this.socket !== socket) return;
|
|
1333
1380
|
void this.rebuildSocket();
|
|
1334
1381
|
});
|
|
1335
|
-
|
|
1382
|
+
socket.on("db:event", (event) => {
|
|
1336
1383
|
const base = `db:${event.projectId}:${event.collectionName}`;
|
|
1337
1384
|
const baseCallbacks = this.subscriptions.get(base);
|
|
1338
1385
|
if (baseCallbacks) {
|
|
@@ -1354,7 +1401,7 @@ var RealtimeClient = class {
|
|
|
1354
1401
|
}
|
|
1355
1402
|
}
|
|
1356
1403
|
});
|
|
1357
|
-
|
|
1404
|
+
socket.on(
|
|
1358
1405
|
"subscription-invalidated",
|
|
1359
1406
|
(payload) => {
|
|
1360
1407
|
if (!payload?.projectId || !payload?.collectionName) return;
|
|
@@ -1366,10 +1413,10 @@ var RealtimeClient = class {
|
|
|
1366
1413
|
}
|
|
1367
1414
|
}
|
|
1368
1415
|
);
|
|
1369
|
-
|
|
1416
|
+
socket.on("event", (payload) => {
|
|
1370
1417
|
this.dispatchStreamEvent(payload).catch(() => void 0);
|
|
1371
1418
|
});
|
|
1372
|
-
|
|
1419
|
+
socket.on("event-gap", (info) => {
|
|
1373
1420
|
const streamKey = `stream:${info.projectId}:${info.collectionName}`;
|
|
1374
1421
|
for (const set of this.streamSetsForStreamKey(streamKey)) {
|
|
1375
1422
|
for (const state of set) {
|
|
@@ -1377,7 +1424,7 @@ var RealtimeClient = class {
|
|
|
1377
1424
|
}
|
|
1378
1425
|
}
|
|
1379
1426
|
});
|
|
1380
|
-
|
|
1427
|
+
socket.on("subscription-reset", (info) => {
|
|
1381
1428
|
if (!info || typeof info.projectId !== "string") return;
|
|
1382
1429
|
for (const set of this.streamSubscriptions.values()) {
|
|
1383
1430
|
for (const state of set) {
|
|
@@ -1392,10 +1439,10 @@ var RealtimeClient = class {
|
|
|
1392
1439
|
}
|
|
1393
1440
|
}
|
|
1394
1441
|
});
|
|
1395
|
-
|
|
1442
|
+
socket.on("disconnect", () => {
|
|
1396
1443
|
if (this.disposed) return;
|
|
1397
1444
|
if (initialConnect) return;
|
|
1398
|
-
if (
|
|
1445
|
+
if (this.socket !== socket) return;
|
|
1399
1446
|
this.setConnectionState("reconnecting");
|
|
1400
1447
|
});
|
|
1401
1448
|
});
|
|
@@ -1443,17 +1490,30 @@ var RealtimeClient = class {
|
|
|
1443
1490
|
newSocket?.disconnect();
|
|
1444
1491
|
return;
|
|
1445
1492
|
}
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1493
|
+
}
|
|
1494
|
+
/**
|
|
1495
|
+
* A subscription was kept after a failed connect (#983): schedule the retry,
|
|
1496
|
+
* report 'reconnecting' (one is now pending; 'disconnected' means nothing
|
|
1497
|
+
* will retry), then tell the caller once. The handler runs last and guarded,
|
|
1498
|
+
* so a throwing consumer cannot cancel recovery or make subscribe reject.
|
|
1499
|
+
*/
|
|
1500
|
+
keepRetrying(error, onError) {
|
|
1501
|
+
this.scheduleRebuildRetry();
|
|
1502
|
+
this.setConnectionState("reconnecting");
|
|
1503
|
+
try {
|
|
1504
|
+
onError?.(error);
|
|
1505
|
+
} catch {
|
|
1451
1506
|
}
|
|
1452
1507
|
}
|
|
1453
1508
|
scheduleRebuildRetry() {
|
|
1454
1509
|
if (this.disposed || this.rebuildRetryTimer) return;
|
|
1455
1510
|
this.rebuildRetryTimer = setTimeout(() => {
|
|
1456
1511
|
this.rebuildRetryTimer = null;
|
|
1512
|
+
if (this.connecting) {
|
|
1513
|
+
this.scheduleRebuildRetry();
|
|
1514
|
+
return;
|
|
1515
|
+
}
|
|
1516
|
+
if (this.socket?.connected) return;
|
|
1457
1517
|
void this.rebuildSocket();
|
|
1458
1518
|
}, REBUILD_RETRY_DELAY_MS);
|
|
1459
1519
|
this.rebuildRetryTimer.unref?.();
|