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