npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fastify/busboy

v2.1.1

Published

A streaming parser for HTML form data for node.js

Downloads

26,764,589

Readme

busboy

Build Status Coverage Status js-standard-style Security Responsible Disclosure

NPM version NPM downloads

Description

A Node.js module for parsing incoming HTML form data.

This is an officially supported fork by fastify organization of the amazing library originally created by Brian White, aimed at addressing long-standing issues with it.

Benchmark (Mean time for 500 Kb payload, 2000 cycles, 1000 cycle warmup):

| Library | Version | Mean time in nanoseconds (less is better) | |-----------------------|---------|-------------------------------------------| | busboy | 0.3.1 | 340114 | | @fastify/busboy | 1.0.0 | 270984 |

Changelog since busboy 0.31.

Requirements

Install

npm i @fastify/busboy

Examples

  • Parsing (multipart) with default options:
const http = require('node:http');
const { inspect } = require('node:util');
const Busboy = require('busboy');

http.createServer((req, res) => {
  if (req.method === 'POST') {
    const busboy = new Busboy({ headers: req.headers });
    busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
      console.log(`File [${fieldname}]: filename: ${filename}, encoding: ${encoding}, mimetype: ${mimetype}`);
      file.on('data', data => {
        console.log(`File [${fieldname}] got ${data.length} bytes`);
      });
      file.on('end', () => {
        console.log(`File [${fieldname}] Finished`);
      });
    });
    busboy.on('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
      console.log(`Field [${fieldname}]: value: ${inspect(val)}`);
    });
    busboy.on('finish', () => {
      console.log('Done parsing form!');
      res.writeHead(303, { Connection: 'close', Location: '/' });
      res.end();
    });
    req.pipe(busboy);
  } else if (req.method === 'GET') {
    res.writeHead(200, { Connection: 'close' });
    res.end(`<html><head></head><body>
               <form method="POST" enctype="multipart/form-data">
                <input type="text" name="textfield"><br>
                <input type="file" name="filefield"><br>
                <input type="submit">
              </form>
            </body></html>`);
  }
}).listen(8000, () => {
  console.log('Listening for requests');
});

// Example output, using http://nodejs.org/images/ryan-speaker.jpg as the file:
//
// Listening for requests
// File [filefield]: filename: ryan-speaker.jpg, encoding: binary
// File [filefield] got 11971 bytes
// Field [textfield]: value: 'testing! :-)'
// File [filefield] Finished
// Done parsing form!
  • Save all incoming files to disk:
const http = require('node:http');
const path = require('node:path');
const os = require('node:os');
const fs = require('node:fs');

const Busboy = require('busboy');

http.createServer(function(req, res) {
  if (req.method === 'POST') {
    const busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
      var saveTo = path.join(os.tmpdir(), path.basename(fieldname));
      file.pipe(fs.createWriteStream(saveTo));
    });
    busboy.on('finish', function() {
      res.writeHead(200, { 'Connection': 'close' });
      res.end("That's all folks!");
    });
    return req.pipe(busboy);
  }
  res.writeHead(404);
  res.end();
}).listen(8000, function() {
  console.log('Listening for requests');
});
  • Parsing (urlencoded) with default options:
const http = require('node:http');
const { inspect } = require('node:util');

const Busboy = require('busboy');

http.createServer(function(req, res) {
  if (req.method === 'POST') {
    const busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
      console.log('File [' + fieldname + ']: filename: ' + filename);
      file.on('data', function(data) {
        console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
      });
      file.on('end', function() {
        console.log('File [' + fieldname + '] Finished');
      });
    });
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
      console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    });
    busboy.on('finish', function() {
      console.log('Done parsing form!');
      res.writeHead(303, { Connection: 'close', Location: '/' });
      res.end();
    });
    req.pipe(busboy);
  } else if (req.method === 'GET') {
    res.writeHead(200, { Connection: 'close' });
    res.end('<html><head></head><body>\
               <form method="POST">\
                <input type="text" name="textfield"><br />\
                <select name="selectfield">\
                  <option value="1">1</option>\
                  <option value="10">10</option>\
                  <option value="100">100</option>\
                  <option value="9001">9001</option>\
                </select><br />\
                <input type="checkbox" name="checkfield">Node.js rules!<br />\
                <input type="submit">\
              </form>\
            </body></html>');
  }
}).listen(8000, function() {
  console.log('Listening for requests');
});

// Example output:
//
// Listening for requests
// Field [textfield]: value: 'testing! :-)'
// Field [selectfield]: value: '9001'
// Field [checkfield]: value: 'on'
// Done parsing form!

API

Busboy is a Writable stream

Busboy (special) events

  • file(< string >fieldname, < ReadableStream >stream, < string >filename, < string >transferEncoding, < string >mimeType) - Emitted for each new file form field found. transferEncoding contains the 'Content-Transfer-Encoding' value for the file stream. mimeType contains the 'Content-Type' value for the file stream.

    • Note: if you listen for this event, you should always handle the stream no matter if you care about the file contents or not (e.g. you can simply just do stream.resume(); if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about any incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards files and parts limits).
    • If a configured file size limit was reached, stream will both have a boolean property truncated (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
    • The property bytesRead informs about the number of bytes that have been read so far.
  • field(< string >fieldname, < string >value, < boolean >fieldnameTruncated, < boolean >valueTruncated, < string >transferEncoding, < string >mimeType) - Emitted for each new non-file field found.

  • partsLimit() - Emitted when specified parts limit has been reached. No more 'file' or 'field' events will be emitted.

  • filesLimit() - Emitted when specified files limit has been reached. No more 'file' events will be emitted.

  • fieldsLimit() - Emitted when specified fields limit has been reached. No more 'field' events will be emitted.

Busboy methods

  • (constructor)(< object >config) - Creates and returns a new Busboy instance.

    • The constructor takes the following valid config settings:

      • headers - object - These are the HTTP headers of the incoming request, which are used by individual parsers.

      • autoDestroy - boolean - Whether this stream should automatically call .destroy() on itself after ending. (Default: false).

      • highWaterMark - integer - highWaterMark to use for this Busboy instance (Default: WritableStream default).

      • fileHwm - integer - highWaterMark to use for file streams (Default: ReadableStream default).

      • defCharset - string - Default character set to use when one isn't defined (Default: 'utf8').

      • preservePath - boolean - If paths in the multipart 'filename' field shall be preserved. (Default: false).

      • isPartAFile - function - Use this function to override the default file detection functionality. It has following parameters:

        • fieldName - string The name of the field.

        • contentType - string The content-type of the part, e.g. text/plain, image/jpeg, application/octet-stream

        • fileName - string The name of a file supplied by the part.

        (Default: (fieldName, contentType, fileName) => (contentType === 'application/octet-stream' || fileName !== undefined))

      • limits - object - Various limits on incoming data. Valid properties are:

        • fieldNameSize - integer - Max field name size (in bytes) (Default: 100 bytes).

        • fieldSize - integer - Max field value size (in bytes) (Default: 1 MiB, which is 1024 x 1024 bytes).

        • fields - integer - Max number of non-file fields (Default: Infinity).

        • fileSize - integer - For multipart forms, the max file size (in bytes) (Default: Infinity).

        • files - integer - For multipart forms, the max number of file fields (Default: Infinity).

        • parts - integer - For multipart forms, the max number of parts (fields + files) (Default: Infinity).

        • headerPairs - integer - For multipart forms, the max number of header key=>value pairs to parse Default: 2000

        • headerSize - integer - For multipart forms, the max size of a multipart header Default: 81920.

    • The constructor can throw errors:

      • Busboy expected an options-Object. - Busboy expected an Object as first parameters.

      • Busboy expected an options-Object with headers-attribute. - The first parameter is lacking of a headers-attribute.

      • Limit $limit is not a valid number - Busboy expected the desired limit to be of type number. Busboy throws this Error to prevent a potential security issue by falling silently back to the Busboy-defaults. Potential source for this Error can be the direct use of environment variables without transforming them to the type number.

      • Unsupported Content-Type. - The Content-Type isn't one Busboy can parse.

      • Missing Content-Type-header. - The provided headers don't include Content-Type at all.