diff options
Diffstat (limited to 'dgbuilder/dgeflows/node_modules/body-parser/lib')
5 files changed, 604 insertions, 0 deletions
diff --git a/dgbuilder/dgeflows/node_modules/body-parser/lib/read.js b/dgbuilder/dgeflows/node_modules/body-parser/lib/read.js new file mode 100644 index 00000000..0880a3dd --- /dev/null +++ b/dgbuilder/dgeflows/node_modules/body-parser/lib/read.js @@ -0,0 +1,153 @@ +/*! + * body-parser + * Copyright(c) 2014 Douglas Christopher Wilson + * MIT Licensed + */ + +/** + * Module dependencies. + */ + +var getBody = require('raw-body') +var iconv = require('iconv-lite') +var onFinished = require('on-finished') +var typer = require('media-typer') +var zlib = require('zlib') + +/** + * Module exports. + */ + +module.exports = read + +/** + * Read a request into a buffer and parse. + * + * @param {object} req + * @param {object} res + * @param {function} next + * @param {function} parse + * @param {object} options + * @api private + */ + +function read(req, res, next, parse, options) { + var length + var stream + + // flag as parsed + req._body = true + + try { + stream = contentstream(req, options.inflate) + length = stream.length + delete stream.length + } catch (err) { + return next(err) + } + + options = options || {} + options.length = length + + var encoding = options.encoding !== null + ? options.encoding || 'utf-8' + : null + var verify = options.verify + + options.encoding = verify + ? null + : encoding + + // read body + getBody(stream, options, function (err, body) { + if (err) { + if (!err.status) { + err.status = 400 + } + + // echo back charset + if (err.type === 'encoding.unsupported') { + err = new Error('unsupported charset "' + encoding.toUpperCase() + '"') + err.charset = encoding.toLowerCase() + err.status = 415 + } + + // read off entire request + stream.resume() + onFinished(req, function onfinished() { + next(err) + }) + return + } + + // verify + if (verify) { + try { + verify(req, res, body, encoding) + } catch (err) { + if (!err.status) err.status = 403 + return next(err) + } + } + + // parse + try { + body = typeof body !== 'string' && encoding !== null + ? iconv.decode(body, encoding) + : body + req.body = parse(body) + } catch (err) { + if (!err.status) { + err.body = body + err.status = 400 + } + return next(err) + } + + next() + }) +} + +/** + * Get the content stream of the request. + * + * @param {object} req + * @param {boolean} [inflate=true] + * @return {object} + * @api private + */ + +function contentstream(req, inflate) { + var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase() + var err + var length = req.headers['content-length'] + var stream + + if (inflate === false && encoding !== 'identity') { + err = new Error('content encoding unsupported') + err.status = 415 + throw err + } + + switch (encoding) { + case 'deflate': + stream = zlib.createInflate() + req.pipe(stream) + break + case 'gzip': + stream = zlib.createGunzip() + req.pipe(stream) + break + case 'identity': + stream = req + stream.length = length + break + default: + err = new Error('unsupported content encoding "' + encoding + '"') + err.encoding = encoding + err.status = 415 + throw err + } + + return stream +} diff --git a/dgbuilder/dgeflows/node_modules/body-parser/lib/types/json.js b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/json.js new file mode 100644 index 00000000..b66e309b --- /dev/null +++ b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/json.js @@ -0,0 +1,117 @@ +/*! + * body-parser + * Copyright(c) 2014 Jonathan Ong + * Copyright(c) 2014 Douglas Christopher Wilson + * MIT Licensed + */ + +/** + * Module dependencies. + */ + +var bytes = require('bytes') +var read = require('../read') +var typer = require('media-typer') +var typeis = require('type-is') + +/** + * Module exports. + */ + +module.exports = json + +/** + * RegExp to match the first non-space in a string. + * + * Allowed whitespace is defined in RFC 7159: + * + * ws = *( + * %x20 / ; Space + * %x09 / ; Horizontal tab + * %x0A / ; Line feed or New line + * %x0D ) ; Carriage return + */ + +var firstcharRegExp = /^[\x20\x09\x0a\x0d]*(.)/ + +/** + * Create a middleware to parse JSON bodies. + * + * @param {object} [options] + * @return {function} + * @api public + */ + +function json(options) { + options = options || {} + + var limit = typeof options.limit !== 'number' + ? bytes(options.limit || '100kb') + : options.limit + var inflate = options.inflate !== false + var reviver = options.reviver + var strict = options.strict !== false + var type = options.type || 'json' + var verify = options.verify || false + + if (verify !== false && typeof verify !== 'function') { + throw new TypeError('option verify must be function') + } + + function parse(body) { + if (body.length === 0) { + // special-case empty json body, as it's a common client-side mistake + // TODO: maybe make this configurable or part of "strict" option + return {} + } + + if (strict) { + var first = firstchar(body) + + if (first !== '{' && first !== '[') { + throw new Error('invalid json') + } + } + + return JSON.parse(body, reviver) + } + + return function jsonParser(req, res, next) { + if (req._body) return next() + req.body = req.body || {} + + if (!typeis(req, type)) return next() + + // RFC 7159 sec 8.1 + var charset = (typer.parse(req).parameters.charset || 'utf-8').toLowerCase() + if (charset.substr(0, 4) !== 'utf-') { + var err = new Error('unsupported charset "' + charset.toUpperCase() + '"') + err.charset = charset + err.status = 415 + next(err) + return + } + + // read + read(req, res, next, parse, { + encoding: charset, + inflate: inflate, + limit: limit, + verify: verify + }) + } +} + +/** + * Get the first non-whitespace character in a string. + * + * @param {string} str + * @return {function} + * @api public + */ + + +function firstchar(str) { + var match = firstcharRegExp.exec(str) + return match ? match[1] : '' +} diff --git a/dgbuilder/dgeflows/node_modules/body-parser/lib/types/raw.js b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/raw.js new file mode 100644 index 00000000..9d7e49a6 --- /dev/null +++ b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/raw.js @@ -0,0 +1,61 @@ +/*! + * body-parser + * Copyright(c) 2014 Douglas Christopher Wilson + * MIT Licensed + */ + +/** + * Module dependencies. + */ + +var bytes = require('bytes') +var read = require('../read') +var typeis = require('type-is') + +/** + * Module exports. + */ + +module.exports = raw + +/** + * Create a middleware to parse raw bodies. + * + * @param {object} [options] + * @return {function} + * @api public + */ + +function raw(options) { + options = options || {}; + + var inflate = options.inflate !== false + var limit = typeof options.limit !== 'number' + ? bytes(options.limit || '100kb') + : options.limit + var type = options.type || 'application/octet-stream' + var verify = options.verify || false + + if (verify !== false && typeof verify !== 'function') { + throw new TypeError('option verify must be function') + } + + function parse(buf) { + return buf + } + + return function rawParser(req, res, next) { + if (req._body) return next() + req.body = req.body || {} + + if (!typeis(req, type)) return next() + + // read + read(req, res, next, parse, { + encoding: null, + inflate: inflate, + limit: limit, + verify: verify + }) + } +} diff --git a/dgbuilder/dgeflows/node_modules/body-parser/lib/types/text.js b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/text.js new file mode 100644 index 00000000..2330b5ba --- /dev/null +++ b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/text.js @@ -0,0 +1,66 @@ +/*! + * body-parser + * Copyright(c) 2014 Douglas Christopher Wilson + * MIT Licensed + */ + +/** + * Module dependencies. + */ + +var bytes = require('bytes') +var read = require('../read') +var typeis = require('type-is') +var typer = require('media-typer') + +/** + * Module exports. + */ + +module.exports = text + +/** + * Create a middleware to parse text bodies. + * + * @param {object} [options] + * @return {function} + * @api public + */ + +function text(options) { + options = options || {}; + + var defaultCharset = options.defaultCharset || 'utf-8' + var inflate = options.inflate !== false + var limit = typeof options.limit !== 'number' + ? bytes(options.limit || '100kb') + : options.limit + var type = options.type || 'text/plain' + var verify = options.verify || false + + if (verify !== false && typeof verify !== 'function') { + throw new TypeError('option verify must be function') + } + + function parse(buf) { + return buf + } + + return function textParser(req, res, next) { + if (req._body) return next() + req.body = req.body || {} + + if (!typeis(req, type)) return next() + + // get charset + var charset = typer.parse(req).parameters.charset || defaultCharset + + // read + read(req, res, next, parse, { + encoding: charset, + inflate: inflate, + limit: limit, + verify: verify + }) + } +} diff --git a/dgbuilder/dgeflows/node_modules/body-parser/lib/types/urlencoded.js b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/urlencoded.js new file mode 100644 index 00000000..4d88586d --- /dev/null +++ b/dgbuilder/dgeflows/node_modules/body-parser/lib/types/urlencoded.js @@ -0,0 +1,207 @@ +/*! + * body-parser + * Copyright(c) 2014 Jonathan Ong + * Copyright(c) 2014 Douglas Christopher Wilson + * MIT Licensed + */ + +/** + * Module dependencies. + */ + +var bytes = require('bytes') +var deprecate = require('depd')('body-parser') +var read = require('../read') +var typer = require('media-typer') +var typeis = require('type-is') + +/** + * Module exports. + */ + +module.exports = urlencoded + +/** + * Cache of parser modules. + */ + +var parsers = Object.create(null) + +/** + * Create a middleware to parse urlencoded bodies. + * + * @param {object} [options] + * @return {function} + * @api public + */ + +function urlencoded(options){ + options = options || {}; + + // notice because option default will flip in next major + if (options.extended === undefined) { + deprecate('undefined extended: provide extended option') + } + + var extended = options.extended !== false + var inflate = options.inflate !== false + var limit = typeof options.limit !== 'number' + ? bytes(options.limit || '100kb') + : options.limit + var type = options.type || 'urlencoded' + var verify = options.verify || false + + if (verify !== false && typeof verify !== 'function') { + throw new TypeError('option verify must be function') + } + + var queryparse = extended + ? extendedparser(options) + : simpleparser(options) + + function parse(body) { + return body.length + ? queryparse(body) + : {} + } + + return function urlencodedParser(req, res, next) { + if (req._body) return next(); + req.body = req.body || {} + + if (!typeis(req, type)) return next(); + + var charset = (typer.parse(req).parameters.charset || 'utf-8').toLowerCase() + if (charset !== 'utf-8') { + var err = new Error('unsupported charset "' + charset.toUpperCase() + '"') + err.charset = charset + err.status = 415 + next(err) + return + } + + // read + read(req, res, next, parse, { + encoding: charset, + inflate: inflate, + limit: limit, + verify: verify + }) + } +} + +/** + * Get the extended query parser. + * + * @param {object} options + */ + +function extendedparser(options) { + var parameterLimit = options.parameterLimit !== undefined + ? options.parameterLimit + : 1000 + var parse = parser('qs') + + if (isNaN(parameterLimit) || parameterLimit < 1) { + throw new TypeError('option parameterLimit must be a positive number') + } + + if (isFinite(parameterLimit)) { + parameterLimit = parameterLimit | 0 + } + + return function queryparse(body) { + var paramCount = parameterCount(body, parameterLimit) + + if (paramCount === undefined) { + var err = new Error('too many parameters') + err.status = 413 + throw err + } + + var arrayLimit = Math.max(100, paramCount) + + return parse(body, { + arrayLimit: arrayLimit, + parameterLimit: parameterLimit + }) + } +} + +/** + * Count the number of parameters, stopping once limit reached + * + * @param {string} body + * @param {number} limit + * @api private + */ + +function parameterCount(body, limit) { + var count = 0 + var index = 0 + + while ((index = body.indexOf('&', index)) !== -1) { + count++ + index++ + + if (count === limit) { + return undefined + } + } + + return count +} + +/** + * Get parser for module name dynamically. + * + * @param {string} name + * @return {function} + * @api private + */ + +function parser(name) { + var mod = parsers[name] + + if (mod) { + return mod.parse + } + + // load module + mod = parsers[name] = require(name) + + return mod.parse +} + +/** + * Get the simple query parser. + * + * @param {object} options + */ + +function simpleparser(options) { + var parameterLimit = options.parameterLimit !== undefined + ? options.parameterLimit + : 1000 + var parse = parser('querystring') + + if (isNaN(parameterLimit) || parameterLimit < 1) { + throw new TypeError('option parameterLimit must be a positive number') + } + + if (isFinite(parameterLimit)) { + parameterLimit = parameterLimit | 0 + } + + return function queryparse(body) { + var paramCount = parameterCount(body, parameterLimit) + + if (paramCount === undefined) { + var err = new Error('too many parameters') + err.status = 413 + throw err + } + + return parse(body, undefined, undefined, {maxKeys: parameterLimit}) + } +} |