Comparing version 5.0.0 to 5.1.0
@@ -5,3 +5,2 @@ 'use strict' | ||
var bl = require('bl') | ||
var isNaN = require('./helpers.js').isNaN | ||
var isFloat = require('./helpers.js').isFloat | ||
@@ -12,3 +11,2 @@ | ||
if (obj === undefined) throw new Error('undefined is not encodable in msgpack!') | ||
if (isNaN(obj)) throw new Error('NaN is not encodable in msgpack!') | ||
@@ -31,3 +29,3 @@ if (obj === null) return Buffer.from([ 0xc0 ]) | ||
} | ||
if (Array.isArray(obj)) return bl().append([ getHeader(obj.length, 0x90, 0xdc), ...obj.map(encode) ]) | ||
if (Array.isArray(obj)) return encodeArray(obj, encode) | ||
if (typeof obj === 'object') return encodeExt(obj, encodingTypes) || encodeObject(obj, options, encode) | ||
@@ -50,2 +48,18 @@ if (typeof obj === 'number') return encodeNumber(obj, options) | ||
function encodeArray (array, encode) { | ||
const acc = [ getHeader(array.length, 0x90, 0xdc) ] | ||
// This has to be forEach; Array.prototype.map preserves missing values and | ||
// Array.prototype.values yields them as undefined | ||
array.forEach(item => { | ||
acc.push(encode(item)) | ||
}) | ||
if (acc.length !== array.length + 1) { | ||
throw new Error('Sparse arrays are not encodable in msgpack') | ||
} | ||
return bl(acc) | ||
} | ||
function encodeMap (map, options, encode) { | ||
@@ -124,3 +138,3 @@ const acc = [ getHeader(map.size, 0x80, 0xde) ] | ||
if (forceFloat64 || !fround || fround(obj) !== obj) { | ||
if (forceFloat64 || !fround || !Object.is(fround(obj), obj)) { | ||
buf = Buffer.allocUnsafe(9) | ||
@@ -127,0 +141,0 @@ buf[0] = 0xcb |
@@ -21,7 +21,1 @@ 'use strict' | ||
} | ||
exports.isNaN = function isNaN (n) { | ||
/* eslint-disable no-self-compare */ | ||
return n !== n && typeof n === 'number' | ||
/* eslint-enable no-self-compare */ | ||
} |
{ | ||
"name": "msgpack5", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "A msgpack v5 implementation for node.js and the browser, with extension points", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -74,9 +74,17 @@ 'use strict' | ||
test('throw error on NaN in a map', function (t) { | ||
test('encode NaN in a map', function (t) { | ||
var instance = msgpack() | ||
var toEncode = { a: NaN, hello: 'world' } | ||
t.throws(function () { | ||
instance.encode(toEncode) | ||
}, Error, 'must throw Error') | ||
const buf = instance.encode(toEncode) | ||
t.assert(Object.is(instance.decode(buf).a, NaN)) | ||
const expected = {...toEncode} | ||
delete toEncode.a | ||
const actual = instance.decode(buf) | ||
delete buf.a | ||
t.deepEqual(actual, expected) | ||
t.end() | ||
@@ -83,0 +91,0 @@ }) |
@@ -6,10 +6,48 @@ 'use strict' | ||
test('encode NaN', function (t) { | ||
var encoder = msgpack() | ||
test('encode NaN as 32-bit float', function (t) { | ||
const encoder = msgpack() | ||
t.throws(function () { | ||
encoder.encode(NaN) | ||
}, Error, 'must throw Error') | ||
const buf = encoder.encode(NaN) | ||
t.equal(buf[0], 0xca) | ||
t.equal(buf.byteLength, 5) | ||
t.end() | ||
}) | ||
test('encode NaN as 64-bit float with forceFloat64', function (t) { | ||
const encoder = msgpack({forceFloat64: true}) | ||
const buf = encoder.encode(NaN) | ||
t.equal(buf[0], 0xcb) | ||
t.equal(buf.byteLength, 9) | ||
t.end() | ||
}) | ||
test('round-trip 32-bit NaN', function (t) { | ||
const encoder = msgpack() | ||
t.assert(Object.is(encoder.decode(encoder.encode(NaN)), NaN)) | ||
t.end() | ||
}) | ||
test('round-trip 64-bit NaN with forceFloat64', function (t) { | ||
const encoder = msgpack({forceFloat64: true}) | ||
t.assert(Object.is(encoder.decode(encoder.encode(NaN)), NaN)) | ||
t.end() | ||
}) | ||
test('decode 64-bit NaN', function (t) { | ||
const encoder = msgpack() | ||
const buf = Buffer.alloc(9) | ||
buf.writeUInt8(0xcb, 0) | ||
buf.writeDoubleBE(NaN, 1) | ||
t.assert(Object.is(encoder.decode(buf), NaN)) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
520099
68
10188