From 4a97015fd5b7895bb94120005767632164d0d0d7 Mon Sep 17 00:00:00 2001 From: Will Harney Date: Tue, 19 Mar 2024 14:16:50 -0400 Subject: [PATCH] standardize helpers on returning undefined --- lib/__tests__/domainMatch.spec.ts | 8 ++++---- lib/cookie/canonicalDomain.ts | 4 ++-- lib/cookie/cookie.ts | 14 ++++++++------ lib/cookie/cookieJar.ts | 6 +++--- lib/cookie/domainMatch.ts | 6 +++--- lib/getPublicSuffix.ts | 5 +++-- lib/permuteDomain.ts | 4 ++-- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/__tests__/domainMatch.spec.ts b/lib/__tests__/domainMatch.spec.ts index bfc3afbe..edb00754 100644 --- a/lib/__tests__/domainMatch.spec.ts +++ b/lib/__tests__/domainMatch.spec.ts @@ -13,10 +13,10 @@ describe('domainMatch', () => { ['example.com', 'example.com.', false], // RFC6265 S4.1.2.3 // nulls and undefineds - [null, 'example.com', null], - ['example.com', null, null], - [null, null, null], - [undefined, undefined, null], + [null, 'example.com', undefined], + ['example.com', null, undefined], + [null, null, undefined], + [undefined, undefined, undefined], // suffix matching: ['www.example.com', 'example.com', true], // substr AND suffix diff --git a/lib/cookie/canonicalDomain.ts b/lib/cookie/canonicalDomain.ts index 4ef35daf..c709cbaa 100644 --- a/lib/cookie/canonicalDomain.ts +++ b/lib/cookie/canonicalDomain.ts @@ -3,9 +3,9 @@ import { IP_V6_REGEX_OBJECT } from './constants' import type { Nullable } from '../utils' // S5.1.2 Canonicalized Host Names -export function canonicalDomain(str: Nullable): string | null { +export function canonicalDomain(str: Nullable): string | undefined { if (str == null) { - return null + return undefined } let _str = str.trim().replace(/^\./, '') // S4.1.2.3 & S5.2.3: ignore leading . diff --git a/lib/cookie/cookie.ts b/lib/cookie/cookie.ts index 25ece895..ef5db5e3 100644 --- a/lib/cookie/cookie.ts +++ b/lib/cookie/cookie.ts @@ -280,6 +280,10 @@ function parse(str: string, options?: ParseCookieOptions): Cookie | undefined { return c } +// TBD: `null` is valid JSON, but `undefined` is not. Options: +// 1. Change this to return `undefined` - weird because it's not JSON +// 2. Keep this as `null` - weird because it's a violation of our new convention +// 3. Change *everything* to return `null` - a lot of work, maybe other edge cases that we can't change? function fromJSON(str: unknown): Cookie | null { if (!str || validators.isEmptyString(str)) { return null @@ -532,6 +536,7 @@ export class Cookie { return obj } + // TBD: This is a wrapper for `fromJSON`, return type depends on decision there clone(): Cookie | null { return fromJSON(this.toJSON()) } @@ -697,15 +702,12 @@ export class Cookie { } // Mostly S5.1.2 and S5.2.3: - canonicalizedDomain(): string | null { - if (this.domain == null) { - return null - } + canonicalizedDomain(): string | undefined { return canonicalDomain(this.domain) } - cdomain(): string | null { - return this.canonicalizedDomain() + cdomain(): string | undefined { + return canonicalDomain(this.domain) } static parse = parse diff --git a/lib/cookie/cookieJar.ts b/lib/cookie/cookieJar.ts index f638d28f..dd21a53a 100644 --- a/lib/cookie/cookieJar.ts +++ b/lib/cookie/cookieJar.ts @@ -85,13 +85,13 @@ function getCookieContext(url: unknown): URL | urlParse { } type SameSiteLevel = keyof (typeof Cookie)['sameSiteLevel'] -function checkSameSiteContext(value: string): SameSiteLevel | null { +function checkSameSiteContext(value: string): SameSiteLevel | undefined { validators.validate(validators.isNonEmptyString(value), value) const context = String(value).toLowerCase() if (context === 'none' || context === 'lax' || context === 'strict') { return context } else { - return null + return undefined } } @@ -268,7 +268,7 @@ export class CookieJar { return promiseCallback.resolve(undefined) } - const host = canonicalDomain(context.hostname) + const host = canonicalDomain(context.hostname) ?? null const loose = options?.loose || this.enableLooseMode let sameSiteContext = null diff --git a/lib/cookie/domainMatch.ts b/lib/cookie/domainMatch.ts index 91cfcfbe..caaea90a 100644 --- a/lib/cookie/domainMatch.ts +++ b/lib/cookie/domainMatch.ts @@ -13,9 +13,9 @@ export function domainMatch( str?: Nullable, domStr?: Nullable, canonicalize?: boolean, -): boolean | null { +): boolean | undefined { if (str == null || domStr == null) { - return null + return undefined } let _str: Nullable @@ -30,7 +30,7 @@ export function domainMatch( } if (_str == null || _domStr == null) { - return null + return undefined } /* diff --git a/lib/getPublicSuffix.ts b/lib/getPublicSuffix.ts index 7e7967cf..63ca2c88 100644 --- a/lib/getPublicSuffix.ts +++ b/lib/getPublicSuffix.ts @@ -49,7 +49,7 @@ const defaultGetPublicSuffixOptions: GetPublicSuffixOptions = { export function getPublicSuffix( domain: string, options: GetPublicSuffixOptions = {}, -): string | null { +): string | undefined { options = { ...defaultGetPublicSuffixOptions, ...options } const domainParts = domain.split('.') const topLevelDomain = domainParts[domainParts.length - 1] @@ -84,8 +84,9 @@ export function getPublicSuffix( ) } - return getDomain(domain, { + const publicSuffix = getDomain(domain, { allowIcannDomains: true, allowPrivateDomains: true, }) + if (publicSuffix) return publicSuffix } diff --git a/lib/permuteDomain.ts b/lib/permuteDomain.ts index 195d7267..26d3780c 100644 --- a/lib/permuteDomain.ts +++ b/lib/permuteDomain.ts @@ -37,13 +37,13 @@ import { getPublicSuffix } from './getPublicSuffix' export function permuteDomain( domain: string, allowSpecialUseDomain?: boolean, -): string[] | null { +): string[] | undefined { const pubSuf = getPublicSuffix(domain, { allowSpecialUseDomain: allowSpecialUseDomain, }) if (!pubSuf) { - return null + return undefined } if (pubSuf == domain) { return [domain]