msgpack5 4.1.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/lib/encoder.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  var Buffer = require('safe-buffer').Buffer
4
4
  var bl = require('bl')
5
- var TOLERANCE = 0.1
6
5
 
7
6
  module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilityMode, disableTimestampEncoding) {
8
7
  function encode (obj, avoidSlice) {
@@ -301,22 +300,35 @@ function write64BitInt (buf, offset, num) {
301
300
  }
302
301
 
303
302
  function isFloat (n) {
304
- return n !== Math.floor(n)
303
+ return n % 1 !== 0
305
304
  }
306
305
 
307
306
  function encodeFloat (obj, forceFloat64) {
308
- var buf
307
+ var useDoublePrecision = true
308
+
309
+ // If `fround` is supported, we can check if a float
310
+ // is double or single precision by rounding the object
311
+ // to single precision and comparing the difference.
312
+ // If it's not supported, it's safer to use a 64 bit
313
+ // float so we don't lose precision without meaning to.
314
+ if (Math.fround) {
315
+ useDoublePrecision = Math.fround(obj) !== obj
316
+ }
309
317
 
310
- buf = Buffer.allocUnsafe(5)
311
- buf[0] = 0xca
312
- buf.writeFloatBE(obj, 1)
318
+ if (forceFloat64) {
319
+ useDoublePrecision = true
320
+ }
321
+
322
+ var buf
313
323
 
314
- // FIXME is there a way to check if a
315
- // value fits in a float?
316
- if (forceFloat64 || Math.abs(obj - buf.readFloatBE(1)) > TOLERANCE) {
324
+ if (useDoublePrecision) {
317
325
  buf = Buffer.allocUnsafe(9)
318
326
  buf[0] = 0xcb
319
327
  buf.writeDoubleBE(obj, 1)
328
+ } else {
329
+ buf = Buffer.allocUnsafe(5)
330
+ buf[0] = 0xca
331
+ buf.writeFloatBE(obj, 1)
320
332
  }
321
333
 
322
334
  return buf
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msgpack5",
3
- "version": "4.1.0",
3
+ "version": "4.2.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": {
package/test/floats.js CHANGED
@@ -7,29 +7,67 @@ var bl = require('bl')
7
7
 
8
8
  test('encoding/decoding 32-bits float numbers', function (t) {
9
9
  var encoder = msgpack()
10
- var allNum = []
10
+ var float32 = [
11
+ 1.5,
12
+ 0.15625,
13
+ -2.5
14
+ ]
11
15
 
12
- allNum.push(-222.42)
13
- allNum.push(748364.2)
14
- allNum.push(2.2)
16
+ var float64 = [
17
+ 2 ** 150,
18
+ 1.337,
19
+ 2.2
20
+ ]
15
21
 
16
- allNum.forEach(function (num) {
22
+ float64.forEach(function (num) {
23
+ t.test('encoding ' + num, function (t) {
24
+ var buf = encoder.encode(num)
25
+ t.equal(buf.length, 9, 'must have 5 bytes')
26
+ t.equal(buf[0], 0xcb, 'must have the proper header')
27
+
28
+ var dec = buf.readDoubleBE(1)
29
+ t.equal(dec, num, 'must decode correctly')
30
+ t.end()
31
+ })
32
+
33
+ t.test('decoding ' + num, function (t) {
34
+ var buf = Buffer.allocUnsafe(9)
35
+ var dec
36
+ buf[0] = 0xcb
37
+ buf.writeDoubleBE(num, 1)
38
+
39
+ dec = encoder.decode(buf)
40
+ t.equal(dec, num, 'must decode correctly')
41
+ t.end()
42
+ })
43
+
44
+ t.test('mirror test ' + num, function (t) {
45
+ var dec = encoder.decode(encoder.encode(num))
46
+ t.equal(dec, num, 'must decode correctly')
47
+ t.end()
48
+ })
49
+ })
50
+
51
+ float32.forEach(function (num) {
17
52
  t.test('encoding ' + num, function (t) {
18
53
  var buf = encoder.encode(num)
19
- var dec = buf.readFloatBE(1)
20
54
  t.equal(buf.length, 5, 'must have 5 bytes')
21
55
  t.equal(buf[0], 0xca, 'must have the proper header')
22
- t.true(Math.abs(dec - num) < 0.1, 'must decode correctly')
56
+
57
+ var dec = buf.readFloatBE(1)
58
+ t.equal(dec, num, 'must decode correctly')
23
59
  t.end()
24
60
  })
25
61
 
26
62
  t.test('forceFloat64 encoding ' + num, function (t) {
27
63
  var enc = msgpack({ forceFloat64: true })
28
64
  var buf = enc.encode(num)
29
- var dec = buf.readDoubleBE(1)
65
+
30
66
  t.equal(buf.length, 9, 'must have 9 bytes')
31
67
  t.equal(buf[0], 0xcb, 'must have the proper header')
32
- t.true(Math.abs(dec - num) < 0.1, 'must decode correctly')
68
+
69
+ var dec = buf.readDoubleBE(1)
70
+ t.equal(dec, num, 'must decode correctly')
33
71
  t.end()
34
72
  })
35
73
 
@@ -38,14 +76,15 @@ test('encoding/decoding 32-bits float numbers', function (t) {
38
76
  var dec
39
77
  buf[0] = 0xca
40
78
  buf.writeFloatBE(num, 1)
79
+
41
80
  dec = encoder.decode(buf)
42
- t.true(Math.abs(dec - num) < 0.1, 'must decode correctly')
81
+ t.equal(dec, num, 'must decode correctly')
43
82
  t.end()
44
83
  })
45
84
 
46
85
  t.test('mirror test ' + num, function (t) {
47
86
  var dec = encoder.decode(encoder.encode(num))
48
- t.true(Math.abs(dec - num) < 0.1, 'must decode correctly')
87
+ t.equal(dec, num, 'must decode correctly')
49
88
  t.end()
50
89
  })
51
90
  })
@@ -65,3 +104,16 @@ test('decoding an incomplete 32-bits float numbers', function (t) {
65
104
  t.equals(buf.length, origLength, 'must not consume any byte')
66
105
  t.end()
67
106
  })
107
+
108
+ test('decoding an incomplete 64-bits float numbers', function (t) {
109
+ var encoder = msgpack()
110
+ var buf = Buffer.allocUnsafe(8)
111
+ buf[0] = 0xcb
112
+ buf = bl().append(buf)
113
+ var origLength = buf.length
114
+ t.throws(function () {
115
+ encoder.decode(buf)
116
+ }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError')
117
+ t.equals(buf.length, origLength, 'must not consume any byte')
118
+ t.end()
119
+ })