Skip to content

Commit

Permalink
Use explicit return type in all files.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhsf committed Mar 15, 2024
1 parent 3ee553c commit e2c445b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/cookie/canonicalDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as punycode from 'punycode/punycode.js'
import { IP_V6_REGEX_OBJECT } from './constants'

// S5.1.2 Canonicalized Host Names
export function canonicalDomain(str: string | null) {
export function canonicalDomain(str: string | null): string | null {
if (str == null) {
return null
}
Expand Down
27 changes: 15 additions & 12 deletions lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const CONTROL_CHARS = /[\x00-\x1F]/
// https://github.com/ChromiumWebApps/chromium/blob/b3d3b4da8bb94c1b2e061600df106d590fda3620/net/cookies/parsed_cookie.cc#L60
const TERMINATORS = ['\n', '\r', '\0']

function trimTerminator(str: string) {
function trimTerminator(str: string): string {
if (validators.isEmptyString(str)) return str
for (let t = 0; t < TERMINATORS.length; t++) {
const terminator = TERMINATORS[t]
Expand All @@ -70,7 +70,10 @@ function trimTerminator(str: string) {
return str
}

function parseCookiePair(cookiePair: string, looseMode: boolean) {
function parseCookiePair(
cookiePair: string,
looseMode: boolean,
): Cookie | undefined {
cookiePair = trimTerminator(cookiePair)
validators.validate(validators.isString(cookiePair), cookiePair)

Expand Down Expand Up @@ -273,7 +276,7 @@ function parse(
return c
}

function fromJSON(str: unknown) {
function fromJSON(str: unknown): Cookie | null {
if (!str || validators.isEmptyString(str)) {
return null
}
Expand Down Expand Up @@ -441,7 +444,7 @@ export class Cookie {
this.creationIndex = Cookie.cookiesCreated
}

[Symbol.for('nodejs.util.inspect.custom')]() {
[Symbol.for('nodejs.util.inspect.custom')](): string {
const now = Date.now()
const hostOnly = this.hostOnly != null ? this.hostOnly.toString() : '?'
const createAge =
Expand Down Expand Up @@ -525,11 +528,11 @@ export class Cookie {
return obj
}

clone() {
clone(): Cookie | null {
return fromJSON(this.toJSON())
}

validate() {
validate(): boolean {
if (this.value == null || !COOKIE_OCTETS.test(this.value)) {
return false
}
Expand Down Expand Up @@ -565,15 +568,15 @@ export class Cookie {
return true
}

setExpires(exp: string | Date) {
setExpires(exp: string | Date): void {
if (exp instanceof Date) {
this.expires = exp
} else {
this.expires = parseDate(exp) || 'Infinity'
}
}

setMaxAge(age: number) {
setMaxAge(age: number): void {
if (age === Infinity) {
this.maxAge = 'Infinity'
} else if (age === -Infinity) {
Expand All @@ -583,7 +586,7 @@ export class Cookie {
}
}

cookieString() {
cookieString(): string {
const val = this.value ?? ''
if (this.key) {
return `${this.key}=${val}`
Expand All @@ -592,7 +595,7 @@ export class Cookie {
}

// gives Set-Cookie header format
toString() {
toString(): string {
let str = this.cookieString()

if (this.expires != 'Infinity') {
Expand Down Expand Up @@ -690,14 +693,14 @@ export class Cookie {
}

// Mostly S5.1.2 and S5.2.3:
canonicalizedDomain() {
canonicalizedDomain(): string | null {
if (this.domain == null) {
return null
}
return canonicalDomain(this.domain)
}

cdomain() {
cdomain(): string | null {
return this.canonicalizedDomain()
}

Expand Down

0 comments on commit e2c445b

Please sign in to comment.