Skip to content

Commit

Permalink
fix: use @bundled-es-modules/tough-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jul 22, 2024
1 parent 7150cbc commit 90711f8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ msw-*.tgz
.husky/_
.env
**/test-results
/test/modules/node/node-esm-tests

# Smoke test temporary files.
/package.json.copy
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,19 @@
"dependencies": {
"@bundled-es-modules/cookie": "^2.0.0",
"@bundled-es-modules/statuses": "^1.0.1",
"@bundled-es-modules/tough-cookie": "^0.1.5",
"@inquirer/confirm": "^3.0.0",
"@mswjs/interceptors": "^0.29.0",
"@open-draft/until": "^2.1.0",
"@types/cookie": "^0.6.0",
"@types/statuses": "^2.0.4",
"@types/tough-cookie": "^4.0.5",
"chalk": "^4.1.2",
"graphql": "^16.8.1",
"headers-polyfill": "^4.0.2",
"is-node-process": "^1.2.0",
"outvariant": "^1.4.2",
"path-to-regexp": "^6.2.0",
"strict-event-emitter": "^0.5.1",
"tough-cookie": "^4.1.4",
"type-fest": "^4.9.0",
"yargs": "^17.7.2"
},
Expand Down
16 changes: 10 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 22 additions & 21 deletions src/core/utils/cookieStore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { invariant } from 'outvariant'
import { isNodeProcess } from 'is-node-process'
import {
Cookie,
CookieJar,
Store,
MemoryCookieStore,
domainMatch,
pathMatch,
} from 'tough-cookie'
import toughCookie, {
type Cookie as CookieInstance,
} from '@bundled-es-modules/tough-cookie'

const { Cookie, CookieJar, Store, MemoryCookieStore, domainMatch, pathMatch } =
toughCookie

/**
* Custom cookie store that uses the Web Storage API.
Expand All @@ -34,7 +32,7 @@ class WebStorageCookieStore extends Store {
domain: string,
path: string,
key: string,
callback: (error: Error | null, cookie: Cookie | null) => void,
callback: (error: Error | null, cookie: CookieInstance | null) => void,
): void {
try {
const store = this.getStore()
Expand All @@ -51,7 +49,7 @@ class WebStorageCookieStore extends Store {
domain: string,
path: string,
allowSpecialUseDomain: boolean,
callback: (error: Error | null, cookie: Array<Cookie>) => void,
callback: (error: Error | null, cookie: Array<CookieInstance>) => void,
): void {
if (!domain) {
callback(null, [])
Expand All @@ -72,7 +70,10 @@ class WebStorageCookieStore extends Store {
}
}

putCookie(cookie: Cookie, callback: (error: Error | null) => void): void {
putCookie(
cookie: CookieInstance,
callback: (error: Error | null) => void,
): void {
try {
const store = this.getStore()
store.push(cookie)
Expand All @@ -85,8 +86,8 @@ class WebStorageCookieStore extends Store {
}

updateCookie(
oldCookie: Cookie,
newCookie: Cookie,
oldCookie: CookieInstance,
newCookie: CookieInstance,
callback: (error: Error | null) => void,
): void {
this.putCookie(newCookie, callback)
Expand Down Expand Up @@ -128,7 +129,7 @@ class WebStorageCookieStore extends Store {
}

getAllCookies(
callback: (error: Error | null, cookie: Array<Cookie>) => void,
callback: (error: Error | null, cookie: Array<CookieInstance>) => void,
): void {
try {
callback(null, this.getStore())
Expand All @@ -139,7 +140,7 @@ class WebStorageCookieStore extends Store {
}
}

private getStore(): Array<Cookie> {
private getStore(): Array<CookieInstance> {
try {
const json = this.storage.getItem(this.storageKey)

Expand All @@ -148,7 +149,7 @@ class WebStorageCookieStore extends Store {
}

const rawCookies = JSON.parse(json) as Array<Record<string, any>>
const cookies: Array<Cookie> = []
const cookies: Array<CookieInstance> = []
for (const rawCookie of rawCookies) {
const cookie = Cookie.fromJSON(rawCookie)
if (cookie != null) {
Expand All @@ -161,18 +162,18 @@ class WebStorageCookieStore extends Store {
}
}

private updateStore(nextStore: Array<Cookie>) {
private updateStore(nextStore: Array<CookieInstance>) {
this.storage.setItem(
this.storageKey,
JSON.stringify(nextStore.map((cookie) => cookie.toJSON())),
)
}

private filterCookiesFromList(
cookies: Array<Cookie>,
cookies: Array<CookieInstance>,
matches: { domain?: string; path?: string; key?: string },
): Array<Cookie> {
const result: Array<Cookie> = []
): Array<CookieInstance> {
const result: Array<CookieInstance> = []

for (const cookie of cookies) {
if (matches.domain && !domainMatch(matches.domain, cookie.domain || '')) {
Expand All @@ -196,7 +197,7 @@ class WebStorageCookieStore extends Store {
}

private deleteCookiesFromList(
cookies: Array<Cookie>,
cookies: Array<CookieInstance>,
matches: { domain?: string; path?: string; key?: string },
) {
const matchingCookies = this.filterCookiesFromList(cookies, matches)
Expand Down

0 comments on commit 90711f8

Please sign in to comment.