Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 18, 2024
1 parent 88e623a commit dbab6c3
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 104 deletions.
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -66,48 +66,48 @@
"@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",
"create-cert": "^1.0.6",
"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"
},
Expand Down
14 changes: 9 additions & 5 deletions source/as-promise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -168,13 +172,13 @@ export default function asPromise<T>(firstRequest?: Request): CancelableRequest<
makeRequest(0);
}) as CancelableRequest<T>;

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;
};

Expand Down
2 changes: 1 addition & 1 deletion source/core/calculate-retry-delay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {RetryFunction} from './options.js';

type Returns<T extends (...args: any) => unknown, V> = (...args: Parameters<T>) => V;
type Returns<T extends (...arguments_: any) => unknown, V> = (...arguments_: Parameters<T>) => V;

const calculateRetryDelay: Returns<RetryFunction, number> = ({
attemptCount,
Expand Down
8 changes: 3 additions & 5 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,9 +1114,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}
}

if (!request) {
request = options.getRequestFunction();
}
request ||= options.getRequestFunction();

const url = options.url as URL;

Expand All @@ -1130,12 +1128,12 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

// 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;
Expand Down
4 changes: 2 additions & 2 deletions source/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export type Agents = {
export type Headers = Record<string, string | string[] | undefined>;

export type ToughCookieJar = {
getCookieString: ((currentUrl: string, options: Record<string, unknown>, cb: (error: Error | null, cookies: string) => void) => void) // eslint-disable-line @typescript-eslint/ban-types
getCookieString: ((currentUrl: string, options: Record<string, unknown>, 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<string, unknown>, cb: (error: Error | null, cookie: unknown) => void) => void) // eslint-disable-line @typescript-eslint/ban-types
setCookie: ((cookieOrString: unknown, currentUrl: string, options: Record<string, unknown>, 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
};

Expand Down
10 changes: 5 additions & 5 deletions source/core/utils/proxy-events.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {EventEmitter} from 'node:events';

type Fn = (...args: unknown[]) => void;
type Fns = Record<string, Fn>;
type AnyFunction = (...arguments_: unknown[]) => void;
type Functions = Record<string, AnyFunction>;

export default function proxyEvents(from: EventEmitter, to: EventEmitter, events: Readonly<string[]>): () => 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;
Expand Down
12 changes: 6 additions & 6 deletions source/core/utils/unhandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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() {
Expand Down
8 changes: 2 additions & 6 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ const create = (defaults: InstanceDefaults): Got => {
return request;
}

if (!promise) {
promise = asPromise(request);
}
promise ||= asPromise(request);

return promise;
};
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions test/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand All @@ -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});
Expand Down
9 changes: 8 additions & 1 deletion test/helpers/create-http-test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ const createHttpTestServer = async (options: HttpServerOptions = {}): Promise<Ex
if (options.bodyParser !== false) {
server.use(bodyParser.json({limit: '1mb', type: 'application/json', ...options.bodyParser}));
server.use(bodyParser.text({limit: '1mb', type: 'text/plain', ...options.bodyParser}));
server.use(bodyParser.urlencoded({limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true, ...options.bodyParser}));

server.use(bodyParser.urlencoded({
limit: '1mb',
type: 'application/x-www-form-urlencoded',
extended: true,
...options.bodyParser,
}));

server.use(bodyParser.raw({limit: '1mb', type: 'application/octet-stream', ...options.bodyParser}));
}

Expand Down
7 changes: 6 additions & 1 deletion test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import delay from 'delay';
import type {Handler} from 'express';
import Responselike from 'responselike';
import type {Constructor} from 'type-fest';
import got, {RequestError, HTTPError, type Response, type OptionsInit} from '../source/index.js';
import got, {
RequestError,
HTTPError,
type Response,
type OptionsInit,
} from '../source/index.js';
import withServer from './helpers/with-server.js';

const errorString = 'oops';
Expand Down
4 changes: 2 additions & 2 deletions test/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ test('https request with `checkServerIdentity` NOT OK', withHttpsServer(), async

// The built-in `openssl` on macOS does not support negative days.
{
const testFn = process.platform === 'darwin' ? test.skip : test;
testFn('https request with expired certificate', withHttpsServer({days: -1}), async (t, _server, got) => {
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({}),
{
Expand Down
68 changes: 40 additions & 28 deletions test/merge-instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
Expand All @@ -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);

Expand All @@ -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);

Expand Down
Loading

0 comments on commit dbab6c3

Please sign in to comment.