* 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)
| 52 | * @private |
| 53 | */ |
| 54 | function normalizeOptions (options, defaultType) { |
| 55 | if (!defaultType) { |
| 56 | // Parsers must define a default content type |
| 57 | throw new TypeError('defaultType must be provided') |
| 58 | } |
| 59 | |
| 60 | const inflate = options?.inflate !== false |
| 61 | const limit = typeof options?.limit === 'undefined' || options?.limit === null |
| 62 | ? 102400 // 100kb default |
| 63 | : bytes.parse(options.limit) |
| 64 | const type = options?.type || defaultType |
| 65 | const verify = options?.verify || false |
| 66 | const defaultCharset = options?.defaultCharset || 'utf-8' |
| 67 | |
| 68 | if (limit === null) { |
| 69 | throw new TypeError(`option limit "${String(options.limit)}" is invalid`) |
| 70 | } |
| 71 | |
| 72 | if (verify !== false && typeof verify !== 'function') { |
| 73 | throw new TypeError('option verify must be function') |
| 74 | } |
| 75 | |
| 76 | // create the appropriate type checking function |
| 77 | const shouldParse = typeof type !== 'function' |
| 78 | ? typeChecker(type) |
| 79 | : type |
| 80 | |
| 81 | return { |
| 82 | inflate, |
| 83 | limit, |
| 84 | verify, |
| 85 | defaultCharset, |
| 86 | shouldParse |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Passthrough function that returns input unchanged. |
no test coverage detected
searching dependent graphs…