-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer.ts
151 lines (136 loc) · 4.43 KB
/
integer.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
import { BufferCodec } from "../types";
export enum IntegerEncoding {
UInt8 = "UInt8",
UInt16LE = "UInt16LE",
UInt16BE = "UInt16BE",
UInt32LE = "UInt32LE",
UInt32BE = "UInt32BE",
BigUInt64LE = "BigUInt64LE",
BigUInt64BE = "BigUInt64BE",
Int8 = "Int8",
Int16LE = "Int16LE",
Int16BE = "Int16BE",
Int32LE = "Int32LE",
Int32BE = "Int32BE",
BigInt64LE = "BigInt64LE",
BigInt64BE = "BigInt64BE",
}
const byteLengths = {
[IntegerEncoding.UInt8]: 1,
[IntegerEncoding.UInt16LE]: 2,
[IntegerEncoding.UInt16BE]: 2,
[IntegerEncoding.UInt32LE]: 4,
[IntegerEncoding.UInt32BE]: 4,
[IntegerEncoding.BigUInt64LE]: 8,
[IntegerEncoding.BigUInt64BE]: 8,
[IntegerEncoding.Int8]: 1,
[IntegerEncoding.Int16LE]: 2,
[IntegerEncoding.Int16BE]: 2,
[IntegerEncoding.Int32LE]: 4,
[IntegerEncoding.Int32BE]: 4,
[IntegerEncoding.BigInt64LE]: 8,
[IntegerEncoding.BigInt64BE]: 8,
} as const;
type BigIntTypes =
| IntegerEncoding.BigUInt64LE
| IntegerEncoding.BigUInt64BE
| IntegerEncoding.BigInt64LE
| IntegerEncoding.BigInt64BE;
export const returnsBigInt = (type: IntegerEncoding): type is BigIntTypes =>
byteLengths[type] === 8;
const bufferReadFns = {
[IntegerEncoding.UInt8]: "readUInt8",
[IntegerEncoding.UInt16LE]: "readUInt16LE",
[IntegerEncoding.UInt16BE]: "readUInt16BE",
[IntegerEncoding.UInt32LE]: "readUInt32LE",
[IntegerEncoding.UInt32BE]: "readUInt32BE",
[IntegerEncoding.BigUInt64LE]: "readBigUInt64LE",
[IntegerEncoding.BigUInt64BE]: "readBigUInt64BE",
[IntegerEncoding.Int8]: "readInt8",
[IntegerEncoding.Int16LE]: "readInt16LE",
[IntegerEncoding.Int16BE]: "readInt16BE",
[IntegerEncoding.Int32LE]: "readInt32LE",
[IntegerEncoding.Int32BE]: "readInt32BE",
[IntegerEncoding.BigInt64LE]: "readBigInt64LE",
[IntegerEncoding.BigInt64BE]: "readBigInt64BE",
} as const;
const bufferWriteFns = {
[IntegerEncoding.UInt8]: "writeUInt8",
[IntegerEncoding.UInt16LE]: "writeUInt16LE",
[IntegerEncoding.UInt16BE]: "writeUInt16BE",
[IntegerEncoding.UInt32LE]: "writeUInt32LE",
[IntegerEncoding.UInt32BE]: "writeUInt32BE",
[IntegerEncoding.BigUInt64LE]: "writeBigUInt64LE",
[IntegerEncoding.BigUInt64BE]: "writeBigUInt64BE",
[IntegerEncoding.Int8]: "writeInt8",
[IntegerEncoding.Int16LE]: "writeInt16LE",
[IntegerEncoding.Int16BE]: "writeInt16BE",
[IntegerEncoding.Int32LE]: "writeInt32LE",
[IntegerEncoding.Int32BE]: "writeInt32BE",
[IntegerEncoding.BigInt64LE]: "writeBigInt64LE",
[IntegerEncoding.BigInt64BE]: "writeBigInt64BE",
} as const;
interface IntegerParser<Parsed, Length extends number> {
(buffer: Buffer): { value: Parsed; byteLength: Length };
}
interface IntegerSerializer<T> {
(value: T): Buffer;
}
export type Output<T extends IntegerEncoding> = ReturnType<
Buffer[typeof bufferReadFns[T]]
>;
type Input<Encoding extends IntegerEncoding> = Parameters<
Buffer[typeof bufferWriteFns[Encoding]]
>[0];
function integerParser<Encoding extends IntegerEncoding>(
type: Encoding
): IntegerParser<
ReturnType<Buffer[typeof bufferReadFns[Encoding]]>,
typeof byteLengths[Encoding]
> {
const readerFn: typeof bufferReadFns[Encoding] = bufferReadFns[type];
const byteLength: typeof byteLengths[Encoding] = byteLengths[type];
return ((buffer) => ({
value: buffer[readerFn](),
byteLength,
})) as IntegerParser<Output<Encoding>, typeof byteLength>;
}
function integerSerializer<Encoding extends keyof typeof bufferWriteFns>(
type: Encoding
): IntegerSerializer<Input<Encoding>> {
const writerFnName: typeof bufferWriteFns[Encoding] = bufferWriteFns[type];
const byteLength: typeof byteLengths[Encoding] = byteLengths[type];
return ((value) => {
const buf = Buffer.alloc(byteLength);
buf[writerFnName](
// @ts-ignore -- TODO: figure out
value
);
return buf;
}) as IntegerSerializer<Input<Encoding>>;
}
const integerCodec = <Encoding extends IntegerEncoding>(type: Encoding) => ({
parse: integerParser(type),
serialize: integerSerializer(type),
});
type IntegerCodecs = {
[T in IntegerEncoding]: BufferCodec<Output<T>, any>;
};
/**
* An object which maps from each `IntegerEncoding` to a codec
* that handles it.
*
* @example ```ts
* integer.UInt8.parse(Buffer.from([123]))
* // => { value: 123, byteLength: 1 }
*
* integer.UInt8.serialize(0xFF)
* // => <Buffer FF>
* ```
*
* @see {IntegerEncoding}
*/
export const integer = Object.values(IntegerEncoding).reduce(
(acc, key) => ({ ...acc, [key]: integerCodec(key) }),
{}
) as IntegerCodecs;