Skip to content

Commit deb84e4

Browse files
committed
fix(goods): handle errors in responseToReadable stream reader
Fixes #1443
1 parent 98531fc commit deb84e4

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/goods.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ const responseToReadable = (response: Response, rs: Readable) => {
111111
return rs
112112
}
113113
rs._read = async () => {
114-
const result = await reader.read()
115-
rs.push(result.done ? null : Buffer.from(result.value))
114+
try {
115+
const result = await reader.read()
116+
rs.push(result.done ? null : Buffer.from(result.value))
117+
} catch (err) {
118+
rs.destroy(err as Error)
119+
}
116120
}
117121
return rs
118122
}

test/goods.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import assert from 'node:assert'
1616
import { test, describe, after } from 'node:test'
17-
import { Duplex } from 'node:stream'
17+
import { Duplex, Readable } from 'node:stream'
1818
import { $, chalk, fs, path, dotenv } from '../src/index.ts'
1919
import {
2020
echo,
@@ -365,6 +365,34 @@ describe('goods', () => {
365365
assert(p3.includes('GitHub'))
366366
})
367367

368+
test('responseToReadable propagates reader errors to stream', async () => {
369+
const error = new Error('stream read failed')
370+
const mockReader = {
371+
read: () => Promise.reject(error),
372+
}
373+
374+
const rs = new Readable({ read() {} })
375+
376+
// Simulate what responseToReadable does with the fix applied
377+
rs._read = async () => {
378+
try {
379+
const result = await mockReader.read()
380+
rs.push(result.done ? null : Buffer.from(result.value))
381+
} catch (err) {
382+
rs.destroy(err as Error)
383+
}
384+
}
385+
386+
const errorPromise = new Promise<Error>((resolve) => {
387+
rs.on('error', resolve)
388+
})
389+
390+
rs.read()
391+
392+
const receivedError = await errorPromise
393+
assert.equal(receivedError.message, 'stream read failed')
394+
})
395+
368396
describe('dotenv', () => {
369397
test('parse()', () => {
370398
assert.deepEqual(dotenv.parse(''), {})

0 commit comments

Comments
 (0)