Skip to content

Commit 31d80ef

Browse files
committed
Apply destroy patch
1 parent 2675046 commit 31d80ef

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

source/core/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import http2wrapper = require('http2-wrapper');
1515
import lowercaseKeys = require('lowercase-keys');
1616
import ResponseLike = require('responselike');
1717
import is, {assert} from '@sindresorhus/is';
18+
import applyDestroyPatch from './utils/apply-destroy-patch';
1819
import getBodySize from './utils/get-body-size';
1920
import isFormData from './utils/is-form-data';
2021
import proxyEvents from './utils/proxy-events';
@@ -1379,6 +1380,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
13791380
highWaterMark: 0
13801381
});
13811382

1383+
// TODO: Remove this when targeting Node.js 14
1384+
applyDestroyPatch(this);
1385+
13821386
this[kDownloadedSize] = 0;
13831387
this[kUploadedSize] = 0;
13841388
this.requestInitialized = false;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Readable, Writable} from 'stream';
2+
3+
export default function applyDestroyPatch(stream: Readable | Writable): void {
4+
const kDestroy = Symbol('destroy');
5+
6+
if (Number(process.versions.node.split('.')[0]) >= 14) {
7+
return;
8+
}
9+
10+
// @ts-expect-error
11+
stream[kDestroy] = stream.destroy;
12+
stream.destroy = (...args) => {
13+
if (!stream.destroyed) {
14+
// @ts-expect-error
15+
return stream[kDestroy](...args);
16+
}
17+
};
18+
}

test/stream.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as FormData from 'form-data';
1111
import is from '@sindresorhus/is';
1212
import got, {RequestError} from '../source/index';
1313
import withServer from './helpers/with-server';
14+
import delay = require('delay');
1415

1516
const pStreamPipeline = promisify(stream.pipeline);
1617

@@ -419,6 +420,22 @@ test('async iterator works', withServer, async (t, server, got) => {
419420
t.is(Buffer.concat(chunks).toString(), payload);
420421
});
421422

423+
test('destroys only once', async t => {
424+
const stream = got.stream('https://example.com');
425+
stream.destroy();
426+
stream.destroy(new Error('oh no'));
427+
428+
let errored = false;
429+
430+
stream.once('error', () => {
431+
errored = true;
432+
});
433+
434+
await delay(1);
435+
436+
t.false(errored);
437+
});
438+
422439
if (Number.parseInt(process.versions.node.split('.')[0]!, 10) <= 12) {
423440
test('does not emit end event on error', withServer, async (t, server, got) => {
424441
server.get('/', infiniteHandler);

0 commit comments

Comments
 (0)