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: propagate duplex transform sink errors #8

Merged
merged 3 commits into from
Jan 13, 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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@
"license": "MIT",
"devDependencies": {
"aegir": "^36.1.3",
"delay": "^5.0.0",
"it-all": "^1.0.6",
"it-drain": "^1.0.5",
"streaming-iterables": "^6.0.0"
},
"dependencies": {
"it-merge": "^1.0.4",
"it-pushable": "^2.0.0",
"it-stream-types": "^1.0.3"
},
"repository": {
Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { pushable } from 'it-pushable'
import merge from 'it-merge'
import type * as it from 'it-stream-types'

export const rawPipe = (...fns: any) => {
Expand All @@ -22,7 +24,19 @@ export const isDuplex = <TSource, TSink = TSource, RSink = Promise<void>> (obj:

const duplexPipelineFn = <TSource> (duplex: any) => {
return (source: any): it.Source<TSource> => {
duplex.sink(source) // TODO: error on sink side is unhandled rejection - this is the same as pull streams
const p = duplex.sink(source)

if (p.then != null) {
const stream = pushable<TSource>()
p.then(() => {
stream.end()
}, (err: Error) => {
stream.end(err)
})

return merge(stream, duplex.source)
}

return duplex.source
}
}
Expand Down
23 changes: 22 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { pipe } from '../src/index.js'
import all from 'it-all'
import drain from 'it-drain'
import { filter, collect, consume } from 'streaming-iterables'
import type { Duplex } from 'it-stream-types'
import delay from 'delay'
import type { Duplex, Source } from 'it-stream-types'

const oneTwoThree = () => [1, 2, 3]

Expand Down Expand Up @@ -116,4 +117,24 @@ describe('it-pipe', () => {

expect(result).to.be.undefined()
})

it('should propagate duplex transform sink errors', async () => {
const err = new Error('Aaargh')

await expect(
pipe(
oneTwoThree, {
source: (async function * () {
await delay(1000)
yield 5
}()),
sink: async (source: Source<number>) => {
await delay(20)
throw err
}
},
async (source) => await drain(source)
)
).to.eventually.be.rejected.with.property('message', err.message)
})
})