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

Change res.links() to also accept array #2702

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 30 additions & 6 deletions lib/response.js
Expand Up @@ -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-*]+$/;

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)

Copy link
Author

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.

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('; ');

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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
<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last", <http://api.example.com/users?page=1>; rel="prev"

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.
What we can do here is to make no assumptions about the attribute value and let the user define the attributes like this:

{
    href: '"http://api.example.com/users?page=2"', // Quoted string
    rel: '"next"', // Quoted string
    hreflang: 'en' // Not quoted
}

Generating something like this:

<http://api.example.com/users?page=2>; rel="next"; hreflang=en

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.
Expand Down
94 changes: 92 additions & 2 deletions test/res.links.js
Expand Up @@ -4,7 +4,7 @@ var request = require('supertest');

describe('res', function(){
describe('.links(obj)', function(){
it('should set Link header field', function (done) {
it('should set Link header field by passing an object', function (done) {
var app = express();

app.use(function (req, res) {
Expand All @@ -21,7 +21,7 @@ describe('res', function(){
.expect(200, done);
})

it('should set Link header field for multiple calls', function (done) {
it('should set Link header field for multiple calls by passing an object', function (done) {
var app = express();

app.use(function (req, res) {
Expand All @@ -43,4 +43,94 @@ describe('res', function(){
.expect(200, done);
})
})

describe('.links(array)', function(){
it('should set Link header field by passing an array of objects', function (done) {
var app = express();

app.use(function (req, res) {
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'
}]);

res.end();
});

request(app)
.get('/')
.expect('Link', '<http://api.example.com/users?page=2>; rel="next"; title="next chapter"; type="text/plain;charset=UTF-8", <http://api.example.com/users?page=5>; rel="last"; title="last chapter"; type="text/plain;charset=UTF-8"')
.expect(200, done);
})

it('should set Link header field for multiple calls by passing an array of objects', function (done) {
var app = express();

app.use(function (req, res) {
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'
}]);

res.links([{
href: 'http://api.example.com/users?page=1',
rel: 'prev',
title: 'previous',
type: 'text/plain;charset=UTF-8'
}]);

res.end();
});

request(app)
.get('/')
.expect('Link', '<http://api.example.com/users?page=2>; rel="next"; title="next chapter"; type="text/plain;charset=UTF-8", <http://api.example.com/users?page=5>; rel="last"; title="last chapter"; type="text/plain;charset=UTF-8", <http://api.example.com/users?page=1>; rel="prev"; title="previous"; type="text/plain;charset=UTF-8"')
.expect(200, done);
})

it('should ignore invalid keys from the config object.', function (done) {
var app = express();

app.use(function (req, res) {
res.links([{
href: 'http://api.example.com/users?page=2',
rel: 'next',
'>>>>>': 'bad key',
title: 'next chapter',
'title*': 'title in another charset',
type: 'text/plain;charset=UTF-8'
}, {
href: 'http://api.example.com/users?page=5',
rel: 'last',
'*': 'bad key',
'12543': 'bad key',
title: 'last chapter',
type: 'text/plain;charset=UTF-8'
}]);

res.end();
});

request(app)
.get('/')
.expect('Link', '<http://api.example.com/users?page=2>; rel="next"; title="next chapter"; title*="title in another charset"; type="text/plain;charset=UTF-8", <http://api.example.com/users?page=5>; rel="last"; title="last chapter"; type="text/plain;charset=UTF-8"')
.expect(200, done);
})

})

})