@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.mjs CHANGED
@@ -684,6 +684,78 @@ function whereValueMatches(actual, expected) {
684
684
  if (Array.isArray(actual)) return actual.includes(expected);
685
685
  return actual === expected;
686
686
  }
687
+ function isFiniteScalar(v) {
688
+ const t = typeof v;
689
+ if (t === "string" || t === "boolean") return true;
690
+ if (t === "number") return Number.isFinite(v);
691
+ return false;
692
+ }
693
+ function isSupportedInClause(v) {
694
+ if (typeof v !== "object" || v === null || Array.isArray(v)) return false;
695
+ const keys = Object.keys(v);
696
+ if (keys.length !== 1 || keys[0] !== "$in") return false;
697
+ const candidates = v["$in"];
698
+ return Array.isArray(candidates) && candidates.every(isFiniteScalar);
699
+ }
700
+ var MAX_WHERE_DEPTH = 10;
701
+ function isMongoWhereFullySupported(where) {
702
+ return isSupportedWhereObject(where, 0);
703
+ }
704
+ function isSupportedWhereObject(where, depth) {
705
+ if (depth > MAX_WHERE_DEPTH) return false;
706
+ for (const [key, value] of Object.entries(where)) {
707
+ if (key === "$and" || key === "$or") {
708
+ if (!Array.isArray(value) || value.length === 0) return false;
709
+ for (const clause of value) {
710
+ if (clause === null || typeof clause !== "object" || Array.isArray(clause)) {
711
+ return false;
712
+ }
713
+ if (!isSupportedWhereObject(clause, depth + 1)) {
714
+ return false;
715
+ }
716
+ }
717
+ continue;
718
+ }
719
+ if (key.startsWith("$")) return false;
720
+ if (key.includes(".")) return false;
721
+ if (RESERVED_PATH_SEGMENTS.has(key)) return false;
722
+ if (isFiniteScalar(value)) continue;
723
+ if (isSupportedInClause(value)) continue;
724
+ return false;
725
+ }
726
+ return true;
727
+ }
728
+ function matchesMongoWhere(document2, where) {
729
+ for (const [key, value] of Object.entries(where)) {
730
+ if (key === "$and") {
731
+ const clauses = value;
732
+ if (!clauses.every((clause) => matchesMongoWhere(document2, clause))) {
733
+ return false;
734
+ }
735
+ continue;
736
+ }
737
+ if (key === "$or") {
738
+ const clauses = value;
739
+ if (!clauses.some((clause) => matchesMongoWhere(document2, clause))) {
740
+ return false;
741
+ }
742
+ continue;
743
+ }
744
+ const actual = resolveWherePath(document2, key);
745
+ if (actual === WHERE_PATH_ABSENT || actual === WHERE_PATH_UNRESOLVABLE) {
746
+ return false;
747
+ }
748
+ if (isSupportedInClause(value)) {
749
+ const candidates = value["$in"];
750
+ const matched2 = Array.isArray(actual) ? actual.some((el) => candidates.includes(el)) : candidates.includes(actual);
751
+ if (!matched2) return false;
752
+ continue;
753
+ }
754
+ const matched = Array.isArray(actual) ? actual.includes(value) : actual === value;
755
+ if (!matched) return false;
756
+ }
757
+ return true;
758
+ }
687
759
  var RealtimeClient = class {
688
760
  constructor(config) {
689
761
  this.socket = null;
@@ -2784,9 +2856,11 @@ var CollectionRef = class {
2784
2856
  }
2785
2857
  return Object.keys(out).length > 0 ? out : void 0;
2786
2858
  })();
2787
- if (opts.where && streamWhere === void 0 && Object.keys(opts.where).length > 0) {
2859
+ const postFilterWhere = opts.where && Object.keys(opts.where).length > 0 && isMongoWhereFullySupported(opts.where) ? opts.where : void 0;
2860
+ const postFilter = postFilterWhere !== void 0;
2861
+ if (opts.where && Object.keys(opts.where).length > 0 && !postFilter) {
2788
2862
  console.warn(
2789
- `[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.`
2863
+ `[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.`
2790
2864
  );
2791
2865
  }
2792
2866
  const runRecovery = async () => {
@@ -2805,6 +2879,9 @@ var CollectionRef = class {
2805
2879
  where: streamWhere,
2806
2880
  onEvent: async ({ event }) => {
2807
2881
  if (!isActive) return;
2882
+ if (postFilterWhere && (!event.document || !matchesMongoWhere(event.document, postFilterWhere))) {
2883
+ return;
2884
+ }
2808
2885
  if (!recoverySnapshotDelivered) {
2809
2886
  pendingRecoveryEvents.push(event);
2810
2887
  return;
@@ -2936,6 +3013,9 @@ var CollectionRef = class {
2936
3013
  where: streamWhere,
2937
3014
  onEvent: async ({ event }) => {
2938
3015
  if (!isActive) return;
3016
+ if (postFilterWhere && (!event.document || !matchesMongoWhere(event.document, postFilterWhere))) {
3017
+ return;
3018
+ }
2939
3019
  if (!initialSnapshotDelivered) {
2940
3020
  pendingInitialEvents.push(event);
2941
3021
  return;