@spacelr/sdk 0.10.10 → 0.10.11
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 +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +365 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +365 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -566,6 +566,17 @@ interface DatabaseChangeEvent {
|
|
|
566
566
|
* for pubsub-mode events. Used by function triggers as an idempotency key.
|
|
567
567
|
*/
|
|
568
568
|
eventId?: string;
|
|
569
|
+
/**
|
|
570
|
+
* Actual-BSON-type sidecar (#725). Envelope-only. Consumed by the typed
|
|
571
|
+
* `where` matcher in a later release; carried here so older matchers ignore
|
|
572
|
+
* it harmlessly. Kept in sync with @spacelr-workspace/shared-types.
|
|
573
|
+
*/
|
|
574
|
+
__t?: {
|
|
575
|
+
fields?: Record<string, 'date' | 'objectId' | {
|
|
576
|
+
array: Array<'date' | 'objectId' | 'other'>;
|
|
577
|
+
}>;
|
|
578
|
+
incomplete?: true;
|
|
579
|
+
};
|
|
569
580
|
}
|
|
570
581
|
interface RealtimeConfig {
|
|
571
582
|
baseUrl: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -566,6 +566,17 @@ interface DatabaseChangeEvent {
|
|
|
566
566
|
* for pubsub-mode events. Used by function triggers as an idempotency key.
|
|
567
567
|
*/
|
|
568
568
|
eventId?: string;
|
|
569
|
+
/**
|
|
570
|
+
* Actual-BSON-type sidecar (#725). Envelope-only. Consumed by the typed
|
|
571
|
+
* `where` matcher in a later release; carried here so older matchers ignore
|
|
572
|
+
* it harmlessly. Kept in sync with @spacelr-workspace/shared-types.
|
|
573
|
+
*/
|
|
574
|
+
__t?: {
|
|
575
|
+
fields?: Record<string, 'date' | 'objectId' | {
|
|
576
|
+
array: Array<'date' | 'objectId' | 'other'>;
|
|
577
|
+
}>;
|
|
578
|
+
incomplete?: true;
|
|
579
|
+
};
|
|
569
580
|
}
|
|
570
581
|
interface RealtimeConfig {
|
|
571
582
|
baseUrl: string;
|
package/dist/index.js
CHANGED
|
@@ -728,6 +728,33 @@ function whereValueMatches(actual, expected) {
|
|
|
728
728
|
if (Array.isArray(actual)) return actual.includes(expected);
|
|
729
729
|
return actual === expected;
|
|
730
730
|
}
|
|
731
|
+
var TYPED_EQUALITY_MAX_COMPARAND_LENGTH = 64;
|
|
732
|
+
var TYPED_EQUALITY_RFC3339_WITH_OFFSET = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
733
|
+
var TYPED_EQUALITY_OBJECT_ID_HEX = /^[0-9a-f]{24}$/i;
|
|
734
|
+
var NEVER_TYPED_FIELD = "_id";
|
|
735
|
+
function resolveFieldTypeTag(t, key) {
|
|
736
|
+
if (key === NEVER_TYPED_FIELD) return void 0;
|
|
737
|
+
if (!t || t.incomplete) return void 0;
|
|
738
|
+
const tag = t.fields?.[key];
|
|
739
|
+
return tag === "date" || tag === "objectId" ? tag : void 0;
|
|
740
|
+
}
|
|
741
|
+
function whereValueMatchesTyped(actual, expected, fieldType) {
|
|
742
|
+
if (fieldType === "date" && typeof actual === "string" && typeof expected === "string") {
|
|
743
|
+
if (actual.length <= TYPED_EQUALITY_MAX_COMPARAND_LENGTH && expected.length <= TYPED_EQUALITY_MAX_COMPARAND_LENGTH && TYPED_EQUALITY_RFC3339_WITH_OFFSET.test(actual) && TYPED_EQUALITY_RFC3339_WITH_OFFSET.test(expected)) {
|
|
744
|
+
const a = Date.parse(actual);
|
|
745
|
+
const e = Date.parse(expected);
|
|
746
|
+
if (!Number.isNaN(a) && !Number.isNaN(e)) return a === e;
|
|
747
|
+
}
|
|
748
|
+
return whereValueMatches(actual, expected);
|
|
749
|
+
}
|
|
750
|
+
if (fieldType === "objectId" && typeof actual === "string" && typeof expected === "string") {
|
|
751
|
+
if (actual.length <= TYPED_EQUALITY_MAX_COMPARAND_LENGTH && expected.length <= TYPED_EQUALITY_MAX_COMPARAND_LENGTH && TYPED_EQUALITY_OBJECT_ID_HEX.test(actual) && TYPED_EQUALITY_OBJECT_ID_HEX.test(expected)) {
|
|
752
|
+
return actual.toLowerCase() === expected.toLowerCase();
|
|
753
|
+
}
|
|
754
|
+
return whereValueMatches(actual, expected);
|
|
755
|
+
}
|
|
756
|
+
return whereValueMatches(actual, expected);
|
|
757
|
+
}
|
|
731
758
|
function isFiniteScalar(v) {
|
|
732
759
|
const t = typeof v;
|
|
733
760
|
if (t === "string" || t === "boolean") return true;
|
|
@@ -1113,7 +1140,8 @@ var RealtimeClient = class {
|
|
|
1113
1140
|
if (!where || Object.keys(where).length === 0) return true;
|
|
1114
1141
|
if (!event.document) return true;
|
|
1115
1142
|
for (const [key, value] of Object.entries(where)) {
|
|
1116
|
-
|
|
1143
|
+
const fieldType = resolveFieldTypeTag(event.__t, key);
|
|
1144
|
+
if (!whereValueMatchesTyped(resolveWherePath(event.document, key), value, fieldType)) {
|
|
1117
1145
|
return false;
|
|
1118
1146
|
}
|
|
1119
1147
|
}
|
|
@@ -1279,7 +1307,8 @@ var RealtimeClient = class {
|
|
|
1279
1307
|
if (!where) return false;
|
|
1280
1308
|
if (!event.document) return false;
|
|
1281
1309
|
for (const [key, value] of Object.entries(where)) {
|
|
1282
|
-
|
|
1310
|
+
const fieldType = resolveFieldTypeTag(event.__t, key);
|
|
1311
|
+
if (!whereValueMatchesTyped(resolveWherePath(event.document, key), value, fieldType)) {
|
|
1283
1312
|
return false;
|
|
1284
1313
|
}
|
|
1285
1314
|
}
|
|
@@ -2392,6 +2421,262 @@ function isTimelineSourceStats(value) {
|
|
|
2392
2421
|
return true;
|
|
2393
2422
|
}
|
|
2394
2423
|
|
|
2424
|
+
// libs/sdk/src/core/typed-matcher.ts
|
|
2425
|
+
var MAX_WHERE_DEPTH2 = 10;
|
|
2426
|
+
var RESERVED_FIELD_NAMES = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
2427
|
+
var SCALAR_OPS = ["$eq", "$ne", "$gt", "$gte", "$lt", "$lte"];
|
|
2428
|
+
var ARRAY_ELEMENT_OPS = ["$in", "$nin"];
|
|
2429
|
+
function isScalarOp(op) {
|
|
2430
|
+
return SCALAR_OPS.includes(op);
|
|
2431
|
+
}
|
|
2432
|
+
function isArrayElementOp(op) {
|
|
2433
|
+
return ARRAY_ELEMENT_OPS.includes(op);
|
|
2434
|
+
}
|
|
2435
|
+
function isPlainObject(value) {
|
|
2436
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2437
|
+
}
|
|
2438
|
+
function isSafeComparableNumber(n) {
|
|
2439
|
+
return Number.isFinite(n) && Math.abs(n) <= Number.MAX_SAFE_INTEGER;
|
|
2440
|
+
}
|
|
2441
|
+
function compileComparand(leaf, rawValue, allowScalar) {
|
|
2442
|
+
if (leaf?.kind === "date") return { kind: "date", epochMs: leaf.epochMs };
|
|
2443
|
+
if (leaf?.kind === "objectId") return { kind: "objectId", hex: leaf.hex };
|
|
2444
|
+
if (leaf?.kind === "raw") return null;
|
|
2445
|
+
if (typeof rawValue === "number" && isSafeComparableNumber(rawValue)) return { kind: "number", value: rawValue };
|
|
2446
|
+
if (allowScalar && (typeof rawValue === "string" || typeof rawValue === "boolean")) {
|
|
2447
|
+
return { kind: "scalar", value: rawValue };
|
|
2448
|
+
}
|
|
2449
|
+
return null;
|
|
2450
|
+
}
|
|
2451
|
+
function comparableValue(comparand) {
|
|
2452
|
+
switch (comparand.kind) {
|
|
2453
|
+
case "date":
|
|
2454
|
+
return comparand.epochMs;
|
|
2455
|
+
case "objectId":
|
|
2456
|
+
return comparand.hex;
|
|
2457
|
+
case "number":
|
|
2458
|
+
return comparand.value;
|
|
2459
|
+
case "scalar":
|
|
2460
|
+
return comparand.value;
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
function scalarActualKind(raw, tag, untrusted) {
|
|
2464
|
+
if (untrusted) return "unknown";
|
|
2465
|
+
if (tag === "date") return "date";
|
|
2466
|
+
if (tag === "objectId") return "objectId";
|
|
2467
|
+
if (typeof raw === "number") return isSafeComparableNumber(raw) ? "number" : "unknown";
|
|
2468
|
+
if (typeof raw === "string" || typeof raw === "boolean") return "scalar";
|
|
2469
|
+
return "unknown";
|
|
2470
|
+
}
|
|
2471
|
+
function comparandNeedsTag(comparand) {
|
|
2472
|
+
return comparand.kind === "date" || comparand.kind === "objectId";
|
|
2473
|
+
}
|
|
2474
|
+
function isUntrusted(t, comparand) {
|
|
2475
|
+
if (t?.incomplete === true) return true;
|
|
2476
|
+
return comparandNeedsTag(comparand) && t?.fields === void 0;
|
|
2477
|
+
}
|
|
2478
|
+
function toEpochMs(raw) {
|
|
2479
|
+
if (typeof raw === "string") {
|
|
2480
|
+
const parsed = Date.parse(raw);
|
|
2481
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
2482
|
+
}
|
|
2483
|
+
if (typeof raw === "number" && Number.isFinite(raw)) return raw;
|
|
2484
|
+
return void 0;
|
|
2485
|
+
}
|
|
2486
|
+
function resolveScalar(comparand, present, raw, actualKind) {
|
|
2487
|
+
if (!present) return { status: "absent" };
|
|
2488
|
+
if (actualKind === "unknown") return { status: "unknown" };
|
|
2489
|
+
if (comparand.kind === "date") {
|
|
2490
|
+
if (actualKind !== "date") return { status: "cross" };
|
|
2491
|
+
const epoch = toEpochMs(raw);
|
|
2492
|
+
if (epoch === void 0) return { status: "unknown" };
|
|
2493
|
+
return { status: "ok", value: epoch };
|
|
2494
|
+
}
|
|
2495
|
+
if (comparand.kind === "objectId") {
|
|
2496
|
+
if (actualKind !== "objectId") return { status: "cross" };
|
|
2497
|
+
if (typeof raw !== "string") return { status: "unknown" };
|
|
2498
|
+
return { status: "ok", value: raw.toLowerCase() };
|
|
2499
|
+
}
|
|
2500
|
+
if (actualKind === "date" || actualKind === "objectId") return { status: "unknown" };
|
|
2501
|
+
if (comparand.kind === "number") {
|
|
2502
|
+
if (actualKind !== "number") return { status: "cross" };
|
|
2503
|
+
return { status: "ok", value: raw };
|
|
2504
|
+
}
|
|
2505
|
+
if (actualKind !== "scalar" || typeof raw !== typeof comparand.value) return { status: "cross" };
|
|
2506
|
+
return { status: "ok", value: raw };
|
|
2507
|
+
}
|
|
2508
|
+
function confirmedEqual(resolved, comparand) {
|
|
2509
|
+
return resolved.status === "ok" && resolved.value === comparableValue(comparand);
|
|
2510
|
+
}
|
|
2511
|
+
function maybeEqual(resolved, comparand) {
|
|
2512
|
+
return resolved.status === "unknown" || confirmedEqual(resolved, comparand);
|
|
2513
|
+
}
|
|
2514
|
+
function compareOrder(a, b) {
|
|
2515
|
+
if (a === b) return 0;
|
|
2516
|
+
return a < b ? -1 : 1;
|
|
2517
|
+
}
|
|
2518
|
+
function satisfiesRange(op, cmp) {
|
|
2519
|
+
switch (op) {
|
|
2520
|
+
case "$gt":
|
|
2521
|
+
return cmp > 0;
|
|
2522
|
+
case "$gte":
|
|
2523
|
+
return cmp >= 0;
|
|
2524
|
+
case "$lt":
|
|
2525
|
+
return cmp < 0;
|
|
2526
|
+
case "$lte":
|
|
2527
|
+
return cmp <= 0;
|
|
2528
|
+
default:
|
|
2529
|
+
return false;
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
function maybeSatisfiesRange(resolved, comparand, op) {
|
|
2533
|
+
if (resolved.status === "unknown") return true;
|
|
2534
|
+
if (resolved.status !== "ok") return false;
|
|
2535
|
+
return satisfiesRange(op, compareOrder(resolved.value, comparableValue(comparand)));
|
|
2536
|
+
}
|
|
2537
|
+
function fieldPresent(document2, field) {
|
|
2538
|
+
return Object.prototype.hasOwnProperty.call(document2, field);
|
|
2539
|
+
}
|
|
2540
|
+
function fieldTag(t, field) {
|
|
2541
|
+
return t?.fields?.[field];
|
|
2542
|
+
}
|
|
2543
|
+
function isArrayTag(tag) {
|
|
2544
|
+
return typeof tag === "object" && tag !== null && "array" in tag;
|
|
2545
|
+
}
|
|
2546
|
+
function evalScalarLeaf(field, op, comparand) {
|
|
2547
|
+
return (document2, t) => {
|
|
2548
|
+
const present = fieldPresent(document2, field);
|
|
2549
|
+
const raw = present ? document2[field] : void 0;
|
|
2550
|
+
const untrusted = isUntrusted(t, comparand);
|
|
2551
|
+
const tag = fieldTag(t, field);
|
|
2552
|
+
if (present && Array.isArray(raw)) {
|
|
2553
|
+
const arrayTag = isArrayTag(tag) ? tag.array : void 0;
|
|
2554
|
+
return evalArrayLeaf(raw, arrayTag, untrusted, op, comparand);
|
|
2555
|
+
}
|
|
2556
|
+
const scalarTag = isArrayTag(tag) ? void 0 : tag;
|
|
2557
|
+
const actualKind = scalarActualKind(raw, scalarTag, untrusted);
|
|
2558
|
+
const resolved = resolveScalar(comparand, present, raw, actualKind);
|
|
2559
|
+
if (op === "$ne") return !confirmedEqual(resolved, comparand);
|
|
2560
|
+
if (op === "$eq") return maybeEqual(resolved, comparand);
|
|
2561
|
+
return maybeSatisfiesRange(resolved, comparand, op);
|
|
2562
|
+
};
|
|
2563
|
+
}
|
|
2564
|
+
function evalArrayLeaf(raw, arrayTag, untrusted, op, comparand) {
|
|
2565
|
+
const resolvedElements = raw.map((el, i) => {
|
|
2566
|
+
const tag = arrayTag?.[i];
|
|
2567
|
+
const scalarTag = tag === "date" || tag === "objectId" ? tag : void 0;
|
|
2568
|
+
const actualKind = scalarActualKind(el, scalarTag, untrusted);
|
|
2569
|
+
return resolveScalar(comparand, true, el, actualKind);
|
|
2570
|
+
});
|
|
2571
|
+
if (op === "$ne") {
|
|
2572
|
+
return !resolvedElements.some((r) => confirmedEqual(r, comparand));
|
|
2573
|
+
}
|
|
2574
|
+
if (op === "$eq") {
|
|
2575
|
+
return resolvedElements.some((r) => maybeEqual(r, comparand));
|
|
2576
|
+
}
|
|
2577
|
+
return resolvedElements.some((r) => maybeSatisfiesRange(r, comparand, op));
|
|
2578
|
+
}
|
|
2579
|
+
function evalArrayElementOpLeaf(field, op, comparands) {
|
|
2580
|
+
return (document2, t) => {
|
|
2581
|
+
const present = fieldPresent(document2, field);
|
|
2582
|
+
const raw = present ? document2[field] : void 0;
|
|
2583
|
+
const tag = fieldTag(t, field);
|
|
2584
|
+
const evalOne = (comparand) => {
|
|
2585
|
+
const untrusted = isUntrusted(t, comparand);
|
|
2586
|
+
if (present && Array.isArray(raw)) {
|
|
2587
|
+
const arrayTag = isArrayTag(tag) ? tag.array : void 0;
|
|
2588
|
+
const resolvedElements = raw.map((el, i) => {
|
|
2589
|
+
const elTag = arrayTag?.[i];
|
|
2590
|
+
const scalarTag2 = elTag === "date" || elTag === "objectId" ? elTag : void 0;
|
|
2591
|
+
const actualKind2 = scalarActualKind(el, scalarTag2, untrusted);
|
|
2592
|
+
return resolveScalar(comparand, true, el, actualKind2);
|
|
2593
|
+
});
|
|
2594
|
+
return {
|
|
2595
|
+
any: resolvedElements.some((r) => maybeEqual(r, comparand)),
|
|
2596
|
+
confirmed: resolvedElements.some((r) => confirmedEqual(r, comparand))
|
|
2597
|
+
};
|
|
2598
|
+
}
|
|
2599
|
+
const scalarTag = isArrayTag(tag) ? void 0 : tag;
|
|
2600
|
+
const actualKind = scalarActualKind(raw, scalarTag, untrusted);
|
|
2601
|
+
const resolved = resolveScalar(comparand, present, raw, actualKind);
|
|
2602
|
+
return { any: maybeEqual(resolved, comparand), confirmed: confirmedEqual(resolved, comparand) };
|
|
2603
|
+
};
|
|
2604
|
+
const perCandidate = comparands.map(evalOne);
|
|
2605
|
+
const inMatch = perCandidate.some((r) => r.any);
|
|
2606
|
+
const confirmedAny = perCandidate.some((r) => r.confirmed);
|
|
2607
|
+
return op === "$in" ? inMatch : !confirmedAny;
|
|
2608
|
+
};
|
|
2609
|
+
}
|
|
2610
|
+
function compileFieldClause(field, value, plan, pathPrefix) {
|
|
2611
|
+
if (field.includes(".") || RESERVED_FIELD_NAMES.has(field) || field.startsWith("$")) return null;
|
|
2612
|
+
const planPath = `${pathPrefix}${field}`;
|
|
2613
|
+
if (!isPlainObject(value)) {
|
|
2614
|
+
const comparand = compileComparand(plan[planPath], value, true);
|
|
2615
|
+
if (!comparand) return null;
|
|
2616
|
+
return evalScalarLeaf(field, "$eq", comparand);
|
|
2617
|
+
}
|
|
2618
|
+
const entries = Object.entries(value);
|
|
2619
|
+
const predicates = [];
|
|
2620
|
+
for (const [op, opValue] of entries) {
|
|
2621
|
+
if (!op.startsWith("$")) return null;
|
|
2622
|
+
if (isScalarOp(op)) {
|
|
2623
|
+
const leaf = plan[`${planPath}.${op}`];
|
|
2624
|
+
const allowScalar = op === "$eq";
|
|
2625
|
+
const comparand = compileComparand(leaf, opValue, allowScalar);
|
|
2626
|
+
if (!comparand) return null;
|
|
2627
|
+
predicates.push(evalScalarLeaf(field, op, comparand));
|
|
2628
|
+
continue;
|
|
2629
|
+
}
|
|
2630
|
+
if (isArrayElementOp(op)) {
|
|
2631
|
+
if (!Array.isArray(opValue) || opValue.length === 0) return null;
|
|
2632
|
+
const allowScalar = op === "$in";
|
|
2633
|
+
const comparands = [];
|
|
2634
|
+
for (let i = 0; i < opValue.length; i += 1) {
|
|
2635
|
+
const leaf = plan[`${planPath}.${op}.${i}`];
|
|
2636
|
+
const comparand = compileComparand(leaf, opValue[i], allowScalar);
|
|
2637
|
+
if (!comparand) return null;
|
|
2638
|
+
comparands.push(comparand);
|
|
2639
|
+
}
|
|
2640
|
+
predicates.push(evalArrayElementOpLeaf(field, op, comparands));
|
|
2641
|
+
continue;
|
|
2642
|
+
}
|
|
2643
|
+
return null;
|
|
2644
|
+
}
|
|
2645
|
+
if (predicates.length === 0) return null;
|
|
2646
|
+
return (document2, t) => predicates.every((p) => p(document2, t));
|
|
2647
|
+
}
|
|
2648
|
+
function compileWhereObject(where, plan, depth, pathPrefix) {
|
|
2649
|
+
if (depth > MAX_WHERE_DEPTH2) return null;
|
|
2650
|
+
const predicates = [];
|
|
2651
|
+
for (const [key, value] of Object.entries(where)) {
|
|
2652
|
+
if (key === "$and" || key === "$or") {
|
|
2653
|
+
if (!Array.isArray(value) || value.length === 0) return null;
|
|
2654
|
+
const subPredicates = [];
|
|
2655
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
2656
|
+
const clause = value[i];
|
|
2657
|
+
if (!isPlainObject(clause)) return null;
|
|
2658
|
+
const compiled2 = compileWhereObject(clause, plan, depth + 1, `${pathPrefix}${key}.${i}.`);
|
|
2659
|
+
if (!compiled2) return null;
|
|
2660
|
+
subPredicates.push(compiled2);
|
|
2661
|
+
}
|
|
2662
|
+
predicates.push(
|
|
2663
|
+
key === "$and" ? (document2, t) => subPredicates.every((p) => p(document2, t)) : (document2, t) => subPredicates.some((p) => p(document2, t))
|
|
2664
|
+
);
|
|
2665
|
+
continue;
|
|
2666
|
+
}
|
|
2667
|
+
if (key.startsWith("$")) return null;
|
|
2668
|
+
const compiled = compileFieldClause(key, value, plan, pathPrefix);
|
|
2669
|
+
if (!compiled) return null;
|
|
2670
|
+
predicates.push(compiled);
|
|
2671
|
+
}
|
|
2672
|
+
return (document2, t) => predicates.every((p) => p(document2, t));
|
|
2673
|
+
}
|
|
2674
|
+
function compileMatcher(where, plan) {
|
|
2675
|
+
const compiled = compileWhereObject(where, plan, 0, "");
|
|
2676
|
+
if (!compiled) return null;
|
|
2677
|
+
return { matches: (document2, t) => compiled(document2, t) };
|
|
2678
|
+
}
|
|
2679
|
+
|
|
2395
2680
|
// libs/sdk/src/modules/database.module.ts
|
|
2396
2681
|
var DEFAULT_CATCHUP_MAX_PAGES = 1e3;
|
|
2397
2682
|
var DEFAULT_CATCHUP_MAX_DOCS = 5e4;
|
|
@@ -2458,6 +2743,10 @@ var Paginator = class {
|
|
|
2458
2743
|
return { documents: result.documents, hasMore };
|
|
2459
2744
|
}
|
|
2460
2745
|
};
|
|
2746
|
+
var typedWhereV1Flags = /* @__PURE__ */ new WeakMap();
|
|
2747
|
+
function enableTypedWhereV1(builder) {
|
|
2748
|
+
typedWhereV1Flags.set(builder, true);
|
|
2749
|
+
}
|
|
2461
2750
|
var QueryBuilder = class {
|
|
2462
2751
|
constructor(http, basePath, filter) {
|
|
2463
2752
|
this.http = http;
|
|
@@ -2540,6 +2829,7 @@ var QueryBuilder = class {
|
|
|
2540
2829
|
if (this._before !== void 0) query["before"] = this._before;
|
|
2541
2830
|
if (this._after !== void 0) query["after"] = this._after;
|
|
2542
2831
|
if (this._fields) query["fields"] = this._fields.join(",");
|
|
2832
|
+
if (typedWhereV1Flags.get(this)) query["typedWhereV1"] = "true";
|
|
2543
2833
|
if (this._populate.length) {
|
|
2544
2834
|
query["populate"] = this._populate.map(
|
|
2545
2835
|
(p) => p.foreignField ? `${p.field}:${p.collection}:${p.foreignField}` : `${p.field}:${p.collection}`
|
|
@@ -2849,8 +3139,9 @@ var CollectionRef = class {
|
|
|
2849
3139
|
if (opts.sort) builder.sort(opts.sort);
|
|
2850
3140
|
if (opts.limit !== void 0) builder.limit(opts.limit);
|
|
2851
3141
|
if (opts.select) builder.select(opts.select);
|
|
3142
|
+
enableTypedWhereV1(builder);
|
|
2852
3143
|
const result = await builder.execute(signal);
|
|
2853
|
-
return result.documents;
|
|
3144
|
+
return { documents: result.documents, coercionPlan: result.coercionPlan };
|
|
2854
3145
|
}
|
|
2855
3146
|
const asCap = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 0 ? Math.floor(v) : def;
|
|
2856
3147
|
const asPageSize = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 1 ? Math.floor(v) : def;
|
|
@@ -2859,10 +3150,21 @@ var CollectionRef = class {
|
|
|
2859
3150
|
const pageSize = asPageSize(opts.limit, DEFAULT_CATCHUP_PAGE_SIZE);
|
|
2860
3151
|
const all = [];
|
|
2861
3152
|
let cursor;
|
|
3153
|
+
let coercionPlan;
|
|
3154
|
+
let coercionPlanCaptured = false;
|
|
3155
|
+
let coercionPlanMismatch = false;
|
|
2862
3156
|
for (let page = 0; page < maxPages && all.length < maxDocs; page++) {
|
|
2863
3157
|
const builder = this.find(opts.where).sort({ _id: 1 }).limit(pageSize);
|
|
2864
3158
|
if (opts.select) builder.select(opts.select);
|
|
3159
|
+
enableTypedWhereV1(builder);
|
|
2865
3160
|
const result = cursor === void 0 ? await builder.execute(signal) : await builder.after(cursor).execute(signal);
|
|
3161
|
+
if (!coercionPlanCaptured) {
|
|
3162
|
+
coercionPlan = result.coercionPlan;
|
|
3163
|
+
coercionPlanCaptured = true;
|
|
3164
|
+
} else if (!coercionPlanMismatch && JSON.stringify(result.coercionPlan) !== JSON.stringify(coercionPlan)) {
|
|
3165
|
+
coercionPlanMismatch = true;
|
|
3166
|
+
coercionPlan = void 0;
|
|
3167
|
+
}
|
|
2866
3168
|
const docs = result.documents;
|
|
2867
3169
|
if (docs.length === 0) break;
|
|
2868
3170
|
const room = maxDocs - all.length;
|
|
@@ -2879,7 +3181,7 @@ var CollectionRef = class {
|
|
|
2879
3181
|
}
|
|
2880
3182
|
cursor = nextCursor;
|
|
2881
3183
|
}
|
|
2882
|
-
return all;
|
|
3184
|
+
return { documents: all, coercionPlan };
|
|
2883
3185
|
}
|
|
2884
3186
|
async subscribeWithSnapshot(opts) {
|
|
2885
3187
|
if (!this.realtime) {
|
|
@@ -2894,7 +3196,7 @@ var CollectionRef = class {
|
|
|
2894
3196
|
if (!opts.where) return void 0;
|
|
2895
3197
|
const out = {};
|
|
2896
3198
|
for (const [k, v] of Object.entries(opts.where)) {
|
|
2897
|
-
if (typeof v === "
|
|
3199
|
+
if (typeof v === "number" || typeof v === "boolean" || typeof v === "string") {
|
|
2898
3200
|
out[k] = v;
|
|
2899
3201
|
}
|
|
2900
3202
|
}
|
|
@@ -2902,11 +3204,39 @@ var CollectionRef = class {
|
|
|
2902
3204
|
})();
|
|
2903
3205
|
const postFilterWhere = opts.where && Object.keys(opts.where).length > 0 && isMongoWhereFullySupported(opts.where) ? opts.where : void 0;
|
|
2904
3206
|
const postFilter = postFilterWhere !== void 0;
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
);
|
|
2909
|
-
|
|
3207
|
+
let typedMatcherPlan = null;
|
|
3208
|
+
let planCoercedSomething = false;
|
|
3209
|
+
const recomputeTypedMatcherPlan = (coercionPlan) => {
|
|
3210
|
+
planCoercedSomething = coercionPlan ? Object.values(coercionPlan).some((leaf) => leaf.kind !== "raw") : false;
|
|
3211
|
+
typedMatcherPlan = opts.where && Object.keys(opts.where).length > 0 && coercionPlan ? compileMatcher(opts.where, coercionPlan) : null;
|
|
3212
|
+
};
|
|
3213
|
+
const passesLiveGate = (event) => {
|
|
3214
|
+
if (typedMatcherPlan) {
|
|
3215
|
+
return !!event.document && typedMatcherPlan.matches(event.document, event.__t);
|
|
3216
|
+
}
|
|
3217
|
+
if (planCoercedSomething) {
|
|
3218
|
+
return true;
|
|
3219
|
+
}
|
|
3220
|
+
if (postFilterWhere) {
|
|
3221
|
+
return !!event.document && matchesMongoWhere(event.document, postFilterWhere);
|
|
3222
|
+
}
|
|
3223
|
+
return true;
|
|
3224
|
+
};
|
|
3225
|
+
let warnedAboutFallback = false;
|
|
3226
|
+
const warnIfNoGateAvailable = () => {
|
|
3227
|
+
if (warnedAboutFallback) return;
|
|
3228
|
+
if (opts.where && Object.keys(opts.where).length > 0 && !typedMatcherPlan && // No usable gate: either the #719 subset check failed, OR the snapshot
|
|
3229
|
+
// coerced a leaf but no typed matcher compiled (#725 C-1) — in the
|
|
3230
|
+
// latter case `passesLiveGate` deliberately over-delivers, which is
|
|
3231
|
+
// exactly the "onChange volume higher than expected" the warning
|
|
3232
|
+
// exists to explain.
|
|
3233
|
+
(!postFilter || planCoercedSomething)) {
|
|
3234
|
+
warnedAboutFallback = true;
|
|
3235
|
+
console.warn(
|
|
3236
|
+
`[spacelr] subscribeWithSnapshot on ${this.projectId}:${this.collectionName}: 'where' filter contains clauses (e.g. $gt, $ne, Date, dotted paths) that may not be enforced on the live stream. onChange may receive events beyond the filter; snapshot is still fully filtered.`
|
|
3237
|
+
);
|
|
3238
|
+
}
|
|
3239
|
+
};
|
|
2910
3240
|
const runRecovery = async () => {
|
|
2911
3241
|
currentUnsubscribe?.();
|
|
2912
3242
|
currentUnsubscribe = null;
|
|
@@ -2914,6 +3244,7 @@ var CollectionRef = class {
|
|
|
2914
3244
|
let recoveryAckResolved = false;
|
|
2915
3245
|
let recoverySnapshotDelivered = false;
|
|
2916
3246
|
const pendingRecoveryEvents = [];
|
|
3247
|
+
let freshRecoveryPlanReady = false;
|
|
2917
3248
|
let next;
|
|
2918
3249
|
try {
|
|
2919
3250
|
next = await realtime.subscribeWithCursorWithMeta({
|
|
@@ -2923,8 +3254,8 @@ var CollectionRef = class {
|
|
|
2923
3254
|
where: streamWhere,
|
|
2924
3255
|
onEvent: async ({ event }) => {
|
|
2925
3256
|
if (!isActive) return;
|
|
2926
|
-
if (
|
|
2927
|
-
return;
|
|
3257
|
+
if (freshRecoveryPlanReady) {
|
|
3258
|
+
if (!passesLiveGate(event)) return;
|
|
2928
3259
|
}
|
|
2929
3260
|
if (!recoverySnapshotDelivered) {
|
|
2930
3261
|
pendingRecoveryEvents.push(event);
|
|
@@ -2957,7 +3288,16 @@ var CollectionRef = class {
|
|
|
2957
3288
|
snapshotAbort = new AbortController();
|
|
2958
3289
|
let recoveryDocs;
|
|
2959
3290
|
try {
|
|
2960
|
-
|
|
3291
|
+
const recoveryResult = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
|
|
3292
|
+
recoveryDocs = recoveryResult.documents;
|
|
3293
|
+
recomputeTypedMatcherPlan(recoveryResult.coercionPlan);
|
|
3294
|
+
warnIfNoGateAvailable();
|
|
3295
|
+
freshRecoveryPlanReady = true;
|
|
3296
|
+
for (let i = pendingRecoveryEvents.length - 1; i >= 0; i--) {
|
|
3297
|
+
if (!passesLiveGate(pendingRecoveryEvents[i])) {
|
|
3298
|
+
pendingRecoveryEvents.splice(i, 1);
|
|
3299
|
+
}
|
|
3300
|
+
}
|
|
2961
3301
|
} catch (err) {
|
|
2962
3302
|
if (!isActive) return;
|
|
2963
3303
|
if (err instanceof DOMException && err.name === "AbortError") return;
|
|
@@ -3048,6 +3388,7 @@ var CollectionRef = class {
|
|
|
3048
3388
|
let subscribeAckResolved = false;
|
|
3049
3389
|
let initialSnapshotDelivered = false;
|
|
3050
3390
|
const pendingInitialEvents = [];
|
|
3391
|
+
let initialPlanReady = false;
|
|
3051
3392
|
let initial;
|
|
3052
3393
|
try {
|
|
3053
3394
|
initial = await realtime.subscribeWithCursorWithMeta({
|
|
@@ -3057,7 +3398,7 @@ var CollectionRef = class {
|
|
|
3057
3398
|
where: streamWhere,
|
|
3058
3399
|
onEvent: async ({ event }) => {
|
|
3059
3400
|
if (!isActive) return;
|
|
3060
|
-
if (
|
|
3401
|
+
if (initialPlanReady && !passesLiveGate(event)) {
|
|
3061
3402
|
return;
|
|
3062
3403
|
}
|
|
3063
3404
|
if (!initialSnapshotDelivered) {
|
|
@@ -3104,7 +3445,16 @@ var CollectionRef = class {
|
|
|
3104
3445
|
currentUnsubscribe = initial.unsubscribe;
|
|
3105
3446
|
let snapshotDocs;
|
|
3106
3447
|
try {
|
|
3107
|
-
|
|
3448
|
+
const snapshotResult = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
|
|
3449
|
+
snapshotDocs = snapshotResult.documents;
|
|
3450
|
+
recomputeTypedMatcherPlan(snapshotResult.coercionPlan);
|
|
3451
|
+
initialPlanReady = true;
|
|
3452
|
+
for (let i = pendingInitialEvents.length - 1; i >= 0; i--) {
|
|
3453
|
+
if (!passesLiveGate(pendingInitialEvents[i])) {
|
|
3454
|
+
pendingInitialEvents.splice(i, 1);
|
|
3455
|
+
}
|
|
3456
|
+
}
|
|
3457
|
+
warnIfNoGateAvailable();
|
|
3108
3458
|
} catch (err) {
|
|
3109
3459
|
currentUnsubscribe?.();
|
|
3110
3460
|
isActive = false;
|