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: add support for passing encoder/decoder #2

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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