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 2 commits
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
6 changes: 3 additions & 3 deletions src/compile/encodings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var encoder = function (type, encode, decode, encodingLength) {

exports.make = encoder

exports.bytes = (function (tag) {
exports.bytes = (function () {
var bufferLength = function (val) {
return Buffer.isBuffer(val) ? val.length : Buffer.byteLength(val)
}
Expand All @@ -34,7 +34,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 Down Expand Up @@ -198,7 +198,7 @@ exports.sfixed64 = (function () {
}

var encode = function (val, buffer, offset) {
val.copy(buffer, offset)
buffer.set(val, offset)
encode.bytes = 8
return buffer
}
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