mafintosh / mafintosh/pump

Question: when not to use `pump`?

Open
#52 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
917
Forks
64
PR merge metrics
No merged PRs in 30d

Description

Consider this example.

As soon as stream data have been collected, the HTTP connection is closed just before `someAsyncStuff` is invoked. Refactoring `pump(req, busboy...` to `req.pipe(busboy)` fixes the issue. It's also my understanding that using `pump` is a best practice for piping streams (for error handling and some clean-up), but in this case it introduces an issue. I would like to understand why this example is not working as I was expecting.

```typescript
import * as Busboy from 'busboy'
import * as express from 'express'
import * as pump from 'pump'
import concat = require('concat-stream')

const app = express()

app.post('/', (req, res) => {
const busboy = new Busboy({ headers: req.headers })

busboy.on('file', (_, file) => {
pump(
file,
concat(buffer =>
someAsyncStuff(buffer.toString())
.then(length => res.send({ length }))
.catch(err => res.status(500).send(err.message))
),
err => {
if (err) res.status(500).send(err.message)
}
)
})

pump(req, busboy, err => {
if (err) res.status(500).send(err.message)
})
})

function someAsyncStuff(s: string): Promise {
return new Promise(resolve => setTimeout(() => resolve(s.length), 1))
}

app.listen('3000')
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the TypeScript example in the issue and compare pump(req, busboy) with req.pipe(busboy). Trace the two pump calls, including the file stream and concat-stream callback, to determine why the HTTP connection closes before someAsyncStuff completes. Done means documenting when this usage of pump differs from piping directly and explaining the observed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.