multer icon indicating copy to clipboard operation
multer copied to clipboard

Add `startProcessing` config for pre-processed request objects

Open emadalam opened this issue 4 years ago • 8 comments

The following has been added to allow working with pre-processed request objects, eg those in cloud functions and similar environments.

  • startProcessing config
  • 📚 Documentation update and sample usage
  • 🚨 Tests to cover existing and updated use cases

Example:

var express = require('express')
var app = express()
var multer  = require('multer')
var upload = multer({
  startProcessing (req, busboy) {
    if (req.rawBody) { // indicates the request was pre-processed
      busboy.end(req.rawBody)
    } else {
      req.pipe(busboy)
    }
  }
})

app.post('/profile', upload.none(), function (req, res, next) {
  // req.body contains the text fields
})

emadalam avatar Oct 22 '19 15:10 emadalam

Will this be merged? I am using this fork with good success and multer really needs something to support Google Cloud Function requests :S

TheAmazingPT avatar Jan 21 '20 15:01 TheAmazingPT

@TheAmazingPT Same here, I've been using this myself on production since the day I created this PR, no fallouts or errors so far.

I've added everything possible, tests, documentation etc, to make this PR ready for review and merge. It's up to the library maintainers to decide whether they want to review and merge this or provide something else to merge this 🤷‍♂

emadalam avatar Jan 23 '20 19:01 emadalam

It would be nice to have something (or similar) like this. It could be a simple option useRawBody which is default false, and a few line of code in the make-middleware.js

if (options.useRawBody && req.rawBody) {
  busboy.end(req.rawBody)
} else {
  req.pipe(busboy)
}

With such an option we could use multer with google cloud functions and don't have to reinvent the wheel.

gmarcz avatar Jan 16 '21 19:01 gmarcz

any updates on this...

ezzabuzaid avatar Apr 05 '21 14:04 ezzabuzaid

I would love this as well!

Parth-2412 avatar May 25 '22 17:05 Parth-2412

Sorry for not seeing this earlier. I'm not sure that I'm comfortable adding this to Multer since it exposes a lot of the internals, which means that we won't be able to change it without it being a breaking change.

Is this really the only way that Express works on Google Cloud Function? Won't every single middleware need to be updated then? How does express.json() and similar middleware deal with this?

Another way that wouldn't expose any internals would be to add support for rawBody internally in the library. But I still think it's very strange that GCF took the decision to completely change how the body works. Is this documented somewhere?

LinusU avatar May 29 '22 08:05 LinusU

Hi @LinusU

I had to build an abstraction over busboy directly to have it work. It's been a while for this issue so I do not remember why Multer didn't work before. You might refer to this page GCF busboy the last line which says more about why this PR is needed.

ezzabuzaid avatar May 29 '22 11:05 ezzabuzaid

any updates here?

laruss avatar Nov 08 '22 14:11 laruss