From dbab6c383204fd0e204562e5976c00fe652d61a8 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 17 May 2024 20:12:53 +0200 Subject: [PATCH] Update dependencies --- package.json | 28 +++++----- source/as-promise/index.ts | 14 +++-- source/core/calculate-retry-delay.ts | 2 +- source/core/index.ts | 8 ++- source/core/options.ts | 4 +- source/core/utils/proxy-events.ts | 10 ++-- source/core/utils/unhandle.ts | 12 ++--- source/create.ts | 8 +-- test/create.ts | 10 ++-- test/helpers/create-http-test-server.ts | 9 +++- test/hooks.ts | 7 ++- test/https.ts | 4 +- test/merge-instances.ts | 68 +++++++++++++++---------- test/post.ts | 8 ++- test/redirects.ts | 10 ++-- test/retry.ts | 16 +++--- test/stream.ts | 10 ++-- test/timeout.ts | 8 +-- test/types/pem.ts | 8 ++- 19 files changed, 140 insertions(+), 104 deletions(-) diff --git a/package.json b/package.json index c07dc74d0..0f81dddcb 100644 --- a/package.json +++ b/package.json @@ -48,10 +48,10 @@ "ky" ], "dependencies": { - "@sindresorhus/is": "^6.1.0", + "@sindresorhus/is": "^6.3.1", "@szmarczak/http-timer": "^5.0.1", "cacheable-lookup": "^7.0.0", - "cacheable-request": "^10.2.14", + "cacheable-request": "^12.0.1", "decompress-response": "^6.0.0", "form-data-encoder": "^4.0.2", "get-stream": "^8.0.1", @@ -66,14 +66,14 @@ "@sinonjs/fake-timers": "^11.2.2", "@types/benchmark": "^2.1.5", "@types/express": "^4.17.21", - "@types/node": "^20.10.0", + "@types/node": "^20.12.12", "@types/pem": "^1.14.4", - "@types/readable-stream": "^4.0.9", + "@types/readable-stream": "^4.0.14", "@types/request": "^2.48.12", "@types/sinon": "^17.0.2", "@types/sinonjs__fake-timers": "^8.1.5", "ava": "^5.3.1", - "axios": "^1.6.2", + "axios": "^1.6.8", "benchmark": "^2.1.4", "bluebird": "^3.7.2", "body-parser": "^1.20.2", @@ -81,33 +81,33 @@ "create-test-server": "^3.0.1", "del-cli": "^5.1.0", "delay": "^6.0.0", - "express": "^4.18.2", + "express": "^4.19.2", "form-data": "^4.0.0", "formdata-node": "^6.0.3", - "nock": "^13.4.0", + "nock": "^13.5.4", "node-fetch": "^3.3.2", - "np": "^9.0.0", + "np": "^10.0.5", "nyc": "^15.1.0", - "p-event": "^6.0.0", + "p-event": "^6.0.1", "pem": "^1.14.8", "pify": "^6.1.0", "readable-stream": "^4.4.2", "request": "^2.88.2", - "sinon": "^17.0.1", + "sinon": "^18.0.0", "slow-stream": "0.0.4", "tempy": "^3.1.0", "then-busboy": "^5.2.1", - "tough-cookie": "^4.1.3", - "tsx": "^4.6.0", + "tough-cookie": "^4.1.4", + "tsx": "^4.10.4", "type-fest": "^4.8.2", - "typescript": "^5.3.2", + "typescript": "^5.4.5", "xo": "^0.56.0" }, "ava": { "files": [ "test/*" ], - "timeout": "1m", + "timeout": "10m", "extensions": { "ts": "module" }, diff --git a/source/as-promise/index.ts b/source/as-promise/index.ts index 35d2bff63..34c2d7ba8 100644 --- a/source/as-promise/index.ts +++ b/source/as-promise/index.ts @@ -7,7 +7,11 @@ import { type RequestError, } from '../core/errors.js'; import Request from '../core/index.js'; -import {parseBody, isResponseOk, type Response, ParseError} from '../core/response.js'; +import { + parseBody, + isResponseOk, + type Response, ParseError, +} from '../core/response.js'; import proxyEvents from '../core/utils/proxy-events.js'; import type Options from '../core/options.js'; import {CancelError, type CancelableRequest} from './types.js'; @@ -168,13 +172,13 @@ export default function asPromise(firstRequest?: Request): CancelableRequest< makeRequest(0); }) as CancelableRequest; - promise.on = (event: string, fn: (...args: any[]) => void) => { - emitter.on(event, fn); + promise.on = (event: string, function_: (...arguments_: any[]) => void) => { + emitter.on(event, function_); return promise; }; - promise.off = (event: string, fn: (...args: any[]) => void) => { - emitter.off(event, fn); + promise.off = (event: string, function_: (...arguments_: any[]) => void) => { + emitter.off(event, function_); return promise; }; diff --git a/source/core/calculate-retry-delay.ts b/source/core/calculate-retry-delay.ts index 5f9970c2b..779d0f97f 100644 --- a/source/core/calculate-retry-delay.ts +++ b/source/core/calculate-retry-delay.ts @@ -1,6 +1,6 @@ import type {RetryFunction} from './options.js'; -type Returns unknown, V> = (...args: Parameters) => V; +type Returns unknown, V> = (...arguments_: Parameters) => V; const calculateRetryDelay: Returns = ({ attemptCount, diff --git a/source/core/index.ts b/source/core/index.ts index fc2858bfc..a23ec0ab8 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1114,9 +1114,7 @@ export default class Request extends Duplex implements RequestEvents { } } - if (!request) { - request = options.getRequestFunction(); - } + request ||= options.getRequestFunction(); const url = options.url as URL; @@ -1130,12 +1128,12 @@ export default class Request extends Duplex implements RequestEvents { } // Cache support - const fn = options.cache ? this._createCacheableRequest : request; + const function_ = options.cache ? this._createCacheableRequest : request; try { // We can't do `await fn(...)`, // because stream `error` event can be emitted before `Promise.resolve()`. - let requestOrResponse = fn!(url, this._requestOptions); + let requestOrResponse = function_!(url, this._requestOptions); if (is.promise(requestOrResponse)) { requestOrResponse = await requestOrResponse; diff --git a/source/core/options.ts b/source/core/options.ts index e8b70b409..f3aedef90 100644 --- a/source/core/options.ts +++ b/source/core/options.ts @@ -50,9 +50,9 @@ export type Agents = { export type Headers = Record; export type ToughCookieJar = { - getCookieString: ((currentUrl: string, options: Record, cb: (error: Error | null, cookies: string) => void) => void) // eslint-disable-line @typescript-eslint/ban-types + getCookieString: ((currentUrl: string, options: Record, callback: (error: Error | null, cookies: string) => void) => void) // eslint-disable-line @typescript-eslint/ban-types & ((url: string, callback: (error: Error | null, cookieHeader: string) => void) => void); // eslint-disable-line @typescript-eslint/ban-types - setCookie: ((cookieOrString: unknown, currentUrl: string, options: Record, cb: (error: Error | null, cookie: unknown) => void) => void) // eslint-disable-line @typescript-eslint/ban-types + setCookie: ((cookieOrString: unknown, currentUrl: string, options: Record, callback: (error: Error | null, cookie: unknown) => void) => void) // eslint-disable-line @typescript-eslint/ban-types & ((rawCookie: string, url: string, callback: (error: Error | null, result: unknown) => void) => void); // eslint-disable-line @typescript-eslint/ban-types }; diff --git a/source/core/utils/proxy-events.ts b/source/core/utils/proxy-events.ts index ad185eaf8..a297ca05a 100644 --- a/source/core/utils/proxy-events.ts +++ b/source/core/utils/proxy-events.ts @@ -1,14 +1,14 @@ import type {EventEmitter} from 'node:events'; -type Fn = (...args: unknown[]) => void; -type Fns = Record; +type AnyFunction = (...arguments_: unknown[]) => void; +type Functions = Record; export default function proxyEvents(from: EventEmitter, to: EventEmitter, events: Readonly): () => void { - const eventFunctions: Fns = {}; + const eventFunctions: Functions = {}; for (const event of events) { - const eventFunction = (...args: unknown[]) => { - to.emit(event, ...args); + const eventFunction = (...arguments_: unknown[]) => { + to.emit(event, ...arguments_); }; eventFunctions[event] = eventFunction; diff --git a/source/core/utils/unhandle.ts b/source/core/utils/unhandle.ts index 9837444fa..26a792055 100644 --- a/source/core/utils/unhandle.ts +++ b/source/core/utils/unhandle.ts @@ -2,16 +2,16 @@ import type {EventEmitter} from 'node:events'; type Origin = EventEmitter; type Event = string | symbol; -type Fn = (...args: any[]) => void; +type AnyFunction = (...arguments_: any[]) => void; type Handler = { origin: Origin; event: Event; - fn: Fn; + fn: AnyFunction; }; type Unhandler = { - once: (origin: Origin, event: Event, fn: Fn) => void; + once: (origin: Origin, event: Event, function_: AnyFunction) => void; unhandleAll: () => void; }; @@ -23,9 +23,9 @@ export default function unhandle(): Unhandler { const handlers: Handler[] = []; return { - once(origin: Origin, event: Event, fn: Fn) { - origin.once(event, fn); - handlers.push({origin, event, fn}); + once(origin: Origin, event: Event, function_: AnyFunction) { + origin.once(event, function_); + handlers.push({origin, event, fn: function_}); }, unhandleAll() { diff --git a/source/create.ts b/source/create.ts index 5a706973e..eed99cb2b 100644 --- a/source/create.ts +++ b/source/create.ts @@ -61,9 +61,7 @@ const create = (defaults: InstanceDefaults): Got => { return request; } - if (!promise) { - promise = asPromise(request); - } + promise ||= asPromise(request); return promise; }; @@ -75,9 +73,7 @@ const create = (defaults: InstanceDefaults): Got => { const result = handler(newOptions, iterateHandlers) as GotReturn; if (is.promise(result) && !request.options.isStream) { - if (!promise) { - promise = asPromise(request); - } + promise ||= asPromise(request); if (result !== promise) { const descriptors = Object.getOwnPropertyDescriptors(promise); diff --git a/test/create.ts b/test/create.ts index 8cd2186c1..4f9cab3e0 100644 --- a/test/create.ts +++ b/test/create.ts @@ -196,14 +196,14 @@ test('ability to pass a custom request method', withServer, async (t, server, go let isCalled = false; - const request: RequestFunction = (...args: [ + const request: RequestFunction = (...arguments_: [ string | URL | RequestOptions, (RequestOptions | ((response: IncomingMessage) => void))?, ((response: IncomingMessage) => void)?, ]) => { isCalled = true; // @ts-expect-error Overload error - return httpRequest(...args); + return httpRequest(...arguments_); }; const instance = got.extend({request}); @@ -217,17 +217,17 @@ test('does not include the `request` option in normalized `http` options', withS let isCalled = false; - const request: RequestFunction = (...args: [ + const request: RequestFunction = (...arguments_: [ string | URL | RequestOptions, (RequestOptions | ((response: IncomingMessage) => void))?, ((response: IncomingMessage) => void)?, ]) => { isCalled = true; - t.false(Reflect.has(args[0] as RequestOptions, 'request')); + t.false(Reflect.has(arguments_[0] as RequestOptions, 'request')); // @ts-expect-error Overload error - return httpRequest(...args); + return httpRequest(...arguments_); }; const instance = got.extend({request}); diff --git a/test/helpers/create-http-test-server.ts b/test/helpers/create-http-test-server.ts index 149a6dffc..fb8f179a2 100644 --- a/test/helpers/create-http-test-server.ts +++ b/test/helpers/create-http-test-server.ts @@ -25,7 +25,14 @@ const createHttpTestServer = async (options: HttpServerOptions = {}): Promise { + const testFunction = process.platform === 'darwin' ? test.skip : test; + testFunction('https request with expired certificate', withHttpsServer({days: -1}), async (t, _server, got) => { await t.throwsAsync( got({}), { diff --git a/test/merge-instances.ts b/test/merge-instances.ts index b3aefebb8..9c8a27025 100644 --- a/test/merge-instances.ts +++ b/test/merge-instances.ts @@ -79,20 +79,24 @@ test('merging two groups of merged instances', withServer, async (t, server) => test('hooks are merged', t => { const getBeforeRequestHooks = (instance: Got): BeforeRequestHook[] => instance.defaults.options.hooks.beforeRequest; - const instanceA = got.extend({hooks: { - beforeRequest: [ - options => { - options.headers.dog = 'woof'; - }, - ], - }}); - const instanceB = got.extend({hooks: { - beforeRequest: [ - options => { - options.headers.cat = 'meow'; - }, - ], - }}); + const instanceA = got.extend({ + hooks: { + beforeRequest: [ + options => { + options.headers.dog = 'woof'; + }, + ], + }, + }); + const instanceB = got.extend({ + hooks: { + beforeRequest: [ + options => { + options.headers.cat = 'meow'; + }, + ], + }, + }); const merged = instanceA.extend(instanceB); t.deepEqual(getBeforeRequestHooks(merged), [...getBeforeRequestHooks(instanceA), ...getBeforeRequestHooks(instanceB)]); @@ -118,13 +122,17 @@ test('URL is not polluted', withServer, async (t, server, got) => { }); test('merging instances with HTTPS options', t => { - const instanceA = got.extend({https: { - rejectUnauthorized: true, - certificate: 'FIRST', - }}); - const instanceB = got.extend({https: { - certificate: 'SECOND', - }}); + const instanceA = got.extend({ + https: { + rejectUnauthorized: true, + certificate: 'FIRST', + }, + }); + const instanceB = got.extend({ + https: { + certificate: 'SECOND', + }, + }); const merged = instanceA.extend(instanceB); @@ -133,13 +141,17 @@ test('merging instances with HTTPS options', t => { }); test('merging instances with HTTPS options undefined', t => { - const instanceA = got.extend({https: { - rejectUnauthorized: true, - certificate: 'FIRST', - }}); - const instanceB = got.extend({https: { - certificate: undefined, - }}); + const instanceA = got.extend({ + https: { + rejectUnauthorized: true, + certificate: 'FIRST', + }, + }); + const instanceB = got.extend({ + https: { + certificate: undefined, + }, + }); const merged = instanceA.extend(instanceB); diff --git a/test/post.ts b/test/post.ts index 08707c9e1..425703030 100644 --- a/test/post.ts +++ b/test/post.ts @@ -9,7 +9,13 @@ import test from 'ava'; import delay from 'delay'; import {pEvent} from 'p-event'; import type {Handler} from 'express'; -import {parse, Body, isBodyFile, type BodyEntryPath, type BodyEntryRawValue} from 'then-busboy'; +import { + parse, + Body, + isBodyFile, + type BodyEntryPath, + type BodyEntryRawValue, +} from 'then-busboy'; import {FormData as FormDataNode, Blob, File} from 'formdata-node'; import {fileFromPath} from 'formdata-node/file-from-path'; import getStream from 'get-stream'; diff --git a/test/redirects.ts b/test/redirects.ts index 805182387..c9e7597b1 100644 --- a/test/redirects.ts +++ b/test/redirects.ts @@ -115,10 +115,12 @@ test('followRedirect gets plainResponse and does not follow', withServer, async response.end(); }); - const {statusCode} = await got('temporary', {followRedirect(response) { - t.is(response.headers.location, '/redirect'); - return false; - }}); + const {statusCode} = await got('temporary', { + followRedirect(response) { + t.is(response.headers.location, '/redirect'); + return false; + }, + }); t.is(statusCode, 307); }); diff --git a/test/retry.ts b/test/retry.ts index 9f47b8c00..c615291d2 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -46,14 +46,14 @@ test('works on timeout', withServer, async (t, server, got) => { timeout: { socket: socketTimeout, }, - request(...args: [ + request(...arguments_: [ string | URL | http.RequestOptions, (http.RequestOptions | ((response: http.IncomingMessage) => void))?, ((response: http.IncomingMessage) => void)?, ]) { if (knocks === 1) { // @ts-expect-error Overload error - return http.request(...args); + return http.request(...arguments_); } knocks++; @@ -462,7 +462,7 @@ test('can retry a Got stream', withServer, async (t, server, got) => { const responseStreamPromise = new Promise((resolve, reject) => { let writeStream: PassThroughStream; - const fn = (retryStream?: Request) => { + const function_ = (retryStream?: Request) => { const stream = retryStream ?? got.stream(''); globalRetryCount = stream.retryCount; @@ -476,7 +476,7 @@ test('can retry a Got stream', withServer, async (t, server, got) => { stream.pipe(writeStream); stream.once('retry', (_retryCount, _error, createRetryStream) => { - fn(createRetryStream()); + function_(createRetryStream()); }); stream.once('error', reject); @@ -485,7 +485,7 @@ test('can retry a Got stream', withServer, async (t, server, got) => { }); }; - fn(); + function_(); }); const responseStream = await responseStreamPromise; @@ -504,14 +504,14 @@ test('throws when cannot retry a Got stream', withServer, async (t, server, got) let globalRetryCount = 0; const streamPromise = new Promise((resolve, reject) => { - const fn = (retryStream?: Request) => { + const function_ = (retryStream?: Request) => { const stream = retryStream ?? got.stream(''); globalRetryCount = stream.retryCount; stream.resume(); stream.once('retry', (_retryCount, _error, createRetryStream) => { - fn(createRetryStream()); + function_(createRetryStream()); }); stream.once('data', () => { @@ -522,7 +522,7 @@ test('throws when cannot retry a Got stream', withServer, async (t, server, got) stream.once('end', resolve); }; - fn(); + function_(); }); const error = await t.throwsAsync(streamPromise, { diff --git a/test/stream.ts b/test/stream.ts index ff3bb8c77..f275da893 100644 --- a/test/stream.ts +++ b/test/stream.ts @@ -255,9 +255,9 @@ test('proxies `content-encoding` header when `options.decompress` is false', wit { const nodejsMajorVersion = Number(process.versions.node.split('.')[0]); - const testFn = nodejsMajorVersion < 14 ? test.failing : test; + const testFunction = nodejsMajorVersion < 14 ? test.failing : test; - testFn('destroying got.stream() destroys the request - `request` event', withServer, async (t, server, got) => { + testFunction('destroying got.stream() destroys the request - `request` event', withServer, async (t, server, got) => { server.get('/', defaultHandler); const stream = got.stream(''); @@ -266,7 +266,7 @@ test('proxies `content-encoding` header when `options.decompress` is false', wit t.truthy(request.destroyed); }); - testFn('destroying got.stream() destroys the request - `response` event', withServer, async (t, server, got) => { + testFunction('destroying got.stream() destroys the request - `response` event', withServer, async (t, server, got) => { server.get('/', (_request, response) => { response.write('hello'); }); @@ -489,8 +489,8 @@ if (Number.parseInt(process.versions.node.split('.')[0]!, 10) <= 12) { } // Test only on Linux -const testFn = process.platform === 'linux' ? test : test.skip; -testFn('it sends a body of file with size on stat = 0', withServer, async (t, server, got) => { +const testFunction = process.platform === 'linux' ? test : test.skip; +testFunction('it sends a body of file with size on stat = 0', withServer, async (t, server, got) => { server.post('/', async (request, response) => { response.end(await getStream(request)); }); diff --git a/test/timeout.ts b/test/timeout.ts index 160d86073..b48cdb9a1 100644 --- a/test/timeout.ts +++ b/test/timeout.ts @@ -480,8 +480,8 @@ test.serial('no unhandled timeout errors', withServer, async (t, _server, got) = await t.throwsAsync(got({ retry: {limit: 0}, timeout: {request: 100}, - request(...args) { - const result = http.request(...args); + request(...arguments_) { + const result = http.request(...arguments_); result.once('socket', () => { result.socket?.destroy(); @@ -526,7 +526,7 @@ test.serial('no more timeouts after an error', withServer, async (t, _server, go const {clearTimeout} = global; // @ts-expect-error FIXME - global.setTimeout = (callback, _ms, ...args) => { + global.setTimeout = (callback, _ms, ...arguments_) => { const timeout = { isCleared: false, }; @@ -536,7 +536,7 @@ test.serial('no more timeouts after an error', withServer, async (t, _server, go return; } - callback(...args); + callback(...arguments_); }); return timeout; diff --git a/test/types/pem.ts b/test/types/pem.ts index 58fa3e650..bb2274d75 100644 --- a/test/types/pem.ts +++ b/test/types/pem.ts @@ -1,4 +1,10 @@ -import type {CertificateCreationOptions, CertificateCreationResult, PrivateKeyCreationOptions, CSRCreationOptions, Callback} from 'pem'; +import type { + CertificateCreationOptions, + CertificateCreationResult, + PrivateKeyCreationOptions, + CSRCreationOptions, + Callback, +} from 'pem'; export type CreateCertificate = (options: CertificateCreationOptions, callback: Callback) => void;