-
Notifications
You must be signed in to change notification settings - Fork 983
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
support passing most qs options to restify.queryParser #1209
Merged
Merged
Changes from 1 commit
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.
Jump to
Jump to file
Failed to load files.
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
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 |
---|---|---|
|
@@ -5,6 +5,23 @@ | |
var qs = require('qs'); | ||
var assert = require('assert-plus'); | ||
|
||
var EXPOSED_QS_OPTIONS = { | ||
allowDots: assert.optionalBool, | ||
arrayLimit: assert.optionalNumber, | ||
depth: assert.optionalNumber, | ||
parameterLimit: assert.optionalNumber, | ||
parseArrays: assert.optionalBool, | ||
plainObjects: assert.optionalBool, | ||
strictNullHandling: assert.optionalBool | ||
|
||
/* | ||
* Exclusions (`qs.parse` options that restify does NOT expose): | ||
* - `allowPrototypes`: It is strongly suggested against in qs docs. | ||
* - `decoder` | ||
* - `delimiter`: For query string parsing we shouldn't support anything | ||
* but the default '&'. | ||
*/ | ||
}; | ||
|
||
/** | ||
* Returns a plugin that will parse the query string, and merge the results | ||
|
@@ -22,14 +39,33 @@ function queryParser(options) { | |
} | ||
assert.object(options, 'options'); | ||
|
||
/* | ||
* Releases of restify 4.x up to 4.1.1 used qs@3 which effectively defaulted | ||
* to `plainObjects=true` and `allowDots=true`. To maintain backward | ||
* compatibility for the restify 4.x stream while using the latest qs | ||
* version, we need to maintain those defaults. Note that restify-plugins | ||
* changes back to the pre-restify-4.x behaviour. See test/query.test.js | ||
* for more details. | ||
*/ | ||
var qsOptions = { | ||
plainObjects: true, | ||
allowDots: true | ||
}; | ||
Object.keys(EXPOSED_QS_OPTIONS).forEach(function (k) { | ||
EXPOSED_QS_OPTIONS[k](options[k], k); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mind dropping a comment to describe what's happening here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
if (options.hasOwnProperty(k)) { | ||
qsOptions[k] = options[k]; | ||
} | ||
}); | ||
|
||
function parseQueryString(req, res, next) { | ||
if (!req.getQuery()) { | ||
req.query = {}; | ||
return (next()); | ||
} | ||
|
||
req.query = qs.parse(req.getQuery()); | ||
req.query = qs.parse(req.getQuery(), qsOptions); | ||
|
||
if (options.mapParams !== false) { | ||
Object.keys(req.query).forEach(function (k) { | ||
|
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
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.
Not sure about the performance impact of Object.keys, but presumably you could use https://www.npmjs.com/package/lodash.foreach to optimize this.
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.
restify 4.x doesn't have a dep on any lodash modules. I don't think adding a dep is justified for this here. If iteration code is in a hot path I typically use
for (var i = 0; i < arr.length; i++) ...
which is typically much faster. However, this isn't a hot path at all: it is in section of queryParser that is just called once at server or endpoint setup. It isn't called for every request.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.
Fair enough :)