Skip to content

ERR_STREAM_WRITE_AFTER_END when piping request body #856

@alxndrsn

Description

@alxndrsn

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:

> 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:

> 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:

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

Checklist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions