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 2 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
44 changes: 44 additions & 0 deletions lib/__tests__/memoryCookieStore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import util from 'node:util'
import { Cookie } from '../cookie/cookie'
import { CookieJar } from '../cookie/cookieJar'
import { MemoryCookieStore } from '../memstore'

describe('MemoryCookieStore', () => {
Expand Down Expand Up @@ -27,4 +29,46 @@ describe('MemoryCookieStore', () => {
expect(memoryCookieStore.removeAllCookies()).toBeInstanceOf(Promise)
expect(memoryCookieStore.getAllCookies()).toBeInstanceOf(Promise)
})

describe('custom inspect matches util.inspect', () => {
let memoryStore: MemoryCookieStore
let cookieJar: CookieJar

beforeEach(() => {
memoryStore = new MemoryCookieStore()
cookieJar = new CookieJar(memoryStore)
})

it('for empty store', () => {
expect(memoryStore.inspect()).toEqual(util.inspect(memoryStore))
})

it('for store with single cookie', async () => {
await cookieJar.setCookie(
'a=1; Domain=example.com; Path=/',
'http://example.com/index.html',
)
expect(memoryStore.inspect()).toEqual(util.inspect(memoryStore))
})

it('for store with multiple cookies', async () => {
const url = 'http://example.com/index.html'
await cookieJar.setCookie('a=0; Domain=example.com; Path=/', url)
await cookieJar.setCookie('b=1; Domain=example.com; Path=/', url)
await cookieJar.setCookie('c=2; Domain=example.com; Path=/', url)
await cookieJar.setCookie(
'd=3; Domain=example.com; Path=/some-path/',
url,
)
await cookieJar.setCookie(
'e=4; Domain=example.com; Path=/some-path/',
url,
)
await cookieJar.setCookie(
'f=5; Domain=another.com; Path=/',
'http://another.com/index.html',
)
expect(memoryStore.inspect()).toEqual(util.inspect(memoryStore))
})
})
})
119 changes: 0 additions & 119 deletions lib/__tests__/nodeUtilFallback.spec.ts

This file was deleted.

19 changes: 7 additions & 12 deletions lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@

import * as pubsuffix from '../pubsuffix-psl'
import * as validators from '../validators'
import { getCustomInspectSymbol } from '../utilHelper'
import { inOperator } from '../utils'
import { inOperator, NODEJS_UTIL_INSPECT_CUSTOM } from '../utils'

import { formatDate } from './formatDate'
import { parseDate } from './parseDate'
Expand Down Expand Up @@ -421,16 +420,12 @@ 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.defineProperty(this, NODEJS_UTIL_INSPECT_CUSTOM, {
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 Down
23 changes: 10 additions & 13 deletions lib/memstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ 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,
NODEJS_UTIL_INSPECT_CUSTOM,
} from './utils'

export type MemoryCookieStoreIndex = {
Expand All @@ -57,20 +57,16 @@ 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,
})
}
Object.defineProperty(this, NODEJS_UTIL_INSPECT_CUSTOM, {
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)} }`
return `{ idx: ${inspect(this.idx)} }`
}

override findCookie(
Expand Down Expand Up @@ -311,7 +307,8 @@ export class MemoryCookieStore extends Store {
}
}

export function inspectFallback(val: unknown): string {
// Approximates node's util.inspect, so that we can still inspect in non-node environments
function inspect(val: unknown): string {
if (typeof val === 'string') {
return `'${val}'`
}
Expand Down
70 changes: 0 additions & 70 deletions lib/utilHelper.ts

This file was deleted.

8 changes: 8 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ export function inOperator<K extends string, T extends object>(
): o is T & Record<K, unknown> {
return k in o
}

/**
* Symbol used by node.js for custom inspect output.
* @see https://nodejs.org/docs/latest-v16.x/api/util.html#utilinspectcustom
*/
export const NODEJS_UTIL_INSPECT_CUSTOM = Symbol.for(
'nodejs.util.inspect.custom',
)
Loading