forwardemail / forwardemail/supertest

ERR_STREAM_WRITE_AFTER_END when piping request body

Open
#856 5 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
14.4k
Forks
782
PR merge metrics
No merged PRs in 30d

Description

## Describe the bug

**Node.js version:** v22.14.0

**supertest version:** 7.1.0

**Description:** in some circumstances, calling `stream.pipe(supertestRequest)` can throw `ERR_STREAM_WRITE_AFTER_END`

## Actual behavior

`stream.pipe(supertestRequest)` throws `ERR_STREAM_WRITE_AFTER_END`

## Expected behavior

Should pipe the stream to the request.

## Code to reproduce

The following requests with different stream types have different results with https://httpbin.org/post.

Changing the server name (e.g. https://httpbingo.org) can cause both types of streams to fail with `ERR_STREAM_WRITE_AFTER_END`, so server behaviour may be a factor.

### working:

```js
> req = require('supertest')('https://httpbin.org').post('/post'); stream.Readable.from('hi').pipe(req, { end: false }); try { await req; console.log({ status:rez.status, headers:rez.headers, body:rez.body }) } catch(err) { console.log('Caught:', err) }
{
status: 200,
...
```

### broken:

```js
> req = require('supertest')('https://httpbin.org').post('/post'); rs = fs.createReadStream('./package.json'); rs.pipe(req, { end: false }); try { await req; console.log({ status:rez.status, headers:rez.headers, body:rez.body }) } catch(err) { console.log('Caught:', err) }
Uncaught Error [ERR_STREAM_WRITE_AFTER_END]: write after end
```

### Reference:

```js
> await fetch('https://httpbin.org/post', { method:'POST', body:stream.Readable.from('hi'), duplex:'half' })
Response {
status: 200,
...
```

```js
> await fetch('https://httpbin.org/post', { method:'POST', body:fs.createReadStream('./package.json'), duplex:'half' })
Response {
status: 200,
...
```

## Checklist

- [x] I have searched through GitHub issues for similar issues.
- [ ] I have completely read through the README and documentation.
- [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the commands with Node.js v22.14.0 and supertest 7.1.0, comparing stream.Readable.from('hi') with fs.createReadStream('./package.json') when piping to a request. Investigate the request-stream entry point and add a regression test covering both stream types; done means neither produces ERR_STREAM_WRITE_AFTER_END against the reported servers.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.