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

Avoid using arguments #316

Merged
merged 11 commits into from
Nov 3, 2023
4 changes: 0 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
},
"reportUnusedDisableDirectives": true,
"rules": {
// this is only needed because `createPromiseCallback` uses arguments to support the callback-style api
// we should strongly consider dropping the callback and sync api variants (in say v6) to reduce the
// surface area and complexity of tough-cookie
"prefer-rest-params": "warn",
"max-lines": ["warn", 500]
}
}
18 changes: 9 additions & 9 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('CookieJar', () => {
})

describe('setCookie', () => {
let cookie: Cookie
let cookie: Cookie | undefined

apiVariants(
'should resolve to a Cookie',
Expand Down Expand Up @@ -83,8 +83,8 @@ describe('CookieJar', () => {
},
() => {
expect(cookie).toBeInstanceOf(Cookie)
expect(cookie.key).toBe('foo')
expect(cookie.value).toBe('bar')
expect(cookie?.key).toBe('foo')
expect(cookie?.value).toBe('bar')
},
)

Expand Down Expand Up @@ -185,8 +185,8 @@ describe('CookieJar', () => {
lastAccessed: t1,
}),
)
expect(cookie.TTL()).toBe(Infinity)
expect(cookie.isPersistent()).toBe(false)
expect(cookie?.TTL()).toBe(Infinity)
expect(cookie?.isPersistent()).toBe(false)

// updates the last access when retrieving a cookie
jest.advanceTimersByTime(10000)
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('CookieJar', () => {
'a=b; Domain=example.com; Path=/',
'http://www.app.example.com/index.html',
)
expect(cookie.domain).toBe('example.com')
expect(cookie?.domain).toBe('example.com')
})

it('should allow a sub-path cookie on a super-domain', async () => {
Expand Down Expand Up @@ -348,8 +348,8 @@ describe('CookieJar', () => {
lastAccessed: t0,
}),
)
expect(cookie.TTL()).toBe(Infinity)
expect(cookie.isPersistent()).toBe(false)
expect(cookie?.TTL()).toBe(Infinity)
expect(cookie?.isPersistent()).toBe(false)
})
})

Expand Down Expand Up @@ -800,7 +800,7 @@ describe('CookieJar', () => {
})

describe('getSetCookieStrings', () => {
let cookieHeaders: string[]
let cookieHeaders: string[] | undefined

describe('api', () => {
beforeEach(async () => {
Expand Down
2 changes: 2 additions & 0 deletions lib/__tests__/data/parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file just contains test data, so we don't care about the number of lines.
/* eslint-disable max-lines */
export default [
{
test: '0001',
Expand Down
16 changes: 8 additions & 8 deletions lib/__tests__/removeAll.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,23 @@ class StoreWithoutRemoveAll extends Store {
domain: string,
path: string,
key: string,
): Promise<Cookie>
): Promise<Cookie | undefined>
override findCookie(
domain: string,
path: string,
key: string,
callback: Callback<Cookie>,
callback: Callback<Cookie | undefined>,
): void
override findCookie(
_domain: string,
_path: string,
_key: string,
callback?: Callback<Cookie>,
callback?: Callback<Cookie | undefined>,
colincasey marked this conversation as resolved.
Show resolved Hide resolved
): unknown {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(undefined, undefined)
return callback(null, undefined)
wjhsf marked this conversation as resolved.
Show resolved Hide resolved
}

override findCookies(
Expand All @@ -177,7 +177,7 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(undefined, [])
return callback(null, [])
}

override putCookie(cookie: Cookie): Promise<void>
Expand All @@ -188,7 +188,7 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(undefined)
return callback(null)
}

override getAllCookies(): Promise<Cookie[]>
Expand All @@ -198,7 +198,7 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(undefined, this.cookies.slice())
return callback(null, this.cookies.slice())
}

override removeCookie(
Expand All @@ -222,7 +222,7 @@ class StoreWithoutRemoveAll extends Store {
if (!callback) {
throw new Error('This should not be undefined')
}
return callback(undefined)
return callback(null)
}
}

Expand Down
9 changes: 8 additions & 1 deletion lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

// This file was too big before we added max-lines, and it's ongoing work to reduce its size.
/* eslint max-lines: [1, 750] */

import * as pubsuffix from '../pubsuffix-psl'
import * as validators from '../validators'
import { getCustomInspectSymbol } from '../utilHelper'
Expand Down Expand Up @@ -540,7 +543,11 @@ export class Cookie {
) {
return false
}
if (this.maxAge != null && this.maxAge <= 0) {
if (
this.maxAge != null &&
this.maxAge !== 'Infinity' &&
(this.maxAge === '-Infinity' || this.maxAge <= 0)
) {
return false // "Max-Age=" non-zero-digit *DIGIT
}
if (this.path != null && !PATH_VALUE.test(this.path)) {
Expand Down
Loading
Loading