app-lib-mock-server-parse
Version:
body解析-【app-lib-mock-server-parse】
1,466 lines (1,186 loc) • 33.3 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("bytes"), require("content-type"), require("debug"), require("depd"), require("destroy"), require("http-errors"), require("iconv-lite"), require("on-finished"), require("qs"), require("querystring"), require("raw-body"), require("type-is"), require("unpipe"), require("zlib"));
else if(typeof define === 'function' && define.amd)
define(["bytes", "content-type", "debug", "depd", "destroy", "http-errors", "iconv-lite", "on-finished", "qs", "querystring", "raw-body", "type-is", "unpipe", "zlib"], factory);
else {
var a = typeof exports === 'object' ? factory(require("bytes"), require("content-type"), require("debug"), require("depd"), require("destroy"), require("http-errors"), require("iconv-lite"), require("on-finished"), require("qs"), require("querystring"), require("raw-body"), require("type-is"), require("unpipe"), require("zlib")) : factory(root["bytes"], root["content-type"], root["debug"], root["depd"], root["destroy"], root["http-errors"], root["iconv-lite"], root["on-finished"], root["qs"], root["querystring"], root["raw-body"], root["type-is"], root["unpipe"], root["zlib"]);
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, (__WEBPACK_EXTERNAL_MODULE__348__, __WEBPACK_EXTERNAL_MODULE__897__, __WEBPACK_EXTERNAL_MODULE__999__, __WEBPACK_EXTERNAL_MODULE__622__, __WEBPACK_EXTERNAL_MODULE__57__, __WEBPACK_EXTERNAL_MODULE__790__, __WEBPACK_EXTERNAL_MODULE__671__, __WEBPACK_EXTERNAL_MODULE__544__, __WEBPACK_EXTERNAL_MODULE__656__, __WEBPACK_EXTERNAL_MODULE__871__, __WEBPACK_EXTERNAL_MODULE__379__, __WEBPACK_EXTERNAL_MODULE__240__, __WEBPACK_EXTERNAL_MODULE__961__, __WEBPACK_EXTERNAL_MODULE__638__) => {
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({
/***/ 814:
/***/ ((module, exports, __webpack_require__) => {
;
/*!
* body-parser
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module dependencies.
* @private
*/
var deprecate = __webpack_require__(622)('body-parser')
/**
* Cache of loaded parsers.
* @private
*/
var parsers = Object.create(null)
/**
* @typedef Parsers
* @type {function}
* @property {function} json
* @property {function} raw
* @property {function} text
* @property {function} urlencoded
*/
/**
* Module exports.
* @type {Parsers}
*/
exports = module.exports = deprecate.function(bodyParser,
'bodyParser: use individual json/urlencoded middlewares')
/**
* JSON parser.
* @public
*/
Object.defineProperty(exports, "json", ({
configurable: true,
enumerable: true,
get: createParserGetter('json')
}))
/**
* Raw parser.
* @public
*/
Object.defineProperty(exports, "raw", ({
configurable: true,
enumerable: true,
get: createParserGetter('raw')
}))
/**
* Text parser.
* @public
*/
Object.defineProperty(exports, "text", ({
configurable: true,
enumerable: true,
get: createParserGetter('text')
}))
/**
* URL-encoded parser.
* @public
*/
Object.defineProperty(exports, "urlencoded", ({
configurable: true,
enumerable: true,
get: createParserGetter('urlencoded')
}))
/**
* Create a middleware to parse json and urlencoded bodies.
*
* @param {object} [options]
* @return {function}
* @deprecated
* @public
*/
function bodyParser (options) {
// use default type for parsers
var opts = Object.create(options || null, {
type: {
configurable: true,
enumerable: true,
value: undefined,
writable: true
}
})
var _urlencoded = exports.urlencoded(opts)
var _json = exports.json(opts)
return function bodyParser (req, res, next) {
_json(req, res, function (err) {
if (err) return next(err)
_urlencoded(req, res, next)
})
}
}
/**
* Create a getter for loading a parser.
* @private
*/
function createParserGetter (name) {
return function get () {
return loadParser(name)
}
}
/**
* Load a parser module.
* @private
*/
function loadParser (parserName) {
var parser = parsers[parserName]
if (parser !== undefined) {
return parser
}
// this uses a switch for static require analysis
switch (parserName) {
case 'json':
parser = __webpack_require__(2)
break
case 'raw':
parser = __webpack_require__(842)
break
case 'text':
parser = __webpack_require__(492)
break
case 'urlencoded':
parser = __webpack_require__(980)
break
}
// store to prevent invoking require()
return (parsers[parserName] = parser)
}
/***/ }),
/***/ 992:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
;
/*!
* body-parser
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module dependencies.
* @private
*/
var createError = __webpack_require__(790)
var destroy = __webpack_require__(57)
var getBody = __webpack_require__(379)
var iconv = __webpack_require__(671)
var onFinished = __webpack_require__(544)
var unpipe = __webpack_require__(961)
var zlib = __webpack_require__(638)
/**
* 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 {function} debug
* @param {object} options
* @private
*/
function read(req, res, next, parse, debug, options) {
var length = req.__bodyData?.length; // 读取过 直接获取
var opts = options;
var stream = req.__bodyData?.stream; // 读取过 直接获取
// flag as parsed
req._body = true;
// read options
var encoding = opts.encoding !== null
? opts.encoding
: null
var verify = opts.verify;
if (!req.__bodyData) { // 扩展支持未读取的时候 进行读取
try {
// get the content stream
stream = contentstream(req, debug, opts.inflate)
length = stream.length
stream.length = undefined
req.__bodyData = {
length
}
} catch (err) {
return next(err)
}
}
// set raw-body options
opts.length = length
opts.encoding = verify
? null
: encoding
// assert charset is supported
if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) {
return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
charset: encoding.toLowerCase(),
type: 'charset.unsupported'
}))
}
// read body
debug('read body')
if (req.__bodyData.readStream) {
var body = req.__bodyData.body;
// verify
if (verify) {
try {
debug('verify body')
verify(req, res, body, encoding)
} catch (err) {
next(createError(403, err, {
body: body,
type: err.type || 'entity.verify.failed'
}))
return
}
}
var str = body;
try {
debug('parse body')
str = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
} catch (err) {
next(createError(400, err, {
body: str,
type: err.type || 'entity.parse.failed'
}))
return
}
next()
return;
}
getBody(stream, opts, function (error, body) {
req.__bodyData.readStream = true;
req.__bodyData.body = body;
if (error) {
var _error
if (error.type === 'encoding.unsupported') {
// echo back charset
_error = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
charset: encoding.toLowerCase(),
type: 'charset.unsupported'
})
} else {
// set status code on error
_error = createError(400, error)
}
// unpipe from stream and destroy
if (stream !== req) {
unpipe(req)
destroy(stream, true)
}
// read off entire request
dump(req, function onfinished() {
next(createError(400, _error))
})
return
}
// verify
if (verify) {
try {
debug('verify body')
verify(req, res, body, encoding)
} catch (err) {
next(createError(403, err, {
body: body,
type: err.type || 'entity.verify.failed'
}))
return
}
}
// parse
var str = body
try {
debug('parse body')
str = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
} catch (err) {
next(createError(400, err, {
body: str,
type: err.type || 'entity.parse.failed'
}))
return
}
next()
})
}
/**
* Get the content stream of the request.
*
* @param {object} req
* @param {function} debug
* @param {boolean} [inflate=true]
* @return {object}
* @api private
*/
function contentstream(req, debug, inflate) {
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
var length = req.headers['content-length']
var stream
debug('content-encoding "%s"', encoding)
if (inflate === false && encoding !== 'identity') {
throw createError(415, 'content encoding unsupported', {
encoding: encoding,
type: 'encoding.unsupported'
})
}
switch (encoding) {
case 'deflate':
stream = zlib.createInflate()
debug('inflate body')
req.pipe(stream)
break
case 'gzip':
stream = zlib.createGunzip()
debug('gunzip body')
req.pipe(stream)
break
case 'identity':
stream = req
stream.length = length
break
default:
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
encoding: encoding,
type: 'encoding.unsupported'
})
}
return stream
}
/**
* Dump the contents of a request.
*
* @param {object} req
* @param {function} callback
* @api private
*/
function dump(req, callback) {
if (onFinished.isFinished(req)) {
callback(null)
} else {
onFinished(req, callback)
req.resume()
}
}
/***/ }),
/***/ 2:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
;
/*!
* body-parser
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module dependencies.
* @private
*/
var bytes = __webpack_require__(348)
var contentType = __webpack_require__(897)
var createError = __webpack_require__(790)
var debug = __webpack_require__(999)('body-parser:json')
var read = __webpack_require__(992)
var typeis = __webpack_require__(240)
/**
* 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 FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
/**
* Create a middleware to parse JSON bodies.
*
* @param {object} [options]
* @return {function}
* @public
*/
function json (options) {
var opts = options || {}
var limit = typeof opts.limit !== 'number'
? bytes.parse(opts.limit || '100kb')
: opts.limit
var inflate = opts.inflate !== false
var reviver = opts.reviver
var strict = opts.strict !== false
var type = opts.type || 'application/json'
var verify = opts.verify || false
if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}
// create the appropriate type checking function
var shouldParse = typeof type !== 'function'
? typeChecker(type)
: type
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 !== '[') {
debug('strict violation')
throw createStrictSyntaxError(body, first)
}
}
try {
debug('parse json')
return JSON.parse(body, reviver)
} catch (e) {
throw normalizeJsonSyntaxError(e, {
message: e.message,
stack: e.stack
})
}
}
return function jsonParser (req, res, next) {
if (req._body) {
debug('body already parsed')
next()
return
}
req.body = req.body || {}
// skip requests without bodies
if (!typeis.hasBody(req)) {
debug('skip empty body')
next()
return
}
debug('content-type %j', req.headers['content-type'])
// determine if request should be parsed
if (!shouldParse(req)) {
debug('skip parsing')
next()
return
}
// assert charset per RFC 7159 sec 8.1
var charset = getCharset(req) || 'utf-8'
if (charset.slice(0, 4) !== 'utf-') {
debug('invalid charset')
next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
charset: charset,
type: 'charset.unsupported'
}))
return
}
// read
read(req, res, next, parse, debug, {
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
})
}
}
/**
* Create strict violation syntax error matching native error.
*
* @param {string} str
* @param {string} char
* @return {Error}
* @private
*/
function createStrictSyntaxError (str, char) {
var index = str.indexOf(char)
var partial = index !== -1
? str.substring(0, index) + '#'
: ''
try {
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation')
} catch (e) {
return normalizeJsonSyntaxError(e, {
message: e.message.replace('#', char),
stack: e.stack
})
}
}
/**
* Get the first non-whitespace character in a string.
*
* @param {string} str
* @return {function}
* @private
*/
function firstchar (str) {
var match = FIRST_CHAR_REGEXP.exec(str)
return match
? match[1]
: undefined
}
/**
* Get the charset of a request.
*
* @param {object} req
* @api private
*/
function getCharset (req) {
try {
return (contentType.parse(req).parameters.charset || '').toLowerCase()
} catch (e) {
return undefined
}
}
/**
* Normalize a SyntaxError for JSON.parse.
*
* @param {SyntaxError} error
* @param {object} obj
* @return {SyntaxError}
*/
function normalizeJsonSyntaxError (error, obj) {
var keys = Object.getOwnPropertyNames(error)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
if (key !== 'stack' && key !== 'message') {
delete error[key]
}
}
// replace stack before message for Node.js 0.10 and below
error.stack = obj.stack.replace(error.message, obj.message)
error.message = obj.message
return error
}
/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/
function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
/***/ }),
/***/ 842:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
;
/*!
* body-parser
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module dependencies.
*/
var bytes = __webpack_require__(348)
var debug = __webpack_require__(999)('body-parser:raw')
var read = __webpack_require__(992)
var typeis = __webpack_require__(240)
/**
* Module exports.
*/
module.exports = raw
/**
* Create a middleware to parse raw bodies.
*
* @param {object} [options]
* @return {function}
* @api public
*/
function raw (options) {
var opts = options || {}
var inflate = opts.inflate !== false
var limit = typeof opts.limit !== 'number'
? bytes.parse(opts.limit || '100kb')
: opts.limit
var type = opts.type || 'application/octet-stream'
var verify = opts.verify || false
if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}
// create the appropriate type checking function
var shouldParse = typeof type !== 'function'
? typeChecker(type)
: type
function parse (buf) {
return buf
}
return function rawParser (req, res, next) {
if (req._body) {
debug('body already parsed')
next()
return
}
req.body = req.body || {}
// skip requests without bodies
if (!typeis.hasBody(req)) {
debug('skip empty body')
next()
return
}
debug('content-type %j', req.headers['content-type'])
// determine if request should be parsed
if (!shouldParse(req)) {
debug('skip parsing')
next()
return
}
// read
read(req, res, next, parse, debug, {
encoding: null,
inflate: inflate,
limit: limit,
verify: verify
})
}
}
/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/
function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
/***/ }),
/***/ 492:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
;
/*!
* body-parser
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module dependencies.
*/
var bytes = __webpack_require__(348)
var contentType = __webpack_require__(897)
var debug = __webpack_require__(999)('body-parser:text')
var read = __webpack_require__(992)
var typeis = __webpack_require__(240)
/**
* Module exports.
*/
module.exports = text
/**
* Create a middleware to parse text bodies.
*
* @param {object} [options]
* @return {function}
* @api public
*/
function text(options) {
var opts = options || {}
var defaultCharset = opts.defaultCharset || 'utf-8'
var inflate = opts.inflate !== false
var limit = typeof opts.limit !== 'number'
? bytes.parse(opts.limit || '100kb')
: opts.limit
var type = opts.type || 'text/plain'
var verify = opts.verify || false
if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}
// create the appropriate type checking function
var shouldParse = typeof type !== 'function'
? typeChecker(type)
: type
function parse(buf) {
return buf
}
return function textParser(req, res, next) {
if (req._body) {
debug('body already parsed')
next()
return
}
req.body = req.body || {}
// skip requests without bodies
// 增加强制文本解析
const isTextParase = (req) => {
return req.__forceParse == "text"
}
if (!typeis.hasBody(req)) {
debug('skip empty body')
next()
return
}
debug('content-type %j', req.headers['content-type'])
// determine if request should be parsed
if (!isTextParase(req) && !shouldParse(req)) {
debug('skip parsing')
next()
return
}
// get charset
var charset = getCharset(req) || defaultCharset
// read
read(req, res, next, parse, debug, {
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
})
}
}
/**
* Get the charset of a request.
*
* @param {object} req
* @api private
*/
function getCharset(req) {
try {
return (contentType.parse(req).parameters.charset || '').toLowerCase()
} catch (e) {
return undefined
}
}
/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/
function typeChecker(type) {
return function checkType(req) {
return Boolean(typeis(req, type))
}
}
/***/ }),
/***/ 980:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
;
/*!
* body-parser
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module dependencies.
* @private
*/
var bytes = __webpack_require__(348)
var contentType = __webpack_require__(897)
var createError = __webpack_require__(790)
var debug = __webpack_require__(999)('body-parser:urlencoded')
var deprecate = __webpack_require__(622)('body-parser')
var read = __webpack_require__(992)
var typeis = __webpack_require__(240)
/**
* 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}
* @public
*/
function urlencoded (options) {
var opts = options || {}
// notice because option default will flip in next major
if (opts.extended === undefined) {
deprecate('undefined extended: provide extended option')
}
var extended = opts.extended !== false
var inflate = opts.inflate !== false
var limit = typeof opts.limit !== 'number'
? bytes.parse(opts.limit || '100kb')
: opts.limit
var type = opts.type || 'application/x-www-form-urlencoded'
var verify = opts.verify || false
if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}
// create the appropriate query parser
var queryparse = extended
? extendedparser(opts)
: simpleparser(opts)
// create the appropriate type checking function
var shouldParse = typeof type !== 'function'
? typeChecker(type)
: type
function parse (body) {
return body.length
? queryparse(body)
: {}
}
return function urlencodedParser (req, res, next) {
if (req._body) {
debug('body already parsed')
next()
return
}
req.body = req.body || {}
// skip requests without bodies
if (!typeis.hasBody(req)) {
debug('skip empty body')
next()
return
}
debug('content-type %j', req.headers['content-type'])
// determine if request should be parsed
if (!shouldParse(req)) {
debug('skip parsing')
next()
return
}
// assert charset
var charset = getCharset(req) || 'utf-8'
if (charset !== 'utf-8') {
debug('invalid charset')
next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
charset: charset,
type: 'charset.unsupported'
}))
return
}
// read
read(req, res, next, parse, debug, {
debug: debug,
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) {
debug('too many parameters')
throw createError(413, 'too many parameters', {
type: 'parameters.too.many'
})
}
var arrayLimit = Math.max(100, paramCount)
debug('parse extended urlencoding')
return parse(body, {
allowPrototypes: true,
arrayLimit: arrayLimit,
depth: Infinity,
parameterLimit: parameterLimit
})
}
}
/**
* Get the charset of a request.
*
* @param {object} req
* @api private
*/
function getCharset (req) {
try {
return (contentType.parse(req).parameters.charset || '').toLowerCase()
} catch (e) {
return undefined
}
}
/**
* 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 !== undefined) {
return mod.parse
}
// this uses a switch for static require analysis
switch (name) {
case 'qs':
mod = __webpack_require__(656)
break
case 'querystring':
mod = __webpack_require__(871)
break
}
// store to prevent invoking require()
parsers[name] = mod
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) {
debug('too many parameters')
throw createError(413, 'too many parameters', {
type: 'parameters.too.many'
})
}
debug('parse urlencoding')
return parse(body, undefined, undefined, { maxKeys: parameterLimit })
}
}
/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/
function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
/***/ }),
/***/ 138:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
/*************************************************************************************
*
* 动态解析或者指定解析方式
*
* 1. 当前系统存在多种或一种解析方式 往往一种不一定能满足需求 故自动解析
* 2. 动态根据contenttype + body内容合法性校验 进行解析
* 3. 可在插件中间件中prase进行配置
*
*
**************************************************************************************/
/*************************************************************************************
*
* 动态解析或者指定解析方式
*
*
* 1.中间件配置
*
* ```
* "params":{
* "paraseType":"text",
* // 解析类型 text 文件 json json格式 raw raw格式 后续有需要自定义该插件
* }
* ```
*
*
**************************************************************************************/
/*************************************************************************************
*
* 改造body-parser
*
* 扩展
* 1. 原有的解析 一个解析失败后 另个不能在解析 针对json类型需要text解析的情况
*
*
**************************************************************************************/
const PARSER = __webpack_require__(814);
const middwareName = "MIDDWARE_PARSE"
// stream 读过一次不能再读--TODO 先用json 自用其他?
const myNext = (req, next, type, log) => {
return (error) => {
if (error) {
req._body = undefined; // 清空内部解析记录 继续下一个解析
log.md(middwareName, `${type} parse error`, error.type, error.message)
} else {
if (!req.__parse) {
req.__parse = type;
}
}
next();
}
}
// https://github.com/expressjs/body-parser
const bodyParser = (parseOpt, type) => {
let { option: { log: { log } } } = Frame;
return (req, res, next) => {
// 设置强制解析
if (parseOpt.forceParse) {
req.__forceParse = type;
}
PARSER[type](parseOpt || {})(req, res, myNext(req, next, type, log))
}
};
const PARSES = {
json: bodyParser,
text: bodyParser,
raw: bodyParser,
urlencoded: bodyParser,
// 后续按需扩展
}
const middleware = (middlewareConfig) => {
let { params } = middlewareConfig;
let types = params.paraseType || ["json"]; // 目前支持四种 ["json","raw","text","urlencoded"]
// 按照配置解析顺序进行解析
return types.map(type => {
let parasemiddware = PARSES[type];
if (!parasemiddware) {
log.mwn(middwareName, `current not spport parase [${type}]`);
return null;
} else {
return { position: 'begin', method: "use", alias: type, matchPath: "", middleware: parasemiddware(params[type], type) };
}
}).filter(v => !!v);
}
module.exports = { middleware }
/***/ }),
/***/ 348:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__348__;
/***/ }),
/***/ 897:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__897__;
/***/ }),
/***/ 999:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__999__;
/***/ }),
/***/ 622:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__622__;
/***/ }),
/***/ 57:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__57__;
/***/ }),
/***/ 790:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__790__;
/***/ }),
/***/ 671:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__671__;
/***/ }),
/***/ 544:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__544__;
/***/ }),
/***/ 656:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__656__;
/***/ }),
/***/ 871:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__871__;
/***/ }),
/***/ 379:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__379__;
/***/ }),
/***/ 240:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__240__;
/***/ }),
/***/ 961:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__961__;
/***/ }),
/***/ 638:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__638__;
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __webpack_require__(138);
/******/
/******/ return __webpack_exports__;
/******/ })()
;
});
//# sourceMappingURL=index.js.map