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 +21 -9
- package/package.json +1 -1
- package/test/floats.js +63 -11
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 !==
|
|
303
|
+
return n % 1 !== 0
|
|
305
304
|
}
|
|
306
305
|
|
|
307
306
|
function encodeFloat (obj, forceFloat64) {
|
|
308
|
-
var
|
|
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
|
-
|
|
311
|
-
|
|
312
|
-
|
|
318
|
+
if (forceFloat64) {
|
|
319
|
+
useDoublePrecision = true
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
var buf
|
|
313
323
|
|
|
314
|
-
|
|
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
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
|
|
10
|
+
var float32 = [
|
|
11
|
+
1.5,
|
|
12
|
+
0.15625,
|
|
13
|
+
-2.5
|
|
14
|
+
]
|
|
11
15
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
var float64 = [
|
|
17
|
+
2 ** 150,
|
|
18
|
+
1.337,
|
|
19
|
+
2.2
|
|
20
|
+
]
|
|
15
21
|
|
|
16
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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
|
+
})
|