@spacelr/sdk 0.10.7 → 0.10.9

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 CHANGED
@@ -1208,20 +1208,27 @@ 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** (top-level primitives only): this helper
1214
- * forwards only top-level primitive values (`string | number | boolean`)
1215
- * from the Mongo-query `where` to the live filter. MongoDB operators
1216
- * (`$in`, `$gt`, …) are silently dropped at handshake time and are NOT
1217
- * translated into the gateway's realtime `in` / `array-contains-any`
1218
- * operators — the remaining primitive entries still apply, so a typical
1219
- * chat-style `{ chatId: 'c1' }` filter works identically on both sides. If
1220
- * you pass a mixed `{ chatId: 'c1', status: { $in: [...] } }`, the snapshot
1221
- * is fully filtered but the live stream filters by `chatId` only; you may
1222
- * receive `onChange` events for documents whose `status` is outside your
1223
- * `$in` set. Re-check in the handler, or use `subscribeEvents()` directly
1224
- * with a realtime `{ status: { in: [...] } }` operator for live filtering.
1214
+ * **Live-stream semantics** — three cases (#712):
1215
+ *
1216
+ * 1. Every clause is scalar equality or `{ $in: [...] }` of finite scalars
1217
+ * (e.g. `{ chatId: 'c1' }` or `{ room: 'r1', status: { $in: ['a','b'] } }`):
1218
+ * the filter is enforced on `onChange` too, via a client-side post-filter,
1219
+ * so live delivery matches the snapshot exactly. No re-check needed.
1220
+ * 2. `where` contains ANY other clause (`$gt`, `$ne`, a `Date`, a dotted
1221
+ * path, a nested object, a non-finite number, …): the WHOLE `where` falls
1222
+ * back to server-side filtering only, which keeps top-level scalar
1223
+ * equality but drops operator clauses. The snapshot is still fully
1224
+ * filtered, but `onChange` may OVER-deliver (you may see events outside
1225
+ * the dropped clauses); a one-time `console.warn` fires. Re-check in the
1226
+ * handler, or use `subscribeEvents()` directly with a realtime
1227
+ * `{ field: { in: [...] } }` operator for live filtering.
1228
+ * 3. When the case-1 post-filter is active, document-less `delete`
1229
+ * tombstones (which carry no document to match against) are SUPPRESSED on
1230
+ * `onChange` — a positive filter can never match an absent document. If
1231
+ * you need deletes for a filtered subscription, use `subscribeEvents()`.
1225
1232
  */
1226
1233
  where?: Record<string, unknown>;
1227
1234
  sort?: Record<string, 1 | -1>;
package/dist/index.d.ts CHANGED
@@ -1208,20 +1208,27 @@ 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** (top-level primitives only): this helper
1214
- * forwards only top-level primitive values (`string | number | boolean`)
1215
- * from the Mongo-query `where` to the live filter. MongoDB operators
1216
- * (`$in`, `$gt`, …) are silently dropped at handshake time and are NOT
1217
- * translated into the gateway's realtime `in` / `array-contains-any`
1218
- * operators — the remaining primitive entries still apply, so a typical
1219
- * chat-style `{ chatId: 'c1' }` filter works identically on both sides. If
1220
- * you pass a mixed `{ chatId: 'c1', status: { $in: [...] } }`, the snapshot
1221
- * is fully filtered but the live stream filters by `chatId` only; you may
1222
- * receive `onChange` events for documents whose `status` is outside your
1223
- * `$in` set. Re-check in the handler, or use `subscribeEvents()` directly
1224
- * with a realtime `{ status: { in: [...] } }` operator for live filtering.
1214
+ * **Live-stream semantics** — three cases (#712):
1215
+ *
1216
+ * 1. Every clause is scalar equality or `{ $in: [...] }` of finite scalars
1217
+ * (e.g. `{ chatId: 'c1' }` or `{ room: 'r1', status: { $in: ['a','b'] } }`):
1218
+ * the filter is enforced on `onChange` too, via a client-side post-filter,
1219
+ * so live delivery matches the snapshot exactly. No re-check needed.
1220
+ * 2. `where` contains ANY other clause (`$gt`, `$ne`, a `Date`, a dotted
1221
+ * path, a nested object, a non-finite number, …): the WHOLE `where` falls
1222
+ * back to server-side filtering only, which keeps top-level scalar
1223
+ * equality but drops operator clauses. The snapshot is still fully
1224
+ * filtered, but `onChange` may OVER-deliver (you may see events outside
1225
+ * the dropped clauses); a one-time `console.warn` fires. Re-check in the
1226
+ * handler, or use `subscribeEvents()` directly with a realtime
1227
+ * `{ field: { in: [...] } }` operator for live filtering.
1228
+ * 3. When the case-1 post-filter is active, document-less `delete`
1229
+ * tombstones (which carry no document to match against) are SUPPRESSED on
1230
+ * `onChange` — a positive filter can never match an absent document. If
1231
+ * you need deletes for a filtered subscription, use `subscribeEvents()`.
1225
1232
  */
1226
1233
  where?: Record<string, unknown>;
1227
1234
  sort?: Record<string, 1 | -1>;
package/dist/index.js CHANGED
@@ -728,6 +728,47 @@ 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
+ function isMongoWhereFullySupported(where) {
745
+ for (const [key, value] of Object.entries(where)) {
746
+ if (key.includes(".")) return false;
747
+ if (RESERVED_PATH_SEGMENTS.has(key)) return false;
748
+ if (key.startsWith("$")) return false;
749
+ if (isFiniteScalar(value)) continue;
750
+ if (isSupportedInClause(value)) continue;
751
+ return false;
752
+ }
753
+ return true;
754
+ }
755
+ function matchesMongoWhere(document2, where) {
756
+ for (const [key, value] of Object.entries(where)) {
757
+ const actual = resolveWherePath(document2, key);
758
+ if (actual === WHERE_PATH_ABSENT || actual === WHERE_PATH_UNRESOLVABLE) {
759
+ return false;
760
+ }
761
+ if (isSupportedInClause(value)) {
762
+ const candidates = value["$in"];
763
+ const matched2 = Array.isArray(actual) ? actual.some((el) => candidates.includes(el)) : candidates.includes(actual);
764
+ if (!matched2) return false;
765
+ continue;
766
+ }
767
+ const matched = Array.isArray(actual) ? actual.includes(value) : actual === value;
768
+ if (!matched) return false;
769
+ }
770
+ return true;
771
+ }
731
772
  var RealtimeClient = class {
732
773
  constructor(config) {
733
774
  this.socket = null;
@@ -2828,9 +2869,11 @@ var CollectionRef = class {
2828
2869
  }
2829
2870
  return Object.keys(out).length > 0 ? out : void 0;
2830
2871
  })();
2831
- if (opts.where && streamWhere === void 0 && Object.keys(opts.where).length > 0) {
2872
+ const postFilterWhere = opts.where && Object.keys(opts.where).length > 0 && isMongoWhereFullySupported(opts.where) ? opts.where : void 0;
2873
+ const postFilter = postFilterWhere !== void 0;
2874
+ if (opts.where && Object.keys(opts.where).length > 0 && !postFilter) {
2832
2875
  console.warn(
2833
- `[spacelr] subscribeWithSnapshot on ${this.projectId}:${this.collectionName}: 'where' filter contains only operator-shaped values (e.g. $gt, $in) which are not supported by the live stream filter. onChange will receive ALL collection events; snapshot is still filtered.`
2876
+ `[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
2877
  );
2835
2878
  }
2836
2879
  const runRecovery = async () => {
@@ -2849,6 +2892,9 @@ var CollectionRef = class {
2849
2892
  where: streamWhere,
2850
2893
  onEvent: async ({ event }) => {
2851
2894
  if (!isActive) return;
2895
+ if (postFilterWhere && (!event.document || !matchesMongoWhere(event.document, postFilterWhere))) {
2896
+ return;
2897
+ }
2852
2898
  if (!recoverySnapshotDelivered) {
2853
2899
  pendingRecoveryEvents.push(event);
2854
2900
  return;
@@ -2964,7 +3010,8 @@ var CollectionRef = class {
2964
3010
  };
2965
3011
  const handleGap = (info) => {
2966
3012
  if (!isActive) return;
2967
- if (info.reason !== "outside-retention-window") return;
3013
+ if (info.reason !== "outside-retention-window" && info.reason !== "replay-truncated")
3014
+ return;
2968
3015
  dispatchRecovery();
2969
3016
  };
2970
3017
  let subscribeAckResolved = false;
@@ -2979,6 +3026,9 @@ var CollectionRef = class {
2979
3026
  where: streamWhere,
2980
3027
  onEvent: async ({ event }) => {
2981
3028
  if (!isActive) return;
3029
+ if (postFilterWhere && (!event.document || !matchesMongoWhere(event.document, postFilterWhere))) {
3030
+ return;
3031
+ }
2982
3032
  if (!initialSnapshotDelivered) {
2983
3033
  pendingInitialEvents.push(event);
2984
3034
  return;