Skip to content

Commit 1f7506d

Browse files
authored
fix: destroy request body when we are aborting it midway though r… (#31)
Workaround for node-fetch/node-fetch#766
1 parent dae0483 commit 1f7506d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/http.js

+21
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,27 @@ const ndjson = async function * (source) {
262262

263263
const streamToAsyncIterator = function (source) {
264264
if (isAsyncIterator(source)) {
265+
// Workaround for https://github.com/node-fetch/node-fetch/issues/766
266+
if (source.writable && source.readable) {
267+
const iter = source[Symbol.asyncIterator]()
268+
269+
const wrapper = {
270+
next: iter.next.bind(iter),
271+
return: () => {
272+
if (source.writableEnded) {
273+
source.destroy()
274+
}
275+
276+
return iter.return()
277+
},
278+
[Symbol.asyncIterator]: () => {
279+
return wrapper
280+
}
281+
}
282+
283+
return wrapper
284+
}
285+
265286
return source
266287
}
267288

0 commit comments

Comments
 (0)