@spacelr/sdk 0.10.5 → 0.10.7
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 +47 -1
- package/dist/index.d.ts +47 -1
- package/dist/index.js +69 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1227,6 +1227,31 @@ interface SubscribeWithSnapshotOptions<T> {
|
|
|
1227
1227
|
sort?: Record<string, 1 | -1>;
|
|
1228
1228
|
limit?: number;
|
|
1229
1229
|
select?: string[];
|
|
1230
|
+
/**
|
|
1231
|
+
* #699 P1 — opt-in full-backlog catch-up. When set, the snapshot (both the
|
|
1232
|
+
* initial one and every gap-recovery one) drains **all** matching documents
|
|
1233
|
+
* via keyset pagination instead of returning a single bounded page, then
|
|
1234
|
+
* delivers them to `onSnapshot` in one call. This is what makes a long
|
|
1235
|
+
* offline window (backlog larger than one page / the Redis stream window)
|
|
1236
|
+
* fully recoverable under a document-dependent `.read`.
|
|
1237
|
+
*
|
|
1238
|
+
* Ordering is forced to `_id` ascending (the order keyset pagination
|
|
1239
|
+
* requires); `limit` becomes the per-page size. Bounded by `maxPages` /
|
|
1240
|
+
* `maxDocs` so a pathological backlog cannot drain unbounded — on reaching
|
|
1241
|
+
* either cap the snapshot is delivered partial. A server `SCAN_LIMIT_EXCEEDED`
|
|
1242
|
+
* (sparse `.filter` scan) surfaces via `onSnapshotError`.
|
|
1243
|
+
*
|
|
1244
|
+
* **Requires 24-hex ObjectId `_id`s** — the drain uses keyset `.after()`
|
|
1245
|
+
* pagination, which only supports ObjectId cursors (see `.after()`/`.before()`).
|
|
1246
|
+
* On a collection with custom string `_id`s the drain degrades (an HTTP 400
|
|
1247
|
+
* mid-drain or a partial result); do not enable `catchUp` there.
|
|
1248
|
+
*
|
|
1249
|
+
* When omitted, behavior is unchanged: one page honoring `limit`/`sort`.
|
|
1250
|
+
*/
|
|
1251
|
+
catchUp?: {
|
|
1252
|
+
maxPages?: number;
|
|
1253
|
+
maxDocs?: number;
|
|
1254
|
+
};
|
|
1230
1255
|
/**
|
|
1231
1256
|
* Called with the full snapshot:
|
|
1232
1257
|
* - once after the initial find()
|
|
@@ -1322,7 +1347,17 @@ interface SubscribeEventsHandlers<T = Record<string, unknown>> {
|
|
|
1322
1347
|
_id: string;
|
|
1323
1348
|
_eventId: string;
|
|
1324
1349
|
}) => void | Promise<void>;
|
|
1325
|
-
/**
|
|
1350
|
+
/**
|
|
1351
|
+
* Called on deletes. `document` may be absent on tombstones — only documentId
|
|
1352
|
+
* + eventId are guaranteed.
|
|
1353
|
+
*
|
|
1354
|
+
* #699 P2: on a **stream-mode** collection a *document-less* delete tombstone
|
|
1355
|
+
* (e.g. a TTL-index expiry) is now dropped fail-closed by the gateway and
|
|
1356
|
+
* will **not** reach `onDelete` at all — previously it arrived with just
|
|
1357
|
+
* documentId+eventId. Consumers that relied on TTL-eviction firing `onDelete`
|
|
1358
|
+
* should not expect it; opt-in delete delivery via change-stream pre-images
|
|
1359
|
+
* is tracked in #710. Document-bearing deletes are unaffected.
|
|
1360
|
+
*/
|
|
1326
1361
|
onDelete?: (documentId: string, eventId: string) => void | Promise<void>;
|
|
1327
1362
|
/** Called when the server emits an event-gap. See GapReason for the four codes. */
|
|
1328
1363
|
onGap?: (info: StreamGapInfo) => void;
|
|
@@ -1574,6 +1609,17 @@ declare class CollectionRef<T = Record<string, unknown>> {
|
|
|
1574
1609
|
* aborts the request and silences the AbortError. Gap-recovery state
|
|
1575
1610
|
* machine lands in Task 6.
|
|
1576
1611
|
*/
|
|
1612
|
+
/**
|
|
1613
|
+
* #699 P1 — resolve the snapshot documents for `subscribeWithSnapshot`.
|
|
1614
|
+
* Without `catchUp`, one bounded page honoring `limit`/`sort` (unchanged
|
|
1615
|
+
* behavior). With `catchUp`, keyset-drain ALL matching pages (`_id`
|
|
1616
|
+
* ascending) up to `maxPages`/`maxDocs` and return the union. All pages are
|
|
1617
|
+
* gathered before the caller delivers `onSnapshot`, so the existing
|
|
1618
|
+
* snapshot-ordering buffer only unblocks live `onChange` after the full
|
|
1619
|
+
* drain. A thrown error (e.g. `SCAN_LIMIT_EXCEEDED`, abort) propagates to the
|
|
1620
|
+
* caller's existing snapshot try/catch.
|
|
1621
|
+
*/
|
|
1622
|
+
private resolveSnapshotDocs;
|
|
1577
1623
|
subscribeWithSnapshot(opts: SubscribeWithSnapshotOptions<T>): Promise<() => void>;
|
|
1578
1624
|
}
|
|
1579
1625
|
declare class DatabaseModule {
|
package/dist/index.d.ts
CHANGED
|
@@ -1227,6 +1227,31 @@ interface SubscribeWithSnapshotOptions<T> {
|
|
|
1227
1227
|
sort?: Record<string, 1 | -1>;
|
|
1228
1228
|
limit?: number;
|
|
1229
1229
|
select?: string[];
|
|
1230
|
+
/**
|
|
1231
|
+
* #699 P1 — opt-in full-backlog catch-up. When set, the snapshot (both the
|
|
1232
|
+
* initial one and every gap-recovery one) drains **all** matching documents
|
|
1233
|
+
* via keyset pagination instead of returning a single bounded page, then
|
|
1234
|
+
* delivers them to `onSnapshot` in one call. This is what makes a long
|
|
1235
|
+
* offline window (backlog larger than one page / the Redis stream window)
|
|
1236
|
+
* fully recoverable under a document-dependent `.read`.
|
|
1237
|
+
*
|
|
1238
|
+
* Ordering is forced to `_id` ascending (the order keyset pagination
|
|
1239
|
+
* requires); `limit` becomes the per-page size. Bounded by `maxPages` /
|
|
1240
|
+
* `maxDocs` so a pathological backlog cannot drain unbounded — on reaching
|
|
1241
|
+
* either cap the snapshot is delivered partial. A server `SCAN_LIMIT_EXCEEDED`
|
|
1242
|
+
* (sparse `.filter` scan) surfaces via `onSnapshotError`.
|
|
1243
|
+
*
|
|
1244
|
+
* **Requires 24-hex ObjectId `_id`s** — the drain uses keyset `.after()`
|
|
1245
|
+
* pagination, which only supports ObjectId cursors (see `.after()`/`.before()`).
|
|
1246
|
+
* On a collection with custom string `_id`s the drain degrades (an HTTP 400
|
|
1247
|
+
* mid-drain or a partial result); do not enable `catchUp` there.
|
|
1248
|
+
*
|
|
1249
|
+
* When omitted, behavior is unchanged: one page honoring `limit`/`sort`.
|
|
1250
|
+
*/
|
|
1251
|
+
catchUp?: {
|
|
1252
|
+
maxPages?: number;
|
|
1253
|
+
maxDocs?: number;
|
|
1254
|
+
};
|
|
1230
1255
|
/**
|
|
1231
1256
|
* Called with the full snapshot:
|
|
1232
1257
|
* - once after the initial find()
|
|
@@ -1322,7 +1347,17 @@ interface SubscribeEventsHandlers<T = Record<string, unknown>> {
|
|
|
1322
1347
|
_id: string;
|
|
1323
1348
|
_eventId: string;
|
|
1324
1349
|
}) => void | Promise<void>;
|
|
1325
|
-
/**
|
|
1350
|
+
/**
|
|
1351
|
+
* Called on deletes. `document` may be absent on tombstones — only documentId
|
|
1352
|
+
* + eventId are guaranteed.
|
|
1353
|
+
*
|
|
1354
|
+
* #699 P2: on a **stream-mode** collection a *document-less* delete tombstone
|
|
1355
|
+
* (e.g. a TTL-index expiry) is now dropped fail-closed by the gateway and
|
|
1356
|
+
* will **not** reach `onDelete` at all — previously it arrived with just
|
|
1357
|
+
* documentId+eventId. Consumers that relied on TTL-eviction firing `onDelete`
|
|
1358
|
+
* should not expect it; opt-in delete delivery via change-stream pre-images
|
|
1359
|
+
* is tracked in #710. Document-bearing deletes are unaffected.
|
|
1360
|
+
*/
|
|
1326
1361
|
onDelete?: (documentId: string, eventId: string) => void | Promise<void>;
|
|
1327
1362
|
/** Called when the server emits an event-gap. See GapReason for the four codes. */
|
|
1328
1363
|
onGap?: (info: StreamGapInfo) => void;
|
|
@@ -1574,6 +1609,17 @@ declare class CollectionRef<T = Record<string, unknown>> {
|
|
|
1574
1609
|
* aborts the request and silences the AbortError. Gap-recovery state
|
|
1575
1610
|
* machine lands in Task 6.
|
|
1576
1611
|
*/
|
|
1612
|
+
/**
|
|
1613
|
+
* #699 P1 — resolve the snapshot documents for `subscribeWithSnapshot`.
|
|
1614
|
+
* Without `catchUp`, one bounded page honoring `limit`/`sort` (unchanged
|
|
1615
|
+
* behavior). With `catchUp`, keyset-drain ALL matching pages (`_id`
|
|
1616
|
+
* ascending) up to `maxPages`/`maxDocs` and return the union. All pages are
|
|
1617
|
+
* gathered before the caller delivers `onSnapshot`, so the existing
|
|
1618
|
+
* snapshot-ordering buffer only unblocks live `onChange` after the full
|
|
1619
|
+
* drain. A thrown error (e.g. `SCAN_LIMIT_EXCEEDED`, abort) propagates to the
|
|
1620
|
+
* caller's existing snapshot try/catch.
|
|
1621
|
+
*/
|
|
1622
|
+
private resolveSnapshotDocs;
|
|
1577
1623
|
subscribeWithSnapshot(opts: SubscribeWithSnapshotOptions<T>): Promise<() => void>;
|
|
1578
1624
|
}
|
|
1579
1625
|
declare class DatabaseModule {
|
package/dist/index.js
CHANGED
|
@@ -354,18 +354,27 @@ var HttpClient = class {
|
|
|
354
354
|
}
|
|
355
355
|
throwHttpError(statusCode, body) {
|
|
356
356
|
const apiBody = body;
|
|
357
|
-
const message = apiBody.error?.message ?? `HTTP ${statusCode}`;
|
|
358
|
-
const code = apiBody.error?.code ?? `HTTP_${statusCode}`;
|
|
359
|
-
const details = apiBody.error?.details;
|
|
360
357
|
const flatBody = body;
|
|
358
|
+
const flatMessage = (() => {
|
|
359
|
+
const m = flatBody["message"];
|
|
360
|
+
if (typeof m === "string" && m.length > 0) return m;
|
|
361
|
+
if (Array.isArray(m)) {
|
|
362
|
+
const parts = m.filter((x) => typeof x === "string");
|
|
363
|
+
if (parts.length > 0) return parts.join("; ");
|
|
364
|
+
}
|
|
365
|
+
return void 0;
|
|
366
|
+
})();
|
|
367
|
+
const message = apiBody.error?.message ?? flatMessage ?? `HTTP ${statusCode}`;
|
|
368
|
+
const code = apiBody.error?.code ?? `HTTP_${statusCode}`;
|
|
369
|
+
const details = apiBody.error?.details ?? (flatMessage !== void 0 ? flatBody : void 0);
|
|
361
370
|
if (statusCode === 400 && flatBody["errorCode"] === "SEARCH_FILTER_REQUIRED") {
|
|
362
|
-
const
|
|
371
|
+
const flatMessage2 = typeof flatBody["message"] === "string" ? flatBody["message"] : message;
|
|
363
372
|
const missingFields = Array.isArray(flatBody["missingFields"]) ? flatBody["missingFields"] : [];
|
|
364
|
-
throw new SpacelrSearchFilterRequiredError(
|
|
373
|
+
throw new SpacelrSearchFilterRequiredError(flatMessage2, { missingFields });
|
|
365
374
|
}
|
|
366
375
|
if (statusCode === 400 && flatBody["errorCode"] === "SCAN_LIMIT_EXCEEDED") {
|
|
367
|
-
const
|
|
368
|
-
throw new SpacelrScanLimitExceededError(
|
|
376
|
+
const flatMessage2 = typeof flatBody["message"] === "string" ? flatBody["message"] : message;
|
|
377
|
+
throw new SpacelrScanLimitExceededError(flatMessage2);
|
|
369
378
|
}
|
|
370
379
|
if (statusCode === 401 || statusCode === 403) {
|
|
371
380
|
throw new SpacelrAuthError(message, statusCode, details);
|
|
@@ -2312,6 +2321,9 @@ function isTimelineSourceStats(value) {
|
|
|
2312
2321
|
}
|
|
2313
2322
|
|
|
2314
2323
|
// libs/sdk/src/modules/database.module.ts
|
|
2324
|
+
var DEFAULT_CATCHUP_MAX_PAGES = 1e3;
|
|
2325
|
+
var DEFAULT_CATCHUP_MAX_DOCS = 5e4;
|
|
2326
|
+
var DEFAULT_CATCHUP_PAGE_SIZE = 100;
|
|
2315
2327
|
var Paginator = class {
|
|
2316
2328
|
constructor(http, basePath, opts) {
|
|
2317
2329
|
this.http = http;
|
|
@@ -2749,6 +2761,54 @@ var CollectionRef = class {
|
|
|
2749
2761
|
* aborts the request and silences the AbortError. Gap-recovery state
|
|
2750
2762
|
* machine lands in Task 6.
|
|
2751
2763
|
*/
|
|
2764
|
+
/**
|
|
2765
|
+
* #699 P1 — resolve the snapshot documents for `subscribeWithSnapshot`.
|
|
2766
|
+
* Without `catchUp`, one bounded page honoring `limit`/`sort` (unchanged
|
|
2767
|
+
* behavior). With `catchUp`, keyset-drain ALL matching pages (`_id`
|
|
2768
|
+
* ascending) up to `maxPages`/`maxDocs` and return the union. All pages are
|
|
2769
|
+
* gathered before the caller delivers `onSnapshot`, so the existing
|
|
2770
|
+
* snapshot-ordering buffer only unblocks live `onChange` after the full
|
|
2771
|
+
* drain. A thrown error (e.g. `SCAN_LIMIT_EXCEEDED`, abort) propagates to the
|
|
2772
|
+
* caller's existing snapshot try/catch.
|
|
2773
|
+
*/
|
|
2774
|
+
async resolveSnapshotDocs(opts, signal) {
|
|
2775
|
+
if (!opts.catchUp) {
|
|
2776
|
+
const builder = this.find(opts.where);
|
|
2777
|
+
if (opts.sort) builder.sort(opts.sort);
|
|
2778
|
+
if (opts.limit !== void 0) builder.limit(opts.limit);
|
|
2779
|
+
if (opts.select) builder.select(opts.select);
|
|
2780
|
+
const result = await builder.execute(signal);
|
|
2781
|
+
return result.documents;
|
|
2782
|
+
}
|
|
2783
|
+
const asCap = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 0 ? Math.floor(v) : def;
|
|
2784
|
+
const asPageSize = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 1 ? Math.floor(v) : def;
|
|
2785
|
+
const maxPages = asCap(opts.catchUp.maxPages, DEFAULT_CATCHUP_MAX_PAGES);
|
|
2786
|
+
const maxDocs = asCap(opts.catchUp.maxDocs, DEFAULT_CATCHUP_MAX_DOCS);
|
|
2787
|
+
const pageSize = asPageSize(opts.limit, DEFAULT_CATCHUP_PAGE_SIZE);
|
|
2788
|
+
const all = [];
|
|
2789
|
+
let cursor;
|
|
2790
|
+
for (let page = 0; page < maxPages && all.length < maxDocs; page++) {
|
|
2791
|
+
const builder = this.find(opts.where).sort({ _id: 1 }).limit(pageSize);
|
|
2792
|
+
if (opts.select) builder.select(opts.select);
|
|
2793
|
+
const result = cursor === void 0 ? await builder.execute(signal) : await builder.after(cursor).execute(signal);
|
|
2794
|
+
const docs = result.documents;
|
|
2795
|
+
if (docs.length === 0) break;
|
|
2796
|
+
const room = maxDocs - all.length;
|
|
2797
|
+
if (docs.length >= room) {
|
|
2798
|
+
all.push(...docs.slice(0, room));
|
|
2799
|
+
break;
|
|
2800
|
+
}
|
|
2801
|
+
all.push(...docs);
|
|
2802
|
+
const more = result.mode === "cursor" ? result.hasMore : result.hasMore ?? docs.length >= pageSize;
|
|
2803
|
+
if (!more) break;
|
|
2804
|
+
const nextCursor = docs[docs.length - 1]._id;
|
|
2805
|
+
if (nextCursor === void 0 || nextCursor === null || cursor !== void 0 && nextCursor <= cursor) {
|
|
2806
|
+
break;
|
|
2807
|
+
}
|
|
2808
|
+
cursor = nextCursor;
|
|
2809
|
+
}
|
|
2810
|
+
return all;
|
|
2811
|
+
}
|
|
2752
2812
|
async subscribeWithSnapshot(opts) {
|
|
2753
2813
|
if (!this.realtime) {
|
|
2754
2814
|
throw new Error("Realtime not available: no RealtimeClient configured");
|
|
@@ -2820,12 +2880,7 @@ var CollectionRef = class {
|
|
|
2820
2880
|
snapshotAbort = new AbortController();
|
|
2821
2881
|
let recoveryDocs;
|
|
2822
2882
|
try {
|
|
2823
|
-
|
|
2824
|
-
if (opts.sort) builder.sort(opts.sort);
|
|
2825
|
-
if (opts.limit !== void 0) builder.limit(opts.limit);
|
|
2826
|
-
if (opts.select) builder.select(opts.select);
|
|
2827
|
-
const result = await builder.execute(snapshotAbort.signal);
|
|
2828
|
-
recoveryDocs = result.documents;
|
|
2883
|
+
recoveryDocs = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
|
|
2829
2884
|
} catch (err) {
|
|
2830
2885
|
if (!isActive) return;
|
|
2831
2886
|
if (err instanceof DOMException && err.name === "AbortError") return;
|
|
@@ -2968,12 +3023,7 @@ var CollectionRef = class {
|
|
|
2968
3023
|
currentUnsubscribe = initial.unsubscribe;
|
|
2969
3024
|
let snapshotDocs;
|
|
2970
3025
|
try {
|
|
2971
|
-
|
|
2972
|
-
if (opts.sort) builder.sort(opts.sort);
|
|
2973
|
-
if (opts.limit !== void 0) builder.limit(opts.limit);
|
|
2974
|
-
if (opts.select) builder.select(opts.select);
|
|
2975
|
-
const result = await builder.execute(snapshotAbort.signal);
|
|
2976
|
-
snapshotDocs = result.documents;
|
|
3026
|
+
snapshotDocs = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
|
|
2977
3027
|
} catch (err) {
|
|
2978
3028
|
currentUnsubscribe?.();
|
|
2979
3029
|
isActive = false;
|