forked from chakrit/msgpack-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsgpack.js
613 lines (579 loc) · 15 KB
/
msgpack.js
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
"use strict";
var bops = require('bops');
exports.encode = function (value) {
var toJSONed = []
var size = sizeof(value)
if(size == 0)
return undefined
var buffer = bops.create(size);
encode(value, buffer, 0);
return buffer;
};
exports.decode = decode;
exports.encodeAll = function(values) {
var size = 0;
for (var i = 0; i < values.length; i++ ) {
size+= sizeof(values[i]);
}
var buffer = bops.create(size), offset = 0;
for (var i = 0; i < values.length; i++ ) {
offset += encode(values[i], buffer, offset);
}
return buffer;
}
exports.decodeAll = function(buffer) {
var result = [], decoder = new Decoder(buffer, 0);
while (decoder.offset < buffer.length) {
result.push(decoder.parse());
}
return result;
}
// https://gist.github.com/frsyuki/5432559 - v5 spec
//
// I've used one extension point from `fixext 1` to store `undefined`. On the wire this
// should translate to exactly 0xd40000
//
// +--------+--------+--------+
// | 0xd4 | 0x00 | 0x00 |
// +--------+--------+--------+
// ^ fixext | ^ value part unused (fixed to be 0)
// ^ indicates undefined value
//
function Decoder(buffer, offset) {
this.offset = offset || 0;
this.buffer = buffer;
}
Decoder.prototype.map = function (length) {
var value = {};
for (var i = 0; i < length; i++) {
var key = this.parse();
value[key] = this.parse();
}
return value;
};
Decoder.prototype.bin = function (length) {
var value = bops.subarray(this.buffer, this.offset, this.offset + length);
this.offset += length;
return value;
};
Decoder.prototype.str = function (length) {
var value = bops.to(bops.subarray(this.buffer, this.offset, this.offset + length));
this.offset += length;
return value;
};
Decoder.prototype.array = function (length) {
var value = new Array(length);
for (var i = 0; i < length; i++) {
value[i] = this.parse();
}
return value;
};
Decoder.prototype.parse = function () {
var type = this.buffer[this.offset];
var value, length, extType;
// Positive FixInt
if ((type & 0x80) === 0x00) {
this.offset++;
return type;
}
// FixMap
if ((type & 0xf0) === 0x80) {
length = type & 0x0f;
this.offset++;
return this.map(length);
}
// FixArray
if ((type & 0xf0) === 0x90) {
length = type & 0x0f;
this.offset++;
return this.array(length);
}
// FixStr
if ((type & 0xe0) === 0xa0) {
length = type & 0x1f;
this.offset++;
return this.str(length);
}
// Negative FixInt
if ((type & 0xe0) === 0xe0) {
value = bops.readInt8(this.buffer, this.offset);
this.offset++;
return value;
}
switch (type) {
// nil
case 0xc0:
this.offset++;
return null;
// 0xc1: (never used)
// false
case 0xc2:
this.offset++;
return false;
// true
case 0xc3:
this.offset++;
return true;
// bin 8
case 0xc4:
length = bops.readUInt8(this.buffer, this.offset + 1);
this.offset += 2;
return this.bin(length);
// bin 16
case 0xc5:
length = bops.readUInt16BE(this.buffer, this.offset + 1);
this.offset += 3;
return this.bin(length);
// bin 32
case 0xc6:
length = bops.readUInt32BE(this.buffer, this.offset + 1);
this.offset += 5;
return this.bin(length);
// ext 8
case 0xc7:
length = bops.readUInt8(this.buffer, this.offset + 1);
extType = bops.readUInt8(this.buffer, this.offset + 2);
this.offset += 3;
return [extType, this.bin(length)];
// ext 16
case 0xc8:
length = bops.readUInt16BE(this.buffer, this.offset + 1);
extType = bops.readUInt8(this.buffer, this.offset + 3);
this.offset += 4;
return [extType, this.bin(length)];
// ext 32
case 0xc9:
length = bops.readUInt32BE(this.buffer, this.offset + 1);
extType = bops.readUInt8(this.buffer, this.offset + 5);
this.offset += 6;
return [extType, this.bin(length)];
// float 32
case 0xca:
value = bops.readFloatBE(this.buffer, this.offset + 1);
this.offset += 5;
return value;
// float 64 / double
case 0xcb:
value = bops.readDoubleBE(this.buffer, this.offset + 1);
this.offset += 9;
return value;
// uint8
case 0xcc:
value = this.buffer[this.offset + 1];
this.offset += 2;
return value;
// uint 16
case 0xcd:
value = bops.readUInt16BE(this.buffer, this.offset + 1);
this.offset += 3;
return value;
// uint 32
case 0xce:
value = bops.readUInt32BE(this.buffer, this.offset + 1);
this.offset += 5;
return value;
// uint64
case 0xcf:
value = bops.readUInt64BE(this.buffer, this.offset + 1);
this.offset += 9;
return value;
// int 8
case 0xd0:
value = bops.readInt8(this.buffer, this.offset + 1);
this.offset += 2;
return value;
// int 16
case 0xd1:
value = bops.readInt16BE(this.buffer, this.offset + 1);
this.offset += 3;
return value;
// int 32
case 0xd2:
value = bops.readInt32BE(this.buffer, this.offset + 1);
this.offset += 5;
return value;
// int 64
case 0xd3:
value = bops.readInt64BE(this.buffer, this.offset + 1);
this.offset += 9;
return value;
// fixext 1 / undefined
case 0xd4:
extType = bops.readUInt8(this.buffer, this.offset + 1);
value = bops.readUInt8(this.buffer, this.offset + 2);
this.offset += 3;
return (extType === 0 && value === 0) ? undefined : [extType, value];
// fixext 2
case 0xd5:
extType = bops.readUInt8(this.buffer, this.offset + 1);
this.offset += 2;
return [extType, this.bin(2)];
// fixext 4
case 0xd6:
extType = bops.readUInt8(this.buffer, this.offset + 1);
this.offset += 2;
return [extType, this.bin(4)];
// fixext 8
case 0xd7:
extType = bops.readUInt8(this.buffer, this.offset + 1);
this.offset += 2;
return [extType, this.bin(8)];
// fixext 16
case 0xd8:
extType = bops.readUInt8(this.buffer, this.offset + 1);
this.offset += 2;
return [extType, this.bin(16)];
// str 8
case 0xd9:
length = bops.readUInt8(this.buffer, this.offset + 1);
this.offset += 2;
return this.str(length);
// str 16
case 0xda:
length = bops.readUInt16BE(this.buffer, this.offset + 1);
this.offset += 3;
return this.str(length);
// str 32
case 0xdb:
length = bops.readUInt32BE(this.buffer, this.offset + 1);
this.offset += 5;
return this.str(length);
// array 16
case 0xdc:
length = bops.readUInt16BE(this.buffer, this.offset + 1);
this.offset += 3;
return this.array(length);
// array 32
case 0xdd:
length = bops.readUInt32BE(this.buffer, this.offset + 1);
this.offset += 5;
return this.array(length);
// map 16:
case 0xde:
length = bops.readUInt16BE(this.buffer, this.offset + 1);
this.offset += 3;
return this.map(length);
// map 32
case 0xdf:
length = bops.readUInt32BE(this.buffer, this.offset + 1);
this.offset += 5;
return this.map(length);
// buffer 16
case 0xd8:
length = bops.readUInt16BE(this.buffer, this.offset + 1);
this.offset += 3;
return this.buf(length);
// buffer 32
case 0xd9:
length = bops.readUInt32BE(this.buffer, this.offset + 1);
this.offset += 5;
return this.buf(length);
}
throw new Error("Unknown type 0x" + type.toString(16));
};
function decode(buffer) {
var decoder = new Decoder(buffer);
var value = decoder.parse();
if (decoder.offset !== buffer.length) throw new Error((buffer.length - decoder.offset) + " trailing bytes");
return value;
}
function encodeableKeys (value) {
return Object.keys(value).filter(function (e) {
return 'function' !== typeof value[e] || !!value[e].toJSON
})
}
function encode(value, buffer, offset) {
var type = typeof value;
var length, size;
// Strings Bytes
if (type === "string") {
value = bops.from(value);
length = value.length;
// fixstr
if (length < 0x20) {
buffer[offset] = length | 0xa0;
bops.copy(value, buffer, offset + 1);
return 1 + length;
}
// str 8
if (length < 0x100) {
buffer[offset] = 0xd9;
bops.writeUInt8(buffer, length, offset + 1);
bops.copy(value, buffer, offset + 2);
return 2 + length;
}
// str 16
if (length < 0x10000) {
buffer[offset] = 0xda;
bops.writeUInt16BE(buffer, length, offset + 1);
bops.copy(value, buffer, offset + 3);
return 3 + length;
}
// str 32
if (length < 0x100000000) {
buffer[offset] = 0xdb;
bops.writeUInt32BE(buffer, length, offset + 1);
bops.copy(value, buffer, offset + 5);
return 5 + length;
}
}
if (bops.is(value)) {
length = value.length;
// bin 8
if (length < 0x100) {
buffer[offset] = 0xc4;
bops.writeUInt8(buffer, length, offset + 1);
bops.copy(value, buffer, offset + 2);
return 2 + length;
}
// bin 16
if (length < 0x10000) {
buffer[offset] = 0xc5;
bops.writeUInt16BE(buffer, length, offset + 1);
bops.copy(value, buffer, offset + 3);
return 3 + length;
}
// bin 32
if (length < 0x100000000) {
buffer[offset] = 0xc6;
bops.writeUInt32BE(buffer, length, offset + 1);
bops.copy(value, buffer, offset + 5);
return 5 + length;
}
}
if (type === "number") {
// Floating Point
if ((value << 0) !== value) {
buffer[offset] = 0xcb;
bops.writeDoubleBE(buffer, value, offset + 1);
return 9;
}
// Integers
if (value >=0) {
// positive fixnum
if (value < 0x80) {
buffer[offset] = value;
return 1;
}
// uint 8
if (value < 0x100) {
buffer[offset] = 0xcc;
buffer[offset + 1] = value;
return 2;
}
// uint 16
if (value < 0x10000) {
buffer[offset] = 0xcd;
bops.writeUInt16BE(buffer, value, offset + 1);
return 3;
}
// uint 32
if (value < 0x100000000) {
buffer[offset] = 0xce;
bops.writeUInt32BE(buffer, value, offset + 1);
return 5;
}
// uint 64
if (value < 0x10000000000000000) {
buffer[offset] = 0xcf;
bops.writeUInt64BE(buffer, value, offset + 1);
return 9;
}
throw new Error("Number too big 0x" + value.toString(16));
}
// negative fixnum
if (value >= -0x20) {
bops.writeInt8(buffer, value, offset);
return 1;
}
// int 8
if (value >= -0x80) {
buffer[offset] = 0xd0;
bops.writeInt8(buffer, value, offset + 1);
return 2;
}
// int 16
if (value >= -0x8000) {
buffer[offset] = 0xd1;
bops.writeInt16BE(buffer, value, offset + 1);
return 3;
}
// int 32
if (value >= -0x80000000) {
buffer[offset] = 0xd2;
bops.writeInt32BE(buffer, value, offset + 1);
return 5;
}
// int 64
if (value >= -0x8000000000000000) {
buffer[offset] = 0xd3;
bops.writeInt64BE(buffer, value, offset + 1);
return 9;
}
throw new Error("Number too small -0x" + value.toString(16).substr(1));
}
if (type === "undefined") {
buffer[offset] = 0xd4;
buffer[offset + 1] = 0x00; // fixext special type/value
buffer[offset + 2] = 0x00;
return 1;
}
// null
if (value === null) {
buffer[offset] = 0xc0;
return 1;
}
// Boolean
if (type === "boolean") {
buffer[offset] = value ? 0xc3 : 0xc2;
return 1;
}
if('function' === typeof value.toJSON)
return encode(value.toJSON(), buffer, offset)
// Container Types
if (type === "object") {
size = 0;
var isArray = Array.isArray(value);
if (isArray) {
length = value.length;
}
else {
var keys = encodeableKeys(value)
length = keys.length;
}
// fixarray
if (length < 0x10) {
buffer[offset] = length | (isArray ? 0x90 : 0x80);
size = 1;
}
// array 16 / map 16
else if (length < 0x10000) {
buffer[offset] = isArray ? 0xdc : 0xde;
bops.writeUInt16BE(buffer, length, offset + 1);
size = 3;
}
// array 32 / map 32
else if (length < 0x100000000) {
buffer[offset] = isArray ? 0xdd : 0xdf;
bops.writeUInt32BE(buffer, length, offset + 1);
size = 5;
}
if (isArray) {
for (var i = 0; i < length; i++) {
size += encode(value[i], buffer, offset + size);
}
}
else {
for (var i = 0; i < length; i++) {
var key = keys[i];
size += encode(key, buffer, offset + size);
size += encode(value[key], buffer, offset + size);
}
}
return size;
}
if(type === "function")
return undefined
throw new Error("Unknown type " + type);
}
function sizeof(value) {
var type = typeof value;
var length, size;
// Raw Bytes
if (type === "string") {
// TODO: this creates a throw-away buffer which is probably expensive on browsers.
length = bops.from(value).length;
if (length < 0x20) {
return 1 + length;
}
if (length < 0x100) {
return 2 + length;
}
if (length < 0x10000) {
return 3 + length;
}
if (length < 0x100000000) {
return 5 + length;
}
}
if (bops.is(value)) {
length = value.length;
if (length < 0x100) {
return 2 + length;
}
if (length < 0x10000) {
return 3 + length;
}
if (length < 0x100000000) {
return 5 + length;
}
}
if (type === "number") {
// Floating Point
// double
if (value << 0 !== value) return 9;
// Integers
if (value >=0) {
// positive fixnum
if (value < 0x80) return 1;
// uint 8
if (value < 0x100) return 2;
// uint 16
if (value < 0x10000) return 3;
// uint 32
if (value < 0x100000000) return 5;
// uint 64
if (value < 0x10000000000000000) return 9;
throw new Error("Number too big 0x" + value.toString(16));
}
// negative fixnum
if (value >= -0x20) return 1;
// int 8
if (value >= -0x80) return 2;
// int 16
if (value >= -0x8000) return 3;
// int 32
if (value >= -0x80000000) return 5;
// int 64
if (value >= -0x8000000000000000) return 9;
throw new Error("Number too small -0x" + value.toString(16).substr(1));
}
// Boolean, null
if (type === "boolean" || value === null) return 1;
if (type === 'undefined') return 3;
if('function' === typeof value.toJSON)
return sizeof(value.toJSON())
// Container Types
if (type === "object") {
if('function' === typeof value.toJSON)
value = value.toJSON()
size = 0;
if (Array.isArray(value)) {
length = value.length;
for (var i = 0; i < length; i++) {
size += sizeof(value[i]);
}
}
else {
var keys = encodeableKeys(value)
length = keys.length;
for (var i = 0; i < length; i++) {
var key = keys[i];
size += sizeof(key) + sizeof(value[key]);
}
}
if (length < 0x10) {
return 1 + size;
}
if (length < 0x10000) {
return 3 + size;
}
if (length < 0x100000000) {
return 5 + size;
}
throw new Error("Array or object too long 0x" + length.toString(16));
}
if(type === "function")
return 0
throw new Error("Unknown type " + type);
}