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 4 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ stages:
- cov

node_js:
- '10'
- '12'
Gozala marked this conversation as resolved.
Show resolved Hide resolved

os:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"varint": "^5.0.0"
},
"devDependencies": {
"ipfs-utils": "^2.3.0",
"aegir": "^21.4.0",
"benchmark": "^2.1.4",
"protocol-buffers": "^4.1.0",
Expand Down
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
18 changes: 10 additions & 8 deletions test/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

var tape = require('tape')
var protobuf = require('../')
const TextEncoder = require('ipfs-utils/src/text-encoder')

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 +33,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 +50,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
17 changes: 9 additions & 8 deletions test/repeated.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
var tape = require('tape')
var protobuf = require('../')
var Repeated = protobuf(require('./test.proto')).Repeated
const TextEncoder = require('ipfs-utils/src/text-encoder')

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 +36,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 +54,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