msgpack-lite 0.1.26 → 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/LICENSE +1 -1
- package/README.md +44 -164
- package/dist/msgpack.min.js +1 -2
- package/lib/bufferish-buffer.js +1 -1
- package/lib/decode-stream.js +7 -3
- package/lib/encode-stream.js +12 -4
- package/lib/ext-unpacker.js +1 -1
- package/lib/write-type.js +5 -0
- package/package.json +30 -19
- package/.npmignore +0 -8
- package/.travis.yml +0 -26
- package/.zuul.yml +0 -20
- package/Makefile +0 -48
- package/bower.json +0 -34
- 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 -371
- 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 -119
- 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/zuul/ie.html +0 -4
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
|
-
# msgpack-lite
|
|
1
|
+
# msgpack-lite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/kawanet/msgpack-lite/actions/)
|
|
4
|
+
[](https://www.npmjs.com/package/msgpack-lite)
|
|
5
|
+
[](https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
Fast Pure JavaScript MessagePack Encoder and Decoder
|
|
6
8
|
|
|
7
9
|
Online demo: [http://kawanet.github.io/msgpack-lite/](http://kawanet.github.io/msgpack-lite/)
|
|
8
10
|
|
|
9
|
-
### Features
|
|
10
|
-
|
|
11
|
-
- Pure JavaScript only (No node-gyp nor gcc required)
|
|
12
|
-
- Faster than any other pure JavaScript libraries on node.js v4
|
|
13
|
-
- Even faster than node-gyp C++ based [msgpack](https://www.npmjs.com/package/msgpack) library (**90% faster** on encoding)
|
|
14
|
-
- Streaming encoding and decoding interface is also available. It's more faster.
|
|
15
|
-
- Ready for [Web browsers](https://saucelabs.com/u/msgpack-lite) including Chrome, Firefox, Safari and even IE8
|
|
16
|
-
- [Tested](https://travis-ci.org/kawanet/msgpack-lite) on Node.js v0.10, v0.12, v4, v5 and v6 as well as Web browsers
|
|
17
|
-
|
|
18
11
|
### Encoding and Decoding MessagePack
|
|
19
12
|
|
|
20
13
|
```js
|
|
21
|
-
|
|
14
|
+
const msgpack = require("msgpack-lite");
|
|
22
15
|
|
|
23
16
|
// encode from JS Object to MessagePack (Buffer)
|
|
24
|
-
|
|
17
|
+
const buffer = msgpack.encode({"foo": "bar"});
|
|
25
18
|
|
|
26
19
|
// decode from MessagePack (Buffer) to JS Object
|
|
27
|
-
|
|
20
|
+
const data = msgpack.decode(buffer); // => {"foo": "bar"}
|
|
28
21
|
|
|
29
22
|
// if encode/decode receives an invalid argument an error is thrown
|
|
30
23
|
```
|
|
@@ -32,11 +25,11 @@ var data = msgpack.decode(buffer); // => {"foo": "bar"}
|
|
|
32
25
|
### Writing to MessagePack Stream
|
|
33
26
|
|
|
34
27
|
```js
|
|
35
|
-
|
|
36
|
-
|
|
28
|
+
const fs = require("fs");
|
|
29
|
+
const msgpack = require("msgpack-lite");
|
|
37
30
|
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
const writeStream = fs.createWriteStream("test.msp");
|
|
32
|
+
const encodeStream = msgpack.createEncodeStream();
|
|
40
33
|
encodeStream.pipe(writeStream);
|
|
41
34
|
|
|
42
35
|
// send multiple objects to stream
|
|
@@ -50,11 +43,11 @@ encodeStream.end();
|
|
|
50
43
|
### Reading from MessagePack Stream
|
|
51
44
|
|
|
52
45
|
```js
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
const fs = require("fs");
|
|
47
|
+
const msgpack = require("msgpack-lite");
|
|
55
48
|
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
const readStream = fs.createReadStream("test.msp");
|
|
50
|
+
const decodeStream = msgpack.createDecodeStream();
|
|
58
51
|
|
|
59
52
|
// show multiple objects decoded from stream
|
|
60
53
|
readStream.pipe(decodeStream).on("data", console.warn);
|
|
@@ -63,10 +56,10 @@ readStream.pipe(decodeStream).on("data", console.warn);
|
|
|
63
56
|
### Decoding MessagePack Bytes Array
|
|
64
57
|
|
|
65
58
|
```js
|
|
66
|
-
|
|
59
|
+
const msgpack = require("msgpack-lite");
|
|
67
60
|
|
|
68
61
|
// decode() accepts Buffer instance per default
|
|
69
|
-
msgpack.decode(Buffer([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
|
|
62
|
+
msgpack.decode(Buffer.from([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
|
|
70
63
|
|
|
71
64
|
// decode() also accepts Array instance
|
|
72
65
|
msgpack.decode([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);
|
|
@@ -101,24 +94,16 @@ Run tests on node.js:
|
|
|
101
94
|
$ make test
|
|
102
95
|
```
|
|
103
96
|
|
|
104
|
-
Run tests on browsers:
|
|
105
|
-
|
|
106
|
-
```sh
|
|
107
|
-
$ make test-browser-local
|
|
108
|
-
open the following url in a browser:
|
|
109
|
-
http://localhost:4000/__zuul
|
|
110
|
-
```
|
|
111
|
-
|
|
112
97
|
### Browser Build
|
|
113
98
|
|
|
114
|
-
Browser version [msgpack.min.js](https://
|
|
99
|
+
Browser version [msgpack.min.js](https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js) is also available. 50KB minified, 14KB gziped.
|
|
115
100
|
|
|
116
101
|
```html
|
|
117
102
|
<!--[if lte IE 9]>
|
|
118
103
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.10/es5-shim.min.js"></script>
|
|
119
104
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
|
|
120
105
|
<![endif]-->
|
|
121
|
-
<script src="https://
|
|
106
|
+
<script src="https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js"></script>
|
|
122
107
|
<script>
|
|
123
108
|
// encode from JS Object to MessagePack (Uint8Array)
|
|
124
109
|
var buffer = msgpack.encode({foo: "bar"});
|
|
@@ -129,111 +114,6 @@ var data = msgpack.decode(array);
|
|
|
129
114
|
</script>
|
|
130
115
|
```
|
|
131
116
|
|
|
132
|
-
### MessagePack With Browserify
|
|
133
|
-
|
|
134
|
-
Step #1: write some code at first.
|
|
135
|
-
|
|
136
|
-
```js
|
|
137
|
-
var msgpack = require("msgpack-lite");
|
|
138
|
-
var buffer = msgpack.encode({"foo": "bar"});
|
|
139
|
-
var data = msgpack.decode(buffer);
|
|
140
|
-
console.warn(data); // => {"foo": "bar"}
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
Proceed to the next steps if you prefer faster browserify compilation time.
|
|
144
|
-
|
|
145
|
-
Step #2: add `browser` property on `package.json` in your project. This refers the global `msgpack` object instead of including whole of `msgpack-lite` source code.
|
|
146
|
-
|
|
147
|
-
```json
|
|
148
|
-
{
|
|
149
|
-
"dependencies": {
|
|
150
|
-
"msgpack-lite": "*"
|
|
151
|
-
},
|
|
152
|
-
"browser": {
|
|
153
|
-
"msgpack-lite": "msgpack-lite/global"
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Step #3: compile it with [browserify](https://www.npmjs.com/package/browserify) and [uglifyjs](https://www.npmjs.com/package/uglify-js).
|
|
159
|
-
|
|
160
|
-
```sh
|
|
161
|
-
browserify src/main.js -o tmp/main.browserify.js -s main
|
|
162
|
-
uglifyjs tmp/main.browserify.js -m -c -o js/main.min.js
|
|
163
|
-
cp node_modules/msgpack-lite/dist/msgpack.min.js js/msgpack.min.js
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
Step #4: load [msgpack.min.js](https://rawgit.com/kawanet/msgpack-lite/master/dist/msgpack.min.js) before your code.
|
|
167
|
-
|
|
168
|
-
```html
|
|
169
|
-
<script src="js/msgpack.min.js"></script>
|
|
170
|
-
<script src="js/main.min.js"></script>
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### Interoperability
|
|
174
|
-
|
|
175
|
-
It is tested to have basic compatibility with other Node.js MessagePack modules below:
|
|
176
|
-
|
|
177
|
-
- [https://www.npmjs.com/package/msgpack](https://www.npmjs.com/package/msgpack) (1.0.2)
|
|
178
|
-
- [https://www.npmjs.com/package/msgpack-js](https://www.npmjs.com/package/msgpack-js) (0.3.0)
|
|
179
|
-
- [https://www.npmjs.com/package/msgpack-js-v5](https://www.npmjs.com/package/msgpack-js-v5) (0.3.0-v5)
|
|
180
|
-
- [https://www.npmjs.com/package/msgpack-unpack](https://www.npmjs.com/package/msgpack-unpack) (2.1.1)
|
|
181
|
-
- [https://github.com/msgpack/msgpack-javascript](https://github.com/msgpack/msgpack-javascript) (msgpack.codec)
|
|
182
|
-
- [https://www.npmjs.com/package/msgpack5](https://www.npmjs.com/package/msgpack5) (3.3.0)
|
|
183
|
-
- [https://www.npmjs.com/package/notepack](https://www.npmjs.com/package/notepack) (0.0.2)
|
|
184
|
-
|
|
185
|
-
### Benchmarks
|
|
186
|
-
|
|
187
|
-
A benchmark tool `lib/benchmark.js` is available to compare encoding/decoding speed
|
|
188
|
-
(operation per second) with other MessagePack modules.
|
|
189
|
-
It counts operations of [1KB JSON document](https://github.com/kawanet/msgpack-lite/blob/master/test/example.json) in 10 seconds.
|
|
190
|
-
|
|
191
|
-
```sh
|
|
192
|
-
$ npm install msgpack msgpack-js msgpack-js-v5 msgpack-unpack msgpack5 notepack
|
|
193
|
-
$ npm run benchmark 10
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
operation | op | ms | op/s
|
|
197
|
-
--------------------------------------------------------- | -----: | ----: | -----:
|
|
198
|
-
buf = Buffer(JSON.stringify(obj)); | 1055200 | 10000 | 105520
|
|
199
|
-
obj = JSON.parse(buf); | 863800 | 10000 | 86380
|
|
200
|
-
buf = require("msgpack-lite").encode(obj); | 969100 | 10000 | 96910
|
|
201
|
-
obj = require("msgpack-lite").decode(buf); | 600300 | 10000 | 60030
|
|
202
|
-
buf = require("msgpack").pack(obj); | 503500 | 10001 | 50344
|
|
203
|
-
obj = require("msgpack").unpack(buf); | 560200 | 10001 | 56014
|
|
204
|
-
buf = Buffer(require("msgpack.codec").msgpack.pack(obj)); | 653500 | 10000 | 65349
|
|
205
|
-
obj = require("msgpack.codec").msgpack.unpack(buf); | 367500 | 10001 | 36746
|
|
206
|
-
buf = require("msgpack-js-v5").encode(obj); | 189500 | 10002 | 18946
|
|
207
|
-
obj = require("msgpack-js-v5").decode(buf); | 408900 | 10000 | 40890
|
|
208
|
-
buf = require("msgpack-js").encode(obj); | 189200 | 10000 | 18920
|
|
209
|
-
obj = require("msgpack-js").decode(buf); | 375600 | 10002 | 37552
|
|
210
|
-
buf = require("msgpack5")().encode(obj); | 110500 | 10009 | 11040
|
|
211
|
-
obj = require("msgpack5")().decode(buf); | 165500 | 10000 | 16550
|
|
212
|
-
buf = require("notepack")().encode(obj); | 847800 | 10000 | 84780
|
|
213
|
-
obj = require("notepack")().decode(buf); | 599800 | 10000 | 59980
|
|
214
|
-
obj = require("msgpack-unpack").decode(buf); | 48100 | 10002 | 4809
|
|
215
|
-
|
|
216
|
-
Streaming benchmark tool `lib/benchmark-stream.js` is also available.
|
|
217
|
-
It counts milliseconds for 1,000,000 operations of 30 bytes fluentd msgpack fragment.
|
|
218
|
-
This shows streaming encoding and decoding are super faster.
|
|
219
|
-
|
|
220
|
-
```sh
|
|
221
|
-
$ npm run benchmark-stream 2
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
operation (1000000 x 2) | op | ms | op/s
|
|
225
|
-
------------------------------------------------ | ------: | ----: | -----:
|
|
226
|
-
stream.write(msgpack.encode(obj)); | 1000000 | 3027 | 330360
|
|
227
|
-
stream.write(notepack.encode(obj)); | 1000000 | 2012 | 497017
|
|
228
|
-
msgpack.Encoder().on("data",ondata).encode(obj); | 1000000 | 2956 | 338294
|
|
229
|
-
msgpack.createEncodeStream().write(obj); | 1000000 | 1888 | 529661
|
|
230
|
-
stream.write(msgpack.decode(buf)); | 1000000 | 2020 | 495049
|
|
231
|
-
stream.write(notepack.decode(buf)); | 1000000 | 1794 | 557413
|
|
232
|
-
msgpack.Decoder().on("data",ondata).decode(buf); | 1000000 | 2744 | 364431
|
|
233
|
-
msgpack.createDecodeStream().write(buf); | 1000000 | 1341 | 745712
|
|
234
|
-
|
|
235
|
-
Test environment: msgpack-lite 0.1.14, Node v4.2.3, Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz
|
|
236
|
-
|
|
237
117
|
### MessagePack Mapping Table
|
|
238
118
|
|
|
239
119
|
The following table shows how JavaScript objects (value) will be mapped to
|
|
@@ -288,15 +168,15 @@ Other extension types are mapped to built-in ExtBuffer object.
|
|
|
288
168
|
Register a custom extension type number to serialize/deserialize your own class instances.
|
|
289
169
|
|
|
290
170
|
```js
|
|
291
|
-
|
|
171
|
+
const msgpack = require("msgpack-lite");
|
|
292
172
|
|
|
293
|
-
|
|
173
|
+
const codec = msgpack.createCodec();
|
|
294
174
|
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
|
|
295
175
|
codec.addExtUnpacker(0x3F, myVectorUnpacker);
|
|
296
176
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
177
|
+
const data = new MyVector(1, 2);
|
|
178
|
+
const encoded = msgpack.encode(data, {codec: codec});
|
|
179
|
+
const decoded = msgpack.decode(encoded, {codec: codec});
|
|
300
180
|
|
|
301
181
|
function MyVector(x, y) {
|
|
302
182
|
this.x = x;
|
|
@@ -304,17 +184,17 @@ function MyVector(x, y) {
|
|
|
304
184
|
}
|
|
305
185
|
|
|
306
186
|
function myVectorPacker(vector) {
|
|
307
|
-
|
|
187
|
+
const array = [vector.x, vector.y];
|
|
308
188
|
return msgpack.encode(array); // return Buffer serialized
|
|
309
189
|
}
|
|
310
190
|
|
|
311
191
|
function myVectorUnpacker(buffer) {
|
|
312
|
-
|
|
192
|
+
const array = msgpack.decode(buffer);
|
|
313
193
|
return new MyVector(array[0], array[1]); // return Object deserialized
|
|
314
194
|
}
|
|
315
195
|
```
|
|
316
196
|
|
|
317
|
-
The first argument of `addExtPacker` and `addExtUnpacker` should be an integer within the range of 0 and 127 (0x0 and 0x7F). `
|
|
197
|
+
The first argument of `addExtPacker` and `addExtUnpacker` should be an integer within the range of 0 and 127 (0x0 and 0x7F). `myVectorPacker` is a function that accepts an instance of `MyVector`, and should return a buffer representing that instance. `myVectorUnpacker` is the opposite: it accepts a buffer and should return an instance of `MyVector`.
|
|
318
198
|
|
|
319
199
|
If you pass an array of functions to `addExtPacker` or `addExtUnpacker`, the value to be encoded/decoded will pass through each one in order. This allows you to do things like this:
|
|
320
200
|
|
|
@@ -333,50 +213,50 @@ If you wish to modify the default built-in codec, you can access it at `msgpack.
|
|
|
333
213
|
It does NOT have the preset extension types defined when no options given.
|
|
334
214
|
|
|
335
215
|
```js
|
|
336
|
-
|
|
216
|
+
const codec = msgpack.createCodec();
|
|
337
217
|
```
|
|
338
218
|
|
|
339
219
|
`preset`: It has the preset extension types described above.
|
|
340
220
|
|
|
341
221
|
```js
|
|
342
|
-
|
|
222
|
+
const codec = msgpack.createCodec({preset: true});
|
|
343
223
|
```
|
|
344
224
|
|
|
345
225
|
`safe`: It runs a validation of the value before writing it into buffer. This is the default behavior for some old browsers which do not support `ArrayBuffer` object.
|
|
346
226
|
|
|
347
227
|
```js
|
|
348
|
-
|
|
228
|
+
const codec = msgpack.createCodec({safe: true});
|
|
349
229
|
```
|
|
350
230
|
|
|
351
231
|
`useraw`: It uses `raw` formats instead of `bin` and `str`.
|
|
352
232
|
|
|
353
233
|
```js
|
|
354
|
-
|
|
234
|
+
const codec = msgpack.createCodec({useraw: true});
|
|
355
235
|
```
|
|
356
236
|
|
|
357
237
|
`int64`: It decodes msgpack's `int64`/`uint64` formats with [int64-buffer](https://github.com/kawanet/int64-buffer) object.
|
|
358
238
|
|
|
359
239
|
```js
|
|
360
|
-
|
|
240
|
+
const codec = msgpack.createCodec({int64: true});
|
|
361
241
|
```
|
|
362
242
|
|
|
363
243
|
`binarraybuffer`: It ties msgpack's `bin` format with `ArrayBuffer` object, instead of `Buffer` object.
|
|
364
244
|
|
|
365
245
|
```js
|
|
366
|
-
|
|
246
|
+
const codec = msgpack.createCodec({binarraybuffer: true, preset: true});
|
|
367
247
|
```
|
|
368
248
|
|
|
369
249
|
`uint8array`: It returns Uint8Array object when encoding, instead of `Buffer` object.
|
|
370
250
|
|
|
371
251
|
```js
|
|
372
|
-
|
|
252
|
+
const codec = msgpack.createCodec({uint8array: true});
|
|
373
253
|
```
|
|
374
254
|
|
|
375
255
|
`usemap`: Uses the global JavaScript Map type, if available, to unpack
|
|
376
256
|
MessagePack map elements.
|
|
377
257
|
|
|
378
258
|
```js
|
|
379
|
-
|
|
259
|
+
const codec = msgpack.createCodec({usemap: true});
|
|
380
260
|
```
|
|
381
261
|
|
|
382
262
|
### Compatibility Mode
|
|
@@ -386,18 +266,18 @@ The [compatibility mode](https://github.com/kawanet/msgpack-lite/issues/22) resp
|
|
|
386
266
|
```js
|
|
387
267
|
// default mode handles both str and bin formats individually
|
|
388
268
|
msgpack.encode("Aa"); // => <Buffer a2 41 61> (str format)
|
|
389
|
-
msgpack.encode(
|
|
269
|
+
msgpack.encode(Buffer.from([0x41, 0x61])); // => <Buffer c4 02 41 61> (bin format)
|
|
390
270
|
|
|
391
|
-
msgpack.decode(
|
|
392
|
-
msgpack.decode(
|
|
271
|
+
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61])); // => 'Aa' (String)
|
|
272
|
+
msgpack.decode(Buffer.from([0xc4, 0x02, 0x41, 0x61])); // => <Buffer 41 61> (Buffer)
|
|
393
273
|
|
|
394
274
|
// compatibility mode handles only raw format both for String and Buffer
|
|
395
|
-
|
|
275
|
+
const options = {codec: msgpack.createCodec({useraw: true})};
|
|
396
276
|
msgpack.encode("Aa", options); // => <Buffer a2 41 61> (raw format)
|
|
397
|
-
msgpack.encode(
|
|
277
|
+
msgpack.encode(Buffer.from([0x41, 0x61]), options); // => <Buffer a2 41 61> (raw format)
|
|
398
278
|
|
|
399
|
-
msgpack.decode(
|
|
400
|
-
msgpack.decode(
|
|
279
|
+
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options); // => <Buffer 41 61> (Buffer)
|
|
280
|
+
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String)
|
|
401
281
|
```
|
|
402
282
|
|
|
403
283
|
### Repository
|
|
@@ -412,7 +292,7 @@ msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (
|
|
|
412
292
|
|
|
413
293
|
The MIT License (MIT)
|
|
414
294
|
|
|
415
|
-
Copyright (c) 2015-
|
|
295
|
+
Copyright (c) 2015-2026 Yusuke Kawasaki
|
|
416
296
|
|
|
417
297
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
418
298
|
of this software and associated documentation files (the "Software"), to deal
|