Skip to content

Commit

Permalink
feat: add support for passing encoder/decoder (#2)
Browse files Browse the repository at this point in the history
feat: add support for passing encoder/decoder
  • Loading branch information
mkg20001 authored Nov 21, 2019
2 parents 5f37554 + db8c7a7 commit 8d8633b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A convinience-wrapper arround protocol-buffers and lp-messages functions

# API

- `wrap(duplex)`: Wraps a duplex, returns below object
- `wrap(duplex, opts)`: Wraps a duplex, returns below object (opts=Object with .lengthEncoder, .lengthDecoder from [it-length-prefixed api](https://www.npmjs.com/package/it-length-prefixed#api))
- `.read(bytes)`: async, reads the given amount of bytes
- `.readLP()`: async, reads one length-prefixed message
- `.readPB(proto)`: async, reads one protocol-buffers length-prefixed message (proto=Object with .encode, .decode functions)
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const Shake = require('it-handshake')
const lp = require('it-length-prefixed')

module.exports = (duplex) => {
module.exports = (duplex, opts = {}) => {
const shake = Shake(duplex)
const lpReader = lp.decode.fromReader(shake.reader)
const lpReader = lp.decode.fromReader(shake.reader, { lengthDecoder: opts.lengthDecoder })

let isDone = false

Expand All @@ -24,7 +24,7 @@ module.exports = (duplex) => {
if (!value) { throw new Error('Value is null') }
return value
},
readLP: async (useBE32) => {
readLP: async () => {
// read, decode
const { value, done } = await lpReader.next()

Expand All @@ -46,9 +46,9 @@ module.exports = (duplex) => {
// just write
shake.writer.push(data)
},
writeLP: (data, useBE32) => {
writeLP: (data) => {
// encode, write
W.write(lp.encode.single(data))
W.write(lp.encode.single(data, { lengthEncoder: opts.lengthEncoder }))
},
writePB: (data, proto) => {
// encode, writeLP
Expand Down
27 changes: 23 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Pair = require('it-pair')
const { collect } = require('streaming-iterables')
const Wrap = require('..')
const assert = require('assert').strict
const { int32BEDecode, int32BEEncode } = require('it-length-prefixed')

/* eslint-env mocha */
/* eslint-disable require-await */
Expand All @@ -26,12 +27,30 @@ describe('it-pb-rpc', () => {
assert.deepEqual(data, res.slice())
})

it.skip('lp fixed', async () => {
it('lp fixed encode', async () => {
const duplex = Pair()
const wrap = Wrap(duplex, { lengthEncoder: int32BEEncode })
const data = Buffer.from('hellllllllloooo')

w.writeLP(data, true)
const res = await w.readLP(true)
assert.deepEqual(data, res.slice())
wrap.writeLP(data)
const res = await wrap.read()
const length = Buffer.allocUnsafe(4)
length.writeInt32BE(data.length, 0)
const expected = Buffer.concat([length, data])
assert.deepEqual(res.slice(), expected)
})

it('lp fixed decode', async () => {
const duplex = Pair()
const wrap = Wrap(duplex, { lengthDecoder: int32BEDecode })
const data = Buffer.from('hellllllllloooo')
const length = Buffer.allocUnsafe(4)
length.writeInt32BE(data.length, 0)
const encoded = Buffer.concat([length, data])

wrap.write(encoded)
const res = await wrap.readLP()
assert.deepEqual(res.slice(), data)
})
})

Expand Down

0 comments on commit 8d8633b

Please sign in to comment.