Skip to content

Commit 529c048

Browse files
committed
wip: look into adding type checking and types
1 parent 8e18762 commit 529c048

File tree

11 files changed

+678
-243
lines changed

11 files changed

+678
-243
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ charset = utf-8
66
insert_final_newline = true
77
trim_trailing_whitespace = true
88

9-
[{*.js,*.json,*.yml}]
9+
[{*.js,*.ts,*.json,*.yml}]
1010
indent_size = 2
1111
indent_style = space

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,24 @@
1919
"uid-safe": "~2.1.5"
2020
},
2121
"devDependencies": {
22+
"@types/cookie": "^0.6.0",
23+
"@types/cookie-signature": "^1.0.7",
24+
"@types/debug": "^4.1.12",
25+
"@types/depd": "^1.1.37",
26+
"@types/express": "^5.0.0",
27+
"@types/node": "^18.19.80",
28+
"@types/on-headers": "~1.0.2",
29+
"@types/parseurl": "~1.3.3",
30+
"@types/uid-safe": "~2.1.5",
2231
"after": "0.8.2",
2332
"cookie-parser": "1.4.6",
2433
"eslint": "8.56.0",
2534
"eslint-plugin-markdown": "3.0.1",
2635
"express": "4.17.3",
2736
"mocha": "10.2.0",
2837
"nyc": "15.1.0",
29-
"supertest": "6.3.4"
38+
"supertest": "6.3.4",
39+
"typescript": "5.8.2"
3040
},
3141
"files": [
3242
"session/",
@@ -38,6 +48,7 @@
3848
},
3949
"scripts": {
4050
"lint": "eslint . && node ./scripts/lint-readme.js",
51+
"check-types": "tsc --project ./tsconfig.json",
4152
"test": "./test/support/gencert.sh && mocha --require test/support/env --check-leaks --no-exit --reporter spec test/",
4253
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
4354
"test-cov": "nyc npm test",

session/cookie-options.d.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
export declare interface CookieOptions {
2+
/**
3+
* Specifies the number (in milliseconds) to use when calculating the `Expires Set-Cookie` attribute.
4+
* This is done by taking the current server time and adding `maxAge` milliseconds to the value to calculate an `Expires` datetime. By default, no maximum age is set.
5+
*
6+
* If both `expires` and `maxAge` are set in the options, then the last one defined in the object is what is used.
7+
* `maxAge` should be preferred over `expires`.
8+
*
9+
* @see expires
10+
*/
11+
maxAge?: number | undefined;
12+
13+
/**
14+
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/)
15+
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not.
16+
* By default, the `Partitioned` attribute is not set.
17+
*
18+
* **Note** This is an attribute that has not yet been fully standardized, and may
19+
* change in the future. This also means many clients may ignore this attribute until
20+
* they understand it.
21+
*/
22+
partitioned?: boolean | undefined;
23+
24+
/**
25+
* Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
26+
*
27+
* - `'low'` will set the `Priority` attribute to `Low`.
28+
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
29+
* - `'high'` will set the `Priority` attribute to `High`.
30+
*
31+
* More information about the different priority levels can be found in
32+
* [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
33+
*
34+
* **Note** This is an attribute that has not yet been fully standardized, and may change in the future.
35+
* This also means many clients may ignore this attribute until they understand it.
36+
*/
37+
priority?: "low" | "medium" | "high" | undefined;
38+
39+
signed?: boolean | undefined;
40+
41+
/**
42+
* Specifies the boolean value for the `HttpOnly Set-Cookie` attribute. When truthy, the `HttpOnly` attribute is set, otherwise it is not.
43+
* By default, the `HttpOnly` attribute is set.
44+
*
45+
* Be careful when setting this to `true`, as compliant clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
46+
*/
47+
httpOnly?: boolean | undefined;
48+
49+
/**
50+
* Specifies the value for the `Path Set-Cookie` attribute.
51+
* By default, this is set to '/', which is the root path of the domain.
52+
*/
53+
path?: string | undefined;
54+
55+
/**
56+
* Specifies the value for the `Domain Set-Cookie` attribute.
57+
* By default, no domain is set, and most clients will consider the cookie to apply to only the current domain.
58+
*/
59+
domain?: string | undefined;
60+
61+
/**
62+
* Specifies the boolean value for the `Secure Set-Cookie` attribute. When truthy, the `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
63+
* Be careful when setting this to true, as compliant clients will not send the cookie back to the server in the future if the browser does not have an HTTPS connection.
64+
*
65+
* Please note that `secure: true` is a **recommended option**.
66+
* However, it requires an https-enabled website, i.e., HTTPS is necessary for secure cookies.
67+
* If `secure` is set, and you access your site over HTTP, **the cookie will not be set**.
68+
*
69+
* The cookie.secure option can also be set to the special value `auto` to have this setting automatically match the determined security of the connection.
70+
* Be careful when using this setting if the site is available both as HTTP and HTTPS, as once the cookie is set on HTTPS, it will no longer be visible over HTTP.
71+
* This is useful when the Express "trust proxy" setting is properly setup to simplify development vs production configuration.
72+
*
73+
* If you have your node.js behind a proxy and are using `secure: true`, you need to set "trust proxy" in express. Please see the [README](https://github.com/expressjs/session) for details.
74+
*
75+
* Please see the [README](https://github.com/expressjs/session) for an example of using secure cookies in production, but allowing for testing in development based on NODE_ENV.
76+
*/
77+
secure?: boolean | "auto" | undefined;
78+
79+
encode?: ((val: string) => string) | undefined;
80+
81+
/**
82+
* Specifies the boolean or string to be the value for the `SameSite Set-Cookie` attribute.
83+
* - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
84+
* - `false` will not set the `SameSite` attribute.
85+
* - `lax` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
86+
* - `none` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
87+
* - `strict` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
88+
*
89+
* More information about the different enforcement levels can be found in the specification.
90+
*
91+
* **Note:** This is an attribute that has not yet been fully standardized, and may change in the future.
92+
* This also means many clients may ignore this attribute until they understand it.
93+
*/
94+
sameSite?: boolean | "lax" | "strict" | "none" | undefined;
95+
}

session/cookie.js

Lines changed: 115 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,146 +7,183 @@
77

88
'use strict';
99

10+
const cookie = require('cookie')
11+
1012
/**
11-
* Module dependencies.
13+
* @import {CookieOptions} from "./cookie-options"
1214
*/
1315

14-
var cookie = require('cookie')
15-
var deprecate = require('depd')('express-session')
16-
1716
/**
18-
* Initialize a new `Cookie` with the given `options`.
19-
*
20-
* @param {IncomingMessage} req
21-
* @param {Object} options
22-
* @api private
17+
* Cookie TODO: add description
18+
* @class
19+
* @implements CookieOptions
2320
*/
2421

25-
var Cookie = module.exports = function Cookie(options) {
26-
this.path = '/';
27-
this.maxAge = null;
28-
this.httpOnly = true;
22+
class Cookie {
23+
/** @type {Date | undefined} @private */
24+
_expires;
25+
/** @type {number | undefined} */
26+
originalMaxAge;
27+
/** @type {boolean | undefined} */
28+
partitioned;
29+
/** @type { "low" | "medium" | "high" | undefined} */
30+
priority;
31+
/** @type {boolean | undefined} */
32+
signed; // FIXME: how this is used??
33+
/** @type {boolean} */
34+
httpOnly;
35+
/** @type {string} */
36+
path;
37+
/** @type {string | undefined} */
38+
domain;
39+
/** @type {boolean | "auto" | undefined} */
40+
secure;
41+
/** @type {((val: string) => string) | undefined} */
42+
encode;
43+
/** @type {boolean | "lax" | "strict" | "none" | undefined} */
44+
sameSite;
2945

30-
if (options) {
31-
if (typeof options !== 'object') {
32-
throw new TypeError('argument options must be a object')
33-
}
34-
35-
for (var key in options) {
36-
if (key !== 'data') {
37-
this[key] = options[key]
46+
/**
47+
* Initialize a new `Cookie` with the given `options`.
48+
* @param {CookieOptions} options
49+
* @private
50+
*/
51+
constructor(options) {
52+
if (options) {
53+
if (typeof options !== 'object') {
54+
throw new TypeError('argument options must be a object')
3855
}
56+
console.log(`CookieOptions: ${JSON.stringify(options)}`)
57+
this.maxAge = options.maxAge
58+
this.originalMaxAge ??= options.maxAge // FIXME: rethink this
59+
60+
this.partitioned = options.partitioned
61+
this.priority = options.priority
62+
this.secure = options.secure
63+
this.httpOnly = options.httpOnly ?? true
64+
this.domain = options.domain
65+
this.path = options.path || '/'
66+
this.sameSite = options.sameSite
67+
68+
this.signed = options.signed // FIXME: how this is used??
69+
this.encode = options.encode // FIXME: is this used / real ??
70+
} else {
71+
this.path = '/'
72+
this.httpOnly = true
3973
}
4074
}
4175

42-
if (this.originalMaxAge === undefined || this.originalMaxAge === null) {
43-
this.originalMaxAge = this.maxAge
76+
/**
77+
* Initialize a new `Cookie` using stored cookie data.
78+
* @param {CookieOptions & {expires?: string, originalMaxAge?: number}} data
79+
* @returns {Cookie}
80+
* @protected
81+
*/
82+
static fromJSON(data) {
83+
console.log(`Cookie.fromJSON: ${JSON.stringify(data)}`)
84+
const { expires, originalMaxAge, ...options } = data
85+
const cookie = new Cookie(options)
86+
cookie.expires = expires ? new Date(expires) : undefined
87+
cookie.originalMaxAge = originalMaxAge
88+
return cookie
4489
}
45-
};
46-
47-
/*!
48-
* Prototype.
49-
*/
50-
51-
Cookie.prototype = {
5290

5391
/**
5492
* Set expires `date`.
5593
*
56-
* @param {Date} date
57-
* @api public
94+
* @param {Date | null | undefined} date
95+
* @public
5896
*/
5997

6098
set expires(date) {
61-
this._expires = date;
62-
this.originalMaxAge = this.maxAge;
63-
},
99+
this._expires = date || undefined
100+
this.originalMaxAge = this.maxAge
101+
}
64102

65103
/**
66-
* Get expires `date`.
104+
* Get expires `Date` object to be the value for the `Expires Set-Cookie` attribute.
105+
* By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting a web browser application.
67106
*
68-
* @return {Date}
69-
* @api public
107+
* @return {Date | undefined}
108+
* @public
70109
*/
71110

72111
get expires() {
73-
return this._expires;
74-
},
112+
return this._expires
113+
}
75114

76115
/**
77116
* Set expires via max-age in `ms`.
78117
*
79-
* @param {Number} ms
80-
* @api public
118+
* @param {number | undefined} ms
119+
* @public
81120
*/
82121

83122
set maxAge(ms) {
84-
if (ms && typeof ms !== 'number' && !(ms instanceof Date)) {
85-
throw new TypeError('maxAge must be a number or Date')
86-
}
87-
88-
if (ms instanceof Date) {
89-
deprecate('maxAge as Date; pass number of milliseconds instead')
123+
if (ms !== undefined) {
124+
if (typeof ms !== 'number') {
125+
throw new TypeError('maxAge must be a number')
126+
}
127+
this.expires = new Date(Date.now() + ms)
128+
} else {
129+
this.expires = undefined
90130
}
91-
92-
this.expires = typeof ms === 'number'
93-
? new Date(Date.now() + ms)
94-
: ms;
95-
},
131+
}
96132

97133
/**
98134
* Get expires max-age in `ms`.
99135
*
100-
* @return {Number}
101-
* @api public
136+
* @return {number | undefined}
137+
* @public
102138
*/
103139

104140
get maxAge() {
105141
return this.expires instanceof Date
106142
? this.expires.valueOf() - Date.now()
107-
: this.expires;
108-
},
143+
: this.expires
144+
}
109145

110146
/**
111147
* Return cookie data object.
112148
*
113-
* @return {Object}
114-
* @api private
149+
* @return {cookie.CookieSerializeOptions}
150+
* @private
115151
*/
116152

117153
get data() {
154+
if (this.secure === 'auto') {
155+
throw new Error("Invalid runtime state, the Cookie.secure == 'auto', which should not be possible.")
156+
}
118157
return {
119-
originalMaxAge: this.originalMaxAge,
120158
partitioned: this.partitioned,
121159
priority: this.priority
122-
, expires: this._expires
160+
, expires: this.expires
123161
, secure: this.secure
124162
, httpOnly: this.httpOnly
125163
, domain: this.domain
126164
, path: this.path
127165
, sameSite: this.sameSite
128166
}
129-
},
130-
131-
/**
132-
* Return a serialized cookie string.
133-
*
134-
* @return {String}
135-
* @api public
136-
*/
137-
138-
serialize: function(name, val){
139-
return cookie.serialize(name, val, this.data);
140-
},
167+
}
141168

142169
/**
143170
* Return JSON representation of this cookie.
144171
*
145-
* @return {Object}
146-
* @api private
172+
* Used by `JSON.stringify`
173+
*
174+
* @returns {Object}
175+
* @protected
147176
*/
148177

149-
toJSON: function(){
150-
return this.data;
178+
toJSON() {
179+
const data = {
180+
...this.data,
181+
expires: this.expires,
182+
originalMaxAge: this.originalMaxAge,
183+
}
184+
console.log(`Cookie.toJSON: ${JSON.stringify(data)}`)
185+
return data
151186
}
152-
};
187+
}
188+
189+
module.exports = Cookie

0 commit comments

Comments
 (0)