msgpack-lite 0.1.27 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -78
- package/dist/msgpack.min.js +1 -2
- package/lib/write-type.js +5 -0
- package/package.json +26 -15
- package/.github/workflows/nodejs.yml +0 -23
- package/.github/workflows/publish.yml +0 -64
- package/Makefile +0 -43
- package/lib/benchmark-stream.js +0 -247
- package/lib/benchmark.js +0 -157
- package/test/10.encode.js +0 -218
- package/test/11.decode.js +0 -382
- package/test/12.encoder.js +0 -36
- package/test/13.decoder.js +0 -32
- package/test/14.codec.js +0 -95
- package/test/15.useraw.js +0 -92
- package/test/16.binarraybuffer.js +0 -91
- package/test/17.uint8array.js +0 -43
- package/test/18.utf8.js +0 -59
- package/test/20.roundtrip.js +0 -220
- package/test/21.ext.js +0 -85
- package/test/22.typedarray.js +0 -79
- package/test/23.extbuffer.js +0 -79
- package/test/24.int64.js +0 -76
- package/test/26.es6.js +0 -20
- package/test/27.usemap.js +0 -66
- package/test/30.stream.js +0 -167
- package/test/50.compat.js +0 -58
- package/test/61.encode-only.js +0 -26
- package/test/62.decode-only.js +0 -23
- package/test/63.module-deps.js +0 -77
- package/test/example.json +0 -52
package/test/10.encode.js
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env mocha -R spec
|
|
2
|
-
|
|
3
|
-
var assert = require("assert");
|
|
4
|
-
var msgpackJS = "../index";
|
|
5
|
-
var isBrowser = ("undefined" !== typeof window);
|
|
6
|
-
var msgpack = isBrowser && window.msgpack || require(msgpackJS);
|
|
7
|
-
var TITLE = __filename.replace(/^.*\//, "");
|
|
8
|
-
|
|
9
|
-
var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
|
|
10
|
-
|
|
11
|
-
describe(TITLE, function() {
|
|
12
|
-
describe("Buffer", function() {
|
|
13
|
-
run_tests();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
var describe_Uint8Array = HAS_UINT8ARRAY ? describe : describe.skip;
|
|
17
|
-
describe_Uint8Array("Uint8Array", function() {
|
|
18
|
-
run_tests({uint8array: true});
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
function run_tests(codecopt) {
|
|
23
|
-
var options;
|
|
24
|
-
|
|
25
|
-
if (codecopt) it(JSON.stringify(codecopt), function() {
|
|
26
|
-
var codec = msgpack.createCodec(codecopt);
|
|
27
|
-
assert.ok(codec);
|
|
28
|
-
options = {codec: codec};
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
// positive fixint -- 0x00 - 0x7f
|
|
32
|
-
it("00-7f: positive fixint", function() {
|
|
33
|
-
for (var i = 0; i <= 0x7F; i++) {
|
|
34
|
-
assert.deepEqual(toArray(msgpack.encode(i, options)), [i]);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// fixmap -- 0x80 - 0x8f
|
|
39
|
-
it("80-8f: fixmap", function() {
|
|
40
|
-
var map = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16};
|
|
41
|
-
var src = {};
|
|
42
|
-
var exp = [0x80];
|
|
43
|
-
Object.keys(map).forEach(function(key) {
|
|
44
|
-
assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
|
|
45
|
-
src[key] = map[key];
|
|
46
|
-
exp[0]++;
|
|
47
|
-
exp.push(0xa1);
|
|
48
|
-
exp.push(key.charCodeAt(0));
|
|
49
|
-
exp.push(map[key]);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// fixarray -- 0x90 - 0x9f
|
|
54
|
-
it("90-9f: fixarray", function() {
|
|
55
|
-
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
|
56
|
-
var src = [];
|
|
57
|
-
var exp = [0x90];
|
|
58
|
-
for (var i = 0; i < 16; i++) {
|
|
59
|
-
assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
|
|
60
|
-
src.push(array[i]);
|
|
61
|
-
exp[0]++;
|
|
62
|
-
exp.push(array[i]);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// fixstr -- 0xa0 - 0xbf
|
|
67
|
-
it("a0-bf: fixstr", function() {
|
|
68
|
-
assert.deepEqual(toArray(msgpack.encode("", options)), [0xa0]);
|
|
69
|
-
|
|
70
|
-
var str = "0123456789abcdefghijklmnopqrstu";
|
|
71
|
-
var exp = [0xa0];
|
|
72
|
-
for (var i = 0; i < 32; i++) {
|
|
73
|
-
var src = str.substr(0, i);
|
|
74
|
-
assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
|
|
75
|
-
exp[0]++;
|
|
76
|
-
exp.push(str.charCodeAt(i));
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// nil -- 0xc0
|
|
81
|
-
it("c0: nil (null)", function() {
|
|
82
|
-
assert.deepEqual(toArray(msgpack.encode(null, options)), [0xc0]);
|
|
83
|
-
});
|
|
84
|
-
it("c0: nil (undefined)", function() {
|
|
85
|
-
assert.deepEqual(toArray(msgpack.encode(undefined, options)), [0xc0]);
|
|
86
|
-
});
|
|
87
|
-
it("c0: nil (Function)", function() {
|
|
88
|
-
assert.deepEqual(toArray(msgpack.encode(NOP, options)), [0xc0]);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// false -- 0xc2
|
|
92
|
-
// true -- 0xc3
|
|
93
|
-
it("c2-c3: boolean", function() {
|
|
94
|
-
assert.deepEqual(toArray(msgpack.encode(false, options)), [0xc2]);
|
|
95
|
-
assert.deepEqual(toArray(msgpack.encode(true, options)), [0xc3]);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// bin 8 -- 0xc4
|
|
99
|
-
// bin 16 -- 0xc5
|
|
100
|
-
// bin 32 -- 0xc6
|
|
101
|
-
it("c4-c6: bin 8/16/32", function() {
|
|
102
|
-
this.timeout(30000);
|
|
103
|
-
var bin;
|
|
104
|
-
bin = Buffer.alloc(1);
|
|
105
|
-
bin.fill(0);
|
|
106
|
-
assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc4, 1], bin));
|
|
107
|
-
|
|
108
|
-
bin = Buffer.alloc(256);
|
|
109
|
-
bin.fill(0);
|
|
110
|
-
assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc5, 1, 0], bin));
|
|
111
|
-
|
|
112
|
-
bin = Buffer.alloc(65536);
|
|
113
|
-
bin.fill(0);
|
|
114
|
-
assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc6, 0, 1, 0, 0], bin));
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
// float 32 -- 0xca -- NOT SUPPORTED
|
|
118
|
-
// float 64 -- 0xcb
|
|
119
|
-
it("ca-cb: float 32/64", function() {
|
|
120
|
-
assert.deepEqual(toArray(msgpack.encode(0.5, options)), [0xcb, 63, 224, 0, 0, 0, 0, 0, 0]);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// uint 8 -- 0xcc
|
|
124
|
-
// uint 16 -- 0xcd
|
|
125
|
-
// uint 32 -- 0xce
|
|
126
|
-
// uint 64 -- 0xcf -- NOT SUPPORTED
|
|
127
|
-
it("cc-cf: uint 8/16/32/64", function() {
|
|
128
|
-
assert.deepEqual(toArray(msgpack.encode(0xFF, options)), [0xcc, 0xFF]);
|
|
129
|
-
assert.deepEqual(toArray(msgpack.encode(0xFFFF, options)), [0xcd, 0xFF, 0xFF]);
|
|
130
|
-
assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF, options)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// int 8 -- 0xd0
|
|
134
|
-
// int 16 -- 0xd1
|
|
135
|
-
// int 32 -- 0xd2
|
|
136
|
-
// int 64 -- 0xd3 -- NOT SUPPORTED
|
|
137
|
-
it("d0-d3: int 8/16/32/64", function() {
|
|
138
|
-
assert.deepEqual(toArray(msgpack.encode(-0x80, options)), [0xd0, 0x80]);
|
|
139
|
-
assert.deepEqual(toArray(msgpack.encode(-0x8000, options)), [0xd1, 0x80, 0x00]);
|
|
140
|
-
assert.deepEqual(toArray(msgpack.encode(-0x80000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00]);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
// str 8 -- 0xd9
|
|
144
|
-
// str 16 -- 0xda
|
|
145
|
-
// str 32 -- 0xdb
|
|
146
|
-
it("d9-db: str 8/16/32", function() {
|
|
147
|
-
this.timeout(30000);
|
|
148
|
-
var str, src = "a";
|
|
149
|
-
for (var i = 0; i < 17; i++) src += src;
|
|
150
|
-
|
|
151
|
-
str = src.substr(0, 0xFF);
|
|
152
|
-
assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xd9, 0xFF], Buffer.from(str)));
|
|
153
|
-
|
|
154
|
-
str = src.substr(0, 0x0100);
|
|
155
|
-
assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0x01, 0x00], Buffer.from(str)));
|
|
156
|
-
|
|
157
|
-
str = src.substr(0, 0xFFFF);
|
|
158
|
-
assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0xFF, 0xFF], Buffer.from(str)));
|
|
159
|
-
|
|
160
|
-
str = src.substr(0, 0x010000);
|
|
161
|
-
assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer.from(str)));
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// array 16 -- 0xdc
|
|
165
|
-
// array 32 -- 0xdd
|
|
166
|
-
it("dc-dd: array 16/32", function() {
|
|
167
|
-
this.timeout(30000);
|
|
168
|
-
var i, exp;
|
|
169
|
-
var src = new Array(256);
|
|
170
|
-
for (i = 0; i < 256; i++) src[i] = i & 0x7F;
|
|
171
|
-
exp = [0xdc, 0x01, 0x00].concat(src);
|
|
172
|
-
assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
|
|
173
|
-
|
|
174
|
-
for (i = 0; i < 8; i++) src = src.concat(src);
|
|
175
|
-
exp = [0xdd, 0x00, 0x01, 0x00, 0x00].concat(src);
|
|
176
|
-
assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// map 16 -- 0xde
|
|
180
|
-
// map 32 -- 0xdf
|
|
181
|
-
it("de-df: map 16/32", function() {
|
|
182
|
-
this.timeout(30000);
|
|
183
|
-
var i, actual;
|
|
184
|
-
var map = {};
|
|
185
|
-
for (i = 0; i < 256; i++) map[i] = i;
|
|
186
|
-
actual = msgpack.encode(map, options);
|
|
187
|
-
// check only headers because order may vary
|
|
188
|
-
assert.equal(actual[0], 0xde);
|
|
189
|
-
assert.equal(actual[1], 1);
|
|
190
|
-
assert.equal(actual[2], 0);
|
|
191
|
-
|
|
192
|
-
for (i = 256; i < 65536; i++) map[i] = i;
|
|
193
|
-
actual = msgpack.encode(map, options);
|
|
194
|
-
assert.equal(actual[0], 0xdf);
|
|
195
|
-
assert.equal(actual[1], 0);
|
|
196
|
-
assert.equal(actual[2], 1);
|
|
197
|
-
assert.equal(actual[3], 0);
|
|
198
|
-
assert.equal(actual[4], 0);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
// negative fixint -- 0xe0 - 0xff
|
|
202
|
-
it("e0-ff: negative fixint", function() {
|
|
203
|
-
for (var i = -32; i <= -1; i++) {
|
|
204
|
-
assert.deepEqual(toArray(msgpack.encode(i, options)), [i & 0xFF]);
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function toArray(buffer) {
|
|
210
|
-
return Array.prototype.slice.call(buffer);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function concat(buf) {
|
|
214
|
-
return Array.prototype.concat.apply([], Array.prototype.map.call(arguments, toArray));
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function NOP() {
|
|
218
|
-
}
|
package/test/11.decode.js
DELETED
|
@@ -1,382 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env mocha -R spec
|
|
2
|
-
|
|
3
|
-
var assert = require("assert");
|
|
4
|
-
var msgpackJS = "../index";
|
|
5
|
-
var isBrowser = ("undefined" !== typeof window);
|
|
6
|
-
var msgpack = isBrowser && window.msgpack || require(msgpackJS);
|
|
7
|
-
var TITLE = __filename.replace(/^.*\//, "");
|
|
8
|
-
|
|
9
|
-
var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
|
|
10
|
-
|
|
11
|
-
BufferBridge.concat = Buffer.concat;
|
|
12
|
-
ArrayBridge.concat = ArrayBridge_concat;
|
|
13
|
-
Uint8ArrayBridge.concat = Uint8ArrayBridge_concat;
|
|
14
|
-
|
|
15
|
-
describe(TITLE, function() {
|
|
16
|
-
describe("Buffer", function() {
|
|
17
|
-
run_tests(BufferBridge);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe("Array", function() {
|
|
21
|
-
run_tests(ArrayBridge);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
var describe_Uint8Array = HAS_UINT8ARRAY ? describe : describe.skip;
|
|
25
|
-
describe_Uint8Array("Uint8Array", function() {
|
|
26
|
-
run_tests(Uint8ArrayBridge);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
function run_tests(BUFFER) {
|
|
31
|
-
// positive fixint -- 0x00 - 0x7f
|
|
32
|
-
it("00-7f: positive fixint", function() {
|
|
33
|
-
for (var i = 0; i <= 0x7F; i++) {
|
|
34
|
-
assert.deepEqual(msgpack.decode(BUFFER([i])), i);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// fixmap -- 0x80 - 0x8f
|
|
39
|
-
it("80-8f: fixmap", function() {
|
|
40
|
-
var map = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16};
|
|
41
|
-
var src = [0x80];
|
|
42
|
-
var exp = {};
|
|
43
|
-
Object.keys(map).forEach(function(key) {
|
|
44
|
-
assert.deepEqual(msgpack.decode(BUFFER(src)), exp);
|
|
45
|
-
src[0]++;
|
|
46
|
-
src.push(0xa1);
|
|
47
|
-
src.push(key.charCodeAt(0));
|
|
48
|
-
src.push(map[key]);
|
|
49
|
-
exp[key] = map[key];
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// fixarray -- 0x90 - 0x9f
|
|
54
|
-
it("90-9f: fixarray", function() {
|
|
55
|
-
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
|
56
|
-
var src = [0x90];
|
|
57
|
-
var exp = [];
|
|
58
|
-
for (var i = 0; i < 16; i++) {
|
|
59
|
-
assert.deepEqual(msgpack.decode(BUFFER(src)), exp);
|
|
60
|
-
src[0]++;
|
|
61
|
-
src.push(array[i]);
|
|
62
|
-
exp.push(array[i]);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// fixstr -- 0xa0 - 0xbf
|
|
67
|
-
it("a0-bf: fixstr", function() {
|
|
68
|
-
var str = "0123456789abcdefghijklmnopqrstu";
|
|
69
|
-
var src = [0xa0];
|
|
70
|
-
for (var i = 0; i < 32; i++) {
|
|
71
|
-
var exp = str.substr(0, i);
|
|
72
|
-
assert.deepEqual(msgpack.decode(BUFFER(src)), exp);
|
|
73
|
-
src[0]++;
|
|
74
|
-
src.push(str.charCodeAt(i));
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// nil -- 0xc0
|
|
79
|
-
it("c0: nil", function() {
|
|
80
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xc0])), null);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
// (never used) -- 0xc1
|
|
84
|
-
it("c1: (never used)", function(done) {
|
|
85
|
-
try {
|
|
86
|
-
msgpack.decode(BUFFER([0xc1]));
|
|
87
|
-
done("should throw");
|
|
88
|
-
} catch (e) {
|
|
89
|
-
done();
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// false -- 0xc2
|
|
94
|
-
// true -- 0xc3
|
|
95
|
-
it("c2-c3: boolean", function() {
|
|
96
|
-
assert.equal(msgpack.decode(BUFFER([0xc2])), false);
|
|
97
|
-
assert.equal(msgpack.decode(BUFFER([0xc3])), true);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// bin 8 -- 0xc4
|
|
101
|
-
// bin 16 -- 0xc5
|
|
102
|
-
// bin 32 -- 0xc6
|
|
103
|
-
it("c4-c6: bin 8/16/32", function() {
|
|
104
|
-
this.timeout(30000);
|
|
105
|
-
var bin, buf, act;
|
|
106
|
-
|
|
107
|
-
bin = BUFFER(1);
|
|
108
|
-
buf = BUFFER.concat([BUFFER([0xc4, 1]), bin]);
|
|
109
|
-
act = msgpack.decode(buf);
|
|
110
|
-
assert.ok(Buffer.isBuffer(act));
|
|
111
|
-
assert.deepEqual(ArrayBridge(act), ArrayBridge(bin));
|
|
112
|
-
|
|
113
|
-
bin = BUFFER(256);
|
|
114
|
-
buf = BUFFER.concat([BUFFER([0xc5, 1, 0]), bin]);
|
|
115
|
-
act = msgpack.decode(buf);
|
|
116
|
-
assert.ok(Buffer.isBuffer(act));
|
|
117
|
-
assert.deepEqual(ArrayBridge(act), ArrayBridge(bin));
|
|
118
|
-
|
|
119
|
-
bin = BUFFER(65536);
|
|
120
|
-
buf = BUFFER.concat([BUFFER([0xc6, 0, 1, 0, 0]), bin]);
|
|
121
|
-
act = msgpack.decode(buf);
|
|
122
|
-
assert.ok(Buffer.isBuffer(act));
|
|
123
|
-
assert.deepEqual(ArrayBridge(act), ArrayBridge(bin));
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// ext 8 -- 0xc7
|
|
127
|
-
// ext 16 -- 0xc8
|
|
128
|
-
// ext 32 -- 0xc9
|
|
129
|
-
it("c7-c9: ext 8/16/32", function() {
|
|
130
|
-
this.timeout(30000);
|
|
131
|
-
var ext, buf, act;
|
|
132
|
-
|
|
133
|
-
ext = BUFFER(1);
|
|
134
|
-
buf = BUFFER.concat([BUFFER([0xc7, 1, 0]), ext]);
|
|
135
|
-
act = msgpack.decode(buf);
|
|
136
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
137
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
138
|
-
|
|
139
|
-
ext = BUFFER(256);
|
|
140
|
-
buf = BUFFER.concat([BUFFER([0xc8, 1, 0, 0]), ext]);
|
|
141
|
-
act = msgpack.decode(buf);
|
|
142
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
143
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
144
|
-
|
|
145
|
-
ext = BUFFER(65536);
|
|
146
|
-
buf = BUFFER.concat([BUFFER([0xc9, 0, 1, 0, 0, 0]), ext]);
|
|
147
|
-
act = msgpack.decode(buf);
|
|
148
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
149
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// float 32 -- 0xca
|
|
153
|
-
// float 64 -- 0xcb
|
|
154
|
-
it("ca-cb: float 32/64", function() {
|
|
155
|
-
var buf;
|
|
156
|
-
|
|
157
|
-
buf = Buffer.alloc(5);
|
|
158
|
-
buf.writeUInt8(0xCA, 0);
|
|
159
|
-
buf.writeFloatBE(0.5, 1);
|
|
160
|
-
assert.deepEqual(msgpack.decode(BUFFER(buf)), 0.5);
|
|
161
|
-
|
|
162
|
-
buf = Buffer.alloc(9);
|
|
163
|
-
buf.writeUInt8(0xCB, 0);
|
|
164
|
-
buf.writeDoubleBE(0.5, 1);
|
|
165
|
-
assert.deepEqual(msgpack.decode(BUFFER(buf)), 0.5);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
// uint 8 -- 0xcc
|
|
169
|
-
// uint 16 -- 0xcd
|
|
170
|
-
// uint 32 -- 0xce
|
|
171
|
-
// uint 64 -- 0xcf
|
|
172
|
-
it("cc-cf: uint 8/16/32/64", function() {
|
|
173
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xcc, 0x01])), 0x01);
|
|
174
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xcc, 0xFF])), 0xFF);
|
|
175
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xcd, 0x00, 0x01])), 0x0001);
|
|
176
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xcd, 0xFF, 0xFF])), 0xFFFF);
|
|
177
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xce, 0x00, 0x00, 0x00, 0x01])), 0x00000001);
|
|
178
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xce, 0x7F, 0xFF, 0xFF, 0xFF])), 0x7FFFFFFF);
|
|
179
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xce, 0xFF, 0xFF, 0xFF, 0xFF])), 0xFFFFFFFF);
|
|
180
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xce, 0x12, 0x34, 0x56, 0x78])), 0x12345678);
|
|
181
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xcf, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF])), 0x00000000FFFFFFFF);
|
|
182
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xcf, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0])), 0x0000FFFFFFFF0000);
|
|
183
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xcf, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0])), 0xFFFFFFFF00000000);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
// int 8 -- 0xd0
|
|
187
|
-
// int 16 -- 0xd1
|
|
188
|
-
// int 32 -- 0xd2
|
|
189
|
-
// int 64 -- 0xd3
|
|
190
|
-
it("d0-d3: int 8/16/32/64", function() {
|
|
191
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd0, 0x7F])), 0x7F);
|
|
192
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd0, 0x80])), -0x80);
|
|
193
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd0, 0xFF])), -1);
|
|
194
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd1, 0x7F, 0xFF])), 0x7FFF);
|
|
195
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd1, 0x80, 0x00])), -0x8000);
|
|
196
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd1, 0xFF, 0xFF])), -1);
|
|
197
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0x7F, 0xFF, 0xFF, 0xFF])), 0x7FFFFFFF);
|
|
198
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0x80, 0x00, 0x00, 0x00])), -0x80000000);
|
|
199
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0xFF, 0xFF, 0xFF, 0xFF])), -1);
|
|
200
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0x12, 0x34, 0x56, 0x78])), 0x12345678);
|
|
201
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF])), 0x00000000FFFFFFFF);
|
|
202
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0])), 0x0000FFFFFFFF0000);
|
|
203
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0x7F, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0])), 0x7FFFFFFF00000000);
|
|
204
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0x80, 0, 0, 0, 0, 0, 0, 0])), -0x8000000000000000);
|
|
205
|
-
assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])), -1);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
// fixext 1 -- 0xd4
|
|
209
|
-
// fixext 2 -- 0xd5
|
|
210
|
-
// fixext 4 -- 0xd6
|
|
211
|
-
// fixext 8 -- 0xd7
|
|
212
|
-
// fixext 16 -- 0xd8
|
|
213
|
-
it("d4-d8: fixext 1/2/4/8/16", function() {
|
|
214
|
-
var ext, buf, act;
|
|
215
|
-
|
|
216
|
-
ext = BUFFER(1);
|
|
217
|
-
buf = BUFFER.concat([BUFFER([0xd4, 0]), ext]);
|
|
218
|
-
act = msgpack.decode(buf);
|
|
219
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
220
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
221
|
-
|
|
222
|
-
ext = BUFFER(2);
|
|
223
|
-
buf = BUFFER.concat([BUFFER([0xd5, 0]), ext]);
|
|
224
|
-
act = msgpack.decode(buf);
|
|
225
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
226
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
227
|
-
|
|
228
|
-
ext = BUFFER(4);
|
|
229
|
-
buf = BUFFER.concat([BUFFER([0xd6, 0]), ext]);
|
|
230
|
-
act = msgpack.decode(buf);
|
|
231
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
232
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
233
|
-
|
|
234
|
-
ext = BUFFER(8);
|
|
235
|
-
buf = BUFFER.concat([BUFFER([0xd7, 0]), ext]);
|
|
236
|
-
act = msgpack.decode(buf);
|
|
237
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
238
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
239
|
-
|
|
240
|
-
ext = BUFFER(16);
|
|
241
|
-
buf = BUFFER.concat([BUFFER([0xd8, 0]), ext]);
|
|
242
|
-
act = msgpack.decode(buf);
|
|
243
|
-
assert.ok(Buffer.isBuffer(act.buffer));
|
|
244
|
-
assert.deepEqual(ArrayBridge(act.buffer), ArrayBridge(ext));
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
// str 8 -- 0xd9
|
|
248
|
-
// str 16 -- 0xda
|
|
249
|
-
// str 32 -- 0xdb
|
|
250
|
-
it("d9-db: str 8/16/32", function() {
|
|
251
|
-
this.timeout(30000);
|
|
252
|
-
var str, buf, src = "a";
|
|
253
|
-
for (var i = 0; i < 17; i++) src += src;
|
|
254
|
-
|
|
255
|
-
str = src.substr(0, 0xFF);
|
|
256
|
-
buf = BUFFER.concat([BUFFER([0xd9, 0xFF]), BUFFER(str)]);
|
|
257
|
-
assert.deepEqual(msgpack.decode(buf), str);
|
|
258
|
-
|
|
259
|
-
str = src.substr(0, 0x0100);
|
|
260
|
-
buf = BUFFER.concat([BUFFER([0xda, 0x01, 0x00]), BUFFER(str)]);
|
|
261
|
-
assert.deepEqual(msgpack.decode(buf), str);
|
|
262
|
-
|
|
263
|
-
str = src.substr(0, 0xFFFF);
|
|
264
|
-
buf = BUFFER.concat([BUFFER([0xda, 0xFF, 0xFF]), BUFFER(str)]);
|
|
265
|
-
assert.deepEqual(msgpack.decode(buf), str);
|
|
266
|
-
|
|
267
|
-
str = src.substr(0, 0x010000);
|
|
268
|
-
buf = BUFFER.concat([BUFFER([0xdb, 0x00, 0x01, 0x00, 0x00]), BUFFER(str)]);
|
|
269
|
-
assert.deepEqual(msgpack.decode(buf), str);
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
// array 16 -- 0xdc
|
|
273
|
-
// array 32 -- 0xdd
|
|
274
|
-
it("dc-dd: array 16/32", function() {
|
|
275
|
-
this.timeout(30000);
|
|
276
|
-
var i, src;
|
|
277
|
-
var array = new Array(256);
|
|
278
|
-
for (i = 0; i < 256; i++) array[i] = i & 0x7F;
|
|
279
|
-
src = [0xdc, 0x01, 0x00].concat(array);
|
|
280
|
-
assert.deepEqual(msgpack.decode(BUFFER(src)), array);
|
|
281
|
-
|
|
282
|
-
for (i = 0; i < 8; i++) array = array.concat(array);
|
|
283
|
-
src = [0xdd, 0x00, 0x01, 0x00, 0x00].concat(array);
|
|
284
|
-
assert.deepEqual(msgpack.decode(BUFFER(src)), array);
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
// map 16 -- 0xde
|
|
288
|
-
// map 32 -- 0xdf
|
|
289
|
-
it("de-df: map 16/32", function() {
|
|
290
|
-
this.timeout(30000);
|
|
291
|
-
var i, src, key;
|
|
292
|
-
var map = {};
|
|
293
|
-
var array = [];
|
|
294
|
-
for (i = 0; i < 256; i++) {
|
|
295
|
-
key = i.toString(16);
|
|
296
|
-
if (i < 16) key = "0" + key;
|
|
297
|
-
map[key] = i & 0x7F;
|
|
298
|
-
array.push(0xa2);
|
|
299
|
-
array.push(key.charCodeAt(0));
|
|
300
|
-
array.push(key.charCodeAt(1));
|
|
301
|
-
array.push(i & 0x7F);
|
|
302
|
-
}
|
|
303
|
-
src = [0xde, 0x01, 0x00].concat(array);
|
|
304
|
-
assert.deepEqual(msgpack.decode(BUFFER(src)), map);
|
|
305
|
-
|
|
306
|
-
for (i = 0; i < 8; i++) array = array.concat(array);
|
|
307
|
-
src = [0xdf, 0x00, 0x01, 0x00, 0x00].concat(array);
|
|
308
|
-
assert.deepEqual(msgpack.decode(BUFFER(src)), map);
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
// negative fixint -- 0xe0 - 0xff
|
|
312
|
-
it("e0-ff: negative fixint", function() {
|
|
313
|
-
for (var i = -32; i <= -1; i++) {
|
|
314
|
-
assert.deepEqual(msgpack.decode(BUFFER([i & 0xFF])), i);
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function BufferBridge(array) {
|
|
320
|
-
if ("number" === typeof array) {
|
|
321
|
-
array = init_seq(Buffer.alloc(array), array);
|
|
322
|
-
} else {
|
|
323
|
-
array = Buffer.from(array);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
return array;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
function ArrayBridge(array) {
|
|
330
|
-
if ("number" === typeof array) {
|
|
331
|
-
array = init_seq([], array);
|
|
332
|
-
} else if ("string" === typeof array) {
|
|
333
|
-
array = copy_string([], array);
|
|
334
|
-
} else if (Buffer.isBuffer(array) || (HAS_UINT8ARRAY && (array instanceof Uint8Array))) {
|
|
335
|
-
array = copy_array([], array);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return array;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function init_seq(array, length) {
|
|
342
|
-
for (var i = 0; i < length; i++) {
|
|
343
|
-
array[i] = i & 255;
|
|
344
|
-
}
|
|
345
|
-
return array;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
function copy_string(array, src) {
|
|
349
|
-
for (var i = 0; i < src.length; i++) {
|
|
350
|
-
array[i] = src.charCodeAt(i);
|
|
351
|
-
}
|
|
352
|
-
return array;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
function copy_array(array, src) {
|
|
356
|
-
for (var i = 0; i < src.length; i++) {
|
|
357
|
-
array[i] = src[i];
|
|
358
|
-
}
|
|
359
|
-
return array;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
function ArrayBridge_concat(pair) {
|
|
363
|
-
return Array.prototype.concat.apply([], pair);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function Uint8ArrayBridge(array) {
|
|
367
|
-
if ("number" === typeof array) {
|
|
368
|
-
array = init_seq(new Uint8Array(array), array);
|
|
369
|
-
} else if ("string" === typeof array) {
|
|
370
|
-
array = copy_string(new Uint8Array(array.length), array);
|
|
371
|
-
} else if (Buffer.isBuffer(array)) {
|
|
372
|
-
array = copy_array(new Uint8Array(array.length), array);
|
|
373
|
-
} else {
|
|
374
|
-
array = new Uint8Array(array);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
return array;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
function Uint8ArrayBridge_concat(pair) {
|
|
381
|
-
return Uint8ArrayBridge(ArrayBridge_concat(pair.map(ArrayBridge)));
|
|
382
|
-
}
|
package/test/12.encoder.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env mocha -R spec
|
|
2
|
-
|
|
3
|
-
var assert = require("assert");
|
|
4
|
-
var msgpackJS = "../index";
|
|
5
|
-
var isBrowser = ("undefined" !== typeof window);
|
|
6
|
-
var msgpack = isBrowser && window.msgpack || require(msgpackJS);
|
|
7
|
-
var TITLE = __filename.replace(/^.*\//, "");
|
|
8
|
-
|
|
9
|
-
var source = {"foo": "bar"};
|
|
10
|
-
var packed = toArray(msgpack.encode(source));
|
|
11
|
-
|
|
12
|
-
describe(TITLE, function() {
|
|
13
|
-
|
|
14
|
-
it("Encoder().encode(obj)", function(done) {
|
|
15
|
-
var encoder = new msgpack.Encoder();
|
|
16
|
-
encoder.on("data", function(data) {
|
|
17
|
-
assert.deepEqual(toArray(data), packed);
|
|
18
|
-
});
|
|
19
|
-
encoder.on("end", done);
|
|
20
|
-
encoder.encode(source);
|
|
21
|
-
encoder.end();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("Encoder().end(obj)", function(done) {
|
|
25
|
-
var encoder = new msgpack.Encoder();
|
|
26
|
-
encoder.on("data", function(data) {
|
|
27
|
-
assert.deepEqual(toArray(data), packed);
|
|
28
|
-
});
|
|
29
|
-
encoder.on("end", done);
|
|
30
|
-
encoder.end(source);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
function toArray(buffer) {
|
|
35
|
-
return Array.prototype.slice.call(buffer);
|
|
36
|
-
}
|
package/test/13.decoder.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env mocha -R spec
|
|
2
|
-
|
|
3
|
-
var assert = require("assert");
|
|
4
|
-
var msgpackJS = "../index";
|
|
5
|
-
var isBrowser = ("undefined" !== typeof window);
|
|
6
|
-
var msgpack = isBrowser && window.msgpack || require(msgpackJS);
|
|
7
|
-
var TITLE = __filename.replace(/^.*\//, "");
|
|
8
|
-
|
|
9
|
-
var source = {"foo": "bar"};
|
|
10
|
-
var packed = msgpack.encode(source);
|
|
11
|
-
|
|
12
|
-
describe(TITLE, function() {
|
|
13
|
-
|
|
14
|
-
it("Decoder().decode(obj)", function(done) {
|
|
15
|
-
var decoder = new msgpack.Decoder();
|
|
16
|
-
decoder.on("data", function(data) {
|
|
17
|
-
assert.deepEqual(data, source);
|
|
18
|
-
});
|
|
19
|
-
decoder.on("end", done);
|
|
20
|
-
decoder.decode(packed);
|
|
21
|
-
decoder.end();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("Decoder().end(obj)", function(done) {
|
|
25
|
-
var decoder = new msgpack.Decoder();
|
|
26
|
-
decoder.on("data", function(data) {
|
|
27
|
-
assert.deepEqual(data, source);
|
|
28
|
-
});
|
|
29
|
-
decoder.on("end", done);
|
|
30
|
-
decoder.end(packed);
|
|
31
|
-
});
|
|
32
|
-
});
|