Skip to content

Commit a5dd9aa

Browse files
committed
General code improvements
1 parent 732e9bd commit a5dd9aa

File tree

5 files changed

+28
-72
lines changed

5 files changed

+28
-72
lines changed

source/core/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ import {
3232
import type {PlainResponse} from './response.js';
3333
import type {PromiseCookieJar, NativeRequestOptions, RetryOptions} from './options.js';
3434

35+
type Error = NodeJS.ErrnoException;
36+
3537
export interface Progress {
3638
percent: number;
3739
transferred: number;
3840
total?: number;
3941
}
4042

41-
type Error = NodeJS.ErrnoException;
42-
4343
const supportsBrotli = is.string(process.versions.brotli);
4444

4545
const methodsWithoutBody: ReadonlySet<string> = new Set(['GET', 'HEAD']);
@@ -125,7 +125,7 @@ const proxiedRequestEvents = [
125125
'continue',
126126
'information',
127127
'upgrade',
128-
];
128+
] as const;
129129

130130
const noop = () => {};
131131

source/core/options.ts

+19-61
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ export interface RetryOptions {
299299
maxRetryAfter?: number;
300300
}
301301

302-
export type CreateConnectionFunction = (options: NativeRequestOptions, oncreate: (error: Error, socket: Socket) => void) => Socket;
303-
export type CheckServerIdentityFunction = (hostname: string, certificate: DetailedPeerCertificate) => Error | void;
302+
export type CreateConnectionFunction = (options: NativeRequestOptions, oncreate: (error: NodeJS.ErrnoException, socket: Socket) => void) => Socket;
303+
export type CheckServerIdentityFunction = (hostname: string, certificate: DetailedPeerCertificate) => NodeJS.ErrnoException | void;
304304

305305
export interface CacheOptions {
306306
shared?: boolean;
@@ -768,7 +768,7 @@ export default class Options {
768768
private _merging: boolean;
769769
private readonly _init: OptionsInit[];
770770

771-
constructor(input?: string | URL | OptionsInit, options?: OptionsInit, defaults?: Options | OptionsInit) {
771+
constructor(input?: string | URL | OptionsInit, options?: OptionsInit, defaults?: Options) {
772772
assert.any([is.string, is.urlInstance, is.object, is.undefined], input);
773773
assert.any([is.object, is.undefined], options);
774774
assert.any([is.object, is.undefined], defaults);
@@ -777,8 +777,8 @@ export default class Options {
777777
throw new TypeError('The defaults must be passed as the third argument');
778778
}
779779

780-
this._internals = cloneInternals((defaults as Options)?._internals ?? defaults ?? defaultInternals);
781-
this._init = [...((defaults as Options)?._init ?? [])];
780+
this._internals = cloneInternals(defaults?._internals ?? defaults ?? defaultInternals);
781+
this._init = [...(defaults?._init ?? [])];
782782
this._merging = false;
783783
this._unixOptions = undefined;
784784

@@ -1495,24 +1495,30 @@ export default class Options {
14951495
throw new Error(`Unexpected hook event: ${knownHookEvent}`);
14961496
}
14971497

1498-
const hooks: unknown[] = value[knownHookEvent as keyof Hooks];
1498+
const typedKnownHookEvent = knownHookEvent as keyof Hooks;
1499+
const typedValue = value as Hooks;
1500+
const hooks = typedValue[typedKnownHookEvent];
1501+
14991502
assert.any([is.array, is.undefined], hooks);
15001503

1501-
for (const hook of hooks) {
1502-
assert.function_(hook);
1504+
if (hooks) {
1505+
for (const hook of hooks) {
1506+
assert.function_(hook);
1507+
}
15031508
}
15041509

15051510
if (this._merging) {
15061511
if (hooks) {
15071512
// @ts-expect-error FIXME
1508-
this._internals.hooks[knownHookEvent].push(...hooks);
1513+
this._internals.hooks[typedKnownHookEvent].push(...hooks);
15091514
}
1510-
} else if (hooks) {
1511-
// @ts-expect-error FIXME
1512-
this._internals.hooks[knownHookEvent] = [...hooks];
15131515
} else {
1516+
if (!hooks) {
1517+
throw new Error(`Missing hook event: ${knownHookEvent}`);
1518+
}
1519+
15141520
// @ts-expect-error FIXME
1515-
this._internals.hooks[knownHookEvent] = [];
1521+
this._internals.hooks[knownHookEvent] = [...hooks];
15161522
}
15171523
}
15181524
}
@@ -2257,51 +2263,3 @@ export default class Options {
22572263
Object.freeze(options.context);
22582264
}
22592265
}
2260-
2261-
// It's user responsibility to make sensitive data in `context` non-enumerable
2262-
const nonEnumerableProperties = new Set([
2263-
// Functions
2264-
'constructor',
2265-
'merge',
2266-
'tryMerge',
2267-
'createNativeRequestOptions',
2268-
'getRequestFunction',
2269-
'getFallbackRequestFunction',
2270-
'freeze',
2271-
2272-
// Payload
2273-
'body',
2274-
'form',
2275-
'json',
2276-
2277-
// Getters that always throw
2278-
'auth',
2279-
'followRedirects',
2280-
'searchParameters',
2281-
2282-
// May contain sensitive data
2283-
'username',
2284-
'password',
2285-
'headers',
2286-
'searchParams',
2287-
'url',
2288-
2289-
// Privates
2290-
'_unixOptions',
2291-
'_internals',
2292-
'_merging',
2293-
'_init',
2294-
]);
2295-
2296-
// We want all the properties to be enumerable, so people instead doing
2297-
// `util.inspect(options, {getters: true, showHidden: true})`
2298-
// can do just `util.inspect(options, {getters: true})`.
2299-
const propertyDescriptors: PropertyDescriptorMap = {};
2300-
const keys = Object.getOwnPropertyNames(Options.prototype).filter(property => !nonEnumerableProperties.has(property));
2301-
const makeEnumerable = {enumerable: true};
2302-
2303-
for (const key of keys) {
2304-
propertyDescriptors[key] = makeEnumerable;
2305-
}
2306-
2307-
Object.defineProperties(Options.prototype, propertyDescriptors);

source/core/utils/proxy-events.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {EventEmitter} from 'events';
33
type Fn = (...args: unknown[]) => void;
44
type Fns = Record<string, Fn>;
55

6-
export default function proxyEvents(from: EventEmitter, to: EventEmitter, events: string[]): () => void {
6+
export default function proxyEvents(from: EventEmitter, to: EventEmitter, events: Readonly<string[]>): () => void {
77
const eventFunctions: Fns = {};
88

99
for (const event of events) {

source/create.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ const delay = async (ms: number) => new Promise(resolve => {
2323
setTimeout(resolve, ms);
2424
});
2525

26-
const isGotInstance = (value: Got | ExtendOptions): value is Got => (
27-
'defaults' in value && 'options' in value.defaults
28-
);
26+
const isGotInstance = (value: Got | ExtendOptions): value is Got => is.function_(value);
2927

3028
const aliases: readonly HTTPAlias[] = [
3129
'get',
@@ -50,7 +48,7 @@ const create = (defaults: InstanceDefaults): Got => {
5048
});
5149

5250
// Got interface
53-
const got: Got = ((url: string | URL | OptionsInit | undefined, options?: OptionsInit, defaultOptions: Options = defaults.options as Options): GotReturn => {
51+
const got: Got = ((url: string | URL | OptionsInit | undefined, options?: OptionsInit, defaultOptions: Options = defaults.options): GotReturn => {
5452
const request = new Request(url, options, defaultOptions);
5553
let promise: CancelableRequest | undefined;
5654

@@ -240,7 +238,7 @@ const create = (defaults: InstanceDefaults): Got => {
240238

241239
if (!defaults.mutableDefaults) {
242240
Object.freeze(defaults.handlers);
243-
(defaults.options as Options).freeze();
241+
defaults.options.freeze();
244242
}
245243

246244
Object.defineProperty(got, 'defaults', {

source/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {Response} from './core/response.js';
44
// eslint-disable-next-line import/no-duplicates
55
import type Options from './core/options.js';
66
// eslint-disable-next-line import/no-duplicates
7-
import type {PaginationOptions, OptionsInit, InternalsType} from './core/options.js';
7+
import type {PaginationOptions, OptionsInit} from './core/options.js';
88
import type Request from './core/index.js';
99

1010
// `type-fest` utilities
@@ -18,7 +18,7 @@ export interface InstanceDefaults {
1818
/**
1919
An object containing the default options of Got.
2020
*/
21-
options: InternalsType | Options;
21+
options: Options;
2222

2323
/**
2424
An array of functions. You execute them directly by calling `got()`.

0 commit comments

Comments
 (0)