GitHub - fastify/busboy: A streaming parser for HTML form data for node.js (original) (raw)

Build Status NPM version neostandard javascript style Security Responsible Disclosure

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

Requirements

Install

Examples

const http = require('node:http'); const { inspect } = require('node:util'); const Busboy = require('@fastify/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: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo separator="true">,</mo><mi>e</mi><mi>n</mi><mi>c</mi><mi>o</mi><mi>d</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">{filename}, encoding: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">co</span><span class="mord mathnormal">d</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>{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!

const http = require('node:http'); const path = require('node:path'); const os = require('node:os'); const fs = require('node:fs');

const Busboy = require('@fastify/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'); });

const http = require('node:http'); const { inspect } = require('node:util');

const Busboy = require('@fastify/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('










Node.js rules!



'); } }).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

busboy.on('file', (fieldname, stream) => { stream.on('limit', () => { console.log('File size exceeded') }) })

Busboy methods