msgpack5 3.4.0 → 3.6.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/.travis.yml +2 -0
- package/README.md +9 -0
- package/dist/msgpack5.js +2711 -1755
- package/dist/msgpack5.min.js +1 -4
- package/example.js +4 -1
- package/index.js +4 -1
- package/lib/encoder.js +38 -30
- package/lib/streams.js +2 -0
- package/package.json +13 -13
- package/test/1-byte-length-buffers.js +6 -5
- package/test/1-byte-length-exts.js +4 -3
- package/test/1-byte-length-strings.js +4 -3
- package/test/1-byte-length-uint8arrays.js +43 -0
- package/test/15-elements-arrays.js +2 -1
- package/test/15-elements-maps.js +3 -2
- package/test/16-bits-signed-integers.js +3 -2
- package/test/16-bits-unsigned-integers.js +3 -2
- package/test/2-bytes-length-arrays.js +3 -2
- package/test/2-bytes-length-buffers.js +5 -4
- package/test/2-bytes-length-exts.js +4 -3
- package/test/2-bytes-length-maps.js +3 -2
- package/test/2-bytes-length-strings.js +8 -7
- package/test/2-bytes-length-uint8arrays.js +43 -0
- package/test/31-chars-strings.js +7 -4
- package/test/32-bits-signed-integers.js +3 -2
- package/test/32-bits-unsigned-integers.js +3 -2
- package/test/32-bytes-strings.js +2 -1
- package/test/4-bytes-length-arrays.js +3 -2
- package/test/4-bytes-length-buffers.js +5 -4
- package/test/4-bytes-length-exts.js +4 -3
- package/test/4-bytes-length-strings.js +7 -6
- package/test/4-bytes-length-uint8arrays.js +42 -0
- package/test/5-bits-negative-integers.js +2 -1
- package/test/64-bits-signed-integers.js +2 -1
- package/test/64-bits-unsigned-integers.js +2 -1
- package/test/7-bits-positive-integers.js +2 -1
- package/test/8-bits-positive-integers.js +3 -2
- package/test/8-bits-signed-integers.js +3 -2
- package/test/booleans.js +3 -2
- package/test/doubles.js +3 -2
- package/test/ext-custom-encode-check.js +3 -2
- package/test/fixexts.js +21 -20
- package/test/floats.js +3 -2
- package/test/levelup-encoding.js +4 -4
- package/test/null.js +2 -1
- package/test/object-with-buffers.js +4 -3
- package/test/streams.js +3 -2
- package/.npmignore +0 -27
package/example.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
1
4
|
var msgpack = require('./')() // namespace our extensions
|
|
2
5
|
var a = new MyType(2, 'a')
|
|
3
6
|
var encode = msgpack.encode
|
|
@@ -22,7 +25,7 @@ function MyType (size, value) {
|
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
function mytipeEncode (obj) {
|
|
25
|
-
var buf =
|
|
28
|
+
var buf = Buffer.allocUnsafe(obj.size)
|
|
26
29
|
buf.fill(obj.value)
|
|
27
30
|
return buf
|
|
28
31
|
}
|
package/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
1
4
|
var assert = require('assert')
|
|
2
5
|
var bl = require('bl')
|
|
3
6
|
var streams = require('./lib/streams')
|
|
@@ -47,7 +50,7 @@ function msgpack (options) {
|
|
|
47
50
|
|
|
48
51
|
function reEncode (obj) {
|
|
49
52
|
var buf = bl()
|
|
50
|
-
var header =
|
|
53
|
+
var header = Buffer.allocUnsafe(1)
|
|
51
54
|
|
|
52
55
|
header.writeInt8(type, 0)
|
|
53
56
|
|
package/lib/encoder.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
1
4
|
var bl = require('bl')
|
|
2
5
|
var TOLERANCE = 0.1
|
|
3
6
|
|
|
@@ -9,50 +12,55 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
|
|
|
9
12
|
if (obj === undefined) {
|
|
10
13
|
throw new Error('undefined is not encodable in msgpack!')
|
|
11
14
|
} else if (obj === null) {
|
|
12
|
-
buf =
|
|
15
|
+
buf = Buffer.allocUnsafe(1)
|
|
13
16
|
buf[0] = 0xc0
|
|
14
17
|
} else if (obj === true) {
|
|
15
|
-
buf =
|
|
18
|
+
buf = Buffer.allocUnsafe(1)
|
|
16
19
|
buf[0] = 0xc3
|
|
17
20
|
} else if (obj === false) {
|
|
18
|
-
buf =
|
|
21
|
+
buf = Buffer.allocUnsafe(1)
|
|
19
22
|
buf[0] = 0xc2
|
|
20
23
|
} else if (typeof obj === 'string') {
|
|
21
24
|
len = Buffer.byteLength(obj)
|
|
22
25
|
if (len < 32) {
|
|
23
|
-
buf =
|
|
26
|
+
buf = Buffer.allocUnsafe(1 + len)
|
|
24
27
|
buf[0] = 0xa0 | len
|
|
25
|
-
|
|
28
|
+
if (len > 0) {
|
|
29
|
+
buf.write(obj, 1)
|
|
30
|
+
}
|
|
26
31
|
} else if (len <= 0xff && !compatibilityMode) {
|
|
27
32
|
// str8, but only when not in compatibility mode
|
|
28
|
-
buf =
|
|
33
|
+
buf = Buffer.allocUnsafe(2 + len)
|
|
29
34
|
buf[0] = 0xd9
|
|
30
35
|
buf[1] = len
|
|
31
36
|
buf.write(obj, 2)
|
|
32
37
|
} else if (len <= 0xffff) {
|
|
33
|
-
buf =
|
|
38
|
+
buf = Buffer.allocUnsafe(3 + len)
|
|
34
39
|
buf[0] = 0xda
|
|
35
40
|
buf.writeUInt16BE(len, 1)
|
|
36
41
|
buf.write(obj, 3)
|
|
37
42
|
} else {
|
|
38
|
-
buf =
|
|
43
|
+
buf = Buffer.allocUnsafe(5 + len)
|
|
39
44
|
buf[0] = 0xdb
|
|
40
45
|
buf.writeUInt32BE(len, 1)
|
|
41
46
|
buf.write(obj, 5)
|
|
42
47
|
}
|
|
43
|
-
} else if (obj && obj.readUInt32LE) {
|
|
48
|
+
} else if (obj && (obj.readUInt32LE || obj instanceof Uint8Array)) {
|
|
49
|
+
if (obj instanceof Uint8Array) {
|
|
50
|
+
obj = Buffer.from(obj)
|
|
51
|
+
}
|
|
44
52
|
// weird hack to support Buffer
|
|
45
53
|
// and Buffer-like objects
|
|
46
54
|
if (obj.length <= 0xff) {
|
|
47
|
-
buf =
|
|
55
|
+
buf = Buffer.allocUnsafe(2)
|
|
48
56
|
buf[0] = 0xc4
|
|
49
57
|
buf[1] = obj.length
|
|
50
58
|
} else if (obj.length <= 0xffff) {
|
|
51
|
-
buf =
|
|
59
|
+
buf = Buffer.allocUnsafe(3)
|
|
52
60
|
buf[0] = 0xc5
|
|
53
61
|
buf.writeUInt16BE(obj.length, 1)
|
|
54
62
|
} else {
|
|
55
|
-
buf =
|
|
63
|
+
buf = Buffer.allocUnsafe(5)
|
|
56
64
|
buf[0] = 0xc6
|
|
57
65
|
buf.writeUInt32BE(obj.length, 1)
|
|
58
66
|
}
|
|
@@ -60,14 +68,14 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
|
|
|
60
68
|
buf = bl([buf, obj])
|
|
61
69
|
} else if (Array.isArray(obj)) {
|
|
62
70
|
if (obj.length < 16) {
|
|
63
|
-
buf =
|
|
71
|
+
buf = Buffer.allocUnsafe(1)
|
|
64
72
|
buf[0] = 0x90 | obj.length
|
|
65
73
|
} else if (obj.length < 65536) {
|
|
66
|
-
buf =
|
|
74
|
+
buf = Buffer.allocUnsafe(3)
|
|
67
75
|
buf[0] = 0xdc
|
|
68
76
|
buf.writeUInt16BE(obj.length, 1)
|
|
69
77
|
} else {
|
|
70
|
-
buf =
|
|
78
|
+
buf = Buffer.allocUnsafe(5)
|
|
71
79
|
buf[0] = 0xdd
|
|
72
80
|
buf.writeUInt32BE(obj.length, 1)
|
|
73
81
|
}
|
|
@@ -83,22 +91,22 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
|
|
|
83
91
|
return encodeFloat(obj, forceFloat64)
|
|
84
92
|
} else if (obj >= 0) {
|
|
85
93
|
if (obj < 128) {
|
|
86
|
-
buf =
|
|
94
|
+
buf = Buffer.allocUnsafe(1)
|
|
87
95
|
buf[0] = obj
|
|
88
96
|
} else if (obj < 256) {
|
|
89
|
-
buf =
|
|
97
|
+
buf = Buffer.allocUnsafe(2)
|
|
90
98
|
buf[0] = 0xcc
|
|
91
99
|
buf[1] = obj
|
|
92
100
|
} else if (obj < 65536) {
|
|
93
|
-
buf =
|
|
101
|
+
buf = Buffer.allocUnsafe(3)
|
|
94
102
|
buf[0] = 0xcd
|
|
95
103
|
buf.writeUInt16BE(obj, 1)
|
|
96
104
|
} else if (obj <= 0xffffffff) {
|
|
97
|
-
buf =
|
|
105
|
+
buf = Buffer.allocUnsafe(5)
|
|
98
106
|
buf[0] = 0xce
|
|
99
107
|
buf.writeUInt32BE(obj, 1)
|
|
100
108
|
} else if (obj <= 9007199254740991) {
|
|
101
|
-
buf =
|
|
109
|
+
buf = Buffer.allocUnsafe(9)
|
|
102
110
|
buf[0] = 0xcf
|
|
103
111
|
write64BitUint(buf, obj)
|
|
104
112
|
} else {
|
|
@@ -106,22 +114,22 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
|
|
|
106
114
|
}
|
|
107
115
|
} else {
|
|
108
116
|
if (obj >= -32) {
|
|
109
|
-
buf =
|
|
117
|
+
buf = Buffer.allocUnsafe(1)
|
|
110
118
|
buf[0] = 0x100 + obj
|
|
111
119
|
} else if (obj >= -128) {
|
|
112
|
-
buf =
|
|
120
|
+
buf = Buffer.allocUnsafe(2)
|
|
113
121
|
buf[0] = 0xd0
|
|
114
122
|
buf.writeInt8(obj, 1)
|
|
115
123
|
} else if (obj >= -32768) {
|
|
116
|
-
buf =
|
|
124
|
+
buf = Buffer.allocUnsafe(3)
|
|
117
125
|
buf[0] = 0xd1
|
|
118
126
|
buf.writeInt16BE(obj, 1)
|
|
119
127
|
} else if (obj > -214748365) {
|
|
120
|
-
buf =
|
|
128
|
+
buf = Buffer.allocUnsafe(5)
|
|
121
129
|
buf[0] = 0xd2
|
|
122
130
|
buf.writeInt32BE(obj, 1)
|
|
123
131
|
} else if (obj >= -9007199254740991) {
|
|
124
|
-
buf =
|
|
132
|
+
buf = Buffer.allocUnsafe(9)
|
|
125
133
|
buf[0] = 0xd3
|
|
126
134
|
write64BitInt(buf, 1, obj)
|
|
127
135
|
} else {
|
|
@@ -187,7 +195,7 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
|
|
|
187
195
|
headers.push(length & 0x000000ff)
|
|
188
196
|
}
|
|
189
197
|
|
|
190
|
-
return bl().append(
|
|
198
|
+
return bl().append(Buffer.from(headers)).append(encoded)
|
|
191
199
|
}
|
|
192
200
|
|
|
193
201
|
function encodeObject (obj) {
|
|
@@ -207,10 +215,10 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
|
|
|
207
215
|
}
|
|
208
216
|
|
|
209
217
|
if (length < 16) {
|
|
210
|
-
header =
|
|
218
|
+
header = Buffer.allocUnsafe(1)
|
|
211
219
|
header[0] = 0x80 | length
|
|
212
220
|
} else {
|
|
213
|
-
header =
|
|
221
|
+
header = Buffer.allocUnsafe(3)
|
|
214
222
|
header[0] = 0xde
|
|
215
223
|
header.writeUInt16BE(length, 1)
|
|
216
224
|
}
|
|
@@ -264,14 +272,14 @@ function isFloat (n) {
|
|
|
264
272
|
function encodeFloat (obj, forceFloat64) {
|
|
265
273
|
var buf
|
|
266
274
|
|
|
267
|
-
buf =
|
|
275
|
+
buf = Buffer.allocUnsafe(5)
|
|
268
276
|
buf[0] = 0xca
|
|
269
277
|
buf.writeFloatBE(obj, 1)
|
|
270
278
|
|
|
271
279
|
// FIXME is there a way to check if a
|
|
272
280
|
// value fits in a float?
|
|
273
281
|
if (forceFloat64 || Math.abs(obj - buf.readFloatBE(1)) > TOLERANCE) {
|
|
274
|
-
buf =
|
|
282
|
+
buf = Buffer.allocUnsafe(9)
|
|
275
283
|
buf[0] = 0xcb
|
|
276
284
|
buf.writeDoubleBE(obj, 1)
|
|
277
285
|
}
|
package/lib/streams.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msgpack5",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "A msgpack v5 implementation for node.js and the browser, with extension points",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,8 +21,7 @@
|
|
|
21
21
|
"msgpack",
|
|
22
22
|
"extension",
|
|
23
23
|
"v5",
|
|
24
|
-
"
|
|
25
|
-
"v5",
|
|
24
|
+
"MessagePack",
|
|
26
25
|
"ext"
|
|
27
26
|
],
|
|
28
27
|
"author": "Matteo collina <hello@matteocollina.com>",
|
|
@@ -32,15 +31,15 @@
|
|
|
32
31
|
},
|
|
33
32
|
"homepage": "https://github.com/mcollina/msgpack5",
|
|
34
33
|
"devDependencies": {
|
|
35
|
-
"browserify": "^
|
|
34
|
+
"browserify": "^14.0.0",
|
|
36
35
|
"faucet": "0.0.1",
|
|
37
|
-
"jshint": "^2.5
|
|
38
|
-
"
|
|
39
|
-
"pre-commit": "1.
|
|
40
|
-
"standard": "^
|
|
41
|
-
"tape": "^4.
|
|
36
|
+
"jshint": "^2.9.5",
|
|
37
|
+
"memdb": "^1.3.1",
|
|
38
|
+
"pre-commit": "^1.2.2",
|
|
39
|
+
"standard": "^10.0.3",
|
|
40
|
+
"tape": "^4.8.0",
|
|
42
41
|
"testling": "^1.7.1",
|
|
43
|
-
"uglify-js": "^
|
|
42
|
+
"uglify-js": "^3.0.28"
|
|
44
43
|
},
|
|
45
44
|
"standard": {
|
|
46
45
|
"ignore": [
|
|
@@ -61,8 +60,9 @@
|
|
|
61
60
|
]
|
|
62
61
|
},
|
|
63
62
|
"dependencies": {
|
|
64
|
-
"bl": "^1.
|
|
65
|
-
"inherits": "^2.0.
|
|
66
|
-
"readable-stream": "^2.
|
|
63
|
+
"bl": "^1.2.1",
|
|
64
|
+
"inherits": "^2.0.3",
|
|
65
|
+
"readable-stream": "^2.3.3",
|
|
66
|
+
"safe-buffer": "^5.1.1"
|
|
67
67
|
}
|
|
68
68
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -7,7 +8,7 @@ var bl = require('bl')
|
|
|
7
8
|
function build (size) {
|
|
8
9
|
var buf
|
|
9
10
|
|
|
10
|
-
buf =
|
|
11
|
+
buf = Buffer.allocUnsafe(size)
|
|
11
12
|
buf.fill('a')
|
|
12
13
|
|
|
13
14
|
return buf
|
|
@@ -20,7 +21,7 @@ test('encode/decode 2^8-1 bytes buffers', function (t) {
|
|
|
20
21
|
all.push(build(Math.pow(2, 8) - 1))
|
|
21
22
|
all.push(build(Math.pow(2, 6) + 1))
|
|
22
23
|
all.push(build(1))
|
|
23
|
-
all.push(
|
|
24
|
+
all.push(Buffer.allocUnsafe(0))
|
|
24
25
|
|
|
25
26
|
all.forEach(function (orig) {
|
|
26
27
|
t.test('encoding a buffer of length ' + orig.length, function (t) {
|
|
@@ -33,7 +34,7 @@ test('encode/decode 2^8-1 bytes buffers', function (t) {
|
|
|
33
34
|
})
|
|
34
35
|
|
|
35
36
|
t.test('decoding a buffer of length ' + orig.length, function (t) {
|
|
36
|
-
var buf =
|
|
37
|
+
var buf = Buffer.allocUnsafe(2 + orig.length)
|
|
37
38
|
buf[0] = 0xc4
|
|
38
39
|
buf[1] = orig.length
|
|
39
40
|
orig.copy(buf, 2)
|
|
@@ -53,7 +54,7 @@ test('encode/decode 2^8-1 bytes buffers', function (t) {
|
|
|
53
54
|
test('decoding a chopped 2^8-1 bytes buffer', function (t) {
|
|
54
55
|
var encoder = msgpack()
|
|
55
56
|
var orig = build(Math.pow(2, 6))
|
|
56
|
-
var buf =
|
|
57
|
+
var buf = Buffer.allocUnsafe(2 + orig.length)
|
|
57
58
|
buf[0] = 0xc4
|
|
58
59
|
buf[1] = Math.pow(2, 8) - 1 // set bigger size
|
|
59
60
|
orig.copy(buf, 2)
|
|
@@ -68,7 +69,7 @@ test('decoding a chopped 2^8-1 bytes buffer', function (t) {
|
|
|
68
69
|
|
|
69
70
|
test('decoding an incomplete header of 2^8-1 bytes buffer', function (t) {
|
|
70
71
|
var encoder = msgpack()
|
|
71
|
-
var buf =
|
|
72
|
+
var buf = Buffer.allocUnsafe(1)
|
|
72
73
|
buf[0] = 0xc4
|
|
73
74
|
buf = bl().append(buf)
|
|
74
75
|
var origLength = buf.length
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -14,7 +15,7 @@ test('encode/decode variable ext data up to 0xff', function (t) {
|
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
function mytipeEncode (obj) {
|
|
17
|
-
var buf =
|
|
18
|
+
var buf = Buffer.allocUnsafe(obj.size)
|
|
18
19
|
buf.fill(obj.value)
|
|
19
20
|
return buf
|
|
20
21
|
}
|
|
@@ -72,7 +73,7 @@ test('encode/decode variable ext data up to 0xff', function (t) {
|
|
|
72
73
|
|
|
73
74
|
t.test('decoding an incomplete variable ext data up to 0xff', function (t) {
|
|
74
75
|
var obj = encoder.encode(new MyType(250, 'a'))
|
|
75
|
-
var buf =
|
|
76
|
+
var buf = Buffer.allocUnsafe(obj.length)
|
|
76
77
|
buf[0] = 0xc7
|
|
77
78
|
buf.writeUInt8(obj.length + 2, 1) // set bigger size
|
|
78
79
|
obj.copy(buf, 2, 2, obj.length)
|
|
@@ -86,7 +87,7 @@ test('encode/decode variable ext data up to 0xff', function (t) {
|
|
|
86
87
|
})
|
|
87
88
|
|
|
88
89
|
t.test('decoding an incomplete header of variable ext data up to 0xff', function (t) {
|
|
89
|
-
var buf =
|
|
90
|
+
var buf = Buffer.allocUnsafe(2)
|
|
90
91
|
buf[0] = 0xc7
|
|
91
92
|
buf = bl().append(buf)
|
|
92
93
|
var origLength = buf.length
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -28,7 +29,7 @@ test('encode/decode 32 <-> (2^8-1) bytes strings', function (t) {
|
|
|
28
29
|
})
|
|
29
30
|
|
|
30
31
|
t.test('decoding a string of length ' + str.length, function (t) {
|
|
31
|
-
var buf =
|
|
32
|
+
var buf = Buffer.allocUnsafe(2 + Buffer.byteLength(str))
|
|
32
33
|
buf[0] = 0xd9
|
|
33
34
|
buf[1] = Buffer.byteLength(str)
|
|
34
35
|
buf.write(str, 2)
|
|
@@ -50,7 +51,7 @@ test('decoding a chopped string', function (t) {
|
|
|
50
51
|
var str
|
|
51
52
|
for (str = 'a'; str.length < 40; str += 'a') {
|
|
52
53
|
}
|
|
53
|
-
var buf =
|
|
54
|
+
var buf = Buffer.allocUnsafe(2 + Buffer.byteLength(str))
|
|
54
55
|
buf[0] = 0xd9
|
|
55
56
|
buf[1] = Buffer.byteLength(str) + 10 // set bigger size
|
|
56
57
|
buf.write(str, 2)
|
|
@@ -65,7 +66,7 @@ test('decoding a chopped string', function (t) {
|
|
|
65
66
|
|
|
66
67
|
test('decoding an incomplete header of a string', function (t) {
|
|
67
68
|
var encoder = msgpack()
|
|
68
|
-
var buf =
|
|
69
|
+
var buf = Buffer.allocUnsafe(1)
|
|
69
70
|
buf[0] = 0xd9
|
|
70
71
|
buf = bl().append(buf)
|
|
71
72
|
var origLength = buf.length
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
4
|
+
var test = require('tape').test
|
|
5
|
+
var msgpack = require('../')
|
|
6
|
+
|
|
7
|
+
function build (size) {
|
|
8
|
+
var array = []
|
|
9
|
+
var i
|
|
10
|
+
|
|
11
|
+
for (i = 0; i < size; i++) {
|
|
12
|
+
array.push(42)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return new Uint8Array(array)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
test('encode/decode 2^8-1 Uint8Arrays', function (t) {
|
|
19
|
+
var encoder = msgpack()
|
|
20
|
+
var all = []
|
|
21
|
+
|
|
22
|
+
all.push(build(Math.pow(2, 8) - 1))
|
|
23
|
+
all.push(build(Math.pow(2, 6) + 1))
|
|
24
|
+
all.push(build(1))
|
|
25
|
+
all.push(new Uint8Array(0))
|
|
26
|
+
|
|
27
|
+
all.forEach(function (array) {
|
|
28
|
+
t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
|
|
29
|
+
var buf = encoder.encode(array)
|
|
30
|
+
t.equal(buf.length, 2 + array.byteLength, 'must have the right length')
|
|
31
|
+
t.equal(buf.readUInt8(0), 0xc4, 'must have the proper header')
|
|
32
|
+
t.equal(buf.readUInt8(1), array.byteLength, 'must include the buf length')
|
|
33
|
+
t.end()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
t.test('mirror test for an Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
|
|
37
|
+
t.deepEqual(encoder.decode(encoder.encode(array)), new Buffer(array), 'must stay the same')
|
|
38
|
+
t.end()
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
t.end()
|
|
43
|
+
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -65,7 +66,7 @@ test('decoding an incomplete array', function (t) {
|
|
|
65
66
|
|
|
66
67
|
var array = ['a', 'b', 'c']
|
|
67
68
|
var size = computeLength(array)
|
|
68
|
-
var buf =
|
|
69
|
+
var buf = Buffer.allocUnsafe(size)
|
|
69
70
|
buf[0] = 0x90 | array.length + 2 // set bigger size
|
|
70
71
|
var pos = 1
|
|
71
72
|
for (var i = 0; i < array.length; i++) {
|
package/test/15-elements-maps.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -74,7 +75,7 @@ test('encode/decode map with buf, ints and strings', function (t) {
|
|
|
74
75
|
var map = {
|
|
75
76
|
topic: 'hello',
|
|
76
77
|
qos: 1,
|
|
77
|
-
payload:
|
|
78
|
+
payload: Buffer.from('world'),
|
|
78
79
|
messageId: '42',
|
|
79
80
|
ttl: 1416309270167
|
|
80
81
|
}
|
|
@@ -87,7 +88,7 @@ test('encode/decode map with buf, ints and strings', function (t) {
|
|
|
87
88
|
test('decoding a chopped map', function (t) {
|
|
88
89
|
var encoder = msgpack()
|
|
89
90
|
var map = encoder.encode({'a': 'b', 'c': 'd', 'e': 'f'})
|
|
90
|
-
var buf =
|
|
91
|
+
var buf = Buffer.allocUnsafe(map.length)
|
|
91
92
|
buf[0] = 0x80 | 5 // set bigger size
|
|
92
93
|
map.copy(buf, 1, 1, map.length)
|
|
93
94
|
buf = bl().append(buf)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -25,7 +26,7 @@ test('encoding/decoding 16-bits big-endian signed integers', function (t) {
|
|
|
25
26
|
})
|
|
26
27
|
|
|
27
28
|
t.test('decoding ' + num, function (t) {
|
|
28
|
-
var buf =
|
|
29
|
+
var buf = Buffer.allocUnsafe(3)
|
|
29
30
|
buf[0] = 0xd1
|
|
30
31
|
buf.writeInt16BE(num, 1)
|
|
31
32
|
t.equal(encoder.decode(buf), num, 'must decode correctly')
|
|
@@ -43,7 +44,7 @@ test('encoding/decoding 16-bits big-endian signed integers', function (t) {
|
|
|
43
44
|
|
|
44
45
|
test('decoding an incomplete 16-bits big-endian integer', function (t) {
|
|
45
46
|
var encoder = msgpack()
|
|
46
|
-
var buf =
|
|
47
|
+
var buf = Buffer.allocUnsafe(2)
|
|
47
48
|
buf[0] = 0xd1
|
|
48
49
|
buf = bl().append(buf)
|
|
49
50
|
var origLength = buf.length
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -25,7 +26,7 @@ test('encoding/decoding 16-bits big-endian unsigned integers', function (t) {
|
|
|
25
26
|
})
|
|
26
27
|
|
|
27
28
|
t.test('decoding ' + num, function (t) {
|
|
28
|
-
var buf =
|
|
29
|
+
var buf = Buffer.allocUnsafe(3)
|
|
29
30
|
buf[0] = 0xcd
|
|
30
31
|
buf.writeUInt16BE(num, 1)
|
|
31
32
|
t.equal(encoder.decode(buf), num, 'must decode correctly')
|
|
@@ -43,7 +44,7 @@ test('encoding/decoding 16-bits big-endian unsigned integers', function (t) {
|
|
|
43
44
|
|
|
44
45
|
test('decoding an incomplete 16-bits big-endian unsigned integer', function (t) {
|
|
45
46
|
var encoder = msgpack()
|
|
46
|
-
var buf =
|
|
47
|
+
var buf = Buffer.allocUnsafe(2)
|
|
47
48
|
buf[0] = 0xcd
|
|
48
49
|
buf = bl().append(buf)
|
|
49
50
|
var origLength = buf.length
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -50,7 +51,7 @@ test('decoding an incomplete array', function (t) {
|
|
|
50
51
|
var encoder = msgpack()
|
|
51
52
|
|
|
52
53
|
var array = build(0xffff / 2)
|
|
53
|
-
var buf =
|
|
54
|
+
var buf = Buffer.allocUnsafe(3 + array.length)
|
|
54
55
|
buf[0] = 0xdc
|
|
55
56
|
buf.writeUInt16BE(array.length + 10, 1) // set bigger size
|
|
56
57
|
var pos = 3
|
|
@@ -71,7 +72,7 @@ test('decoding an incomplete array', function (t) {
|
|
|
71
72
|
test('decoding an incomplete header', function (t) {
|
|
72
73
|
var encoder = msgpack()
|
|
73
74
|
|
|
74
|
-
var buf =
|
|
75
|
+
var buf = Buffer.allocUnsafe(2)
|
|
75
76
|
buf[0] = 0xdc
|
|
76
77
|
buf = bl().append(buf)
|
|
77
78
|
var origLength = buf.length
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -7,7 +8,7 @@ var bl = require('bl')
|
|
|
7
8
|
function build (size) {
|
|
8
9
|
var buf
|
|
9
10
|
|
|
10
|
-
buf =
|
|
11
|
+
buf = Buffer.allocUnsafe(size)
|
|
11
12
|
buf.fill('a')
|
|
12
13
|
|
|
13
14
|
return buf
|
|
@@ -33,7 +34,7 @@ test('encode/decode 2^16-1 bytes buffers', function (t) {
|
|
|
33
34
|
})
|
|
34
35
|
|
|
35
36
|
t.test('decoding a buffer of length ' + orig.length, function (t) {
|
|
36
|
-
var buf =
|
|
37
|
+
var buf = Buffer.allocUnsafe(3 + orig.length)
|
|
37
38
|
buf[0] = 0xc5
|
|
38
39
|
buf.writeUInt16BE(orig.length, 1)
|
|
39
40
|
orig.copy(buf, 3)
|
|
@@ -53,7 +54,7 @@ test('encode/decode 2^16-1 bytes buffers', function (t) {
|
|
|
53
54
|
test('decoding a chopped 2^16-1 bytes buffer', function (t) {
|
|
54
55
|
var encoder = msgpack()
|
|
55
56
|
var orig = build(Math.pow(2, 12))
|
|
56
|
-
var buf =
|
|
57
|
+
var buf = Buffer.allocUnsafe(3 + orig.length)
|
|
57
58
|
buf[0] = 0xc5
|
|
58
59
|
buf[1] = Math.pow(2, 16) - 1 // set bigger size
|
|
59
60
|
orig.copy(buf, 3)
|
|
@@ -68,7 +69,7 @@ test('decoding a chopped 2^16-1 bytes buffer', function (t) {
|
|
|
68
69
|
|
|
69
70
|
test('decoding an incomplete header of 2^16-1 bytes buffer', function (t) {
|
|
70
71
|
var encoder = msgpack()
|
|
71
|
-
var buf =
|
|
72
|
+
var buf = Buffer.allocUnsafe(2)
|
|
72
73
|
buf[0] = 0xc5
|
|
73
74
|
buf = bl().append(buf)
|
|
74
75
|
var origLength = buf.length
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -14,7 +15,7 @@ test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t
|
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
function mytipeEncode (obj) {
|
|
17
|
-
var buf =
|
|
18
|
+
var buf = Buffer.allocUnsafe(obj.size)
|
|
18
19
|
buf.fill(obj.value)
|
|
19
20
|
return buf
|
|
20
21
|
}
|
|
@@ -56,7 +57,7 @@ test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t
|
|
|
56
57
|
|
|
57
58
|
t.test('decoding an incomplete variable ext data up between 0x0100 and 0xffff', function (t) {
|
|
58
59
|
var obj = encoder.encode(new MyType(0xfff0, 'a'))
|
|
59
|
-
var buf =
|
|
60
|
+
var buf = Buffer.allocUnsafe(obj.length)
|
|
60
61
|
buf[0] = 0xc8
|
|
61
62
|
buf.writeUInt16BE(obj.length + 2, 1) // set bigger size
|
|
62
63
|
obj.copy(buf, 3, 3, obj.length)
|
|
@@ -70,7 +71,7 @@ test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t
|
|
|
70
71
|
})
|
|
71
72
|
|
|
72
73
|
t.test('decoding an incomplete header of variable ext data up between 0x0100 and 0xffff', function (t) {
|
|
73
|
-
var buf =
|
|
74
|
+
var buf = Buffer.allocUnsafe(3)
|
|
74
75
|
buf[0] = 0xc8
|
|
75
76
|
buf = bl().append(buf)
|
|
76
77
|
var origLength = buf.length
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
var Buffer = require('safe-buffer').Buffer
|
|
3
4
|
var test = require('tape').test
|
|
4
5
|
var msgpack = require('../')
|
|
5
6
|
var bl = require('bl')
|
|
@@ -57,7 +58,7 @@ test('encode/decode maps up to 2^16-1 elements', function (t) {
|
|
|
57
58
|
test('decoding a chopped map', function (t) {
|
|
58
59
|
var encoder = msgpack()
|
|
59
60
|
var map = encoder.encode(build(Math.pow(2, 12) + 1, 42))
|
|
60
|
-
var buf =
|
|
61
|
+
var buf = Buffer.allocUnsafe(map.length)
|
|
61
62
|
buf[0] = 0xde
|
|
62
63
|
buf.writeUInt16BE(Math.pow(2, 16) - 1, 1) // set bigger size
|
|
63
64
|
map.copy(buf, 3, 3, map.length)
|
|
@@ -72,7 +73,7 @@ test('decoding a chopped map', function (t) {
|
|
|
72
73
|
|
|
73
74
|
test('decoding an incomplete header of a map', function (t) {
|
|
74
75
|
var encoder = msgpack()
|
|
75
|
-
var buf =
|
|
76
|
+
var buf = Buffer.allocUnsafe(2)
|
|
76
77
|
buf[0] = 0xde
|
|
77
78
|
buf = bl().append(buf)
|
|
78
79
|
var origLength = buf.length
|