@spacelr/sdk 0.10.11 → 0.12.0
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 +150 -10
- package/dist/index.d.ts +150 -10
- package/dist/index.js +279 -44
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +276 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
BrowserTokenStorage: () => BrowserTokenStorage,
|
|
24
|
+
CONSUMER_ID_STORAGE_KEY: () => CONSUMER_ID_STORAGE_KEY,
|
|
24
25
|
CodeChallengeMethod: () => CodeChallengeMethod,
|
|
25
26
|
CursorInvalidError: () => CursorInvalidError,
|
|
26
27
|
FileVisibility: () => FileVisibility,
|
|
@@ -45,7 +46,8 @@ __export(index_exports, {
|
|
|
45
46
|
createClient: () => createClient,
|
|
46
47
|
generatePKCEChallenge: () => generatePKCEChallenge,
|
|
47
48
|
localStorageCursorStorage: () => localStorageCursorStorage,
|
|
48
|
-
memoryCursorStorage: () => memoryCursorStorage
|
|
49
|
+
memoryCursorStorage: () => memoryCursorStorage,
|
|
50
|
+
resolveConsumerId: () => resolveConsumerId
|
|
49
51
|
});
|
|
50
52
|
module.exports = __toCommonJS(index_exports);
|
|
51
53
|
|
|
@@ -215,17 +217,27 @@ var HttpClient = class {
|
|
|
215
217
|
* caller should retry with a refreshed token. 403 is never routed here —
|
|
216
218
|
* it means "forbidden for this action" and must not trigger logout.
|
|
217
219
|
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
+
* Auth-loss ownership (#759 review):
|
|
221
|
+
* - When a refresh WAS possible (a refresh token existed), `doRefresh` owns
|
|
222
|
+
* the outcome: it clears + emits on a server-confirmed rejection, and stays
|
|
223
|
+
* silent on a transient transport failure. We must not second-guess it, so
|
|
224
|
+
* we do NOT emit here — that avoids a double clear/emit and stops a transient
|
|
225
|
+
* blip during a 401 retry from ejecting the user.
|
|
226
|
+
* - When no refresh was possible (no refresh token), this 401 IS the confirmed
|
|
227
|
+
* loss and we own it: `notifyAuthLost` clears the dead session then emits.
|
|
228
|
+
* The single clear always runs before any listener, so a listener that
|
|
229
|
+
* re-logs-in via `setTokens()` can never have its fresh session erased.
|
|
220
230
|
*/
|
|
221
231
|
async recoverFromAuthFailure(isRetry) {
|
|
222
232
|
if (isRetry) {
|
|
223
|
-
this.tokenManager.
|
|
233
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
224
234
|
return false;
|
|
225
235
|
}
|
|
236
|
+
const hadRefreshToken = await this.tokenManager.hasRefreshToken();
|
|
226
237
|
const refreshed = await this.tokenManager.forceRefresh();
|
|
227
238
|
if (refreshed) return true;
|
|
228
|
-
|
|
239
|
+
if (hadRefreshToken) return false;
|
|
240
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
229
241
|
return false;
|
|
230
242
|
}
|
|
231
243
|
async uploadFormOnce(url, headers, formData, timeoutMs) {
|
|
@@ -457,6 +469,11 @@ var TokenManager = class {
|
|
|
457
469
|
// that hits `/auth/logout` with a dead token) don't re-emit and loop.
|
|
458
470
|
// Cleared by setTokens() / clearTokens() to re-arm for the next session.
|
|
459
471
|
this.authLostEmitted = false;
|
|
472
|
+
// The in-flight auth-loss storage wipe, if any. `setTokens` awaits it before
|
|
473
|
+
// writing so a re-login that lands DURING the wipe (e.g. an external
|
|
474
|
+
// auth-components bridge calling setTokens concurrently with a background
|
|
475
|
+
// refresh failure) is applied AFTER the wipe and never erased (#759 review).
|
|
476
|
+
this.pendingClear = null;
|
|
460
477
|
this.storage = storage ?? new MemoryTokenStorage();
|
|
461
478
|
this.refreshBufferSeconds = refreshBufferSeconds;
|
|
462
479
|
this.refreshLockName = refreshLockName;
|
|
@@ -478,6 +495,7 @@ var TokenManager = class {
|
|
|
478
495
|
return tokens.accessToken;
|
|
479
496
|
}
|
|
480
497
|
async setTokens(tokens) {
|
|
498
|
+
if (this.pendingClear) await this.pendingClear;
|
|
481
499
|
const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
|
|
482
500
|
const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
|
|
483
501
|
await this.storage.setTokens(normalised);
|
|
@@ -510,6 +528,16 @@ var TokenManager = class {
|
|
|
510
528
|
const refreshed = await this.tryRefresh(tokens);
|
|
511
529
|
return refreshed?.accessToken ?? null;
|
|
512
530
|
}
|
|
531
|
+
/**
|
|
532
|
+
* Whether the stored session currently carries a refresh token. Used by the
|
|
533
|
+
* 401-recovery path to decide whether a refresh was even possible, so it can
|
|
534
|
+
* let `doRefresh` own the auth-loss classification instead of second-guessing
|
|
535
|
+
* it. A rejecting storage backend reads as "no refresh token".
|
|
536
|
+
*/
|
|
537
|
+
async hasRefreshToken() {
|
|
538
|
+
const tokens = await this.safeGetTokens();
|
|
539
|
+
return !!tokens?.refreshToken;
|
|
540
|
+
}
|
|
513
541
|
onTokenRefreshed(listener) {
|
|
514
542
|
this.tokenRefreshedListeners.add(listener);
|
|
515
543
|
return () => this.tokenRefreshedListeners.delete(listener);
|
|
@@ -521,6 +549,9 @@ var TokenManager = class {
|
|
|
521
549
|
emitAuthLost(reason) {
|
|
522
550
|
if (this.authLostEmitted) return;
|
|
523
551
|
this.authLostEmitted = true;
|
|
552
|
+
this.dispatchAuthLost(reason);
|
|
553
|
+
}
|
|
554
|
+
dispatchAuthLost(reason) {
|
|
524
555
|
for (const listener of this.authLostListeners) {
|
|
525
556
|
try {
|
|
526
557
|
listener(reason);
|
|
@@ -528,6 +559,57 @@ var TokenManager = class {
|
|
|
528
559
|
}
|
|
529
560
|
}
|
|
530
561
|
}
|
|
562
|
+
/**
|
|
563
|
+
* Handle a server-confirmed auth loss: drop the dead session, THEN notify.
|
|
564
|
+
*
|
|
565
|
+
* The stored tokens are cleared **before** listeners run (#759) so that a
|
|
566
|
+
* reload lands on the login screen and `hasStoredSession()` reads false — and,
|
|
567
|
+
* critically, so a listener that re-logs-in via `setTokens()` (synchronously
|
|
568
|
+
* OR after an await) writes its new session strictly AFTER the clear and can
|
|
569
|
+
* never be erased by it. The clear is a storage-only wipe: we do NOT reset
|
|
570
|
+
* `authLostEmitted` (as `clearTokens()` would), so the emit stays deduped for
|
|
571
|
+
* this lost session. A rejecting storage backend is swallowed — best-effort,
|
|
572
|
+
* the consumer is routed to logout regardless. `refreshPromise` is left to
|
|
573
|
+
* `doRefresh`'s own `finally`; nulling it here could clobber a newer in-flight
|
|
574
|
+
* refresh.
|
|
575
|
+
*
|
|
576
|
+
* The token-clearing auth-loss path. Called from `doRefresh` (single-flighted
|
|
577
|
+
* via `refreshPromise`) on a server-confirmed refresh rejection, and from
|
|
578
|
+
* `HttpClient` only when a 401 could not be refreshed AND no refresh was even
|
|
579
|
+
* attempted (no refresh token) — these are mutually exclusive, so there is
|
|
580
|
+
* exactly one clear per loss and it runs BEFORE any listener. A re-login a
|
|
581
|
+
* listener performs is therefore never erased.
|
|
582
|
+
*/
|
|
583
|
+
async notifyAuthLost(reason) {
|
|
584
|
+
if (this.authLostEmitted) return;
|
|
585
|
+
this.authLostEmitted = true;
|
|
586
|
+
this.pendingClear = this.storage.clearTokens().catch(() => {
|
|
587
|
+
});
|
|
588
|
+
await this.pendingClear;
|
|
589
|
+
this.pendingClear = null;
|
|
590
|
+
this.dispatchAuthLost(reason);
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* A refresh failure that is transient (retryable), not a server-confirmed auth
|
|
594
|
+
* loss — so it must NOT eject a local-first app; the next request retries.
|
|
595
|
+
* Covers:
|
|
596
|
+
* - a transport failure (offline, tunnel, captive portal, unreachable
|
|
597
|
+
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
598
|
+
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
599
|
+
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
600
|
+
* a token rejection.
|
|
601
|
+
* A real auth rejection (401/invalid_grant) or any other non-5xx server error
|
|
602
|
+
* is NOT transient and is treated as a server-confirmed loss.
|
|
603
|
+
*/
|
|
604
|
+
isTransientTransportError(error) {
|
|
605
|
+
if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
|
|
606
|
+
return true;
|
|
607
|
+
}
|
|
608
|
+
if (error instanceof SpacelrError && typeof error.statusCode === "number") {
|
|
609
|
+
return error.statusCode >= 500 && error.statusCode <= 599;
|
|
610
|
+
}
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
531
613
|
isTokenExpired(tokens) {
|
|
532
614
|
if (!tokens.expiresAt) return false;
|
|
533
615
|
return Date.now() >= tokens.expiresAt * 1e3;
|
|
@@ -566,15 +648,18 @@ var TokenManager = class {
|
|
|
566
648
|
return current;
|
|
567
649
|
}
|
|
568
650
|
const activeRefreshToken = current?.refreshToken || refreshToken;
|
|
651
|
+
let newTokens;
|
|
569
652
|
try {
|
|
570
|
-
|
|
571
|
-
await this.storage.setTokens(newTokens);
|
|
572
|
-
this.emitTokenRefreshed(newTokens);
|
|
573
|
-
return newTokens;
|
|
653
|
+
newTokens = await callback(activeRefreshToken);
|
|
574
654
|
} catch (error) {
|
|
575
|
-
this.
|
|
655
|
+
if (!this.isTransientTransportError(error)) {
|
|
656
|
+
await this.notifyAuthLost("refresh-failed");
|
|
657
|
+
}
|
|
576
658
|
throw error;
|
|
577
659
|
}
|
|
660
|
+
await this.storage.setTokens(newTokens);
|
|
661
|
+
this.emitTokenRefreshed(newTokens);
|
|
662
|
+
return newTokens;
|
|
578
663
|
}
|
|
579
664
|
async safeGetTokens() {
|
|
580
665
|
try {
|
|
@@ -968,7 +1053,8 @@ var RealtimeClient = class {
|
|
|
968
1053
|
// gateways that haven't been updated yet are treated as pubsub-mode
|
|
969
1054
|
// so the SDK helper's fail-fast still fires correctly.
|
|
970
1055
|
realtimeMode: ack.realtimeMode ?? "pubsub",
|
|
971
|
-
latestStreamId: ack.latestStreamId ?? null
|
|
1056
|
+
latestStreamId: ack.latestStreamId ?? null,
|
|
1057
|
+
resumeCursor: ack.resumeCursor ?? null
|
|
972
1058
|
};
|
|
973
1059
|
}
|
|
974
1060
|
/**
|
|
@@ -1002,7 +1088,23 @@ var RealtimeClient = class {
|
|
|
1002
1088
|
}
|
|
1003
1089
|
await this.ensureConnected();
|
|
1004
1090
|
const streamKey = `stream:${options.projectId}:${options.collectionName}`;
|
|
1005
|
-
const subscriptionKey = this.streamSubscriptionKey(
|
|
1091
|
+
const subscriptionKey = this.streamSubscriptionKey(
|
|
1092
|
+
streamKey,
|
|
1093
|
+
options.where,
|
|
1094
|
+
options.consumerId
|
|
1095
|
+
);
|
|
1096
|
+
if (options.consumerId) {
|
|
1097
|
+
const base = this.streamSubscriptionKey(streamKey, options.where);
|
|
1098
|
+
const mine = `${base}#${options.consumerId}`;
|
|
1099
|
+
for (const key of this.streamSubscriptions.keys()) {
|
|
1100
|
+
if (key.startsWith(`${base}#`) && key !== mine) {
|
|
1101
|
+
throw new SpacelrError(
|
|
1102
|
+
`A durable subscription for ${base} already exists on this client under a different consumerId. Use one consumerId per (collection, filter) per client.`,
|
|
1103
|
+
"DURABLE_CONSUMER_CONFLICT"
|
|
1104
|
+
);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1006
1108
|
const state = {
|
|
1007
1109
|
options,
|
|
1008
1110
|
lastDeliveredId: options.sinceId ?? null,
|
|
@@ -1035,6 +1137,7 @@ var RealtimeClient = class {
|
|
|
1035
1137
|
options.onError?.(err);
|
|
1036
1138
|
throw err;
|
|
1037
1139
|
}
|
|
1140
|
+
this.captureDurableHandshake(state, ack);
|
|
1038
1141
|
return {
|
|
1039
1142
|
unsubscribe: () => this.unsubscribeStream(state),
|
|
1040
1143
|
ack
|
|
@@ -1108,10 +1211,14 @@ var RealtimeClient = class {
|
|
|
1108
1211
|
* encoding as `buildRoomKey` / the gateway's `streamSubscriptionKey`; it is a
|
|
1109
1212
|
* client-local Map key only (never sent — the gateway derives its own).
|
|
1110
1213
|
*/
|
|
1111
|
-
streamSubscriptionKey(streamKey, where) {
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1214
|
+
streamSubscriptionKey(streamKey, where, consumerId) {
|
|
1215
|
+
let key = streamKey;
|
|
1216
|
+
if (where && Object.keys(where).length > 0) {
|
|
1217
|
+
const filterStr = Object.keys(where).sort().map((k) => `${k}=${JSON.stringify(where[k])}`).join("&");
|
|
1218
|
+
key = `${streamKey}?${filterStr}`;
|
|
1219
|
+
}
|
|
1220
|
+
if (consumerId) key = `${key}#${consumerId}`;
|
|
1221
|
+
return key;
|
|
1115
1222
|
}
|
|
1116
1223
|
/**
|
|
1117
1224
|
* All per-filter subscription sets registered for one collection's stream
|
|
@@ -1121,9 +1228,12 @@ var RealtimeClient = class {
|
|
|
1121
1228
|
*/
|
|
1122
1229
|
streamSetsForStreamKey(streamKey) {
|
|
1123
1230
|
const sets = [];
|
|
1124
|
-
const
|
|
1231
|
+
const wherePrefix = `${streamKey}?`;
|
|
1232
|
+
const consumerPrefix = `${streamKey}#`;
|
|
1125
1233
|
for (const [key, set] of this.streamSubscriptions) {
|
|
1126
|
-
if (key === streamKey || key.startsWith(
|
|
1234
|
+
if (key === streamKey || key.startsWith(wherePrefix) || key.startsWith(consumerPrefix)) {
|
|
1235
|
+
sets.push(set);
|
|
1236
|
+
}
|
|
1127
1237
|
}
|
|
1128
1238
|
return sets;
|
|
1129
1239
|
}
|
|
@@ -1291,6 +1401,21 @@ var RealtimeClient = class {
|
|
|
1291
1401
|
}
|
|
1292
1402
|
}
|
|
1293
1403
|
});
|
|
1404
|
+
this.socket.on("subscription-reset", (info) => {
|
|
1405
|
+
if (!info || typeof info.projectId !== "string") return;
|
|
1406
|
+
for (const set of this.streamSubscriptions.values()) {
|
|
1407
|
+
for (const state of set) {
|
|
1408
|
+
if (state.options.consumerId && state.options.projectId === info.projectId) {
|
|
1409
|
+
state.options.onGap?.({
|
|
1410
|
+
projectId: info.projectId,
|
|
1411
|
+
collectionName: state.options.collectionName,
|
|
1412
|
+
retentionWindow: "unknown",
|
|
1413
|
+
reason: "evicted"
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
});
|
|
1294
1419
|
this.socket.on("disconnect", () => {
|
|
1295
1420
|
if (this.disposed) return;
|
|
1296
1421
|
if (initialConnect) return;
|
|
@@ -1486,9 +1611,66 @@ var RealtimeClient = class {
|
|
|
1486
1611
|
);
|
|
1487
1612
|
});
|
|
1488
1613
|
}
|
|
1614
|
+
/** #768 — capture the durable fencing tokens from a successful handshake so
|
|
1615
|
+
* `ack-events` echoes the (generation, session) the server expects. */
|
|
1616
|
+
captureDurableHandshake(state, ack) {
|
|
1617
|
+
if (ack.generation !== void 0) state.generation = ack.generation;
|
|
1618
|
+
if (ack.session !== void 0) state.session = ack.session;
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* #768 — schedule a coalesced durable ack. Only durable subscriptions (a
|
|
1622
|
+
* consumerId with a captured generation+session) ack. At most one ack per
|
|
1623
|
+
* microtask drain, carrying the highest delivered seq — cumulative on the
|
|
1624
|
+
* server, so a burst of N events costs one ack, not N.
|
|
1625
|
+
*/
|
|
1626
|
+
scheduleDurableAck(state) {
|
|
1627
|
+
if (!state.options.consumerId || state.generation === void 0 || state.session === void 0 || state.ackFlushScheduled) {
|
|
1628
|
+
return;
|
|
1629
|
+
}
|
|
1630
|
+
state.ackFlushScheduled = true;
|
|
1631
|
+
queueMicrotask(() => {
|
|
1632
|
+
state.ackFlushScheduled = false;
|
|
1633
|
+
const seq = state.lastDeliveredId;
|
|
1634
|
+
if (seq) this.emitDurableAck(state, seq);
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
/** #768 — emit `ack-events` for `seq`; a fenced/evicted response routes into
|
|
1638
|
+
* the gap-recovery path so the caller re-snapshots (§12.5/§12.1). */
|
|
1639
|
+
emitDurableAck(state, seq) {
|
|
1640
|
+
const socket = this.socket;
|
|
1641
|
+
if (!socket) return;
|
|
1642
|
+
const { projectId, collectionName, where, consumerId } = state.options;
|
|
1643
|
+
if (consumerId === void 0 || state.generation === void 0 || state.session === void 0) {
|
|
1644
|
+
return;
|
|
1645
|
+
}
|
|
1646
|
+
const payload = {
|
|
1647
|
+
projectId,
|
|
1648
|
+
collectionName,
|
|
1649
|
+
consumerId,
|
|
1650
|
+
generation: state.generation,
|
|
1651
|
+
session: state.session,
|
|
1652
|
+
seq
|
|
1653
|
+
};
|
|
1654
|
+
if (where && Object.keys(where).length > 0) payload["where"] = where;
|
|
1655
|
+
socket.emit(
|
|
1656
|
+
"ack-events",
|
|
1657
|
+
payload,
|
|
1658
|
+
(res) => {
|
|
1659
|
+
if (!res?.gap) return;
|
|
1660
|
+
if (!this.streamSubscriptions.get(state.subscriptionKey)?.has(state)) return;
|
|
1661
|
+
state.options.onGap?.({
|
|
1662
|
+
projectId,
|
|
1663
|
+
collectionName,
|
|
1664
|
+
retentionWindow: "unknown",
|
|
1665
|
+
reason: res.gap
|
|
1666
|
+
});
|
|
1667
|
+
}
|
|
1668
|
+
);
|
|
1669
|
+
}
|
|
1489
1670
|
buildStreamPayload(state) {
|
|
1490
1671
|
const { projectId, collectionName, where } = state.options;
|
|
1491
1672
|
const payload = { projectId, collectionName };
|
|
1673
|
+
if (state.options.consumerId) payload["consumerId"] = state.options.consumerId;
|
|
1492
1674
|
if (state.lastDeliveredId) {
|
|
1493
1675
|
payload["sinceId"] = state.lastDeliveredId;
|
|
1494
1676
|
} else if (state.options.fromStart) {
|
|
@@ -1529,6 +1711,7 @@ var RealtimeClient = class {
|
|
|
1529
1711
|
try {
|
|
1530
1712
|
await state.options.onEvent({ eventId: entryId, event: payload });
|
|
1531
1713
|
state.lastDeliveredId = entryId;
|
|
1714
|
+
this.scheduleDurableAck(state);
|
|
1532
1715
|
} catch (err) {
|
|
1533
1716
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
1534
1717
|
try {
|
|
@@ -1567,6 +1750,11 @@ var RealtimeClient = class {
|
|
|
1567
1750
|
}
|
|
1568
1751
|
return;
|
|
1569
1752
|
}
|
|
1753
|
+
for (const s of set) {
|
|
1754
|
+
s.generation = void 0;
|
|
1755
|
+
s.session = void 0;
|
|
1756
|
+
}
|
|
1757
|
+
this.captureDurableHandshake(primary, ack);
|
|
1570
1758
|
if (!this.streamSubscriptions.has(primary.subscriptionKey)) {
|
|
1571
1759
|
const payload = {
|
|
1572
1760
|
projectId: primary.options.projectId,
|
|
@@ -1660,6 +1848,23 @@ function localStorageCursorStorage(prefix = "spacelr:cursor:") {
|
|
|
1660
1848
|
};
|
|
1661
1849
|
}
|
|
1662
1850
|
|
|
1851
|
+
// libs/sdk/src/core/consumer-id.ts
|
|
1852
|
+
var CONSUMER_ID_STORAGE_KEY = "spacelr:consumer-id";
|
|
1853
|
+
function generateConsumerId() {
|
|
1854
|
+
const c = globalThis.crypto;
|
|
1855
|
+
if (c?.randomUUID) return c.randomUUID();
|
|
1856
|
+
return (Math.random().toString(36).slice(2) + "-" + Date.now().toString(36)).slice(0, 128);
|
|
1857
|
+
}
|
|
1858
|
+
async function resolveConsumerId(explicit, storage, key = CONSUMER_ID_STORAGE_KEY) {
|
|
1859
|
+
if (explicit) return explicit;
|
|
1860
|
+
if (!storage) return generateConsumerId();
|
|
1861
|
+
const existing = await Promise.resolve(storage.load(key));
|
|
1862
|
+
if (existing) return existing;
|
|
1863
|
+
const generated = generateConsumerId();
|
|
1864
|
+
await Promise.resolve(storage.save(key, generated));
|
|
1865
|
+
return generated;
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1663
1868
|
// libs/sdk/src/modules/auth.module.ts
|
|
1664
1869
|
function mapProfile(u) {
|
|
1665
1870
|
const name = u.name ?? ([u.firstName, u.lastName].filter(Boolean).join(" ") || void 0) ?? u.displayName;
|
|
@@ -2678,6 +2883,13 @@ function compileMatcher(where, plan) {
|
|
|
2678
2883
|
}
|
|
2679
2884
|
|
|
2680
2885
|
// libs/sdk/src/modules/database.module.ts
|
|
2886
|
+
var RESNAPSHOT_GAP_REASONS = /* @__PURE__ */ new Set([
|
|
2887
|
+
"outside-retention-window",
|
|
2888
|
+
"replay-truncated",
|
|
2889
|
+
"evicted",
|
|
2890
|
+
"log-reset",
|
|
2891
|
+
"auth-unavailable"
|
|
2892
|
+
]);
|
|
2681
2893
|
var DEFAULT_CATCHUP_MAX_PAGES = 1e3;
|
|
2682
2894
|
var DEFAULT_CATCHUP_MAX_DOCS = 5e4;
|
|
2683
2895
|
var DEFAULT_CATCHUP_PAGE_SIZE = 100;
|
|
@@ -3071,6 +3283,7 @@ var CollectionRef = class {
|
|
|
3071
3283
|
projectId: this.projectId,
|
|
3072
3284
|
collectionName: this.collectionName,
|
|
3073
3285
|
sinceId,
|
|
3286
|
+
consumerId: handlers.consumerId,
|
|
3074
3287
|
// #685 — only meaningful on a fresh subscription (no sinceId/cursor);
|
|
3075
3288
|
// buildStreamPayload ignores it once a cursor exists.
|
|
3076
3289
|
fromStart: handlers.fromStart,
|
|
@@ -3187,6 +3400,11 @@ var CollectionRef = class {
|
|
|
3187
3400
|
if (!this.realtime) {
|
|
3188
3401
|
throw new Error("Realtime not available: no RealtimeClient configured");
|
|
3189
3402
|
}
|
|
3403
|
+
if (opts.consumerId !== void 0 && !/^[A-Za-z0-9._-]{1,128}$/.test(opts.consumerId)) {
|
|
3404
|
+
throw new Error(
|
|
3405
|
+
`Invalid consumerId ${JSON.stringify(opts.consumerId)}: must match [A-Za-z0-9._-]{1,128}. Use resolveConsumerId() to generate one, or omit it for a stateless subscription.`
|
|
3406
|
+
);
|
|
3407
|
+
}
|
|
3190
3408
|
let isActive = true;
|
|
3191
3409
|
let currentUnsubscribe = null;
|
|
3192
3410
|
let snapshotAbort = new AbortController();
|
|
@@ -3251,6 +3469,7 @@ var CollectionRef = class {
|
|
|
3251
3469
|
projectId: this.projectId,
|
|
3252
3470
|
collectionName: this.collectionName,
|
|
3253
3471
|
sinceId: void 0,
|
|
3472
|
+
consumerId: opts.consumerId,
|
|
3254
3473
|
where: streamWhere,
|
|
3255
3474
|
onEvent: async ({ event }) => {
|
|
3256
3475
|
if (!isActive) return;
|
|
@@ -3381,8 +3600,7 @@ var CollectionRef = class {
|
|
|
3381
3600
|
};
|
|
3382
3601
|
const handleGap = (info) => {
|
|
3383
3602
|
if (!isActive) return;
|
|
3384
|
-
if (
|
|
3385
|
-
return;
|
|
3603
|
+
if (!RESNAPSHOT_GAP_REASONS.has(info.reason)) return;
|
|
3386
3604
|
dispatchRecovery();
|
|
3387
3605
|
};
|
|
3388
3606
|
let subscribeAckResolved = false;
|
|
@@ -3395,6 +3613,7 @@ var CollectionRef = class {
|
|
|
3395
3613
|
projectId: this.projectId,
|
|
3396
3614
|
collectionName: this.collectionName,
|
|
3397
3615
|
sinceId: void 0,
|
|
3616
|
+
consumerId: opts.consumerId,
|
|
3398
3617
|
where: streamWhere,
|
|
3399
3618
|
onEvent: async ({ event }) => {
|
|
3400
3619
|
if (!isActive) return;
|
|
@@ -3443,11 +3662,10 @@ var CollectionRef = class {
|
|
|
3443
3662
|
);
|
|
3444
3663
|
}
|
|
3445
3664
|
currentUnsubscribe = initial.unsubscribe;
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
recomputeTypedMatcherPlan(snapshotResult.coercionPlan);
|
|
3665
|
+
const isDurableResume = initial.resumeCursor !== null;
|
|
3666
|
+
let snapshotDocs = [];
|
|
3667
|
+
if (isDurableResume) {
|
|
3668
|
+
recomputeTypedMatcherPlan(void 0);
|
|
3451
3669
|
initialPlanReady = true;
|
|
3452
3670
|
for (let i = pendingInitialEvents.length - 1; i >= 0; i--) {
|
|
3453
3671
|
if (!passesLiveGate(pendingInitialEvents[i])) {
|
|
@@ -3455,29 +3673,44 @@ var CollectionRef = class {
|
|
|
3455
3673
|
}
|
|
3456
3674
|
}
|
|
3457
3675
|
warnIfNoGateAvailable();
|
|
3458
|
-
}
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3676
|
+
} else {
|
|
3677
|
+
try {
|
|
3678
|
+
const snapshotResult = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
|
|
3679
|
+
snapshotDocs = snapshotResult.documents;
|
|
3680
|
+
recomputeTypedMatcherPlan(snapshotResult.coercionPlan);
|
|
3681
|
+
initialPlanReady = true;
|
|
3682
|
+
for (let i = pendingInitialEvents.length - 1; i >= 0; i--) {
|
|
3683
|
+
if (!passesLiveGate(pendingInitialEvents[i])) {
|
|
3684
|
+
pendingInitialEvents.splice(i, 1);
|
|
3685
|
+
}
|
|
3686
|
+
}
|
|
3687
|
+
warnIfNoGateAvailable();
|
|
3688
|
+
} catch (err) {
|
|
3689
|
+
currentUnsubscribe?.();
|
|
3690
|
+
isActive = false;
|
|
3691
|
+
const isAbort = err instanceof DOMException && err.name === "AbortError";
|
|
3692
|
+
if (isAbort) throw err;
|
|
3693
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3694
|
+
if (opts.onSnapshotError) opts.onSnapshotError(error);
|
|
3695
|
+
else opts.onError?.(error);
|
|
3696
|
+
throw error;
|
|
3697
|
+
}
|
|
3467
3698
|
}
|
|
3468
3699
|
if (!isActive) {
|
|
3469
3700
|
currentUnsubscribe?.();
|
|
3470
3701
|
return () => void 0;
|
|
3471
3702
|
}
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3703
|
+
if (!isDurableResume) {
|
|
3704
|
+
try {
|
|
3705
|
+
await opts.onSnapshot(snapshotDocs, initial.latestStreamId);
|
|
3706
|
+
} catch (err) {
|
|
3707
|
+
currentUnsubscribe?.();
|
|
3708
|
+
currentUnsubscribe = null;
|
|
3709
|
+
isActive = false;
|
|
3710
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3711
|
+
opts.onError?.(error);
|
|
3712
|
+
throw error;
|
|
3713
|
+
}
|
|
3481
3714
|
}
|
|
3482
3715
|
while (true) {
|
|
3483
3716
|
if (!isActive) return () => void 0;
|
|
@@ -3865,6 +4098,7 @@ function createClient(config) {
|
|
|
3865
4098
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3866
4099
|
0 && (module.exports = {
|
|
3867
4100
|
BrowserTokenStorage,
|
|
4101
|
+
CONSUMER_ID_STORAGE_KEY,
|
|
3868
4102
|
CodeChallengeMethod,
|
|
3869
4103
|
CursorInvalidError,
|
|
3870
4104
|
FileVisibility,
|
|
@@ -3889,6 +4123,7 @@ function createClient(config) {
|
|
|
3889
4123
|
createClient,
|
|
3890
4124
|
generatePKCEChallenge,
|
|
3891
4125
|
localStorageCursorStorage,
|
|
3892
|
-
memoryCursorStorage
|
|
4126
|
+
memoryCursorStorage,
|
|
4127
|
+
resolveConsumerId
|
|
3893
4128
|
});
|
|
3894
4129
|
//# sourceMappingURL=index.js.map
|