mscdex / connect-busboy

Connect middleware for busboy
MIT License
155 stars 25 forks source link

"Finish" and "field" events does not work when "file" event is added. #24

Closed soubhikchatterjee closed 2 years ago

soubhikchatterjee commented 2 years ago

For eg:

This works!

const registerBusboyFields = (req, res, next) => {
  req.uploadBody = {};

  req.busboy.on("field", function (name, value) {
    req.uploadBody[name] = value;
    console.log(name, value);
  });

  // Finished all busboy events
  req.busboy.on("finish", () => {
    // The finish event is triggered...
    console.log(req.uploadBody);
    next();
  });

  req.pipe(req.busboy);
};

This does not work :(

const registerBusboyFields = (req, res, next) => {
  req.uploadBody = {};
  req.busboy.on("file", function (name, file, info) {
    req.uploadBody[name] = info;
    console.log(info);
  });

  req.busboy.on("field", function (name, value) {
    req.uploadBody[name] = value;
    console.log(name, value);
  });

  // Finished all busboy events
  req.busboy.on("finish", () => {
    // The finish event is NOT triggered...
    console.log(req.uploadBody);
    next();
  });

  req.pipe(req.busboy);
};

PS: When i say "does not work", i mean, i just see the console.log happening on "file" event, but console.log does neither happen on "field" event not in "finish" event.

Am I doing something wrong?

mscdex commented 2 years ago

You have to completely consume file in one way or another in order to parse the rest of the request.

soubhikchatterjee commented 2 years ago

@mscdex Not sure what you mean by "consume file", can you pls give a code example?

mscdex commented 2 years ago

file is a readable stream. You have to read all of its contents whether you care about them or not in order for the form parsing to complete.

soubhikchatterjee commented 2 years ago

@mscdex So there is no way to get the filename without using the req.busboy.on("file") event?

mscdex commented 2 years ago

Assuming the filename is coming from the file part, then no. If the filename is being transmitted separately from the file part, as a normal field ('field' event), then obviously yes.

Also FWIW, in general filenames given by a user/client in forms should rarely be used as-is as they could contain a malicious value.