| 'use strict' |
|
|
| |
| |
| |
|
|
| const bytes = require('bytes') |
| const contentType = require('content-type') |
| const typeis = require('type-is') |
|
|
| |
| |
| |
| module.exports = { |
| getCharset, |
| normalizeOptions, |
| passthrough |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function getCharset (req) { |
| const header = req.headers['content-type'] |
| if (!header) return undefined |
| return contentType.parse(header).parameters.charset?.toLowerCase() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function typeChecker (type) { |
| return function checkType (req) { |
| return Boolean(typeis(req, type)) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function normalizeOptions (options, defaultType) { |
| if (!defaultType) { |
| |
| throw new TypeError('defaultType must be provided') |
| } |
|
|
| const inflate = options?.inflate !== false |
| const limit = typeof options?.limit === 'undefined' || options?.limit === null |
| ? 102400 |
| : bytes.parse(options.limit) |
| const type = options?.type || defaultType |
| const verify = options?.verify || false |
| const defaultCharset = options?.defaultCharset || 'utf-8' |
|
|
| if (limit === null) { |
| throw new TypeError(`option limit "${String(options.limit)}" is invalid`) |
| } |
|
|
| if (verify !== false && typeof verify !== 'function') { |
| throw new TypeError('option verify must be function') |
| } |
|
|
| |
| const shouldParse = typeof type !== 'function' |
| ? typeChecker(type) |
| : type |
|
|
| return { |
| inflate, |
| limit, |
| verify, |
| defaultCharset, |
| shouldParse |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function passthrough (value) { |
| return value |
| } |
|
|