Skip to content
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

Remove workarounds for util in non-node environments #359

Merged
merged 8 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/__tests__/cookieUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { inspect } from 'node:util'
import { Cookie } from '../cookie/cookie'

describe('Cookie utils', () => {
describe('custom inspect', () => {
it('should be a readable string', () => {
const cookie = new Cookie({
key: 'test',
value: 'b',
maxAge: 60,
})
expect(inspect(cookie)).toBe(
'Cookie="test=b; Max-Age=60; hostOnly=?; aAge=?; cAge=0ms"',
)
})
})
})
119 changes: 0 additions & 119 deletions lib/__tests__/nodeUtilFallback.spec.ts

This file was deleted.

14 changes: 1 addition & 13 deletions lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import { getPublicSuffix } from '../getPublicSuffix'
import * as validators from '../validators'
import { getCustomInspectSymbol } from '../utilHelper'
import { inOperator } from '../utils'

import { formatDate } from './formatDate'
Expand Down Expand Up @@ -421,17 +420,6 @@ export class Cookie {
sameSite: string | undefined

constructor(options: CreateCookieOptions = {}) {
// supports inspect if that feature is available in the environment
const customInspectSymbol = getCustomInspectSymbol()
if (customInspectSymbol) {
Object.defineProperty(this, customInspectSymbol, {
value: this.inspect.bind(this),
enumerable: false,
writable: false,
configurable: false,
})
}

Object.assign(this, cookieDefaults, options)
this.creation = options.creation ?? cookieDefaults.creation ?? new Date()

Expand All @@ -444,7 +432,7 @@ export class Cookie {
})
}

inspect() {
[Symbol.for('nodejs.util.inspect.custom')]() {
const now = Date.now()
const hostOnly = this.hostOnly != null ? this.hostOnly.toString() : '?'
const createAge =
Expand Down
108 changes: 1 addition & 107 deletions lib/memstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ import type { Cookie } from './cookie/cookie'
import { pathMatch } from './pathMatch'
import { permuteDomain } from './permuteDomain'
import { Store } from './store'
import { getCustomInspectSymbol, getUtilInspect } from './utilHelper'
import {
Callback,
createPromiseCallback,
inOperator,
ErrorCallback,
} from './utils'
import { Callback, createPromiseCallback, ErrorCallback } from './utils'

export type MemoryCookieStoreIndex = {
[domain: string]: {
Expand All @@ -57,20 +51,6 @@ export class MemoryCookieStore extends Store {
super()
this.synchronous = true
this.idx = Object.create(null) as MemoryCookieStoreIndex
const customInspectSymbol = getCustomInspectSymbol()
if (customInspectSymbol) {
Object.defineProperty(this, customInspectSymbol, {
value: this.inspect.bind(this),
enumerable: false,
writable: false,
configurable: false,
})
}
}

inspect() {
const util = { inspect: getUtilInspect(inspectFallback) }
return `{ idx: ${util.inspect(this.idx, false, 2)} }`
}

override findCookie(
Expand Down Expand Up @@ -310,89 +290,3 @@ export class MemoryCookieStore extends Store {
return promiseCallback.resolve(cookies)
}
}

export function inspectFallback(val: unknown): string {
if (typeof val === 'string') {
return `'${val}'`
}

if (val && typeof val === 'object') {
const domains = Object.keys(val)
if (domains.length === 0) {
return '[Object: null prototype] {}'
}
let result = '[Object: null prototype] {\n'
Object.keys(val).forEach((domain, i) => {
if (inOperator(domain, val)) {
result += formatDomain(domain, val[domain])
if (i < domains.length - 1) {
result += ','
}
result += '\n'
}
})
result += '}'
return result
}

return String(val)
}

function formatDomain(domainName: string, domainValue: unknown) {
if (typeof domainValue === 'string') {
return `'${domainValue}'`
}

if (domainValue && typeof domainValue === 'object') {
const indent = ' '
let result = `${indent}'${domainName}': [Object: null prototype] {\n`
Object.keys(domainValue).forEach((path, i, paths) => {
if (inOperator(path, domainValue)) {
result += formatPath(path, domainValue[path])
if (i < paths.length - 1) {
result += ','
}
result += '\n'
}
})
result += `${indent}}`
return result
}

return String(domainValue)
}

function formatPath(pathName: string, pathValue: unknown) {
if (typeof pathValue === 'string') {
return `'${pathValue}'`
}

if (pathValue && typeof pathValue === 'object') {
const indent = ' '
let result = `${indent}'${pathName}': [Object: null prototype] {\n`
Object.keys(pathValue).forEach((cookieName, i, cookieNames) => {
if (inOperator(cookieName, pathValue)) {
const cookie = pathValue[cookieName]
if (
cookie != null &&
typeof cookie === 'object' &&
inOperator('inspect', cookie) &&
typeof cookie.inspect === 'function'
) {
const inspectedValue: unknown = cookie.inspect()
if (typeof inspectedValue === 'string') {
result += ` ${cookieName}: ${inspectedValue}`
if (i < cookieNames.length - 1) {
result += ','
}
result += '\n'
}
}
}
})
result += `${indent}}`
return result
}

return String(pathValue)
}
70 changes: 0 additions & 70 deletions lib/utilHelper.ts

This file was deleted.

Loading
Loading