From 034622926ef5d76985a9fdaf3ab82df27dfa067c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 7 Apr 2020 12:30:06 +0100 Subject: [PATCH] fix: destroy request body when we are aborting it midway though reading Workaround for https://github.com/node-fetch/node-fetch/issues/766 --- src/http.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/http.js b/src/http.js index f6fd1e0..2106335 100644 --- a/src/http.js +++ b/src/http.js @@ -262,6 +262,27 @@ const ndjson = async function * (source) { const streamToAsyncIterator = function (source) { if (isAsyncIterator(source)) { + // Workaround for https://github.com/node-fetch/node-fetch/issues/766 + if (source.writable && source.readable) { + const iter = source[Symbol.asyncIterator]() + + const wrapper = { + next: iter.next.bind(iter), + return: () => { + if (source.writableEnded) { + source.destroy() + } + + return iter.return() + }, + [Symbol.asyncIterator]: () => { + return wrapper + } + } + + return wrapper + } + return source }