diff --git a/README.md b/README.md index 79a8b81..b1713f5 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/index.js b/src/index.js index a255aed..4e50cc1 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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() @@ -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 diff --git a/test/index.js b/test/index.js index 3194f8a..82c871e 100644 --- a/test/index.js +++ b/test/index.js @@ -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 */ @@ -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) }) })