Skip to content

Commit 693de21

Browse files
committedSep 2, 2022
Attempt to skip emitting uploadProgress after destroy
1 parent b671480 commit 693de21

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
 

‎source/core/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ export default class Request extends Duplex implements RequestEvents<Request> {
483483
return;
484484
}
485485

486-
if (!error) {
486+
// The `!destroyed` check is required to prevent `uploadProgress` being emitted after the stream was destroyed
487+
if (!error && this._request!.destroyed) {
487488
this._bodySize = this._uploadedSize;
488489

489490
this.emit('uploadProgress', this.uploadProgress);
@@ -1178,7 +1179,8 @@ export default class Request extends Duplex implements RequestEvents<Request> {
11781179
}
11791180

11801181
this._request.write(chunk, encoding!, (error?: Error | null) => { // eslint-disable-line @typescript-eslint/ban-types
1181-
if (!error) {
1182+
// The `!destroyed` check is required to prevent `uploadProgress` being emitted after the stream was destroyed
1183+
if (!error && !this._request!.destroyed) {
11821184
this._uploadedSize += Buffer.byteLength(chunk, encoding);
11831185

11841186
const progress = this.uploadProgress;

‎test/post.ts

+28
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,31 @@ test('formdata retry', withServer, async (t, server, got) => {
440440
message: 'Cannot retry with consumed body stream',
441441
});
442442
});
443+
444+
test('does not emit uploadProgress after cancelation', withServer, async (t, server, got) => {
445+
server.post('/', () => {});
446+
447+
const stream = got.stream.post();
448+
449+
stream.once('uploadProgress', () => { // 0%
450+
stream.once('uploadProgress', () => { // 'foo'
451+
stream.write('bar');
452+
453+
process.nextTick(() => {
454+
process.nextTick(() => {
455+
stream.on('uploadProgress', () => {
456+
t.fail('Emitted uploadProgress after cancelation');
457+
});
458+
459+
stream.destroy();
460+
});
461+
});
462+
});
463+
});
464+
465+
stream.write('foo');
466+
467+
await pEvent(stream, 'close');
468+
469+
t.pass();
470+
});

0 commit comments

Comments
 (0)
Please sign in to comment.