{"version":3,"file":"execution.mjs","names":["rethrowQueryError","error","SearchError","ManagedReadError","CursorError","RelationshipError","executeQuery","executeStream","schema","request","table","plan","planner","codec","binding","complete","raw","leaves","leafReader","progress","nativeSchema","base","stream","selected","nativeIndexRange","q","order","expansion","ordered","mergedStream","prefix","rangeForIndex","getIndexFields","source","transform","result","EmptyStream","args","EligibleStream","authorizationTable","protectedResult","seal","range","field","value","bounded","bounds","lower","upper","active","pending","wrap","target","key","withIndex","iterator","next","failed","fetchStopReason","c","b","CapturedKey","QueryStream","entry","reason","candidatesBefore","document","bytes","output","permitted","completed","encode","fulfilled","continuation"],"sources":["../../src/querying/execution.ts"],"sourcesContent":["import {\n\tEmptyStream,\n\tmergedStream,\n\tgetIndexFields,\n\tQueryStream,\n\tstream,\n\ttype IndexBounds,\n\ttype IndexKey,\n} from 'convex-helpers/server/stream'\nimport type {\n\tGenericDatabaseReader,\n\tGenericDataModel,\n\tGenericDocument,\n\tQueryInitializer,\n\tSchemaDefinition,\n\tGenericSchema,\n\tTableDefinition,\n\tIndexRangeBuilder,\n} from 'convex/server'\nimport type { Value } from 'convex/values'\nimport type { createPlanner } from './planner'\nimport { MAX_DOCUMENT_BYTES, ManagedReadError, type ManagedReadRequest } from './security'\nimport { CursorError, type CursorBinding, type createCursorCodec } from './cursors'\nimport {\n\trangeForIndex,\n\ttype NativePagination,\n\ttype NativeRange,\n\ttype FilterScalar,\n} from './filters'\nimport { SearchError } from './search'\nimport { RelationshipError } from './relationships'\nimport type { EnginePaginationResult, PageCompleteness } from '../zod-table'\n\ntype Entry = [Record<string, Value> | null, IndexKey, number]\ntype Plan = ReturnType<ReturnType<typeof createPlanner>['plan']>\ntype Codec = ReturnType<typeof createCursorCodec>\nexport type ExecutionRequest = ManagedReadRequest<GenericDataModel>\ntype Request = Omit<ExecutionRequest, 'db'>\ntype ExecutionSchema = SchemaDefinition<\n\tRecord<string, TableDefinition<any, Record<string, string[]>>>,\n\tboolean\n>\n\n/** Private shared boundary: only the established fixed typed errors are public. */\n// oxlint-disable-next-line anti-slop/no-unknown-parameters -- Thrown values enter this error boundary untrusted; the class checks below admit only the fixed public error contracts.\nexport function rethrowQueryError(error: unknown): never {\n\tif (\n\t\terror instanceof SearchError ||\n\t\terror instanceof ManagedReadError ||\n\t\terror instanceof CursorError ||\n\t\terror instanceof RelationshipError\n\t)\n\t\tthrow error\n\t// Native/policy errors may contain traversal values or structured private data.\n\tthrow new Error('Query execution failed')\n}\n\nexport async function executeQuery(\n\tschema: SchemaDefinition<GenericSchema, boolean>,\n\trequest: Request,\n\ttable: string,\n\tplan: Plan,\n\tplanner: ReturnType<typeof createPlanner>,\n\tcodec: Codec,\n\tbinding: CursorBinding,\n\tcomplete?: (\n\t\tdocument: Record<string, Value>,\n\t\tpermitted: boolean,\n\t) => Promise<Record<string, Value> | null>,\n) {\n\t// Authenticate BOTH navigation slots before constructing or consuming native queries.\n\tconst args = {\n\t\t...plan.args,\n\t\tcursor: plan.args.cursor === null ? null : codec.open(plan.args.cursor, binding),\n\t\tendCursor:\n\t\t\tplan.args.endCursor == null ? plan.args.endCursor : codec.open(plan.args.endCursor, binding),\n\t}\n\treturn executeStream(schema, request, table, plan, planner, codec, binding, args, complete)\n}\n\nexport async function executeStream(\n\tschema: SchemaDefinition<GenericSchema, boolean>,\n\trequest: Request,\n\ttable: string,\n\tplan: Plan,\n\tplanner: ReturnType<typeof createPlanner>,\n\tcodec: Codec,\n\tbinding: CursorBinding,\n\targs: NativePagination,\n\tcomplete?: (\n\t\tdocument: Record<string, Value>,\n\t\tpermitted: boolean,\n\t) => Promise<Record<string, Value> | null>,\n\tseal: (raw: string) => string = (raw) => codec.seal(raw, binding),\n\ttransform?: (\n\t\tsource: QueryStream<Record<string, Value>>,\n\t\tdb: GenericDatabaseReader<any>,\n\t) => QueryStream<Record<string, Value>>,\n\tauthorizationTable = table,\n) {\n\tconst leaves = leafReader(request, planner)\n\tconst progress: Progress = { completed: [] }\n\ttry {\n\t\t// SAFETY: runtime ownership/index validation precedes this dynamic table\n\t\t// adapter. Public generics retain the original application's schema.\n\t\tconst nativeSchema = schema as ExecutionSchema\n\t\tconst base = stream(leaves.db, nativeSchema).query(table)\n\t\tconst selected = plan.index\n\t\t\t? base.withIndex(plan.index.name, (q) => nativeIndexRange(q, plan.prefix, plan.range))\n\t\t\t: base\n\t\tconst order = plan.args.order ?? 'asc'\n\t\tconst expansion = plan.expansion\n\t\tconst ordered =\n\t\t\texpansion && plan.index && !plan.unsatisfiable\n\t\t\t\t? mergedStream(\n\t\t\t\t\t\texpansion.values.map((value) => {\n\t\t\t\t\t\t\tconst prefix = [...plan.prefix, { field: expansion.field, value }]\n\t\t\t\t\t\t\t// SAFETY: each stream binds the same admitted index at one consecutive position.\n\t\t\t\t\t\t\treturn base\n\t\t\t\t\t\t\t\t.withIndex(plan.index!.name, (q) =>\n\t\t\t\t\t\t\t\t\tnativeIndexRange(q, prefix, rangeForIndex(plan.index!, prefix, plan.ranges)),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t.order(order)\n\t\t\t\t\t\t}),\n\t\t\t\t\t\texpansion.comparator === 'full-index'\n\t\t\t\t\t\t\t? getIndexFields(table, plan.index.name, nativeSchema)\n\t\t\t\t\t\t\t: getIndexFields(table, plan.index.name, nativeSchema).slice(plan.prefix.length + 1),\n\t\t\t\t\t)\n\t\t\t\t: selected.order(order)\n\t\tconst source = transform ? transform(ordered, leaves.db) : ordered\n\t\tconst result: EnginePaginationResult<Record<string, Value>> = plan.unsatisfiable\n\t\t\t? await new EmptyStream<Record<string, Value>>(\n\t\t\t\t\tsource.getOrder(),\n\t\t\t\t\tsource.getIndexFields(),\n\t\t\t\t\tsource.getEqualityIndexFilter(),\n\t\t\t\t).paginate(args)\n\t\t\t: await new EligibleStream(\n\t\t\t\t\tsource,\n\t\t\t\t\trequest,\n\t\t\t\t\tauthorizationTable,\n\t\t\t\t\tplan,\n\t\t\t\t\tplanner,\n\t\t\t\t\tprogress,\n\t\t\t\t\tcomplete,\n\t\t\t\t).paginate(args)\n\t\tconst protectedResult = {\n\t\t\t...result,\n\t\t\tcompleteness: result.completeness ?? 'complete',\n\t\t\tcontinueCursor: seal(result.continueCursor),\n\t\t}\n\t\tif (result.splitCursor != null) protectedResult.splitCursor = seal(result.splitCursor)\n\t\treturn protectedResult\n\t} catch (error) {\n\t\trethrowQueryError(error)\n\t} finally {\n\t\tawait leaves.close()\n\t}\n}\n\n/** Private adapter shared by root and relationship streams; the planner owns admission. */\nexport function nativeIndexRange(\n\tq: IndexRangeBuilder<any, string[], number>,\n\tprefix: readonly { field: string; value: FilterScalar }[],\n\tbounds: NativeRange | null,\n): import('convex/server').IndexRange {\n\tlet range = q\n\tfor (const { field, value } of prefix) {\n\t\t// SAFETY: every equality advances the validated consecutive native prefix.\n\t\trange = range.eq(field, value) as IndexRangeBuilder<any, string[], number>\n\t}\n\t// SAFETY: an equality builder also admits upper bounds; lower methods retain that interface.\n\tlet bounded = range as ReturnType<typeof range.gte>\n\tif (bounds?.lower) {\n\t\tconst { field, lower } = bounds\n\t\tbounded = lower.inclusive ? range.gte(field, lower.value) : range.gt(field, lower.value)\n\t}\n\tif (bounds?.upper) {\n\t\tconst { field, upper } = bounds\n\t\treturn upper.inclusive ? bounded.lte(field, upper.value) : bounded.lt(field, upper.value)\n\t}\n\treturn bounded\n}\n\n/** Retain the actual metered leaf, since the helper's public key iterator does\n * not forward return(). This private reader only constructs raw metered queries.\n */\nfunction leafReader(request: Request, planner: ReturnType<typeof createPlanner>) {\n\tconst active = new Set<AsyncIterator<GenericDocument>>()\n\tconst pending = new Set<Promise<IteratorResult<GenericDocument>>>()\n\tfunction wrap<Source extends AsyncIterable<GenericDocument>>(\n\t\ttable: string,\n\t\tsource: Source,\n\t): Source {\n\t\t// SAFETY: the empty proxy shell is private; supported methods forward to the real native source below.\n\t\treturn new Proxy({} as Source, {\n\t\t\tget(_target, key) {\n\t\t\t\tconst target = source\n\t\t\t\tif (key === 'withIndex' && 'withIndex' in target)\n\t\t\t\t\treturn (...args: Parameters<QueryInitializer<any>['withIndex']>) => {\n\t\t\t\t\t\t// SAFETY: the private facade forwards an existing native index constructor only.\n\t\t\t\t\t\tconst withIndex = target.withIndex as QueryInitializer<any>['withIndex']\n\t\t\t\t\t\treturn wrap(table, withIndex.apply(target, args))\n\t\t\t\t\t}\n\t\t\t\tif (key === 'order' && 'order' in target)\n\t\t\t\t\treturn (...args: Parameters<QueryInitializer<any>['order']>) => {\n\t\t\t\t\t\t// SAFETY: the private facade forwards the existing native order method only.\n\t\t\t\t\t\tconst order = target.order as QueryInitializer<any>['order']\n\t\t\t\t\t\treturn wrap(table, order.apply(target, args))\n\t\t\t\t\t}\n\t\t\t\tif (key === Symbol.asyncIterator)\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst iterator = target[Symbol.asyncIterator]()\n\t\t\t\t\t\tactive.add(iterator)\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tnext: () => {\n\t\t\t\t\t\t\t\tplanner.beforeLeaf(table)\n\t\t\t\t\t\t\t\tconst next = iterator.next()\n\t\t\t\t\t\t\t\tpending.add(next)\n\t\t\t\t\t\t\t\tvoid next.then(\n\t\t\t\t\t\t\t\t\t() => pending.delete(next),\n\t\t\t\t\t\t\t\t\t() => pending.delete(next),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\treturn next\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\tthrow new Error('Unsupported private query operation')\n\t\t\t},\n\t\t})\n\t}\n\n\t// SAFETY: stream() uses only query to construct the indexed stream. The\n\t// request's authorized reader handles policy gets separately; never publish this facade.\n\tconst db = {\n\t\tquery: (table: string) => {\n\t\t\tplanner.beforeLeaf(table)\n\t\t\treturn wrap(table, request.leafQuery(table))\n\t\t},\n\t} as GenericDatabaseReader<any>\n\treturn {\n\t\tdb,\n\t\tasync close() {\n\t\t\tawait Promise.allSettled(pending)\n\t\t\tlet failed = false\n\t\t\tfor (const iterator of active) {\n\t\t\t\ttry {\n\t\t\t\t\tawait iterator.return?.()\n\t\t\t\t} catch {\n\t\t\t\t\tfailed = true\n\t\t\t\t}\n\t\t\t}\n\t\t\tactive.clear()\n\t\t\tif (failed) throw new Error('Query execution failed')\n\t\t},\n\t}\n}\n\nfunction fetchStopReason(request: Request): 'row-budget' | 'byte-budget' | undefined {\n\tconst c = request.counters,\n\t\tb = request.budgets\n\t// Row checks precede byte reservation, matching the managed leaf admission order.\n\tif (c.readOperations >= b.readOperations || c.rawCandidates >= b.rawCandidates)\n\t\treturn 'row-budget'\n\tif (c.documentBytesObserved + c.documentBytesReserved + MAX_DOCUMENT_BYTES > b.documentBytes)\n\t\treturn 'byte-budget'\n\treturn undefined\n}\n\n/** Encoder-only use of public native pagination; no private cursor wire format,\n * artificial bandwidth, lookahead, or database read.\n */\nclass CapturedKey extends QueryStream<object> {\n\tconstructor(\n\t\tprivate readonly entry: Entry,\n\t\tprivate readonly source: QueryStream<Record<string, Value>>,\n\t) {\n\t\tsuper()\n\t}\n\tgetOrder() {\n\t\treturn this.source.getOrder()\n\t}\n\tgetIndexFields() {\n\t\treturn this.source.getIndexFields()\n\t}\n\tgetEqualityIndexFilter() {\n\t\treturn this.source.getEqualityIndexFilter()\n\t}\n\tnarrow(bounds: IndexBounds) {\n\t\tif (\n\t\t\tbounds.lowerBound.length ||\n\t\t\tbounds.upperBound.length ||\n\t\t\t!bounds.lowerBoundInclusive ||\n\t\t\t!bounds.upperBoundInclusive\n\t\t)\n\t\t\tthrow new Error('Invalid cursor encoder range')\n\t\treturn this\n\t}\n\tasync *iterWithKeys(): AsyncGenerator<[object, IndexKey, number], undefined> {\n\t\tyield [{}, this.entry[1], this.entry[2]]\n\t}\n}\n\ninterface Progress {\n\tcompleted: Entry[]\n\tstopReason?: PageCompleteness\n}\nclass EligibleStream extends QueryStream<Record<string, Value>> {\n\tconstructor(\n\t\tprivate readonly source: QueryStream<Record<string, Value>>,\n\t\tprivate readonly request: Request,\n\t\tprivate readonly table: string,\n\t\tprivate readonly plan: Plan,\n\t\tprivate readonly planner: ReturnType<typeof createPlanner>,\n\t\tprivate readonly progress: Progress,\n\t\tprivate readonly complete?: (\n\t\t\tdocument: Record<string, Value>,\n\t\t\tpermitted: boolean,\n\t\t) => Promise<Record<string, Value> | null>,\n\t) {\n\t\tsuper()\n\t}\n\tgetOrder() {\n\t\treturn this.source.getOrder()\n\t}\n\tgetIndexFields() {\n\t\treturn this.source.getIndexFields()\n\t}\n\tgetEqualityIndexFilter() {\n\t\treturn this.source.getEqualityIndexFilter()\n\t}\n\tnarrow(bounds: IndexBounds): QueryStream<Record<string, Value>> {\n\t\treturn new EligibleStream(\n\t\t\tthis.source.narrow(bounds),\n\t\t\tthis.request,\n\t\t\tthis.table,\n\t\t\tthis.plan,\n\t\t\tthis.planner,\n\t\t\tthis.progress,\n\t\t\tthis.complete,\n\t\t)\n\t}\n\tasync *iterWithKeys(): AsyncGenerator<Entry, undefined> {\n\t\t// Always request actual root bandwidth, even without a native byte option.\n\t\tconst iterator = this.source.iterWithKeys(true)[Symbol.asyncIterator]()\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tconst reason = fetchStopReason(this.request)\n\t\t\t\tif (reason) {\n\t\t\t\t\tif (!this.progress.completed.length) throw new ManagedReadError('MANAGED_READ_BUDGET')\n\t\t\t\t\tthis.progress.stopReason = reason\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tlet next: IteratorResult<Entry>\n\t\t\t\tconst candidatesBefore = this.request.counters.rawCandidates\n\t\t\t\ttry {\n\t\t\t\t\tnext = await iterator.next()\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// A reservation failure BEFORE the next root is safe to stop at\n\t\t\t\t\t// completed progress. Authorization errors below are never partialized.\n\t\t\t\t\tif (\n\t\t\t\t\t\terror instanceof ManagedReadError &&\n\t\t\t\t\t\terror.code === 'MANAGED_READ_BUDGET' &&\n\t\t\t\t\t\tthis.request.leafBudgetFailure(error) &&\n\t\t\t\t\t\tthis.progress.completed.length &&\n\t\t\t\t\t\tthis.request.counters.rawCandidates === candidatesBefore\n\t\t\t\t\t) {\n\t\t\t\t\t\tthis.progress.stopReason = this.request.leafBudgetFailure(error)\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\t\t\t\tif (next.done) return\n\t\t\t\tconst [document, key, bytes] = next.value\n\t\t\t\tlet output: Record<string, Value> | null = null\n\t\t\t\tif (document) {\n\t\t\t\t\t// SAFETY: metered native stream documents contain their system IDs.\n\t\t\t\t\tconst permitted = await this.request.authorize(this.table, document as GenericDocument)\n\t\t\t\t\t// SAFETY: the registry DTO validates declared public native values only.\n\t\t\t\t\toutput = this.complete\n\t\t\t\t\t\t? await this.complete(document, permitted)\n\t\t\t\t\t\t: (this.planner.complete(this.table, document, permitted, this.plan.residual) as Record<\n\t\t\t\t\t\t\t\tstring,\n\t\t\t\t\t\t\t\tValue\n\t\t\t\t\t\t\t> | null)\n\t\t\t\t}\n\t\t\t\t// Complete policy, residual, and required DTO before recording the key.\n\t\t\t\tconst entry: Entry = [output, key, bytes]\n\t\t\t\tthis.progress.completed.push(entry)\n\t\t\t\tyield entry\n\t\t\t}\n\t\t} finally {\n\t\t\tawait iterator.return?.()\n\t\t}\n\t}\n\toverride async paginate(\n\t\targs: NativePagination,\n\t): Promise<EnginePaginationResult<Record<string, Value>>> {\n\t\tconst result = await super.paginate(args)\n\t\tconst completed = this.progress.completed\n\t\tconst encode = async (entry: Entry) =>\n\t\t\t(\n\t\t\t\tawait new CapturedKey(entry, this.source).paginate({\n\t\t\t\t\tnumItems: 1,\n\t\t\t\t\tcursor: null,\n\t\t\t\t})\n\t\t\t).continueCursor\n\t\tconst fulfilled = args.endCursor\n\t\t\t? !!completed.length && (await encode(completed.at(-1)!)) === args.endCursor\n\t\t\t: result.page.length >= args.numItems\n\t\tif (\n\t\t\tthis.request.atomicCompletion &&\n\t\t\t((this.progress.stopReason && !fulfilled) ||\n\t\t\t\t(result.pageStatus === 'SplitRequired' && !fulfilled))\n\t\t)\n\t\t\tthrow new ManagedReadError('MANAGED_READ_BUDGET')\n\t\tif (!this.progress.stopReason)\n\t\t\treturn {\n\t\t\t\t...result,\n\t\t\t\t// A helper limit can coincide with the requested final row. That page\n\t\t\t\t// is fulfilled; preserve the native split/isDone/cursor metadata separately.\n\t\t\t\tcompleteness:\n\t\t\t\t\tresult.pageStatus === 'SplitRequired' && !fulfilled ? 'native-limit' : 'complete',\n\t\t\t}\n\t\tif (!completed.length) throw new ManagedReadError('MANAGED_READ_BUDGET')\n\t\tconst continuation = await encode(completed.at(-1)!)\n\t\treturn {\n\t\t\t...result,\n\t\t\tcompleteness: fulfilled ? 'complete' : this.progress.stopReason,\n\t\t\tisDone: false,\n\t\t\tpageStatus: 'SplitRequired',\n\t\t\tcontinueCursor: continuation,\n\t\t\tsplitCursor: await encode(completed[Math.floor((completed.length - 1) / 2)]),\n\t\t}\n\t}\n}\n"],"mappings":"6WA6CA,SAAgBA,EAAkB,EAAuB,CASxD,MAPCC,aAAiBC,GACjBD,aAAiBE,GACjBF,aAAiBG,GACjBH,aAAiBI,EAEXJ,EAEG,MAAM,wBAAwB,CACzC,CAEA,eAAsBK,EACrB,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EAIC,CAQD,OAAOC,EAAcC,EAAQC,EAASC,EAAOC,EAAMC,EAASC,EAAOC,EAAS,CAL3E,GAAGH,EAAK,KACR,OAAQA,EAAK,KAAK,SAAW,KAAO,KAAOE,EAAM,KAAKF,EAAK,KAAK,OAAQG,CAAO,EAC/E,UACCH,EAAK,KAAK,WAAa,KAAOA,EAAK,KAAK,UAAYE,EAAM,KAAKF,EAAK,KAAK,UAAWG,CAAO,CAEd,EAAGC,CAAQ,CAC3F,CAEA,eAAsBR,EACrB,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EAIA,EAAiC,GAAQM,EAAM,KAAKG,EAAKF,CAAO,EAChE,EAIA,EAAqB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