Skip to content

Commit

Permalink
fix: correct return type (#29)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achingbrain committed Aug 11, 2022
1 parent 12cc27d commit 50baa15
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
]
},
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"build": "aegir build",
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TSource extends Uint8Array | Uint8ArrayList = Uint8Array, TSink extends Uint8Array | Uint8ArrayList = TSource> {
export interface Handshake<TSink = Uint8Array | Uint8ArrayList> {
reader: Reader
writer: Pushable<TSink>
stream: Duplex<TSource, TSink>
stream: Duplex<Uint8ArrayList, TSink>
rest: () => Source<TSink>
write: (data: TSink) => void
read: () => Promise<Uint8ArrayList | undefined>
}

// Convert a duplex stream into a reader and writer and rest stream
export function handshake<TSource extends Uint8Array | Uint8ArrayList = Uint8Array, TSink extends Uint8Array | Uint8ArrayList = TSource> (stream: Duplex<TSource, TSink>): Handshake<TSource, TSink> {
export function handshake<TSink extends Uint8ArrayList | Uint8Array = Uint8ArrayList> (stream: Duplex<Uint8ArrayList | Uint8Array, TSink>): Handshake<TSink> {
const writer = pushable<TSink>() // Write bytes on demand to the sink
const source = reader(stream.source) // Read bytes on demand from the source

Expand All @@ -33,7 +33,7 @@ export function handshake<TSource extends Uint8Array | Uint8ArrayList = Uint8Arr
sinkErr = err
})

const rest: Duplex<TSource, TSink> = {
const rest: Duplex<Uint8ArrayList, TSink> = {
sink: async source => {
if (sinkErr != null) {
return await Promise.reject(sinkErr)
Expand All @@ -42,7 +42,7 @@ export function handshake<TSource extends Uint8Array | Uint8ArrayList = Uint8Arr
sourcePromise.resolve(source)
return await sinkPromise
},
source: stream.source
source
}

return {
Expand Down
14 changes: 7 additions & 7 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ describe('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<Uint8ArrayList>()
Expand Down Expand Up @@ -96,15 +96,15 @@ describe('handshake', () => {
rShake2.stream,
(source) => (async function * () {
for await (const message of source) {
yield message
yield message.subarray()
}
})(),
rShake2.stream
)

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 () => {
Expand Down Expand Up @@ -141,15 +141,15 @@ describe('handshake', () => {
rShake2.stream,
(source) => (async function * () {
for await (const message of source) {
yield message
yield message.subarray()
}
})(),
rShake2.stream
)

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 () => {
Expand Down

0 comments on commit 50baa15

Please sign in to comment.