msgpack5 4.0.0 → 4.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/.travis.yml CHANGED
@@ -5,3 +5,4 @@ node_js:
5
5
  - "7"
6
6
  - "8"
7
7
  - "9"
8
+ - "10"
package/README.md CHANGED
@@ -177,17 +177,25 @@ This is just a commodity that calls
177
177
  -------------------------------------------------------
178
178
  <a name="encoder"></a>
179
179
 
180
- ### encoder()
180
+ ### encoder(options)
181
181
 
182
182
  Builds a stream in object mode that encodes msgpack.
183
183
 
184
+ Supported options:
185
+ * `wrap`, objects should be passed to encoder in wrapped object {value: data}. Wrap option should be used if you need to pass null to encoder.
186
+
187
+
184
188
  -------------------------------------------------------
185
189
  <a name="decoder"></a>
186
190
 
187
- ### decoder()
191
+ ### decoder(options)
188
192
 
189
193
  Builds a stream in object mode that decodes msgpack.
190
194
 
195
+ Supported options:
196
+ * `wrap`, decoded objects returned in wrapped object {value: data}. Wrap option should be used if stream contains msgpack nil.
197
+
198
+
191
199
  LevelUp Support
192
200
  ---------------
193
201
 
package/dist/msgpack5.js CHANGED
@@ -14,7 +14,8 @@ function msgpack (options) {
14
14
 
15
15
  options = options || {
16
16
  forceFloat64: false,
17
- compatibilityMode: false
17
+ compatibilityMode: false,
18
+ disableTimestampEncoding: false // if true, skips encoding Dates using the msgpack timestamp ext format (-1)
18
19
  }
19
20
 
20
21
  function registerEncoder (check, encode) {
@@ -68,7 +69,7 @@ function msgpack (options) {
68
69
  }
69
70
 
70
71
  return {
71
- encode: buildEncode(encodingTypes, options.forceFloat64, options.compatibilityMode),
72
+ encode: buildEncode(encodingTypes, options.forceFloat64, options.compatibilityMode, options.disableTimestampEncoding),
72
73
  decode: buildDecode(decodingTypes),
73
74
  register: register,
74
75
  registerEncoder: registerEncoder,
@@ -459,16 +460,50 @@ module.exports = function buildDecode (decodingTypes) {
459
460
  }
460
461
 
461
462
  function decodeFixExt (buf, offset, size) {
462
- var type = buf.readUInt8(offset + 1)
463
-
463
+ var type = buf.readInt8(offset + 1) // Signed
464
464
  return decodeExt(buf, offset, type, size, 2)
465
465
  }
466
+ function decodeTimestamp (buf, size, headerSize) {
467
+ var seconds, nanoseconds
468
+ nanoseconds = 0
469
+
470
+ switch (size) {
471
+ case 4:
472
+ // timestamp 32 stores the number of seconds that have elapsed since 1970-01-01 00:00:00 UTC in an 32-bit unsigned integer
473
+ seconds = buf.readUInt32BE()
474
+ break
475
+
476
+ case 8: // Timestamp 64 stores the number of seconds and nanoseconds that have elapsed
477
+ // since 1970-01-01 00:00:00 UTC in 32-bit unsigned integers, split 30/34 bits
478
+ var upper = buf.readUInt32BE()
479
+ var lower = buf.readUInt32BE(4)
480
+ nanoseconds = upper / 4
481
+ seconds = ((upper & 0x03) * Math.pow(2, 32)) + lower // If we use bitwise operators, we get truncated to 32bits
482
+ break
483
+
484
+ case 12:
485
+ throw new Error('timestamp 96 is not yet implemented')
486
+ }
487
+
488
+ var millis = (seconds * 1000) + Math.round(nanoseconds / 1E6)
489
+ return buildDecodeResult(new Date(millis), size + headerSize)
490
+ }
466
491
 
467
492
  function decodeExt (buf, offset, type, size, headerSize) {
468
493
  var i,
469
494
  toDecode
470
495
 
471
496
  offset += headerSize
497
+
498
+ // Pre-defined
499
+ if (type < 0) { // Reserved for future extensions
500
+ switch (type) {
501
+ case -1: // Tiemstamp https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
502
+ toDecode = buf.slice(offset, offset + size)
503
+ return decodeTimestamp(toDecode, size, headerSize)
504
+ }
505
+ }
506
+
472
507
  for (i = 0; i < decodingTypes.length; i++) {
473
508
  if (type === decodingTypes[i].type) {
474
509
  toDecode = buf.slice(offset, offset + size)
@@ -490,7 +525,7 @@ var Buffer = require('safe-buffer').Buffer
490
525
  var bl = require('bl')
491
526
  var TOLERANCE = 0.1
492
527
 
493
- module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilityMode) {
528
+ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilityMode, disableTimestampEncoding) {
494
529
  function encode (obj, avoidSlice) {
495
530
  var buf,
496
531
  len
@@ -531,7 +566,10 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
531
566
  buf.writeUInt32BE(len, 1)
532
567
  buf.write(obj, 5)
533
568
  }
534
- } else if (obj && obj.readUInt32LE) {
569
+ } else if (obj && (obj.readUInt32LE || obj instanceof Uint8Array)) {
570
+ if (obj instanceof Uint8Array) {
571
+ obj = Buffer.from(obj)
572
+ }
535
573
  // weird hack to support Buffer
536
574
  // and Buffer-like objects
537
575
  if (obj.length <= 0xff) {
@@ -567,6 +605,8 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
567
605
  acc.append(encode(obj, true))
568
606
  return acc
569
607
  }, bl().append(buf))
608
+ } else if (!disableTimestampEncoding && typeof obj.getDate === 'function') {
609
+ return encodeDate(obj)
570
610
  } else if (typeof obj === 'object') {
571
611
  buf = encodeExt(obj) || encodeObject(obj)
572
612
  } else if (typeof obj === 'number') {
@@ -632,6 +672,35 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
632
672
  }
633
673
  }
634
674
 
675
+ function encodeDate (dt) {
676
+ var encoded
677
+ var millis = dt * 1
678
+ var seconds = Math.floor(millis / 1000)
679
+ var nanos = (millis - (seconds * 1000)) * 1E6
680
+
681
+ if (nanos || seconds > 0xFFFFFFFF) {
682
+ // Timestamp64
683
+ encoded = new Buffer(10)
684
+ encoded[0] = 0xd7
685
+ encoded[1] = -1
686
+
687
+ var upperNanos = ((nanos * 4))
688
+ var upperSeconds = seconds / Math.pow(2, 32)
689
+ var upper = (upperNanos + upperSeconds) & 0xFFFFFFFF
690
+ var lower = seconds & 0xFFFFFFFF
691
+
692
+ encoded.writeInt32BE(upper, 2)
693
+ encoded.writeInt32BE(lower, 6)
694
+ } else {
695
+ // Timestamp32
696
+ encoded = new Buffer(6)
697
+ encoded[0] = 0xd6
698
+ encoded[1] = -1
699
+ encoded.writeUInt32BE(Math.floor(millis / 1000), 2)
700
+ }
701
+ return bl().append(encoded)
702
+ }
703
+
635
704
  function encodeExt (obj) {
636
705
  var i
637
706
  var encoded
@@ -1757,7 +1826,7 @@ module.exports = BufferList
1757
1826
  /*!
1758
1827
  * The buffer module from node.js, for the browser.
1759
1828
  *
1760
- * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
1829
+ * @author Feross Aboukhadijeh <https://feross.org>
1761
1830
  * @license MIT
1762
1831
  */
1763
1832
  /* eslint-disable no-proto */
@@ -1860,7 +1929,7 @@ function from (value, encodingOrOffset, length) {
1860
1929
  throw new TypeError('"value" argument must not be a number')
1861
1930
  }
1862
1931
 
1863
- if (value instanceof ArrayBuffer) {
1932
+ if (isArrayBuffer(value)) {
1864
1933
  return fromArrayBuffer(value, encodingOrOffset, length)
1865
1934
  }
1866
1935
 
@@ -2120,7 +2189,7 @@ function byteLength (string, encoding) {
2120
2189
  if (Buffer.isBuffer(string)) {
2121
2190
  return string.length
2122
2191
  }
2123
- if (isArrayBufferView(string) || string instanceof ArrayBuffer) {
2192
+ if (isArrayBufferView(string) || isArrayBuffer(string)) {
2124
2193
  return string.byteLength
2125
2194
  }
2126
2195
  if (typeof string !== 'string') {
@@ -3452,6 +3521,14 @@ function blitBuffer (src, dst, offset, length) {
3452
3521
  return i
3453
3522
  }
3454
3523
 
3524
+ // ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check
3525
+ // but they should be treated as valid. See: https://github.com/feross/buffer/issues/166
3526
+ function isArrayBuffer (obj) {
3527
+ return obj instanceof ArrayBuffer ||
3528
+ (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' &&
3529
+ typeof obj.byteLength === 'number')
3530
+ }
3531
+
3455
3532
  // Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView`
3456
3533
  function isArrayBufferView (obj) {
3457
3534
  return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj)
@@ -3991,7 +4068,7 @@ if (typeof Object.create === 'function') {
3991
4068
  /*!
3992
4069
  * Determine if an object is a Buffer
3993
4070
  *
3994
- * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
4071
+ * @author Feross Aboukhadijeh <https://feross.org>
3995
4072
  * @license MIT
3996
4073
  */
3997
4074