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

Adding support for encoding Uint8Arrays using bin format family #59

Merged
merged 2 commits into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
5 changes: 4 additions & 1 deletion lib/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilit
buf.writeUInt32BE(len, 1)
buf.write(obj, 5)
}
} else if (obj && obj.readUInt32LE) {
} else if (obj && (obj.readUInt32LE || obj instanceof Uint8Array)) {
if (obj instanceof Uint8Array) {
obj = new Buffer(obj)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new Buffer() constructor is depracted. Can you use Buffeer.from?

}
// weird hack to support Buffer
// and Buffer-like objects
if (obj.length <= 0xff) {
Expand Down
43 changes: 43 additions & 0 deletions test/1-byte-length-uint8arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

var Buffer = require('safe-buffer').Buffer
var test = require('tape').test
var msgpack = require('../')

function build (size) {
var array = []
var i

for (i = 0; i < size; i++) {
array.push(42)
}

return new Uint8Array(array)
}

test('encode/decode 2^8-1 Uint8Arrays', function (t) {
var encoder = msgpack()
var all = []

all.push(build(Math.pow(2, 8) - 1))
all.push(build(Math.pow(2, 6) + 1))
all.push(build(1))
all.push(new Uint8Array(0))

all.forEach(function (array) {
t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
var 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')
t.end()
})

t.test('mirror test for an Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
t.deepEqual(encoder.decode(encoder.encode(array)), new Buffer(array), 'must stay the same')
t.end()
})
})

t.end()
Copy link
Contributor Author

@moozzyk moozzyk Oct 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No decoding tests since bin family is decoded to Buffers. This code has not changed and already has test coverage.

})
43 changes: 43 additions & 0 deletions test/2-bytes-length-uint8arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

var Buffer = require('safe-buffer').Buffer
var test = require('tape').test
var msgpack = require('../')

function build (size) {
var array = []
var i

for (i = 0; i < size; i++) {
array.push(42)
}

return new Uint8Array(array)
}

test('encode/decode 2^8-1 Uint8Arrays', function (t) {
var encoder = msgpack()
var all = []

all.push(build(Math.pow(2, 8)))
all.push(build(Math.pow(2, 8) + 1))
all.push(build(Math.pow(2, 12) + 1))
all.push(build(Math.pow(2, 16) - 1))

all.forEach(function (array) {
t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
var 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')
t.end()
})

t.test('mirror test for an Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
t.deepEqual(encoder.decode(encoder.encode(array)), new Buffer(array), 'must stay the same')
t.end()
})
})

t.end()
})
42 changes: 42 additions & 0 deletions test/4-bytes-length-uint8arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

var Buffer = require('safe-buffer').Buffer
var test = require('tape').test
var msgpack = require('../')

function build (size) {
var array = []
var i

for (i = 0; i < size; i++) {
array.push(42)
}

return new Uint8Array(array)
}

test('encode/decode 2^8-1 Uint8Arrays', function (t) {
var encoder = msgpack()
var all = []

all.push(build(Math.pow(2, 16)))
all.push(build(Math.pow(2, 16) + 1))
all.push(build(Math.pow(2, 18) + 1))

all.forEach(function (array) {
t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
var 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')
t.end()
})

t.test('mirror test for an Uint8Array of length ' + array.byteLength + ' bytes', function (t) {
t.deepEqual(encoder.decode(encoder.encode(array)), new Buffer(array), 'must stay the same')
t.end()
})
})

t.end()
})