Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract shared utility functions #550

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
=========================

* extract shared utility functions
* remove `unpipe` package and use native `unpipe()` method

2.0.1 / 2024-09-10
Expand Down
30 changes: 1 addition & 29 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/

var bytes = require('bytes')
var contentType = require('content-type')
var createError = require('http-errors')
var debug = require('debug')('body-parser:json')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var { getCharset, typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -196,21 +196,6 @@ function firstchar (str) {
: 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.
*
Expand All @@ -235,16 +220,3 @@ function normalizeJsonSyntaxError (error, obj) {

return error
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
14 changes: 1 addition & 13 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var debug = require('debug')('body-parser:raw')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var { typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -89,16 +90,3 @@ function raw (options) {
})
}
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
30 changes: 1 addition & 29 deletions lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/

var bytes = require('bytes')
var contentType = require('content-type')
var debug = require('debug')('body-parser:text')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var { getCharset, typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -94,31 +94,3 @@ function text (options) {
})
}
}

/**
* 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))
}
}
30 changes: 1 addition & 29 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
*/

var bytes = require('bytes')
var contentType = require('content-type')
var createError = require('http-errors')
var debug = require('debug')('body-parser:urlencoded')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var qs = require('qs')
var { getCharset, typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -184,21 +184,6 @@ function createQueryParser (options, extended) {
}
}

/**
* 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
*
Expand All @@ -222,16 +207,3 @@ function parameterCount (body, limit) {

return count
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
51 changes: 51 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*!
* body-parser
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/

'use strict'

/**
* Module dependencies.
*/

var contentType = require('content-type')
var typeis = require('type-is')

/**
* Module exports.
*/

module.exports = {
getCharset,
typeChecker
}

/**
* 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))
}
}