Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bindings-typescript/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions crates/bindings-typescript/src/server/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* const unboundedRange = new Range();
* ```
*/
const RANGE_BRAND: unique symbol = Symbol.for('spacetimedb.Range');

export class Range<T> {
#from: Bound<T>;
#to: Bound<T>;
Expand All @@ -23,6 +25,20 @@ export class Range<T> {
this.#to = to ?? { tag: 'unbounded' };
}

public get [RANGE_BRAND](): true {
return true;
}

public static isRange(val: unknown): val is Range<any> {
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<T> {
return this.#from;
}
Expand All @@ -31,6 +47,10 @@ export class Range<T> {
}
}

export function isRange(val: unknown): val is Range<any> {
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.
Expand Down
13 changes: 5 additions & 8 deletions crates/bindings-typescript/src/server/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1538,7 +1538,7 @@ export function makeTableView(
const rawIndex = {
filter: (range: any): IteratorObject<RowType<any>> => {
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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 = (
Expand All @@ -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<any>) => {
const tags = { included: 0, excluded: 1, unbounded: 2 };
writer.writeU8(tags[bound.tag]);
Expand Down
112 changes: 111 additions & 1 deletion crates/bindings-typescript/tests/index_prefix_filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<T> {
#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();
Expand Down Expand Up @@ -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);
});
});