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

fix: correct return type #29

Merged
merged 1 commit into from
Aug 11, 2022
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
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