Skip to content
Draft
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
12 changes: 10 additions & 2 deletions core/packages/teeny-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,18 @@ function teenyRequest(
let responseStream: any;
requestStream.once('reading', () => {
if (responseStream) {
pipeline(responseStream, requestStream, () => {});
if (!requestStream.destroyed) {
pipeline(responseStream, requestStream, () => {});
} else if (responseStream && typeof responseStream.destroy === 'function') {
responseStream.destroy();
}
Comment on lines +281 to +285

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check responseStream && is redundant here because this block is already nested inside if (responseStream) on line 280. We can safely simplify this condition.

        if (!requestStream.destroyed) {
          pipeline(responseStream, requestStream, () => {});
        } else if (typeof responseStream.destroy === 'function') {
          responseStream.destroy();
        }

} else {
requestStream.once('response', () => {
pipeline(responseStream, requestStream, () => {});
if (!requestStream.destroyed) {
pipeline(responseStream, requestStream, () => {});
} else if (responseStream && typeof responseStream.destroy === 'function') {
responseStream.destroy();
}
});
}
});
Expand Down
19 changes: 19 additions & 0 deletions core/packages/teeny-request/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,25 @@ describe('teeny', () => {
});
});

it('should not pipe if request stream is destroyed early', done => {
const scope = mockJson();

const stream = teenyRequest({uri});
stream.on('error', done);

stream.once('response', response => {
response.body.once('close', () => {
scope.done();
done();
});

// Destroy the stream before piping can be fully set up
stream.destroy();
});

stream.resume();
});
Comment on lines +301 to +318

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using an arbitrary setTimeout in asynchronous tests can lead to flakiness in slower CI/CD environments. Since we expect the stream to be destroyed, we can listen directly to the close event of the response body to resolve the test, which is more robust and faster.

  it('should not pipe if request stream is destroyed early', done => {
    const scope = mockJson();

    const stream = teenyRequest({uri});
    stream.on('error', done);

    stream.once('response', response => {
      response.body.once('close', () => {
        scope.done();
        done();
      });

      // Destroy the stream before piping can be fully set up
      stream.destroy();
    });

    stream.resume();
  });


it('should expose TeenyStatistics instance', () => {
assert.ok(teenyRequest.stats instanceof TeenyStatistics);
});
Expand Down
Loading