From 212052c519dc40230677c5c3edde72c45e92efb9 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 10 Aug 2022 22:37:58 +0100 Subject: [PATCH] fix: update return type Because we use `it-reader` to read fixed-length chunks out of the stream to do the handshake, and then pipe the rest of the stream through the reader, we'll always transform the stream output to `Uint8ArrayList`s before yielding it. Update the types to reflect this. --- package.json | 1 + src/index.ts | 10 +++++----- test/index.spec.ts | 14 +++++++------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 3ae6694..6b5fe6b 100644 --- a/package.json +++ b/package.json @@ -124,6 +124,7 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", "dep-check": "aegir dep-check", "build": "aegir build", diff --git a/src/index.ts b/src/index.ts index 637182c..b24b0aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,17 +5,17 @@ import type { Duplex, Source } from 'it-stream-types' import type { Pushable } from 'it-pushable' import type { Uint8ArrayList } from 'uint8arraylist' -export interface Handshake { +export interface Handshake { reader: Reader writer: Pushable - stream: Duplex + stream: Duplex rest: () => Source write: (data: TSink) => void read: () => Promise } // Convert a duplex stream into a reader and writer and rest stream -export function handshake (stream: Duplex): Handshake { +export function handshake (stream: Duplex): Handshake { const writer = pushable() // Write bytes on demand to the sink const source = reader(stream.source) // Read bytes on demand from the source @@ -33,7 +33,7 @@ export function handshake = { + const rest: Duplex = { sink: async source => { if (sinkErr != null) { return await Promise.reject(sinkErr) @@ -42,7 +42,7 @@ export function handshake { rShake.stream, (source) => (async function * () { for await (const message of source) { - expect(message).to.eql(buffer) - yield message + expect(message.subarray()).to.eql(buffer) + yield message.subarray() } })(), rShake.stream ) const data = await pipe([buffer], iShake.stream, async (source) => await all(source)) - expect(data).to.eql([buffer]) + expect(data[0].subarray()).to.eql(buffer) }) it('should be able to perform a handshake via Uint8ArrayList', async () => { const [initiator, responder] = duplexPair() @@ -96,7 +96,7 @@ describe('handshake', () => { rShake2.stream, (source) => (async function * () { for await (const message of source) { - yield message + yield message.subarray() } })(), rShake2.stream @@ -104,7 +104,7 @@ describe('handshake', () => { const buffer = uint8ArrayFromString('more data') const data = await pipe([buffer], iShake2.stream, async (source) => await all(source)) - expect(data).to.eql([buffer]) + expect(data[0].subarray()).to.eql(buffer) }) it('should persist data across handshakes', async () => { @@ -141,7 +141,7 @@ describe('handshake', () => { rShake2.stream, (source) => (async function * () { for await (const message of source) { - yield message + yield message.subarray() } })(), rShake2.stream @@ -149,7 +149,7 @@ describe('handshake', () => { const buffer = uint8ArrayFromString('more data') const data = await pipe([buffer], iShake2.stream, async (source) => await all(source)) - expect(data).to.have.nested.property('[0]').that.equalBytes(buffer) + expect(data[0].subarray()).to.eql(buffer) }) it('should survive an exploding sink while doing other async work', async () => {