deno.land / std@0.224.0 / msgpack / encode.ts

encode.ts
View Documentation
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { concat } from "../bytes/concat.ts";
/** * Value types that can be encoded to MessagePack. */export type ValueType = | number | bigint | string | boolean | null | Uint8Array | ValueType[] | ValueMap;
/** * Value map that can be encoded to MessagePack. */export interface ValueMap { /** * Value types that can be encoded to MessagePack. */ [index: string | number]: ValueType;}
const FOUR_BITS = 16;const FIVE_BITS = 32;const SEVEN_BITS = 128;const EIGHT_BITS = 256;const FIFTEEN_BITS = 32768;const SIXTEEN_BITS = 65536;const THIRTY_ONE_BITS = 2147483648;const THIRTY_TWO_BITS = 4294967296;const SIXTY_THREE_BITS = 9223372036854775808n;const SIXTY_FOUR_BITS = 18446744073709551616n;
const encoder = new TextEncoder();
/** * Encode a value to MessagePack binary format. * * @example * ```ts * import { encode } from "https://deno.land/std@$STD_VERSION/msgpack/encode.ts"; * * const obj = { * str: "deno", * arr: [1, 2, 3], * map: { * foo: "bar" * } * } * * console.log(encode(obj)) * ``` */export function encode(object: ValueType): Uint8Array { const byteParts: Uint8Array[] = []; encodeSlice(object, byteParts); return concat(byteParts);}
function encodeFloat64(num: number) { const dataView = new DataView(new ArrayBuffer(9)); dataView.setFloat64(1, num); dataView.setUint8(0, 0xcb); return new Uint8Array(dataView.buffer);}
function encodeNumber(num: number) { if (!Number.isInteger(num)) { // float 64 return encodeFloat64(num); }
if (num < 0) { if (num >= -FIVE_BITS) { // negative fixint return new Uint8Array([num]); }
if (num >= -SEVEN_BITS) { // int 8 return new Uint8Array([0xd0, num]); }
if (num >= -FIFTEEN_BITS) { // int 16 const dataView = new DataView(new ArrayBuffer(3)); dataView.setInt16(1, num); dataView.setUint8(0, 0xd1); return new Uint8Array(dataView.buffer); }
if (num >= -THIRTY_ONE_BITS) { // int 32 const dataView = new DataView(new ArrayBuffer(5)); dataView.setInt32(1, num); dataView.setUint8(0, 0xd2); return new Uint8Array(dataView.buffer); }
// float 64 return encodeFloat64(num); }
// if the number fits within a positive fixint, use it if (num <= 0x7f) { return new Uint8Array([num]); }
if (num < EIGHT_BITS) { // uint8 return new Uint8Array([0xcc, num]); }
if (num < SIXTEEN_BITS) { // uint16 const dataView = new DataView(new ArrayBuffer(3)); dataView.setUint16(1, num); dataView.setUint8(0, 0xcd); return new Uint8Array(dataView.buffer); }
if (num < THIRTY_TWO_BITS) { // uint32 const dataView = new DataView(new ArrayBuffer(5)); dataView.setUint32(1, num); dataView.setUint8(0, 0xce); return new Uint8Array(dataView.buffer); }
// float 64 return encodeFloat64(num);}
function encodeSlice(object: ValueType, byteParts: Uint8Array[]) { if (object === null) { byteParts.push(new Uint8Array([0xc0])); return; }
if (object === false) { byteParts.push(new Uint8Array([0xc2])); return; }
if (object === true) { byteParts.push(new Uint8Array([0xc3])); return; }
if (typeof object === "number") { byteParts.push(encodeNumber(object)); return; }
if (typeof object === "bigint") { if (object < 0) { if (object < -SIXTY_THREE_BITS) { throw new Error("Cannot safely encode bigint larger than 64 bits"); }
const dataView = new DataView(new ArrayBuffer(9)); dataView.setBigInt64(1, object); dataView.setUint8(0, 0xd3); byteParts.push(new Uint8Array(dataView.buffer)); return; }
if (object >= SIXTY_FOUR_BITS) { throw new Error("Cannot safely encode bigint larger than 64 bits"); }
const dataView = new DataView(new ArrayBuffer(9)); dataView.setBigUint64(1, object); dataView.setUint8(0, 0xcf); byteParts.push(new Uint8Array(dataView.buffer)); return; }
if (typeof object === "string") { const encoded = encoder.encode(object); const len = encoded.length;
if (len < FIVE_BITS) { // fixstr byteParts.push(new Uint8Array([0xa0 | len])); } else if (len < EIGHT_BITS) { // str 8 byteParts.push(new Uint8Array([0xd9, len])); } else if (len < SIXTEEN_BITS) { // str 16 const dataView = new DataView(new ArrayBuffer(3)); dataView.setUint16(1, len); dataView.setUint8(0, 0xda); byteParts.push(new Uint8Array(dataView.buffer)); } else if (len < THIRTY_TWO_BITS) { // str 32 const dataView = new DataView(new ArrayBuffer(5)); dataView.setUint32(1, len); dataView.setUint8(0, 0xdb); byteParts.push(new Uint8Array(dataView.buffer)); } else { throw new Error( "Cannot safely encode string with size larger than 32 bits", ); } byteParts.push(encoded); return; }
if (object instanceof Uint8Array) { if (object.length < EIGHT_BITS) { // bin 8 byteParts.push(new Uint8Array([0xc4, object.length])); } else if (object.length < SIXTEEN_BITS) { // bin 16 const dataView = new DataView(new ArrayBuffer(3)); dataView.setUint16(1, object.length); dataView.setUint8(0, 0xc5); byteParts.push(new Uint8Array(dataView.buffer)); } else if (object.length < THIRTY_TWO_BITS) { // bin 32 const dataView = new DataView(new ArrayBuffer(5)); dataView.setUint32(1, object.length); dataView.setUint8(0, 0xc6); byteParts.push(new Uint8Array(dataView.buffer)); } else { throw new Error( "Cannot safely encode Uint8Array with size larger than 32 bits", ); } byteParts.push(object); return; }
if (Array.isArray(object)) { if (object.length < FOUR_BITS) { // fixarray byteParts.push(new Uint8Array([0x90 | object.length])); } else if (object.length < SIXTEEN_BITS) { // array 16 const dataView = new DataView(new ArrayBuffer(3)); dataView.setUint16(1, object.length); dataView.setUint8(0, 0xdc); byteParts.push(new Uint8Array(dataView.buffer)); } else if (object.length < THIRTY_TWO_BITS) { // array 32 const dataView = new DataView(new ArrayBuffer(5)); dataView.setUint32(1, object.length); dataView.setUint8(0, 0xdd); byteParts.push(new Uint8Array(dataView.buffer)); } else { throw new Error( "Cannot safely encode array with size larger than 32 bits", ); }
for (const obj of object) { encodeSlice(obj, byteParts); } return; }
// If object is a plain object if (Object.getPrototypeOf(object) === Object.prototype) { const numKeys = Object.keys(object).length;
if (numKeys < FOUR_BITS) { // fixarray byteParts.push(new Uint8Array([0x80 | numKeys])); } else if (numKeys < SIXTEEN_BITS) { // map 16 const dataView = new DataView(new ArrayBuffer(3)); dataView.setUint16(1, numKeys); dataView.setUint8(0, 0xde); byteParts.push(new Uint8Array(dataView.buffer)); } else if (numKeys < THIRTY_TWO_BITS) { // map 32 const dataView = new DataView(new ArrayBuffer(5)); dataView.setUint32(1, numKeys); dataView.setUint8(0, 0xdf); byteParts.push(new Uint8Array(dataView.buffer)); } else { throw new Error("Cannot safely encode map with size larger than 32 bits"); }
for (const [key, value] of Object.entries(object)) { encodeSlice(key, byteParts); encodeSlice(value, byteParts); } return; }
throw new Error("Cannot safely encode value into messagepack");}
std

Version Info

Tagged at
6 months ago