all files / build/ index.js

63.33% Statements 38/60
43.33% Branches 13/30
92.31% Functions 12/13
96.88% Lines 31/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81          14×                                                           18×                                
'use strict';
 
Object.defineProperty(exports, "__esModule", {
	value: true
});
 
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { Eif (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
 
function _defineProperty(obj, key, value) { Iif (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
 
/**
 * @description Body-Parsing Middleware
 * This is a curried function with options, so you have to
 * call it once before passing it to spirit
 * @param {Object} options The parsing components to be used.
 * @return {function} Middleware to be used.
 * @example const body = spiritBody({json: true, urlEncoded: true})
 */
exports.default = function () {
	var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
		json: false,
		urlEncoded: false
	};
	return function (handler) {
		return function (request) {
			return new Promise(function (resolve, reject) {
				var req = request.req();
 
				// Push to array for (most likely) improved concatenation performance
				var chunks = [];
				req.on('data', function (data) {
					return chunks.push(data);
				});
 
				// connection ended
				req.on('end', function () {
					// concat the chunks and convert to String — that's ok, because
					// we only receive plaintext. Buffer defaults to UTF-8.
					// TODO: check for other encodings
					var raw = Buffer.concat(chunks).toString();
 
					// convert the headers to lowercase for easier comparison
					var lowercaseHeaders = Object.keys(request.headers).map(function (key) {
						return _defineProperty({}, key.toLowerCase(), request.headers[key]);
					}).reduce(function (previous, current) {
						return Object.assign(previous, current);
					}, {});
 
					var _split = (lowercaseHeaders['content-type'] || '').split(';'),
					    _split2 = _slicedToArray(_split, 1),
					    type = _split2[0];
 
					if (options.urlEncoded && type === 'application/x-www-form-urlencoded') {
						var result = {};
						var keyValueRegex = /([^&=]+)=?([^&]*)/g;
						var sanitize = function sanitize(input) {
							return decodeURIComponent(input.replace(/[+]/g, ' '));
						};
 
						var match = void 0;
						while (match = keyValueRegex.exec(raw)) {
							var _match$map = match.map(sanitize),
							    _match$map2 = _slicedToArray(_match$map, 3),
							    key = _match$map2[1],
							    value = _match$map2[2];
 
							result[key] = value;
						}
						request.body = result;
					} else Eif (options.json && ['application/json', 'text/json'].includes(type)) {
						request.body = JSON.parse(raw);
					} else {
						request.body = raw;
					}
 
					resolve(handler(request));
				});
			});
		};
	};
};