corps
Version:
Streaming http body parser
44 lines • 1.44 kB
JavaScript
// Generated by LiveScript 1.3.1
var σ, qs, bodyParams, handleError, jsonParse, queryParse, json, query, raw, mimeParsers, auto, out$ = typeof exports != 'undefined' && exports || this;
σ = require('highland');
qs = require('qs');
out$.bodyParams = bodyParams = curry$(function(parser, req){
return σ(req).flatMap(parser);
});
out$.handleError = handleError = curry$(function(f, x){
return σ([null]).map(function(){
return f(x);
});
});
out$.jsonParse = jsonParse = handleError(JSON.parse);
out$.queryParse = queryParse = handleError(qs.parse);
out$.json = json = bodyParams(jsonParse);
out$.query = query = bodyParams(queryParse);
out$.raw = raw = bodyParams(function(it){
return σ([it]);
});
out$.mimeParsers = mimeParsers = {
'application/json': json,
'application/x-www-form-urlencoded': query
};
out$.auto = auto = function(req){
var ref$;
if (typeof req.setEncoding == 'function') {
req.setEncoding('utf8');
}
return ((ref$ = mimeParsers[req.headers['content-type']]) != null ? ref$ : raw)(
req);
};
function curry$(f, bound){
var context,
_curry = function(args) {
return f.length > 1 ? function(){
var params = args ? args.concat() : [];
context = bound ? context || this : this;
return params.push.apply(params, arguments) <
f.length && arguments.length ?
_curry.call(context, params) : f.apply(context, params);
} : f;
};
return _curry();
}