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

Commit 782beb9

Browse files
committed
multipart: better backpressure: waiting for drain before resuming file content
1 parent 1a0eafa commit 782beb9

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/utils/multipart.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const NEW_LINE_BUFFER = Buffer.from(NEW_LINE)
88

99
class Multipart extends Transform {
1010
constructor (options) {
11-
super(Object.assign({}, options, { objectMode: true }))
11+
super(Object.assign({}, options, { objectMode: true, highWaterMark: 1 }))
1212

1313
this._boundary = this._generateBoundary()
1414
this._files = []
@@ -90,7 +90,11 @@ class Multipart extends Transform {
9090
})
9191

9292
content.on('data', (data) => {
93-
this.push(data)
93+
const drained = this.push(data)
94+
if (!drained) {
95+
content.pause()
96+
this.once('drain', () => content.resume())
97+
}
9498
})
9599
}
96100

src/utils/send-files-stream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ module.exports = (send, path) => {
146146
response.pipe(convertedResponse)
147147
})
148148

149+
// signal the multipart that the underlying stream has drained and that
150+
// it can continue producing data..
151+
request.on('drain', () => multipart.emit('drain'))
152+
149153
multipart.pipe(request)
150154

151155
callback(null, retStream)

0 commit comments

Comments
 (0)