Skip to content

Commit

Permalink
Merge pull request #69 from nigrosimone/issue-68
Browse files Browse the repository at this point in the history
fix: #68
  • Loading branch information
dimdenGD authored Nov 24, 2024
2 parents 4157079 + ab16aee commit 641eb71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,26 +601,24 @@ module.exports = class Response extends Writable {
this.app.render(view, options, done);
}
cookie(name, value, options) {
if(!options) {
options = {};
}
const opt = {...(options ?? {})}; // create a new ref because we change original object (https://github.com/dimdenGD/ultimate-express/issues/68)
let val = typeof value === 'object' ? "j:"+JSON.stringify(value) : String(value);
if(options.maxAge != null) {
const maxAge = options.maxAge - 0;
if(opt.maxAge != null) {
const maxAge = opt.maxAge - 0;
if(!isNaN(maxAge)) {
options.expires = new Date(Date.now() + maxAge);
options.maxAge = Math.floor(maxAge / 1000);
opt.expires = new Date(Date.now() + maxAge);
opt.maxAge = Math.floor(maxAge / 1000);
}
}
if(options.signed) {
if(opt.signed) {
val = 's:' + sign(val, this.req.secret);
}

if(options.path == null) {
options.path = '/';
if(opt.path == null) {
opt.path = '/';
}

this.append('Set-Cookie', cookie.serialize(name, val, options));
this.append('Set-Cookie', cookie.serialize(name, val, opt));
return this;
}
clearCookie(name, options) {
Expand Down
8 changes: 8 additions & 0 deletions tests/tests/res/res-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ const express = require("express");

const app = express();

const sharedOptions = { maxAge: 1000 };

app.get('/test', (req, res) => {
res.cookie('test', '1');
res.cookie('test2', '2', { maxAge: 1000 });
res.cookie('test3', '3', { maxAge: 1000, path: '/test' });
res.cookie('test4', '4', { maxAge: 1000, path: '/test', httpOnly: true });
res.cookie('test5', '5', { maxAge: 1000, path: '/test', secure: true });

// see: https://github.com/dimdenGD/ultimate-express/issues/68
res.cookie('test6', '2', sharedOptions);
res.cookie('test6', '2', sharedOptions);
console.log(JSON.stringify(sharedOptions))

res.send('test');
});

Expand Down

0 comments on commit 641eb71

Please sign in to comment.