-
Notifications
You must be signed in to change notification settings - Fork 92
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 and rework http coercion. #231
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Expose `httpCoerce` | ||
*/ | ||
|
||
module.exports = httpCoerce; | ||
|
||
/** | ||
* Do a sloppy string coercion into a target type. | ||
* Useful for params sent via HTTP params, querystring, headers, or non-JSON body. | ||
* | ||
* @param {*} val Value to coerce. Only works on strings, just returns non-string values. | ||
* @param {string|Array<String>} type Type to coerce to. | ||
* @param {Context} HTTP Context. | ||
*/ | ||
|
||
function httpCoerce(val, type, ctx) { | ||
// If an array type if defined, regardless of what type it is, try to coerce the string | ||
// into an array. | ||
if (Array.isArray(type)) { | ||
val = coerceArray(val, ctx); | ||
// Members may need to be coerced as well. | ||
val = val.map(function(v) { | ||
return httpCoerce(v, type[0], ctx); | ||
}); | ||
} else if (type === 'any' || type === 'object') { | ||
if (val && typeof val === 'object') { | ||
// Objects should have all their members coerced. | ||
var props = Object.keys(val); | ||
for (var i = 0, n = props.length; i < n; i++) { | ||
var key = props[i]; | ||
val[key] = httpCoerce(val[key], 'any', ctx); | ||
} | ||
} else { | ||
// If the type specified is 'any', do sloppy string conversion. | ||
val = coerceString(val); | ||
} | ||
} | ||
return val; | ||
} | ||
|
||
/*! | ||
* Integer test regexp. Doesn't match if number has a leading zero. | ||
*/ | ||
|
||
var isInt = /^\-?(?:[0-9]|[1-9][0-9]*)$/; | ||
|
||
/*! | ||
* Float test regexp. | ||
*/ | ||
|
||
var isFloat = /^([0-9]+)?\.[0-9]+$/; | ||
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1; | ||
|
||
function coerceString(val) { | ||
if (typeof val !== 'string') return val; | ||
if (coerceMap.hasOwnProperty(val)) return coerceMap[val]; | ||
if (isFloat.test(val) || isInt.test(val)) { | ||
var out = Number(val); | ||
// Cap at MAX_SAFE_INTEGER so we don't lose precision. | ||
if (out > MAX_SAFE_INTEGER) out = val; | ||
return out; | ||
} | ||
// Nothing matched; return string. | ||
return val; | ||
} | ||
|
||
function coerceArray(val, ctx) { | ||
if (val === undefined || val === null || val === '') return []; | ||
if (Array.isArray(val)) return val; | ||
// If it looks like an array, try to parse it. | ||
if (val[0] === '[') { | ||
try { | ||
return JSON.parse(val); | ||
} catch (ex) { /* Do nothing */ } | ||
} | ||
|
||
// The user may set delimiters like ',', or ';' to designate array items | ||
// for easier usage. | ||
var delims = ctx.options && ctx.options.arrayItemDelimiters; | ||
if (delims) { | ||
// Construct delimiter regex if input was an array. Overwrite option | ||
// so this only needs to happen once. | ||
if (Array.isArray(delims)) { | ||
delims = new RegExp(delims.map(escapeRegex).join('|'), 'g'); | ||
ctx.options.arrayItemDelimiters = delims; | ||
} | ||
return val.split(delims); | ||
} | ||
// Alright, not array-like, just wrap it in an array on the way out. | ||
return [val]; | ||
} | ||
|
||
// see http://stackoverflow.com/a/6969486/69868 | ||
function escapeRegex(d) { | ||
return d.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); | ||
} | ||
|
||
// Map of some values to convert directly to primitives. | ||
var coerceMap = { | ||
'false': false, | ||
'true': true, | ||
'undefined': undefined, | ||
'null': null | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid this closure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if we're going to pass
ctx
toconverter
- and the anon func syntax is still quite a bit faster than bind. It's not async, no chance of a leak here because of vars bound outside the closure.