{"version":3,"file":"contract.mjs","names":["nativeSchemas","nativeIdPredicates","sharedNativeValidator","validator","schema","z","cloneNative","defineFilterContract","fields","predicates","native","descriptors","field","input","scalar","scalarSchema","operators","ordered","scalarPredicateFromSchema","nativeJson","describe","boundedFilterSchema","freezeMetadata","evaluateFilter","normalizeFilters","filters","document","accessible","withinUrlSize","value","parsed","encoded","canonical","convexToJson","envelope","guardJson","jsonToConvex","result","children","v","literals","c","object","k","depth","state","child","member","optional","pending","size","nodes","current","keys","key","descriptor"],"sources":["../../../src/querying/shared/contract.ts"],"sourcesContent":["import { z } from 'zod'\nimport {\n\tconvexToJson,\n\tjsonToConvex,\n\tv,\n\ttype GenericId,\n\ttype JSONValue,\n\ttype ValidatorJSON,\n\ttype GenericValidator,\n\ttype Value,\n} from 'convex/values'\nimport {\n\tboundedFilterSchema,\n\tnormalizeFilters,\n\tscalarPredicateFromSchema,\n\ttype FilterTree,\n} from './filters'\nimport { evaluateFilter } from './evaluate'\n\n// Metadata only: no parser callbacks or server state cross this boundary.\nconst nativeSchemas = new WeakMap<z.core.$ZodType, GenericValidator>()\n// Zod preserves a custom predicate through clone/describe/meta/refine. Associate\n// ID metadata with that private predicate too, so those derivations stay native IDs.\nconst nativeIdPredicates = new WeakMap<z.core.$ZodCustom['_zod']['def']['fn'], GenericValidator>()\nexport function sharedNativeValidator(schema: z.core.$ZodType): GenericValidator | undefined {\n\tconst validator =\n\t\tnativeSchemas.get(schema) ??\n\t\t(schema instanceof z.core.$ZodCustom ? nativeIdPredicates.get(schema._zod.def.fn) : undefined)\n\treturn validator ? cloneNative(validator) : undefined\n}\ntype RequiredSharedField<V extends GenericValidator> = V extends { kind: 'string' }\n\t? z.ZodString\n\t: V extends { kind: 'float64' }\n\t\t? z.ZodNumber\n\t\t: V extends { kind: 'int64' }\n\t\t\t? z.ZodBigInt\n\t\t\t: V extends { kind: 'boolean' }\n\t\t\t\t? z.ZodBoolean\n\t\t\t\t: V extends { kind: 'null' }\n\t\t\t\t\t? z.ZodNull\n\t\t\t\t\t: V extends { kind: 'literal'; value: infer L extends string | number | bigint | boolean }\n\t\t\t\t\t\t? z.ZodLiteral<L>\n\t\t\t\t\t\t: V extends { kind: 'id' }\n\t\t\t\t\t\t\t? z.ZodCustom<Extract<V['type'], GenericId<string>>, string>\n\t\t\t\t\t\t\t: V extends { kind: 'union'; members: [] }\n\t\t\t\t\t\t\t\t? z.ZodUnion<[]>\n\t\t\t\t\t\t\t\t: V extends {\n\t\t\t\t\t\t\t\t\t\t\tkind: 'union'\n\t\t\t\t\t\t\t\t\t\t\tmembers: [\n\t\t\t\t\t\t\t\t\t\t\t\tinfer A extends GenericValidator,\n\t\t\t\t\t\t\t\t\t\t\t\t...infer R extends GenericValidator[],\n\t\t\t\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t\t\t  }\n\t\t\t\t\t\t\t\t\t? z.ZodUnion<readonly [SharedField<A>, ...{ [K in keyof R]: SharedField<R[K]> }]>\n\t\t\t\t\t\t\t\t\t: V extends { kind: 'union'; members: (infer E extends GenericValidator)[] }\n\t\t\t\t\t\t\t\t\t\t? [GenericValidator] extends [E]\n\t\t\t\t\t\t\t\t\t\t\t? z.ZodUnion<readonly z.core.SomeType[]>\n\t\t\t\t\t\t\t\t\t\t\t: z.ZodUnion<readonly SharedField<E>[]>\n\t\t\t\t\t\t\t\t\t\t: never\nexport type SharedField<V extends GenericValidator> = V extends { isOptional: 'optional' }\n\t? z.ZodOptional<RequiredSharedField<V>>\n\t: RequiredSharedField<V>\ntype FieldSchemas<F extends Record<string, GenericValidator>> = {\n\t[K in keyof F]: SharedField<F[K]>\n}\ntype FilterSide = 'input' | 'output'\ntype InputValue<V> = V extends GenericId<string> ? string : V\ntype Scalar<V extends GenericValidator, S extends FilterSide> = S extends 'input'\n\t? InputValue<Exclude<V['type'], undefined>>\n\t: Exclude<V['type'], undefined>\ntype Ordered<V extends GenericValidator> = [GenericValidator] extends [V]\n\t? false\n\t: V extends { kind: 'string' | 'float64' | 'int64' }\n\t\t? true\n\t\t: V extends { kind: 'literal' }\n\t\t\t? Exclude<V['type'], undefined> extends string | number | bigint\n\t\t\t\t? true\n\t\t\t\t: false\n\t\t\t: V extends { kind: 'union'; members: infer M extends GenericValidator[] }\n\t\t\t\t? false extends Ordered<M[number]>\n\t\t\t\t\t? false\n\t\t\t\t\t: true\n\t\t\t\t: false\ntype Predicate<V extends GenericValidator, S extends FilterSide> =\n\t| Scalar<V, S>\n\t| ({ eq?: Scalar<V, S>; in?: Scalar<V, S>[] } & (Ordered<V> extends true\n\t\t\t? { gt?: Scalar<V, S>; gte?: Scalar<V, S>; lt?: Scalar<V, S>; lte?: Scalar<V, S> }\n\t\t\t: {}) &\n\t\t\t(V['isOptional'] extends 'optional' ? { exists?: boolean } : {}))\ntype Fields<F extends Record<string, GenericValidator>, S extends FilterSide> = {\n\t[K in keyof F]?: Predicate<F[K], S>\n}\ntype Tail<D extends 1[]> = D extends [1, ...infer R extends 1[]] ? R : []\nexport type SharedFilters<\n\tF extends Record<string, GenericValidator>,\n\tS extends FilterSide = 'output',\n\tD extends 1[] = [1, 1, 1, 1],\n> = Omit<Fields<F, S>, 'and' | 'or' | 'not' | 'fields'> & { fields?: Fields<F, S> } & (D extends []\n\t\t? {}\n\t\t: {\n\t\t\t\tand?: SharedFilters<F, S, Tail<D>>[]\n\t\t\t\tor?: SharedFilters<F, S, Tail<D>>[]\n\t\t\t\tnot?: SharedFilters<F, S, Tail<D>>\n\t\t\t})\nexport interface FilterDescriptor {\n\tfield: string\n\tkind: 'string' | 'number' | 'bigint' | 'boolean' | 'null' | 'id' | 'enum' | 'union'\n\toptional: boolean\n\tnullable: boolean\n\tvalues?: readonly (string | number | bigint | boolean)[]\n\tidTables: readonly string[]\n\toperators: readonly string[]\n\tmembers?: readonly FilterDescriptor[]\n}\nexport interface FilterContract<F extends Record<string, GenericValidator>> {\n\treadonly fields: FieldSchemas<F>\n\treadonly native: Readonly<Record<string, { json: ValidatorJSON; optional: boolean }>>\n\treadonly schema: z.ZodType<SharedFilters<F>, SharedFilters<F, 'input'>>\n\tdescribe(): readonly FilterDescriptor[]\n\tevaluate(\n\t\tfilters: SharedFilters<F, 'input'>,\n\t\tdocument: Record<string, Value | undefined>,\n\t\taccessible?: ReadonlySet<string>,\n\t): boolean | 'unknown'\n\treadonly searchParam: {\n\t\tencode(\n\t\t\tvalue: SharedFilters<F, 'input'>,\n\t\t): { success: true; value: string } | { success: false; error: string }\n\t\tdecode(\n\t\t\tvalue: string,\n\t\t): { success: true; value: SharedFilters<F> } | { success: false; error: string }\n\t}\n}\n\n/** Scalar-only contracts explicitly reject objects, arrays, relations and variants. */\nexport function defineFilterContract<const F extends Record<string, GenericValidator>>(\n\tinput: F,\n): FilterContract<F> {\n\ttype Schemas = Record<string, z.ZodType>\n\tconst fields: Schemas = {},\n\t\tpredicates: Record<string, z.ZodType> = {}\n\tconst native: Record<string, { json: ValidatorJSON; optional: boolean }> = {}\n\tconst descriptors: FilterDescriptor[] = []\n\tfor (const [field, validator] of Object.entries(input)) {\n\t\tif (['__proto__', 'constructor', 'prototype'].includes(field))\n\t\t\tthrow new Error('Unsupported filter field')\n\t\tconst scalar = scalarSchema(validator)\n\t\tfields[field] = validator.isOptional === 'optional' ? scalar.optional() : scalar\n\t\tconst operators = ['eq', 'in']\n\t\tif (ordered(validator)) operators.push('gt', 'gte', 'lt', 'lte')\n\t\tif (validator.isOptional === 'optional') operators.push('exists')\n\t\tpredicates[field] = scalarPredicateFromSchema(\n\t\t\tscalar,\n\t\t\tordered(validator),\n\t\t\tvalidator.isOptional === 'optional',\n\t\t).optional()\n\t\tnative[field] = { json: nativeJson(validator), optional: validator.isOptional === 'optional' }\n\t\tdescriptors.push(describe(field, validator, operators))\n\t}\n\t// SAFETY: every selected native scalar generates precisely the mapped predicate grammar.\n\tconst schema = boundedFilterSchema(predicates) as z.ZodType<\n\t\tSharedFilters<F>,\n\t\tSharedFilters<F, 'input'>\n\t>\n\tconst encode = (value: SharedFilters<F, 'input'>) => {\n\t\t// SAFETY: bounded scalar schema outputs contain only Convex scalar values and containers.\n\t\ttry {\n\t\t\t// SAFETY: the input boundary inspects descriptors before parsing or serializing.\n\t\t\tif (!withinUrlSize(value as FilterTree)) throw new Error('URL size exceeded')\n\t\t\tconst parsed = schema.parse(value)\n\t\t\t// SAFETY: bounded schema output contains only Convex values.\n\t\t\tconst encoded = JSON.stringify({ v: 1, filters: canonical(convexToJson(parsed as Value)) })\n\t\t\tif (encoded.length > 32768) throw new Error('URL size exceeded')\n\t\t\treturn { success: true as const, value: encoded }\n\t\t} catch {\n\t\t\treturn { success: false as const, error: 'Invalid filters' }\n\t\t}\n\t}\n\tconst decode = (value: string) => {\n\t\ttry {\n\t\t\tif (value.length > 32768) throw new Error('URL size exceeded')\n\t\t\tconst envelope = JSON.parse(value)\n\t\t\tif (!envelope || envelope.v !== 1 || Object.keys(envelope).sort().join(',') !== 'filters,v')\n\t\t\t\tthrow new Error('Unsupported URL version')\n\t\t\tguardJson(envelope.filters)\n\t\t\tconst parsed = schema.parse(jsonToConvex(envelope.filters))\n\t\t\treturn { success: true as const, value: parsed }\n\t\t} catch {\n\t\t\treturn { success: false as const, error: 'Invalid filter URL' }\n\t\t}\n\t}\n\treturn Object.freeze({\n\t\t// SAFETY: fields were generated from their corresponding native validator types.\n\t\tfields: Object.freeze(fields) as FieldSchemas<F>,\n\t\tnative: freezeMetadata(native),\n\t\tschema,\n\t\tdescribe: () => structuredClone(descriptors),\n\t\t// SAFETY: the bounded scalar schema validates the tree before normalization.\n\t\tevaluate: (\n\t\t\tfilters: SharedFilters<F, 'input'>,\n\t\t\tdocument: Record<string, Value | undefined>,\n\t\t\taccessible?: ReadonlySet<string>,\n\t\t) =>\n\t\t\tevaluateFilter(normalizeFilters(schema.parse(filters) as FilterTree), document, accessible),\n\t\tsearchParam: Object.freeze({ encode, decode }),\n\t})\n}\nfunction scalarSchema(validator: GenericValidator): z.ZodType {\n\tlet result: z.ZodType\n\tswitch (validator.kind) {\n\t\tcase 'string':\n\t\t\tresult = z.string()\n\t\t\tbreak\n\t\tcase 'float64':\n\t\t\tresult = z.number()\n\t\t\tbreak\n\t\tcase 'int64':\n\t\t\tresult = z\n\t\t\t\t.bigint()\n\t\t\t\t.min(-(1n << 63n))\n\t\t\t\t.max((1n << 63n) - 1n)\n\t\t\tbreak\n\t\tcase 'boolean':\n\t\t\tresult = z.boolean()\n\t\t\tbreak\n\t\tcase 'null':\n\t\t\tresult = z.null()\n\t\t\tbreak\n\t\tcase 'literal':\n\t\t\tresult = z.literal(validator.value)\n\t\t\tbreak\n\t\tcase 'id':\n\t\t\tresult = z.custom<string>((value) => z.string().safeParse(value).success)\n\t\t\tbreak\n\t\tcase 'union':\n\t\t\tresult = z.union(validator.members.map(scalarSchema))\n\t\t\tbreak\n\t\tdefault:\n\t\t\tthrow new Error(\n\t\t\t\t`Unsupported shared scalar kind \"${validator.kind}\"; relations and variants require a server contract`,\n\t\t\t)\n\t}\n\t// Capture immutable scalar metadata. Binding and conversion never consult mutable callers.\n\tconst native = cloneNative(validator, false)\n\tnativeSchemas.set(result, native)\n\tif (validator.kind === 'id' && result instanceof z.core.$ZodCustom)\n\t\tnativeIdPredicates.set(result._zod.def.fn, native)\n\treturn result\n}\nfunction ordered(validator: GenericValidator): boolean {\n\treturn (\n\t\t['string', 'float64', 'int64'].includes(validator.kind) ||\n\t\t(validator.kind === 'literal' &&\n\t\t\tz.union([z.string(), z.number(), z.bigint()]).safeParse(validator.value).success) ||\n\t\t(validator.kind === 'union' && validator.members.every(ordered))\n\t)\n}\nfunction describe(\n\tfield: string,\n\tvalidator: GenericValidator,\n\toperators: string[],\n): FilterDescriptor {\n\tconst children =\n\t\tvalidator.kind === 'union'\n\t\t\t? validator.members.map((v) => describe(field, v, operators))\n\t\t\t: undefined\n\tconst literals = children?.every((c) => c.kind === 'enum')\n\tconst kind =\n\t\tvalidator.kind === 'float64'\n\t\t\t? 'number'\n\t\t\t: validator.kind === 'int64'\n\t\t\t\t? 'bigint'\n\t\t\t\t: validator.kind === 'literal'\n\t\t\t\t\t? 'enum'\n\t\t\t\t\t: literals\n\t\t\t\t\t\t? 'enum'\n\t\t\t\t\t\t: validator.kind\n\t// SAFETY: unsupported native kinds were rejected while generating scalar schemas.\n\treturn {\n\t\tfield,\n\t\tkind: kind as FilterDescriptor['kind'],\n\t\toptional: validator.isOptional === 'optional',\n\t\tnullable: validator.kind === 'null' || !!children?.some((c) => c.nullable),\n\t\tvalues:\n\t\t\tvalidator.kind === 'literal'\n\t\t\t\t? [validator.value]\n\t\t\t\t: literals\n\t\t\t\t\t? children!.flatMap((c) => c.values ?? [])\n\t\t\t\t\t: undefined,\n\t\tidTables:\n\t\t\tvalidator.kind === 'id'\n\t\t\t\t? [validator.tableName]\n\t\t\t\t: (children?.flatMap((c) => c.idTables) ?? []),\n\t\toperators,\n\t\tmembers: children,\n\t}\n}\n// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Serialized Convex JSON contains only validated JSON containers and scalars.\nfunction canonical(value: JSONValue): JSONValue {\n\tif (Array.isArray(value)) return value.map(canonical)\n\t// SAFETY: arrays were handled; the remaining JSON object supports string-key lookup.\n\t// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Traverse JSON containers before canonical encoding.\n\tif (value && typeof value === 'object') {\n\t\t// SAFETY: arrays were handled, leaving a JSON object with string keys.\n\t\tconst object = value as Record<string, JSONValue>\n\t\treturn Object.fromEntries(\n\t\t\tObject.keys(object)\n\t\t\t\t.sort()\n\t\t\t\t.map((k) => [k, canonical(object[k])]),\n\t\t)\n\t}\n\treturn value\n}\n// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Bound untrusted JSON before Convex decoding or recursive schema validation.\nfunction guardJson(value: JSONValue, depth = 0, state = { nodes: 0 }): void {\n\tif (depth > 16 || ++state.nodes > 2048) throw new Error('URL budget exceeded')\n\t// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Traverse only JSON containers or owned metadata; no domain narrowing occurs.\n\tif (value && typeof value === 'object')\n\t\tfor (const child of Object.values(value)) guardJson(child, depth + 1, state)\n}\n\nfunction nativeJson(validator: GenericValidator): ValidatorJSON {\n\t// SAFETY: native scalar validators expose json at runtime, stripped from declarations.\n\treturn (validator as GenericValidator & { json: ValidatorJSON }).json\n}\nfunction cloneNative(validator: GenericValidator, optional = true): GenericValidator {\n\tlet result: GenericValidator\n\tswitch (validator.kind) {\n\t\tcase 'id':\n\t\t\tresult = v.id(validator.tableName)\n\t\t\tbreak\n\t\tcase 'string':\n\t\t\tresult = v.string()\n\t\t\tbreak\n\t\tcase 'float64':\n\t\t\tresult = v.float64()\n\t\t\tbreak\n\t\tcase 'int64':\n\t\t\tresult = v.int64()\n\t\t\tbreak\n\t\tcase 'null':\n\t\t\tresult = v.null()\n\t\t\tbreak\n\t\tcase 'boolean':\n\t\t\tresult = v.boolean()\n\t\t\tbreak\n\t\tcase 'literal':\n\t\t\tresult = v.literal(validator.value)\n\t\t\tbreak\n\t\tcase 'union':\n\t\t\tresult = v.union(...validator.members.map((member) => cloneNative(member)))\n\t\t\tbreak\n\t\tdefault:\n\t\t\tthrow new Error('Unsupported shared scalar')\n\t}\n\treturn optional && validator.isOptional === 'optional' ? v.optional(result) : result\n}\n\nfunction freezeMetadata<T>(value: T): T {\n\t// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Traverse only JSON containers or owned metadata; no domain narrowing occurs.\n\tif (value && typeof value === 'object') {\n\t\tfor (const child of Object.values(value)) freezeMetadata(child)\n\t\tObject.freeze(value)\n\t}\n\treturn value\n}\n\nfunction withinUrlSize(value: FilterTree): boolean {\n\tconst pending = [value]\n\tlet size = 0,\n\t\tnodes = 0\n\twhile (pending.length) {\n\t\tconst current = pending.pop()\n\t\tif (++nodes > 512) return false\n\t\t// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Count raw scalar text before any serialization allocation.\n\t\tif (typeof current === 'string') {\n\t\t\tsize += current.length\n\t\t\tif (size > 32768) return false\n\t\t}\n\t\tif (Object(current) !== current) continue\n\t\t// SAFETY: boxing excluded null and primitives; inspect data descriptors without invoking getters.\n\t\tconst object = current as object\n\t\tconst keys = Reflect.ownKeys(object)\n\t\tif (keys.length + nodes + pending.length > 512) return false\n\t\tfor (const key of keys) {\n\t\t\tif (Array.isArray(object) && key === 'length') continue\n\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(object, key)\n\t\t\tif (!descriptor || !('value' in descriptor)) return false\n\t\t\tsize += String(key).length\n\t\t\tif (size > 32768) return false\n\t\t\tpending.push(descriptor.value)\n\t\t}\n\t}\n\treturn true\n}\n"],"mappings":"uPAoBA,MAAMA,kBAAgB,IAAI,QAGpBC,kBAAqB,IAAI,QAC/B,SAAgBC,EAAsB,EAAuD,CAC5F,IAAMC,EACLH,EAAc,IAAII,CAAM,IACvBA,aAAkBC,EAAE,KAAK,WAAaJ,EAAmB,IAAIG,EAAO,KAAK,IAAI,EAAE,EAAI,IAAA,IACrF,OAAOD,EAAYG,EAAYH,CAAS,EAAI,IAAA,EAC7C,CA0GA,SAAgBI,EACf,EACoB,CAEpB,IAAMC,EAAkB,CAAC,EACxBC,EAAwC,CAAC,EACpCC,EAAqE,CAAC,EACtEC,EAAkC,CAAC,EACzC,IAAK,GAAM,CAACC,EAAOT,KAAc,OAAO,QAAQU,CAAK,EAAG,CACvD,GAAI,CAAC,YAAa,cAAe,WAAW,CAAC,CAAC,SAASD,CAAK,EAC3D,MAAU,MAAM,0BAA0B,EAC3C,IAAME,EAASC,EAAaZ,CAAS,EACrC,EAAOS,GAAST,EAAU,aAAe,WAAaW,EAAO,SAAS,EAAIA,EAC1E,IAAME,EAAY,CAAC,KAAM,IAAI,EACzBC,EAAQd,CAAS,GAAGa,EAAU,KAAK,KAAM,MAAO,KAAM,KAAK,EAC3Db,EAAU,aAAe,YAAYa,EAAU,KAAK,QAAQ,EAChE,EAAWJ,GAASM,EACnBJ,EACAG,EAAQd,CAAS,EACjBA,EAAU,aAAe,UAC1B,CAAC,CAAC,SAAS,EACX,EAAOS,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