Search code examples
node.jsstreambusboy

Count bytes from Busboy stream


I'm getting the file stream from Busboy and then i need to count the bytes, grab the first line and then send it to azure storage. It works for files up to about 25MB but after that all of the bytes aren't counted. I'm not sure how to make it wait. I'm using a passthrough stream just to hold the original data while I get the first line.

busboy
.on('file', function(fieldname, file, filename, encoding, mimetype) {
    file
    .on('data', function(data) {
        bytes = bytes + data.length;
    })
    .on('end', function() {
        console.log("end");
    })
    .pipe(passthrough)
    .pipe(firstLine)
    .pipe(es.wait(function (err, body) {
        blobService.createBlockBlobFromStream(containter, name, passthrough, bytes, function (error, result) {
            if(error) {
                return sendResponse(error.toString(), res, null);
            }
            sendResponse(null, res, "done");
        });
    }));
})
.on('finish', function() {
    console.log("busboy done");
});

Solution

  • If you want to pipe data to the blob, createWriteStreamToBlockBlob is the API you might need.

    busboy
    .on('file', function(fieldname, file, filename, encoding, mimetype) {
      file
      .on('data', function(data) {
        bytes = bytes + data.length;
      })
      .on('end', function() {
        console.log("end");
      })
      .pipe(passthrough)
      .pipe(firstLine)
      .pipe(blobService.createWriteStreamToBlockBlob(containter, name, function (error, result) {
         if(error) {
            return sendResponse(error.toString(), res, null);
         }
         sendResponse(null, res, "done");
      }))
    })
    .on('finish', function() {
      console.log("busboy done");
    });