Skip to content

Commit

Permalink
Updated all dependencies (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Mar 5, 2021
1 parent 0a925ea commit 15333ce
Show file tree
Hide file tree
Showing 60 changed files with 831 additions and 837 deletions.
18 changes: 8 additions & 10 deletions benchmarks/encodedecode.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -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)
18 changes: 8 additions & 10 deletions benchmarks/parseshortmap.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -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)
20 changes: 10 additions & 10 deletions example.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)

Expand Down
37 changes: 20 additions & 17 deletions lib/codecs/DateCodec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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--
}
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
38 changes: 19 additions & 19 deletions lib/decoder.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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])
Expand All @@ -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) {
Expand Down Expand Up @@ -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]
}
Expand All @@ -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)
Expand All @@ -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]
Expand All @@ -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]
}
}
Loading

0 comments on commit 15333ce

Please sign in to comment.