-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.ts
161 lines (157 loc) · 3.66 KB
/
encode.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
156
157
158
159
160
161
import { OffsetArrayBuffer } from "./offset_array_buffer.ts";
import { getSize } from "./size.ts";
import { defaultStringTransformer } from "./transformer.ts";
import { DecodeRule, RelateEntries, RelateRule } from "./types.ts";
export interface EncodeOptions {
littleEndian?: boolean;
}
function _encodeEntries(
buffer: OffsetArrayBuffer,
value: Record<string, unknown>,
entries: RelateEntries,
options: EncodeOptions,
) {
for (const { name, ...rule } of entries) {
// deno-lint-ignore no-explicit-any
_encode(buffer, value[name] as any, rule, options);
}
}
/**
* @internal
*/
export function _encode(
buffer: OffsetArrayBuffer,
value: unknown,
rule: RelateRule,
options: EncodeOptions = {},
) {
const view = new DataView(buffer.raw);
switch (rule.type) {
case "f64": {
view.setFloat64(
buffer.offset,
value as number,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 8;
break;
}
case "f32": {
view.setFloat32(
buffer.offset,
value as number,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 4;
break;
}
case "i64": {
view.setBigInt64(
buffer.offset,
value as bigint,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 8;
break;
}
case "u64": {
view.setBigUint64(
buffer.offset,
value as bigint,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 8;
break;
}
case "i32": {
view.setInt32(
buffer.offset,
value as number,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 4;
break;
}
case "u32": {
view.setUint32(
buffer.offset,
value as number,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 4;
break;
}
case "i16": {
view.setInt16(
buffer.offset,
value as number,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 2;
break;
}
case "u16": {
view.setUint16(
buffer.offset,
value as number,
rule.littleEndian ?? options.littleEndian,
);
buffer.offset += 2;
break;
}
case "i8": {
view.setInt8(buffer.offset, value as number);
buffer.offset += 1;
break;
}
case "u8": {
view.setUint8(buffer.offset, value as number);
buffer.offset += 1;
break;
}
case "string": {
const transformer = rule.transformer ??
defaultStringTransformer;
const target = new Uint8Array(buffer.raw, buffer.offset, rule.size);
const bytes = transformer.encode(value as string);
target.fill(0);
target.set(bytes.slice(0, rule.size));
buffer.offset += rule.size;
break;
}
case "object": {
if (typeof value !== "object" || value === null) {
throw new TypeError("Expected object");
}
_encodeEntries(
buffer,
value as Record<string, unknown>,
rule.of,
options,
);
break;
}
case "array": {
if (!Array.isArray(value)) throw new TypeError("Expected array");
for (let i = 0; i < rule.size; i++) {
_encode(buffer, value[i], rule.of, options);
}
break;
}
}
return value;
}
export function encode<T extends RelateRule, TValue = DecodeRule<T>>(
value: TValue,
rule: T,
options: EncodeOptions = {},
): ArrayBuffer {
const buffer = new OffsetArrayBuffer(new ArrayBuffer(getSize(rule)));
_encode(
buffer,
value,
rule,
options,
);
return buffer.raw;
}