Skip to content

Commit 11d0f74

Browse files
CatTaildougwilson
authored andcommitted
Change prototype construction for proper "constructor" property
closes #60
1 parent d141e58 commit 11d0f74

File tree

4 files changed

+82
-74
lines changed

4 files changed

+82
-74
lines changed

History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.6.x
2+
=====
3+
4+
* Change prototype construction for proper "constructor" property
5+
16
0.5.1 / 2014-07-27
27
==================
38

lib/cookies.js

Lines changed: 70 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -27,67 +27,65 @@ function Cookies(request, response, keys) {
2727
}
2828
}
2929

30-
Cookies.prototype = {
31-
get: function(name, opts) {
32-
var sigName = name + ".sig"
33-
, header, match, value, remote, data, index
34-
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys
30+
Cookies.prototype.get = function(name, opts) {
31+
var sigName = name + ".sig"
32+
, header, match, value, remote, data, index
33+
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys
3534

36-
header = this.request.headers["cookie"]
37-
if (!header) return
35+
header = this.request.headers["cookie"]
36+
if (!header) return
3837

39-
match = header.match(getPattern(name))
40-
if (!match) return
38+
match = header.match(getPattern(name))
39+
if (!match) return
4140

42-
value = match[1]
43-
if (!opts || !signed) return value
41+
value = match[1]
42+
if (!opts || !signed) return value
4443

45-
remote = this.get(sigName)
46-
if (!remote) return
44+
remote = this.get(sigName)
45+
if (!remote) return
4746

48-
data = name + "=" + value
49-
if (!this.keys) throw new Error('.keys required for signed cookies');
50-
index = this.keys.index(data, remote)
51-
52-
if (index < 0) {
53-
this.set(sigName, null, {path: "/", signed: false })
54-
} else {
55-
index && this.set(sigName, this.keys.sign(data), { signed: false })
56-
return value
57-
}
58-
},
59-
60-
set: function(name, value, opts) {
61-
var res = this.response
62-
, req = this.request
63-
, headers = res.getHeader("Set-Cookie") || []
64-
, secure = req.protocol === 'https' || req.connection.encrypted
65-
, cookie = new Cookie(name, value, opts)
66-
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys
67-
68-
if (typeof headers == "string") headers = [headers]
69-
70-
if (!secure && opts && opts.secure) {
71-
throw new Error('Cannot send secure cookie over unencrypted connection')
72-
}
73-
74-
cookie.secure = secure
75-
if (opts && "secure" in opts) cookie.secure = opts.secure
76-
if (opts && "secureProxy" in opts) cookie.secure = opts.secureProxy
77-
headers = pushCookie(headers, cookie)
47+
data = name + "=" + value
48+
if (!this.keys) throw new Error('.keys required for signed cookies');
49+
index = this.keys.index(data, remote)
50+
51+
if (index < 0) {
52+
this.set(sigName, null, {path: "/", signed: false })
53+
} else {
54+
index && this.set(sigName, this.keys.sign(data), { signed: false })
55+
return value
56+
}
57+
};
7858

79-
if (opts && signed) {
80-
if (!this.keys) throw new Error('.keys required for signed cookies');
81-
cookie.value = this.keys.sign(cookie.toString())
82-
cookie.name += ".sig"
83-
headers = pushCookie(headers, cookie)
84-
}
59+
Cookies.prototype.set = function(name, value, opts) {
60+
var res = this.response
61+
, req = this.request
62+
, headers = res.getHeader("Set-Cookie") || []
63+
, secure = req.protocol === 'https' || req.connection.encrypted
64+
, cookie = new Cookie(name, value, opts)
65+
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys
8566

86-
var setHeader = res.set ? http.OutgoingMessage.prototype.setHeader : res.setHeader
87-
setHeader.call(res, 'Set-Cookie', headers)
88-
return this
67+
if (typeof headers == "string") headers = [headers]
68+
69+
if (!secure && opts && opts.secure) {
70+
throw new Error('Cannot send secure cookie over unencrypted connection')
8971
}
90-
}
72+
73+
cookie.secure = secure
74+
if (opts && "secure" in opts) cookie.secure = opts.secure
75+
if (opts && "secureProxy" in opts) cookie.secure = opts.secureProxy
76+
headers = pushCookie(headers, cookie)
77+
78+
if (opts && signed) {
79+
if (!this.keys) throw new Error('.keys required for signed cookies');
80+
cookie.value = this.keys.sign(cookie.toString())
81+
cookie.name += ".sig"
82+
headers = pushCookie(headers, cookie)
83+
}
84+
85+
var setHeader = res.set ? http.OutgoingMessage.prototype.setHeader : res.setHeader
86+
setHeader.call(res, 'Set-Cookie', headers)
87+
return this
88+
};
9189

9290
function Cookie(name, value, attrs) {
9391
if (!fieldContentRegExp.test(name)) {
@@ -116,32 +114,30 @@ function Cookie(name, value, attrs) {
116114
}
117115
}
118116

119-
Cookie.prototype = {
120-
path: "/",
121-
expires: undefined,
122-
domain: undefined,
123-
httpOnly: true,
124-
secure: false,
125-
overwrite: false,
117+
Cookie.prototype.path = "/";
118+
Cookie.prototype.expires = undefined;
119+
Cookie.prototype.domain = undefined;
120+
Cookie.prototype.httpOnly = true;
121+
Cookie.prototype.secure = false;
122+
Cookie.prototype.overwrite = false;
126123

127-
toString: function() {
128-
return this.name + "=" + this.value
129-
},
124+
Cookie.prototype.toString = function() {
125+
return this.name + "=" + this.value
126+
};
130127

131-
toHeader: function() {
132-
var header = this.toString()
128+
Cookie.prototype.toHeader = function() {
129+
var header = this.toString()
133130

134-
if (this.maxAge) this.expires = new Date(Date.now() + this.maxAge);
131+
if (this.maxAge) this.expires = new Date(Date.now() + this.maxAge);
135132

136-
if (this.path ) header += "; path=" + this.path
137-
if (this.expires ) header += "; expires=" + this.expires.toUTCString()
138-
if (this.domain ) header += "; domain=" + this.domain
139-
if (this.secure ) header += "; secure"
140-
if (this.httpOnly ) header += "; httponly"
133+
if (this.path ) header += "; path=" + this.path
134+
if (this.expires ) header += "; expires=" + this.expires.toUTCString()
135+
if (this.domain ) header += "; domain=" + this.domain
136+
if (this.secure ) header += "; secure"
137+
if (this.httpOnly ) header += "; httponly"
141138

142-
return header
143-
}
144-
}
139+
return header
140+
};
145141

146142
// back-compat so maxage mirrors maxAge
147143
Object.defineProperty(Cookie.prototype, 'maxage', {

test/cookie.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ var assert = require('assert')
33
var cookies = require('..')
44

55
describe('new Cookie', function () {
6+
it('should have correct constructor', function () {
7+
var cookie = new cookies.Cookie('foo', 'bar')
8+
assert.equal(cookie.constructor, cookies.Cookie)
9+
})
10+
611
it('should throw on invalid name', function () {
712
assert.throws(function () {
813
new cookies.Cookie('foo\n', 'bar')

test/http.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ describe('HTTP', function () {
1414
var cookies = new Cookies( req, res, keys )
1515
, unsigned, signed, tampered, overwrite
1616

17+
assert.equal( cookies.constructor, Cookies )
18+
1719
if ( req.url == "/set" ) {
1820
cookies
1921
// set a regular cookie

0 commit comments

Comments
 (0)