import { type Either, isLeft, right } from "./either.js"; import type { NonEmptyArray } from "ts-essentials"; import type { HKT, Intersection, MapOver } from "./hkt.js"; import type { Refinement, ANY, MIXED, Validation, Context, Encode, Decode, Is, Predicate, Errors } from "./context.js"; import { Codec, ValidationError, LazyContext, getFunctionName, identity } from "./context.js"; export function isNonEmpty(array: Array): array is NonEmptyArray { return array.length > 0; } export type TypeOf = T["_A"]; export type InputOf = T["_I"]; export type OutputOf = T["_O"]; export type TypeOf$ = T extends ANY ? T["_A"] : never; export type InputOf$ = T extends ANY ? T["_I"] : never; export type OutputOf$ = T extends ANY ? T["_O"] : never; export interface $TypeOf extends HKT { readonly type: TypeOf$; } export interface $OutputOf extends HKT { readonly type: OutputOf$; } export interface $InputOf extends HKT { readonly type: InputOf$; } export interface $PropsOf extends HKT { readonly type: PropsOf; } export type Props = { [K in string]: ANY }; type WithProps = { props: Props } | { codec: WithProps }; export type PropsOf = TCodec extends { props: infer P extends Props } ? P : TCodec extends { codec: infer P } ? PropsOf

: {}; export class TrivialCodec extends Codec { constructor( name: string, readonly is: Is, ) { super(name); } encode = identity; decode(input: unknown, context: Context): Validation { if (this.is(input)) { return context.success(input); } else { return context.failure(); } } } export const nullCodec = new TrivialCodec("null", (input: unknown): input is null => input === null); export const undefinedCodec = new TrivialCodec( "undefined", (input: unknown): input is undefined => input === undefined, ); export const voidCodec = new TrivialCodec("void", (input: unknown): input is void => input === undefined); export const string = new TrivialCodec( "string", (input: unknown): input is string => typeof input === "string", ); export const number = new TrivialCodec( "number", (input: unknown): input is number => typeof input === "number", ); export const boolean = new TrivialCodec( "boolean", (input: unknown): input is boolean => typeof input === "boolean", ); export const bigint = new TrivialCodec( "bigint", (input: unknown): input is bigint => typeof input === "bigint", ); export const unknown = new TrivialCodec("unknown", (input: unknown): input is unknown => true); export const object = new TrivialCodec( "object", (input: unknown): input is object => Boolean(input) && typeof input === "object", ); export class LiteralCodec extends TrivialCodec { readonly keys: TValue[]; constructor( private readonly value: TValue, name: string = JSON.stringify(value), ) { super(name, (input: unknown): input is TValue => input === value); this.keys = [value]; } } export function literal(value: TValue, name?: string) { return new LiteralCodec(value, name); } export const unknownArray = new TrivialCodec>( "UnknownArray", (input: unknown): input is Array => Array.isArray(input), ); export const unknownRecord = new TrivialCodec>( "UnknownDictionary", (input: unknown): input is Record => { const isObjectLike = Boolean(input) && typeof input === "object"; if (!isObjectLike) return false; const isArrayLike = Array.isArray(input); if (isArrayLike) return false; const proto = Object.getPrototypeOf(input); if (!proto) return true; const Ctor = Object.hasOwn(proto, "constructor") && proto.constructor; return typeof Ctor === "function" && Ctor instanceof Ctor && Ctor.toString() == Object.toString(); }, ); export class ArrayCodec extends Codec>, Array>> { constructor( readonly item: TCodec, name: string, ) { super(name); } decode(input: Array>, context: Context): Validation>> { const decodedArrayE = unknownArray.decode(input, context); if (isLeft(decodedArrayE)) return decodedArrayE; const decodedArray = decodedArrayE.right; const errors: Array = []; const result = new Array>(decodedArray.length); decodedArray.forEach((item, index) => { const decoded = this.item.decode(item, context.child(String(index), this.item, item)); if (isLeft(decoded)) { decoded.left.forEach((e) => errors.push(e)); } else { result[index] = decoded.right; } }); if (isNonEmpty(errors)) { return context.failures(errors); } else { return context.success(result); } } encode(value: Array>): Array> { if (this.item.encode === identity) { return value; } else { return value.map((itemValue) => this.item.encode(itemValue)); } } is(input: unknown): input is Array> { return unknownArray.is(input) && input.every((item) => this.item.is(item)); } } export function array( item: TCodec, name: string = `${item.name}[]`, ): Codec>, Array>> & { item: TCodec } { return new ArrayCodec(item, name); } export const any = new TrivialCodec("any", (input): input is any => true); export const never = new TrivialCodec("never", (input): input is never => false); export function getInterfaceTypeName(props: Props): string { return `{${getNameFromProps(props)}}`; } export function getNameFromProps(props: Props): string { return Object.keys(props) .map((k) => `${k}:${props[k].name}`) .join(","); } export class TypeCodec

extends Codec, MapOver> { constructor( readonly props: P, name: string = getInterfaceTypeName(props), ) { super(name); } decode(input: unknown, context: Context): Either> { const inputE = object.decode(input, context); if (isLeft(inputE)) return inputE; const inputObject = inputE.right as any; const output = { ...inputObject }; const errors: Array = []; Object.entries(this.props).forEach(([propName, propCodec]) => { const inputProperty = inputObject[propName]; const result = propCodec.decode(inputProperty, context.child(propName, propCodec, inputProperty)); if (isLeft(result)) { result.left.forEach((e) => errors.push(e)); } else { output[propName] = result.right; } }); return isNonEmpty(errors) ? context.failures(errors) : context.success(output as any); } encode(value: MapOver): MapOver { const s: Record = { ...value }; Object.entries(this.props).forEach(([propName, propCodec]) => { s[propName] = propCodec.encode(s[propName]); }); return s as any; } is(input: unknown): input is MapOver { if (!object.is(input)) return false; const inputObject = input as any; return Object.entries(this.props).every(([propName, propCodec]) => { const inputEntry = inputObject[propName]; if (inputEntry) { return propCodec.is(inputEntry); } else { return Object.hasOwn(input, propName) && propCodec.is(inputEntry); } }); } } export function type

(props: P, name?: string) { return new TypeCodec(props, name); } function getUnionName]>(codecs: CS): string { return codecs.map((type) => type.name).join("|"); } export class UnionCodec]> extends Codec< TypeOf, OutputOf > { constructor( readonly codecs: TCodecs, name: string = getUnionName(codecs), ) { super(name); } decode(input: unknown, context: Context): Validation> { const errors: Errors = []; for (const [index, codec] of this.codecs.entries()) { try { const result = codec.decode(input, context.child(String(index), codec, input)); if (isLeft(result)) { result.left.forEach((e) => errors.push(e)); } else { return context.success(result.right); } } catch (e) { errors.push(e as ValidationError); } } return context.failures(errors as NonEmptyArray); } encode(value: TypeOf): OutputOf { for (const codec of this.codecs) { if (codec.is(value)) { return codec.encode(value); } } throw new Error(`no codec found to encode value in union type ${this.name}`); } is(input: unknown): input is TypeOf { return this.codecs.some((c) => c.is(input)); } parse(input: unknown, context: Context = LazyContext.root(this, input)) { return super.parse(input, context); } } export function union]>(codecs: TCodecs, name?: string) { return new UnionCodec(codecs, name); } export class RefinementCodec = TypeOf> extends Codec< B, OutputOf, InputOf > { constructor( readonly codec: TCodec, readonly predicate: Predicate>, name = `${codec.name}≍${getFunctionName(predicate)}`, ) { super(name); } decode(input: InputOf, context: Context): Validation { const decodedE = this.codec.decode(input, context); if (isLeft(decodedE)) return decodedE; if (this.predicate(decodedE.right)) { return decodedE; } else { return context.failure(); } } encode(value: B): OutputOf { return this.codec.encode(value); } is(input: unknown): input is B { return this.codec.is(input) && this.predicate(input); } } export function refinement>( codec: TCodec, predicate: Refinement, B>, name?: string, ): RefinementCodec; export function refinement>( codec: TCodec, predicate: Predicate>, name?: string, ): RefinementCodec>; export function refinement>( codec: TCodec, predicate: Predicate>, name?: string, ): RefinementCodec> { return new RefinementCodec(codec, predicate, name); } export class DefaultsCodec extends Codec, OutputOf, InputOf> { readonly is: Is>; readonly encode: Encode, OutputOf>; constructor( readonly codec: TCodec, readonly replacement: TypeOf, name: string = `(${codec.name} ❮ ${replacement})`, ) { super(name); this.is = this.codec.is.bind(this.codec); this.encode = this.codec.encode.bind(this.codec); } decode(input: InputOf, context: Context): Validation> { const decodedE = this.codec.decode(input, new LazyContext(context.trail)); if (isLeft(decodedE)) { return context.success(this.replacement); } else { return decodedE; } } } export function defaults(codec: TCodec, replacement: TypeOf, name?: string) { return new DefaultsCodec(codec, replacement, name); } export class ReplacementCodec extends Codec, OutputOf, InputOf> { readonly is: Is>; readonly encode: Encode, OutputOf>; constructor( readonly codec: TCodec, readonly replacement: InputOf, name: string = `(${codec.name} ❮❮ ${replacement})`, ) { super(name); this.is = this.codec.is.bind(this.codec); this.encode = this.codec.encode.bind(this.codec); } decode(input: InputOf, context: Context): Validation> { const decodedE = this.codec.decode(input, new LazyContext(context.trail)); if (isLeft(decodedE)) { return this.codec.decode(this.replacement, context); } else { return decodedE; } } } export function replacement(codec: TCodec, replacement: InputOf, name?: string) { return new ReplacementCodec(codec, replacement, name); } export class PostprocessDecodeCodec extends Codec< TypeOf, OutputOf, InputOf > { readonly is: Is>; readonly encode: Encode, OutputOf>; constructor( readonly codec: TCodec, readonly onDecode: (input: TypeOf) => TypeOf, name: string = `(${codec.name} ❮❮ ??})`, ) { super(name); this.is = this.codec.is.bind(this.codec); this.encode = this.codec.encode.bind(this.codec); } decode(input: InputOf, context: Context): Validation> { const decodedE = this.codec.decode(input, new LazyContext(context.trail)); if (isLeft(decodedE)) { return decodedE; } else { return right(this.onDecode(decodedE.right)); } } } /** * Replace input in runtime for `codec`. For example, change `${CWD}` in the input to a current working dir. */ export function postprocessDecode( codec: TCodec, replacementFn: PostprocessDecodeCodec["onDecode"], name?: string, ): PostprocessDecodeCodec { return new PostprocessDecodeCodec(codec, replacementFn, name); } export class TupleCodec]> extends Codec< MapOver, MapOver > { constructor( readonly codecs: TCodecs, readonly name: string = `[${codecs.map((type) => type.name).join(",")}]`, ) { super(name); } decode(input: MapOver, context: Context): Validation> { const arrayE = unknownArray.decode(input, context); if (isLeft(arrayE)) return arrayE; const array = arrayE.right; // strip additional components let result: Array = array.length > this.codecs.length ? array.slice(0, this.codecs.length) : array; const errors: Errors = []; for (const [index, codec] of this.codecs.entries()) { const decodedE = codec.decode(array[index], context.child(String(index), codec, array[index])); if (isLeft(decodedE)) { decodedE.left.forEach((e) => errors.push(e)); } else { result[index] = decodedE.right; } } return isNonEmpty(errors) ? context.failures(errors) : context.success(result as any); } encode(value: MapOver): MapOver { return this.codecs.map((type, i) => type.encode(value[i])) as any; } is(input: unknown): input is MapOver { return ( unknownArray.is(input) && input.length === this.codecs.length && this.codecs.every((type, i) => type.is(input[i])) ); } } export function tuple>(codecs: TCodecs, name?: string): TupleCodec { return new TupleCodec(codecs, name); } function stripKeys>(o: T, props: Props): T { const keys = Object.getOwnPropertyNames(o); let shouldStrip = false; const r: any = {}; for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (!Object.hasOwn(props, key)) { shouldStrip = true; } else { r[key] = o[key]; } } return shouldStrip ? r : o; } export class ExactCodec extends Codec, OutputOf, InputOf> { readonly props: PropsOf; constructor( readonly codec: TCodec & WithProps, name: string = `Exact<${codec.name}>`, ) { super(name); this.props = getProps(codec); } is(input: unknown): input is InputOf { return this.codec.is(input); } decode(input: InputOf, context: Context): Validation> { const dictionaryE = object.decode(input, context); if (isLeft(dictionaryE)) return dictionaryE; const dictionaryDecoded = dictionaryE.right as any; return this.codec.decode(stripKeys(dictionaryDecoded, this.props), context); } encode(value: TypeOf): OutputOf { return this.codec.encode(stripKeys(value, this.props)); } } export function exact(codec: TCodec, name?: string): ExactCodec { return new ExactCodec(codec, name); } function hasOwnProps(codec: TCodec): codec is TCodec & { props: PropsOf } { return "props" in codec; } function hasOwnCodec(codec: TCodec): codec is TCodec & { codec: ANY } { return "codec" in codec; } function hasOwnCodecs(codec: TCodec): codec is TCodec & { codecs: Array } { return "codecs" in codec; } function getProps(codec: TCodec): PropsOf { if (hasOwnProps(codec)) return codec.props; if (hasOwnCodec(codec)) return getProps(codec.codec) as any; if (hasOwnCodecs(codec)) return Object.assign({}, ...codec.codecs.map((c) => getProps(c))); return {} as any; } export function strict

(props: P, name?: string): ExactCodec> { return exact(type(props), name); } export class ReadonlyCodec extends Codec< Readonly>, OutputOf, InputOf > { readonly codec: TCodec; readonly name: string; readonly is: TCodec["is"]; readonly encode: TCodec["encode"]; readonly decode: TCodec["decode"]; constructor(codec: TCodec, name = `Readonly<${codec.name}>`) { super(name); this.codec = codec; this.name = name; this.is = this.codec.is.bind(this.codec); this.encode = this.codec.encode.bind(this.codec); this.decode = this.codec.decode.bind(this.codec); } } export function readonly(codec: TCodec, name?: string) { return new ReadonlyCodec(codec, name); } export class PartialCodec

extends Codec>, Partial>> { constructor( readonly props: P, name: string = `Partial<${getInterfaceTypeName(props)}>`, ) { super(name); } encode(value: Partial>): Partial> { const s: Record = { ...value }; Object.entries(this.props).forEach(([propName, propCodec]) => { const inputEntry = value[propName]; if (inputEntry) { s[propName] = propCodec.encode(inputEntry); } }); return s as any; } is(input: unknown): input is Partial> { if (!unknownRecord.is(input)) return false; return Object.entries(this.props).every(([propName, propCodec]) => { const inputEntry = input[propName]; if (inputEntry) { return propCodec.is(inputEntry); } else { return true; } }); } decode(input: unknown, context: Context): Validation>> { const dictionaryE = unknownRecord.decode(input, context); if (isLeft(dictionaryE)) return dictionaryE; const dictionary = dictionaryE.right; const errors: Array = []; const result: Record = { ...dictionary }; for (const [propName, propCodec] of Object.entries(this.props)) { const inputEntry = dictionary[propName]; const decodedE = propCodec.decode(inputEntry, context.child(propName, propCodec, inputEntry)); if (isLeft(decodedE)) { if (inputEntry) decodedE.left.forEach((e) => errors.push(e)); } else { result[propName] = decodedE.right; } } return isNonEmpty(errors) ? context.failures(errors) : context.success(result as any); } } export function partial

(props: P, name?: string): PartialCodec

{ return new PartialCodec(props, name); } // FIXME Replace with Object.assign // It is shallow anyway export function mergeAll(base: any, us: Array): any { let equal = true; let primitive = true; const baseIsNotADictionary = !unknownRecord.is(base); for (const u of us) { if (u !== base) { equal = false; } if (unknownRecord.is(u)) { primitive = false; } } if (equal) { return base; } else if (primitive) { return us[us.length - 1]; } const r: any = {}; for (const u of us) { for (const k in u) { if (!Object.hasOwn(r, k) || baseIsNotADictionary || u[k] !== base[k]) { r[k] = u[k]; } } } return r; } export class IntersectionCodec>> extends Codec< Intersection>, Intersection>, Intersection> > { readonly props: Intersection>; constructor( readonly codecs: TCodecs, name: string = `${codecs.map((c) => c.name).join("&")}`, ) { super(name); this.props = Object.assign({}, ...codecs.map((c) => getProps(c))); } encode(value: Intersection>): Intersection> { return mergeAll( value, this.codecs.map((codec) => codec.encode(value)), ); } is(input: unknown): input is Intersection> { return this.codecs.every((codec) => codec.is(input)); } decode(input: unknown, context: Context): Validation>> { const us: Array = []; const errors: Errors = []; for (const [index, codec] of this.codecs.entries()) { const result = codec.decode(input, context.child(String(index), codec, input)); if (isLeft(result)) { result.left.forEach((e) => errors.push(e)); } else { us.push(result.right); } } if (isNonEmpty(errors)) { return context.failures(errors); } else { return context.success(mergeAll(input, us)); } } } export function intersection(codecs: TCodecs, name?: string) { return new IntersectionCodec(codecs, name); } export class KeyOfCodec> extends TrivialCodec { readonly keys: string[]; constructor(keys: Array, name: string, is: Is) { super(name, is); this.keys = keys; } } export function keyof>( keys: D, name = Object.keys(keys) .map((k) => JSON.stringify(k)) .join("|"), ) { return new KeyOfCodec( Object.keys(keys), name, (input): input is keyof D => string.is(input) && Object.hasOwn(keys, input), ); } /** * Codec for `Record` where `string` could be replaced by a string-like non-enumerable codec. * @see EnumerableRecordCodec */ export class NonEnumerableRecordCodec extends Codec< { [K in TypeOf]: TypeOf }, { [K in OutputOf]: OutputOf } > { constructor( readonly domain: D, readonly codomain: C, name: string = `{[${domain.name}]:${codomain.name}}`, ) { super(name); } decode(input: unknown, context: Context): Validation<{ [K in TypeOf]: TypeOf }> { const dictionaryE = unknownRecord.decode(input, context); if (isLeft(dictionaryE)) return dictionaryE; const dictionary = dictionaryE.right; const result: Record = {}; const errors: Array = []; for (const [name, prop] of Object.entries(dictionary)) { const domainResult = this.domain.decode(name, context.child(name, this.domain, name)); if (isLeft(domainResult)) { domainResult.left.forEach((e) => errors.push(e)); } else { const codomainResult = this.codomain.decode(prop, context.child(name, this.codomain, prop)); if (isLeft(codomainResult)) { codomainResult.left.forEach((e) => errors.push(e)); } else { result[domainResult.right] = codomainResult.right; } } } if (isNonEmpty(errors)) { return context.failures(errors); } else { return context.success(result) as any; } } encode(value: { [K in TypeOf]: TypeOf }): { [K in OutputOf]: OutputOf } { const result: { [key: string]: any } = {}; for (const [name, valueItem] of Object.entries(value)) { result[String(this.domain.encode(name))] = this.codomain.encode(valueItem); } return result as any; } is(input: unknown): input is { [K in TypeOf]: TypeOf } { if (!unknownRecord.is(input)) return false; const entries = Object.entries(input); if (entries.length == 0) return true; return entries.some(([key, value]) => this.domain.is(key) && this.codomain.is(value)); } } export class EnumerableRecordCodec extends Codec< { [K in TypeOf]: TypeOf }, { [K in OutputOf]: OutputOf } > { readonly keys: string[]; constructor( readonly domain: D, readonly codomain: C, name: string = `{[${domain.name}]:${codomain.name}}`, ) { super(name); this.keys = domain.keys; } decode(input: unknown, context: Context): Validation<{ [K in TypeOf]: TypeOf }> { const recordE = unknownRecord.decode(input, context); if (isLeft(recordE)) return recordE; const inputRecord = recordE.right; const result: Record = {}; const errors: Errors = []; for (const k of this.keys) { const inputElement = inputRecord[k]; const codomainResult = this.codomain.decode(inputElement, context.child(k, this.codomain, inputElement)); if (isLeft(codomainResult)) { codomainResult.left.forEach((e) => errors.push(e)); } else { result[k] = codomainResult.right; } } if (isNonEmpty(errors)) { return context.failures(errors); } else { return context.success(result); } } encode(value: { [K in TypeOf]: TypeOf }): { [K in OutputOf]: OutputOf } { const s: { [key: string]: any } = {}; for (const k of this.keys) { s[k] = this.codomain.encode((value as any)[k]); } return s as any; } is(input: unknown): input is { [K in TypeOf]: TypeOf } { return unknownRecord.is(input) && this.keys.every((k) => this.codomain.is(input[k])); } } export interface EnumerableRecordDomain { keys: string[]; } export function record, C extends MIXED>( domain: D, codomain: C, name?: string, ): NonEnumerableRecordCodec; export function record, C extends MIXED>( domain: D & EnumerableRecordDomain, codomain: C, name?: string, ): EnumerableRecordCodec; export function record, C extends MIXED>( domain: D | (D & EnumerableRecordDomain), codomain: C, name?: string, ): EnumerableRecordCodec | NonEnumerableRecordCodec { if ("keys" in domain) { return new EnumerableRecordCodec(domain, codomain, name); } else { return new NonEnumerableRecordCodec(domain, codomain, name); } } export class RecursiveCodec extends Codec { constructor( /** a unique name for this codec */ readonly name: string, /** a custom type guard */ readonly is: Is, /** succeeds if a value of type I can be decoded to a value of type A */ readonly decode: Decode, /** converts a value of type A to a value of type O */ readonly encode: Encode, private readonly runDefinition: () => C, ) { super(name); } get codec(): C { return this.runDefinition(); } } export function recursive = Codec>( name: string, definition: (self: C) => C, ): RecursiveCodec { let cache: C; let Self: any; const runDefinition = (): C => { if (!cache) { cache = definition(Self); (cache as any).name = name; } return cache; }; Self = new RecursiveCodec( name, (input): input is A => runDefinition().is(input), (input, context) => runDefinition().decode(input, context), (value) => runDefinition().encode(value), runDefinition, ); return Self; } type OptionalFlag = { optional: true }; export function optional(codec: C, name?: string): OptionalCodec { return new OptionalCodec(codec, name); } export class OptionalCodec extends Codec< TypeOf | undefined, OutputOf | undefined, InputOf | undefined > { readonly optional: true = true; readonly #codec: UnionCodec<[C, typeof undefinedCodec]>; constructor( codec: C, readonly name: string = `${codec.name}?`, ) { super(name); this.#codec = union([codec, undefinedCodec]); } decode(input: InputOf | undefined, context: Context): Validation | undefined> { return this.#codec.decode(input, context); } encode(value: TypeOf | undefined): OutputOf | undefined { return this.#codec.encode(value); } is(input: unknown): input is TypeOf | undefined { return this.#codec.is(input); } } export function isOptionalCodec(codec: C | OptionalCodec): codec is OptionalCodec { return "optional" in codec && codec.optional; } type RequiredPropsKeys

= { [K in keyof P]: P[K] extends OptionalFlag ? never : K }[keyof P]; type RequiredProps

= { [K in RequiredPropsKeys

]: P[K] }; type OptionalProps

= { [K in Exclude>]?: P[K] }; export class SparseCodec

extends Codec< MapOver, $TypeOf> & MapOver, $TypeOf>, MapOver, $OutputOf> & MapOver, $OutputOf> > { readonly #codec: TypeCodec

; constructor( readonly props: P, name: string = getInterfaceTypeName(props), ) { super(name); this.#codec = new TypeCodec(props); } cleanup(output: any): any { Object.entries(this.props).forEach(([propName, propCodec]) => { if (isOptionalCodec(propCodec) && typeof output[propName] === "undefined") { delete output[propName]; } }); return output; } decode(input: unknown, context: Context): Either> { const outputE = this.#codec.decode(input, context); if (isLeft(outputE)) return outputE; return context.success(this.cleanup(outputE.right)); } encode(value: MapOver, $TypeOf> & MapOver, $TypeOf>): MapOver { const output = this.#codec.encode(value as any); return this.cleanup(output); } is(input: unknown): input is MapOver, $TypeOf> & MapOver, $TypeOf> { const effectiveInput = { ...(input as any) }; Object.entries(this.props).forEach(([propName, propCodec]) => { if (isOptionalCodec(propCodec) && !effectiveInput[propName]) { effectiveInput[propName] = undefined; } }); return this.#codec.is(effectiveInput); } } export function sparseType

(props: P, name?: string) { const optionalProps: Props = {}; const requiredProps: Props = {}; for (const [key, value] of Object.entries(props)) { if (isOptionalCodec(value)) { optionalProps[key] = value; } else { requiredProps[key] = value; } } return new SparseCodec(props, name); } export const sparse = sparseType; export { nullCodec as null, undefinedCodec as undefined, voidCodec as void };