Skip to content

Commit

Permalink
Leave uploadProgress so it gets emitted on stream end
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Sep 2, 2022
1 parent 693de21 commit 8180a53
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

// The `!destroyed` check is required to prevent `uploadProgress` being emitted after the stream was destroyed
if (!error && this._request!.destroyed) {

This comment has been minimized.

Copy link
@yakov116

yakov116 Sep 5, 2022

I think you forgot to remove the comment by mistake

This comment has been minimized.

Copy link
@szmarczak

szmarczak Sep 5, 2022

Author Collaborator

yeah indeed, ty for noticing 🙏🏼

if (!error) {
this._bodySize = this._uploadedSize;

this.emit('uploadProgress', this.uploadProgress);
Expand Down
28 changes: 0 additions & 28 deletions test/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,31 +440,3 @@ test('formdata retry', withServer, async (t, server, got) => {
message: 'Cannot retry with consumed body stream',
});
});

test('does not emit uploadProgress after cancelation', withServer, async (t, server, got) => {
server.post('/', () => {});

const stream = got.stream.post();

stream.once('uploadProgress', () => { // 0%
stream.once('uploadProgress', () => { // 'foo'
stream.write('bar');

process.nextTick(() => {
process.nextTick(() => {
stream.on('uploadProgress', () => {
t.fail('Emitted uploadProgress after cancelation');
});

stream.destroy();
});
});
});
});

stream.write('foo');

await pEvent(stream, 'close');

t.pass();
});
30 changes: 30 additions & 0 deletions test/progress.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'process';
import {Buffer} from 'buffer';
import {promisify} from 'util';
import stream from 'stream';
Expand All @@ -11,6 +12,7 @@ import {temporaryFile} from 'tempy';
import is from '@sindresorhus/is';
import test, {type ExecutionContext} from 'ava';
import type {Handler} from 'express';
import {pEvent} from 'p-event';
import type {Progress} from '../source/index.js';
import withServer from './helpers/with-server.js';

Expand Down Expand Up @@ -262,3 +264,31 @@ test('upload progress - one event when removed listener', withServer, async (t,
},
]);
});

test('does not emit uploadProgress after cancelation', withServer, async (t, server, got) => {
server.post('/', () => {});

const stream = got.stream.post();

stream.once('uploadProgress', () => { // 0%
stream.once('uploadProgress', () => { // 'foo'
stream.write('bar');

process.nextTick(() => {
process.nextTick(() => {
stream.on('uploadProgress', () => {
t.fail('Emitted uploadProgress after cancelation');
});

stream.destroy();
});
});
});
});

stream.write('foo');

await pEvent(stream, 'close');

t.pass();
});

0 comments on commit 8180a53

Please sign in to comment.