msgpack-lite 0.1.20 → 0.1.27

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.
Files changed (55) hide show
  1. package/.github/workflows/nodejs.yml +23 -0
  2. package/.github/workflows/publish.yml +64 -0
  3. package/LICENSE +1 -1
  4. package/Makefile +8 -13
  5. package/README.md +28 -91
  6. package/dist/msgpack.min.js +2 -2
  7. package/lib/buffer-global.js +11 -0
  8. package/lib/buffer-lite.js +88 -60
  9. package/lib/bufferish-array.js +41 -0
  10. package/lib/bufferish-buffer.js +46 -0
  11. package/lib/bufferish-proto.js +86 -0
  12. package/lib/bufferish-uint8array.js +51 -0
  13. package/lib/bufferish.js +108 -0
  14. package/lib/codec-base.js +67 -0
  15. package/lib/codec.js +8 -1
  16. package/lib/decode-buffer.js +6 -50
  17. package/lib/decode-stream.js +7 -3
  18. package/lib/encode-buffer.js +6 -79
  19. package/lib/encode-stream.js +12 -4
  20. package/lib/ext-buffer.js +3 -1
  21. package/lib/ext-packer.js +78 -0
  22. package/lib/ext-unpacker.js +81 -0
  23. package/lib/ext.js +4 -77
  24. package/lib/flex-buffer.js +194 -0
  25. package/lib/read-core.js +36 -2
  26. package/lib/read-format.js +83 -56
  27. package/lib/read-token.js +1 -1
  28. package/lib/write-core.js +55 -2
  29. package/lib/write-token.js +54 -26
  30. package/lib/write-type.js +81 -73
  31. package/lib/write-uint8.js +2 -2
  32. package/package.json +22 -12
  33. package/test/10.encode.js +53 -33
  34. package/test/11.decode.js +16 -3
  35. package/test/14.codec.js +10 -3
  36. package/test/15.useraw.js +4 -4
  37. package/test/16.binarraybuffer.js +91 -0
  38. package/test/17.uint8array.js +43 -0
  39. package/test/18.utf8.js +59 -0
  40. package/test/20.roundtrip.js +54 -34
  41. package/test/23.extbuffer.js +46 -4
  42. package/test/27.usemap.js +66 -0
  43. package/test/30.stream.js +49 -1
  44. package/test/50.compat.js +6 -6
  45. package/test/61.encode-only.js +26 -0
  46. package/test/62.decode-only.js +23 -0
  47. package/test/63.module-deps.js +77 -0
  48. package/.npmignore +0 -7
  49. package/.travis.yml +0 -25
  50. package/.zuul.yml +0 -20
  51. package/bower.json +0 -34
  52. package/lib/buffer-shortage.js +0 -8
  53. package/lib/ext-preset.js +0 -188
  54. package/test/60.buffer-lite.js +0 -98
  55. package/test/zuul/ie.html +0 -4
@@ -8,7 +8,7 @@ for (var i = 0x00; i <= 0xFF; i++) {
8
8
 
9
9
  function write0(type) {
10
10
  return function(encoder) {
11
- encoder.reserve(1);
12
- encoder.buffer[encoder.offset++] = type;
11
+ var offset = encoder.reserve(1);
12
+ encoder.buffer[offset] = type;
13
13
  };
14
14
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpack-lite",
3
3
  "description": "Fast Pure JavaScript MessagePack Encoder and Decoder",
4
- "version": "0.1.20",
4
+ "version": "0.1.27",
5
5
  "author": "@kawanet",
6
6
  "bin": {
7
7
  "msgpack": "./bin/msgpack"
@@ -11,26 +11,29 @@
11
11
  "url": "https://github.com/kawanet/msgpack-lite/issues"
12
12
  },
13
13
  "contributors": [
14
+ "Christopher Vermilion <chris@figlyinc.com>",
14
15
  "Frederik Dudzik <4004blog@gmail.com>",
15
16
  "Garrett Serack <gserack@gmail.com>",
16
17
  "Jesse Armand <jesse@jessearmand.com>",
17
- "Joshua Wise <josh@joshuawise.ninja>"
18
+ "Joshua Mouch <josh@mouch.name>",
19
+ "Joshua Wise <josh@joshuawise.ninja>",
20
+ "Maciej Hirsz <maciej.hirsz@gmail.com>",
21
+ "Rafał Michalski <royaltm75@gmail.com>"
18
22
  ],
19
23
  "dependencies": {
20
24
  "event-lite": "^0.1.1",
21
- "ieee754": "^1.1.6",
22
- "int64-buffer": "^0.1.6",
25
+ "ieee754": "^1.1.8",
26
+ "int64-buffer": "^0.1.9",
23
27
  "isarray": "^1.0.0"
24
28
  },
25
29
  "devDependencies": {
26
- "async": "^1.5.2",
27
- "browserify": "^13.0.0",
28
- "concat-stream": "^1.5.1",
29
- "jshint": "^2.9.2",
30
- "mocha": "^2.4.5",
30
+ "async": "^2.1.1",
31
+ "browserify": "^13.1.0",
32
+ "concat-stream": "^1.5.2",
33
+ "jshint": "^2.9.3",
34
+ "mocha": "^3.1.2",
31
35
  "msgpack.codec": "git+https://github.com/kawanet/msgpack-javascript.git#msgpack.codec",
32
- "uglify-js": "^2.6.2",
33
- "zuul": "^3.10.1"
36
+ "uglify-js": "^2.7.3"
34
37
  },
35
38
  "homepage": "https://github.com/kawanet/msgpack-lite",
36
39
  "jshintConfig": {
@@ -38,6 +41,7 @@
38
41
  "globals": {
39
42
  "JSON": true,
40
43
  "Symbol": true,
44
+ "Map": true,
41
45
  "window": true
42
46
  },
43
47
  "mocha": true,
@@ -45,6 +49,7 @@
45
49
  "undef": true
46
50
  },
47
51
  "keywords": [
52
+ "arraybuffer",
48
53
  "buffer",
49
54
  "fluentd",
50
55
  "messagepack",
@@ -52,7 +57,6 @@
52
57
  "serialize",
53
58
  "stream",
54
59
  "typedarray",
55
- "arraybuffer",
56
60
  "uint8array"
57
61
  ],
58
62
  "license": "MIT",
@@ -62,6 +66,12 @@
62
66
  "url": "https://github.com/kawanet/msgpack-lite.git"
63
67
  },
64
68
  "scripts": {
69
+ "benchmark": "./lib/benchmark.js",
70
+ "benchmark-lite": "./lib/benchmark.js msgpack-lite",
71
+ "benchmark-stream": "./lib/benchmark-stream.js",
72
+ "fixpack": "fixpack",
73
+ "make": "make",
74
+ "size": "make clean dist/msgpack.min.js && gzip -9fkv dist/msgpack.min.js && ls -l dist",
65
75
  "test": "make test"
66
76
  }
67
77
  }
package/test/10.encode.js CHANGED
@@ -6,12 +6,32 @@ var isBrowser = ("undefined" !== typeof window);
6
6
  var msgpack = isBrowser && window.msgpack || require(msgpackJS);
7
7
  var TITLE = __filename.replace(/^.*\//, "");
8
8
 
9
+ var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
10
+
9
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
+ });
10
30
 
11
31
  // positive fixint -- 0x00 - 0x7f
12
32
  it("00-7f: positive fixint", function() {
13
33
  for (var i = 0; i <= 0x7F; i++) {
14
- assert.deepEqual(toArray(msgpack.encode(i)), [i]);
34
+ assert.deepEqual(toArray(msgpack.encode(i, options)), [i]);
15
35
  }
16
36
  });
17
37
 
@@ -21,7 +41,7 @@ describe(TITLE, function() {
21
41
  var src = {};
22
42
  var exp = [0x80];
23
43
  Object.keys(map).forEach(function(key) {
24
- assert.deepEqual(toArray(msgpack.encode(src)), exp);
44
+ assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
25
45
  src[key] = map[key];
26
46
  exp[0]++;
27
47
  exp.push(0xa1);
@@ -36,7 +56,7 @@ describe(TITLE, function() {
36
56
  var src = [];
37
57
  var exp = [0x90];
38
58
  for (var i = 0; i < 16; i++) {
39
- assert.deepEqual(toArray(msgpack.encode(src)), exp);
59
+ assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
40
60
  src.push(array[i]);
41
61
  exp[0]++;
42
62
  exp.push(array[i]);
@@ -45,13 +65,13 @@ describe(TITLE, function() {
45
65
 
46
66
  // fixstr -- 0xa0 - 0xbf
47
67
  it("a0-bf: fixstr", function() {
48
- assert.deepEqual(toArray(msgpack.encode("")), [0xa0]);
68
+ assert.deepEqual(toArray(msgpack.encode("", options)), [0xa0]);
49
69
 
50
70
  var str = "0123456789abcdefghijklmnopqrstu";
51
71
  var exp = [0xa0];
52
72
  for (var i = 0; i < 32; i++) {
53
73
  var src = str.substr(0, i);
54
- assert.deepEqual(toArray(msgpack.encode(src)), exp);
74
+ assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
55
75
  exp[0]++;
56
76
  exp.push(str.charCodeAt(i));
57
77
  }
@@ -59,20 +79,20 @@ describe(TITLE, function() {
59
79
 
60
80
  // nil -- 0xc0
61
81
  it("c0: nil (null)", function() {
62
- assert.deepEqual(toArray(msgpack.encode(null)), [0xc0]);
82
+ assert.deepEqual(toArray(msgpack.encode(null, options)), [0xc0]);
63
83
  });
64
84
  it("c0: nil (undefined)", function() {
65
- assert.deepEqual(toArray(msgpack.encode(undefined)), [0xc0]);
85
+ assert.deepEqual(toArray(msgpack.encode(undefined, options)), [0xc0]);
66
86
  });
67
87
  it("c0: nil (Function)", function() {
68
- assert.deepEqual(toArray(msgpack.encode(NOP)), [0xc0]);
88
+ assert.deepEqual(toArray(msgpack.encode(NOP, options)), [0xc0]);
69
89
  });
70
90
 
71
91
  // false -- 0xc2
72
92
  // true -- 0xc3
73
93
  it("c2-c3: boolean", function() {
74
- assert.deepEqual(toArray(msgpack.encode(false)), [0xc2]);
75
- assert.deepEqual(toArray(msgpack.encode(true)), [0xc3]);
94
+ assert.deepEqual(toArray(msgpack.encode(false, options)), [0xc2]);
95
+ assert.deepEqual(toArray(msgpack.encode(true, options)), [0xc3]);
76
96
  });
77
97
 
78
98
  // bin 8 -- 0xc4
@@ -81,23 +101,23 @@ describe(TITLE, function() {
81
101
  it("c4-c6: bin 8/16/32", function() {
82
102
  this.timeout(30000);
83
103
  var bin;
84
- bin = Buffer(1);
104
+ bin = Buffer.alloc(1);
85
105
  bin.fill(0);
86
- assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc4, 1], bin));
106
+ assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc4, 1], bin));
87
107
 
88
- bin = Buffer(256);
108
+ bin = Buffer.alloc(256);
89
109
  bin.fill(0);
90
- assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc5, 1, 0], bin));
110
+ assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc5, 1, 0], bin));
91
111
 
92
- bin = Buffer(65536);
112
+ bin = Buffer.alloc(65536);
93
113
  bin.fill(0);
94
- assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc6, 0, 1, 0, 0], bin));
114
+ assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc6, 0, 1, 0, 0], bin));
95
115
  });
96
116
 
97
117
  // float 32 -- 0xca -- NOT SUPPORTED
98
118
  // float 64 -- 0xcb
99
119
  it("ca-cb: float 32/64", function() {
100
- assert.deepEqual(toArray(msgpack.encode(0.5)), [0xcb, 63, 224, 0, 0, 0, 0, 0, 0]);
120
+ assert.deepEqual(toArray(msgpack.encode(0.5, options)), [0xcb, 63, 224, 0, 0, 0, 0, 0, 0]);
101
121
  });
102
122
 
103
123
  // uint 8 -- 0xcc
@@ -105,9 +125,9 @@ describe(TITLE, function() {
105
125
  // uint 32 -- 0xce
106
126
  // uint 64 -- 0xcf -- NOT SUPPORTED
107
127
  it("cc-cf: uint 8/16/32/64", function() {
108
- assert.deepEqual(toArray(msgpack.encode(0xFF)), [0xcc, 0xFF]);
109
- assert.deepEqual(toArray(msgpack.encode(0xFFFF)), [0xcd, 0xFF, 0xFF]);
110
- assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]);
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]);
111
131
  });
112
132
 
113
133
  // int 8 -- 0xd0
@@ -115,9 +135,9 @@ describe(TITLE, function() {
115
135
  // int 32 -- 0xd2
116
136
  // int 64 -- 0xd3 -- NOT SUPPORTED
117
137
  it("d0-d3: int 8/16/32/64", function() {
118
- assert.deepEqual(toArray(msgpack.encode(-0x80)), [0xd0, 0x80]);
119
- assert.deepEqual(toArray(msgpack.encode(-0x8000)), [0xd1, 0x80, 0x00]);
120
- assert.deepEqual(toArray(msgpack.encode(-0x80000000)), [0xd2, 0x80, 0x00, 0x00, 0x00]);
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]);
121
141
  });
122
142
 
123
143
  // str 8 -- 0xd9
@@ -129,16 +149,16 @@ describe(TITLE, function() {
129
149
  for (var i = 0; i < 17; i++) src += src;
130
150
 
131
151
  str = src.substr(0, 0xFF);
132
- assert.deepEqual(toArray(msgpack.encode(str)), concat([0xd9, 0xFF], Buffer(str)));
152
+ assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xd9, 0xFF], Buffer.from(str)));
133
153
 
134
154
  str = src.substr(0, 0x0100);
135
- assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0x01, 0x00], Buffer(str)));
155
+ assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0x01, 0x00], Buffer.from(str)));
136
156
 
137
157
  str = src.substr(0, 0xFFFF);
138
- assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0xFF, 0xFF], Buffer(str)));
158
+ assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0xFF, 0xFF], Buffer.from(str)));
139
159
 
140
160
  str = src.substr(0, 0x010000);
141
- assert.deepEqual(toArray(msgpack.encode(str)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer(str)));
161
+ assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer.from(str)));
142
162
  });
143
163
 
144
164
  // array 16 -- 0xdc
@@ -149,11 +169,11 @@ describe(TITLE, function() {
149
169
  var src = new Array(256);
150
170
  for (i = 0; i < 256; i++) src[i] = i & 0x7F;
151
171
  exp = [0xdc, 0x01, 0x00].concat(src);
152
- assert.deepEqual(toArray(msgpack.encode(src)), exp);
172
+ assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
153
173
 
154
174
  for (i = 0; i < 8; i++) src = src.concat(src);
155
175
  exp = [0xdd, 0x00, 0x01, 0x00, 0x00].concat(src);
156
- assert.deepEqual(toArray(msgpack.encode(src)), exp);
176
+ assert.deepEqual(toArray(msgpack.encode(src, options)), exp);
157
177
  });
158
178
 
159
179
  // map 16 -- 0xde
@@ -163,14 +183,14 @@ describe(TITLE, function() {
163
183
  var i, actual;
164
184
  var map = {};
165
185
  for (i = 0; i < 256; i++) map[i] = i;
166
- actual = msgpack.encode(map);
186
+ actual = msgpack.encode(map, options);
167
187
  // check only headers because order may vary
168
188
  assert.equal(actual[0], 0xde);
169
189
  assert.equal(actual[1], 1);
170
190
  assert.equal(actual[2], 0);
171
191
 
172
192
  for (i = 256; i < 65536; i++) map[i] = i;
173
- actual = msgpack.encode(map);
193
+ actual = msgpack.encode(map, options);
174
194
  assert.equal(actual[0], 0xdf);
175
195
  assert.equal(actual[1], 0);
176
196
  assert.equal(actual[2], 1);
@@ -181,10 +201,10 @@ describe(TITLE, function() {
181
201
  // negative fixint -- 0xe0 - 0xff
182
202
  it("e0-ff: negative fixint", function() {
183
203
  for (var i = -32; i <= -1; i++) {
184
- assert.deepEqual(toArray(msgpack.encode(i)), [i & 0xFF]);
204
+ assert.deepEqual(toArray(msgpack.encode(i, options)), [i & 0xFF]);
185
205
  }
186
206
  });
187
- });
207
+ }
188
208
 
189
209
  function toArray(buffer) {
190
210
  return Array.prototype.slice.call(buffer);
package/test/11.decode.js CHANGED
@@ -8,12 +8,13 @@ var TITLE = __filename.replace(/^.*\//, "");
8
8
 
9
9
  var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
10
10
 
11
+ BufferBridge.concat = Buffer.concat;
11
12
  ArrayBridge.concat = ArrayBridge_concat;
12
13
  Uint8ArrayBridge.concat = Uint8ArrayBridge_concat;
13
14
 
14
15
  describe(TITLE, function() {
15
16
  describe("Buffer", function() {
16
- run_tests(Buffer);
17
+ run_tests(BufferBridge);
17
18
  });
18
19
 
19
20
  describe("Array", function() {
@@ -153,12 +154,12 @@ function run_tests(BUFFER) {
153
154
  it("ca-cb: float 32/64", function() {
154
155
  var buf;
155
156
 
156
- buf = Buffer(5);
157
+ buf = Buffer.alloc(5);
157
158
  buf.writeUInt8(0xCA, 0);
158
159
  buf.writeFloatBE(0.5, 1);
159
160
  assert.deepEqual(msgpack.decode(BUFFER(buf)), 0.5);
160
161
 
161
- buf = Buffer(9);
162
+ buf = Buffer.alloc(9);
162
163
  buf.writeUInt8(0xCB, 0);
163
164
  buf.writeDoubleBE(0.5, 1);
164
165
  assert.deepEqual(msgpack.decode(BUFFER(buf)), 0.5);
@@ -176,6 +177,7 @@ function run_tests(BUFFER) {
176
177
  assert.deepEqual(msgpack.decode(BUFFER([0xce, 0x00, 0x00, 0x00, 0x01])), 0x00000001);
177
178
  assert.deepEqual(msgpack.decode(BUFFER([0xce, 0x7F, 0xFF, 0xFF, 0xFF])), 0x7FFFFFFF);
178
179
  assert.deepEqual(msgpack.decode(BUFFER([0xce, 0xFF, 0xFF, 0xFF, 0xFF])), 0xFFFFFFFF);
180
+ assert.deepEqual(msgpack.decode(BUFFER([0xce, 0x12, 0x34, 0x56, 0x78])), 0x12345678);
179
181
  assert.deepEqual(msgpack.decode(BUFFER([0xcf, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF])), 0x00000000FFFFFFFF);
180
182
  assert.deepEqual(msgpack.decode(BUFFER([0xcf, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0])), 0x0000FFFFFFFF0000);
181
183
  assert.deepEqual(msgpack.decode(BUFFER([0xcf, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0])), 0xFFFFFFFF00000000);
@@ -195,6 +197,7 @@ function run_tests(BUFFER) {
195
197
  assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0x7F, 0xFF, 0xFF, 0xFF])), 0x7FFFFFFF);
196
198
  assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0x80, 0x00, 0x00, 0x00])), -0x80000000);
197
199
  assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0xFF, 0xFF, 0xFF, 0xFF])), -1);
200
+ assert.deepEqual(msgpack.decode(BUFFER([0xd2, 0x12, 0x34, 0x56, 0x78])), 0x12345678);
198
201
  assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF])), 0x00000000FFFFFFFF);
199
202
  assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0])), 0x0000FFFFFFFF0000);
200
203
  assert.deepEqual(msgpack.decode(BUFFER([0xd3, 0x7F, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0])), 0x7FFFFFFF00000000);
@@ -313,6 +316,16 @@ function run_tests(BUFFER) {
313
316
  });
314
317
  }
315
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
+
316
329
  function ArrayBridge(array) {
317
330
  if ("number" === typeof array) {
318
331
  array = init_seq([], array);
package/test/14.codec.js CHANGED
@@ -6,6 +6,8 @@ var isBrowser = ("undefined" !== typeof window);
6
6
  var msgpack = isBrowser && window.msgpack || require(msgpackJS);
7
7
  var TITLE = __filename.replace(/^.*\//, "");
8
8
 
9
+ var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
10
+
9
11
  describe(TITLE, function() {
10
12
  it("createCodec()", function() {
11
13
  var codec = msgpack.createCodec();
@@ -19,12 +21,12 @@ describe(TITLE, function() {
19
21
 
20
22
  function test(type) {
21
23
  // fixext 1 -- 0xd4
22
- var source = new Buffer([0xd4, type, type]);
24
+ var source = Buffer.from([0xd4, type, type]);
23
25
  var decoded = msgpack.decode(source, options);
24
26
  assert.equal(decoded.type, type);
25
27
  assert.equal(decoded.buffer.length, 1);
26
28
  var encoded = msgpack.encode(decoded, options);
27
- assert.deepEqual(encoded, source);
29
+ assert.deepEqual(toArray(encoded), toArray(source));
28
30
  }
29
31
  });
30
32
 
@@ -80,9 +82,14 @@ function MyClass(value) {
80
82
  }
81
83
 
82
84
  function myClassPacker(obj) {
83
- return new Buffer([obj.value]);
85
+ return Buffer.from([obj.value]);
84
86
  }
85
87
 
86
88
  function myClassUnpacker(buffer) {
87
89
  return new MyClass(buffer[0]);
88
90
  }
91
+
92
+ function toArray(array) {
93
+ if (HAS_UINT8ARRAY && array instanceof ArrayBuffer) array = new Uint8Array(array);
94
+ return Array.prototype.slice.call(array);
95
+ }
package/test/15.useraw.js CHANGED
@@ -19,10 +19,10 @@ describe(TITLE, function() {
19
19
  options = {codec: msgpack.createCodec({useraw: true})};
20
20
 
21
21
  // raw
22
- assert.deepEqual(toArray(msgpack.decode(new Buffer([0xa1, 65]), options)), [65]);
22
+ assert.deepEqual(toArray(msgpack.decode(Buffer.from([0xa1, 65]), options)), [65]);
23
23
 
24
24
  // str
25
- assert.equal(msgpack.decode(new Buffer([0xa1, 65])), "A");
25
+ assert.equal(msgpack.decode(Buffer.from([0xa1, 65])), "A");
26
26
  });
27
27
 
28
28
  it("useraw (encode)", function() {
@@ -30,7 +30,7 @@ describe(TITLE, function() {
30
30
  assert.deepEqual(toArray(msgpack.encode("A", options)), [0xa1, 65]);
31
31
 
32
32
  // raw (Buffer)
33
- assert.deepEqual(toArray(msgpack.encode(new Buffer([65]), options)), [0xa1, 65]);
33
+ assert.deepEqual(toArray(msgpack.encode(Buffer.from([65]), options)), [0xa1, 65]);
34
34
 
35
35
  // str
36
36
  assert.deepEqual(toArray(msgpack.encode("A")), [0xa1, 65]);
@@ -67,7 +67,7 @@ describe(TITLE, function() {
67
67
  TESTS.forEach(test);
68
68
 
69
69
  function test(length) {
70
- var source = new Buffer(length);
70
+ var source = Buffer.alloc(length);
71
71
  for (var i = 0; i < length; i++) {
72
72
  source[i] = 65; // "A"
73
73
  }
@@ -0,0 +1,91 @@
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
+ var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
9
+
10
+ var TESTS = [0, 1, 31, 32, 255, 256, 65535, 65536];
11
+
12
+ function toArray(array) {
13
+ if (array instanceof ArrayBuffer) array = new Uint8Array(array);
14
+ return Array.prototype.slice.call(array);
15
+ }
16
+
17
+ // run this test when Uint8Array is available
18
+ var describeSkip = HAS_UINT8ARRAY ? describe : describe.skip;
19
+
20
+ describeSkip(TITLE, function() {
21
+ var options;
22
+
23
+ it("binarraybuffer (decode)", function() {
24
+ var decoded;
25
+ options = {codec: msgpack.createCodec({binarraybuffer: true, preset: true})};
26
+
27
+ // bin (Buffer)
28
+ decoded = msgpack.decode(Buffer.from([0xc4, 2, 65, 66]), options);
29
+ assert.ok(decoded instanceof ArrayBuffer);
30
+ assert.ok(!Buffer.isBuffer(decoded));
31
+ assert.deepEqual(toArray(decoded), [65, 66]);
32
+
33
+ // bin (Uint8Array)
34
+ decoded = msgpack.decode(new Uint8Array([0xc4, 2, 97, 98]), options);
35
+ assert.ok(decoded instanceof ArrayBuffer);
36
+ assert.ok(!Buffer.isBuffer(decoded));
37
+ assert.deepEqual(toArray(decoded), [97, 98]);
38
+
39
+ // bin (Array)
40
+ decoded = msgpack.decode([0xc4, 2, 65, 66], options);
41
+ assert.ok(decoded instanceof ArrayBuffer);
42
+ assert.ok(!Buffer.isBuffer(decoded));
43
+ assert.deepEqual(toArray(decoded), [65, 66]);
44
+ });
45
+
46
+ it("binarraybuffer (encode)", function() {
47
+ // bin (ArrayBuffer)
48
+ var encoded = msgpack.encode(new Uint8Array([65, 66]).buffer, options);
49
+ assert.deepEqual(toArray(encoded), [0xc4, 2, 65, 66]);
50
+ });
51
+
52
+ it("binarraybuffer (large)", function() {
53
+ TESTS.forEach(test);
54
+
55
+ function test(length) {
56
+ var source = new Uint8Array(length);
57
+ for (var i = 0; i < length; i++) {
58
+ source[i] = 65; // "A"
59
+ }
60
+
61
+ var encoded = msgpack.encode(source.buffer, options);
62
+ assert.ok(encoded.length);
63
+
64
+ var decoded = msgpack.decode(encoded, options);
65
+ assert.ok(decoded instanceof ArrayBuffer);
66
+ assert.ok(!Buffer.isBuffer(decoded));
67
+ decoded = new Uint8Array(decoded);
68
+ assert.equal(decoded.length, length);
69
+ if (length) assert.equal(decoded[0], 65);
70
+ }
71
+ });
72
+
73
+ // addExtPacker() and getExtPacker() methods need a valid constructor name.
74
+ // IE10 and iOS7 Safari may give another constructor name than Buffer.
75
+ // At those cases, below will be encoded as Uint8Array: [0xd5, 0x12, 97, 98]
76
+ var b = Buffer.alloc(1);
77
+ var c = b.constructor;
78
+ var d = (c && c.name === "Buffer") ? it : it.skip;
79
+ d("encode Buffer ext format 0x1B", function() {
80
+ // fixext 2 (Buffer)
81
+ var encoded = msgpack.encode(Buffer.from([97, 98]), options);
82
+ assert.deepEqual(toArray(encoded), [0xd5, 0x1b, 97, 98]);
83
+ });
84
+
85
+ it("decode Buffer ext format 0x1B", function() {
86
+ // fixext 2 (Buffer)
87
+ var decoded = msgpack.decode([0xd5, 0x1b, 65, 66], options);
88
+ assert.ok(Buffer.isBuffer(decoded));
89
+ assert.deepEqual(toArray(decoded), [65, 66]);
90
+ });
91
+ });
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env mocha -R spec
2
+
3
+ var assert = require("assert");
4
+ var Bufferish = require("../lib/bufferish");
5
+ var msgpackJS = "../index";
6
+ var isBrowser = ("undefined" !== typeof window);
7
+ var msgpack = isBrowser && window.msgpack || require(msgpackJS);
8
+ var TITLE = __filename.replace(/^.*\//, "");
9
+
10
+ var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
11
+
12
+ describe(TITLE, function() {
13
+ it("{}", function() {
14
+ var encoded = msgpack.encode(1);
15
+ assert.ok(Buffer.isBuffer(encoded));
16
+ // assert.ok(!ArrayBuffer.isView(encoded));
17
+ });
18
+
19
+ var it_Uint8Array = HAS_UINT8ARRAY ? it : it.skip;
20
+ var codecopt = {uint8array: true};
21
+
22
+ it_Uint8Array(JSON.stringify(codecopt), function() {
23
+ var codec = msgpack.createCodec(codecopt);
24
+ assert.ok(codec);
25
+ var options = {codec: codec};
26
+
27
+ // small data
28
+ var encoded = msgpack.encode(1, options);
29
+ if (ArrayBuffer.isView) assert.ok(ArrayBuffer.isView(encoded));
30
+ assert.ok(Bufferish.isView(encoded));
31
+ assert.ok(!Buffer.isBuffer(encoded));
32
+
33
+ // bigger data
34
+ var big = Buffer.alloc(8192); // 8KB
35
+ big[big.length - 1] = 99;
36
+ var source = [big, big, big, big, big, big, big, big]; // 64KB
37
+ encoded = msgpack.encode(source, options);
38
+ if (ArrayBuffer.isView) assert.ok(ArrayBuffer.isView(encoded));
39
+ assert.ok(Bufferish.isView(encoded));
40
+ assert.ok(!Buffer.isBuffer(encoded));
41
+ assert.equal(encoded[encoded.length - 1], 99); // last byte
42
+ });
43
+ });
@@ -0,0 +1,59 @@
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 OPTIONS = [{}, {safe: true}];
10
+
11
+ describe(TITLE, function() {
12
+ OPTIONS.forEach(function(options) {
13
+ var suffix = " " + JSON.stringify(options);
14
+ it("string (ASCII)" + suffix, function() {
15
+ var string = "a";
16
+ var array = [0xa1, 0x61];
17
+ var encoded = msgpack.encode(string, options);
18
+ var decoded = msgpack.decode(array, options);
19
+ assert.deepEqual(toArray(encoded), array);
20
+ assert.equal(decoded, string);
21
+ });
22
+
23
+ it("string (Greek)" + suffix, function() {
24
+ var string = "α";
25
+ var array = [0xa2, 0xce, 0xb1];
26
+ var encoded = msgpack.encode(string, options);
27
+ var decoded = msgpack.decode(array, options);
28
+ assert.deepEqual(toArray(encoded), array);
29
+ assert.equal(decoded, string);
30
+ });
31
+
32
+ it("string (Asian)" + suffix, function() {
33
+ var string = "亜";
34
+ var array = [0xa3, 0xe4, 0xba, 0x9c];
35
+ var encoded = msgpack.encode(string, options);
36
+ var decoded = msgpack.decode(array, options);
37
+ assert.deepEqual(toArray(encoded), array);
38
+ assert.equal(decoded, string);
39
+ });
40
+
41
+ // U+1F426 "🐦" bird
42
+ // http://unicode.org/emoji/charts/full-emoji-list.html#1f426
43
+ it("string (Emoji)" + suffix, function() {
44
+ var string = "\uD83D\uDC26"; // surrogate pair
45
+ var array_utf8 = [0xa4, 0xf0, 0x9f, 0x90, 0xa6]; // UTF-8
46
+ var array_cesu8 = [0xa6, 0xed, 0xa0, 0xbd, 0xed, 0xb0, 0xa6]; // CESU-8
47
+ var encoded = msgpack.encode(string, options);
48
+ var decoded_utf8 = msgpack.decode(array_utf8, options);
49
+ var decoded_cesu8 = msgpack.decode(array_cesu8, options);
50
+ assert.deepEqual(toArray(encoded), array_utf8);
51
+ assert.equal(decoded_utf8, string);
52
+ assert.equal(decoded_cesu8, string);
53
+ });
54
+ });
55
+ });
56
+
57
+ function toArray(buffer) {
58
+ return Array.prototype.slice.call(buffer);
59
+ }