/** * Skillet is an LLM-optimized streaming JSON Parser - perfectly suited for streaming hot and fresh JSON. * * Portions of this code are derived from Zod (MIT License) (https://github.com/colinhacks/zod). * See the LICENSE file in the project root for full license text. * * @license MIT * @author LiveLoveApp, LLC * @see https://github.com/liveloveapp/hashbrown * @see https://github.com/colinhacks/zod */ import type { JsonAstNode, JsonResolvedValue, ParserError } from '../skillet/parser/json-parser'; import { internal } from './constants'; import { CleanInterfaceShape, Flatten, IsStringUnion, IsUnion, UnionToTuple } from '../utils/types'; /** * @internal */ export { internal, PRIMITIVE_WRAPPER_FIELD_NAME } from './constants'; type TypeInternals = { definition: HashbrownTypeDefinition; }; export type StringFormat = 'date-time' | 'time' | 'date' | 'duration' | 'email' | 'hostname' | 'ipv4' | 'ipv6' | 'uuid'; /** * @internal */ export interface HashbrownTypeCtor { new (def: D): T; init(inst: T, def: D): asserts inst is T; toJsonSchema(schema: any): any; toTypeScript: (pathSeen?: Set) => string; fromJsonAstImpl: (schema: HashbrownTypeCtor) => FromJsonAstImpl; } /** * @internal */ export declare const HashbrownTypeCtor: ({ name, initializer, toJsonSchemaImpl, toTypeScriptImpl, fromJsonAstImpl, validateImpl, }: { name: string; initializer: (instance: T, definition: D) => void; toJsonSchemaImpl: (schema: HashbrownTypeCtor) => any; toTypeScriptImpl: (schema: HashbrownTypeCtor, pathSeen: Set) => string; fromJsonAstImpl: (schema: HashbrownTypeCtor) => FromJsonAstImpl; validateImpl: (schema: HashbrownTypeCtor, definition: D, object: unknown, path: string[]) => void; }) => HashbrownTypeCtor; interface HashbrownTypeDefinition { type: 'string' | 'literal' | 'number' | 'boolean' | 'integer' | 'object' | 'array' | 'enum' | 'any-of' | 'null' | 'node'; description: string; streaming: boolean; } /** * @public */ export interface HashbrownType { [internal]: HashbrownTypeInternals; toJsonSchema: () => any; validate: (object: unknown, path?: string[]) => void; toTypeScript: (pathSeen?: Set) => string; fromJsonAst: (input: FromJsonAstInput) => FromJsonAstOutput; } /** * @internal */ export interface HashbrownTypeInternals extends HashbrownType { definition: HashbrownTypeDefinition; result: Result; } /** * @public */ export declare const HashbrownType: HashbrownTypeCtor; export type FromJsonAstInput = { nodes: JsonAstNode[]; rootId: number | null; error: ParserError | null; cache?: FromJsonAstCache; schemaId: number; schema: HashbrownType; }; export type FromJsonAstResult = { state: 'match'; value: T; } | { state: 'no-match'; } | { state: 'invalid'; }; export type FromJsonAstCache = { byNodeId: Record; byNodeIdAndSchemaId: Record; }; export type FromJsonAstOutput = { result: FromJsonAstResult; cache: FromJsonAstCache; }; type FromJsonAstImpl = (input: FromJsonAstInput) => FromJsonAstOutput; /** * -------------------------------------- * -------------------------------------- * String Type * -------------------------------------- * -------------------------------------- */ interface StringTypeDefinition extends HashbrownTypeDefinition { type: 'string'; pattern?: string; format?: StringFormat; } /** * @internal */ export interface StringTypeInternals extends HashbrownTypeInternals { definition: StringTypeDefinition; } /** * @public */ export interface StringType extends HashbrownType { [internal]: StringTypeInternals; } export type StringConstraintsInput = { pattern?: string | RegExp; format?: StringFormat; }; type NumberConstraints = { multipleOf?: number; maximum?: number; exclusiveMaximum?: number; minimum?: number; exclusiveMinimum?: number; }; /** * @public */ export declare const StringType: HashbrownTypeCtor; /** * @public */ export declare function isStringType(type: HashbrownType): type is StringType; /** * @public */ export declare function string(description: string, constraints?: StringConstraintsInput): StringType; /** * -------------------------------------- * -------------------------------------- * Literal Type * -------------------------------------- * -------------------------------------- */ interface LiteralTypeDefinition extends HashbrownTypeDefinition { type: 'literal'; value: T; } /** * @internal */ export interface LiteralTypeInternals extends HashbrownTypeInternals { definition: LiteralTypeDefinition; } /** * @public */ export interface LiteralType extends HashbrownType { [internal]: LiteralTypeInternals; } /** * @public */ export declare const LiteralType: HashbrownTypeCtor; /** * @public */ export declare function isLiteralType(type: HashbrownType): type is LiteralType; /** * @public */ export declare function literal(value: T): LiteralType; /** * -------------------------------------- * -------------------------------------- * Number Type * -------------------------------------- * -------------------------------------- */ interface NumberTypeDefinition extends HashbrownTypeDefinition { type: 'number'; multipleOf?: number; maximum?: number; exclusiveMaximum?: number; minimum?: number; exclusiveMinimum?: number; } /** * @internal */ export interface NumberTypeInternals extends HashbrownTypeInternals { definition: NumberTypeDefinition; } /** * @public */ export interface NumberType extends HashbrownType { [internal]: NumberTypeInternals; } /** * @public */ export declare const NumberType: HashbrownTypeCtor; /** * @public */ export declare function isNumberType(type: HashbrownType): type is NumberType; /** * @public */ export declare function number(description: string, constraints?: NumberConstraints): NumberType; /** * -------------------------------------- * -------------------------------------- * Boolean Type * -------------------------------------- * -------------------------------------- */ interface BooleanTypeDefinition extends HashbrownTypeDefinition { type: 'boolean'; } /** * @internal */ export interface BooleanTypeInternals extends HashbrownTypeInternals { definition: BooleanTypeDefinition; } /** * @public */ export interface BooleanType extends HashbrownType { [internal]: BooleanTypeInternals; } /** * @public */ export declare const BooleanType: HashbrownTypeCtor; /** * @public */ export declare function isBooleanType(type: HashbrownType): type is BooleanType; /** * @public */ export declare function boolean(description: string): BooleanType; /** * -------------------------------------- * -------------------------------------- * Integer Type * -------------------------------------- * -------------------------------------- */ interface IntegerTypeDefinition extends HashbrownTypeDefinition { type: 'integer'; multipleOf?: number; maximum?: number; exclusiveMaximum?: number; minimum?: number; exclusiveMinimum?: number; } /** * @internal */ export interface IntegerTypeInternals extends HashbrownTypeInternals { definition: IntegerTypeDefinition; } /** * @public */ export interface IntegerType extends HashbrownType { [internal]: IntegerTypeInternals; } /** * @public */ export declare const IntegerType: HashbrownTypeCtor; /** * @public */ export declare function isIntegerType(type: HashbrownType): type is IntegerType; /** * @public */ export declare function integer(description: string, constraints?: NumberConstraints): IntegerType; /** * -------------------------------------- * -------------------------------------- * Object Type * -------------------------------------- * -------------------------------------- */ type ObjectTypeResult> = string extends keyof T ? object : {} extends T ? object : Flatten<{ -readonly [K in keyof T]: T[K][internal]['result']; }>; interface ObjectTypeDefinition = Record> extends HashbrownTypeDefinition { type: 'object'; readonly shape: Shape; } /** * @internal */ export interface ObjectTypeInternals>> extends HashbrownTypeInternals> { definition: ObjectTypeDefinition; } /** * @public */ export interface ObjectType> = Readonly>> extends HashbrownType { [internal]: ObjectTypeInternals; } /** * @public */ export declare const ObjectType: HashbrownTypeCtor; /** * @public */ export declare function isObjectType(type: HashbrownType): type is ObjectType; /** * @public */ export declare function object>(description: string, shape: Shape): ObjectType>; /** * -------------------------------------- * -------------------------------------- * Array Type * -------------------------------------- * -------------------------------------- */ interface ArrayTypeDefinition extends HashbrownTypeDefinition { type: 'array'; element: Item; minItems?: number; maxItems?: number; } /** * @internal */ export interface ArrayTypeInternals extends HashbrownTypeInternals { definition: ArrayTypeDefinition; } /** * @public */ export interface ArrayType extends HashbrownType { [internal]: ArrayTypeInternals; } /** * @public */ export declare const ArrayType: HashbrownTypeCtor; /** * @public */ export declare function isArrayType(type: HashbrownType): type is ArrayType; /** * @public */ export declare function array(description: string, item: Item, constraints?: { minItems?: number; maxItems?: number; }): ArrayType; /** * -------------------------------------- * -------------------------------------- * Any-Of Type * -------------------------------------- * -------------------------------------- */ interface AnyOfTypeDefinition extends HashbrownTypeDefinition { type: 'any-of'; options: Options; } /** * @internal */ export interface AnyOfTypeInternals extends HashbrownTypeInternals { definition: AnyOfTypeDefinition; } /** * @public */ export interface AnyOfType extends HashbrownType { [internal]: AnyOfTypeInternals; } /** * @public */ export declare const AnyOfType: HashbrownTypeCtor; /** * @public */ export declare function isAnyOfType(type: HashbrownType): type is AnyOfType; /** * @public */ export declare function isNodeType(type: HashbrownType): type is NodeType; /** * @public */ export declare function anyOf(options: Options): SchemaForUnion; /** * -------------------------------------- * -------------------------------------- * Node Type * -------------------------------------- * -------------------------------------- */ type NodeResult = { complete: boolean; partialValue: JsonResolvedValue; value?: Inner[internal]['result']; }; interface NodeTypeDefinition extends HashbrownTypeDefinition { type: 'node'; inner: Inner; } /** * @internal */ export interface NodeTypeInternals extends HashbrownTypeInternals> { definition: NodeTypeDefinition; } /** * @public */ export interface NodeType extends HashbrownType> { [internal]: NodeTypeInternals; } /** * @public */ export declare const NodeType: HashbrownTypeCtor; /** * @public */ export declare function node(inner: Inner): NodeType; /** * -------------------------------------- * -------------------------------------- * Enum Type * -------------------------------------- * -------------------------------------- */ /** * @internal */ interface EnumTypeDefinition extends HashbrownTypeDefinition { type: 'enum'; entries: Entries; } /** * @internal */ export interface EnumTypeInternals extends HashbrownTypeInternals { definition: EnumTypeDefinition; } /** * @public */ export interface EnumType extends HashbrownType { /** * @internal */ [internal]: EnumTypeInternals; } /** * @public */ export declare const EnumType: HashbrownTypeCtor; /** * @public */ export declare function isEnumType(type: HashbrownType): type is EnumType; /** * @public */ export declare function enumeration(description: string, entries: [...Entries]): EnumType; /** * -------------------------------------- * -------------------------------------- * Null Type * -------------------------------------- * -------------------------------------- */ interface NullTypeDefinition extends HashbrownTypeDefinition { type: 'null'; } /** * @internal */ export interface NullTypeInternals extends HashbrownTypeInternals { definition: NullTypeDefinition; } /** * @public */ export interface NullType extends HashbrownType { [internal]: NullTypeInternals; } /** * @public */ export declare const NullType: HashbrownTypeCtor; /** * @public */ export declare function isNullType(type: HashbrownType): type is NullType; /** * @public */ export declare function nullish(): NullType; /** * -------------------------------------- * -------------------------------------- * Streaming Helpers * -------------------------------------- * -------------------------------------- */ export declare function isStreaming(schema: HashbrownType): boolean; /** * @public */ export declare function isHashbrownType(type: any): type is HashbrownType; /** * -------------------------------------- * -------------------------------------- * Type Utilities * -------------------------------------- * -------------------------------------- */ /** * @public */ export type Infer = T[internal]['result']; /** * @internal */ export type SchemaForUnion = AnyOfType extends infer U ? U extends any[] ? { [K in keyof U]: Schema; } : never : never>; /** * @public */ export type Schema = IsStringUnion extends true ? [T] extends [string] ? UnionToTuple extends infer U ? U extends string[] ? EnumType : never : never : never : IsUnion extends true ? SchemaForUnion : T extends Array ? ArrayType> : T extends string ? string extends T ? StringType : LiteralType : T extends number ? NumberType : T extends boolean ? BooleanType : T extends null ? NullType : T extends object ? ObjectType<{ [K in keyof T]: Schema; }> : never; //# sourceMappingURL=base.d.ts.map