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
Change res.links() to also accept array #2702
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,18 +70,42 @@ res.status = function status(code) { | |
* last: 'http://api.example.com/users?page=5' | ||
* }); | ||
* | ||
* @param {Object} links | ||
* res.links([{ | ||
* href: 'http://api.example.com/users?page=2', | ||
* rel: 'next', | ||
* title: 'next chapter', | ||
* type: 'text/plain;charset=UTF-8' | ||
* }, { | ||
* href: 'http://api.example.com/users?page=5', | ||
* rel: 'last', | ||
* title: 'last chapter', | ||
* type: 'text/plain;charset=UTF-8' | ||
* }]); | ||
* | ||
* | ||
* @param {Object|Array} links | ||
* @return {ServerResponse} | ||
* @public | ||
*/ | ||
|
||
res.links = function(links){ | ||
res.links = function(links) { | ||
var link = this.get('Link') || ''; | ||
if (link) link += ', '; | ||
return this.set('Link', link + Object.keys(links).map(function(rel){ | ||
return '<' + links[rel] + '>; rel="' + rel + '"'; | ||
}).join(', ')); | ||
}; | ||
if (Array.isArray(links)) { | ||
var attrTest = /^[a-z][a-z-*]+$/; | ||
for (var i = 0; i < links.length; i++) { | ||
if (i !== 0) link += ', '; // do not prepend ', ' if it's the first link | ||
link += '<' + links[i].href + '>; '; | ||
link += Object.keys(links[i]).filter(function (key) { return key.toLowerCase() !== 'href' && attrTest.test(key); }) | ||
.map(function(key) { return key + '="' + links[i][key] + '"'; }).join('; '); | ||
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. This (and line 106) only allow quoted-string values. Token and extended notation values should also be allowed. hreflang, for example, only allows tokens. title* only allows extended notation values. See rfc5988. 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. There are two valid parameters for this function, an object like: {
next: 'http://api.example.com/users?page=2',
last: 'http://api.example.com/users?page=5'
} Which generates a link like this That's the implementation you see on line 106 and kept that way for backwards compatibility This one is for the array parameter, which lets the user enter fine tune the links header. {
href: '"http://api.example.com/users?page=2"', // Quoted string
rel: '"next"', // Quoted string
hreflang: 'en' // Not quoted
} Generating something like this:
Thoughts? |
||
} | ||
return this.set('Link', link); | ||
} else { | ||
return this.set('Link', link + Object.keys(links).map(function(rel){ | ||
return '<' + links[rel] + '>; rel="' + rel + '"'; | ||
}).join(', ')); | ||
} | ||
} | ||
|
||
/** | ||
* Send a response. | ||
|
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.
/^[\w!#$&+.^
|~-]+*?$/` would be the exact same as the productions in rfc5988 (see the link-extension production)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.
This regexp allows for numeric-only attributes to be allowed. Check this test
There is a bit more conversation about this here on #2619 This PR is a continuation of that, since the original author hasn't responded for a while.