Repository navigation
Expand file tree
/
Copy pathmsgpack.ts
More file actions
226 lines (206 loc) · 6.76 KB
/
Copy pathmsgpack.ts
File metadata and controls
226 lines (206 loc) · 6.76 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Minimal msgpack encoder/decoder.
// Supports: arrays, unsigned integers, strings, booleans, binary data.
import {
encodeWtf8,
Wtf8Decoder,
} from "./wtf8.ts";
// ── MessagePack format constants ────────────────────────────────────
export const MSGPACK_FIXARRAY3 = 0x93; // 3-element fixarray
export const MSGPACK_BIN8 = 0xc4;
export const MSGPACK_BIN16 = 0xc5;
export const MSGPACK_BIN32 = 0xc6;
export const MSGPACK_UINT8 = 0xcc;
// ── Bin header helpers ──────────────────────────────────────────────
/** Compute the MessagePack bin header size for a given data length. */
export function binHeaderSize(len: number): number {
if (len < 0x100) return 2; // BIN8: marker + 1-byte size
if (len < 0x10000) return 3; // BIN16: marker + 2-byte size
return 5; // BIN32: marker + 4-byte size
}
/** Write a MessagePack bin header into `buf` at `off`, return new offset. */
export function writeBinHeader(buf: Uint8Array, off: number, len: number): number {
if (len < 0x100) {
buf[off++] = MSGPACK_BIN8;
buf[off++] = len;
}
else if (len < 0x10000) {
buf[off++] = MSGPACK_BIN16;
buf[off++] = (len >>> 8) & 0xff;
buf[off++] = len & 0xff;
}
else {
buf[off++] = MSGPACK_BIN32;
buf[off++] = (len >>> 24) & 0xff;
buf[off++] = (len >>> 16) & 0xff;
buf[off++] = (len >>> 8) & 0xff;
buf[off++] = len & 0xff;
}
return off;
}
const decoder = new Wtf8Decoder();
export class MsgpackWriter {
private buf: Uint8Array;
private view: DataView;
private pos: number;
constructor(initialSize = 256) {
this.buf = new Uint8Array(initialSize);
this.view = new DataView(this.buf.buffer);
this.pos = 0;
}
private ensure(n: number): void {
if (this.pos + n > this.buf.length) {
let newSize = this.buf.length * 2;
while (newSize < this.pos + n) newSize *= 2;
const next = new Uint8Array(newSize);
next.set(this.buf);
this.buf = next;
this.view = new DataView(this.buf.buffer);
}
}
writeArrayHeader(length: number): void {
if (length <= 0x0f) {
this.ensure(1);
this.buf[this.pos++] = 0x90 | length;
}
else if (length <= 0xffff) {
this.ensure(3);
this.buf[this.pos++] = 0xdc;
this.view.setUint16(this.pos, length, false);
this.pos += 2;
}
else {
this.ensure(5);
this.buf[this.pos++] = 0xdd;
this.view.setUint32(this.pos, length, false);
this.pos += 4;
}
}
writeUint(value: number): void {
if (value <= 0x7f) {
this.ensure(1);
this.buf[this.pos++] = value;
}
else if (value <= 0xff) {
this.ensure(2);
this.buf[this.pos++] = 0xcc;
this.buf[this.pos++] = value;
}
else if (value <= 0xffff) {
this.ensure(3);
this.buf[this.pos++] = 0xcd;
this.view.setUint16(this.pos, value, false);
this.pos += 2;
}
else {
this.ensure(5);
this.buf[this.pos++] = 0xce;
this.view.setUint32(this.pos, value, false);
this.pos += 4;
}
}
writeString(str: string): void {
const encoded = encodeWtf8(str);
const len = encoded.length;
if (len <= 0x1f) {
this.ensure(1 + len);
this.buf[this.pos++] = 0xa0 | len;
}
else if (len <= 0xff) {
this.ensure(2 + len);
this.buf[this.pos++] = 0xd9;
this.buf[this.pos++] = len;
}
else if (len <= 0xffff) {
this.ensure(3 + len);
this.buf[this.pos++] = 0xda;
this.view.setUint16(this.pos, len, false);
this.pos += 2;
}
else {
this.ensure(5 + len);
this.buf[this.pos++] = 0xdb;
this.view.setUint32(this.pos, len, false);
this.pos += 4;
}
this.buf.set(encoded, this.pos);
this.pos += len;
}
writeBool(value: boolean): void {
this.ensure(1);
this.buf[this.pos++] = value ? 0xc3 : 0xc2;
}
finish(): Uint8Array {
return this.buf.subarray(0, this.pos);
}
}
export class MsgpackReader {
private buf: Uint8Array;
private view: DataView;
private pos: number;
constructor(data: Uint8Array, offset = 0) {
this.buf = data;
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
this.pos = offset;
}
readArrayHeader(): number {
const byte = this.buf[this.pos++];
if ((byte & 0xf0) === 0x90) return byte & 0x0f;
if (byte === 0xdc) {
const len = this.view.getUint16(this.pos, false);
this.pos += 2;
return len;
}
if (byte === 0xdd) {
const len = this.view.getUint32(this.pos, false);
this.pos += 4;
return len;
}
throw new Error(`Expected array header, got 0x${byte.toString(16)}`);
}
readUint(): number {
const byte = this.buf[this.pos++];
if (byte <= 0x7f) return byte;
if (byte === 0xcc) return this.buf[this.pos++];
if (byte === 0xcd) {
const val = this.view.getUint16(this.pos, false);
this.pos += 2;
return val;
}
if (byte === 0xce) {
const val = this.view.getUint32(this.pos, false);
this.pos += 4;
return val;
}
throw new Error(`Expected uint, got 0x${byte.toString(16)}`);
}
readString(): string {
const byte = this.buf[this.pos++];
let len: number;
if ((byte & 0xe0) === 0xa0) {
len = byte & 0x1f;
}
else if (byte === 0xd9) {
len = this.buf[this.pos++];
}
else if (byte === 0xda) {
len = this.view.getUint16(this.pos, false);
this.pos += 2;
}
else if (byte === 0xdb) {
len = this.view.getUint32(this.pos, false);
this.pos += 4;
}
else {
throw new Error(`Expected string, got 0x${byte.toString(16)}`);
}
const str = decoder.decode(this.buf.subarray(this.pos, this.pos + len));
this.pos += len;
return str;
}
readBool(): boolean {
const byte = this.buf[this.pos++];
if (byte === 0xc3) return true;
if (byte === 0xc2) return false;
throw new Error(`Expected bool, got 0x${byte.toString(16)}`);
}
}