Skip to content

Commit 19cc582

Browse files
committedDec 30, 2020
feat: add support for all cookie options
The "cookie" options can now be an object, which will be forwarded to the "cookie" module. The previous syntax is still valid: ``` new Server({ cookieName: "test", cookieHttpOnly: false, cookiePath: "/custom" }) ``` but the new syntax add support for all options: ``` new Server({ cookie: { name: "test", httpOnly: false, path: "/custom" sameSite: "lax" } }) ``` Reference: https://github.com/jshttp/cookie#options-1 Backported from master: a374471
1 parent 5ad2736 commit 19cc582

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed
 

‎lib/server.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,16 @@ Server.prototype.handshake = function (transportName, req) {
316316

317317
if (false !== this.cookie) {
318318
transport.on('headers', function (headers) {
319-
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
320-
{
321-
path: self.cookiePath,
322-
httpOnly: self.cookiePath ? self.cookieHttpOnly : false,
323-
sameSite: true
324-
});
319+
if (typeof self.cookie === 'object') {
320+
headers['Set-Cookie'] = cookieMod.serialize(self.cookie.name, id, self.cookie);
321+
} else {
322+
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
323+
{
324+
path: self.cookiePath,
325+
httpOnly: self.cookiePath ? self.cookieHttpOnly : false,
326+
sameSite: true
327+
});
328+
}
325329
});
326330
}
327331

‎package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"dependencies": {
2828
"accepts": "~1.3.4",
2929
"base64id": "2.0.0",
30-
"cookie": "0.3.1",
30+
"cookie": "~0.4.1",
3131
"debug": "~4.1.0",
3232
"engine.io-parser": "~2.2.0",
3333
"ws": "~7.4.2"

‎test/server.js

+19
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ describe('server', function () {
123123
});
124124
});
125125

126+
it('should forward all cookie options', function (done) {
127+
listen({ cookie: {
128+
name: 'woot',
129+
path: '/test',
130+
httpOnly: true,
131+
sameSite: 'lax'
132+
}}, function (port) {
133+
request.get('http://localhost:%d/engine.io/default/'.s(port))
134+
.query({ transport: 'polling', b64: 1 })
135+
.end(function (err, res) {
136+
expect(err).to.be(null);
137+
// hack-obtain sid
138+
var sid = res.text.match(/"sid":"([^"]+)"/)[1];
139+
expect(res.headers['set-cookie'][0]).to.be('woot=' + sid + '; Path=/test; HttpOnly; SameSite=Lax');
140+
done();
141+
});
142+
});
143+
});
144+
126145
it('should send the io cookie custom name', function (done) {
127146
listen({ cookie: 'woot' }, function (port) {
128147
request.get('http://localhost:%d/engine.io/default/'.s(port))

0 commit comments

Comments
 (0)
Please sign in to comment.