Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
multipart: better backpressure: waiting for drain before resuming fil…
Browse files Browse the repository at this point in the history
…e content
  • Loading branch information
pgte committed Nov 20, 2017
1 parent 1a0eafa commit 782beb9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/utils/multipart.js
Expand Up @@ -8,7 +8,7 @@ const NEW_LINE_BUFFER = Buffer.from(NEW_LINE)

class Multipart extends Transform {
constructor (options) {
super(Object.assign({}, options, { objectMode: true }))
super(Object.assign({}, options, { objectMode: true, highWaterMark: 1 }))

this._boundary = this._generateBoundary()
this._files = []
Expand Down Expand Up @@ -90,7 +90,11 @@ class Multipart extends Transform {
})

content.on('data', (data) => {
this.push(data)
const drained = this.push(data)
if (!drained) {
content.pause()
this.once('drain', () => content.resume())
}
})
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/send-files-stream.js
Expand Up @@ -146,6 +146,10 @@ module.exports = (send, path) => {
response.pipe(convertedResponse)
})

// signal the multipart that the underlying stream has drained and that
// it can continue producing data..
request.on('drain', () => multipart.emit('drain'))

multipart.pipe(request)

callback(null, retStream)
Expand Down

0 comments on commit 782beb9

Please sign in to comment.