-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clean up usage of null/undefined #377
Changes from all commits
6056bbc
f5608fd
030e176
09d2433
73352da
964a495
aecf15f
06cf3ee
2530b43
9bf5e80
f94dcc1
443931c
e57831f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,9 +112,19 @@ type ParseCookieOptions = { | |
loose?: boolean | undefined | ||
} | ||
|
||
/** | ||
* Parses a string into a Cookie object. | ||
* @param str Cookie string to parse | ||
* @returns `Cookie` object for valid string inputs, `undefined` for invalid string inputs, | ||
* or `null` for non-string inputs or empty string | ||
*/ | ||
function parse( | ||
str: string, | ||
options?: ParseCookieOptions, | ||
// TBD: Should we change the API to have a single "invalid input" return type? I think `undefined` | ||
// would be more consistent with the rest of the code, and it would be of minimal impact. Only | ||
// users who are passing an invalid input and doing an explicit null check would be broken, and | ||
// that doesn't seem like it would be a significant number of users. | ||
): Cookie | undefined | null { | ||
if (validators.isEmptyString(str) || !validators.isString(str)) { | ||
return null | ||
|
@@ -365,6 +375,17 @@ function fromJSON(str: unknown) { | |
return c | ||
} | ||
|
||
type CreateCookieOptions = Omit< | ||
{ | ||
// Assume that all non-method attributes on the class can be configured, except creationIndex. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[K in keyof Cookie as Cookie[K] extends (...args: any[]) => any | ||
? never | ||
: K]?: Cookie[K] | ||
}, | ||
'creationIndex' | ||
> | ||
|
||
const cookieDefaults = { | ||
// the order in which the RFC has them: | ||
key: '', | ||
|
@@ -382,46 +403,42 @@ const cookieDefaults = { | |
creation: null, | ||
lastAccessed: null, | ||
sameSite: undefined, | ||
} | ||
|
||
type CreateCookieOptions = { | ||
key?: string | ||
value?: string | ||
expires?: Date | 'Infinity' | null | ||
maxAge?: number | 'Infinity' | '-Infinity' | ||
domain?: string | null | ||
path?: string | null | ||
secure?: boolean | ||
httpOnly?: boolean | ||
extensions?: string[] | null | ||
creation?: Date | 'Infinity' | null | ||
creationIndex?: number | ||
hostOnly?: boolean | null | ||
pathIsDefault?: boolean | null | ||
lastAccessed?: Date | 'Infinity' | null | ||
sameSite?: string | undefined | ||
} | ||
} as const satisfies Required<CreateCookieOptions> | ||
|
||
export class Cookie { | ||
key: string | undefined | ||
value: string | undefined | ||
expires: Date | 'Infinity' | null | undefined | ||
maxAge: number | 'Infinity' | '-Infinity' | undefined | ||
domain: string | null | undefined | ||
path: string | null | undefined | ||
secure: boolean | undefined | ||
httpOnly: boolean | undefined | ||
extensions: string[] | null | undefined | ||
key: string | ||
value: string | ||
expires: Date | 'Infinity' | null | ||
maxAge: number | 'Infinity' | '-Infinity' | null | ||
domain: string | null | ||
path: string | null | ||
secure: boolean | ||
httpOnly: boolean | ||
extensions: string[] | null | ||
creation: Date | 'Infinity' | null | ||
creationIndex: number | undefined | ||
hostOnly: boolean | null | undefined | ||
pathIsDefault: boolean | null | undefined | ||
lastAccessed: Date | 'Infinity' | null | undefined | ||
creationIndex: number | ||
hostOnly: boolean | null | ||
pathIsDefault: boolean | null | ||
lastAccessed: Date | 'Infinity' | null | ||
sameSite: string | undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
constructor(options: CreateCookieOptions = {}) { | ||
Object.assign(this, cookieDefaults, options) | ||
this.creation = options.creation ?? cookieDefaults.creation ?? new Date() | ||
this.key = options.key ?? cookieDefaults.key | ||
this.value = options.value ?? cookieDefaults.value | ||
this.expires = options.expires ?? cookieDefaults.expires | ||
this.maxAge = options.maxAge ?? cookieDefaults.maxAge | ||
this.domain = options.domain ?? cookieDefaults.domain | ||
this.path = options.path ?? cookieDefaults.path | ||
this.secure = options.secure ?? cookieDefaults.secure | ||
this.httpOnly = options.httpOnly ?? cookieDefaults.httpOnly | ||
this.extensions = options.extensions ?? cookieDefaults.extensions | ||
this.creation = options.creation ?? cookieDefaults.creation | ||
this.hostOnly = options.hostOnly ?? cookieDefaults.hostOnly | ||
this.pathIsDefault = options.pathIsDefault ?? cookieDefaults.pathIsDefault | ||
this.lastAccessed = options.lastAccessed ?? cookieDefaults.lastAccessed | ||
this.sameSite = options.sameSite ?? cookieDefaults.sameSite | ||
Comment on lines
+426
to
+439
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypeScript complains if we use |
||
|
||
this.creation = options.creation ?? new Date() | ||
|
||
// used to break creation ties in cookieCompare(): | ||
Object.defineProperty(this, 'creationIndex', { | ||
|
@@ -430,6 +447,8 @@ export class Cookie { | |
writable: true, | ||
value: ++Cookie.cookiesCreated, | ||
}) | ||
// Duplicate operation, but it makes TypeScript happy... | ||
this.creationIndex = Cookie.cookiesCreated | ||
} | ||
|
||
[Symbol.for('nodejs.util.inspect.custom')]() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import type { Nullable } from '../utils' | ||
import { canonicalDomain } from './canonicalDomain' | ||
|
||
// Dumped from [email protected], with the following changes: | ||
|
@@ -9,16 +10,16 @@ const IP_REGEX_LOWERCASE = | |
|
||
// S5.1.3 Domain Matching | ||
export function domainMatch( | ||
str?: string | null, | ||
domStr?: string | null, | ||
str?: Nullable<string>, | ||
domStr?: Nullable<string>, | ||
canonicalize?: boolean, | ||
): boolean | null { | ||
if (str == null || domStr == null) { | ||
return null | ||
} | ||
|
||
let _str: string | null | ||
let _domStr: string | null | ||
let _str: Nullable<string> | ||
let _domStr: Nullable<string> | ||
|
||
if (canonicalize !== false) { | ||
_str = canonicalDomain(str) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these props seem to have
| undefined
, but not actually need it, because none of thecookieDefaults
areundefined
.