From 15333ce83de9297510ce89e0b06e7c056feee8e9 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 5 Mar 2021 11:37:18 +0100 Subject: [PATCH] Updated all dependencies (#96) --- benchmarks/encodedecode.js | 18 ++--- benchmarks/parseshortmap.js | 18 ++--- example.js | 20 ++--- index.js | 24 +++--- lib/codecs/DateCodec.js | 37 ++++----- lib/decoder.js | 38 +++++----- lib/encoder.js | 74 +++++++++--------- lib/helpers.js | 2 +- lib/streams.js | 12 +-- package.json | 12 +-- test/1-byte-length-buffers.js | 34 ++++----- test/1-byte-length-exts.js | 30 ++++---- test/1-byte-length-strings.js | 38 +++++----- test/1-byte-length-uint8arrays.js | 16 ++-- test/15-elements-arrays.js | 40 +++++----- test/15-elements-maps.js | 52 ++++++------- test/16-bits-signed-integers.js | 24 +++--- test/16-bits-unsigned-integers.js | 24 +++--- test/2-bytes-length-arrays.js | 40 +++++----- test/2-bytes-length-buffers.js | 34 ++++----- test/2-bytes-length-exts.js | 30 ++++---- test/2-bytes-length-maps.js | 38 +++++----- test/2-bytes-length-strings.js | 35 ++++----- test/2-bytes-length-uint8arrays.js | 16 ++-- test/31-chars-strings.js | 26 +++---- test/32-bits-signed-integers.js | 24 +++--- test/32-bits-unsigned-integers.js | 24 +++--- test/32-bytes-strings.js | 16 ++-- test/4-bytes-length-arrays.js | 36 ++++----- test/4-bytes-length-buffers.js | 34 ++++----- test/4-bytes-length-exts.js | 30 ++++---- test/4-bytes-length-strings.js | 35 ++++----- test/4-bytes-length-uint8arrays.js | 16 ++-- test/5-bits-negative-integers.js | 16 ++-- test/64-bits-signed-integers.js | 20 ++--- test/64-bits-unsigned-integers.js | 24 +++--- test/7-bits-positive-integers.js | 16 ++-- test/8-bits-positive-integers.js | 24 +++--- test/8-bits-signed-integers.js | 24 +++--- test/NaN.js | 8 +- test/booleans.js | 8 +- test/compatibility-mode.js | 20 ++--- test/datenull.js | 6 +- test/doubles.js | 29 ++++---- test/ext-custom-encode-check.js | 16 ++-- test/fixexts.js | 116 ++++++++++++++--------------- test/floats.js | 54 +++++++------- test/functions.js | 12 +-- test/levelup-encoding.js | 26 +++---- test/map-with-object-key.js | 12 +-- test/null.js | 8 +- test/numerictypeasserts.js | 10 +-- test/object-prototype-poisoning.js | 4 +- test/object-with-arrays.js | 38 +++++----- test/object-with-buffers.js | 20 ++--- test/object-with-many-keys.js | 34 ++++----- test/object-with-strings.js | 18 ++--- test/prefer-map.js | 10 +-- test/streams.js | 100 ++++++++++++------------- test/timestamps.js | 48 ++++++------ 60 files changed, 831 insertions(+), 837 deletions(-) diff --git a/benchmarks/encodedecode.js b/benchmarks/encodedecode.js index 2a9efda..384b8c1 100644 --- a/benchmarks/encodedecode.js +++ b/benchmarks/encodedecode.js @@ -1,11 +1,9 @@ -var msgpack = require('../')() -var msg = { hello: 'world' } -var encode = msgpack.encode -var decode = msgpack.decode -var max = 100000 -var start -var stop -var i +const msgpack = require('../')() +const msg = { hello: 'world' } +const encode = msgpack.encode +const decode = msgpack.decode +const max = 100000 +let i function run () { for (i = 0; i < max; i++) { @@ -16,8 +14,8 @@ function run () { // preheat run() -start = Date.now() +const start = Date.now() run() -stop = Date.now() +const stop = Date.now() console.log('time', stop - start) console.log('decode/s', max / (stop - start) * 1000) diff --git a/benchmarks/parseshortmap.js b/benchmarks/parseshortmap.js index b49befb..d9fded4 100644 --- a/benchmarks/parseshortmap.js +++ b/benchmarks/parseshortmap.js @@ -1,11 +1,9 @@ -var msgpack = require('../')() -var bl = require('bl') -var msg = bl(msgpack.encode({ hello: 'world' })) -var decode = msgpack.decode -var max = 1000000 -var start -var stop -var i +const msgpack = require('../')() +const bl = require('bl') +const msg = bl(msgpack.encode({ hello: 'world' })) +const decode = msgpack.decode +const max = 1000000 +let i function run () { for (i = 0; i < max; i++) { @@ -16,8 +14,8 @@ function run () { // preheat run() -start = Date.now() +const start = Date.now() run() -stop = Date.now() +const stop = Date.now() console.log('time', stop - start) console.log('decode/s', max / (stop - start) * 1000) diff --git a/example.js b/example.js index dfb8d11..048d232 100644 --- a/example.js +++ b/example.js @@ -1,16 +1,16 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var msgpack = require('./')() // namespace our extensions -var a = new MyType(2, 'a') -var encode = msgpack.encode -var decode = msgpack.decode +const Buffer = require('safe-buffer').Buffer +const msgpack = require('./')() // namespace our extensions +const a = new MyType(2, 'a') +const encode = msgpack.encode +const decode = msgpack.decode msgpack.register(0x42, MyType, mytipeEncode, mytipeDecode) -console.log(encode({ 'hello': 'world' }).toString('hex')) +console.log(encode({ hello: 'world' }).toString('hex')) // 81a568656c6c6fa5776f726c64 -console.log(decode(encode({ 'hello': 'world' }))) +console.log(decode(encode({ hello: 'world' }))) // { hello: 'world' } console.log(encode(a).toString('hex')) // d5426161 @@ -25,14 +25,14 @@ function MyType (size, value) { } function mytipeEncode (obj) { - var buf = Buffer.allocUnsafe(obj.size) + const buf = Buffer.allocUnsafe(obj.size) buf.fill(obj.value) return buf } function mytipeDecode (data) { - var result = new MyType(data.length, data.toString('utf8', 0, 1)) - var i + const result = new MyType(data.length, data.toString('utf8', 0, 1)) + let i for (i = 0; i < data.length; i++) { if (data.readUInt8(0) != data.readUInt8(i)) { // eslint-disable-line diff --git a/index.js b/index.js index 6557e35..b12620d 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,17 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var assert = require('assert') -var bl = require('bl') -var streams = require('./lib/streams') -var buildDecode = require('./lib/decoder') -var buildEncode = require('./lib/encoder') -var IncompleteBufferError = require('./lib/helpers.js').IncompleteBufferError -var DateCodec = require('./lib/codecs/DateCodec') +const Buffer = require('safe-buffer').Buffer +const assert = require('assert') +const bl = require('bl') +const streams = require('./lib/streams') +const buildDecode = require('./lib/decoder') +const buildEncode = require('./lib/encoder') +const IncompleteBufferError = require('./lib/helpers.js').IncompleteBufferError +const DateCodec = require('./lib/codecs/DateCodec') function msgpack (options) { - var encodingTypes = [] - var decodingTypes = new Map() + const encodingTypes = [] + const decodingTypes = new Map() options = options || { forceFloat64: false, @@ -56,8 +56,8 @@ function msgpack (options) { } function reEncode (obj) { - var buf = bl() - var header = Buffer.allocUnsafe(1) + const buf = bl() + const header = Buffer.allocUnsafe(1) header.writeInt8(type, 0) diff --git a/lib/codecs/DateCodec.js b/lib/codecs/DateCodec.js index 2c92fda..a073d7c 100644 --- a/lib/codecs/DateCodec.js +++ b/lib/codecs/DateCodec.js @@ -5,9 +5,9 @@ function encode (dt) { return } - var millis = dt * 1 - var seconds = Math.floor(millis / 1000) - var nanos = (millis - seconds * 1000) * 1e6 + const millis = dt * 1 + const seconds = Math.floor(millis / 1000) + const nanos = (millis - seconds * 1000) * 1e6 if (seconds < 0 || seconds > 0x400000000) { // Timestamp96 @@ -16,7 +16,7 @@ function encode (dt) { encoded.writeUInt32BE(nanos, 1) - var hex = '' + let hex = '' if (seconds >= 0) { const padhex = '0000000000000000' hex = seconds.toString(16) @@ -27,8 +27,8 @@ function encode (dt) { // reverse sign // keep all bits 0 and first 1 from right // reverse all other bits - var bin = (seconds * -1).toString(2) - var i = bin.length - 1 + let bin = (seconds * -1).toString(2) + let i = bin.length - 1 while (bin[i] === '0') { i-- } @@ -52,10 +52,10 @@ function encode (dt) { const encoded = Buffer.allocUnsafe(9) encoded[0] = -1 - var upperNanos = nanos * 4 - var upperSeconds = seconds / Math.pow(2, 32) - var upper = (upperNanos + upperSeconds) & 0xffffffff - var lower = seconds & 0xffffffff + const upperNanos = nanos * 4 + const upperSeconds = seconds / Math.pow(2, 32) + const upper = (upperNanos + upperSeconds) & 0xffffffff + const lower = seconds & 0xffffffff encoded.writeInt32BE(upper, 1) encoded.writeInt32BE(lower, 5) @@ -74,8 +74,11 @@ function check (obj) { } function decode (buf) { - var seconds - var nanoseconds = 0 + let seconds + let nanoseconds = 0 + let upper + let lower + let hex switch (buf.length) { case 4: @@ -86,8 +89,8 @@ function decode (buf) { case 8: // Timestamp 64 stores the number of seconds and nanoseconds that have elapsed // since 1970-01-01 00:00:00 UTC in 32-bit unsigned integers, split 30/34 bits - var upper = buf.readUInt32BE(0) - var lower = buf.readUInt32BE(4) + upper = buf.readUInt32BE(0) + lower = buf.readUInt32BE(4) nanoseconds = upper / 4 seconds = ((upper & 0x03) * Math.pow(2, 32)) + lower // If we use bitwise operators, we get truncated to 32bits break @@ -97,11 +100,11 @@ function decode (buf) { // since 1970-01-01 00:00:00 UTC in 64-bit signed integer and 32-bit unsigned integer // get seconds in hex - var hex = buf.toString('hex', 4, 12) + hex = buf.toString('hex', 4, 12) // check if seconds is a negative number if (parseInt(buf.toString('hex', 4, 6), 16) & 0x80) { // convert to binary - var bin = '' + let bin = '' const pad8 = '00000000' hex.match(/.{1,2}/g).forEach(function (byte) { byte = parseInt(byte, 16).toString(2) @@ -120,7 +123,7 @@ function decode (buf) { nanoseconds = buf.readUInt32BE(0) } - var millis = (seconds * 1000) + Math.round(nanoseconds / 1E6) + const millis = (seconds * 1000) + Math.round(nanoseconds / 1E6) return new Date(millis) } diff --git a/lib/decoder.js b/lib/decoder.js index d43d52c..a64130b 100644 --- a/lib/decoder.js +++ b/lib/decoder.js @@ -1,7 +1,7 @@ 'use strict' -var bl = require('bl') -var IncompleteBufferError = require('./helpers.js').IncompleteBufferError +const bl = require('bl') +const IncompleteBufferError = require('./helpers.js').IncompleteBufferError const SIZES = { 0xc4: 2, @@ -46,7 +46,7 @@ module.exports = function buildDecode (decodingTypes, options) { buf = bl().append(buf) } - var result = tryDecode(buf, 0) + const result = tryDecode(buf, 0) // Handle worst case ASAP and keep code flat if (!result) throw new IncompleteBufferError() @@ -58,7 +58,7 @@ module.exports = function buildDecode (decodingTypes, options) { if (buf.length <= initialOffset) return null const bufLength = buf.length - initialOffset - var offset = initialOffset + let offset = initialOffset const first = buf.readUInt8(offset) offset += 1 @@ -112,7 +112,7 @@ module.exports = function buildDecode (decodingTypes, options) { if (inRange(0xcc, 0xcf)) return decodeUnsignedInt(buf, offset, size - 1) if (inRange(0xd0, 0xd3)) return decodeSigned(buf, offset, size - 1) if (inRange(0xd4, 0xd8)) { - var type = buf.readInt8(offset) // Signed + const type = buf.readInt8(offset) // Signed offset += 1 return decodeExt(buf, offset, type, size - 2, 2) } @@ -152,12 +152,12 @@ module.exports = function buildDecode (decodingTypes, options) { } function decodeArray (buf, initialOffset, length, headerLength) { - var offset = initialOffset + let offset = initialOffset const result = [] - var i = 0 + let i = 0 while (i++ < length) { - var decodeResult = tryDecode(buf, offset) + const decodeResult = tryDecode(buf, offset) if (!decodeResult) return null result.push(decodeResult[0]) @@ -169,9 +169,9 @@ module.exports = function buildDecode (decodingTypes, options) { function decodeMap (buf, offset, length, headerLength, options) { const _temp = decodeArray(buf, offset, 2 * length, headerLength) if (!_temp) return null - const [ result, consumedBytes ] = _temp + const [result, consumedBytes] = _temp - var isPlainObject = !options.preferMap + let isPlainObject = !options.preferMap if (isPlainObject) { for (let i = 0; i < 2 * length; i += 2) { @@ -216,22 +216,22 @@ module.exports = function buildDecode (decodingTypes, options) { var negate = (buf[offset] & 0x80) == 0x80; // eslint-disable-line if (negate) { - var carry = 1 - for (var i = offset + 7; i >= offset; i--) { - var v = (buf[i] ^ 0xff) + carry + let carry = 1 + for (let i = offset + 7; i >= offset; i--) { + const v = (buf[i] ^ 0xff) + carry buf[i] = v & 0xff carry = v >> 8 } } - var hi = buf.readUInt32BE(offset + 0) - var lo = buf.readUInt32BE(offset + 4) + const hi = buf.readUInt32BE(offset + 0) + const lo = buf.readUInt32BE(offset + 4) return (hi * 4294967296 + lo) * (negate ? -1 : +1) } function decodeUnsignedInt (buf, offset, size) { const maxOffset = offset + size - var result = 0 + let result = 0 while (offset < maxOffset) { result += buf.readUInt8(offset++) * Math.pow(256, maxOffset - offset) } return [result, size + 1] } @@ -243,7 +243,7 @@ module.exports = function buildDecode (decodingTypes, options) { } function decodeSigned (buf, offset, size) { - var result + let result if (size === 1) result = buf.readInt8(offset) if (size === 2) result = buf.readInt16BE(offset) if (size === 4) result = buf.readInt32BE(offset) @@ -252,7 +252,7 @@ module.exports = function buildDecode (decodingTypes, options) { } function decodeFloat (buf, offset, size) { - var result + let result if (size === 4) result = buf.readFloatBE(offset) if (size === 8) result = buf.readDoubleBE(offset) return [result, size + 1] @@ -264,7 +264,7 @@ module.exports = function buildDecode (decodingTypes, options) { const decode = decodingTypes.get(type) if (!decode) throw new Error('unable to find ext type ' + type) - var value = decode(toDecode) + const value = decode(toDecode) return [value, headerSize + size] } } diff --git a/lib/encoder.js b/lib/encoder.js index c812e5c..098b954 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -1,16 +1,16 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var bl = require('bl') -var isFloat = require('./helpers.js').isFloat +const Buffer = require('safe-buffer').Buffer +const bl = require('bl') +const isFloat = require('./helpers.js').isFloat module.exports = function buildEncode (encodingTypes, options) { function encode (obj) { if (obj === undefined) throw new Error('undefined is not encodable in msgpack!') - if (obj === null) return Buffer.from([ 0xc0 ]) - if (obj === true) return Buffer.from([ 0xc3 ]) - if (obj === false) return Buffer.from([ 0xc2 ]) + if (obj === null) return Buffer.from([0xc0]) + if (obj === true) return Buffer.from([0xc3]) + if (obj === false) return Buffer.from([0xc2]) if (obj instanceof Map) return encodeMap(obj, options, encode) @@ -43,7 +43,7 @@ module.exports = function buildEncode (encodingTypes, options) { // function encodeArray (array, encode) { - const acc = [ getHeader(array.length, 0x90, 0xdc) ] + 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 @@ -59,8 +59,8 @@ function encodeArray (array, encode) { } function encodeMap (map, options, encode) { - const acc = [ getHeader(map.size, 0x80, 0xde) ] - const keys = [ ...map.keys() ] + const acc = [getHeader(map.size, 0x80, 0xde)] + const keys = [...map.keys()] if (!options.preferMap) { if (keys.every(item => typeof item === 'string')) { @@ -75,17 +75,17 @@ function encodeMap (map, options, encode) { } function encodeObject (obj, options, encode) { - var keys = [] + const keys = [] - for (let key in obj) { - if (obj.hasOwnProperty(key) && + for (const key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined && typeof obj[key] !== 'function') { keys.push(key) } } - const acc = [ getHeader(keys.length, 0x80, 0xde) ] + const acc = [getHeader(keys.length, 0x80, 0xde)] if (options.sortKeys) keys.sort() @@ -97,22 +97,22 @@ function encodeObject (obj, options, encode) { } function write64BitUint (buf, offset, num) { - var lo = num % 4294967296 - var hi = Math.floor(num / 4294967296) + const lo = num % 4294967296 + const hi = Math.floor(num / 4294967296) buf.writeUInt32BE(hi, offset + 0) buf.writeUInt32BE(lo, offset + 4) } function write64BitInt (buf, offset, num) { - var negate = num < 0 + const negate = num < 0 num = Math.abs(num) write64BitUint(buf, offset, num) if (negate) negate64BitInt(buf, offset) } function negate64BitInt (buf, offset) { - var i = offset + 8 + let i = offset + 8 // Optimization based on the fact that: // buf[i] == 0x00 => (buf[i] ^ 0xff) + 1 = 0x100 = 0x00 + 1 curry @@ -131,7 +131,7 @@ function negate64BitInt (buf, offset) { const fround = Math.fround function encodeFloat (obj, forceFloat64) { - var buf + let buf if (forceFloat64 || !fround || !Object.is(fround(obj), obj)) { buf = Buffer.allocUnsafe(9) @@ -152,25 +152,25 @@ function encodeExt (obj, encodingTypes) { const encoded = codec.encode(obj) if (!encoded) return null - return bl([ getExtHeader(encoded.length - 1), encoded ]) + return bl([getExtHeader(encoded.length - 1), encoded]) } function getExtHeader (length) { - if (length === 1) return Buffer.from([ 0xd4 ]) - if (length === 2) return Buffer.from([ 0xd5 ]) - if (length === 4) return Buffer.from([ 0xd6 ]) - if (length === 8) return Buffer.from([ 0xd7 ]) - if (length === 16) return Buffer.from([ 0xd8 ]) - - if (length < 256) return Buffer.from([ 0xc7, length ]) - if (length < 0x10000) return Buffer.from([ 0xc8, length >> 8, length & 0x00ff ]) - return Buffer.from([ 0xc9, length >> 24, (length >> 16) & 0x000000ff, (length >> 8) & 0x000000ff, length & 0x000000ff ]) + if (length === 1) return Buffer.from([0xd4]) + if (length === 2) return Buffer.from([0xd5]) + if (length === 4) return Buffer.from([0xd6]) + if (length === 8) return Buffer.from([0xd7]) + if (length === 16) return Buffer.from([0xd8]) + + if (length < 256) return Buffer.from([0xc7, length]) + if (length < 0x10000) return Buffer.from([0xc8, length >> 8, length & 0x00ff]) + return Buffer.from([0xc9, length >> 24, (length >> 16) & 0x000000ff, (length >> 8) & 0x000000ff, length & 0x000000ff]) } function getHeader (length, tag1, tag2) { - if (length < 16) return Buffer.from([ tag1 | length ]) + if (length < 16) return Buffer.from([tag1 | length]) const size = length < 0x10000 ? 2 : 4 - var buf = Buffer.allocUnsafe(1 + size) + const buf = Buffer.allocUnsafe(1 + size) buf[0] = length < 0x10000 ? tag2 : tag2 + 1 buf.writeUIntBE(length, 1, size) @@ -179,7 +179,7 @@ function getHeader (length, tag1, tag2) { function encodeString (obj, options) { const len = Buffer.byteLength(obj) - var buf + let buf if (len < 32) { buf = Buffer.allocUnsafe(1 + len) buf[0] = 0xa0 | len @@ -207,7 +207,7 @@ function encodeString (obj, options) { } function getBufferHeader (length) { - var header + let header if (length <= 0xff) { header = Buffer.allocUnsafe(2) header[0] = 0xc4 @@ -226,7 +226,7 @@ function getBufferHeader (length) { } function encodeNumber (obj, options) { - var buf + let buf if (isFloat(obj)) return encodeFloat(obj, options.forceFloat64) if (Math.abs(obj) > 9007199254740991) { return encodeFloat(obj, true) @@ -234,13 +234,13 @@ function encodeNumber (obj, options) { if (obj >= 0) { if (obj < 128) { - return Buffer.from([ obj ]) + return Buffer.from([obj]) } else if (obj < 256) { - return Buffer.from([ 0xcc, obj ]) + return Buffer.from([0xcc, obj]) } else if (obj < 65536) { - return Buffer.from([ 0xcd, 0xff & (obj >> 8), 0xff & (obj) ]) + return Buffer.from([0xcd, 0xff & (obj >> 8), 0xff & (obj)]) } else if (obj <= 0xffffffff) { - return Buffer.from([ 0xce, 0xff & (obj >> 24), 0xff & (obj >> 16), 0xff & (obj >> 8), 0xff & (obj) ]) + return Buffer.from([0xce, 0xff & (obj >> 24), 0xff & (obj >> 16), 0xff & (obj >> 8), 0xff & (obj)]) } else if (obj <= 9007199254740991) { buf = Buffer.allocUnsafe(9) buf[0] = 0xcf diff --git a/lib/helpers.js b/lib/helpers.js index e6e4224..26fdf21 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,6 +1,6 @@ 'use strict' -var util = require('util') +const util = require('util') exports.IncompleteBufferError = IncompleteBufferError diff --git a/lib/streams.js b/lib/streams.js index 489996e..714f798 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -1,8 +1,8 @@ 'use strict' -var Transform = require('readable-stream').Transform -var inherits = require('inherits') -var bl = require('bl') +const Transform = require('readable-stream').Transform +const inherits = require('inherits') +const bl = require('bl') function Base (opts) { opts = opts || {} @@ -31,7 +31,7 @@ function Encoder (opts) { inherits(Encoder, Base) Encoder.prototype._transform = function (obj, enc, done) { - var buf = null + let buf = null try { buf = this._msgpack.encode(this._wrap ? obj.value : obj).slice(0) @@ -65,9 +65,9 @@ Decoder.prototype._transform = function (buf, enc, done) { } try { - var result = this._msgpack.decode(this._chunks) + let result = this._msgpack.decode(this._chunks) if (this._wrap) { - result = {value: result} + result = { value: result } } this.push(result) } catch (err) { diff --git a/package.json b/package.json index d467300..6bc9173 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,12 @@ }, "homepage": "https://github.com/mcollina/msgpack5", "devDependencies": { - "browserify": "^16.2.0", + "browserify": "^17.0.0", "memdb": "^1.3.1", "pre-commit": "^1.2.2", - "standard": "^11.0.1", - "tap-mocha-reporter": "^3.0.7", - "tape": "^4.9.1", + "standard": "^16.0.0", + "tap-mocha-reporter": "^5.0.0", + "tape": "^5.0.0", "uglify-js": "^3.4.9" }, "standard": { @@ -44,9 +44,9 @@ ] }, "dependencies": { - "bl": "^2.0.1", + "bl": "^4.0.0", "inherits": "^2.0.3", - "readable-stream": "^2.3.6", + "readable-stream": "^3.0.0", "safe-buffer": "^5.1.2" } } diff --git a/test/1-byte-length-buffers.js b/test/1-byte-length-buffers.js index b65fe90..701b88d 100644 --- a/test/1-byte-length-buffers.js +++ b/test/1-byte-length-buffers.js @@ -1,22 +1,20 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size) { - var buf - - buf = Buffer.allocUnsafe(size) + const buf = Buffer.allocUnsafe(size) buf.fill('a') return buf } test('encode/decode 2^8-1 bytes buffers', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] all.push(build(Math.pow(2, 8) - 1)) all.push(build(Math.pow(2, 6) + 1)) @@ -25,7 +23,7 @@ test('encode/decode 2^8-1 bytes buffers', function (t) { all.forEach(function (orig) { t.test('encoding a buffer of length ' + orig.length, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 2 + orig.length, 'must have the right length') t.equal(buf.readUInt8(0), 0xc4, 'must have the proper header') t.equal(buf.readUInt8(1), orig.length, 'must include the buf length') @@ -34,7 +32,7 @@ test('encode/decode 2^8-1 bytes buffers', function (t) { }) t.test('decoding a buffer of length ' + orig.length, function (t) { - var buf = Buffer.allocUnsafe(2 + orig.length) + const buf = Buffer.allocUnsafe(2 + orig.length) buf[0] = 0xc4 buf[1] = orig.length orig.copy(buf, 2) @@ -52,14 +50,14 @@ test('encode/decode 2^8-1 bytes buffers', function (t) { }) test('decoding a chopped 2^8-1 bytes buffer', function (t) { - var encoder = msgpack() - var orig = build(Math.pow(2, 6)) - var buf = Buffer.allocUnsafe(2 + orig.length) + const encoder = msgpack() + const orig = build(Math.pow(2, 6)) + let buf = Buffer.allocUnsafe(2 + orig.length) buf[0] = 0xc4 buf[1] = Math.pow(2, 8) - 1 // set bigger size orig.copy(buf, 2) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -68,11 +66,11 @@ test('decoding a chopped 2^8-1 bytes buffer', function (t) { }) test('decoding an incomplete header of 2^8-1 bytes buffer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(1) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(1) buf[0] = 0xc4 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/1-byte-length-exts.js b/test/1-byte-length-exts.js index ff24ac3..6066bbc 100644 --- a/test/1-byte-length-exts.js +++ b/test/1-byte-length-exts.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode variable ext data up to 0xff', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (size, value) { this.value = value @@ -15,15 +15,15 @@ test('encode/decode variable ext data up to 0xff', function (t) { } function mytipeEncode (obj) { - var buf = Buffer.allocUnsafe(obj.size) + const buf = Buffer.allocUnsafe(obj.size) buf.fill(obj.value) return buf } function mytipeDecode (data) { - var result = new MyType(data.length, data.toString('utf8', 0, 1)) + const result = new MyType(data.length, data.toString('utf8', 0, 1)) - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { if (data.readUInt8(0) !== data.readUInt8(i)) { throw new Error('should all be the same') } @@ -56,7 +56,7 @@ test('encode/decode variable ext data up to 0xff', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj of length ' + orig.size, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 3 + orig.size, 'must have the right length') t.equal(buf.readUInt8(0), 0xc7, 'must have the ext header') t.equal(buf.readUInt8(1), orig.size, 'must include the data length') @@ -72,13 +72,13 @@ test('encode/decode variable ext data up to 0xff', function (t) { }) t.test('decoding an incomplete variable ext data up to 0xff', function (t) { - var obj = encoder.encode(new MyType(250, 'a')) - var buf = Buffer.allocUnsafe(obj.length) + const obj = encoder.encode(new MyType(250, 'a')) + let buf = Buffer.allocUnsafe(obj.length) buf[0] = 0xc7 buf.writeUInt8(obj.length + 2, 1) // set bigger size obj.copy(buf, 2, 2, obj.length) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -87,10 +87,10 @@ test('encode/decode variable ext data up to 0xff', function (t) { }) t.test('decoding an incomplete header of variable ext data up to 0xff', function (t) { - var buf = Buffer.allocUnsafe(2) + let buf = Buffer.allocUnsafe(2) buf[0] = 0xc7 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/1-byte-length-strings.js b/test/1-byte-length-strings.js index c79396a..d5efbe4 100644 --- a/test/1-byte-length-strings.js +++ b/test/1-byte-length-strings.js @@ -1,17 +1,18 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode 32 <-> (2^8-1) bytes strings', function (t) { - var encoder = msgpack() - var all = [] - var i + const encoder = msgpack() + const all = [] + let i // build base - for (i = 'a'; i.length < 32; i += 'a') { + for (i = 'a'; i.length < 32;) { + i += 'a' } for (; i.length < Math.pow(2, 8); i += 'aaaaa') { @@ -20,7 +21,7 @@ test('encode/decode 32 <-> (2^8-1) bytes strings', function (t) { all.forEach(function (str) { t.test('encoding a string of length ' + str.length, function (t) { - var buf = encoder.encode(str) + const buf = encoder.encode(str) t.equal(buf.length, 2 + Buffer.byteLength(str), 'must be the proper length') t.equal(buf.readUInt8(0), 0xd9, 'must have the proper header') t.equal(buf.readUInt8(1), Buffer.byteLength(str), 'must include the str length') @@ -29,7 +30,7 @@ test('encode/decode 32 <-> (2^8-1) bytes strings', function (t) { }) t.test('decoding a string of length ' + str.length, function (t) { - var buf = Buffer.allocUnsafe(2 + Buffer.byteLength(str)) + const buf = Buffer.allocUnsafe(2 + Buffer.byteLength(str)) buf[0] = 0xd9 buf[1] = Buffer.byteLength(str) buf.write(str, 2) @@ -47,16 +48,17 @@ test('encode/decode 32 <-> (2^8-1) bytes strings', function (t) { }) test('decoding a chopped string', function (t) { - var encoder = msgpack() - var str - for (str = 'a'; str.length < 40; str += 'a') { + const encoder = msgpack() + let str + for (str = 'a'; str.length < 40;) { + str += 'a' } - var buf = Buffer.allocUnsafe(2 + Buffer.byteLength(str)) + let buf = Buffer.allocUnsafe(2 + Buffer.byteLength(str)) buf[0] = 0xd9 buf[1] = Buffer.byteLength(str) + 10 // set bigger size buf.write(str, 2) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -65,11 +67,11 @@ test('decoding a chopped string', function (t) { }) test('decoding an incomplete header of a string', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(1) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(1) buf[0] = 0xd9 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/1-byte-length-uint8arrays.js b/test/1-byte-length-uint8arrays.js index 2985bf0..3b62539 100644 --- a/test/1-byte-length-uint8arrays.js +++ b/test/1-byte-length-uint8arrays.js @@ -1,12 +1,12 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') function build (size) { - var array = [] - var i + const array = [] + let i for (i = 0; i < size; i++) { array.push(42) @@ -16,8 +16,8 @@ function build (size) { } test('encode/decode 2^8-1 Uint8Arrays', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] all.push(build(Math.pow(2, 8) - 1)) all.push(build(Math.pow(2, 6) + 1)) @@ -26,7 +26,7 @@ test('encode/decode 2^8-1 Uint8Arrays', function (t) { all.forEach(function (array) { t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) { - var buf = encoder.encode(array) + const buf = encoder.encode(array) t.equal(buf.length, 2 + array.byteLength, 'must have the right length') t.equal(buf.readUInt8(0), 0xc4, 'must have the proper header') t.equal(buf.readUInt8(1), array.byteLength, 'must include the buf length') diff --git a/test/15-elements-arrays.js b/test/15-elements-arrays.js index e24e5e1..bf76904 100644 --- a/test/15-elements-arrays.js +++ b/test/15-elements-arrays.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size, obj) { - var array = [] - var i + const array = [] + let i for (i = 0; i < size; i++) { array.push(obj) @@ -17,8 +17,8 @@ function build (size, obj) { } function computeLength (array) { - var length = 1 // the header - var multi = 1 + let length = 1 // the header + let multi = 1 if (array[0] && typeof array[0] === 'string') { multi += array[0].length @@ -30,9 +30,9 @@ function computeLength (array) { } test('encode/decode arrays up to 15 elements', function (t) { - var encoder = msgpack() - var all = [] - var i + const encoder = msgpack() + const all = [] + let i for (i = 0; i < 16; i++) { all.push(build(i, 42)) @@ -44,7 +44,7 @@ test('encode/decode arrays up to 15 elements', function (t) { all.forEach(function (array) { t.test('encoding an array with ' + array.length + ' elements of ' + array[0], function (t) { - var buf = encoder.encode(array) + const buf = encoder.encode(array) // the array is full of 1-byte integers t.equal(buf.length, computeLength(array), 'must have the right length') t.equal(buf.readUInt8(0) & 0xf0, 0x90, 'must have the proper header') @@ -62,20 +62,20 @@ test('encode/decode arrays up to 15 elements', function (t) { }) test('decoding an incomplete array', function (t) { - var encoder = msgpack() + const encoder = msgpack() - var array = ['a', 'b', 'c'] - var size = computeLength(array) - var buf = Buffer.allocUnsafe(size) + const array = ['a', 'b', 'c'] + const size = computeLength(array) + let buf = Buffer.allocUnsafe(size) buf[0] = 0x90 | array.length + 2 // set bigger size - var pos = 1 - for (var i = 0; i < array.length; i++) { - var obj = encoder.encode(array[i], true) + let pos = 1 + for (let i = 0; i < array.length; i++) { + const obj = encoder.encode(array[i], true) obj.copy(buf, pos) pos += obj.length } buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/15-elements-maps.js b/test/15-elements-maps.js index 0c72545..8506c0f 100644 --- a/test/15-elements-maps.js +++ b/test/15-elements-maps.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size, value) { - var map = {} - var i + const map = {} + let i for (i = 0; i < size; i++) { map[i + 100 + ''] = value @@ -17,8 +17,8 @@ function build (size, value) { } function computeLength (map) { - var length = 1 // the header - var multi = 5 // we have 4 bytes for each key, plus 1 byte for the value + let length = 1 // the header + let multi = 5 // we have 4 bytes for each key, plus 1 byte for the value if (map[100] && typeof map[100] === 'string') { multi += map[100].length @@ -30,9 +30,9 @@ function computeLength (map) { } test('encode/decode maps up to 15 elements', function (t) { - var encoder = msgpack() - var all = [] - var i + const encoder = msgpack() + const all = [] + let i for (i = 0; i < 16; i++) { all.push(build(i, 42)) @@ -43,9 +43,9 @@ test('encode/decode maps up to 15 elements', function (t) { } all.forEach(function (map) { - var length = Object.keys(map).length + const length = Object.keys(map).length t.test('encoding a map with ' + length + ' elements of ' + map[100], function (t) { - var buf = encoder.encode(map) + const buf = encoder.encode(map) t.equal(buf.length, computeLength(map), 'must have the right length') t.equal(buf.readUInt8(0) & 0xf0, 0x80, 'must have the proper header') t.equal(buf.readUInt8(0) & 0x0f, length, 'must include the map length') @@ -62,24 +62,24 @@ test('encode/decode maps up to 15 elements', function (t) { }) test('do not encode undefined in a map', function (t) { - var instance = msgpack() - var expected = { hello: 'world' } - var toEncode = { a: undefined, hello: 'world' } - var buf = instance.encode(toEncode) + const instance = msgpack() + const expected = { hello: 'world' } + const toEncode = { a: undefined, hello: 'world' } + const buf = instance.encode(toEncode) t.deepEqual(expected, instance.decode(buf), 'must ignore undefined') t.end() }) test('encode NaN in a map', function (t) { - var instance = msgpack() - var toEncode = { a: NaN, hello: 'world' } + const instance = msgpack() + const toEncode = { a: NaN, hello: 'world' } const buf = instance.encode(toEncode) t.assert(Object.is(instance.decode(buf).a, NaN)) - const expected = {...toEncode} + const expected = { ...toEncode } delete toEncode.a const actual = instance.decode(buf) delete buf.a @@ -90,27 +90,27 @@ test('encode NaN in a map', function (t) { }) test('encode/decode map with buf, ints and strings', function (t) { - var map = { + const map = { topic: 'hello', qos: 1, payload: Buffer.from('world'), messageId: '42', ttl: 1416309270167 } - var pack = msgpack() + const pack = msgpack() t.deepEqual(pack.decode(pack.encode(map)), map) t.end() }) test('decoding a chopped map', function (t) { - var encoder = msgpack() - var map = encoder.encode({'a': 'b', 'c': 'd', 'e': 'f'}) - var buf = Buffer.allocUnsafe(map.length) + const encoder = msgpack() + const map = encoder.encode({ a: 'b', c: 'd', e: 'f' }) + let buf = Buffer.allocUnsafe(map.length) buf[0] = 0x80 | 5 // set bigger size map.copy(buf, 1, 1, map.length) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/16-bits-signed-integers.js b/test/16-bits-signed-integers.js index 0075a11..0b2a545 100644 --- a/test/16-bits-signed-integers.js +++ b/test/16-bits-signed-integers.js @@ -1,14 +1,14 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 16-bits big-endian signed integers', function (t) { - var encoder = msgpack() - var allNum = [] - var i + const encoder = msgpack() + const allNum = [] + let i for (i = 129; i < 32768; i += 1423) { allNum.push(-i) @@ -18,7 +18,7 @@ test('encoding/decoding 16-bits big-endian signed integers', function (t) { allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 3, 'must have 3 bytes') t.equal(buf[0], 0xd1, 'must have the proper header') t.equal(buf.readInt16BE(1), num, 'must decode correctly') @@ -26,7 +26,7 @@ test('encoding/decoding 16-bits big-endian signed integers', function (t) { }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(3) + const buf = Buffer.allocUnsafe(3) buf[0] = 0xd1 buf.writeInt16BE(num, 1) t.equal(encoder.decode(buf), num, 'must decode correctly') @@ -43,11 +43,11 @@ test('encoding/decoding 16-bits big-endian signed integers', function (t) { }) test('decoding an incomplete 16-bits big-endian integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(2) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(2) buf[0] = 0xd1 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/16-bits-unsigned-integers.js b/test/16-bits-unsigned-integers.js index 3864cd7..78d5908 100644 --- a/test/16-bits-unsigned-integers.js +++ b/test/16-bits-unsigned-integers.js @@ -1,14 +1,14 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 16-bits big-endian unsigned integers', function (t) { - var encoder = msgpack() - var allNum = [] - var i + const encoder = msgpack() + const allNum = [] + let i for (i = 256; i < 65536; i += 1423) { allNum.push(i) @@ -18,7 +18,7 @@ test('encoding/decoding 16-bits big-endian unsigned integers', function (t) { allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 3, 'must have 3 bytes') t.equal(buf[0], 0xcd, 'must have the proper header') t.equal(buf.readUInt16BE(1), num, 'must decode correctly') @@ -26,7 +26,7 @@ test('encoding/decoding 16-bits big-endian unsigned integers', function (t) { }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(3) + const buf = Buffer.allocUnsafe(3) buf[0] = 0xcd buf.writeUInt16BE(num, 1) t.equal(encoder.decode(buf), num, 'must decode correctly') @@ -43,11 +43,11 @@ test('encoding/decoding 16-bits big-endian unsigned integers', function (t) { }) test('decoding an incomplete 16-bits big-endian unsigned integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(2) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(2) buf[0] = 0xcd buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/2-bytes-length-arrays.js b/test/2-bytes-length-arrays.js index 0847d39..5ff658b 100644 --- a/test/2-bytes-length-arrays.js +++ b/test/2-bytes-length-arrays.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size) { - var array = [] - var i + const array = [] + let i for (i = 0; i < size; i++) { array.push(42) @@ -17,9 +17,9 @@ function build (size) { } test('encode/decode arrays up to 0xffff elements', function (t) { - var encoder = msgpack() - var all = [] - var i + const encoder = msgpack() + const all = [] + let i for (i = 16; i < 0xffff; i += 4242) { all.push(build(i)) @@ -30,7 +30,7 @@ test('encode/decode arrays up to 0xffff elements', function (t) { all.forEach(function (array) { t.test('encoding an array with ' + array.length + ' elements', function (t) { - var buf = encoder.encode(array) + const buf = encoder.encode(array) // the array is full of 1-byte integers t.equal(buf.length, 3 + array.length, 'must have the right length') t.equal(buf.readUInt8(0), 0xdc, 'must have the proper header') @@ -48,20 +48,20 @@ test('encode/decode arrays up to 0xffff elements', function (t) { }) test('decoding an incomplete array', function (t) { - var encoder = msgpack() + const encoder = msgpack() - var array = build(0xffff / 2) - var buf = Buffer.allocUnsafe(3 + array.length) + const array = build(0xffff / 2) + let buf = Buffer.allocUnsafe(3 + array.length) buf[0] = 0xdc buf.writeUInt16BE(array.length + 10, 1) // set bigger size - var pos = 3 - for (var i = 0; i < array.length; i++) { - var obj = encoder.encode(array[i], true) + let pos = 3 + for (let i = 0; i < array.length; i++) { + const obj = encoder.encode(array[i], true) obj.copy(buf, pos) pos += obj.length } buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -70,12 +70,12 @@ test('decoding an incomplete array', function (t) { }) test('decoding an incomplete header', function (t) { - var encoder = msgpack() + const encoder = msgpack() - var buf = Buffer.allocUnsafe(2) + let buf = Buffer.allocUnsafe(2) buf[0] = 0xdc buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/2-bytes-length-buffers.js b/test/2-bytes-length-buffers.js index c1123ec..5fdc3ee 100644 --- a/test/2-bytes-length-buffers.js +++ b/test/2-bytes-length-buffers.js @@ -1,22 +1,20 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size) { - var buf - - buf = Buffer.allocUnsafe(size) + const buf = Buffer.allocUnsafe(size) buf.fill('a') return buf } test('encode/decode 2^16-1 bytes buffers', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] all.push(build(Math.pow(2, 8))) all.push(build(Math.pow(2, 8) + 1)) @@ -25,7 +23,7 @@ test('encode/decode 2^16-1 bytes buffers', function (t) { all.forEach(function (orig) { t.test('encoding a buffer of length ' + orig.length, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 3 + orig.length, 'must have the right length') t.equal(buf.readUInt8(0), 0xc5, 'must have the proper header') t.equal(buf.readUInt16BE(1), orig.length, 'must include the buf length') @@ -34,7 +32,7 @@ test('encode/decode 2^16-1 bytes buffers', function (t) { }) t.test('decoding a buffer of length ' + orig.length, function (t) { - var buf = Buffer.allocUnsafe(3 + orig.length) + const buf = Buffer.allocUnsafe(3 + orig.length) buf[0] = 0xc5 buf.writeUInt16BE(orig.length, 1) orig.copy(buf, 3) @@ -52,14 +50,14 @@ test('encode/decode 2^16-1 bytes buffers', function (t) { }) test('decoding a chopped 2^16-1 bytes buffer', function (t) { - var encoder = msgpack() - var orig = build(Math.pow(2, 12)) - var buf = Buffer.allocUnsafe(3 + orig.length) + const encoder = msgpack() + const orig = build(Math.pow(2, 12)) + let buf = Buffer.allocUnsafe(3 + orig.length) buf[0] = 0xc5 buf[1] = Math.pow(2, 16) - 1 // set bigger size orig.copy(buf, 3) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -68,11 +66,11 @@ test('decoding a chopped 2^16-1 bytes buffer', function (t) { }) test('decoding an incomplete header of 2^16-1 bytes buffer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(2) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(2) buf[0] = 0xc5 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/2-bytes-length-exts.js b/test/2-bytes-length-exts.js index 49593c9..f5aefb6 100644 --- a/test/2-bytes-length-exts.js +++ b/test/2-bytes-length-exts.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (size, value) { this.value = value @@ -15,15 +15,15 @@ test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t } function mytipeEncode (obj) { - var buf = Buffer.allocUnsafe(obj.size) + const buf = Buffer.allocUnsafe(obj.size) buf.fill(obj.value) return buf } function mytipeDecode (data) { - var result = new MyType(data.length, data.toString('utf8', 0, 1)) + const result = new MyType(data.length, data.toString('utf8', 0, 1)) - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { if (data.readUInt8(0) !== data.readUInt8(i)) { throw new Error('should all be the same') } @@ -40,7 +40,7 @@ test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t all.forEach(function (orig) { t.test('encoding a custom obj of length ' + orig.size, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 4 + orig.size, 'must have the right length') t.equal(buf.readUInt8(0), 0xc8, 'must have the ext header') t.equal(buf.readUInt16BE(1), orig.size, 'must include the data length') @@ -56,13 +56,13 @@ test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t }) t.test('decoding an incomplete variable ext data up between 0x0100 and 0xffff', function (t) { - var obj = encoder.encode(new MyType(0xfff0, 'a')) - var buf = Buffer.allocUnsafe(obj.length) + const obj = encoder.encode(new MyType(0xfff0, 'a')) + let buf = Buffer.allocUnsafe(obj.length) buf[0] = 0xc8 buf.writeUInt16BE(obj.length + 2, 1) // set bigger size obj.copy(buf, 3, 3, obj.length) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -71,10 +71,10 @@ test('encode/decode variable ext data up between 0x0100 and 0xffff', function (t }) t.test('decoding an incomplete header of variable ext data up between 0x0100 and 0xffff', function (t) { - var buf = Buffer.allocUnsafe(3) + let buf = Buffer.allocUnsafe(3) buf[0] = 0xc8 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/2-bytes-length-maps.js b/test/2-bytes-length-maps.js index dbee52c..0df6321 100644 --- a/test/2-bytes-length-maps.js +++ b/test/2-bytes-length-maps.js @@ -1,15 +1,15 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') -var base = 100000 +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') +const base = 100000 function build (size, value) { - var map = {} + const map = {} - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { map[i + base] = value } @@ -17,8 +17,8 @@ function build (size, value) { } function computeLength (mapLength) { - var length = 3 // the header - var multi = ('' + base).length + 1 + 1 // we have bytes for each key, plus 1 byte for the value + let length = 3 // the header + const multi = ('' + base).length + 1 + 1 // we have bytes for each key, plus 1 byte for the value length += mapLength * multi @@ -26,11 +26,11 @@ function computeLength (mapLength) { } test('encode/decode maps up to 2^16-1 elements', function (t) { - var encoder = msgpack() + const encoder = msgpack() function doTest (length) { - var map = build(length, 42) - var buf = encoder.encode(map) + const map = build(length, 42) + const buf = encoder.encode(map) t.test('encoding a map with ' + length + ' elements of ' + map[base], function (t) { // the map is full of 1-byte integers @@ -56,14 +56,14 @@ test('encode/decode maps up to 2^16-1 elements', function (t) { }) test('decoding a chopped map', function (t) { - var encoder = msgpack() - var map = encoder.encode(build(Math.pow(2, 12) + 1, 42)) - var buf = Buffer.allocUnsafe(map.length) + const encoder = msgpack() + const map = encoder.encode(build(Math.pow(2, 12) + 1, 42)) + let buf = Buffer.allocUnsafe(map.length) buf[0] = 0xde buf.writeUInt16BE(Math.pow(2, 16) - 1, 1) // set bigger size map.copy(buf, 3, 3, map.length) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -72,11 +72,11 @@ test('decoding a chopped map', function (t) { }) test('decoding an incomplete header of a map', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(2) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(2) buf[0] = 0xde buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/2-bytes-length-strings.js b/test/2-bytes-length-strings.js index d03a5ad..ee9f8c1 100644 --- a/test/2-bytes-length-strings.js +++ b/test/2-bytes-length-strings.js @@ -1,14 +1,14 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode 2^8 <-> (2^16-1) bytes strings', function (t) { - var encoder = msgpack() - var all = [] - var str + const encoder = msgpack() + const all = [] + let str str = Buffer.allocUnsafe(Math.pow(2, 8)) str.fill('a') @@ -28,7 +28,7 @@ test('encode/decode 2^8 <-> (2^16-1) bytes strings', function (t) { all.forEach(function (str) { t.test('encoding a string of length ' + str.length, function (t) { - var buf = encoder.encode(str) + const buf = encoder.encode(str) t.equal(buf.length, 3 + Buffer.byteLength(str), 'must be the proper length') t.equal(buf[0], 0xda, 'must have the proper header') t.equal(buf.readUInt16BE(1), Buffer.byteLength(str), 'must include the str length') @@ -37,7 +37,7 @@ test('encode/decode 2^8 <-> (2^16-1) bytes strings', function (t) { }) t.test('decoding a string of length ' + str.length, function (t) { - var buf = Buffer.allocUnsafe(3 + Buffer.byteLength(str)) + const buf = Buffer.allocUnsafe(3 + Buffer.byteLength(str)) buf[0] = 0xda buf.writeUInt16BE(Buffer.byteLength(str), 1) buf.write(str, 3) @@ -55,16 +55,17 @@ test('encode/decode 2^8 <-> (2^16-1) bytes strings', function (t) { }) test('decoding a chopped string', function (t) { - var encoder = msgpack() - var str - for (str = 'a'; str.length < 0xff + 100; str += 'a') { + const encoder = msgpack() + let str + for (str = 'a'; str.length < 0xff + 100;) { + str += 'a' } - var buf = Buffer.allocUnsafe(3 + Buffer.byteLength(str)) + let buf = Buffer.allocUnsafe(3 + Buffer.byteLength(str)) buf[0] = 0xda buf.writeUInt16BE(Buffer.byteLength(str) + 10, 1) // set bigger size buf.write(str, 3) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -73,11 +74,11 @@ test('decoding a chopped string', function (t) { }) test('decoding an incomplete header of a string', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(2) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(2) buf[0] = 0xda buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/2-bytes-length-uint8arrays.js b/test/2-bytes-length-uint8arrays.js index f96788a..c824a30 100644 --- a/test/2-bytes-length-uint8arrays.js +++ b/test/2-bytes-length-uint8arrays.js @@ -1,12 +1,12 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') function build (size) { - var array = [] - var i + const array = [] + let i for (i = 0; i < size; i++) { array.push(42) @@ -16,8 +16,8 @@ function build (size) { } test('encode/decode 2^8-1 Uint8Arrays', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] all.push(build(Math.pow(2, 8))) all.push(build(Math.pow(2, 8) + 1)) @@ -26,7 +26,7 @@ test('encode/decode 2^8-1 Uint8Arrays', function (t) { all.forEach(function (array) { t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) { - var buf = encoder.encode(array) + const buf = encoder.encode(array) t.equal(buf.length, 3 + array.byteLength, 'must have the right length') t.equal(buf.readUInt8(0), 0xc5, 'must have the proper header') t.equal(buf.readUInt16BE(1), array.byteLength, 'must include the buf length') diff --git a/test/31-chars-strings.js b/test/31-chars-strings.js index bb97dd0..239aeb6 100644 --- a/test/31-chars-strings.js +++ b/test/31-chars-strings.js @@ -1,22 +1,22 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode strings with max 31 of length', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] // build base - for (var i = ''; i.length < 32; i += 'a') { + for (let i = ''; i.length < 32; i += 'a') { all.push(i) } all.forEach(function (str) { t.test('encoding a string of length ' + str.length, function (t) { - var buf = encoder.encode(str) + const buf = encoder.encode(str) t.equal(buf.length, 1 + Buffer.byteLength(str), 'must be the proper length') t.equal(buf.readUInt8(0) & 0xe0, 0xa0, 'must have the proper header') t.equal(buf.readUInt8(0) & 0x1f, Buffer.byteLength(str), 'must include the str length') @@ -25,7 +25,7 @@ test('encode/decode strings with max 31 of length', function (t) { }) t.test('decoding a string of length ' + str.length, function (t) { - var buf = Buffer.allocUnsafe(1 + Buffer.byteLength(str)) + const buf = Buffer.allocUnsafe(1 + Buffer.byteLength(str)) buf[0] = 0xa0 | Buffer.byteLength(str) if (str.length > 0) { buf.write(str, 1) @@ -44,13 +44,13 @@ test('encode/decode strings with max 31 of length', function (t) { }) test('decoding a chopped string', function (t) { - var encoder = msgpack() - var str = 'aaa' - var buf = Buffer.allocUnsafe(1 + Buffer.byteLength(str)) + const encoder = msgpack() + const str = 'aaa' + let buf = Buffer.allocUnsafe(1 + Buffer.byteLength(str)) buf[0] = 0xa0 | Buffer.byteLength(str) + 2 // set bigger size buf.write(str, 1) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/32-bits-signed-integers.js b/test/32-bits-signed-integers.js index 75667d0..1bb9c4a 100644 --- a/test/32-bits-signed-integers.js +++ b/test/32-bits-signed-integers.js @@ -1,15 +1,15 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 32-bits big-endian signed integers', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] - for (var i = 32769; i < 214748364; i += 10235023) { + for (let i = 32769; i < 214748364; i += 10235023) { allNum.push(-i) } @@ -17,7 +17,7 @@ test('encoding/decoding 32-bits big-endian signed integers', function (t) { allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 5, 'must have 5 bytes') t.equal(buf[0], 0xd2, 'must have the proper header') t.equal(buf.readInt32BE(1), num, 'must decode correctly') @@ -25,7 +25,7 @@ test('encoding/decoding 32-bits big-endian signed integers', function (t) { }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(5) + const buf = Buffer.allocUnsafe(5) buf[0] = 0xd2 buf.writeInt32BE(num, 1) t.equal(encoder.decode(buf), num, 'must decode correctly') @@ -42,11 +42,11 @@ test('encoding/decoding 32-bits big-endian signed integers', function (t) { }) test('decoding an incomplete 32-bits big-endian integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(4) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(4) buf[0] = 0xd2 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/32-bits-unsigned-integers.js b/test/32-bits-unsigned-integers.js index c3be233..4d73505 100644 --- a/test/32-bits-unsigned-integers.js +++ b/test/32-bits-unsigned-integers.js @@ -1,15 +1,15 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 32-bits big-endian unsigned integers', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] - for (var i = 65536; i < 0xffffffff; i += 102350237) { + for (let i = 65536; i < 0xffffffff; i += 102350237) { allNum.push(i) } @@ -18,7 +18,7 @@ test('encoding/decoding 32-bits big-endian unsigned integers', function (t) { allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 5, 'must have 5 bytes') t.equal(buf[0], 0xce, 'must have the proper header') t.equal(buf.readUInt32BE(1), num, 'must decode correctly') @@ -26,7 +26,7 @@ test('encoding/decoding 32-bits big-endian unsigned integers', function (t) { }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(5) + const buf = Buffer.allocUnsafe(5) buf[0] = 0xce buf.writeUInt32BE(num, 1) t.equal(encoder.decode(buf), num, 'must decode correctly') @@ -43,11 +43,11 @@ test('encoding/decoding 32-bits big-endian unsigned integers', function (t) { }) test('decoding an incomplete 32-bits big-endian unsigned integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(4) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(4) buf[0] = 0xce buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/32-bytes-strings.js b/test/32-bytes-strings.js index fce15c0..b9c2d0e 100644 --- a/test/32-bytes-strings.js +++ b/test/32-bytes-strings.js @@ -1,20 +1,20 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('encode/decode up to 31 bytes strings', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] - for (var i = 'a'; i.length < 32; i += 'a') { + for (let i = 'a'; i.length < 32; i += 'a') { all.push(i) } all.forEach(function (str) { t.test('encoding a string of length ' + str.length, function (t) { - var buf = encoder.encode(str) + const buf = encoder.encode(str) t.equal(buf.length, 1 + Buffer.byteLength(str), 'must have 2 bytes') t.equal(buf[0] & 0xe0, 0xa0, 'must have the proper header') t.equal(buf.toString('utf8', 1, Buffer.byteLength(str) + 1), str, 'must decode correctly') @@ -22,7 +22,7 @@ test('encode/decode up to 31 bytes strings', function (t) { }) t.test('decoding a string of length ' + str.length, function (t) { - var buf = Buffer.allocUnsafe(1 + Buffer.byteLength(str)) + const buf = Buffer.allocUnsafe(1 + Buffer.byteLength(str)) buf[0] = 0xa0 | Buffer.byteLength(str) buf.write(str, 1) t.equal(encoder.decode(buf), str, 'must decode correctly') diff --git a/test/4-bytes-length-arrays.js b/test/4-bytes-length-arrays.js index 0e863fe..46e92cc 100644 --- a/test/4-bytes-length-arrays.js +++ b/test/4-bytes-length-arrays.js @@ -1,14 +1,14 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size) { - var array = [] + const array = [] - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { array.push(42) } @@ -16,11 +16,11 @@ function build (size) { } test('encode/decode arrays up to 0xffffffff elements', function (t) { - var encoder = msgpack() + const encoder = msgpack() function doTest (array) { t.test('encoding an array with ' + array.length + ' elements', function (t) { - var buf = encoder.encode(array) + const buf = encoder.encode(array) // the array is full of 1-byte integers t.equal(buf.length, 5 + array.length, 'must have the right length') t.equal(buf.readUInt8(0), 0xdd, 'must have the proper header') @@ -42,20 +42,20 @@ test('encode/decode arrays up to 0xffffffff elements', function (t) { }) test('decoding an incomplete array', function (t) { - var encoder = msgpack() + const encoder = msgpack() - var array = build(0xffff + 42) - var buf = Buffer.allocUnsafe(5 + array.length) + const array = build(0xffff + 42) + let buf = Buffer.allocUnsafe(5 + array.length) buf[0] = 0xdd buf.writeUInt32BE(array.length + 10, 1) // set bigger size - var pos = 5 - for (var i = 0; i < array.length; i++) { - var obj = encoder.encode(array[i], true) + let pos = 5 + for (let i = 0; i < array.length; i++) { + const obj = encoder.encode(array[i], true) obj.copy(buf, pos) pos += obj.length } buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -64,12 +64,12 @@ test('decoding an incomplete array', function (t) { }) test('decoding an incomplete header', function (t) { - var encoder = msgpack() + const encoder = msgpack() - var buf = Buffer.allocUnsafe(4) + let buf = Buffer.allocUnsafe(4) buf[0] = 0xdd buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/4-bytes-length-buffers.js b/test/4-bytes-length-buffers.js index 5e1912b..27749da 100644 --- a/test/4-bytes-length-buffers.js +++ b/test/4-bytes-length-buffers.js @@ -1,22 +1,20 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size) { - var buf - - buf = Buffer.allocUnsafe(size) + const buf = Buffer.allocUnsafe(size) buf.fill('a') return buf } test('encode/decode 2^32-1 bytes buffers', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] all.push(build(Math.pow(2, 16))) all.push(build(Math.pow(2, 16) + 1)) @@ -24,7 +22,7 @@ test('encode/decode 2^32-1 bytes buffers', function (t) { all.forEach(function (orig) { t.test('encoding a buffer of length ' + orig.length, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 5 + orig.length, 'must have the right length') t.equal(buf.readUInt8(0), 0xc6, 'must have the proper header') t.equal(buf.readUInt32BE(1), orig.length, 'must include the buf length') @@ -33,7 +31,7 @@ test('encode/decode 2^32-1 bytes buffers', function (t) { }) t.test('decoding a buffer of length ' + orig.length, function (t) { - var buf = Buffer.allocUnsafe(5 + orig.length) + const buf = Buffer.allocUnsafe(5 + orig.length) buf[0] = 0xc6 buf.writeUInt32BE(orig.length, 1) orig.copy(buf, 5) @@ -51,14 +49,14 @@ test('encode/decode 2^32-1 bytes buffers', function (t) { }) test('decoding a chopped 2^32-1 bytes buffer', function (t) { - var encoder = msgpack() - var orig = build(Math.pow(2, 18)) - var buf = Buffer.allocUnsafe(5 + orig.length) + const encoder = msgpack() + const orig = build(Math.pow(2, 18)) + let buf = Buffer.allocUnsafe(5 + orig.length) buf[0] = 0xc6 buf[1] = Math.pow(2, 32) - 1 // set bigger size orig.copy(buf, 5) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -67,11 +65,11 @@ test('decoding a chopped 2^32-1 bytes buffer', function (t) { }) test('decoding an incomplete header of 2^32-1 bytes buffer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(4) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(4) buf[0] = 0xc6 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/4-bytes-length-exts.js b/test/4-bytes-length-exts.js index 786de82..e1e539f 100644 --- a/test/4-bytes-length-exts.js +++ b/test/4-bytes-length-exts.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode variable ext data up between 0x10000 and 0xffffffff', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (size, value) { this.value = value @@ -15,15 +15,15 @@ test('encode/decode variable ext data up between 0x10000 and 0xffffffff', functi } function mytipeEncode (obj) { - var buf = Buffer.allocUnsafe(obj.size) + const buf = Buffer.allocUnsafe(obj.size) buf.fill(obj.value) return buf } function mytipeDecode (data) { - var result = new MyType(data.length, data.toString('utf8', 0, 1)) + const result = new MyType(data.length, data.toString('utf8', 0, 1)) - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { if (data.readUInt8(0) !== data.readUInt8(i)) { throw new Error('should all be the same') } @@ -40,7 +40,7 @@ test('encode/decode variable ext data up between 0x10000 and 0xffffffff', functi all.forEach(function (orig) { t.test('encoding a custom obj of length ' + orig.size, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 6 + orig.size, 'must have the right length') t.equal(buf.readUInt8(0), 0xc9, 'must have the ext header') t.equal(buf.readUInt32BE(1), orig.size, 'must include the data length') @@ -56,13 +56,13 @@ test('encode/decode variable ext data up between 0x10000 and 0xffffffff', functi }) t.test('decoding an incomplete variable ext data up between 0x10000 and 0xffffffff', function (t) { - var obj = encoder.encode(new MyType(0xffffff, 'a')) - var buf = Buffer.allocUnsafe(obj.length) + const obj = encoder.encode(new MyType(0xffffff, 'a')) + let buf = Buffer.allocUnsafe(obj.length) buf[0] = 0xc9 buf.writeUInt32BE(obj.length + 2, 1) // set bigger size obj.copy(buf, 5, 5, obj.length) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -71,10 +71,10 @@ test('encode/decode variable ext data up between 0x10000 and 0xffffffff', functi }) t.test('decoding an incomplete header of variable ext data up between 0x10000 and 0xffffffff', function (t) { - var buf = Buffer.allocUnsafe(5) + let buf = Buffer.allocUnsafe(5) buf[0] = 0xc9 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/4-bytes-length-strings.js b/test/4-bytes-length-strings.js index fda7d74..7670151 100644 --- a/test/4-bytes-length-strings.js +++ b/test/4-bytes-length-strings.js @@ -1,14 +1,14 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode 2^16 <-> (2^32 - 1) bytes strings', function (t) { - var encoder = msgpack() - var all = [] - var str + const encoder = msgpack() + const all = [] + let str str = Buffer.allocUnsafe(Math.pow(2, 16)) str.fill('a') @@ -24,7 +24,7 @@ test('encode/decode 2^16 <-> (2^32 - 1) bytes strings', function (t) { all.forEach(function (str) { t.test('encoding a string of length ' + str.length, function (t) { - var buf = encoder.encode(str) + const buf = encoder.encode(str) t.equal(buf.length, 5 + Buffer.byteLength(str), 'must be the proper length') t.equal(buf[0], 0xdb, 'must have the proper header') t.equal(buf.readUInt32BE(1), Buffer.byteLength(str), 'must include the str length') @@ -33,7 +33,7 @@ test('encode/decode 2^16 <-> (2^32 - 1) bytes strings', function (t) { }) t.test('decoding a string of length ' + str.length, function (t) { - var buf = Buffer.allocUnsafe(5 + Buffer.byteLength(str)) + const buf = Buffer.allocUnsafe(5 + Buffer.byteLength(str)) buf[0] = 0xdb buf.writeUInt32BE(Buffer.byteLength(str), 1) buf.write(str, 5) @@ -51,16 +51,17 @@ test('encode/decode 2^16 <-> (2^32 - 1) bytes strings', function (t) { }) test('decoding a chopped string', function (t) { - var encoder = msgpack() - var str - for (str = 'a'; str.length < 0xffff + 100; str += 'a') { + const encoder = msgpack() + let str + for (str = 'a'; str.length < 0xffff + 100;) { + str += 'a' } - var buf = Buffer.allocUnsafe(5 + Buffer.byteLength(str)) + let buf = Buffer.allocUnsafe(5 + Buffer.byteLength(str)) buf[0] = 0xdb buf.writeUInt32BE(Buffer.byteLength(str) + 10, 1) // set bigger size buf.write(str, 5) buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -69,11 +70,11 @@ test('decoding a chopped string', function (t) { }) test('decoding an incomplete header of a string', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(4) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(4) buf[0] = 0xdb buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/4-bytes-length-uint8arrays.js b/test/4-bytes-length-uint8arrays.js index ae5961e..46147d9 100644 --- a/test/4-bytes-length-uint8arrays.js +++ b/test/4-bytes-length-uint8arrays.js @@ -1,12 +1,12 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') function build (size) { - var array = [] - var i + const array = [] + let i for (i = 0; i < size; i++) { array.push(42) @@ -16,8 +16,8 @@ function build (size) { } test('encode/decode 2^8-1 Uint8Arrays', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] all.push(build(Math.pow(2, 16))) all.push(build(Math.pow(2, 16) + 1)) @@ -25,7 +25,7 @@ test('encode/decode 2^8-1 Uint8Arrays', function (t) { all.forEach(function (array) { t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) { - var buf = encoder.encode(array) + const buf = encoder.encode(array) t.equal(buf.length, 5 + array.byteLength, 'must have the right length') t.equal(buf.readUInt8(0), 0xc6, 'must have the proper header') t.equal(buf.readUInt32BE(1), array.byteLength, 'must include the buf length') diff --git a/test/5-bits-negative-integers.js b/test/5-bits-negative-integers.js index 28b8e1e..76d4436 100644 --- a/test/5-bits-negative-integers.js +++ b/test/5-bits-negative-integers.js @@ -1,27 +1,27 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('encoding/decoding 5-bits negative ints', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] - for (var i = 1; i <= 32; i++) { + for (let i = 1; i <= 32; i++) { allNum.push(-i) } allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 1, 'must have 1 byte') t.equal(buf[0], num + 0x100, 'must encode correctly') t.end() }) t.test('decoding' + num, function (t) { - var buf = Buffer.from([num + 0x100]) + const buf = Buffer.from([num + 0x100]) t.equal(encoder.decode(buf), num, 'must decode correctly') t.end() }) diff --git a/test/64-bits-signed-integers.js b/test/64-bits-signed-integers.js index c68c2b8..86b8342 100644 --- a/test/64-bits-signed-integers.js +++ b/test/64-bits-signed-integers.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 64-bits big-endian signed integers', function (t) { - var encoder = msgpack() - var table = [ + const encoder = msgpack() + const table = [ { num: -9007199254740991, hi: 0xffe00000, lo: 0x00000001 }, { num: -4294967297, hi: 0xfffffffe, lo: 0xffffffff }, { num: -4294967296, hi: 0xffffffff, lo: 0x00000000 }, @@ -17,7 +17,7 @@ test('encoding/decoding 64-bits big-endian signed integers', function (t) { table.forEach(function (testCase) { t.test('encoding ' + testCase.num, function (t) { - var buf = encoder.encode(testCase.num) + const buf = encoder.encode(testCase.num) t.equal(buf.length, 9, 'must have 9 bytes') t.equal(buf[0], 0xd3, 'must have the proper header') t.equal(buf.readUInt32BE(1), testCase.hi, 'hi word must be properly written') @@ -35,11 +35,11 @@ test('encoding/decoding 64-bits big-endian signed integers', function (t) { }) test('decoding an incomplete 64-bits big-endian signed integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(8) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(8) buf[0] = 0xd3 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/64-bits-unsigned-integers.js b/test/64-bits-unsigned-integers.js index 348a43c..f5d6281 100644 --- a/test/64-bits-unsigned-integers.js +++ b/test/64-bits-unsigned-integers.js @@ -1,24 +1,24 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 64-bits big-endian unsigned integers', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] allNum.push(0x0000000100000000) allNum.push(0xffffffffeeeee) allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 9, 'must have 9 bytes') t.equal(buf[0], 0xcf, 'must have the proper header') - var result = 0 - for (var k = 7; k >= 0; k--) { + let result = 0 + for (let k = 7; k >= 0; k--) { result += (buf.readUInt8(k + 1) * Math.pow(2, (8 * (7 - k)))) } t.equal(result, num, 'must decode correctly') @@ -35,11 +35,11 @@ test('encoding/decoding 64-bits big-endian unsigned integers', function (t) { }) test('decoding an incomplete 64-bits big-endian unsigned integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(8) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(8) buf[0] = 0xcf buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/7-bits-positive-integers.js b/test/7-bits-positive-integers.js index 89a3ab9..16685ea 100644 --- a/test/7-bits-positive-integers.js +++ b/test/7-bits-positive-integers.js @@ -1,27 +1,27 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('encoding/decoding 7-bits positive ints', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] - for (var i = 0; i < 126; i++) { + for (let i = 0; i < 126; i++) { allNum.push(i) } allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 1, 'must have 1 byte') t.equal(buf[0], num, 'must decode correctly') t.end() }) t.test('decoding ' + num, function (t) { - var buf = Buffer.from([num]) + const buf = Buffer.from([num]) t.equal(encoder.decode(buf), num, 'must decode correctly') t.end() }) diff --git a/test/8-bits-positive-integers.js b/test/8-bits-positive-integers.js index d3ba907..b30fb2c 100644 --- a/test/8-bits-positive-integers.js +++ b/test/8-bits-positive-integers.js @@ -1,21 +1,21 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 8-bits integers', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] - for (var i = 128; i < 256; i++) { + for (let i = 128; i < 256; i++) { allNum.push(i) } allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 2, 'must have 2 bytes') t.equal(buf[0], 0xcc, 'must have the proper header') t.equal(buf[1], num, 'must decode correctly') @@ -23,7 +23,7 @@ test('encoding/decoding 8-bits integers', function (t) { }) t.test('decoding ' + num, function (t) { - var buf = Buffer.from([0xcc, num]) + const buf = Buffer.from([0xcc, num]) t.equal(encoder.decode(buf), num, 'must decode correctly') t.end() }) @@ -38,11 +38,11 @@ test('encoding/decoding 8-bits integers', function (t) { }) test('decoding an incomplete 8-bits unsigned integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(1) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(1) buf[0] = 0xcc buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/8-bits-signed-integers.js b/test/8-bits-signed-integers.js index b81e24c..e074d35 100644 --- a/test/8-bits-signed-integers.js +++ b/test/8-bits-signed-integers.js @@ -1,21 +1,21 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 8-bits big-endian signed integers', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] - for (var i = 33; i <= 128; i++) { + for (let i = 33; i <= 128; i++) { allNum.push(-i) } allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 2, 'must have 2 bytes') t.equal(buf[0], 0xd0, 'must have the proper header') t.equal(buf.readInt8(1), num, 'must decode correctly') @@ -23,7 +23,7 @@ test('encoding/decoding 8-bits big-endian signed integers', function (t) { }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(3) + const buf = Buffer.allocUnsafe(3) buf[0] = 0xd0 buf.writeInt8(num, 1) t.equal(encoder.decode(buf), num, 'must decode correctly') @@ -40,11 +40,11 @@ test('encoding/decoding 8-bits big-endian signed integers', function (t) { }) test('decoding an incomplete 8-bits big-endian signed integer', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(1) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(1) buf[0] = 0xd0 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/NaN.js b/test/NaN.js index ba5054b..bf4c1b2 100644 --- a/test/NaN.js +++ b/test/NaN.js @@ -1,7 +1,7 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') +const test = require('tape').test +const msgpack = require('../') test('encode NaN as 32-bit float', function (t) { const encoder = msgpack() @@ -14,7 +14,7 @@ test('encode NaN as 32-bit float', function (t) { }) test('encode NaN as 64-bit float with forceFloat64', function (t) { - const encoder = msgpack({forceFloat64: true}) + const encoder = msgpack({ forceFloat64: true }) const buf = encoder.encode(NaN) @@ -33,7 +33,7 @@ test('round-trip 32-bit NaN', function (t) { }) test('round-trip 64-bit NaN with forceFloat64', function (t) { - const encoder = msgpack({forceFloat64: true}) + const encoder = msgpack({ forceFloat64: true }) t.assert(Object.is(encoder.decode(encoder.encode(NaN)), NaN)) diff --git a/test/booleans.js b/test/booleans.js index 8781021..02fb5be 100644 --- a/test/booleans.js +++ b/test/booleans.js @@ -1,11 +1,11 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('encode/decode booleans', function (t) { - var encoder = msgpack() + const encoder = msgpack() t.equal(encoder.encode(true)[0], 0xc3, 'encode true as 0xc3') t.equal(encoder.encode(true).length, 1, 'encode true as a buffer of length 1') diff --git a/test/compatibility-mode.js b/test/compatibility-mode.js index fdadb12..743fee7 100644 --- a/test/compatibility-mode.js +++ b/test/compatibility-mode.js @@ -1,22 +1,22 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') +const test = require('tape').test +const msgpack = require('../') test('encode/compatibility mode', function (t) { - var compatEncoder = msgpack({ + const compatEncoder = msgpack({ compatibilityMode: true }) - var defaultEncoder = msgpack({ + const defaultEncoder = msgpack({ compatibilityMode: false }) - var oneBytesStr = Array(31 + 2).join('x') - var twoBytesStr = Array(255 + 2).join('x') + const oneBytesStr = Array(31 + 2).join('x') + const twoBytesStr = Array(255 + 2).join('x') t.test('default encoding a string of length ' + oneBytesStr.length, function (t) { // Default: use 1 byte length string (str8) - var buf = defaultEncoder.encode(oneBytesStr) + const buf = defaultEncoder.encode(oneBytesStr) t.equal(buf[0], 0xd9, 'must have the proper header (str8)') t.equal(buf.toString('utf8', 2, Buffer.byteLength(oneBytesStr) + 2), oneBytesStr, 'must decode correctly') t.end() @@ -24,7 +24,7 @@ test('encode/compatibility mode', function (t) { t.test('compat. encoding a string of length ' + oneBytesStr.length, function (t) { // Compat. mode: use 2 byte length string (str16) - var buf = compatEncoder.encode(oneBytesStr) + const buf = compatEncoder.encode(oneBytesStr) t.equal(buf[0], 0xda, 'must have the proper header (str16)') t.equal(buf.toString('utf8', 3, Buffer.byteLength(oneBytesStr) + 3), oneBytesStr, 'must decode correctly') t.end() @@ -32,8 +32,8 @@ test('encode/compatibility mode', function (t) { t.test('encoding for a string of length ' + twoBytesStr.length, function (t) { // Two byte strings: compat. mode should make no difference - var buf1 = defaultEncoder.encode(twoBytesStr) - var buf2 = compatEncoder.encode(twoBytesStr) + const buf1 = defaultEncoder.encode(twoBytesStr) + const buf2 = compatEncoder.encode(twoBytesStr) t.deepEqual(buf1, buf2, 'must be equal for two byte strings') t.end() }) diff --git a/test/datenull.js b/test/datenull.js index fb9bed9..f193337 100644 --- a/test/datenull.js +++ b/test/datenull.js @@ -1,9 +1,9 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') +const test = require('tape').test +const msgpack = require('../') test('encode date is null ', function (t) { - var encoder = msgpack({ + const encoder = msgpack({ disableTimestampEncoding: true }) diff --git a/test/doubles.js b/test/doubles.js index afa0e48..5fe8002 100644 --- a/test/doubles.js +++ b/test/doubles.js @@ -1,13 +1,13 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 64-bits float numbers', function (t) { - var encoder = msgpack() - var allNum = [] + const encoder = msgpack() + const allNum = [] allNum.push(748365544534.2) allNum.push(-222111111000004.2) @@ -16,8 +16,8 @@ test('encoding/decoding 64-bits float numbers', function (t) { allNum.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) - var dec = buf.readDoubleBE(1) + const buf = encoder.encode(num) + const dec = buf.readDoubleBE(1) t.equal(buf.length, 9, 'must have 9 bytes') t.equal(buf[0], 0xcb, 'must have the proper header') t.true(Math.abs(dec - num) < 0.1, 'must decode correctly') @@ -25,17 +25,16 @@ test('encoding/decoding 64-bits float numbers', function (t) { }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(9) - var dec + const buf = Buffer.allocUnsafe(9) buf[0] = 0xcb buf.writeDoubleBE(num, 1) - dec = encoder.decode(buf) + const dec = encoder.decode(buf) t.true(Math.abs(dec - num) < 0.1, 'must decode correctly') t.end() }) t.test('mirror test ' + num, function (t) { - var dec = encoder.decode(encoder.encode(num)) + const dec = encoder.decode(encoder.encode(num)) t.true(Math.abs(dec - num) < 0.1, 'must decode correctly') t.end() }) @@ -45,11 +44,11 @@ test('encoding/decoding 64-bits float numbers', function (t) { }) test('decoding an incomplete 64-bits float numbers', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(8) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(8) buf[0] = 0xcb buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/ext-custom-encode-check.js b/test/ext-custom-encode-check.js index 3d682c2..22f591f 100644 --- a/test/ext-custom-encode-check.js +++ b/test/ext-custom-encode-check.js @@ -1,12 +1,12 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('encode/decode ext with a custom object check', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data @@ -17,7 +17,7 @@ test('encode/decode ext with a custom object check', function (t) { } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(2) + const buf = Buffer.allocUnsafe(2) buf.writeUInt8(0x42, 0) buf.writeUInt8(obj.data, 1) return buf @@ -36,7 +36,7 @@ test('encode/decode ext with a custom object check', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 3, 'must have the right length') t.equal(buf.readUInt8(0), 0xd4, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x42, 'must include the custom type id') @@ -45,7 +45,7 @@ test('encode/decode ext with a custom object check', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(3) + const buf = Buffer.allocUnsafe(3) buf[0] = 0xd4 buf[1] = 0x42 buf.writeUInt8(orig.data, 2) diff --git a/test/fixexts.js b/test/fixexts.js index 6fb60f8..c045984 100644 --- a/test/fixexts.js +++ b/test/fixexts.js @@ -1,20 +1,20 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encode/decode 1 byte fixext data', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(1) + const buf = Buffer.allocUnsafe(1) buf.writeUInt8(obj.data, 0) return buf } @@ -31,7 +31,7 @@ test('encode/decode 1 byte fixext data', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 3, 'must have the right length') t.equal(buf.readUInt8(0), 0xd4, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x42, 'must include the custom type id') @@ -40,7 +40,7 @@ test('encode/decode 1 byte fixext data', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(3) + const buf = Buffer.allocUnsafe(3) buf[0] = 0xd4 buf[1] = 0x42 buf.writeUInt8(orig.data, 2) @@ -59,15 +59,15 @@ test('encode/decode 1 byte fixext data', function (t) { }) test('encode/decode 2 bytes fixext data', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(2) + const buf = Buffer.allocUnsafe(2) buf.writeUInt16BE(obj.data, 0) return buf } @@ -84,7 +84,7 @@ test('encode/decode 2 bytes fixext data', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 4, 'must have the right length') t.equal(buf.readUInt8(0), 0xd5, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x42, 'must include the custom type id') @@ -93,7 +93,7 @@ test('encode/decode 2 bytes fixext data', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(4) + const buf = Buffer.allocUnsafe(4) buf[0] = 0xd5 buf[1] = 0x42 buf.writeUInt16BE(orig.data, 2) @@ -112,15 +112,15 @@ test('encode/decode 2 bytes fixext data', function (t) { }) test('encode/decode 4 bytes fixext data', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(4) + const buf = Buffer.allocUnsafe(4) buf.writeUInt32BE(obj.data, 0) return buf } @@ -137,7 +137,7 @@ test('encode/decode 4 bytes fixext data', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 6, 'must have the right length') t.equal(buf.readUInt8(0), 0xd6, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x44, 'must include the custom type id') @@ -146,7 +146,7 @@ test('encode/decode 4 bytes fixext data', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(6) + const buf = Buffer.allocUnsafe(6) buf[0] = 0xd6 buf[1] = 0x44 buf.writeUInt32BE(orig.data, 2) @@ -165,15 +165,15 @@ test('encode/decode 4 bytes fixext data', function (t) { }) test('encode/decode 8 bytes fixext data', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(8) + const buf = Buffer.allocUnsafe(8) buf.writeUInt32BE(obj.data / 2, 0) buf.writeUInt32BE(obj.data / 2, 4) return buf @@ -191,7 +191,7 @@ test('encode/decode 8 bytes fixext data', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 10, 'must have the right length') t.equal(buf.readUInt8(0), 0xd7, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x44, 'must include the custom type id') @@ -200,7 +200,7 @@ test('encode/decode 8 bytes fixext data', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(10) + const buf = Buffer.allocUnsafe(10) buf[0] = 0xd7 buf[1] = 0x44 buf.writeUInt32BE(orig.data / 2, 2) @@ -220,15 +220,15 @@ test('encode/decode 8 bytes fixext data', function (t) { }) test('encode/decode 16 bytes fixext data', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(16) + const buf = Buffer.allocUnsafe(16) buf.writeUInt32BE(obj.data / 4, 0) buf.writeUInt32BE(obj.data / 4, 4) buf.writeUInt32BE(obj.data / 4, 8) @@ -248,7 +248,7 @@ test('encode/decode 16 bytes fixext data', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 18, 'must have the right length') t.equal(buf.readUInt8(0), 0xd8, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x46, 'must include the custom type id') @@ -257,7 +257,7 @@ test('encode/decode 16 bytes fixext data', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(18) + const buf = Buffer.allocUnsafe(18) buf[0] = 0xd8 buf[1] = 0x46 buf.writeUInt32BE(orig.data / 4, 2) @@ -279,15 +279,15 @@ test('encode/decode 16 bytes fixext data', function (t) { }) test('encode/decode fixext inside a map', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(4) + const buf = Buffer.allocUnsafe(4) buf.writeUInt32BE(obj.data, 0) return buf } @@ -308,7 +308,7 @@ test('encode/decode fixext inside a map', function (t) { all.forEach(function (orig) { t.test('mirror test with a custom obj inside a map', function (t) { - var encoded = encoder.encode(orig) + const encoded = encoder.encode(orig) t.deepEqual(encoder.decode(encoded), orig, 'must stay the same') t.end() }) @@ -318,15 +318,15 @@ test('encode/decode fixext inside a map', function (t) { }) test('encode/decode 8 bytes fixext data', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(8) + const buf = Buffer.allocUnsafe(8) buf.writeUInt32BE(obj.data / 2, 0) buf.writeUInt32BE(obj.data / 2, 4) return buf @@ -344,7 +344,7 @@ test('encode/decode 8 bytes fixext data', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 10, 'must have the right length') t.equal(buf.readUInt8(0), 0xd7, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x44, 'must include the custom type id') @@ -353,7 +353,7 @@ test('encode/decode 8 bytes fixext data', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(10) + const buf = Buffer.allocUnsafe(10) buf[0] = 0xd7 buf[1] = 0x44 buf.writeUInt32BE(orig.data / 2, 2) @@ -373,15 +373,15 @@ test('encode/decode 8 bytes fixext data', function (t) { }) test('encode/decode 16 bytes fixext data', function (t) { - var encoder = msgpack() - var all = [] + const encoder = msgpack() + const all = [] function MyType (data) { this.data = data } function mytypeEncode (obj) { - var buf = Buffer.allocUnsafe(16) + const buf = Buffer.allocUnsafe(16) buf.writeUInt32BE(obj.data / 4, 0) buf.writeUInt32BE(obj.data / 4, 4) buf.writeUInt32BE(obj.data / 4, 8) @@ -401,7 +401,7 @@ test('encode/decode 16 bytes fixext data', function (t) { all.forEach(function (orig) { t.test('encoding a custom obj encoded as ' + orig.data, function (t) { - var buf = encoder.encode(orig) + const buf = encoder.encode(orig) t.equal(buf.length, 18, 'must have the right length') t.equal(buf.readUInt8(0), 0xd8, 'must have the fixext header') t.equal(buf.readUInt8(1), 0x46, 'must include the custom type id') @@ -410,7 +410,7 @@ test('encode/decode 16 bytes fixext data', function (t) { }) t.test('decoding a custom obj encoded as ' + orig.data, function (t) { - var buf = Buffer.allocUnsafe(18) + const buf = Buffer.allocUnsafe(18) buf[0] = 0xd8 buf[1] = 0x46 buf.writeUInt32BE(orig.data / 4, 2) @@ -429,11 +429,11 @@ test('encode/decode 16 bytes fixext data', function (t) { }) t.test('decoding an incomplete 1 byte fixext data', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(2) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(2) buf[0] = 0xd4 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -442,11 +442,11 @@ test('encode/decode 16 bytes fixext data', function (t) { }) t.test('decoding an incomplete 2 byte fixext data', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(3) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(3) buf[0] = 0xd5 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -455,11 +455,11 @@ test('encode/decode 16 bytes fixext data', function (t) { }) t.test('decoding an incomplete 4 byte fixext data', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(5) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(5) buf[0] = 0xd6 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -468,11 +468,11 @@ test('encode/decode 16 bytes fixext data', function (t) { }) t.test('decoding an incomplete 8 byte fixext data', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(9) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(9) buf[0] = 0xd7 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -481,11 +481,11 @@ test('encode/decode 16 bytes fixext data', function (t) { }) t.test('decoding an incomplete 16 byte fixext data', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(17) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(17) buf[0] = 0xd8 buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/floats.js b/test/floats.js index 8c0a132..540a3fb 100644 --- a/test/floats.js +++ b/test/floats.js @@ -1,19 +1,19 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') test('encoding/decoding 32-bits float numbers', function (t) { - var encoder = msgpack() - var float32 = [ + const encoder = msgpack() + const float32 = [ 1.5, 0.15625, -2.5 ] - var float64 = [ + const float64 = [ Math.pow(2, 150), 1.337, 2.2 @@ -21,28 +21,27 @@ test('encoding/decoding 32-bits float numbers', function (t) { float64.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 9, 'must have 5 bytes') t.equal(buf[0], 0xcb, 'must have the proper header') - var dec = buf.readDoubleBE(1) + const dec = buf.readDoubleBE(1) t.equal(dec, num, 'must decode correctly') t.end() }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(9) - var dec + const buf = Buffer.allocUnsafe(9) buf[0] = 0xcb buf.writeDoubleBE(num, 1) - dec = encoder.decode(buf) + const dec = encoder.decode(buf) t.equal(dec, num, 'must decode correctly') t.end() }) t.test('mirror test ' + num, function (t) { - var dec = encoder.decode(encoder.encode(num)) + const dec = encoder.decode(encoder.encode(num)) t.equal(dec, num, 'must decode correctly') t.end() }) @@ -50,40 +49,39 @@ test('encoding/decoding 32-bits float numbers', function (t) { float32.forEach(function (num) { t.test('encoding ' + num, function (t) { - var buf = encoder.encode(num) + const buf = encoder.encode(num) t.equal(buf.length, 5, 'must have 5 bytes') t.equal(buf[0], 0xca, 'must have the proper header') - var dec = buf.readFloatBE(1) + const dec = buf.readFloatBE(1) t.equal(dec, num, 'must decode correctly') t.end() }) t.test('forceFloat64 encoding ' + num, function (t) { - var enc = msgpack({ forceFloat64: true }) - var buf = enc.encode(num) + const enc = msgpack({ forceFloat64: true }) + const buf = enc.encode(num) t.equal(buf.length, 9, 'must have 9 bytes') t.equal(buf[0], 0xcb, 'must have the proper header') - var dec = buf.readDoubleBE(1) + const dec = buf.readDoubleBE(1) t.equal(dec, num, 'must decode correctly') t.end() }) t.test('decoding ' + num, function (t) { - var buf = Buffer.allocUnsafe(5) - var dec + const buf = Buffer.allocUnsafe(5) buf[0] = 0xca buf.writeFloatBE(num, 1) - dec = encoder.decode(buf) + const dec = encoder.decode(buf) t.equal(dec, num, 'must decode correctly') t.end() }) t.test('mirror test ' + num, function (t) { - var dec = encoder.decode(encoder.encode(num)) + const dec = encoder.decode(encoder.encode(num)) t.equal(dec, num, 'must decode correctly') t.end() }) @@ -93,11 +91,11 @@ test('encoding/decoding 32-bits float numbers', function (t) { }) test('decoding an incomplete 32-bits float numbers', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(4) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(4) buf[0] = 0xca buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -106,11 +104,11 @@ test('decoding an incomplete 32-bits float numbers', function (t) { }) test('decoding an incomplete 64-bits float numbers', function (t) { - var encoder = msgpack() - var buf = Buffer.allocUnsafe(8) + const encoder = msgpack() + let buf = Buffer.allocUnsafe(8) buf[0] = 0xcb buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { encoder.decode(buf) }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/functions.js b/test/functions.js index 66a7030..4a50103 100644 --- a/test/functions.js +++ b/test/functions.js @@ -1,15 +1,15 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') -var noop = function () {} +const test = require('tape').test +const msgpack = require('../') +const noop = function () {} test('encode a function inside a map', function (t) { - var encoder = msgpack() - var expected = { + const encoder = msgpack() + const expected = { hello: 'world' } - var toEncode = { + const toEncode = { hello: 'world', func: noop } diff --git a/test/levelup-encoding.js b/test/levelup-encoding.js index 19b43b2..6bcab77 100644 --- a/test/levelup-encoding.js +++ b/test/levelup-encoding.js @@ -1,17 +1,17 @@ 'use strict' -var test = require('tape').test -var level = require('memdb') -var msgpack = require('../') +const test = require('tape').test +const level = require('memdb') +const msgpack = require('../') test('msgpack level encoding put', function (t) { t.plan(4) - var pack = msgpack() - var db = level({ + const pack = msgpack() + const db = level({ valueEncoding: pack }) - var obj = { my: 'obj' } + const obj = { my: 'obj' } db.put('hello', obj, function (err) { t.error(err, 'put has no errors') @@ -28,12 +28,12 @@ test('msgpack level encoding put', function (t) { test('msgpack level encoding get', function (t) { t.plan(4) - var pack = msgpack() - var db = level({ + const pack = msgpack() + const db = level({ valueEncoding: pack }) - var obj = { my: 'obj' } - var buf = pack.encode(obj) + const obj = { my: 'obj' } + const buf = pack.encode(obj) db.put('hello', buf, { valueEncoding: 'binary' }, function (err) { t.error(err, 'putting has no errors') @@ -50,11 +50,11 @@ test('msgpack level encoding get', function (t) { test('msgpack level encoding mirror', function (t) { t.plan(4) - var pack = msgpack() - var db = level({ + const pack = msgpack() + const db = level({ valueEncoding: pack }) - var obj = { my: 'obj' } + const obj = { my: 'obj' } db.put('hello', obj, function (err) { t.error(err, 'putting has no errors') diff --git a/test/map-with-object-key.js b/test/map-with-object-key.js index cb0329d..ad7c48e 100644 --- a/test/map-with-object-key.js +++ b/test/map-with-object-key.js @@ -1,8 +1,8 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('encode/decode map with multiple short buffers as both keys and values', function (t) { const first = Buffer.from('first') @@ -13,13 +13,13 @@ test('encode/decode map with multiple short buffers as both keys and values', fu .set(second, third) .set(third, first) - var pack = msgpack() + const pack = msgpack() const newMapping = pack.decode(pack.encode(mapping)) t.equals(newMapping.size, mapping.size) - t.deepEqual([ ...newMapping.keys() ], [ ...mapping.keys() ]) - t.deepEqual([ ...newMapping.values() ], [ ...mapping.values() ]) + t.deepEqual([...newMapping.keys()], [...mapping.keys()]) + t.deepEqual([...newMapping.values()], [...mapping.values()]) t.end() }) diff --git a/test/null.js b/test/null.js index 0407424..4e4de06 100644 --- a/test/null.js +++ b/test/null.js @@ -1,11 +1,11 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('encode/decode null', function (t) { - var encoder = msgpack() + const encoder = msgpack() t.equal(encoder.encode(null)[0], 0xc0, 'encode null as 0xc0') t.equal(encoder.encode(null).length, 1, 'encode a buffer of length 1') diff --git a/test/numerictypeasserts.js b/test/numerictypeasserts.js index 3b523a7..098e0d1 100644 --- a/test/numerictypeasserts.js +++ b/test/numerictypeasserts.js @@ -1,10 +1,10 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') +const test = require('tape').test +const msgpack = require('../') test('custom type registeration assertions', function (t) { - var encoder = msgpack() + const encoder = msgpack() function Type0 (value) { this.value = value @@ -37,8 +37,8 @@ test('custom type registeration assertions', function (t) { encoder.register(-1, TypeNeg, typeNegEncode, typeNegDecode) }, undefined, 'A type registered as a negative value should throw') - var encoded = encoder.encode(new Type0('hi')) - var decoded + const encoded = encoder.encode(new Type0('hi')) + let decoded t.equal(encoded.readUInt8(1), 0x0, 'must use the custom type assigned') t.doesNotThrow(function () { decoded = encoder.decode(encoded) diff --git a/test/object-prototype-poisoning.js b/test/object-prototype-poisoning.js index 18644cd..641b25f 100644 --- a/test/object-prototype-poisoning.js +++ b/test/object-prototype-poisoning.js @@ -1,7 +1,7 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') +const test = require('tape').test +const msgpack = require('../') test('decode throws when object has forbidden __proto__ property', function (t) { const encoder = msgpack() diff --git a/test/object-with-arrays.js b/test/object-with-arrays.js index 8e93c49..d2aa429 100644 --- a/test/object-with-arrays.js +++ b/test/object-with-arrays.js @@ -1,12 +1,12 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') -var bl = require('bl') +const test = require('tape').test +const msgpack = require('../') +const bl = require('bl') function build (size) { - var array = [] - var i + const array = [] + let i for (i = 0; i < size; i++) { array.push(42) @@ -16,30 +16,30 @@ function build (size) { } test('decoding a map with multiple big arrays', function (t) { - var map = { + const map = { first: build(0xffff + 42), second: build(0xffff + 42) } - var pack = msgpack() + const pack = msgpack() t.deepEqual(pack.decode(pack.encode(map)), map) t.end() }) test('decoding a map with multiple big arrays. First one is incomplete', function (t) { - var array = build(0xffff + 42) - var map = { + const array = build(0xffff + 42) + const map = { first: array, second: build(0xffff + 42) } - var pack = msgpack() + const pack = msgpack() - var buf = pack.encode(map) + let buf = pack.encode(map) // 1 (fixmap's header 0x82) + first key's length + 1 (first array's 0xdd) - var sizePosOfFirstArray = 1 + pack.encode('first').length + 1 + const sizePosOfFirstArray = 1 + pack.encode('first').length + 1 buf.writeUInt32BE(array.length + 10, sizePosOfFirstArray) // set first array's size bigger than its actual size buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { pack.decode(buf) }, pack.IncompleteBufferError, 'must throw IncompleteBufferError') @@ -48,19 +48,19 @@ test('decoding a map with multiple big arrays. First one is incomplete', functio }) test('decoding a map with multiple big arrays. Second one is incomplete', function (t) { - var array = build(0xffff + 42) - var map = { + const array = build(0xffff + 42) + const map = { first: array, second: build(0xffff + 42) } - var pack = msgpack() + const pack = msgpack() - var buf = pack.encode(map) + let buf = pack.encode(map) // 1 (fixmap's header 0x82) + first key-value pair's length + second key's length + 1 (second array's 0xdd) - var sizePosOfSecondArray = 1 + pack.encode('first').length + pack.encode(array).length + pack.encode('second').length + 1 + const sizePosOfSecondArray = 1 + pack.encode('first').length + pack.encode(array).length + pack.encode('second').length + 1 buf.writeUInt32BE(array.length + 10, sizePosOfSecondArray) // set second array's size bigger than its actual size buf = bl().append(buf) - var origLength = buf.length + const origLength = buf.length t.throws(function () { pack.decode(buf) }, pack.IncompleteBufferError, 'must throw IncompleteBufferError') diff --git a/test/object-with-buffers.js b/test/object-with-buffers.js index 6a73136..d1a3a60 100644 --- a/test/object-with-buffers.js +++ b/test/object-with-buffers.js @@ -1,18 +1,18 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var fs = require('fs') -var p = require('path') -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const fs = require('fs') +const p = require('path') +const msgpack = require('../') test('encode/decode map with multiple short buffers', function (t) { - var map = { + const map = { first: Buffer.from('first'), second: Buffer.from('second'), third: Buffer.from('third') } - var pack = msgpack() + const pack = msgpack() t.deepEqual(pack.decode(pack.encode(map)), map) t.end() @@ -20,12 +20,12 @@ test('encode/decode map with multiple short buffers', function (t) { if (process.title !== 'browser') { test('encode/decode map with all files in this directory', function (t) { - var files = fs.readdirSync(__dirname) - var map = files.reduce(function (acc, file) { + const files = fs.readdirSync(__dirname) + const map = files.reduce(function (acc, file) { acc[file] = fs.readFileSync(p.join(__dirname, file)) return acc }, {}) - var pack = msgpack() + const pack = msgpack() t.deepEqual(pack.decode(pack.encode(map)), map) t.end() diff --git a/test/object-with-many-keys.js b/test/object-with-many-keys.js index 1cbfc33..d7dc05f 100644 --- a/test/object-with-many-keys.js +++ b/test/object-with-many-keys.js @@ -1,18 +1,18 @@ 'use strict' -var test = require('tape').test -var msgpack = require('../') +const test = require('tape').test +const msgpack = require('../') test('encode/decode map with 10 keys', function (t) { - var map = {} + const map = {} - for (var i = 0; i < 10; i++) { + for (let i = 0; i < 10; i++) { map[i] = i } - var pack = msgpack() + const pack = msgpack() - var encoded = pack.encode(map) + const encoded = pack.encode(map) // map16 byte t.equal(encoded[0], 0x8A) @@ -22,15 +22,15 @@ test('encode/decode map with 10 keys', function (t) { }) test('encode/decode map with 10000 keys', function (t) { - var map = {} + const map = {} - for (var i = 0; i < 10000; i++) { + for (let i = 0; i < 10000; i++) { map[i] = i } - var pack = msgpack() + const pack = msgpack() - var encoded = pack.encode(map) + const encoded = pack.encode(map) // map16 byte t.equal(encoded[0], 0xde) @@ -40,15 +40,15 @@ test('encode/decode map with 10000 keys', function (t) { }) test('encode/decode map with 100000 keys', function (t) { - var map = {} + const map = {} - for (var i = 0; i < 100000; i++) { + for (let i = 0; i < 100000; i++) { map[i] = i } - var pack = msgpack() + const pack = msgpack() - var encoded = pack.encode(map) + const encoded = pack.encode(map) // map32 byte t.equal(encoded[0], 0xdf) @@ -58,13 +58,13 @@ test('encode/decode map with 100000 keys', function (t) { }) test('encode/decode map with 1000000 keys', function (t) { - var map = {} + const map = {} - for (var i = 0; i < 1000000; i++) { + for (let i = 0; i < 1000000; i++) { map[i] = i } - var pack = msgpack() + const pack = msgpack() t.deepEqual(pack.decode(pack.encode(map)), map) t.end() diff --git a/test/object-with-strings.js b/test/object-with-strings.js index 709456f..e17f3fd 100644 --- a/test/object-with-strings.js +++ b/test/object-with-strings.js @@ -1,17 +1,17 @@ 'use strict' -var test = require('tape').test -var fs = require('fs') -var p = require('path') -var msgpack = require('../') +const test = require('tape').test +const fs = require('fs') +const p = require('path') +const msgpack = require('../') test('encode/decode map with multiple short buffers', function (t) { - var map = { + const map = { first: 'first', second: 'second', third: 'third' } - var pack = msgpack() + const pack = msgpack() t.deepEqual(pack.decode(pack.encode(map)), map) t.end() @@ -19,12 +19,12 @@ test('encode/decode map with multiple short buffers', function (t) { if (process.title !== 'browser') { test('encode/decode map with all files in this directory', function (t) { - var files = fs.readdirSync(__dirname) - var map = files.reduce(function (acc, file) { + const files = fs.readdirSync(__dirname) + const map = files.reduce(function (acc, file) { acc[file] = fs.readFileSync(p.join(__dirname, file)).toString('utf8') return acc }, {}) - var pack = msgpack() + const pack = msgpack() t.deepEqual(pack.decode(pack.encode(map)), map) t.end() diff --git a/test/prefer-map.js b/test/prefer-map.js index 0d56fa3..d975687 100644 --- a/test/prefer-map.js +++ b/test/prefer-map.js @@ -9,7 +9,7 @@ const map = new Map() .set('01', null) test('round-trip string-keyed Maps', function (t) { - const encoder = msgpack({preferMap: true}) + const encoder = msgpack({ preferMap: true }) for (const input of [new Map(), map]) { const result = encoder.decode(encoder.encode(input)) @@ -21,7 +21,7 @@ test('round-trip string-keyed Maps', function (t) { }) test('preserve iteration order of string-keyed Maps', function (t) { - const encoder = msgpack({preferMap: true}) + const encoder = msgpack({ preferMap: true }) const decoded = encoder.decode(encoder.encode(map)) t.deepEqual([...decoded.keys()], [...map.keys()]) @@ -30,7 +30,7 @@ test('preserve iteration order of string-keyed Maps', function (t) { }) test('user can still encode objects as ext maps', function (t) { - const encoder = msgpack({preferMap: true}) + const encoder = msgpack({ preferMap: true }) const tag = 0x42 // Polyfill Object.fromEntries for node 10 @@ -52,10 +52,10 @@ test('user can still encode objects as ext maps', function (t) { const inputs = [ {}, new Map(), - {foo: 'bar'}, + { foo: 'bar' }, new Map().set('foo', 'bar'), new Map().set(null, null), - {0: 'baz'}, + { 0: 'baz' }, ['baz'] ] diff --git a/test/streams.js b/test/streams.js index 77b722f..f22c9bc 100644 --- a/test/streams.js +++ b/test/streams.js @@ -1,17 +1,17 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') -var BufferList = require('bl') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') +const BufferList = require('bl') test('must send an object through', function (t) { t.plan(1) - var pack = msgpack() - var encoder = pack.encoder() - var decoder = pack.decoder() - var data = { hello: 'world' } + const pack = msgpack() + const encoder = pack.encoder() + const decoder = pack.decoder() + const data = { hello: 'world' } encoder.pipe(decoder) @@ -23,10 +23,10 @@ test('must send an object through', function (t) { }) test('must send three objects through', function (t) { - var pack = msgpack() - var encoder = pack.encoder() - var decoder = pack.decoder() - var data = [ + const pack = msgpack() + const encoder = pack.encoder() + const decoder = pack.decoder() + const data = [ { hello: 1 }, { hello: 2 }, { hello: 3 } @@ -46,10 +46,10 @@ test('must send three objects through', function (t) { }) test('end-to-end', function (t) { - var pack = msgpack() - var encoder = pack.encoder() - var decoder = pack.decoder() - var data = [ + const pack = msgpack() + const encoder = pack.encoder() + const decoder = pack.decoder() + const data = [ { hello: 1 }, { hello: 2 }, { hello: 3 } @@ -71,9 +71,9 @@ test('end-to-end', function (t) { test('encoding error wrapped', function (t) { t.plan(1) - var pack = msgpack() - var encoder = pack.encoder() - var data = new MyType() + const pack = msgpack() + const encoder = pack.encoder() + const data = new MyType() function MyType () { } @@ -97,10 +97,10 @@ test('encoding error wrapped', function (t) { test('decoding error wrapped', function (t) { t.plan(1) - var pack = msgpack() - var encoder = pack.encoder() - var decoder = pack.decoder() - var data = new MyType() + const pack = msgpack() + const encoder = pack.encoder() + const decoder = pack.decoder() + const data = new MyType() function MyType () { } @@ -127,10 +127,10 @@ test('decoding error wrapped', function (t) { test('decoding error wrapped', function (t) { t.plan(1) - var pack = msgpack() - var encoder = pack.encoder({ header: false }) - var decoder = pack.decoder({ header: false }) - var data = new MyType() + const pack = msgpack() + const encoder = pack.encoder({ header: false }) + const decoder = pack.decoder({ header: false }) + const data = new MyType() function MyType () { } @@ -155,10 +155,10 @@ test('decoding error wrapped', function (t) { }) test('concatenated buffers work', function (t) { - var pack = msgpack() - var encoder = pack.encoder() - var decoder = pack.decoder() - var data = [ + const pack = msgpack() + const encoder = pack.encoder() + const decoder = pack.decoder() + const data = [ { hello: 1 }, { hello: 2 }, { hello: 3 } @@ -166,7 +166,7 @@ test('concatenated buffers work', function (t) { t.plan(data.length) - var bl = new BufferList() + const bl = new BufferList() encoder.on('data', bl.append.bind(bl)) data.forEach(encoder.write.bind(encoder)) @@ -176,7 +176,7 @@ test('concatenated buffers work', function (t) { }) encoder.once('finish', function () { - var buf = bl.slice() + const buf = bl.slice() decoder.write(buf) }) @@ -186,9 +186,9 @@ test('concatenated buffers work', function (t) { test('nil processing works', function (t) { t.plan(3) - var pack = msgpack() - var decoder = pack.decoder({wrap: true}) - var decodedItemIndex = 0 + const pack = msgpack() + const decoder = pack.decoder({ wrap: true }) + let decodedItemIndex = 0 decoder.on('data', function (chunk) { decodedItemIndex++ @@ -206,11 +206,11 @@ test('nil processing works', function (t) { test('encoder wrap mode works', function (t) { t.plan(1) - var pack = msgpack() - var encoder = pack.encoder({wrap: true}) - var decoder = pack.decoder() - var data = { hello: 'world' } - var wrappedData = {value: data} + const pack = msgpack() + const encoder = pack.encoder({ wrap: true }) + const decoder = pack.decoder() + const data = { hello: 'world' } + const wrappedData = { value: data } encoder.pipe(decoder) @@ -224,10 +224,10 @@ test('encoder wrap mode works', function (t) { test('encoder/decoder wrap mode must send an object through', function (t) { t.plan(1) - var pack = msgpack() - var encoder = pack.encoder({wrap: true}) - var decoder = pack.decoder({wrap: true}) - var data = {value: { hello: 'world' }} + const pack = msgpack() + const encoder = pack.encoder({ wrap: true }) + const decoder = pack.decoder({ wrap: true }) + const data = { value: { hello: 'world' } } encoder.pipe(decoder) @@ -240,13 +240,13 @@ test('encoder/decoder wrap mode must send an object through', function (t) { test('encoder pack null', function (t) { t.plan(2) - var pack = msgpack() - var encoder = pack.encoder({wrap: true}) - var decoder = pack.decoder({wrap: true}) + const pack = msgpack() + const encoder = pack.encoder({ wrap: true }) + const decoder = pack.decoder({ wrap: true }) encoder.pipe(decoder) - var decodedItemIndex = 0 + let decodedItemIndex = 0 decoder.on('data', function (chunk) { decodedItemIndex++ t.deepEqual(chunk.value, null) @@ -256,6 +256,6 @@ test('encoder pack null', function (t) { t.equal(decodedItemIndex, 1) }) - encoder.write({value: null}) + encoder.write({ value: null }) encoder.end() }) diff --git a/test/timestamps.js b/test/timestamps.js index 61dad03..aae5b5d 100644 --- a/test/timestamps.js +++ b/test/timestamps.js @@ -1,21 +1,21 @@ 'use strict' -var Buffer = require('safe-buffer').Buffer -var test = require('tape').test -var msgpack = require('../') +const Buffer = require('safe-buffer').Buffer +const test = require('tape').test +const msgpack = require('../') test('timestamp disabling', function (t) { - var encoder = msgpack({disableTimestampEncoding: true}) - var timestamps = [ + const encoder = msgpack({ disableTimestampEncoding: true }) + const timestamps = [ [new Date('2018-01-02T03:04:05.000000000Z'), [0x80]] ] timestamps.forEach(function (testcase) { - var item = testcase[0] - var expected = testcase[1] + const item = testcase[0] + const expected = testcase[1] t.test('encoding ' + item.toString(), function (t) { - var buf = encoder.encode(item).slice() + const buf = encoder.encode(item).slice() t.equal(buf.length, expected.length, 'must have ' + expected.length + ' bytes') t.equal(buf[0], expected[0], 'Should return 0x80 ({}) by default') t.end() @@ -25,8 +25,8 @@ test('timestamp disabling', function (t) { t.end() }) test('encoding/decoding timestamp 64', function (t) { - var encoder = msgpack() - var timestamps = [ + const encoder = msgpack() + const timestamps = [ [new Date('2018-01-02T03:04:05.000000000Z'), [0xd6, 0xff, 0x5a, 0x4a, 0xf6, 0xa5]], [new Date('2038-01-19T03:14:08.000000000Z'), [0xd6, 0xff, 0x80, 0x00, 0x00, 0x00]], [new Date('2038-01-19T03:14:07.999000000Z'), [0xd7, 0xff, 0xee, 0x2E, 0x1F, 0x00, 0x7f, 0xff, 0xff, 0xff]], @@ -35,11 +35,11 @@ test('encoding/decoding timestamp 64', function (t) { ] timestamps.forEach(function (testcase) { - var item = testcase[0] - var expected = testcase[1] + const item = testcase[0] + const expected = testcase[1] t.test('encoding ' + item.toString(), function (t) { - var buf = encoder.encode(item).slice() + const buf = encoder.encode(item).slice() t.equal(buf.length, expected.length, 'must have ' + expected.length + ' bytes') switch (expected.length) { case 6: @@ -50,15 +50,15 @@ test('encoding/decoding timestamp 64', function (t) { break } t.equal(buf.readInt8(1), -1, 'must have the correct type') // Signed - for (var j = 2; j < buf.length; j++) { + for (let j = 2; j < buf.length; j++) { t.equal(buf[j], expected[j], 'byte ' + (j - 2) + ' match') } t.end() }) t.test('decoding ' + item, function (t) { - var buf = Buffer.from(expected) - var dt = encoder.decode(buf) + const buf = Buffer.from(expected) + const dt = encoder.decode(buf) t.equal(dt.toString(), item.toString(), 'must decode correctly\nDecoded:\t' + dt * 1 + '\nExp:\t' + item * 1) t.end() }) @@ -73,8 +73,8 @@ test('encoding/decoding timestamp 64', function (t) { }) test('encoding/decoding timestamp 96', function (t) { - var encoder = msgpack() - var timestamps = [ + const encoder = msgpack() + const timestamps = [ [new Date('0001-01-02T03:04:05.000000000Z'), [0xc7, 0x0c, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xf1, 0x88, 0x6f, 0x85, 0xa5]], [new Date('1251-01-19T03:14:08.000000000Z'), [0xc7, 0x0c, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfa, 0xb7, 0xb2, 0xdf, 0x00]], [new Date('1526-01-19T03:14:07.999000000Z'), [0xc7, 0x0c, 0xff, 0x3b, 0x8b, 0x87, 0xc0, 0xff, 0xff, 0xff, 0xfc, 0xbc, 0xf4, 0x34, 0x7f]], @@ -84,24 +84,24 @@ test('encoding/decoding timestamp 96', function (t) { ] timestamps.forEach(function (testcase) { - var item = testcase[0] - var expected = testcase[1] + const item = testcase[0] + const expected = testcase[1] t.test('encoding ' + item.toString(), function (t) { - var buf = encoder.encode(item).slice() + const buf = encoder.encode(item).slice() t.equal(buf.length, expected.length, 'must have ' + expected.length + ' bytes') t.equal(buf[0], 0xc7, 'must have the correct header') t.equal(buf.readInt8(1), 12, 'must have the correct size') t.equal(buf.readInt8(2), -1, 'must have the correct type') // Signed - for (var j = 3; j < buf.length; j++) { + for (let j = 3; j < buf.length; j++) { t.equal(buf[j], expected[j], 'byte ' + (j - 3) + ' match') } t.end() }) t.test('decoding ' + item, function (t) { - var buf = Buffer.from(expected) - var dt = encoder.decode(buf) + const buf = Buffer.from(expected) + const dt = encoder.decode(buf) t.equal(dt.toString(), item.toString(), 'must decode correctly\nDecoded:\t' + dt * 1 + '\nExp:\t' + item * 1) t.end() })