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

feat: support Uint8Array input in place of Buffer #13

Merged
merged 6 commits into from
Jun 24, 2020
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
51 changes: 48 additions & 3 deletions src/compile/encodings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ var varint = require('varint')
var svarint = require('signed-varint')
const { Buffer } = require('buffer')

/**
* @template T
* @typedef {{bytes?:number} & function(T, Buffer, number):Buffer} Encoder
*/
/**
* @template T
* @typedef {{bytes?:number} & function(Buffer, number):T} Decoder
Gozala marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* @template T
* @typedef {Object} Codec
* @property {number} type
* @property {Encoder<T>} encode
* @property {Decoder<T>} decode
* @property {function(T):number} encodingLength
*/

/**
* @template T
* @param {number} type
* @param {Encoder<T>} encode
* @param {Decoder<T>} decode
* @param {function(T):number} encodingLength
* @returns {Codec<T>}
*/
var encoder = function (type, encode, decode, encodingLength) {
encode.bytes = decode.bytes = 0

Expand All @@ -17,7 +43,12 @@ var encoder = function (type, encode, decode, encodingLength) {

exports.make = encoder

exports.bytes = (function (tag) {
/** @type {Codec<Uint8Array>} */
exports.bytes = (function () {
/**
* @param {Uint8Array|string} val
* @returns {number}
*/
var bufferLength = function (val) {
return Buffer.isBuffer(val) ? val.length : Buffer.byteLength(val)
}
Expand All @@ -34,7 +65,7 @@ exports.bytes = (function (tag) {
varint.encode(len, buffer, offset)
offset += varint.encode.bytes

if (Buffer.isBuffer(val)) val.copy(buffer, offset)
if (val instanceof Uint8Array) buffer.set(val, offset)
else buffer.write(val, offset, len)
offset += len

Expand All @@ -58,6 +89,7 @@ exports.bytes = (function (tag) {
return encoder(2, encode, decode, encodingLength)
})()

/** @type {Codec<string>} */
exports.string = (function () {
var encodingLength = function (val) {
var len = Buffer.byteLength(val)
Expand Down Expand Up @@ -94,7 +126,12 @@ exports.string = (function () {
return encoder(2, encode, decode, encodingLength)
})()

/** @type {Codec<boolean>} */
exports.bool = (function () {
/**
* @param {boolean} val
* @returns {number}
*/
var encodingLength = function (val) {
return 1
}
Expand All @@ -114,6 +151,7 @@ exports.bool = (function () {
return encoder(0, encode, decode, encodingLength)
})()

/** @type {Codec<number>} */
exports.int32 = (function () {
var decode = function (buffer, offset) {
var val = varint.decode(buffer, offset)
Expand All @@ -134,6 +172,7 @@ exports.int32 = (function () {
return encoder(0, encode, decode, encodingLength)
})()

/** @type {Codec<number>} */
exports.int64 = (function () {
var decode = function (buffer, offset) {
var val = varint.decode(buffer, offset)
Expand Down Expand Up @@ -178,11 +217,13 @@ exports.int64 = (function () {
return encoder(0, encode, decode, encodingLength)
})()

/** @type {Codec<number>} */
exports.sint32 =
exports.sint64 = (function () {
return encoder(0, svarint.encode, svarint.decode, svarint.encodingLength)
})()

/** @type {Codec<number>} */
exports.uint32 =
exports.uint64 =
exports.enum =
Expand All @@ -191,14 +232,15 @@ exports.varint = (function () {
})()

// we cannot represent these in javascript so we just use buffers
/** @type {Codec<Uint8Array>} */
exports.fixed64 =
exports.sfixed64 = (function () {
var encodingLength = function (val) {
return 8
}

var encode = function (val, buffer, offset) {
val.copy(buffer, offset)
buffer.set(val, offset)
encode.bytes = 8
return buffer
}
Expand Down Expand Up @@ -232,6 +274,7 @@ exports.double = (function () {
return encoder(1, encode, decode, encodingLength)
})()

/** @type {Codec<number>} */
exports.fixed32 = (function () {
var encodingLength = function (val) {
return 4
Expand All @@ -252,6 +295,7 @@ exports.fixed32 = (function () {
return encoder(5, encode, decode, encodingLength)
})()

/** @type {Codec<number>} */
exports.sfixed32 = (function () {
var encodingLength = function (val) {
return 4
Expand All @@ -272,6 +316,7 @@ exports.sfixed32 = (function () {
return encoder(5, encode, decode, encodingLength)
})()

/** @type {Codec<number>} */
exports.float = (function () {
var encodingLength = function (val) {
return 4
Expand Down
2 changes: 1 addition & 1 deletion test/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var Bytes = protobuf(require('./test.proto')).Bytes

tape('bytes encode + decode', function (t) {
var b1 = Bytes.encode({
req: Buffer.from([0, 1, 2, 3])
req: Uint8Array.from([0, 1, 2, 3])
})

var o1 = Bytes.decode(b1)
Expand Down
2 changes: 1 addition & 1 deletion test/corrupted.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var messages = protobuf(protoStr)
tape('invalid message decode', function (t) {
var didFail = false
try {
messages.ABC.decode(Buffer.from([8, 182, 168, 235, 144, 178, 41]))
messages.ABC.decode(Uint8Array.from([8, 182, 168, 235, 144, 178, 41]))
} catch (e) {
didFail = true
}
Expand Down
2 changes: 1 addition & 1 deletion test/custom-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tape('custom types encode + decode', function (t) {
var b1 = CustomType.encode({
req: {
num: 5,
payload: Buffer.from([])
payload: Uint8Array.from([])
}
})

Expand Down
16 changes: 8 additions & 8 deletions test/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ var Nested = protobuf(require('./test.proto')).Nested
tape('nested encode', function (t) {
var b1 = Nested.encode({
num: 1,
payload: Buffer.from('lol'),
payload: new TextEncoder().encode('lol'),
Gozala marked this conversation as resolved.
Show resolved Hide resolved
meh: {
num: 2,
payload: Buffer.from('bar')
payload: new TextEncoder().encode('bar')
}
})

var b2 = Nested.encode({
num: 1,
payload: Buffer.from('lol'),
payload: new TextEncoder().encode('lol'),
meeeh: 42,
meh: {
num: 2,
payload: Buffer.from('bar')
payload: new TextEncoder().encode('bar')
}
})

Expand All @@ -31,10 +31,10 @@ tape('nested encode', function (t) {
tape('nested encode + decode', function (t) {
var b1 = Nested.encode({
num: 1,
payload: Buffer.from('lol'),
payload: new TextEncoder().encode('lol'),
meh: {
num: 2,
payload: Buffer.from('bar')
payload: new TextEncoder().encode('bar')
}
})

Expand All @@ -48,11 +48,11 @@ tape('nested encode + decode', function (t) {

var b2 = Nested.encode({
num: 1,
payload: Buffer.from('lol'),
payload: new TextEncoder().encode('lol'),
meeeh: 42,
meh: {
num: 2,
payload: Buffer.from('bar')
payload: new TextEncoder().encode('bar')
}
})

Expand Down
16 changes: 8 additions & 8 deletions test/repeated.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ tape('repeated encode', function (t) {
var b1 = Repeated.encode({
list: [{
num: 1,
payload: Buffer.from('lol')
payload: new TextEncoder().encode('lol')
}, {
num: 2,
payload: Buffer.from('lol1')
payload: new TextEncoder().encode('lol1')
}]
})

var b2 = Repeated.encode({
list: [{
num: 1,
payload: Buffer.from('lol')
payload: new TextEncoder().encode('lol')
}, {
num: 2,
payload: Buffer.from('lol1'),
payload: new TextEncoder().encode('lol1'),
meeeeh: 100
}],
meeh: 42
Expand All @@ -35,10 +35,10 @@ tape('repeated encode + decode', function (t) {
var b1 = Repeated.encode({
list: [{
num: 1,
payload: Buffer.from('lol')
payload: new TextEncoder().encode('lol')
}, {
num: 2,
payload: Buffer.from('lol1')
payload: new TextEncoder().encode('lol1')
}]
})

Expand All @@ -53,10 +53,10 @@ tape('repeated encode + decode', function (t) {
var b2 = Repeated.encode({
list: [{
num: 1,
payload: Buffer.from('lol')
payload: new TextEncoder().encode('lol')
}, {
num: 2,
payload: Buffer.from('lol1'),
payload: new TextEncoder().encode('lol1'),
meeeeh: 100
}],
meeh: 42
Expand Down