Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions core/packages/teeny-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,19 @@ function teenyRequest(
const requestStream = streamEvents(new PassThrough());
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let responseStream: any;
const pipeResponseStream = () => {
if (requestStream.destroyed) {
responseStream.destroy();
return;
}
Comment thread
rockwotj marked this conversation as resolved.

pipeline(responseStream, requestStream, () => {});
};
requestStream.once('reading', () => {
if (responseStream) {
pipeline(responseStream, requestStream, () => {});
pipeResponseStream();
} else {
requestStream.once('response', () => {
pipeline(responseStream, requestStream, () => {});
});
requestStream.once('response', pipeResponseStream);
}
});
options.compress = false;
Expand Down
17 changes: 17 additions & 0 deletions core/packages/teeny-request/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ describe('teeny', () => {
});
});

it('should discard the response if the request stream is destroyed before piping', async () => {
const scope = mockJson();
const stream = teenyRequest({uri});
const responseClosed = new Promise<void>((resolve, reject) => {
stream.once('error', reject);
stream.once('response', response => {
response.body.once('error', reject);
response.body.once('close', resolve);
stream.destroy();
});
});

stream.resume();
await responseClosed;
scope.done();
});

it('should not pipe response stream to user unless they ask for it', async () => {
const scope = mockJson();
const stream = teenyRequest({uri}).on('error', err => {
Expand Down
Loading