Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve internals and add some features #78

Merged
merged 56 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
a0f5f62
Feature: Manually encode unsuppoted object.
thephoenixofthevoid Apr 22, 2019
12f63ee
Refactoring: inlining hasMinBufferSize and simplifying decode(buf)
thephoenixofthevoid Apr 23, 2019
991cc76
Used while where it actually meant to.
thephoenixofthevoid Apr 23, 2019
287d374
In decodeMap(...) simplify counting amount of bytes consumed
thephoenixofthevoid Apr 23, 2019
f7345f0
Imporove maintainability
thephoenixofthevoid Apr 23, 2019
8a932aa
Getting rid of "offset + 1" artificial expression
thephoenixofthevoid Apr 23, 2019
a9ec4fb
Keep on getting intent more clear
thephoenixofthevoid Apr 23, 2019
11ef5e6
Implemented all integers and all kinds of floats
thephoenixofthevoid Apr 23, 2019
65ce0ba
Added missing Ranges as TODO commends
thephoenixofthevoid Apr 23, 2019
c5789c7
function decodeStr (buf, offset, size)
thephoenixofthevoid Apr 23, 2019
a511bdd
Added decodeBuffers
thephoenixofthevoid Apr 23, 2019
db3f3e8
gitignore updated
thephoenixofthevoid Apr 23, 2019
3ae90f9
if (inRange(0xc7, 0xc9)) return decodeExts(buf, offset, size - 2)
thephoenixofthevoid Apr 23, 2019
a902377
decodeFixExt + Moved functions outside of closure
thephoenixofthevoid Apr 23, 2019
e3546c8
It was just a line!
thephoenixofthevoid Apr 23, 2019
6319853
Update .gitignore
thephoenixofthevoid Apr 23, 2019
f2840b6
decodeArray
thephoenixofthevoid Apr 23, 2019
ebb90c0
small arrays and
thephoenixofthevoid Apr 23, 2019
67d253c
decodeMaps + ordering
thephoenixofthevoid Apr 23, 2019
83cb8ac
fixmap fixarr fixstr
thephoenixofthevoid Apr 23, 2019
e3be779
Inline buf.readUIntBE(offset, size) method where getLength (buf, offs…
thephoenixofthevoid Apr 24, 2019
f70ee03
function decodeTimestamp (buf, size, headerSize) moved to helper
thephoenixofthevoid Apr 24, 2019
c2c4b18
Use array syntax to handle results
thephoenixofthevoid Apr 24, 2019
6f725ac
Few changes
thephoenixofthevoid Apr 25, 2019
76a8958
Avoid using length var at top level
thephoenixofthevoid Apr 25, 2019
8d5003a
Avoid using result var at top level
thephoenixofthevoid Apr 25, 2019
672e68c
remove dist folder from git
thephoenixofthevoid Apr 25, 2019
698c73e
Restore dist files
thephoenixofthevoid Apr 25, 2019
c2e3c1b
Restore dist min files
thephoenixofthevoid Apr 25, 2019
2cbbaeb
Finally restore min version
thephoenixofthevoid Apr 25, 2019
4b7f55f
Inline decodeExts helper
thephoenixofthevoid Apr 25, 2019
c53969f
Revert transform unsupported
thephoenixofthevoid Apr 25, 2019
c80300c
Clean up index.js
thephoenixofthevoid Apr 25, 2019
c02177f
Import IncompleteBufferError from helpers.js and not reexport in deco…
thephoenixofthevoid Apr 25, 2019
1d96857
Remove passing transformUnsupported option from index.js
thephoenixofthevoid Apr 25, 2019
45a8340
Small fixes
thephoenixofthevoid Apr 25, 2019
b5bd1fd
Small fixes
thephoenixofthevoid Apr 25, 2019
40d2871
Fix travis builds
thephoenixofthevoid Apr 25, 2019
311de36
Fix travis builds 2
thephoenixofthevoid Apr 25, 2019
4a404e2
Fix travis builds 3
thephoenixofthevoid Apr 25, 2019
c5fd047
function getHeaders
thephoenixofthevoid Apr 25, 2019
d2ba993
function getHeader
thephoenixofthevoid Apr 25, 2019
cb7ecc2
move some functions out of closure
thephoenixofthevoid Apr 25, 2019
04edacf
Better handling of TimestampEncoding
thephoenixofthevoid Apr 25, 2019
8719e43
encodeFloat function simplified
thephoenixofthevoid Apr 25, 2019
6880581
Feature: Sort Keys
thephoenixofthevoid Apr 26, 2019
c9a589f
Feature: support encoding/decoding Maps
thephoenixofthevoid Apr 26, 2019
e899f6e
Feature: support encoding/decoding Maps
thephoenixofthevoid Apr 26, 2019
4bc7e8d
Test descr updated
thephoenixofthevoid Apr 26, 2019
b3f6eee
Move all Timestamp-related into separate file
thephoenixofthevoid Apr 26, 2019
6479b06
Use bl constuctor instead of append method
thephoenixofthevoid Apr 26, 2019
ff60bea
Unify headers methods
thephoenixofthevoid Apr 26, 2019
654a480
Import helpers from helpers.js
thephoenixofthevoid Apr 26, 2019
8734eae
Merge
thephoenixofthevoid Apr 26, 2019
a38d8a8
fix build
thephoenixofthevoid Apr 26, 2019
4e35526
restructure DataCodec module
thephoenixofthevoid Apr 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ encoded.
options:

- `forceFloat64`, a boolean to that forces all floats to be encoded as 64-bits floats. Defaults to false.
- `sortKeys`, a boolean to force a determinate keys order
- `compatibilityMode`, a boolean that enables "compatibility mode" which doesn't use str 8 format. Defaults to false.
- `disableTimestampEncoding`, a boolean that when set disables the encoding of Dates into the [timestamp extension type](https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type). Defaults to false.

Expand Down
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ 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')

function msgpack (options) {
var encodingTypes = []
var decodingTypes = Object.create(null)
var decodingTypes = new Map()

options = options || {
forceFloat64: false,
Expand All @@ -20,6 +21,11 @@ function msgpack (options) {
disableTimestampEncoding: false
}

decodingTypes.set(DateCodec.type, DateCodec.decode)
if (!options.disableTimestampEncoding) {
encodingTypes.push(DateCodec)
}

function registerEncoder (check, encode) {
assert(check, 'must have an encode function')
assert(encode, 'must have an encode function')
Expand All @@ -32,7 +38,7 @@ function msgpack (options) {
function registerDecoder (type, decode) {
assert(type >= 0, 'must have a non-negative type')
assert(decode, 'must have a decode function')
decodingTypes[type] = decode
decodingTypes.set(type, decode)
return this
}

Expand Down Expand Up @@ -65,7 +71,7 @@ function msgpack (options) {
}

return {
encode: buildEncode(encodingTypes, options.forceFloat64, options.compatibilityMode, options.disableTimestampEncoding),
encode: buildEncode(encodingTypes, options),
decode: buildDecode(decodingTypes),
register,
registerEncoder,
Expand Down
62 changes: 62 additions & 0 deletions lib/codecs/DateCodec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const type = -1

function encode (dt) {
var millis = dt * 1
var seconds = Math.floor(millis / 1000)
var nanos = (millis - seconds * 1000) * 1e6

if (nanos || seconds > 0xffffffff) {
// Timestamp64
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

encoded.writeInt32BE(upper, 1)
encoded.writeInt32BE(lower, 5)
return encoded
} else {
// Timestamp32
const encoded = Buffer.allocUnsafe(5)
encoded[0] = -1
encoded.writeUInt32BE(Math.floor(millis / 1000), 1)
return encoded
}
}

function check (obj) {
return typeof obj.getDate === 'function'
}

function decode (buf) {
var seconds
var nanoseconds = 0

switch (buf.length) {
case 4:
// timestamp 32 stores the number of seconds that have elapsed since 1970-01-01 00:00:00 UTC in an 32-bit unsigned integer
seconds = buf.readUInt32BE(0)
break

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)
nanoseconds = upper / 4
seconds = ((upper & 0x03) * Math.pow(2, 32)) + lower // If we use bitwise operators, we get truncated to 32bits
break

case 12:
throw new Error('timestamp 96 is not yet implemented')
}

var millis = (seconds * 1000) + Math.round(nanoseconds / 1E6)

return new Date(millis)
}

module.exports = { check, type, encode, decode }
57 changes: 32 additions & 25 deletions lib/decoder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

var bl = require('bl')
var helpers = require('./helpers.js')
var IncompleteBufferError = helpers.IncompleteBufferError
var decodeTimestamp = helpers.decodeTimestamp
var IncompleteBufferError = require('./helpers.js').IncompleteBufferError

const SIZES = {
0xc4: 2,
Expand Down Expand Up @@ -75,7 +73,7 @@ module.exports = function buildDecode (decodingTypes) {
const length = first & 0x0f
const headerSize = offset - initialOffset
// we have an array with less than 15 elements
return decodeMap(buf, initialOffset, length, headerSize)
return decodeMap(buf, offset, length, headerSize)
}
if ((first & 0xf0) === 0x90) {
const length = first & 0x0f
Expand Down Expand Up @@ -140,12 +138,12 @@ module.exports = function buildDecode (decodingTypes) {
length = buf.readUInt16BE(offset)
offset += 2
// console.log(offset - initialOffset)
return decodeMap(buf, initialOffset, length, 3)
return decodeMap(buf, offset, length, 3)

case 0xdf:
length = buf.readUInt32BE(offset)
offset += 4
return decodeMap(buf, initialOffset, length, 5)
return decodeMap(buf, offset, length, 5)
}
}
if (first >= 0xe0) return [first - 0x100, 1] // 5 bits negative ints
Expand All @@ -168,24 +166,36 @@ module.exports = function buildDecode (decodingTypes) {
return [result, headerLength + offset - initialOffset]
}

function decodeMap (buf, initialOffset, length, headerLength) {
var offset = initialOffset + headerLength
const result = {}
var i = 0

while (i++ < length) {
var keyResult = tryDecode(buf, offset)
if (!keyResult) return null
offset += keyResult[1]
function decodeMap (buf, offset, length, headerLength) {
const _temp = decodeArray(buf, offset, 2 * length, headerLength)
if (!_temp) return null
const [ result, consumedBytes ] = _temp

var valueResult = tryDecode(buf, offset)
if (!valueResult) return null
offset += valueResult[1]

result[keyResult[0]] = valueResult[0]
var isPlainObject = true
for (let i = 0; i < 2 * length; i += 2) {
if (typeof result[i] !== 'string') {
isPlainObject = false
break
}
}

return [result, offset - initialOffset]
if (isPlainObject) {
const object = {}
for (let i = 0; i < 2 * length; i += 2) {
const key = result[i]
const val = result[i + 1]
object[key] = val
}
return [object, consumedBytes]
} else {
const mapping = new Map()
for (let i = 0; i < 2 * length; i += 2) {
const key = result[i]
const val = result[i + 1]
mapping.set(key, val)
}
return [mapping, consumedBytes]
}
}

function readInt64BE (buf, offset) {
Expand Down Expand Up @@ -237,10 +247,7 @@ module.exports = function buildDecode (decodingTypes) {
function decodeExt (buf, offset, type, size, headerSize) {
const toDecode = buf.slice(offset, offset + size)

// Pre-defined (type < 0) Reserved for future extensions
if (type === -1) return decodeTimestamp(toDecode, size, headerSize)

const decode = decodingTypes[type]
const decode = decodingTypes.get(type)
if (!decode) throw new Error('unable to find ext type ' + type)

var value = decode(toDecode)
Expand Down
Loading