From 43794ca562ee312d7d3a917fc0269dd2ad05e5b9 Mon Sep 17 00:00:00 2001 From: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:39:16 +0530 Subject: [PATCH] fix(bindings-typescript): support cross-bundle Range instances in index scans (#6299) --- .../bindings-typescript/src/server/index.ts | 2 +- .../bindings-typescript/src/server/range.ts | 20 ++++ .../bindings-typescript/src/server/runtime.ts | 13 +- .../tests/index_prefix_filter.test.ts | 112 +++++++++++++++++- 4 files changed, 137 insertions(+), 10 deletions(-) diff --git a/crates/bindings-typescript/src/server/index.ts b/crates/bindings-typescript/src/server/index.ts index f2e1fa1133d..49080c01fd3 100644 --- a/crates/bindings-typescript/src/server/index.ts +++ b/crates/bindings-typescript/src/server/index.ts @@ -32,7 +32,7 @@ export { toCamelCase } from '../lib/util.ts'; export type { Uuid } from '../lib/uuid.ts'; export type { Random } from './rng.ts'; export type { ViewExport, ViewCtx, AnonymousViewCtx } from './views.ts'; -export { Range, type Bound } from './range.ts'; +export { Range, isRange, type Bound } from './range.ts'; export { Headers, Request, diff --git a/crates/bindings-typescript/src/server/range.ts b/crates/bindings-typescript/src/server/range.ts index 38c3277ea41..3e8a59f5d59 100644 --- a/crates/bindings-typescript/src/server/range.ts +++ b/crates/bindings-typescript/src/server/range.ts @@ -15,6 +15,8 @@ * const unboundedRange = new Range(); * ``` */ +const RANGE_BRAND: unique symbol = Symbol.for('spacetimedb.Range'); + export class Range { #from: Bound; #to: Bound; @@ -23,6 +25,20 @@ export class Range { this.#to = to ?? { tag: 'unbounded' }; } + public get [RANGE_BRAND](): true { + return true; + } + + public static isRange(val: unknown): val is Range { + return ( + val instanceof Range || + (typeof val === 'object' && + val !== null && + ((val as any)[RANGE_BRAND] === true || + (val as any).constructor?.name === 'Range')) + ); + } + public get from(): Bound { return this.#from; } @@ -31,6 +47,10 @@ export class Range { } } +export function isRange(val: unknown): val is Range { + return Range.isRange(val); +} + /** * A type representing a bound in a range, which can be inclusive, exclusive, or unbounded. * - `included`: The bound is inclusive, meaning the value is part of the range. diff --git a/crates/bindings-typescript/src/server/runtime.ts b/crates/bindings-typescript/src/server/runtime.ts index cffa5017489..8ddf9976ede 100644 --- a/crates/bindings-typescript/src/server/runtime.ts +++ b/crates/bindings-typescript/src/server/runtime.ts @@ -73,7 +73,7 @@ import { } from './query.ts'; import type { DbView, ReadonlyDbView } from './db_view.ts'; import { getErrorConstructor, SenderError } from './errors.ts'; -import { Range, type Bound } from './range.ts'; +import { Range, isRange, type Bound } from './range.ts'; import { makeRandom, type Random } from './rng.ts'; import type { SubmoduleDispatchInfo, SchemaInner } from './schema.ts'; import { hostBackend, type DatastoreBackend } from './backend.ts'; @@ -1538,7 +1538,7 @@ export function makeTableView( const rawIndex = { filter: (range: any): IteratorObject> => { const buf = LEAF_BUF; - if (serializeSingleRange && range instanceof Range) { + if (serializeSingleRange && isRange(range)) { const args = serializeSingleRange(buf, range); const iter_id = backend.datastoreIndexScanRangeBsatn( index_id, @@ -1557,7 +1557,7 @@ export function makeTableView( }, delete: (range: any): u32 => { const buf = LEAF_BUF; - if (serializeSingleRange && range instanceof Range) { + if (serializeSingleRange && isRange(range)) { const args = serializeSingleRange(buf, range); return backend.datastoreDeleteByIndexScanRangeBsatn( index_id, @@ -1607,10 +1607,7 @@ export function makeTableView( // Preserve the point-scan path only for complete scalar keys. // A complete key with a Range in the final position is still a range // scan over that column with equality over the preceding prefix. - return ( - range.length === numColumns && - !(range[range.length - 1] instanceof Range) - ); + return range.length === numColumns && !isRange(range[range.length - 1]); }; const serializeRange = ( @@ -1628,7 +1625,7 @@ export function makeTableView( const rstartOffset = writer.offset; const term = range[range.length - 1]; const serializeTerm = indexSerializers[range.length - 1]; - if (term instanceof Range) { + if (isRange(term)) { const writeBound = (bound: Bound) => { const tags = { included: 0, excluded: 1, unbounded: 2 }; writer.writeU8(tags[bound.tag]); diff --git a/crates/bindings-typescript/tests/index_prefix_filter.test.ts b/crates/bindings-typescript/tests/index_prefix_filter.test.ts index 97484b53011..dc79f33466e 100644 --- a/crates/bindings-typescript/tests/index_prefix_filter.test.ts +++ b/crates/bindings-typescript/tests/index_prefix_filter.test.ts @@ -12,7 +12,7 @@ vi.mock('../src/server/procedures', () => ({ import { ModuleContext } from '../src/lib/schema'; import { table } from '../src/lib/table'; -import { Range } from '../src/server/range'; +import { Range, isRange } from '../src/server/range'; import { makeTableView } from '../src/server/runtime'; import { t } from '../src/lib/type_builders'; @@ -82,6 +82,49 @@ function tallyCountView() { return view.by_board_count; } +function singleColumnView() { + const ctx = new ModuleContext(); + const item = table( + { + name: 'item', + indexes: [ + { + accessor: 'by_n', + algorithm: 'btree', + columns: ['n'] as const, + }, + ] as const, + }, + { + id: t.u64().primaryKey().autoInc(), + n: t.u32(), + } + ); + + const rawTableDef = item.tableDef(ctx, 'item'); + const view = makeTableView(ctx.typespace, rawTableDef) as any; + return view.by_n; +} + +// Simulated cross-bundle Range class: separate constructor identity, branded via Symbol.for +class CrossBundleRange { + #from: any; + #to: any; + constructor(from?: any, to?: any) { + this.#from = from ?? { tag: 'unbounded' }; + this.#to = to ?? { tag: 'unbounded' }; + } + public get [Symbol.for('spacetimedb.Range')](): true { + return true; + } + public get from() { + return this.#from; + } + public get to() { + return this.#to; + } +} + describe('multi-column index one-column prefix scan', () => { it('full two-column key works (point scan)', () => { const index = tallyView(); @@ -135,3 +178,70 @@ describe('multi-column index one-column prefix scan', () => { expect(index.delete([1n, range])).toBe(0); }); }); + +describe('cross-bundle Range compatibility (#6299)', () => { + it('isRange correctly identifies native and cross-bundle Range instances', () => { + const native = new Range({ tag: 'unbounded' }, { tag: 'unbounded' }); + const foreign = new CrossBundleRange( + { tag: 'included', value: 2 }, + { tag: 'unbounded' } + ); + const duckTyped = { + constructor: { name: 'Range' }, + from: { tag: 'unbounded' }, + to: { tag: 'unbounded' }, + }; + + expect(isRange(native)).toBe(true); + expect(isRange(foreign)).toBe(true); + expect(isRange(duckTyped)).toBe(true); + expect(Range.isRange(native)).toBe(true); + expect(Range.isRange(foreign)).toBe(true); + + expect(isRange(null)).toBe(false); + expect(isRange(undefined)).toBe(false); + expect(isRange(123)).toBe(false); + expect(isRange('Range')).toBe(false); + expect(isRange({ from: 1, to: 2 })).toBe(false); + }); + + it('filters single-column btree index with cross-bundle Range', () => { + const index = singleColumnView(); + const range = new CrossBundleRange( + { tag: 'included', value: 2 }, + { tag: 'unbounded' } + ); + expect(() => [...index.filter(range)]).not.toThrow(); + expect([...index.filter(range)]).toEqual([]); + }); + + it('deletes from single-column btree index with cross-bundle Range', () => { + const index = singleColumnView(); + const range = new CrossBundleRange( + { tag: 'included', value: 2 }, + { tag: 'unbounded' } + ); + expect(() => index.delete(range)).not.toThrow(); + expect(index.delete(range)).toBe(0); + }); + + it('filters multi-column btree index with cross-bundle Range', () => { + const index = tallyCountView(); + const range = new CrossBundleRange( + { tag: 'included', value: 10n }, + { tag: 'included', value: 20n } + ); + expect(() => [...index.filter([1n, range])]).not.toThrow(); + expect([...index.filter([1n, range])]).toEqual([]); + }); + + it('deletes from multi-column btree index with cross-bundle Range', () => { + const index = tallyCountView(); + const range = new CrossBundleRange( + { tag: 'included', value: 10n }, + { tag: 'included', value: 20n } + ); + expect(() => index.delete([1n, range])).not.toThrow(); + expect(index.delete([1n, range])).toBe(0); + }); +});