Skip to content

Commit dffd60f

Browse files
authored
fix(streaming) Prevent console.error(undefined) (#3747)
1 parent baa231e commit dffd60f

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/helper/streaming/stream.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ describe('Basic Streaming Helper', () => {
7171
expect(aborted).toBeTruthy()
7272
})
7373

74+
it('Check stream Response if pipe is aborted by abort signal', async () => {
75+
const ac = new AbortController()
76+
const req = new Request('http://localhost/', { signal: ac.signal })
77+
const c = new Context(req)
78+
79+
let aborted = false
80+
const res = stream(c, async (stream) => {
81+
stream.onAbort(() => {
82+
aborted = true
83+
})
84+
await stream.pipe(new ReadableStream())
85+
})
86+
if (!res.body) {
87+
throw new Error('Body is null')
88+
}
89+
const reader = res.body.getReader()
90+
const pReading = reader.read()
91+
ac.abort()
92+
await pReading
93+
expect(aborted).toBeTruthy()
94+
})
95+
7496
it('Check stream Response if error occurred', async () => {
7597
const onError = vi.fn()
7698
const res = stream(

src/helper/streaming/stream.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ export const stream = (
2222
try {
2323
await cb(stream)
2424
} catch (e) {
25-
if (e instanceof Error && onError) {
25+
if (e === undefined) {
26+
// If reading is canceled without a reason value (e.g. by StreamingApi)
27+
// then the .pipeTo() promise will reject with undefined.
28+
// In this case, do nothing because the stream is already closed.
29+
} else if (e instanceof Error && onError) {
2630
await onError(e, stream)
2731
} else {
2832
console.error(e)

0 commit comments

Comments
 (0)