@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.mjs
CHANGED
|
@@ -171,17 +171,27 @@ var HttpClient = class {
|
|
|
171
171
|
* caller should retry with a refreshed token. 403 is never routed here —
|
|
172
172
|
* it means "forbidden for this action" and must not trigger logout.
|
|
173
173
|
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
174
|
+
* Auth-loss ownership (#759 review):
|
|
175
|
+
* - When a refresh WAS possible (a refresh token existed), `doRefresh` owns
|
|
176
|
+
* the outcome: it clears + emits on a server-confirmed rejection, and stays
|
|
177
|
+
* silent on a transient transport failure. We must not second-guess it, so
|
|
178
|
+
* we do NOT emit here — that avoids a double clear/emit and stops a transient
|
|
179
|
+
* blip during a 401 retry from ejecting the user.
|
|
180
|
+
* - When no refresh was possible (no refresh token), this 401 IS the confirmed
|
|
181
|
+
* loss and we own it: `notifyAuthLost` clears the dead session then emits.
|
|
182
|
+
* The single clear always runs before any listener, so a listener that
|
|
183
|
+
* re-logs-in via `setTokens()` can never have its fresh session erased.
|
|
176
184
|
*/
|
|
177
185
|
async recoverFromAuthFailure(isRetry) {
|
|
178
186
|
if (isRetry) {
|
|
179
|
-
this.tokenManager.
|
|
187
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
180
188
|
return false;
|
|
181
189
|
}
|
|
190
|
+
const hadRefreshToken = await this.tokenManager.hasRefreshToken();
|
|
182
191
|
const refreshed = await this.tokenManager.forceRefresh();
|
|
183
192
|
if (refreshed) return true;
|
|
184
|
-
|
|
193
|
+
if (hadRefreshToken) return false;
|
|
194
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
185
195
|
return false;
|
|
186
196
|
}
|
|
187
197
|
async uploadFormOnce(url, headers, formData, timeoutMs) {
|
|
@@ -413,6 +423,11 @@ var TokenManager = class {
|
|
|
413
423
|
// that hits `/auth/logout` with a dead token) don't re-emit and loop.
|
|
414
424
|
// Cleared by setTokens() / clearTokens() to re-arm for the next session.
|
|
415
425
|
this.authLostEmitted = false;
|
|
426
|
+
// The in-flight auth-loss storage wipe, if any. `setTokens` awaits it before
|
|
427
|
+
// writing so a re-login that lands DURING the wipe (e.g. an external
|
|
428
|
+
// auth-components bridge calling setTokens concurrently with a background
|
|
429
|
+
// refresh failure) is applied AFTER the wipe and never erased (#759 review).
|
|
430
|
+
this.pendingClear = null;
|
|
416
431
|
this.storage = storage ?? new MemoryTokenStorage();
|
|
417
432
|
this.refreshBufferSeconds = refreshBufferSeconds;
|
|
418
433
|
this.refreshLockName = refreshLockName;
|
|
@@ -434,6 +449,7 @@ var TokenManager = class {
|
|
|
434
449
|
return tokens.accessToken;
|
|
435
450
|
}
|
|
436
451
|
async setTokens(tokens) {
|
|
452
|
+
if (this.pendingClear) await this.pendingClear;
|
|
437
453
|
const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
|
|
438
454
|
const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
|
|
439
455
|
await this.storage.setTokens(normalised);
|
|
@@ -466,6 +482,16 @@ var TokenManager = class {
|
|
|
466
482
|
const refreshed = await this.tryRefresh(tokens);
|
|
467
483
|
return refreshed?.accessToken ?? null;
|
|
468
484
|
}
|
|
485
|
+
/**
|
|
486
|
+
* Whether the stored session currently carries a refresh token. Used by the
|
|
487
|
+
* 401-recovery path to decide whether a refresh was even possible, so it can
|
|
488
|
+
* let `doRefresh` own the auth-loss classification instead of second-guessing
|
|
489
|
+
* it. A rejecting storage backend reads as "no refresh token".
|
|
490
|
+
*/
|
|
491
|
+
async hasRefreshToken() {
|
|
492
|
+
const tokens = await this.safeGetTokens();
|
|
493
|
+
return !!tokens?.refreshToken;
|
|
494
|
+
}
|
|
469
495
|
onTokenRefreshed(listener) {
|
|
470
496
|
this.tokenRefreshedListeners.add(listener);
|
|
471
497
|
return () => this.tokenRefreshedListeners.delete(listener);
|
|
@@ -477,6 +503,9 @@ var TokenManager = class {
|
|
|
477
503
|
emitAuthLost(reason) {
|
|
478
504
|
if (this.authLostEmitted) return;
|
|
479
505
|
this.authLostEmitted = true;
|
|
506
|
+
this.dispatchAuthLost(reason);
|
|
507
|
+
}
|
|
508
|
+
dispatchAuthLost(reason) {
|
|
480
509
|
for (const listener of this.authLostListeners) {
|
|
481
510
|
try {
|
|
482
511
|
listener(reason);
|
|
@@ -484,6 +513,57 @@ var TokenManager = class {
|
|
|
484
513
|
}
|
|
485
514
|
}
|
|
486
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Handle a server-confirmed auth loss: drop the dead session, THEN notify.
|
|
518
|
+
*
|
|
519
|
+
* The stored tokens are cleared **before** listeners run (#759) so that a
|
|
520
|
+
* reload lands on the login screen and `hasStoredSession()` reads false — and,
|
|
521
|
+
* critically, so a listener that re-logs-in via `setTokens()` (synchronously
|
|
522
|
+
* OR after an await) writes its new session strictly AFTER the clear and can
|
|
523
|
+
* never be erased by it. The clear is a storage-only wipe: we do NOT reset
|
|
524
|
+
* `authLostEmitted` (as `clearTokens()` would), so the emit stays deduped for
|
|
525
|
+
* this lost session. A rejecting storage backend is swallowed — best-effort,
|
|
526
|
+
* the consumer is routed to logout regardless. `refreshPromise` is left to
|
|
527
|
+
* `doRefresh`'s own `finally`; nulling it here could clobber a newer in-flight
|
|
528
|
+
* refresh.
|
|
529
|
+
*
|
|
530
|
+
* The token-clearing auth-loss path. Called from `doRefresh` (single-flighted
|
|
531
|
+
* via `refreshPromise`) on a server-confirmed refresh rejection, and from
|
|
532
|
+
* `HttpClient` only when a 401 could not be refreshed AND no refresh was even
|
|
533
|
+
* attempted (no refresh token) — these are mutually exclusive, so there is
|
|
534
|
+
* exactly one clear per loss and it runs BEFORE any listener. A re-login a
|
|
535
|
+
* listener performs is therefore never erased.
|
|
536
|
+
*/
|
|
537
|
+
async notifyAuthLost(reason) {
|
|
538
|
+
if (this.authLostEmitted) return;
|
|
539
|
+
this.authLostEmitted = true;
|
|
540
|
+
this.pendingClear = this.storage.clearTokens().catch(() => {
|
|
541
|
+
});
|
|
542
|
+
await this.pendingClear;
|
|
543
|
+
this.pendingClear = null;
|
|
544
|
+
this.dispatchAuthLost(reason);
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* A refresh failure that is transient (retryable), not a server-confirmed auth
|
|
548
|
+
* loss — so it must NOT eject a local-first app; the next request retries.
|
|
549
|
+
* Covers:
|
|
550
|
+
* - a transport failure (offline, tunnel, captive portal, unreachable
|
|
551
|
+
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
552
|
+
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
553
|
+
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
554
|
+
* a token rejection.
|
|
555
|
+
* A real auth rejection (401/invalid_grant) or any other non-5xx server error
|
|
556
|
+
* is NOT transient and is treated as a server-confirmed loss.
|
|
557
|
+
*/
|
|
558
|
+
isTransientTransportError(error) {
|
|
559
|
+
if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
if (error instanceof SpacelrError && typeof error.statusCode === "number") {
|
|
563
|
+
return error.statusCode >= 500 && error.statusCode <= 599;
|
|
564
|
+
}
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
487
567
|
isTokenExpired(tokens) {
|
|
488
568
|
if (!tokens.expiresAt) return false;
|
|
489
569
|
return Date.now() >= tokens.expiresAt * 1e3;
|
|
@@ -522,15 +602,18 @@ var TokenManager = class {
|
|
|
522
602
|
return current;
|
|
523
603
|
}
|
|
524
604
|
const activeRefreshToken = current?.refreshToken || refreshToken;
|
|
605
|
+
let newTokens;
|
|
525
606
|
try {
|
|
526
|
-
|
|
527
|
-
await this.storage.setTokens(newTokens);
|
|
528
|
-
this.emitTokenRefreshed(newTokens);
|
|
529
|
-
return newTokens;
|
|
607
|
+
newTokens = await callback(activeRefreshToken);
|
|
530
608
|
} catch (error) {
|
|
531
|
-
this.
|
|
609
|
+
if (!this.isTransientTransportError(error)) {
|
|
610
|
+
await this.notifyAuthLost("refresh-failed");
|
|
611
|
+
}
|
|
532
612
|
throw error;
|
|
533
613
|
}
|
|
614
|
+
await this.storage.setTokens(newTokens);
|
|
615
|
+
this.emitTokenRefreshed(newTokens);
|
|
616
|
+
return newTokens;
|
|
534
617
|
}
|
|
535
618
|
async safeGetTokens() {
|
|
536
619
|
try {
|
|
@@ -924,7 +1007,8 @@ var RealtimeClient = class {
|
|
|
924
1007
|
// gateways that haven't been updated yet are treated as pubsub-mode
|
|
925
1008
|
// so the SDK helper's fail-fast still fires correctly.
|
|
926
1009
|
realtimeMode: ack.realtimeMode ?? "pubsub",
|
|
927
|
-
latestStreamId: ack.latestStreamId ?? null
|
|
1010
|
+
latestStreamId: ack.latestStreamId ?? null,
|
|
1011
|
+
resumeCursor: ack.resumeCursor ?? null
|
|
928
1012
|
};
|
|
929
1013
|
}
|
|
930
1014
|
/**
|
|
@@ -958,7 +1042,23 @@ var RealtimeClient = class {
|
|
|
958
1042
|
}
|
|
959
1043
|
await this.ensureConnected();
|
|
960
1044
|
const streamKey = `stream:${options.projectId}:${options.collectionName}`;
|
|
961
|
-
const subscriptionKey = this.streamSubscriptionKey(
|
|
1045
|
+
const subscriptionKey = this.streamSubscriptionKey(
|
|
1046
|
+
streamKey,
|
|
1047
|
+
options.where,
|
|
1048
|
+
options.consumerId
|
|
1049
|
+
);
|
|
1050
|
+
if (options.consumerId) {
|
|
1051
|
+
const base = this.streamSubscriptionKey(streamKey, options.where);
|
|
1052
|
+
const mine = `${base}#${options.consumerId}`;
|
|
1053
|
+
for (const key of this.streamSubscriptions.keys()) {
|
|
1054
|
+
if (key.startsWith(`${base}#`) && key !== mine) {
|
|
1055
|
+
throw new SpacelrError(
|
|
1056
|
+
`A durable subscription for ${base} already exists on this client under a different consumerId. Use one consumerId per (collection, filter) per client.`,
|
|
1057
|
+
"DURABLE_CONSUMER_CONFLICT"
|
|
1058
|
+
);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
962
1062
|
const state = {
|
|
963
1063
|
options,
|
|
964
1064
|
lastDeliveredId: options.sinceId ?? null,
|
|
@@ -991,6 +1091,7 @@ var RealtimeClient = class {
|
|
|
991
1091
|
options.onError?.(err);
|
|
992
1092
|
throw err;
|
|
993
1093
|
}
|
|
1094
|
+
this.captureDurableHandshake(state, ack);
|
|
994
1095
|
return {
|
|
995
1096
|
unsubscribe: () => this.unsubscribeStream(state),
|
|
996
1097
|
ack
|
|
@@ -1064,10 +1165,14 @@ var RealtimeClient = class {
|
|
|
1064
1165
|
* encoding as `buildRoomKey` / the gateway's `streamSubscriptionKey`; it is a
|
|
1065
1166
|
* client-local Map key only (never sent — the gateway derives its own).
|
|
1066
1167
|
*/
|
|
1067
|
-
streamSubscriptionKey(streamKey, where) {
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1168
|
+
streamSubscriptionKey(streamKey, where, consumerId) {
|
|
1169
|
+
let key = streamKey;
|
|
1170
|
+
if (where && Object.keys(where).length > 0) {
|
|
1171
|
+
const filterStr = Object.keys(where).sort().map((k) => `${k}=${JSON.stringify(where[k])}`).join("&");
|
|
1172
|
+
key = `${streamKey}?${filterStr}`;
|
|
1173
|
+
}
|
|
1174
|
+
if (consumerId) key = `${key}#${consumerId}`;
|
|
1175
|
+
return key;
|
|
1071
1176
|
}
|
|
1072
1177
|
/**
|
|
1073
1178
|
* All per-filter subscription sets registered for one collection's stream
|
|
@@ -1077,9 +1182,12 @@ var RealtimeClient = class {
|
|
|
1077
1182
|
*/
|
|
1078
1183
|
streamSetsForStreamKey(streamKey) {
|
|
1079
1184
|
const sets = [];
|
|
1080
|
-
const
|
|
1185
|
+
const wherePrefix = `${streamKey}?`;
|
|
1186
|
+
const consumerPrefix = `${streamKey}#`;
|
|
1081
1187
|
for (const [key, set] of this.streamSubscriptions) {
|
|
1082
|
-
if (key === streamKey || key.startsWith(
|
|
1188
|
+
if (key === streamKey || key.startsWith(wherePrefix) || key.startsWith(consumerPrefix)) {
|
|
1189
|
+
sets.push(set);
|
|
1190
|
+
}
|
|
1083
1191
|
}
|
|
1084
1192
|
return sets;
|
|
1085
1193
|
}
|
|
@@ -1247,6 +1355,21 @@ var RealtimeClient = class {
|
|
|
1247
1355
|
}
|
|
1248
1356
|
}
|
|
1249
1357
|
});
|
|
1358
|
+
this.socket.on("subscription-reset", (info) => {
|
|
1359
|
+
if (!info || typeof info.projectId !== "string") return;
|
|
1360
|
+
for (const set of this.streamSubscriptions.values()) {
|
|
1361
|
+
for (const state of set) {
|
|
1362
|
+
if (state.options.consumerId && state.options.projectId === info.projectId) {
|
|
1363
|
+
state.options.onGap?.({
|
|
1364
|
+
projectId: info.projectId,
|
|
1365
|
+
collectionName: state.options.collectionName,
|
|
1366
|
+
retentionWindow: "unknown",
|
|
1367
|
+
reason: "evicted"
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
});
|
|
1250
1373
|
this.socket.on("disconnect", () => {
|
|
1251
1374
|
if (this.disposed) return;
|
|
1252
1375
|
if (initialConnect) return;
|
|
@@ -1442,9 +1565,66 @@ var RealtimeClient = class {
|
|
|
1442
1565
|
);
|
|
1443
1566
|
});
|
|
1444
1567
|
}
|
|
1568
|
+
/** #768 — capture the durable fencing tokens from a successful handshake so
|
|
1569
|
+
* `ack-events` echoes the (generation, session) the server expects. */
|
|
1570
|
+
captureDurableHandshake(state, ack) {
|
|
1571
|
+
if (ack.generation !== void 0) state.generation = ack.generation;
|
|
1572
|
+
if (ack.session !== void 0) state.session = ack.session;
|
|
1573
|
+
}
|
|
1574
|
+
/**
|
|
1575
|
+
* #768 — schedule a coalesced durable ack. Only durable subscriptions (a
|
|
1576
|
+
* consumerId with a captured generation+session) ack. At most one ack per
|
|
1577
|
+
* microtask drain, carrying the highest delivered seq — cumulative on the
|
|
1578
|
+
* server, so a burst of N events costs one ack, not N.
|
|
1579
|
+
*/
|
|
1580
|
+
scheduleDurableAck(state) {
|
|
1581
|
+
if (!state.options.consumerId || state.generation === void 0 || state.session === void 0 || state.ackFlushScheduled) {
|
|
1582
|
+
return;
|
|
1583
|
+
}
|
|
1584
|
+
state.ackFlushScheduled = true;
|
|
1585
|
+
queueMicrotask(() => {
|
|
1586
|
+
state.ackFlushScheduled = false;
|
|
1587
|
+
const seq = state.lastDeliveredId;
|
|
1588
|
+
if (seq) this.emitDurableAck(state, seq);
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
/** #768 — emit `ack-events` for `seq`; a fenced/evicted response routes into
|
|
1592
|
+
* the gap-recovery path so the caller re-snapshots (§12.5/§12.1). */
|
|
1593
|
+
emitDurableAck(state, seq) {
|
|
1594
|
+
const socket = this.socket;
|
|
1595
|
+
if (!socket) return;
|
|
1596
|
+
const { projectId, collectionName, where, consumerId } = state.options;
|
|
1597
|
+
if (consumerId === void 0 || state.generation === void 0 || state.session === void 0) {
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
const payload = {
|
|
1601
|
+
projectId,
|
|
1602
|
+
collectionName,
|
|
1603
|
+
consumerId,
|
|
1604
|
+
generation: state.generation,
|
|
1605
|
+
session: state.session,
|
|
1606
|
+
seq
|
|
1607
|
+
};
|
|
1608
|
+
if (where && Object.keys(where).length > 0) payload["where"] = where;
|
|
1609
|
+
socket.emit(
|
|
1610
|
+
"ack-events",
|
|
1611
|
+
payload,
|
|
1612
|
+
(res) => {
|
|
1613
|
+
if (!res?.gap) return;
|
|
1614
|
+
if (!this.streamSubscriptions.get(state.subscriptionKey)?.has(state)) return;
|
|
1615
|
+
state.options.onGap?.({
|
|
1616
|
+
projectId,
|
|
1617
|
+
collectionName,
|
|
1618
|
+
retentionWindow: "unknown",
|
|
1619
|
+
reason: res.gap
|
|
1620
|
+
});
|
|
1621
|
+
}
|
|
1622
|
+
);
|
|
1623
|
+
}
|
|
1445
1624
|
buildStreamPayload(state) {
|
|
1446
1625
|
const { projectId, collectionName, where } = state.options;
|
|
1447
1626
|
const payload = { projectId, collectionName };
|
|
1627
|
+
if (state.options.consumerId) payload["consumerId"] = state.options.consumerId;
|
|
1448
1628
|
if (state.lastDeliveredId) {
|
|
1449
1629
|
payload["sinceId"] = state.lastDeliveredId;
|
|
1450
1630
|
} else if (state.options.fromStart) {
|
|
@@ -1485,6 +1665,7 @@ var RealtimeClient = class {
|
|
|
1485
1665
|
try {
|
|
1486
1666
|
await state.options.onEvent({ eventId: entryId, event: payload });
|
|
1487
1667
|
state.lastDeliveredId = entryId;
|
|
1668
|
+
this.scheduleDurableAck(state);
|
|
1488
1669
|
} catch (err) {
|
|
1489
1670
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
1490
1671
|
try {
|
|
@@ -1523,6 +1704,11 @@ var RealtimeClient = class {
|
|
|
1523
1704
|
}
|
|
1524
1705
|
return;
|
|
1525
1706
|
}
|
|
1707
|
+
for (const s of set) {
|
|
1708
|
+
s.generation = void 0;
|
|
1709
|
+
s.session = void 0;
|
|
1710
|
+
}
|
|
1711
|
+
this.captureDurableHandshake(primary, ack);
|
|
1526
1712
|
if (!this.streamSubscriptions.has(primary.subscriptionKey)) {
|
|
1527
1713
|
const payload = {
|
|
1528
1714
|
projectId: primary.options.projectId,
|
|
@@ -1616,6 +1802,23 @@ function localStorageCursorStorage(prefix = "spacelr:cursor:") {
|
|
|
1616
1802
|
};
|
|
1617
1803
|
}
|
|
1618
1804
|
|
|
1805
|
+
// libs/sdk/src/core/consumer-id.ts
|
|
1806
|
+
var CONSUMER_ID_STORAGE_KEY = "spacelr:consumer-id";
|
|
1807
|
+
function generateConsumerId() {
|
|
1808
|
+
const c = globalThis.crypto;
|
|
1809
|
+
if (c?.randomUUID) return c.randomUUID();
|
|
1810
|
+
return (Math.random().toString(36).slice(2) + "-" + Date.now().toString(36)).slice(0, 128);
|
|
1811
|
+
}
|
|
1812
|
+
async function resolveConsumerId(explicit, storage, key = CONSUMER_ID_STORAGE_KEY) {
|
|
1813
|
+
if (explicit) return explicit;
|
|
1814
|
+
if (!storage) return generateConsumerId();
|
|
1815
|
+
const existing = await Promise.resolve(storage.load(key));
|
|
1816
|
+
if (existing) return existing;
|
|
1817
|
+
const generated = generateConsumerId();
|
|
1818
|
+
await Promise.resolve(storage.save(key, generated));
|
|
1819
|
+
return generated;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1619
1822
|
// libs/sdk/src/modules/auth.module.ts
|
|
1620
1823
|
function mapProfile(u) {
|
|
1621
1824
|
const name = u.name ?? ([u.firstName, u.lastName].filter(Boolean).join(" ") || void 0) ?? u.displayName;
|
|
@@ -2634,6 +2837,13 @@ function compileMatcher(where, plan) {
|
|
|
2634
2837
|
}
|
|
2635
2838
|
|
|
2636
2839
|
// libs/sdk/src/modules/database.module.ts
|
|
2840
|
+
var RESNAPSHOT_GAP_REASONS = /* @__PURE__ */ new Set([
|
|
2841
|
+
"outside-retention-window",
|
|
2842
|
+
"replay-truncated",
|
|
2843
|
+
"evicted",
|
|
2844
|
+
"log-reset",
|
|
2845
|
+
"auth-unavailable"
|
|
2846
|
+
]);
|
|
2637
2847
|
var DEFAULT_CATCHUP_MAX_PAGES = 1e3;
|
|
2638
2848
|
var DEFAULT_CATCHUP_MAX_DOCS = 5e4;
|
|
2639
2849
|
var DEFAULT_CATCHUP_PAGE_SIZE = 100;
|
|
@@ -3027,6 +3237,7 @@ var CollectionRef = class {
|
|
|
3027
3237
|
projectId: this.projectId,
|
|
3028
3238
|
collectionName: this.collectionName,
|
|
3029
3239
|
sinceId,
|
|
3240
|
+
consumerId: handlers.consumerId,
|
|
3030
3241
|
// #685 — only meaningful on a fresh subscription (no sinceId/cursor);
|
|
3031
3242
|
// buildStreamPayload ignores it once a cursor exists.
|
|
3032
3243
|
fromStart: handlers.fromStart,
|
|
@@ -3143,6 +3354,11 @@ var CollectionRef = class {
|
|
|
3143
3354
|
if (!this.realtime) {
|
|
3144
3355
|
throw new Error("Realtime not available: no RealtimeClient configured");
|
|
3145
3356
|
}
|
|
3357
|
+
if (opts.consumerId !== void 0 && !/^[A-Za-z0-9._-]{1,128}$/.test(opts.consumerId)) {
|
|
3358
|
+
throw new Error(
|
|
3359
|
+
`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.`
|
|
3360
|
+
);
|
|
3361
|
+
}
|
|
3146
3362
|
let isActive = true;
|
|
3147
3363
|
let currentUnsubscribe = null;
|
|
3148
3364
|
let snapshotAbort = new AbortController();
|
|
@@ -3207,6 +3423,7 @@ var CollectionRef = class {
|
|
|
3207
3423
|
projectId: this.projectId,
|
|
3208
3424
|
collectionName: this.collectionName,
|
|
3209
3425
|
sinceId: void 0,
|
|
3426
|
+
consumerId: opts.consumerId,
|
|
3210
3427
|
where: streamWhere,
|
|
3211
3428
|
onEvent: async ({ event }) => {
|
|
3212
3429
|
if (!isActive) return;
|
|
@@ -3337,8 +3554,7 @@ var CollectionRef = class {
|
|
|
3337
3554
|
};
|
|
3338
3555
|
const handleGap = (info) => {
|
|
3339
3556
|
if (!isActive) return;
|
|
3340
|
-
if (
|
|
3341
|
-
return;
|
|
3557
|
+
if (!RESNAPSHOT_GAP_REASONS.has(info.reason)) return;
|
|
3342
3558
|
dispatchRecovery();
|
|
3343
3559
|
};
|
|
3344
3560
|
let subscribeAckResolved = false;
|
|
@@ -3351,6 +3567,7 @@ var CollectionRef = class {
|
|
|
3351
3567
|
projectId: this.projectId,
|
|
3352
3568
|
collectionName: this.collectionName,
|
|
3353
3569
|
sinceId: void 0,
|
|
3570
|
+
consumerId: opts.consumerId,
|
|
3354
3571
|
where: streamWhere,
|
|
3355
3572
|
onEvent: async ({ event }) => {
|
|
3356
3573
|
if (!isActive) return;
|
|
@@ -3399,11 +3616,10 @@ var CollectionRef = class {
|
|
|
3399
3616
|
);
|
|
3400
3617
|
}
|
|
3401
3618
|
currentUnsubscribe = initial.unsubscribe;
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
recomputeTypedMatcherPlan(snapshotResult.coercionPlan);
|
|
3619
|
+
const isDurableResume = initial.resumeCursor !== null;
|
|
3620
|
+
let snapshotDocs = [];
|
|
3621
|
+
if (isDurableResume) {
|
|
3622
|
+
recomputeTypedMatcherPlan(void 0);
|
|
3407
3623
|
initialPlanReady = true;
|
|
3408
3624
|
for (let i = pendingInitialEvents.length - 1; i >= 0; i--) {
|
|
3409
3625
|
if (!passesLiveGate(pendingInitialEvents[i])) {
|
|
@@ -3411,29 +3627,44 @@ var CollectionRef = class {
|
|
|
3411
3627
|
}
|
|
3412
3628
|
}
|
|
3413
3629
|
warnIfNoGateAvailable();
|
|
3414
|
-
}
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3630
|
+
} else {
|
|
3631
|
+
try {
|
|
3632
|
+
const snapshotResult = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
|
|
3633
|
+
snapshotDocs = snapshotResult.documents;
|
|
3634
|
+
recomputeTypedMatcherPlan(snapshotResult.coercionPlan);
|
|
3635
|
+
initialPlanReady = true;
|
|
3636
|
+
for (let i = pendingInitialEvents.length - 1; i >= 0; i--) {
|
|
3637
|
+
if (!passesLiveGate(pendingInitialEvents[i])) {
|
|
3638
|
+
pendingInitialEvents.splice(i, 1);
|
|
3639
|
+
}
|
|
3640
|
+
}
|
|
3641
|
+
warnIfNoGateAvailable();
|
|
3642
|
+
} catch (err) {
|
|
3643
|
+
currentUnsubscribe?.();
|
|
3644
|
+
isActive = false;
|
|
3645
|
+
const isAbort = err instanceof DOMException && err.name === "AbortError";
|
|
3646
|
+
if (isAbort) throw err;
|
|
3647
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3648
|
+
if (opts.onSnapshotError) opts.onSnapshotError(error);
|
|
3649
|
+
else opts.onError?.(error);
|
|
3650
|
+
throw error;
|
|
3651
|
+
}
|
|
3423
3652
|
}
|
|
3424
3653
|
if (!isActive) {
|
|
3425
3654
|
currentUnsubscribe?.();
|
|
3426
3655
|
return () => void 0;
|
|
3427
3656
|
}
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3657
|
+
if (!isDurableResume) {
|
|
3658
|
+
try {
|
|
3659
|
+
await opts.onSnapshot(snapshotDocs, initial.latestStreamId);
|
|
3660
|
+
} catch (err) {
|
|
3661
|
+
currentUnsubscribe?.();
|
|
3662
|
+
currentUnsubscribe = null;
|
|
3663
|
+
isActive = false;
|
|
3664
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3665
|
+
opts.onError?.(error);
|
|
3666
|
+
throw error;
|
|
3667
|
+
}
|
|
3437
3668
|
}
|
|
3438
3669
|
while (true) {
|
|
3439
3670
|
if (!isActive) return () => void 0;
|
|
@@ -3820,6 +4051,7 @@ function createClient(config) {
|
|
|
3820
4051
|
}
|
|
3821
4052
|
export {
|
|
3822
4053
|
BrowserTokenStorage,
|
|
4054
|
+
CONSUMER_ID_STORAGE_KEY,
|
|
3823
4055
|
CodeChallengeMethod,
|
|
3824
4056
|
CursorInvalidError,
|
|
3825
4057
|
FileVisibility,
|
|
@@ -3844,6 +4076,7 @@ export {
|
|
|
3844
4076
|
createClient,
|
|
3845
4077
|
generatePKCEChallenge,
|
|
3846
4078
|
localStorageCursorStorage,
|
|
3847
|
-
memoryCursorStorage
|
|
4079
|
+
memoryCursorStorage,
|
|
4080
|
+
resolveConsumerId
|
|
3848
4081
|
};
|
|
3849
4082
|
//# sourceMappingURL=index.mjs.map
|