Skip to content

Commit

Permalink
fix: output uint8arrays (#46)
Browse files Browse the repository at this point in the history
In order to pipe lp.encode output straight to sockets, etc, output
individual uint8arrays instead of wrapping them in a list.
  • Loading branch information
achingbrain authored Jul 28, 2022
1 parent 257e8d5 commit 6288e61
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
10 changes: 4 additions & 6 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ const defaultEncoder: LengthEncoderFunction = (length) => {
}
defaultEncoder.bytes = 0

export function encode (options?: EncoderOptions): Transform<Uint8ArrayList | Uint8Array, Uint8ArrayList> {
export function encode (options?: EncoderOptions): Transform<Uint8ArrayList | Uint8Array, Uint8Array> {
options = options ?? {}

const encodeLength = options.lengthEncoder ?? defaultEncoder

const encoder = async function * (source: Source<Uint8ArrayList | Uint8Array>): Source<Uint8ArrayList> {
const encoder = async function * (source: Source<Uint8ArrayList | Uint8Array>): Source<Uint8Array> {
for await (const chunk of source) {
// length + data
yield new Uint8ArrayList(
encodeLength(chunk.byteLength),
chunk
)
yield encodeLength(chunk.byteLength).subarray()
yield chunk
}
}

Expand Down
54 changes: 39 additions & 15 deletions test/encode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,38 @@ import { int32BEEncode } from './helpers/int32BE-encode.js'
describe('encode', () => {
it('should encode length as prefix', async () => {
const input = await Promise.all(times(randomInt(1, 10), someBytes))
const output = await pipe(input, lp.encode(), async (source) => await all(source))
output.forEach((o, i) => {
const length = unsigned.decode(o)
expect(length).to.equal(input[i].length)
expect(o.slice(unsigned.encodingLength(length))).to.deep.equal(input[i])
})
const output = await pipe(
input,
lp.encode(),
async (source) => await all(source)
)

let inputIndex = 0

for (let i = 0; i < output.length; i += 2, inputIndex++) {
const prefix = output[i]
const data = output[i + 1]

const length = unsigned.decode(prefix)
expect(length).to.equal(data.length)
expect(data).to.deep.equal(input[inputIndex])
}
})

it('should encode zero length as prefix', async () => {
const input = [new Uint8Array(0)]
const output = await pipe(input, lp.encode(), async (source) => await all(source))
output.forEach((o, i) => {
const length = unsigned.decode(o)
expect(length).to.equal(input[i].length)
expect(o.slice(unsigned.encodingLength(length))).to.deep.equal(input[i])
})

let inputIndex = 0

for (let i = 0; i < output.length; i += 2, inputIndex++) {
const prefix = output[i]
const data = output[i + 1]

const length = unsigned.decode(prefix)
expect(length).to.equal(data.length)
expect(data).to.deep.equal(input[inputIndex])
}
})

it('should encode with custom length encoder (int32BE)', async () => {
Expand All @@ -35,9 +51,17 @@ describe('encode', () => {
lp.encode({ lengthEncoder: int32BEEncode }),
async (source) => await all(source)
)
output.forEach((o, i) => {
const length = o.getUint32(0, false)
expect(length).to.equal(input[i].length)
})

let inputIndex = 0

for (let i = 0; i < output.length; i += 2, inputIndex++) {
const prefix = output[i]
const data = output[i + 1]

const view = new DataView(prefix.buffer)
const length = view.getUint32(0, false)
expect(length).to.equal(data.length)
expect(length).to.equal(input[inputIndex].length)
}
})
})

0 comments on commit 6288e61

Please sign in to comment.