Kaynağa Gözat

Merge pull request #13056 from overleaf/jpa-pipeline-express

[misc] use stream.pipeline in express libraries

GitOrigin-RevId: a6a50864552237b0c4d153d9f272cdc8d0f297ea
Jakob Ackermann 3 yıl önce
ebeveyn
işleme
67a21bdc69

+ 44 - 0
patches/body-parser+1.20.1.patch

@@ -0,0 +1,44 @@
+diff --git a/node_modules/body-parser/lib/read.js b/node_modules/body-parser/lib/read.js
+index fce6283..6131c31 100644
+--- a/node_modules/body-parser/lib/read.js
++++ b/node_modules/body-parser/lib/read.js
+@@ -18,7 +18,7 @@ var iconv = require('iconv-lite')
+ var onFinished = require('on-finished')
+ var unpipe = require('unpipe')
+ var zlib = require('zlib')
+-
++var Stream = require('stream')
+ /**
+  * Module exports.
+  */
+@@ -166,25 +166,25 @@ function contentstream (req, debug, inflate) {
+     case 'deflate':
+       stream = zlib.createInflate()
+       debug('inflate body')
+-      req.pipe(stream)
++      // req.pipe(stream)
+       break
+     case 'gzip':
+       stream = zlib.createGunzip()
+       debug('gunzip body')
+-      req.pipe(stream)
++      // req.pipe(stream)
+       break
+     case 'identity':
+       stream = req
+       stream.length = length
+-      break
++      return req
+     default:
+       throw createError(415, 'unsupported content encoding "' + encoding + '"', {
+         encoding: encoding,
+         type: 'encoding.unsupported'
+       })
+   }
+-
+-  return stream
++  var pass = new Stream.PassThrough(); Stream.pipeline(req, stream, pass, () => {})
++  return pass
+ }
+ 
+ /**

+ 57 - 0
patches/express++send+0.18.0.patch

@@ -0,0 +1,57 @@
+diff --git a/node_modules/express/node_modules/send/index.js b/node_modules/express/node_modules/send/index.js
+index 89afd7e..de56daf 100644
+--- a/node_modules/express/node_modules/send/index.js
++++ b/node_modules/express/node_modules/send/index.js
+@@ -789,29 +789,29 @@ SendStream.prototype.stream = function stream (path, options) {
+   // pipe
+   var stream = fs.createReadStream(path, options)
+   this.emit('stream', stream)
+-  stream.pipe(res)
+-
+-  // cleanup
+-  function cleanup () {
+-    destroy(stream, true)
+-  }
+-
+-  // response finished, cleanup
+-  onFinished(res, cleanup)
+-
+-  // error handling
+-  stream.on('error', function onerror (err) {
+-    // clean up stream early
+-    cleanup()
+-
+-    // error
+-    self.onStatError(err)
+-  })
+-
+-  // end
+-  stream.on('end', function onend () {
+-    self.emit('end')
+-  })
++  Stream.pipeline(stream, res, err => { if (err) { self.onStatError(err) } else { self.emit('end') } })
++
++  // // cleanup
++  // function cleanup () {
++  //   destroy(stream, true)
++  // }
++  //
++  // // response finished, cleanup
++  // onFinished(res, cleanup)
++  //
++  // // error handling
++  // stream.on('error', function onerror (err) {
++  //   // clean up stream early
++  //   cleanup()
++  //
++  //   // error
++  //   self.onStatError(err)
++  // })
++  //
++  // // end
++  // stream.on('end', function onend () {
++  //   self.emit('end')
++  // })
+ }
+ 
+ /**

+ 61 - 0
patches/send+0.17.2.patch

@@ -0,0 +1,61 @@
+diff --git a/node_modules/send/index.js b/node_modules/send/index.js
+index 06d7507..8854216 100644
+--- a/node_modules/send/index.js
++++ b/node_modules/send/index.js
+@@ -795,31 +795,31 @@ SendStream.prototype.stream = function stream (path, options) {
+   // pipe
+   var stream = fs.createReadStream(path, options)
+   this.emit('stream', stream)
+-  stream.pipe(res)
+-
+-  // response finished, done with the fd
+-  onFinished(res, function onfinished () {
+-    finished = true
+-    destroy(stream)
+-  })
+-
+-  // error handling code-smell
+-  stream.on('error', function onerror (err) {
+-    // request already finished
+-    if (finished) return
+-
+-    // clean up stream
+-    finished = true
+-    destroy(stream)
+-
+-    // error
+-    self.onStatError(err)
+-  })
+-
+-  // end
+-  stream.on('end', function onend () {
+-    self.emit('end')
+-  })
++  Stream.pipeline(stream, res, err => { if (err) { self.onStatError(err) } else { self.emit('end') } })
++
++  // // response finished, done with the fd
++  // onFinished(res, function onfinished () {
++  //   finished = true
++  //   destroy(stream)
++  // })
++  //
++  // // error handling code-smell
++  // stream.on('error', function onerror (err) {
++  //   // request already finished
++  //   if (finished) return
++  //
++  //   // clean up stream
++  //   finished = true
++  //   destroy(stream)
++  //
++  //   // error
++  //   self.onStatError(err)
++  // })
++  //
++  // // end
++  // stream.on('end', function onend () {
++  //   self.emit('end')
++  // })
+ }
+ 
+ /**