@spacelr/sdk 0.10.9 → 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 +25 -6
- package/dist/index.d.ts +25 -6
- package/dist/index.js +397 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +397 -16
- 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;
|
|
@@ -1211,14 +1222,22 @@ interface SubscribeWithSnapshotOptions<T> {
|
|
|
1211
1222
|
* `{ status: { $in: [...] } }`, `{ ts: { $gt: ... } }`, etc. Always fully
|
|
1212
1223
|
* applied to the snapshot `find()`.
|
|
1213
1224
|
*
|
|
1214
|
-
* **Live-stream semantics** — three cases (#712):
|
|
1225
|
+
* **Live-stream semantics** — three cases (#712, #719):
|
|
1215
1226
|
*
|
|
1216
|
-
* 1. Every clause is scalar equality
|
|
1217
|
-
*
|
|
1227
|
+
* 1. Every clause is in the safe subset — scalar equality, `{ $in: [...] }` of
|
|
1228
|
+
* finite scalars, or `$and`/`$or` composing those leaves (#719), e.g.
|
|
1229
|
+
* `{ chatId: 'c1' }`, `{ room: 'r1', status: { $in: ['a','b'] } }`, or
|
|
1230
|
+
* `{ $or: [{ authorId: 'me' }, { sharedWith: { $in: ['me'] } }] }`:
|
|
1218
1231
|
* the filter is enforced on `onChange` too, via a client-side post-filter,
|
|
1219
|
-
* so live delivery matches the snapshot
|
|
1220
|
-
*
|
|
1221
|
-
*
|
|
1232
|
+
* so live delivery matches the snapshot's scope: `onChange` never MISSES a
|
|
1233
|
+
* document the snapshot includes (no under-delivery). For plain JSON scalar
|
|
1234
|
+
* fields this is exact; when a value's stored BSON type differs from its
|
|
1235
|
+
* JSON wire form (e.g. a `Date`/`ObjectId` field compared by equality to a
|
|
1236
|
+
* matching string), the post-filter may OVER-deliver slightly — never
|
|
1237
|
+
* under-deliver — so a re-check is only needed if that edge matters.
|
|
1238
|
+
* 2. `where` contains ANY other clause (`$gt`, `$ne`, `$nin`, `$nor`, a `Date`,
|
|
1239
|
+
* a dotted path, a nested object, a non-finite number, …): the WHOLE `where`
|
|
1240
|
+
* falls
|
|
1222
1241
|
* back to server-side filtering only, which keeps top-level scalar
|
|
1223
1242
|
* equality but drops operator clauses. The snapshot is still fully
|
|
1224
1243
|
* filtered, but `onChange` may OVER-deliver (you may see events outside
|
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;
|
|
@@ -1211,14 +1222,22 @@ interface SubscribeWithSnapshotOptions<T> {
|
|
|
1211
1222
|
* `{ status: { $in: [...] } }`, `{ ts: { $gt: ... } }`, etc. Always fully
|
|
1212
1223
|
* applied to the snapshot `find()`.
|
|
1213
1224
|
*
|
|
1214
|
-
* **Live-stream semantics** — three cases (#712):
|
|
1225
|
+
* **Live-stream semantics** — three cases (#712, #719):
|
|
1215
1226
|
*
|
|
1216
|
-
* 1. Every clause is scalar equality
|
|
1217
|
-
*
|
|
1227
|
+
* 1. Every clause is in the safe subset — scalar equality, `{ $in: [...] }` of
|
|
1228
|
+
* finite scalars, or `$and`/`$or` composing those leaves (#719), e.g.
|
|
1229
|
+
* `{ chatId: 'c1' }`, `{ room: 'r1', status: { $in: ['a','b'] } }`, or
|
|
1230
|
+
* `{ $or: [{ authorId: 'me' }, { sharedWith: { $in: ['me'] } }] }`:
|
|
1218
1231
|
* the filter is enforced on `onChange` too, via a client-side post-filter,
|
|
1219
|
-
* so live delivery matches the snapshot
|
|
1220
|
-
*
|
|
1221
|
-
*
|
|
1232
|
+
* so live delivery matches the snapshot's scope: `onChange` never MISSES a
|
|
1233
|
+
* document the snapshot includes (no under-delivery). For plain JSON scalar
|
|
1234
|
+
* fields this is exact; when a value's stored BSON type differs from its
|
|
1235
|
+
* JSON wire form (e.g. a `Date`/`ObjectId` field compared by equality to a
|
|
1236
|
+
* matching string), the post-filter may OVER-deliver slightly — never
|
|
1237
|
+
* under-deliver — so a re-check is only needed if that edge matters.
|
|
1238
|
+
* 2. `where` contains ANY other clause (`$gt`, `$ne`, `$nin`, `$nor`, a `Date`,
|
|
1239
|
+
* a dotted path, a nested object, a non-finite number, …): the WHOLE `where`
|
|
1240
|
+
* falls
|
|
1222
1241
|
* back to server-side filtering only, which keeps top-level scalar
|
|
1223
1242
|
* equality but drops operator clauses. The snapshot is still fully
|
|
1224
1243
|
* filtered, but `onChange` may OVER-deliver (you may see events outside
|
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;
|
|
@@ -741,11 +768,28 @@ function isSupportedInClause(v) {
|
|
|
741
768
|
const candidates = v["$in"];
|
|
742
769
|
return Array.isArray(candidates) && candidates.every(isFiniteScalar);
|
|
743
770
|
}
|
|
771
|
+
var MAX_WHERE_DEPTH = 10;
|
|
744
772
|
function isMongoWhereFullySupported(where) {
|
|
773
|
+
return isSupportedWhereObject(where, 0);
|
|
774
|
+
}
|
|
775
|
+
function isSupportedWhereObject(where, depth) {
|
|
776
|
+
if (depth > MAX_WHERE_DEPTH) return false;
|
|
745
777
|
for (const [key, value] of Object.entries(where)) {
|
|
778
|
+
if (key === "$and" || key === "$or") {
|
|
779
|
+
if (!Array.isArray(value) || value.length === 0) return false;
|
|
780
|
+
for (const clause of value) {
|
|
781
|
+
if (clause === null || typeof clause !== "object" || Array.isArray(clause)) {
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
if (!isSupportedWhereObject(clause, depth + 1)) {
|
|
785
|
+
return false;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
if (key.startsWith("$")) return false;
|
|
746
791
|
if (key.includes(".")) return false;
|
|
747
792
|
if (RESERVED_PATH_SEGMENTS.has(key)) return false;
|
|
748
|
-
if (key.startsWith("$")) return false;
|
|
749
793
|
if (isFiniteScalar(value)) continue;
|
|
750
794
|
if (isSupportedInClause(value)) continue;
|
|
751
795
|
return false;
|
|
@@ -754,6 +798,20 @@ function isMongoWhereFullySupported(where) {
|
|
|
754
798
|
}
|
|
755
799
|
function matchesMongoWhere(document2, where) {
|
|
756
800
|
for (const [key, value] of Object.entries(where)) {
|
|
801
|
+
if (key === "$and") {
|
|
802
|
+
const clauses = value;
|
|
803
|
+
if (!clauses.every((clause) => matchesMongoWhere(document2, clause))) {
|
|
804
|
+
return false;
|
|
805
|
+
}
|
|
806
|
+
continue;
|
|
807
|
+
}
|
|
808
|
+
if (key === "$or") {
|
|
809
|
+
const clauses = value;
|
|
810
|
+
if (!clauses.some((clause) => matchesMongoWhere(document2, clause))) {
|
|
811
|
+
return false;
|
|
812
|
+
}
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
757
815
|
const actual = resolveWherePath(document2, key);
|
|
758
816
|
if (actual === WHERE_PATH_ABSENT || actual === WHERE_PATH_UNRESOLVABLE) {
|
|
759
817
|
return false;
|
|
@@ -1082,7 +1140,8 @@ var RealtimeClient = class {
|
|
|
1082
1140
|
if (!where || Object.keys(where).length === 0) return true;
|
|
1083
1141
|
if (!event.document) return true;
|
|
1084
1142
|
for (const [key, value] of Object.entries(where)) {
|
|
1085
|
-
|
|
1143
|
+
const fieldType = resolveFieldTypeTag(event.__t, key);
|
|
1144
|
+
if (!whereValueMatchesTyped(resolveWherePath(event.document, key), value, fieldType)) {
|
|
1086
1145
|
return false;
|
|
1087
1146
|
}
|
|
1088
1147
|
}
|
|
@@ -1248,7 +1307,8 @@ var RealtimeClient = class {
|
|
|
1248
1307
|
if (!where) return false;
|
|
1249
1308
|
if (!event.document) return false;
|
|
1250
1309
|
for (const [key, value] of Object.entries(where)) {
|
|
1251
|
-
|
|
1310
|
+
const fieldType = resolveFieldTypeTag(event.__t, key);
|
|
1311
|
+
if (!whereValueMatchesTyped(resolveWherePath(event.document, key), value, fieldType)) {
|
|
1252
1312
|
return false;
|
|
1253
1313
|
}
|
|
1254
1314
|
}
|
|
@@ -2361,6 +2421,262 @@ function isTimelineSourceStats(value) {
|
|
|
2361
2421
|
return true;
|
|
2362
2422
|
}
|
|
2363
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
|
+
|
|
2364
2680
|
// libs/sdk/src/modules/database.module.ts
|
|
2365
2681
|
var DEFAULT_CATCHUP_MAX_PAGES = 1e3;
|
|
2366
2682
|
var DEFAULT_CATCHUP_MAX_DOCS = 5e4;
|
|
@@ -2427,6 +2743,10 @@ var Paginator = class {
|
|
|
2427
2743
|
return { documents: result.documents, hasMore };
|
|
2428
2744
|
}
|
|
2429
2745
|
};
|
|
2746
|
+
var typedWhereV1Flags = /* @__PURE__ */ new WeakMap();
|
|
2747
|
+
function enableTypedWhereV1(builder) {
|
|
2748
|
+
typedWhereV1Flags.set(builder, true);
|
|
2749
|
+
}
|
|
2430
2750
|
var QueryBuilder = class {
|
|
2431
2751
|
constructor(http, basePath, filter) {
|
|
2432
2752
|
this.http = http;
|
|
@@ -2509,6 +2829,7 @@ var QueryBuilder = class {
|
|
|
2509
2829
|
if (this._before !== void 0) query["before"] = this._before;
|
|
2510
2830
|
if (this._after !== void 0) query["after"] = this._after;
|
|
2511
2831
|
if (this._fields) query["fields"] = this._fields.join(",");
|
|
2832
|
+
if (typedWhereV1Flags.get(this)) query["typedWhereV1"] = "true";
|
|
2512
2833
|
if (this._populate.length) {
|
|
2513
2834
|
query["populate"] = this._populate.map(
|
|
2514
2835
|
(p) => p.foreignField ? `${p.field}:${p.collection}:${p.foreignField}` : `${p.field}:${p.collection}`
|
|
@@ -2818,8 +3139,9 @@ var CollectionRef = class {
|
|
|
2818
3139
|
if (opts.sort) builder.sort(opts.sort);
|
|
2819
3140
|
if (opts.limit !== void 0) builder.limit(opts.limit);
|
|
2820
3141
|
if (opts.select) builder.select(opts.select);
|
|
3142
|
+
enableTypedWhereV1(builder);
|
|
2821
3143
|
const result = await builder.execute(signal);
|
|
2822
|
-
return result.documents;
|
|
3144
|
+
return { documents: result.documents, coercionPlan: result.coercionPlan };
|
|
2823
3145
|
}
|
|
2824
3146
|
const asCap = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 0 ? Math.floor(v) : def;
|
|
2825
3147
|
const asPageSize = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 1 ? Math.floor(v) : def;
|
|
@@ -2828,10 +3150,21 @@ var CollectionRef = class {
|
|
|
2828
3150
|
const pageSize = asPageSize(opts.limit, DEFAULT_CATCHUP_PAGE_SIZE);
|
|
2829
3151
|
const all = [];
|
|
2830
3152
|
let cursor;
|
|
3153
|
+
let coercionPlan;
|
|
3154
|
+
let coercionPlanCaptured = false;
|
|
3155
|
+
let coercionPlanMismatch = false;
|
|
2831
3156
|
for (let page = 0; page < maxPages && all.length < maxDocs; page++) {
|
|
2832
3157
|
const builder = this.find(opts.where).sort({ _id: 1 }).limit(pageSize);
|
|
2833
3158
|
if (opts.select) builder.select(opts.select);
|
|
3159
|
+
enableTypedWhereV1(builder);
|
|
2834
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
|
+
}
|
|
2835
3168
|
const docs = result.documents;
|
|
2836
3169
|
if (docs.length === 0) break;
|
|
2837
3170
|
const room = maxDocs - all.length;
|
|
@@ -2848,7 +3181,7 @@ var CollectionRef = class {
|
|
|
2848
3181
|
}
|
|
2849
3182
|
cursor = nextCursor;
|
|
2850
3183
|
}
|
|
2851
|
-
return all;
|
|
3184
|
+
return { documents: all, coercionPlan };
|
|
2852
3185
|
}
|
|
2853
3186
|
async subscribeWithSnapshot(opts) {
|
|
2854
3187
|
if (!this.realtime) {
|
|
@@ -2863,7 +3196,7 @@ var CollectionRef = class {
|
|
|
2863
3196
|
if (!opts.where) return void 0;
|
|
2864
3197
|
const out = {};
|
|
2865
3198
|
for (const [k, v] of Object.entries(opts.where)) {
|
|
2866
|
-
if (typeof v === "
|
|
3199
|
+
if (typeof v === "number" || typeof v === "boolean" || typeof v === "string") {
|
|
2867
3200
|
out[k] = v;
|
|
2868
3201
|
}
|
|
2869
3202
|
}
|
|
@@ -2871,11 +3204,39 @@ var CollectionRef = class {
|
|
|
2871
3204
|
})();
|
|
2872
3205
|
const postFilterWhere = opts.where && Object.keys(opts.where).length > 0 && isMongoWhereFullySupported(opts.where) ? opts.where : void 0;
|
|
2873
3206
|
const postFilter = postFilterWhere !== void 0;
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
);
|
|
2878
|
-
|
|
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
|
+
};
|
|
2879
3240
|
const runRecovery = async () => {
|
|
2880
3241
|
currentUnsubscribe?.();
|
|
2881
3242
|
currentUnsubscribe = null;
|
|
@@ -2883,6 +3244,7 @@ var CollectionRef = class {
|
|
|
2883
3244
|
let recoveryAckResolved = false;
|
|
2884
3245
|
let recoverySnapshotDelivered = false;
|
|
2885
3246
|
const pendingRecoveryEvents = [];
|
|
3247
|
+
let freshRecoveryPlanReady = false;
|
|
2886
3248
|
let next;
|
|
2887
3249
|
try {
|
|
2888
3250
|
next = await realtime.subscribeWithCursorWithMeta({
|
|
@@ -2892,8 +3254,8 @@ var CollectionRef = class {
|
|
|
2892
3254
|
where: streamWhere,
|
|
2893
3255
|
onEvent: async ({ event }) => {
|
|
2894
3256
|
if (!isActive) return;
|
|
2895
|
-
if (
|
|
2896
|
-
return;
|
|
3257
|
+
if (freshRecoveryPlanReady) {
|
|
3258
|
+
if (!passesLiveGate(event)) return;
|
|
2897
3259
|
}
|
|
2898
3260
|
if (!recoverySnapshotDelivered) {
|
|
2899
3261
|
pendingRecoveryEvents.push(event);
|
|
@@ -2926,7 +3288,16 @@ var CollectionRef = class {
|
|
|
2926
3288
|
snapshotAbort = new AbortController();
|
|
2927
3289
|
let recoveryDocs;
|
|
2928
3290
|
try {
|
|
2929
|
-
|
|
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
|
+
}
|
|
2930
3301
|
} catch (err) {
|
|
2931
3302
|
if (!isActive) return;
|
|
2932
3303
|
if (err instanceof DOMException && err.name === "AbortError") return;
|
|
@@ -3017,6 +3388,7 @@ var CollectionRef = class {
|
|
|
3017
3388
|
let subscribeAckResolved = false;
|
|
3018
3389
|
let initialSnapshotDelivered = false;
|
|
3019
3390
|
const pendingInitialEvents = [];
|
|
3391
|
+
let initialPlanReady = false;
|
|
3020
3392
|
let initial;
|
|
3021
3393
|
try {
|
|
3022
3394
|
initial = await realtime.subscribeWithCursorWithMeta({
|
|
@@ -3026,7 +3398,7 @@ var CollectionRef = class {
|
|
|
3026
3398
|
where: streamWhere,
|
|
3027
3399
|
onEvent: async ({ event }) => {
|
|
3028
3400
|
if (!isActive) return;
|
|
3029
|
-
if (
|
|
3401
|
+
if (initialPlanReady && !passesLiveGate(event)) {
|
|
3030
3402
|
return;
|
|
3031
3403
|
}
|
|
3032
3404
|
if (!initialSnapshotDelivered) {
|
|
@@ -3073,7 +3445,16 @@ var CollectionRef = class {
|
|
|
3073
3445
|
currentUnsubscribe = initial.unsubscribe;
|
|
3074
3446
|
let snapshotDocs;
|
|
3075
3447
|
try {
|
|
3076
|
-
|
|
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();
|
|
3077
3458
|
} catch (err) {
|
|
3078
3459
|
currentUnsubscribe?.();
|
|
3079
3460
|
isActive = false;
|