question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

finish event never fires

See original GitHub issue

I am trying to upload some images to my cloud storage. I am using form data to send the data down to the cloud function. The data arrives correctly, but the Busboy process never finishes processing it and the function timeouts after 60s.

Here is the implementation:

const firebase = require("firebase");
const Busboy = require("busboy");
require("firebase/storage");
const firebase_db = firebase_admin.database();
const firebase_storage = firebase.storage();


module.exports = (req, res) => {
  const busboy = new Busboy({headers: req.headers});
  const fields = {};
  const files = {};

  busboy.on('field', (fieldname, val) => {
    fields[fieldname] = val;
  });

  busboy.on('file', (fieldname, file, filename) => {
    files[fieldname] = file;
  });

  busboy.on('finish', async () => { // ----> Function never fires
    try {
      let project_id = await firebase_db.ref("/project_config/project_id").once("value")
      project_id = project_id.val()
   
      if (!project_id) return res.send({ message: "Project ID not found" })
      const new_image_storage_ref = firebase_storage.ref().child(`${project_id}/${fields.route}`)
      const snapshot = await new_image_storage_ref.put(files.image)
      const download_url = await snapshot.ref.getDownloadURL()
 
      res.send(download_url)
    } catch (error) {
      console.log("----- Error during saving content data ", error)
      res.send({ message: "Error during saving your image, please re-load page and try again or contact support." })
    }
  });
}

Do you have any idea what might be causing it?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
pizzarobcommented, May 16, 2021

I am having a similar issue. I am trying to write to a path and it does in fact write some of the data, but not all of it and the finish event is never emitted. I’ve tried to add other events to the streams to figure out was is going on but can’t seem to figure it out. This happens when running in a docker container on ECS. It seems to work fine locally.

export default (req) =>
  new Promise((resolve, reject) => {
    const busboy = new Busboy({ headers: req.headers });
    const fieldNameCache = {};
    const output = {
      fields: [],
      files: [],
    };

    busboy.on(
      "file",
      async (fieldName, fileStream, fileName, encoding, mimeType) => {
        if (INVALID_MIME_TYPE.includes(mimeType)) {
          return reject({ message: "mime type not allowed", data: mimeType });
        }
        if (!VALID_FIELD_NAMES.includes(fieldName)) {
          return reject(new Error(`Erroneous value ${fieldName}`));
        }

        const folder = `${uuid()}`;

        await fs.promises.mkdir(folder, { recursive: true });

        const filePath = `${folder}/${fileName}`;

        output.files.push({
          fieldName,
          fileName,
          path: filePath,
          folder,
          mimeType,
        });

        function cb(e) {
          console.log("ENDING", e);
        }

        const ws = fs
          .createWriteStream(filePath)
          .on("error", cb)
          .on("finish", cb)
          .on("end", cb);

        fileStream
          .on("error", cb)
          .on("finish", cb)
          .on("end", cb);

        pipeline(fileStream, ws, (err) => {
          if (err) {
            console.log("Pipeline failed", err);
          } else {
            console.log("Pipleline complete");
          }
        });
      }
    );

    busboy.on(
      "field",
      (
        fieldName,
        value,
        fieldnameTruncated,
        valTruncated,
        encoding,
        mimeType
      ) => {
        if (!VALID_FIELD_NAMES.includes(fieldName)) {
          return reject(new Error(`Erroneous value ${fieldName}`));
        }
        if (fieldNameCache[fieldName]) {
          return reject(new Error(`Duplicate field name ${fieldName}`));
        }
        fieldNameCache[fieldName] = true;
        output.fields.push({ fieldName, value, mimeType });
      }
    );

    busboy.on("finish", async () => {
      // Never happens
      resolve(output);
    });

    // Added these to try to find errors, but there are none
    req.pipe(busboy).on("error", (e) => {
      console.log("error", e);
    });
    req.on("aborted", (e) => {
      console.log(`Request aborted`, e);
    });
    busboy.on("error", (e) => {
      console.log("busboy error");
      console.log(e);
    });
  });
1reaction
Xwilcommented, Jan 12, 2022

Node version matters. Works fine with node v14, but in some case with v16, the problem occurs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js http response end event never fired - Stack Overflow
Can anyone please tell me why it never fires the end event? How can I make it workable? Code: const http = require('http');...
Read more >
File completed event never fires - Deluge Forum
EDIT: Just to clarify, this event is supposed to fire whenever any file in any torrent finishes downloading, right?
Read more >
Node.js Readable Stream end Event - GeeksforGeeks
The 'end' Event in a Readable Stream is emitted when there is no available data to be consumed from the readable stream. And...
Read more >
Stream | Node.js v19.3.0 Documentation
The 'finish' event is emitted after the stream.end() method has been called, and all data has been flushed to the underlying system.
Read more >
map.bounds_changed event fires repeatedly when the map is ...
I'd expect it to fire only when the move has finished. ... If you dont want events during dragging either, can easily combine...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found