* Normalizes the common options for all parsers. * * @param {Object} options options to normalize * @param {string | string[] | Function} defaultType default content type(s) or a function to determine it * @returns {Object} * @private
(options, defaultType)
| 37 | * @private |
| 38 | */ |
| 39 | function normalizeOptions (options, defaultType) { |
| 40 | if (!defaultType) { |
| 41 | // Parsers must define a default content type |
| 42 | throw new TypeError('defaultType must be provided') |
| 43 | } |
| 44 | |
| 45 | const inflate = options?.inflate !== false |
| 46 | const limit = typeof options?.limit === 'undefined' || options?.limit === null |
| 47 | ? 102400 // 100kb default |
| 48 | : bytes.parse(options.limit) |
| 49 | const type = options?.type || defaultType |
| 50 | const verify = options?.verify || false |
| 51 | const defaultCharset = options?.defaultCharset || 'utf-8' |
| 52 | |
| 53 | if (limit === null) { |
| 54 | throw new TypeError(`option limit "${String(options.limit)}" is invalid`) |
| 55 | } |
| 56 | |
| 57 | if (verify !== false && typeof verify !== 'function') { |
| 58 | throw new TypeError('option verify must be function') |
| 59 | } |
| 60 | |
| 61 | // create the appropriate type checking function |
| 62 | const shouldParse = typeof type !== 'function' |
| 63 | ? typeChecker(type) |
| 64 | : type |
| 65 | |
| 66 | return { |
| 67 | inflate, |
| 68 | limit, |
| 69 | verify, |
| 70 | defaultCharset, |
| 71 | shouldParse |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Passthrough function that returns input unchanged. |
no test coverage detected