Skip to content

Commit

Permalink
fix: request body cleanup on abort (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-guggisberg authored Feb 3, 2022
1 parent ce57747 commit 229a05c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ const fetch = async (ctx, url, options) => {
const err = new AbortError('The operation was aborted.');
// cleanup request
/* istanbul ignore else */
if (req.body instanceof Readable) {
req.body.destroy(err);
if (req.init.body instanceof Readable) {
req.init.body.destroy(err);
}
throw err;
}
Expand All @@ -83,8 +83,8 @@ const fetch = async (ctx, url, options) => {
});
} catch (err) {
// cleanup request
if (body instanceof Readable) {
body.destroy(err);
if (initBody instanceof Readable) {
initBody.destroy(err);
}
/* istanbul ignore next */
if (err instanceof TypeError) {
Expand All @@ -104,8 +104,8 @@ const fetch = async (ctx, url, options) => {
const err = new AbortError('The operation was aborted.');
// cleanup request
/* istanbul ignore else */
if (req.body instanceof Readable) {
req.body.destroy(err);
if (req.init.body instanceof Readable) {
req.init.body.destroy(err);
}
// propagate error on response stream
coreResp.readable.emit('error', err);
Expand Down
16 changes: 16 additions & 0 deletions test/fetch/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ testParams.forEach((params) => {
assert(body.destroyed);
});

it('AbortController works (POST with string body)', async () => {
const controller = new AbortController();
setTimeout(() => controller.abort(), 5);
const { signal } = controller;

const method = 'POST';
const body = 'hello, world!';

try {
await fetch(`${baseUrl}/post`, { signal, method, body });
assert.fail();
} catch (err) {
assert(err instanceof AbortError);
}
});

it('AbortController works (dripping response)', async function test() {
this.timeout(5000);

Expand Down

0 comments on commit 229a05c

Please sign in to comment.