Skip to content

Commit

Permalink
refactor: Squeeze some more performance out of clearCookie
Browse files Browse the repository at this point in the history
Plus various other refactoring.
  • Loading branch information
nwoltman committed Jun 19, 2019
1 parent b1ee1af commit 80e75ee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
27 changes: 17 additions & 10 deletions cookie.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const {parse: parseCookie, serialize: serializeCookie} = require('cookie');
const {parse, serialize} = require('cookie');
const {sign, unsign} = require('cookie-signature');

const clearCookieExpiresDate = new Date(1);

function cookie(app, {secret} = {}) {
app.decorateRequest('cookies', null);

Expand All @@ -24,24 +22,33 @@ function cookie(app, {secret} = {}) {

function onRequest(req, res, next) {
const cookieHeader = req.headers.cookie;
req.cookies = cookieHeader === undefined ? {} : parseCookie(cookieHeader);

req.cookies = cookieHeader === undefined ? {} : parse(cookieHeader);

next();
}

function setCookie(name, value, options) {
options = Object.assign({path: '/'}, options);
this.append('set-cookie', serializeCookie(name, value, options));
const opts = Object.assign({path: '/'}, options);

this.append('set-cookie', serialize(name, value, opts));

return this;
}

const clearCookieExpiresDate = new Date(1);

function clearCookie(name, options) {
// IE/Edge don't support Max-Age=0, so use Expires instead
options = Object.assign({expires: null, maxAge: null}, options);
options.expires = clearCookieExpiresDate;
options.maxAge = null; // In case options contains maxAge
const clearOpts = Object.assign({
expires: clearCookieExpiresDate,
maxAge: null,
path: '/',
}, options);

clearOpts.maxAge = null; // In case options contains maxAge

this.setCookie(name, '', options);
this.append('set-cookie', serialize(name, '', clearOpts));

return this;
}
Expand Down
23 changes: 19 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('res.clearCookie()', () => {
const app = makeApp();

app.get('/', (req, res) => {
res.clearCookie('foo', 'bar');
res.clearCookie('foo');
res.send();
});

Expand All @@ -90,18 +90,33 @@ describe('res.clearCookie()', () => {
);
});

it('should clear a cookie with options', async () => {
const app = makeApp();

app.get('/', (req, res) => {
res.clearCookie('foo', {maxAge: 3600, httpOnly: true, secure: true});
res.send();
});

const res = await app.request('/');
assert.deepStrictEqual(
res.headers['set-cookie'],
['foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure']
);
});

it('should clear multiple cookies', async () => {
const app = makeApp();

app.get('/', (req, res) => {
res.clearCookie('a', {expires: new Date()});
res.clearCookie('b', {maxAge: 3600, httpOnly: true, secure: true});
res.clearCookie('a', {path: '/some/path'});
res.clearCookie('b', {httpOnly: true, secure: true});
res.send();
});

const res = await app.request('/');
assert.deepStrictEqual(res.headers['set-cookie'], [
'a=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT',
'a=; Path=/some/path; Expires=Thu, 01 Jan 1970 00:00:00 GMT',
'b=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure',
]);
});
Expand Down

0 comments on commit 80e75ee

Please sign in to comment.