-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmsgpack.js
executable file
·646 lines (610 loc) · 15.6 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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* jshint browser: true */
/* global define, module */
( // Module boilerplate to support browser globals and browserify and AMD.
typeof define === "function" ? function (m) { define("msgpack-js", m); } :
typeof exports === "object" ? function (m) { module.exports = m(); } :
function(m){ this.msgpack = m(); }
)(function () {
"use strict";
var exports = {};
exports.inspect = inspect;
function inspect(buffer) {
if (buffer === undefined) return "undefined";
var view;
var type;
if (buffer instanceof ArrayBuffer) {
type = "ArrayBuffer";
view = new DataView(buffer);
}
else if (buffer instanceof DataView) {
type = "DataView";
view = buffer;
}
if (!view) return JSON.stringify(buffer);
var bytes = [];
for (var i = 0; i < buffer.byteLength; i++) {
if (i > 20) {
bytes.push("...");
break;
}
var byte = view.getUint8(i).toString(16);
if (byte.length === 1) byte = "0" + byte;
bytes.push(byte);
}
return "<" + type + " " + bytes.join(" ") + ">";
}
// Encode string as utf8 into dataview at offset
exports.utf8Write = utf8Write;
function utf8Write(view, offset, string) {
var byteLength = view.byteLength;
for(var i = 0, l = string.length; i < l; i++) {
var codePoint = string.charCodeAt(i);
// One byte of UTF-8
if (codePoint < 0x80) {
view.setUint8(offset++, codePoint >>> 0 & 0x7f | 0x00);
continue;
}
// Two bytes of UTF-8
if (codePoint < 0x800) {
view.setUint8(offset++, codePoint >>> 6 & 0x1f | 0xc0);
view.setUint8(offset++, codePoint >>> 0 & 0x3f | 0x80);
continue;
}
// Three bytes of UTF-8.
if (codePoint < 0x10000) {
view.setUint8(offset++, codePoint >>> 12 & 0x0f | 0xe0);
view.setUint8(offset++, codePoint >>> 6 & 0x3f | 0x80);
view.setUint8(offset++, codePoint >>> 0 & 0x3f | 0x80);
continue;
}
// Four bytes of UTF-8
if (codePoint < 0x110000) {
view.setUint8(offset++, codePoint >>> 18 & 0x07 | 0xf0);
view.setUint8(offset++, codePoint >>> 12 & 0x3f | 0x80);
view.setUint8(offset++, codePoint >>> 6 & 0x3f | 0x80);
view.setUint8(offset++, codePoint >>> 0 & 0x3f | 0x80);
continue;
}
throw new Error("bad codepoint " + codePoint);
}
}
exports.utf8Read = utf8Read;
function utf8Read(view, offset, length) {
var string = "";
for (var i = offset, end = offset + length; i < end; i++) {
var byte = view.getUint8(i);
// One byte character
if ((byte & 0x80) === 0x00) {
string += String.fromCharCode(byte);
continue;
}
// Two byte character
if ((byte & 0xe0) === 0xc0) {
string += String.fromCharCode(
((byte & 0x0f) << 6) |
(view.getUint8(++i) & 0x3f)
);
continue;
}
// Three byte character
if ((byte & 0xf0) === 0xe0) {
string += String.fromCharCode(
((byte & 0x0f) << 12) |
((view.getUint8(++i) & 0x3f) << 6) |
((view.getUint8(++i) & 0x3f) << 0)
);
continue;
}
// Four byte character
if ((byte & 0xf8) === 0xf0) {
string += String.fromCharCode(
((byte & 0x07) << 18) |
((view.getUint8(++i) & 0x3f) << 12) |
((view.getUint8(++i) & 0x3f) << 6) |
((view.getUint8(++i) & 0x3f) << 0)
);
continue;
}
throw new Error("Invalid byte " + byte.toString(16));
}
return string;
}
exports.utf8ByteCount = utf8ByteCount;
function utf8ByteCount(string) {
var count = 0;
for(var i = 0, l = string.length; i < l; i++) {
var codePoint = string.charCodeAt(i);
if (codePoint < 0x80) {
count += 1;
continue;
}
if (codePoint < 0x800) {
count += 2;
continue;
}
if (codePoint < 0x10000) {
count += 3;
continue;
}
if (codePoint < 0x110000) {
count += 4;
continue;
}
throw new Error("bad codepoint " + codePoint);
}
return count;
}
exports.encode = function (value) {
var buffer = new ArrayBuffer(encodedSize(value));
var view = new DataView(buffer);
encode(value, view, 0);
return buffer;
};
exports.decode = decode;
// https://github.com/msgpack/msgpack/blob/master/spec.md
// we reserve extension type 0x00 to encode javascript 'undefined'
function Decoder(view, offset) {
this.offset = offset || 0;
this.view = view;
}
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 = new ArrayBuffer(length);
(new Uint8Array(value)).set(new Uint8Array(this.view.buffer, this.offset, length), 0);
this.offset += length;
return value;
};
Decoder.prototype.str = function (length) {
var value = utf8Read(this.view, 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.view.getUint8(this.offset);
var value, length;
// FixStr
if ((type & 0xe0) === 0xa0) {
length = type & 0x1f;
this.offset++;
return this.str(length);
}
// 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);
}
// Positive FixNum
if ((type & 0x80) === 0x00) {
this.offset++;
return type;
}
// Negative Fixnum
if ((type & 0xe0) === 0xe0) {
value = this.view.getInt8(this.offset);
this.offset++;
return value;
}
// Undefined as FixExt1
if (type === 0xd4 && this.view.getUint8(this.offset + 1) === 0x00) {
this.offset += 3;
return undefined;
}
switch (type) {
// str 8
case 0xd9:
length = this.view.getUint8(this.offset + 1);
this.offset += 2;
return this.str(length);
// str 16
case 0xda:
length = this.view.getUint16(this.offset + 1);
this.offset += 3;
return this.str(length);
// str 32
case 0xdb:
length = this.view.getUint32(this.offset + 1);
this.offset += 5;
return this.str(length);
// bin 8
case 0xc4:
length = this.view.getUint8(this.offset + 1);
this.offset += 2;
return this.bin(length);
// bin 16
case 0xc5:
length = this.view.getUint16(this.offset + 1);
this.offset += 3;
return this.bin(length);
// bin 32
case 0xc6:
length = this.view.getUint32(this.offset + 1);
this.offset += 5;
return this.bin(length);
// nil
case 0xc0:
this.offset++;
return null;
// false
case 0xc2:
this.offset++;
return false;
// true
case 0xc3:
this.offset++;
return true;
// uint8
case 0xcc:
value = this.view.getUint8(this.offset + 1);
this.offset += 2;
return value;
// uint 16
case 0xcd:
value = this.view.getUint16(this.offset + 1);
this.offset += 3;
return value;
// uint 32
case 0xce:
value = this.view.getUint32(this.offset + 1);
this.offset += 5;
return value;
// uint 64
case 0xcf:
var high = this.view.getUint32(this.offset + 1);
var low = this.view.getUint32(this.offset + 5);
value = high*0x100000000 + low;
this.offset += 9;
return value;
// int 8
case 0xd0:
value = this.view.getInt8(this.offset + 1);
this.offset += 2;
return value;
// int 16
case 0xd1:
value = this.view.getInt16(this.offset + 1);
this.offset += 3;
return value;
// int 32
case 0xd2:
value = this.view.getInt32(this.offset + 1);
this.offset += 5;
return value;
// int 64
case 0xd3:
var high = this.view.getInt32(this.offset + 1);
var low = this.view.getUint32(this.offset + 5);
value = high*0x100000000 + low;
this.offset += 9;
return value;
// map 16
case 0xde:
length = this.view.getUint16(this.offset + 1);
this.offset += 3;
return this.map(length);
// map 32
case 0xdf:
length = this.view.getUint32(this.offset + 1);
this.offset += 5;
return this.map(length);
// array 16
case 0xdc:
length = this.view.getUint16(this.offset + 1);
this.offset += 3;
return this.array(length);
// array 32
case 0xdd:
length = this.view.getUint32(this.offset + 1);
this.offset += 5;
return this.array(length);
// float
case 0xca:
value = this.view.getFloat32(this.offset + 1);
this.offset += 5;
return value;
// double
case 0xcb:
value = this.view.getFloat64(this.offset + 1);
this.offset += 9;
return value;
}
throw new Error("Unknown type 0x" + type.toString(16));
};
function decode(buffer) {
var view = new DataView(buffer);
var decoder = new Decoder(view);
var value = decoder.parse();
if (decoder.offset !== buffer.byteLength) throw new Error((buffer.byteLength - decoder.offset) + " trailing bytes");
return value;
}
function encode(value, view, offset) {
var type = typeof value;
// Strings Bytes
if (type === "string") {
var length = utf8ByteCount(value);
// fix str
if (length < 0x20) {
view.setUint8(offset, length | 0xa0);
utf8Write(view, offset + 1, value);
return 1 + length;
}
// str 8
if (length < 0x100) {
view.setUint8(offset, 0xd9);
view.setUint8(offset + 1, length);
utf8Write(view, offset + 2, value);
return 2 + length;
}
// str 16
if (length < 0x10000) {
view.setUint8(offset, 0xda);
view.setUint16(offset + 1, length);
utf8Write(view, offset + 3, value);
return 3 + length;
}
// str 32
if (length < 0x100000000) {
view.setUint8(offset, 0xdb);
view.setUint32(offset + 1, length);
utf8Write(view, offset + 5, value);
return 5 + length;
}
}
if (value instanceof ArrayBuffer) {
var length = value.byteLength;
// bin 8
if (length < 0x100) {
view.setUint8(offset, 0xc4);
view.setUint8(offset + 1, length);
(new Uint8Array(view.buffer)).set(new Uint8Array(value), offset + 2);
return 2 + length;
}
// bin 16
if (length < 0x10000) {
view.setUint8(offset, 0xc5);
view.setUint16(offset + 1, length);
(new Uint8Array(view.buffer)).set(new Uint8Array(value), offset + 3);
return 3 + length;
}
// bin 32
if (length < 0x100000000) {
view.setUint8(offset, 0xc6);
view.setUint32(offset + 1, length);
(new Uint8Array(view.buffer)).set(new Uint8Array(value), offset + 5);
return 5 + length;
}
}
if (type === "number") {
// Floating Point
if ((value << 0) !== value) {
view.setUint8(offset, 0xcb);
view.setFloat64(offset + 1, value);
return 9;
}
// Integers
if (value >=0) {
// positive fixnum
if (value < 0x80) {
view.setUint8(offset, value);
return 1;
}
// uint 8
if (value < 0x100) {
view.setUint8(offset, 0xcc);
view.setUint8(offset + 1, value);
return 2;
}
// uint 16
if (value < 0x10000) {
view.setUint8(offset, 0xcd);
view.setUint16(offset + 1, value);
return 3;
}
// uint 32
if (value < 0x100000000) {
view.setUint8(offset, 0xce);
view.setUint32(offset + 1, value);
return 5;
}
throw new Error("Number too big 0x" + value.toString(16));
}
// negative fixnum
if (value >= -0x20) {
view.setInt8(offset, value);
return 1;
}
// int 8
if (value >= -0x80) {
view.setUint8(offset, 0xd0);
view.setInt8(offset + 1, value);
return 2;
}
// int 16
if (value >= -0x8000) {
view.setUint8(offset, 0xd1);
view.setInt16(offset + 1, value);
return 3;
}
// int 32
if (value >= -0x80000000) {
view.setUint8(offset, 0xd2);
view.setInt32(offset + 1, value);
return 5;
}
throw new Error("Number too small -0x" + (-value).toString(16).substr(1));
}
// undefined
if (type === "undefined") {
view.setUint8(offset, 0xd4); // fixext 1
view.setUint8(offset + 1, 0); // type (undefined)
view.setUint8(offset + 2, 0); // data (ignored)
return 3;
}
// null
if (value === null) {
view.setUint8(offset, 0xc0);
return 1;
}
// Boolean
if (type === "boolean") {
view.setUint8(offset, value ? 0xc3 : 0xc2);
return 1;
}
// Container Types
if (type === "object") {
var length, size = 0;
var isArray = Array.isArray(value);
if (isArray) {
length = value.length;
}
else {
var keys = Object.keys(value);
length = keys.length;
}
var size;
if (length < 0x10) {
view.setUint8(offset, length | (isArray ? 0x90 : 0x80));
size = 1;
}
else if (length < 0x10000) {
view.setUint8(offset, isArray ? 0xdc : 0xde);
view.setUint16(offset + 1, length);
size = 3;
}
else if (length < 0x100000000) {
view.setUint8(offset, isArray ? 0xdd : 0xdf);
view.setUint32(offset + 1, length);
size = 5;
}
if (isArray) {
for (var i = 0; i < length; i++) {
size += encode(value[i], view, offset + size);
}
}
else {
for (var i = 0; i < length; i++) {
var key = keys[i];
size += encode(key, view, offset + size);
size += encode(value[key], view, offset + size);
}
}
return size;
}
throw new Error("Unknown type " + type);
}
function encodedSize(value) {
var type = typeof value;
// Raw Bytes
if (type === "string") {
var length = utf8ByteCount(value);
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 (value instanceof ArrayBuffer) {
var length = value.byteLength;
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));
}
// undefined
if (type === "undefined") return 3;
// Boolean, null
if (type === "boolean" || value === null) return 1;
// Container Types
if (type === "object") {
var length, size = 0;
if (Array.isArray(value)) {
length = value.length;
for (var i = 0; i < length; i++) {
size += encodedSize(value[i]);
}
}
else {
var keys = Object.keys(value);
length = keys.length;
for (var i = 0; i < length; i++) {
var key = keys[i];
size += encodedSize(key) + encodedSize(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));
}
throw new Error("Unknown type " + type);
}
return exports;
});