Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ee-formdata-reader

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ee-formdata-reader - npm Package Compare versions

Comparing version 0.1.16 to 0.1.17

test/post5.json

134

lib/FormdataReader.js
var Class = require( "ee-class" )
, type = require( "ee-types" )
, EventEmitter = require( "ee-event-emitter" )
, log = require( "ee-log" )
var Class = require('ee-class')
, type = require('ee-types')
, EventEmitter = require('ee-event-emitter')
, log = require('ee-log')
, crypto = require('crypto')
, path = require( "path" )
, MimeDecoder = require( "ee-mime-decoder" )
, StreamURLDecoder = require( "ee-stream-url-decoder" )
, StreamCollector = require( "ee-stream-collector" );
, path = require('path')
, MimeDecoder = require('ee-mime-decoder')
, StreamURLDecoder = require('ee-stream-url-decoder')
, StreamCollector = require('ee-stream-collector');

@@ -18,9 +18,9 @@

module.exports = new Class( {
module.exports = new Class({
inherits: EventEmitter
, maxLength : 2 << 26 // a request may not send more than 128 MB ( 134'217'728 Bytes ) of data
, maxFormdataLength : 2 << 20 // a single form non file field may not be larger 2 MB ( 2'097'152 Bytes )
, maxFileLength : 2 << 26 // a single file may not bigger than 128 MB ( 134'217'728 Bytes )
, maxLength : 2 << 26 // a request may not send more than 128 MB (134'217'728 Bytes) of data
, maxFormdataLength : 2 << 20 // a single form non file field may not be larger 2 MB (2'097'152 Bytes)
, maxFileLength : 2 << 26 // a single file may not bigger than 128 MB (134'217'728 Bytes)

@@ -40,6 +40,6 @@ // counters

// the temp file name will be constructed as follows: [ cacheId, machineId, pid, pidTime, ++cacheSequenceId ].join( "-" )
// the temp file name will be constructed as follows: [ cacheId, machineId, pid, pidTime, ++cacheSequenceId ].join('-')
// e.g. «ee-form-data-A97562B3CDDD3EFFA97562B3CDDD3EFF-4059-1378557945760-0». this will produce unique filenames even when
// using a remote storage system
, cachId : "ee-formdata-reader"
, cachId : 'ee-formdata-reader'
, cacheSequenceId : 0

@@ -52,21 +52,21 @@ , pidTime : Date.now()

, init: function( options, request ){
, init: function(options, request) {
var contentType;
this.request = type.object( request ) ? request : ( type.object( options.request ) ? options.request : options );
this.request = type.object(request) ? request : (type.object(options.request) ? options.request : options);
contentType = this.request.getHeader( "content-type" );
contentType = this.request.getHeader('content-type');
// get machine id
/*machineId.get( function( id ){
/*machineId.get(function(id) {
this.machineId = id;
}.bind( this ) );*/
}.bind(this));*/
// store settings
if ( type.number( options.maxLength ) ) this.maxLength = options.maxLength;
if ( type.number( options.maxFormdataLength ) ) this.maxFormdataLength = options.maxFormdataLength;
if ( type.number( options.maxFileLength ) ) this.maxFileLength = options.maxFileLength;
if ( type.string( options.cachePath ) ) this.cachePath = options.cachePath;
if ( type.string( options.cachId ) ) this.cachId = options.cachId;
if (type.number(options.maxLength)) this.maxLength = options.maxLength;
if (type.number(options.maxFormdataLength)) this.maxFormdataLength = options.maxFormdataLength;
if (type.number(options.maxFileLength)) this.maxFileLength = options.maxFileLength;
if (type.string(options.cachePath)) this.cachePath = options.cachePath;
if (type.string(options.cachId)) this.cachId = options.cachId;

@@ -83,11 +83,25 @@

// select the correct MimeDecoder
if (contentType.indexOf("application/x-www-form-urlencoded") >= 0 ) {
if (contentType.indexOf('application/x-www-form-urlencoded') >= 0) {
this.decoder = new StreamURLDecoder();
this.decoder.on( "data", this.handleFormData.bind( this ) );
this.decoder.on('data', this.handleFormData.bind(this));
}
else if (contentType.indexOf("multipart/") >= 0) {
else if (contentType.indexOf('multipart/') >= 0) {
this.decoder = new MimeDecoder(contentType);
this.decoder.on( "data", this.handleMimePart.bind( this ) );
this.decoder.on('data', this.handleMimePart.bind(this));
this.isMultipart = true;
}
else if (contentType === 'application/json') {
this.decoder = new StreamCollector();
this.decoder.on('end', function(buf) {
var data = {};
try {
data = JSON.parse(buf.toString());
} catch(e) {
log.warn('failed to decode json message:', e);
}
this.handleFormData(data);
}.bind(this));
}
else {

@@ -100,9 +114,9 @@ this._isNotSupportedContentType = true;

// handle decoder end
this.decoder.on( "end", this.handleOnEnd.bind( this ) );
this.decoder.on('end', this.handleOnEnd.bind(this));
// redirect data to decoder
this.request.on( "data", this.handleData.bind( this ) );
this.request.on( "end", function(){
this.request.on('data', this.handleData.bind(this));
this.request.on('end', function() {
this.decoder.end();
}.bind( this ) );
}.bind(this));
}

@@ -116,3 +130,3 @@

, getForm: function(){
, getForm: function() {
return this.isSupportedType() ? (this.form || {}) : null;

@@ -123,10 +137,10 @@ }

// data from the request
, handleData: function( chunk ){
, handleData: function(chunk) {
this.dataReceived += chunk.length;
if ( this.dataReceived <= this.maxLength ){
this.decoder.write( chunk );
if (this.dataReceived <= this.maxLength) {
this.decoder.write(chunk);
}
else {
// cancel request, send 413 response
this.request.abort( 413 );
this.request.abort(413);
}

@@ -137,13 +151,13 @@ }

// mime parts, file uploads
, handleMimePart: function( part ){
, handleMimePart: function(part) {
var filename, fn, collector;
if ( part.isStream() ){
if ( this.cachePath ){
if (part.isStream()) {
if (this.cachePath) {
// cache file on fs
fn = part.hasHeader( "content-disposition" ) ? ( part.getHeader( "content-disposition" ).filename || "" ) : "";
filename = path.join( this.cachePath, [ this.cacheId, this.machineId, this.pid, this.pidTime, ++this.cacheSequenceId, fn ].join( "-" ) );
fn = part.hasHeader('content-disposition') ? (part.getHeader('content-disposition').filename || '') : '';
filename = path.join(this.cachePath, [ this.cacheId, this.machineId, this.pid, this.pidTime, ++this.cacheSequenceId, fn ].join('-'));
part.data = filename;
part.pipe( fs.createwriteStream( filename ) );
part.pipe(fs.createwriteStream(filename));
}

@@ -153,4 +167,4 @@ else {

collector = new StreamCollector();
collector.on( "end", function( data ){part.data = data; }.bind( this ) );
part.pipe( collector );
collector.on('end', function(data) {part.data = data; }.bind(this));
part.pipe(collector);
}

@@ -162,8 +176,8 @@ }

// form data
, handleFormData: function( data ){
if ( !this.form ) this.form = data;
, handleFormData: function(data) {
if (!this.form) this.form = data;
else {
Object.keys( data ).forEach( function( key ){
this._storeValue( key, data[ key ] );
}.bind( this ) );
Object.keys(data).forEach(function(key) {
this._storeValue(key, data[ key ]);
}.bind(this));
}

@@ -173,11 +187,11 @@ }

, _storeValue: function(key, value){
, _storeValue: function(key, value) {
if (!this.form) this.form = {};
switch (type(this.form[key])){
case "undefined":
switch (type(this.form[key])) {
case 'undefined':
this.form[key] = value;
break;
case "array":
case 'array':
this.form[key].push(value);

@@ -198,3 +212,3 @@ break;

if (part.hasChildren()){
if (part.hasChildren()) {
this._storeMultipartParts(part.parts, (header ? header.name : 'undefined'));

@@ -204,3 +218,3 @@ }

if (header) {
if (header.value === "file") this._storeValue(parentName || 'undefined', {data: part.data, filename: header.name || header.filename});
if (header.value === 'file') this._storeValue(parentName || 'undefined', {data: part.data, filename: header.name || header.filename});
else this._storeValue(header.name || header.filename || 'undefined', part.data.toString().trim());

@@ -212,3 +226,3 @@ }

}
}.bind( this ) );
}.bind(this));
}

@@ -218,6 +232,6 @@

// streaming finished
, handleOnEnd: function(){
, handleOnEnd: function() {
if (this.isMultipart) this._storeMultipartParts(this.decoder.getMessage().parts);
this.emit( "end" );
this.emit('end');
}
} );
});
{
"name" : "ee-formdata-reader"
, "description" : "reads formdata from requests"
, "version" : "0.1.16"
, "version" : "0.1.17"
, "homepage" : "https://github.com/eventEmitter/ee-formdata-reader"

@@ -6,0 +6,0 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)"

@@ -108,2 +108,14 @@

});
it('Should be able to decode an json message', function(done){
var reader = new FormdataReader({
request: createRequest('post5.json', 'application/json')
});
reader.on('end', function() {
assert.equal('{"a":[1,2,3,4,5,"f"],"rr":"hui"}', JSON.stringify(reader.getForm()), 'message hash is different!')
done();
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc