{"version":3,"file":"domain.mjs","names":["defineDomainContract","definition","memoizeAccessors","target","read","memoize","value","key","receiver","descriptor","prototype","lazyFields","memoizeGetters","keys","get","wrap","memoizeOperation","operation","name","section"],"sources":["../../../src/modules/contracts/domain.ts"],"sourcesContent":["import { memoize } from './lazy'\nimport type { ContractSchema, ContractInput, ContractOutput } from './contract'\nimport type { ErrorContract, ErrorDefinitions } from './errors'\n\n/** Schema-only domain vocabulary; implementations and host policies live in registries. */\nexport type DomainOperationContract = {\n\treadonly input: ContractSchema\n\treadonly result: ContractSchema\n\treadonly classification?: string\n\treadonly wire?: { readonly schema: ContractSchema; readonly project: Function }\n}\nexport type DomainCommandContract = DomainOperationContract & {\n\treadonly classification: string\n\treadonly replayResult?: ContractSchema\n}\nexport type DomainQueryContract = DomainOperationContract & { readonly replayResult?: never }\nexport type DomainContract = {\n\treadonly errors?: Pick<ErrorContract<ErrorDefinitions>, 'project'>\n\treadonly commands?: Readonly<Record<string, DomainCommandContract>>\n\treadonly queries?: Readonly<Record<string, DomainQueryContract>>\n}\n\ntype CheckedEntry<Entry extends DomainOperationContract> = {\n\treadonly handler?: never\n\treadonly audit?: never\n\treadonly prepare?: never\n\treadonly guard?: never\n\treadonly middleware?: never\n\treadonly context?: never\n\treadonly wire?: Entry extends { wire: { schema: infer Wire } }\n\t\t? {\n\t\t\t\treadonly schema: Wire\n\t\t\t\treadonly project: (value: ContractOutput<Entry['result']>) => ContractInput<Wire>\n\t\t\t}\n\t\t: never\n} & (Entry extends { replayResult: infer Replay }\n\t? {\n\t\t\treadonly replayResult: ContractOutput<Replay> extends ContractOutput<Entry['result']>\n\t\t\t\t? Replay\n\t\t\t\t: never\n\t\t}\n\t: {})\ntype CheckedSection<Section> = {\n\treadonly [Key in keyof Section]: Section[Key] extends DomainOperationContract\n\t\t? CheckedEntry<Section[Key]>\n\t\t: never\n}\ntype CheckedDomain<Definition> = {\n\treadonly [Section in Extract<keyof Definition, 'commands' | 'queries'>]: CheckedSection<\n\t\tDefinition[Section]\n\t>\n}\n\n/** Preserve the declaration's literal keys and opaque validator/error-contract identities. */\nexport function defineDomainContract<const Definition extends DomainContract>(\n\tdefinition:\n\t\t| (Definition & NoInfer<CheckedDomain<Definition>>)\n\t\t| (() => Definition & NoInfer<CheckedDomain<Definition>>),\n): Definition {\n\t// oxlint-disable-next-line anti-slop/no-runtime-typeof -- The public input is a checked declaration or its explicitly supported thunk.\n\tif (typeof definition !== 'function') return memoizeAccessors(definition)\n\t// SAFETY: the first read installs the inferred declaration's own descriptors on this target.\n\tconst target = {} as Definition\n\tconst read = memoize(() => {\n\t\tconst value = memoizeAccessors(definition())\n\t\tObject.defineProperties(target, Object.getOwnPropertyDescriptors(value))\n\t\tObject.setPrototypeOf(target, Object.getPrototypeOf(value))\n\t\treturn target\n\t})\n\treturn new Proxy(target, {\n\t\t// oxlint-disable-next-line anti-slop/no-reflect-get -- Preserve accessor receiver semantics for the forwarded declaration.\n\t\tget: (_target, key, receiver) => Reflect.get(read(), key, receiver),\n\t\thas: (_target, key) => Reflect.has(read(), key),\n\t\townKeys: () => Reflect.ownKeys(read()),\n\t\tgetOwnPropertyDescriptor: (_target, key) => Reflect.getOwnPropertyDescriptor(read(), key),\n\t\tset: (_target, key, value) => Reflect.set(read(), key, value),\n\t\tdefineProperty: (_target, key, descriptor) => Reflect.defineProperty(read(), key, descriptor),\n\t\tdeleteProperty: (_target, key) => Reflect.deleteProperty(read(), key),\n\t\tgetPrototypeOf: () => Reflect.getPrototypeOf(read()),\n\t\tsetPrototypeOf: (_target, prototype) => Reflect.setPrototypeOf(read(), prototype),\n\t\tpreventExtensions: () => Reflect.preventExtensions(read()),\n\t\tisExtensible: () => Reflect.isExtensible(read()),\n\t})\n}\n\n/** Exactness applies to variables as well as fresh object literals. */\nexport type ExactOperationKeys<Operations, Contracts> =\n\tExclude<keyof Operations, keyof Contracts> extends never\n\t\t? Exclude<keyof Contracts, keyof Operations> extends never\n\t\t\t? string extends keyof Contracts\n\t\t\t\t? never\n\t\t\t\t: unknown\n\t\t\t: never\n\t\t: never\n\n/** Correlated callback envelope for registry-level command audit. */\nexport type DomainAuditInput<Commands> = {\n\t[Key in Extract<keyof Commands, string>]: Commands[Key] extends DomainOperationContract\n\t\t? {\n\t\t\t\toperation: Key\n\t\t\t\tinput: ContractOutput<Commands[Key]['input']>\n\t\t\t\tresult: ContractOutput<Commands[Key]['result']>\n\t\t\t}\n\t\t: never\n}[Extract<keyof Commands, string>]\n\n/* oxlint-disable anti-slop/no-object-parameters, anti-slop/no-unknown-parameters, anti-slop/no-unknown-returns, anti-slop/no-unsafe-dictionary-type, anti-slop/no-runtime-typeof -- Wrapping walks checked declarations by descriptor and keeps schemas as opaque identities. */\nconst lazyFields = ['input', 'result', 'replayResult', 'wire'] as const\n\n/** Replace configurable getters with memoized ones in place, without invoking any of them. */\nfunction memoizeGetters(\n\ttarget: object,\n\tkeys: readonly PropertyKey[],\n\twrap: (value: unknown) => unknown,\n) {\n\tfor (const key of keys) {\n\t\tconst descriptor = Object.getOwnPropertyDescriptor(target, key)\n\t\tif (!descriptor?.get || !descriptor.configurable) continue\n\t\tconst get = descriptor.get\n\t\tObject.defineProperty(target, key, {\n\t\t\tconfigurable: true,\n\t\t\tenumerable: descriptor.enumerable,\n\t\t\tget: memoize(() => wrap(get.call(target))),\n\t\t})\n\t}\n}\n\nfunction memoizeOperation(operation: unknown) {\n\tif (operation && typeof operation === 'object') memoizeGetters(operation, lazyFields, (value) => value)\n\treturn operation\n}\n\n/** Getter-declared operations and fields build at most once and keep a stable identity. */\nfunction memoizeAccessors<Definition>(definition: Definition): Definition {\n\tfor (const name of ['commands', 'queries'] as const) {\n\t\t// SAFETY: the checked declaration's sections are plain records of operation declarations.\n\t\tconst section = (definition as Record<string, unknown> | undefined)?.[name]\n\t\tif (!section || typeof section !== 'object') continue\n\t\tfor (const key of Object.keys(section)) {\n\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(section, key)\n\t\t\tif (descriptor && 'value' in descriptor) memoizeOperation(descriptor.value)\n\t\t}\n\t\tmemoizeGetters(section, Object.keys(section), memoizeOperation)\n\t}\n\treturn definition\n}\n/* oxlint-enable anti-slop/no-object-parameters, anti-slop/no-unknown-parameters, anti-slop/no-unknown-returns, anti-slop/no-unsafe-dictionary-type, anti-slop/no-runtime-typeof */\n"],"mappings":"qCAsDA,SAAgBA,EACf,EAGa,CAEb,GAAI,OAAOC,GAAe,WAAY,OAAOC,EAAiBD,CAAU,EAExE,IAAME,EAAS,CAAC,EACVC,EAAOC,MAAc,CAC1B,IAAMC,EAAQJ,EAAiBD,EAAW,CAAC,EAG3C,OAFA,OAAO,iBAAiBE,EAAQ,OAAO,0BAA0BG,CAAK,CAAC,EACvE,OAAO,eAAeH,EAAQ,OAAO,eAAeG,CAAK,CAAC,EACnDH,CACR,CAAC,EACD,OAAO,IAAI,MAAMA,EAAQ,CAExB,KAAM,EAAS,EAAK,IAAa,QAAQ,IAAIC,EAAK,EAAGG,EAAKC,CAAQ,EAClE,KAAM,EAAS,IAAQ,QAAQ,IAAIJ,EAAK,EAAGG,CAAG,EAC9C,YAAe,QAAQ,QAAQH,EAAK,CAAC,EACrC,0BAA2B,EAAS,IAAQ,QAAQ,yBAAyBA,EAAK,EAAGG,CAAG,EACxF,KAAM,EAAS,EAAK,IAAU,QAAQ,IAAIH,EAAK,EAAGG,EAAKD,CAAK,EAC5D,gBAAiB,EAAS,EAAK,IAAe,QAAQ,eAAeF,EAAK,EAAGG,EAAKE,CAAU,EAC5F,gBAAiB,EAAS,IAAQ,QAAQ,eAAeL,EAAK,EAAGG,CAAG,EACpE,mBAAsB,QAAQ,eAAeH,EAAK,CAAC,EACnD,gBAAiB,EAAS,IAAc,QAAQ,eAAeA,EAAK,EAAGM,CAAS,EAChF,sBAAyB,QAAQ,kBAAkBN,EAAK,CAAC,EACzD,iBAAoB,QAAQ,aAAaA,EAAK,CAAC,CAChD,CAAC,CACF,CAwBA,MAAMO,EAAa,CAAC,QAAS,SAAU,eAAgB,MAAM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