Skip to content

Commit

Permalink
Handle different function signatures for callback argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhsf committed Oct 27, 2023
1 parent c216409 commit 0fec493
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
36 changes: 21 additions & 15 deletions lib/cookie/cookieJar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,13 @@ export class CookieJar {
setCookie(
cookie: string | Cookie,
url: string,
options?: SetCookieOptions | Callback<Cookie>,
options?: SetCookieOptions | Callback<Cookie | undefined>,
callback?: Callback<Cookie | undefined>,
): unknown {
if (typeof options === 'function') {
callback = options
options = undefined
}
const promiseCallback = createPromiseCallback(callback)
const cb = promiseCallback.callback

Expand Down Expand Up @@ -520,16 +524,19 @@ export class CookieJar {
options?: GetCookiesOptions | Callback<Cookie[]>,
callback?: Callback<Cookie[]>,
): unknown {
if (typeof options === 'function') {
callback = options
options = defaultGetCookieOptions
} else if (options === undefined) {
options = defaultGetCookieOptions
}
const promiseCallback = createPromiseCallback(callback)
const cb = promiseCallback.callback

validators.validate(validators.isNonEmptyString(url), cb, url)
const context = getCookieContext(url)
if (typeof options === 'function' || options === undefined) {
options = defaultGetCookieOptions
}
validators.validate(validators.isObject(options), cb, safeToString(options))
validators.validate(typeof cb === 'function', cb)
const context = getCookieContext(url)

const host = canonicalDomain(context.hostname)
const path = context.pathname || '/'
Expand Down Expand Up @@ -678,20 +685,19 @@ export class CookieJar {
options?: GetCookiesOptions | Callback<string | undefined>,
callback?: Callback<string | undefined>,
): unknown {
const promiseCallback = createPromiseCallback(callback)

if (typeof options === 'function') {
callback = options
options = undefined
}

const promiseCallback = createPromiseCallback(callback)
const next: Callback<Cookie[]> = function (
err: Error | null,
cookies: Cookie[] | undefined,
) {
if (err) {
promiseCallback.callback(err)
} else if (cookies === undefined) {
promiseCallback.callback(null, undefined)
promiseCallback.callback(null, cookies)
} else {
promiseCallback.callback(
null,
Expand Down Expand Up @@ -738,13 +744,13 @@ export class CookieJar {
options?: GetCookiesOptions | Callback<string[] | undefined>,
callback?: Callback<string[] | undefined>,
): unknown {
const promiseCallback = createPromiseCallback<string[] | undefined>(
callback,
)

if (typeof options === 'function') {
callback = options
options = undefined
}
const promiseCallback = createPromiseCallback<string[] | undefined>(
callback,
)

const next: Callback<Cookie[]> = function (
err: Error | null,
Expand Down Expand Up @@ -780,7 +786,6 @@ export class CookieJar {

serialize(callback: Callback<SerializedCookieJar>): void
serialize(): Promise<SerializedCookieJar>
serialize(callback?: Callback<SerializedCookieJar>): unknown
serialize(callback?: Callback<SerializedCookieJar>): unknown {
const promiseCallback = createPromiseCallback<SerializedCookieJar>(callback)
const cb = promiseCallback.callback
Expand Down Expand Up @@ -923,6 +928,7 @@ export class CookieJar {
callback?: Callback<CookieJar>,
): unknown {
if (typeof newStore === 'function') {
callback = newStore
newStore = undefined
}

Expand Down Expand Up @@ -961,7 +967,6 @@ export class CookieJar {

removeAllCookies(callback: Callback<void>): void
removeAllCookies(): Promise<void>
removeAllCookies(callback?: Callback<void>): unknown
removeAllCookies(callback?: Callback<void>): unknown {
const promiseCallback = createPromiseCallback<void>(callback)
const cb = promiseCallback.callback
Expand Down Expand Up @@ -1052,6 +1057,7 @@ export class CookieJar {
callback?: Callback<CookieJar>,
): unknown {
if (typeof store === 'function') {
callback = store
store = undefined
}

Expand Down
3 changes: 3 additions & 0 deletions lib/memstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export class MemoryCookieStore extends Store {
callback?: Callback<Cookie[]>,
): unknown {
if (typeof allowSpecialUseDomain === 'function') {
callback = allowSpecialUseDomain
// TODO: It's weird that `allowSpecialUseDomain` defaults to false with no callback,
// but true with a callback. This is legacy behavior from v4.
allowSpecialUseDomain = true
}

Expand Down

0 comments on commit 0fec493

Please sign in to comment.