msgpack5 6.0.1 → 6.0.2

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 CHANGED
@@ -108,7 +108,7 @@ options:
108
108
 
109
109
  - `forceFloat64`, a boolean to that forces all floats to be encoded as 64-bits floats. Defaults to false.
110
110
  - `sortKeys`, a boolean to force a determinate keys order
111
- - `compatibilityMode`, a boolean that enables "compatibility mode" which doesn't use str 8 format. Defaults to false.
111
+ - `compatibilityMode`, a boolean that enables "compatibility mode" which doesn't use bin format family and str 8 format. Defaults to false.
112
112
  - `disableTimestampEncoding`, a boolean that when set disables the encoding of Dates into the [timestamp extension type](https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type). Defaults to false.
113
113
  - `preferMap`, a boolean that forces all maps to be decoded to `Map`s rather than plain objects. This ensures that `decode(encode(new Map())) instanceof Map` and that iteration order is preserved. Defaults to false.
114
114
  - `protoAction`, a string which can be `error|ignore|remove` that determines what happens when decoding a plain object with a `__proto__` property which would cause prototype poisoning. `error` (default) throws an error, `remove` removes the property, `ignore` (not recommended) allows the property, thereby causing prototype poisoning on the decoded object.
package/lib/encoder.js CHANGED
@@ -22,7 +22,8 @@ module.exports = function buildEncode (encodingTypes, options) {
22
22
  }
23
23
  // weird hack to support Buffer
24
24
  // and Buffer-like objects
25
- return bl([getBufferHeader(obj.length), obj])
25
+ const _getBufferHeader = options.compatibilityMode ? getCompatibleBufferHeader : getBufferHeader
26
+ return bl([_getBufferHeader(obj.length), obj])
26
27
  }
27
28
  if (Array.isArray(obj)) return encodeArray(obj, encode)
28
29
  if (typeof obj === 'object') return encodeExt(obj, encodingTypes) || encodeObject(obj, options, encode)
@@ -225,6 +226,26 @@ function getBufferHeader (length) {
225
226
  return header
226
227
  }
227
228
 
229
+ function getCompatibleBufferHeader (length) {
230
+ let header
231
+ if (length <= 0x1f) {
232
+ // fix raw header: 101XXXXX
233
+ header = Buffer.allocUnsafe(1)
234
+ header[0] = 0xa0 | length
235
+ } else if (length <= 0xffff) {
236
+ // raw 16 header: 0xda, XXXXXXXX, XXXXXXXX
237
+ header = Buffer.allocUnsafe(3)
238
+ header[0] = 0xda
239
+ header.writeUInt16BE(length, 1)
240
+ } else {
241
+ // raw 32 header: 0xdb, XXXXXXXX, XXXXXXXX, XXXXXXXX, XXXXXXXX
242
+ header = Buffer.allocUnsafe(5)
243
+ header[0] = 0xdb
244
+ header.writeUInt32BE(length, 1)
245
+ }
246
+ return header
247
+ }
248
+
228
249
  function encodeNumber (obj, options) {
229
250
  let buf
230
251
  if (isFloat(obj)) return encodeFloat(obj, options.forceFloat64)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msgpack5",
3
- "version": "6.0.1",
3
+ "version": "6.0.2",
4
4
  "description": "A msgpack v5 implementation for node.js and the browser, with extension points",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -3,6 +3,13 @@
3
3
  const test = require('tape').test
4
4
  const msgpack = require('../')
5
5
 
6
+ function buildBuffer (size) {
7
+ const buf = Buffer.allocUnsafe(size)
8
+ buf.fill('a')
9
+
10
+ return buf
11
+ }
12
+
6
13
  test('encode/compatibility mode', function (t) {
7
14
  const compatEncoder = msgpack({
8
15
  compatibilityMode: true
@@ -37,4 +44,30 @@ test('encode/compatibility mode', function (t) {
37
44
  t.deepEqual(buf1, buf2, 'must be equal for two byte strings')
38
45
  t.end()
39
46
  })
47
+
48
+ const fixRawBuffer = buildBuffer(1)
49
+ const raw16Buffer = buildBuffer(Math.pow(2, 16) - 1)
50
+ const raw32Buffer = buildBuffer(Math.pow(2, 16) + 1)
51
+
52
+ t.test('compat. encoding a Buffer of length ' + fixRawBuffer.length, function (t) {
53
+ // fix raw header: 0xa0 | 1 = 0xa1
54
+ const buf = compatEncoder.encode(fixRawBuffer)
55
+ t.equal(buf[0], 0xa1, 'must have the proper header (fix raw)')
56
+ t.equal(buf.toString('utf8', 1, Buffer.byteLength(fixRawBuffer) + 1), fixRawBuffer.toString('utf8'), 'must decode correctly')
57
+ t.end()
58
+ })
59
+
60
+ t.test('compat. encoding a Buffer of length ' + raw16Buffer.length, function (t) {
61
+ const buf = compatEncoder.encode(raw16Buffer)
62
+ t.equal(buf[0], 0xda, 'must have the proper header (raw 16)')
63
+ t.equal(buf.toString('utf8', 3, Buffer.byteLength(raw16Buffer) + 3), raw16Buffer.toString('utf8'), 'must decode correctly')
64
+ t.end()
65
+ })
66
+
67
+ t.test('compat. encoding a Buffer of length ' + raw32Buffer.length, function (t) {
68
+ const buf = compatEncoder.encode(raw32Buffer)
69
+ t.equal(buf[0], 0xdb, 'must have the proper header (raw 32)')
70
+ t.equal(buf.toString('utf8', 5, Buffer.byteLength(raw32Buffer) + 5), raw32Buffer.toString('utf8'), 'must decode correctly')
71
+ t.end()
72
+ })
40
73
  })