forked from sitegui/js-binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-binary.d.ts
97 lines (81 loc) · 2.54 KB
/
js-binary.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
declare module "js-binary" {
export class Data {
private _buffer: Buffer;
private _length: number;
constructor(capacity: number, buffer?: typeof Buffer);
appendBuffer(data: Buffer): void;
writeUInt8(value: number): void;
writeUInt16(value: number): void;
writeUInt32(value: number): void;
writeDouble(value: number): void;
toBuffer(): Buffer;
private _alloc(bytes: number): void;
}
export class ReadState {
constructor(buffer: Buffer);
private _buffer: Buffer;
private _offset: number;
peekUInt8(): number;
readUInt8(): number;
readUInt16(): number;
readUInt32(): number;
readDouble(): number;
readBuffer(): Buffer;
hasEnded(): boolean;
}
export interface DataType<Type> {
write(type: Type, data: Data, path: string): void;
read(state: ReadState): Type;
}
export interface types {
uint: DataType<number>;
int: DataType<number>;
float: DataType<number>;
string: DataType<string>;
Buffer: DataType<Buffer>;
boolean: DataType<boolean>;
json: DataType<any>;
oid: DataType<string>;
regex: DataType<RegExp>;
date: DataType<Date>;
}
interface ITypeDeclaration {
[key: string]: TypeDeclaration;
}
type TypeDeclaration = keyof types | [keyof types] | "{object}" | "[array]" | ITypeDeclaration | [ITypeDeclaration];
enum TYPE {
UINT = 'uint',
INT = 'int',
FLOAT = 'float',
STRING = 'string',
BUFFER = 'Buffer',
BOOLEAN = 'boolean',
JSON = 'json',
OID = 'oid',
REGEX = 'regex',
DATE = 'date',
ARRAY = '[array]',
OBJECT = '{object}'
}
export class Type {
type: TYPE;
fields?: Field[];
subType?: Type;
constructor(type: TypeDeclaration);
encode(value: any): Buffer;
decode(data: Buffer): any;
write(value: any, data: Data, path: string): void;
private _writeArray(value: any, data: Data, path: string, type: Type): void;
read(state: ReadState): void;
private _compileRead(): (state: ReadState) => void;
private _readArray(type: Type, state: ReadState): number[];
getHash(): Buffer;
}
export class Field {
optional: boolean;
name: string;
array: boolean;
type: Type;
constructor(name: string, type: TypeDeclaration);
}
}