|
| 1 | +/// <reference types="node" /> |
| 2 | + |
| 3 | +type ReadFn = (buffer: Buffer, cursor: number, _fieldInfo: any, rootNodes: any) => any |
| 4 | +type WriteFn = (value: any, buffer: Buffer, offset: number, _fieldInfo: any, rootNode: any) => number |
| 5 | +type SizeOfFn = ((value: any, _fieldInfo: any, rootNode: any) => number) |
| 6 | + |
| 7 | +type FieldInfo = string | { type: string, typeArgs: any } |
| 8 | +type TypeFunc = [ReadFn, WriteFn, number | SizeOfFn, ?any] |
| 9 | +type TypeParams = any |
| 10 | +declare interface TypeParamsCounted { count: number | FieldInfo, countType: TypeDef } |
| 11 | +type TypeDef = 'native' | TypeFunc | [string, any] |
| 12 | +type TypesDef = { [field: string]: TypeDef } |
| 13 | +type Protocol = { |
| 14 | + types: TypesDef |
| 15 | + [field: string]: TypeDef | Protocol |
| 16 | +} |
| 17 | +type Results = { |
| 18 | + value: any, |
| 19 | + size: number |
| 20 | +} |
| 21 | +type ExtendedResults = { |
| 22 | + data: any, |
| 23 | + metadata: { |
| 24 | + size: number |
| 25 | + }, |
| 26 | + buffer: Buffer, |
| 27 | + fullBuffer: Buffer |
| 28 | +} |
| 29 | + |
| 30 | +declare abstract class TransformSerialization extends Transform { |
| 31 | + private proto: ProtoDef |
| 32 | + private mainType: string |
| 33 | + constructor(proto: ProtoDef, mainType: string) |
| 34 | + private _transform(chunk: any, enc: BufferEncoding, cb: CallableFunction): never |
| 35 | +} |
| 36 | + |
| 37 | +declare class ProtodefValidator { |
| 38 | + constructor(typesSchemas: unknown) |
| 39 | + createAjvInstance(typesSchemas): void |
| 40 | + addDefaultTypes(): void |
| 41 | + addTypes(schemas): void |
| 42 | + typeToSchemaName(name: string): string |
| 43 | + addType(name: string, schema: unknown): void |
| 44 | + validateType(type: unknown): void |
| 45 | + validateTypeGoingInside(type: unknown): void |
| 46 | + validateProtocol(protocol: unknown): void |
| 47 | +} |
| 48 | +declare type TypeDefKind = 'native' | 'context' | 'parametrizable' |
| 49 | + |
| 50 | +declare abstract class ProtodefBaseCompiler { |
| 51 | + primitiveTypes = {} |
| 52 | + native = {} |
| 53 | + context = {} |
| 54 | + types: TypesDef |
| 55 | + scopeStack = [] |
| 56 | + parameterizableTypes = {} |
| 57 | + addNativeType(type: string, fn: CallableFunction): void |
| 58 | + addContextType(type: string, fn: CallableFunction): void |
| 59 | + addParametrizableType(type: string, maker: CallableFunction): void |
| 60 | + addTypes(types: { [key: string]: [TypeDefKind, CallableFunction] }): void |
| 61 | + addProtocol(protocolData: Protocol, path: string): void |
| 62 | + protected addTypesToCompile(types: any): void |
| 63 | + protected indent(code: string, indent: string): string |
| 64 | + protected getField(name: string): any |
| 65 | + generate(): string |
| 66 | + compile(code: string): Function<any> |
| 67 | + protected wrapCode (code: string, args: string[]): string |
| 68 | + protected compileType(type: string | any[]): string |
| 69 | +} |
| 70 | + |
| 71 | +declare class ProtodefReadCompiler extends ProtodefBaseCompiler { |
| 72 | + private callType(value: string, type: string | any[], offsetExpr: string, args: string[]): string |
| 73 | +} |
| 74 | + |
| 75 | +declare class ProtodefWriteCompiler extends ProtodefBaseCompiler { |
| 76 | + private callType(value: string, type: string | any[], offsetExpr: string, args: string[]): string |
| 77 | +} |
| 78 | + |
| 79 | +declare class ProtodefSizeOfCompiler extends ProtodefBaseCompiler { |
| 80 | + private callType(value: string, type: string | any[], args: string[]): string |
| 81 | +} |
| 82 | + |
| 83 | +declare class ProtodefCompiler { |
| 84 | + readCompiler: ProtodefReadCompiler |
| 85 | + writeCompiler: ProtodefWriteCompiler |
| 86 | + sizeOfCompiler: ProtodefSizeOfCompiler |
| 87 | + addTypes(types: { [key: string]: [TypeDefKind, CallableFunction] }): void |
| 88 | + addProtocol(protocolData: Protocol, path: string): void |
| 89 | + protected addTypesToCompile(types: any): void |
| 90 | + addVariable(key: string, val: any): void |
| 91 | + compileProtoDefSync(options?: { printCode?: boolean }): CompiledProtoDef |
| 92 | +} |
| 93 | + |
| 94 | +declare abstract class AbstractProtoDefInterface { |
| 95 | + read: ReadFn |
| 96 | + write: WriteFn |
| 97 | + sizeOf: SizeOfFn |
| 98 | + createPacketBuffer(type: string, packet: any): Buffer |
| 99 | + parsePacketBuffer(type: string, buffer: Buffer, offset = 0): ExtendedResults |
| 100 | +} |
| 101 | + |
| 102 | +declare class CompiledProtoDef extends AbstractProtoDefInterface { |
| 103 | + private sizeOfCtx: SizeOfFn |
| 104 | + private writeCtx: WriteFn |
| 105 | + private readCtx: ReadFn |
| 106 | + constructor(sizeOfCtx: SizeOfFn, writeCtx: WriteFn, readCtx: ReadFn) |
| 107 | + setVariable(key: string, val: any): void |
| 108 | +} |
| 109 | + |
| 110 | +declare class ProtodefPartialError extends Error { |
| 111 | + partialReadError: true |
| 112 | + constructor(message?: string) |
| 113 | +} |
| 114 | + |
| 115 | +declare module 'protodef' { |
| 116 | + export class ProtoDef extends AbstractProtoDefInterface { |
| 117 | + private types: TypesDef |
| 118 | + constructor(validation: boolean = true) |
| 119 | + private addDefaultTypes(): void |
| 120 | + addType(name: string, functions: TypeDef, validate = true): void |
| 121 | + addTypes(types: TypesDef): void |
| 122 | + addProtocol(protocolData: Protocol, path: string[]): void |
| 123 | + setVariable(key: string, val: any): void |
| 124 | + } |
| 125 | + export class Serializer extends TransformSerialization { |
| 126 | + private queue: Buffer |
| 127 | + createPacketBuffer(packet: any): Buffer |
| 128 | + } |
| 129 | + export class Parser extends TransformSerialization { |
| 130 | + private queue: Buffer |
| 131 | + parsePacketBuffer(packet: any): Buffer |
| 132 | + } |
| 133 | + export class FullPacketParser extends TransformSerialization { |
| 134 | + noErrorLogging: boolean |
| 135 | + constructor(proto: ProtoDef, mainType: string, noErrorLogging = false) |
| 136 | + parsePacketBuffer(packet: any): Buffer |
| 137 | + } |
| 138 | + export const Compiler: { |
| 139 | + ReadCompiler: ProtodefReadCompiler |
| 140 | + WriteCompiler: ProtodefWriteCompiler |
| 141 | + SizeOfCompiler: ProtodefSizeOfCompiler |
| 142 | + ProtoDefCompiler: ProtodefCompiler |
| 143 | + CompiledProtodef: CompiledProtodef |
| 144 | + } |
| 145 | + export const utils: { |
| 146 | + getField(countField: string, context: object): any | undefined |
| 147 | + getFieldInfo(fieldInfo: FieldInfo): FieldInfo |
| 148 | + addErrorField(e: Error & { field: string }, field: string): Error & { field: string } |
| 149 | + getCount(buffer: Buffer, offset: number, options: TypeParamsCounted, rootNode: any): { count: number, size: number } |
| 150 | + sendCount(len: number, buffer: Buffer, offset: number, options: TypeParamsCounted, rootNode: any): number |
| 151 | + calcCount(len: number, options: TypeParamsCounted, rootNode: any): number |
| 152 | + tryCatch(tryfn: CallableFunction, catchfn: CallableFunction): any |
| 153 | + PartialReadError |
| 154 | + } |
| 155 | +} |
0 commit comments