Skip to content

Commit

Permalink
feat: support Uint8Array input in place of Buffer (#13)
Browse files Browse the repository at this point in the history
With this change all the `Buffer` inputs were replaced by `Uint8Array` which is backwards compatible since `Buffer` is a subclass.

This does make an assumption that "bytes" encoder used to take `Buffer|string` here:

https://github.com/ipfs/protons/blob/3b5276f052f2e17c2d806d27cd9a88e156588977/src/compile/encodings.js#L30-L43


Fixes #12
  • Loading branch information
Gozala authored Jun 24, 2020
1 parent 3b5276f commit 6c7add4
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ stages:
- cov

node_js:
- '10'
- '12'
- '14'

os:
- linux
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'),
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

0 comments on commit 6c7add4

Please sign in to comment.