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

openrosa-form-submission-middleware

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openrosa-form-submission-middleware - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

142

index.js

@@ -13,8 +13,9 @@ /*!

var multiparty = require('multiparty');
var onFinished = require('on-finished');
var typeis = require('type-is');
var fs = require('fs');
var openrosaRequest = require('openrosa-request-middleware');
var openrosaRequestMiddleware = openrosaRequest();
var multiparty = require('multiparty')
var onFinished = require('on-finished')
var typeis = require('type-is')
var fs = require('fs')
var debug = require('debug')('openrosa:submission')
var openrosaRequest = require('openrosa-request-middleware')
var openrosaRequestMiddleware = openrosaRequest()

@@ -34,3 +35,3 @@ /**

*
* app.use(connect.multipart({ uploadDir: path }));
* app.use(connect.multipart({ uploadDir: path }))
*

@@ -42,58 +43,66 @@ * @param {Object} options

module.exports = function(options){
options = options || {};
options.maxContentLength = options.maxContentLength || 10485760;
module.exports = function (options) {
options = options || {}
options.maxContentLength = options.maxContentLength || 10485760
function multipart(req, res, next) {
if (req._body) return next();
req.files = [];
req.body = undefined;
function multipart (req, res, next) {
if (req._body) return next()
req.files = []
req.body = undefined
// Response to HEAD requests with status 204
if ('HEAD' === req.method) return res.sendStatus(204);
// Ignore all other request methods other than post
else if ('POST' !== req.method) return next();
if (req.method === 'HEAD') {
// Response to HEAD requests with status 204
return res.sendStatus(204)
} else if (req.method !== 'POST') {
// Ignore all other request methods other than post
return next()
}
// check Content-Type
if (!typeis(req, 'multipart/form-data')) return next();
if (!typeis(req, 'multipart/form-data')) return next()
debug('processing form submission')
// flag as parsed
req._body = true;
req._body = true
// parse
var form = new multiparty.Form(options);
var done = false;
var processingXml = false;
var wasError = false;
var form = new multiparty.Form(options)
var done = false
var processingXml = false
var wasError = false
form.on('file', function(name, val){
val.name = val.originalFilename;
val.type = val.headers['content-type'] || null;
form.on('file', function (name, val) {
val.name = val.originalFilename
val.type = val.headers['content-type'] || null
if (name === 'xml_submission_file') {
processingXml = true;
fs.readFile(val.path, function(err, data) {
if (err) onError(err);
req.body = data.toString();
fs.unlink(val.path, function(err) {
if (err) console.error('Error deleting file %s', val.path);
});
processingXml = false;
if (done && !wasError) next();
});
debug('received xml submission file (%s bytes)', val.size)
processingXml = true
fs.readFile(val.path, function (err, data) {
if (err) onError(err)
req.body = data.toString()
fs.unlink(val.path, function (err) {
if (err) console.error('Error deleting file %s', val.path)
})
debug('processed xml submission')
processingXml = false
if (done && !wasError) next()
})
} else {
req.files.push(val);
debug('received attachment %s (%s bytes)', val.name, val.size)
req.files.push(val)
}
});
})
form.on('error', function(err) {
if (done) return;
onError(err);
});
form.on('error', function (err) {
if (done) return
onError(err)
})
form.on('close', function() {
if (done) return;
form.on('close', function () {
if (done) return
done = true;
done = true

@@ -103,34 +112,35 @@ // only continue if we have already processed the xml submission file

if (req.body) {
next();
next()
} else if (!processingXml) {
onError(new Error('No xml submission file included in request'));
onError(new Error('No xml submission file included in request'))
}
});
})
function onError(err) {
done = wasError = true;
function onError (err) {
debug('form processing error:', err.message)
done = wasError = true
err.status = 400;
err.status = 400
if (!req.readable) return next(err);
if (!req.readable) return next(err)
req.resume();
onFinished(req, function(){
next(err);
});
req.resume()
onFinished(req, function () {
next(err)
})
}
form.parse(req);
form.parse(req)
}
return function(req, res, next) {
return function (req, res, next) {
// Set correct OpenRosa headers
// see https://bitbucket.org/javarosa/javarosa/wiki/OpenRosaRequest
// and https://bitbucket.org/javarosa/javarosa/wiki/FormSubmissionAPI
openrosaRequestMiddleware(req, res, function(err) {
if (err) next(err);
res.setHeader('X-OpenRosa-Accept-Content-Length', options.maxContentLength);
multipart(req, res, next);
});
};
};
openrosaRequestMiddleware(req, res, function (err) {
if (err) next(err)
res.setHeader('X-OpenRosa-Accept-Content-Length', options.maxContentLength)
multipart(req, res, next)
})
}
}
{
"name": "openrosa-form-submission-middleware",
"version": "1.0.3",
"version": "1.0.4",
"description": "Express middleware for receiving OpenRosa xform submissions",
"main": "index.js",
"scripts": {
"test": "mocha --reporter spec --timeout 600 test/*.js"
"test": "standard & mocha --reporter spec --timeout 600 test/*.js"
},

@@ -39,4 +39,5 @@ "repository": {

"should": "~4.0.4",
"standard": "^3.3.0",
"supertest": "~0.14.0"
}
}
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