
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
stream-body-parser
Advanced tools
#stream-body-parser.js Parse multipart/form-data as streams - processing doing upload
##Introduction
This is a (partial) drop-in for the bodyParser in connect.js and express.js. But instead of just being able to upload files to a directory, it directly access the upload stream, as it being uploaded. Thereby you can redirect the uploaded files to another destination than just the disk - being it a database or simply processing the data as it is being uploaded.
I've created this in order to make video upload service, that automatically transcodes the videos to a specific format on upload. Thereby having the video available in the desired format, already when the user finishes uploading.
So together with stream-transcoder.js, you are able to do stuff like this.
stream-body-parser will default back to connect.js/express.js's bodyParser, when non multipart/form-data data is being posted.
var express = require('express'),
StreamBodyParser = require('stream-body-parser'),
Transcoder = require('stream-transcoder');
var app = express();
var bodyParser = new StreamBodyParser(app);
bodyParser.process('video/*', function(stream, req, next) {
var myGridFSWriteStream = (Some MongoDB GridFS stream)
new Transcoder(stream)
.maxSize(320, 240)
.videoCodec('h264')
.videoBitrate(800 * 1000)
.fps(25)
.audioCodec('libfaac')
.sampleRate(44100)
.channels(2)
.audioBitrate(128 * 1000)
.format('mp4')
.on('finish', function() {
next();
})
.stream().pipe(myGridFSWriteStream);
});
app.post('/', function(req, res) {
res.send(200); // File uploaded
});
app.listen(3000);
In the above example the video is transcoded as it is being uploaded, and then piped directly into the database. So when the route is being called, the video is transcoded and stored.
This class provides incoming uploaded files as streams, as soon as the request has been made - but before the upload has started.
app Object - express.js or connect.js app.options Object - options.Example
var bodyParser = require('stream-body-parser');
var bodyParser = new StreamBodyParser({ uploadDir: './public' }, anExpressApp)
Prepares a new StreamBodyParser and - optionally if the app parameter is supplied, configures it for use with connect.js or express.js.
mime String - Mime type to process with fn. As an example: image/jpeg or video/*. Default value */*fn Function - Processor function for when file of specified mime type is about to be uploaded.
fn should be a function accepting three parameters.
stream Object - The stream of the file being uploaded.req Object - The req object of connect.js/express.jsnext Function - Function to be called, when processor is done processing all the data.If uploadDir is specified by options doing object creation, a processor will be registered to mime type */* that simply pipes the input to a files in the specified directory.
var express = require('express'),
StreamBodyParser = require('stream-body-parser'),
Transcoder = require('stream-transcoder');
var app = express();
var bodyParser = new StreamBodyParser(app);
bodyParser.process('image/jpeg', function(stream, req, next) {
stream.pipe(fs.createFileStream(stream.filename));
stream.on('end', next);
});
bodyParser.process(function(stream, req, next) {
console.log('Stream of type ' + stream.mime + ' is being uploaded.');
stream.on('end', function() {
console.log('Done');
next();
});
});
The above example simply images of type jpeg to a filename named the same as the incoming file.
The second is processor simply logs that a file is being uploaded, and when it completes.
FAQs
Process multipart/form-data on the fly as streams.
We found that stream-body-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.