@spacelr/sdk 0.10.8 → 0.10.10
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 +28 -13
- package/dist/index.d.ts +28 -13
- package/dist/index.js +82 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +82 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1208,20 +1208,35 @@ interface SubscribeWithSnapshotOptions<T> {
|
|
|
1208
1208
|
* Filter for both the initial snapshot AND the live `onChange` stream.
|
|
1209
1209
|
*
|
|
1210
1210
|
* **Snapshot semantics** (full MongoDB query): supports operators like
|
|
1211
|
-
* `{ status: { $in: [...] } }`, `{ ts: { $gt: ... } }`, etc.
|
|
1211
|
+
* `{ status: { $in: [...] } }`, `{ ts: { $gt: ... } }`, etc. Always fully
|
|
1212
|
+
* applied to the snapshot `find()`.
|
|
1212
1213
|
*
|
|
1213
|
-
* **Live-stream semantics** (
|
|
1214
|
-
*
|
|
1215
|
-
*
|
|
1216
|
-
*
|
|
1217
|
-
*
|
|
1218
|
-
*
|
|
1219
|
-
*
|
|
1220
|
-
*
|
|
1221
|
-
*
|
|
1222
|
-
*
|
|
1223
|
-
*
|
|
1224
|
-
*
|
|
1214
|
+
* **Live-stream semantics** — three cases (#712, #719):
|
|
1215
|
+
*
|
|
1216
|
+
* 1. Every clause is in the safe subset — scalar equality, `{ $in: [...] }` of
|
|
1217
|
+
* finite scalars, or `$and`/`$or` composing those leaves (#719), e.g.
|
|
1218
|
+
* `{ chatId: 'c1' }`, `{ room: 'r1', status: { $in: ['a','b'] } }`, or
|
|
1219
|
+
* `{ $or: [{ authorId: 'me' }, { sharedWith: { $in: ['me'] } }] }`:
|
|
1220
|
+
* the filter is enforced on `onChange` too, via a client-side post-filter,
|
|
1221
|
+
* so live delivery matches the snapshot's scope: `onChange` never MISSES a
|
|
1222
|
+
* document the snapshot includes (no under-delivery). For plain JSON scalar
|
|
1223
|
+
* fields this is exact; when a value's stored BSON type differs from its
|
|
1224
|
+
* JSON wire form (e.g. a `Date`/`ObjectId` field compared by equality to a
|
|
1225
|
+
* matching string), the post-filter may OVER-deliver slightly — never
|
|
1226
|
+
* under-deliver — so a re-check is only needed if that edge matters.
|
|
1227
|
+
* 2. `where` contains ANY other clause (`$gt`, `$ne`, `$nin`, `$nor`, a `Date`,
|
|
1228
|
+
* a dotted path, a nested object, a non-finite number, …): the WHOLE `where`
|
|
1229
|
+
* falls
|
|
1230
|
+
* back to server-side filtering only, which keeps top-level scalar
|
|
1231
|
+
* equality but drops operator clauses. The snapshot is still fully
|
|
1232
|
+
* filtered, but `onChange` may OVER-deliver (you may see events outside
|
|
1233
|
+
* the dropped clauses); a one-time `console.warn` fires. Re-check in the
|
|
1234
|
+
* handler, or use `subscribeEvents()` directly with a realtime
|
|
1235
|
+
* `{ field: { in: [...] } }` operator for live filtering.
|
|
1236
|
+
* 3. When the case-1 post-filter is active, document-less `delete`
|
|
1237
|
+
* tombstones (which carry no document to match against) are SUPPRESSED on
|
|
1238
|
+
* `onChange` — a positive filter can never match an absent document. If
|
|
1239
|
+
* you need deletes for a filtered subscription, use `subscribeEvents()`.
|
|
1225
1240
|
*/
|
|
1226
1241
|
where?: Record<string, unknown>;
|
|
1227
1242
|
sort?: Record<string, 1 | -1>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1208,20 +1208,35 @@ interface SubscribeWithSnapshotOptions<T> {
|
|
|
1208
1208
|
* Filter for both the initial snapshot AND the live `onChange` stream.
|
|
1209
1209
|
*
|
|
1210
1210
|
* **Snapshot semantics** (full MongoDB query): supports operators like
|
|
1211
|
-
* `{ status: { $in: [...] } }`, `{ ts: { $gt: ... } }`, etc.
|
|
1211
|
+
* `{ status: { $in: [...] } }`, `{ ts: { $gt: ... } }`, etc. Always fully
|
|
1212
|
+
* applied to the snapshot `find()`.
|
|
1212
1213
|
*
|
|
1213
|
-
* **Live-stream semantics** (
|
|
1214
|
-
*
|
|
1215
|
-
*
|
|
1216
|
-
*
|
|
1217
|
-
*
|
|
1218
|
-
*
|
|
1219
|
-
*
|
|
1220
|
-
*
|
|
1221
|
-
*
|
|
1222
|
-
*
|
|
1223
|
-
*
|
|
1224
|
-
*
|
|
1214
|
+
* **Live-stream semantics** — three cases (#712, #719):
|
|
1215
|
+
*
|
|
1216
|
+
* 1. Every clause is in the safe subset — scalar equality, `{ $in: [...] }` of
|
|
1217
|
+
* finite scalars, or `$and`/`$or` composing those leaves (#719), e.g.
|
|
1218
|
+
* `{ chatId: 'c1' }`, `{ room: 'r1', status: { $in: ['a','b'] } }`, or
|
|
1219
|
+
* `{ $or: [{ authorId: 'me' }, { sharedWith: { $in: ['me'] } }] }`:
|
|
1220
|
+
* the filter is enforced on `onChange` too, via a client-side post-filter,
|
|
1221
|
+
* so live delivery matches the snapshot's scope: `onChange` never MISSES a
|
|
1222
|
+
* document the snapshot includes (no under-delivery). For plain JSON scalar
|
|
1223
|
+
* fields this is exact; when a value's stored BSON type differs from its
|
|
1224
|
+
* JSON wire form (e.g. a `Date`/`ObjectId` field compared by equality to a
|
|
1225
|
+
* matching string), the post-filter may OVER-deliver slightly — never
|
|
1226
|
+
* under-deliver — so a re-check is only needed if that edge matters.
|
|
1227
|
+
* 2. `where` contains ANY other clause (`$gt`, `$ne`, `$nin`, `$nor`, a `Date`,
|
|
1228
|
+
* a dotted path, a nested object, a non-finite number, …): the WHOLE `where`
|
|
1229
|
+
* falls
|
|
1230
|
+
* back to server-side filtering only, which keeps top-level scalar
|
|
1231
|
+
* equality but drops operator clauses. The snapshot is still fully
|
|
1232
|
+
* filtered, but `onChange` may OVER-deliver (you may see events outside
|
|
1233
|
+
* the dropped clauses); a one-time `console.warn` fires. Re-check in the
|
|
1234
|
+
* handler, or use `subscribeEvents()` directly with a realtime
|
|
1235
|
+
* `{ field: { in: [...] } }` operator for live filtering.
|
|
1236
|
+
* 3. When the case-1 post-filter is active, document-less `delete`
|
|
1237
|
+
* tombstones (which carry no document to match against) are SUPPRESSED on
|
|
1238
|
+
* `onChange` — a positive filter can never match an absent document. If
|
|
1239
|
+
* you need deletes for a filtered subscription, use `subscribeEvents()`.
|
|
1225
1240
|
*/
|
|
1226
1241
|
where?: Record<string, unknown>;
|
|
1227
1242
|
sort?: Record<string, 1 | -1>;
|
package/dist/index.js
CHANGED
|
@@ -728,6 +728,78 @@ function whereValueMatches(actual, expected) {
|
|
|
728
728
|
if (Array.isArray(actual)) return actual.includes(expected);
|
|
729
729
|
return actual === expected;
|
|
730
730
|
}
|
|
731
|
+
function isFiniteScalar(v) {
|
|
732
|
+
const t = typeof v;
|
|
733
|
+
if (t === "string" || t === "boolean") return true;
|
|
734
|
+
if (t === "number") return Number.isFinite(v);
|
|
735
|
+
return false;
|
|
736
|
+
}
|
|
737
|
+
function isSupportedInClause(v) {
|
|
738
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) return false;
|
|
739
|
+
const keys = Object.keys(v);
|
|
740
|
+
if (keys.length !== 1 || keys[0] !== "$in") return false;
|
|
741
|
+
const candidates = v["$in"];
|
|
742
|
+
return Array.isArray(candidates) && candidates.every(isFiniteScalar);
|
|
743
|
+
}
|
|
744
|
+
var MAX_WHERE_DEPTH = 10;
|
|
745
|
+
function isMongoWhereFullySupported(where) {
|
|
746
|
+
return isSupportedWhereObject(where, 0);
|
|
747
|
+
}
|
|
748
|
+
function isSupportedWhereObject(where, depth) {
|
|
749
|
+
if (depth > MAX_WHERE_DEPTH) return false;
|
|
750
|
+
for (const [key, value] of Object.entries(where)) {
|
|
751
|
+
if (key === "$and" || key === "$or") {
|
|
752
|
+
if (!Array.isArray(value) || value.length === 0) return false;
|
|
753
|
+
for (const clause of value) {
|
|
754
|
+
if (clause === null || typeof clause !== "object" || Array.isArray(clause)) {
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
if (!isSupportedWhereObject(clause, depth + 1)) {
|
|
758
|
+
return false;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
if (key.startsWith("$")) return false;
|
|
764
|
+
if (key.includes(".")) return false;
|
|
765
|
+
if (RESERVED_PATH_SEGMENTS.has(key)) return false;
|
|
766
|
+
if (isFiniteScalar(value)) continue;
|
|
767
|
+
if (isSupportedInClause(value)) continue;
|
|
768
|
+
return false;
|
|
769
|
+
}
|
|
770
|
+
return true;
|
|
771
|
+
}
|
|
772
|
+
function matchesMongoWhere(document2, where) {
|
|
773
|
+
for (const [key, value] of Object.entries(where)) {
|
|
774
|
+
if (key === "$and") {
|
|
775
|
+
const clauses = value;
|
|
776
|
+
if (!clauses.every((clause) => matchesMongoWhere(document2, clause))) {
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
if (key === "$or") {
|
|
782
|
+
const clauses = value;
|
|
783
|
+
if (!clauses.some((clause) => matchesMongoWhere(document2, clause))) {
|
|
784
|
+
return false;
|
|
785
|
+
}
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
const actual = resolveWherePath(document2, key);
|
|
789
|
+
if (actual === WHERE_PATH_ABSENT || actual === WHERE_PATH_UNRESOLVABLE) {
|
|
790
|
+
return false;
|
|
791
|
+
}
|
|
792
|
+
if (isSupportedInClause(value)) {
|
|
793
|
+
const candidates = value["$in"];
|
|
794
|
+
const matched2 = Array.isArray(actual) ? actual.some((el) => candidates.includes(el)) : candidates.includes(actual);
|
|
795
|
+
if (!matched2) return false;
|
|
796
|
+
continue;
|
|
797
|
+
}
|
|
798
|
+
const matched = Array.isArray(actual) ? actual.includes(value) : actual === value;
|
|
799
|
+
if (!matched) return false;
|
|
800
|
+
}
|
|
801
|
+
return true;
|
|
802
|
+
}
|
|
731
803
|
var RealtimeClient = class {
|
|
732
804
|
constructor(config) {
|
|
733
805
|
this.socket = null;
|
|
@@ -2828,9 +2900,11 @@ var CollectionRef = class {
|
|
|
2828
2900
|
}
|
|
2829
2901
|
return Object.keys(out).length > 0 ? out : void 0;
|
|
2830
2902
|
})();
|
|
2831
|
-
|
|
2903
|
+
const postFilterWhere = opts.where && Object.keys(opts.where).length > 0 && isMongoWhereFullySupported(opts.where) ? opts.where : void 0;
|
|
2904
|
+
const postFilter = postFilterWhere !== void 0;
|
|
2905
|
+
if (opts.where && Object.keys(opts.where).length > 0 && !postFilter) {
|
|
2832
2906
|
console.warn(
|
|
2833
|
-
`[spacelr] subscribeWithSnapshot on ${this.projectId}:${this.collectionName}: 'where' filter contains
|
|
2907
|
+
`[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.`
|
|
2834
2908
|
);
|
|
2835
2909
|
}
|
|
2836
2910
|
const runRecovery = async () => {
|
|
@@ -2849,6 +2923,9 @@ var CollectionRef = class {
|
|
|
2849
2923
|
where: streamWhere,
|
|
2850
2924
|
onEvent: async ({ event }) => {
|
|
2851
2925
|
if (!isActive) return;
|
|
2926
|
+
if (postFilterWhere && (!event.document || !matchesMongoWhere(event.document, postFilterWhere))) {
|
|
2927
|
+
return;
|
|
2928
|
+
}
|
|
2852
2929
|
if (!recoverySnapshotDelivered) {
|
|
2853
2930
|
pendingRecoveryEvents.push(event);
|
|
2854
2931
|
return;
|
|
@@ -2980,6 +3057,9 @@ var CollectionRef = class {
|
|
|
2980
3057
|
where: streamWhere,
|
|
2981
3058
|
onEvent: async ({ event }) => {
|
|
2982
3059
|
if (!isActive) return;
|
|
3060
|
+
if (postFilterWhere && (!event.document || !matchesMongoWhere(event.document, postFilterWhere))) {
|
|
3061
|
+
return;
|
|
3062
|
+
}
|
|
2983
3063
|
if (!initialSnapshotDelivered) {
|
|
2984
3064
|
pendingInitialEvents.push(event);
|
|
2985
3065
|
return;
|