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

Upgrade Ava and @sindresorhus/tsconfig #2109

Closed
wants to merge 10 commits into from
14 changes: 5 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,26 @@
},
"devDependencies": {
"@hapi/bourne": "^3.0.0",
"@sindresorhus/tsconfig": "^2.0.0",
"@sindresorhus/tsconfig": "^3.0.1",
"@sinonjs/fake-timers": "^9.1.1",
"@types/benchmark": "^2.1.1",
"@types/express": "^4.17.13",
"@types/node": "^18.0.1",
"@types/node": "^18.7.3",
"@types/pem": "^1.9.6",
"@types/pify": "^5.0.1",
"@types/readable-stream": "^2.3.13",
"@types/request": "^2.48.8",
"@types/sinon": "^10.0.11",
"@types/sinonjs__fake-timers": "^8.1.1",
"@types/tough-cookie": "^4.0.1",
"ava": "^3.15.0",
"ava": "^4.3.1",
"axios": "^0.27.2",
"benchmark": "^2.1.4",
"bluebird": "^3.7.2",
"body-parser": "^1.19.2",
"create-cert": "^1.0.6",
"create-test-server": "^3.0.1",
"del-cli": "^4.0.1",
"del-cli": "^5.0.0",
"delay": "^5.0.0",
"express": "^4.17.3",
"form-data": "^4.0.0",
Expand All @@ -102,18 +102,14 @@
"tough-cookie": "^4.0.0",
"ts-node": "^10.8.2",
"typescript": "^4.7.4",
"xo": "^0.50.0"
"xo": "^0.51.0"
},
"sideEffects": false,
"ava": {
"files": [
"test/*"
],
"timeout": "1m",
"nonSemVerExperiments": {
"nextGenConfig": true,
"configurableModuleFormat": true
},
"extensions": {
"ts": "module"
},
Expand Down
2 changes: 1 addition & 1 deletion source/as-promise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default function asPromise<T>(firstRequest?: Request): CancelableRequest<
const {headers} = globalRequest.options;

if (!globalRequest.writableFinished && !('accept' in headers)) {
headers.accept = 'application/json';
headers['accept'] = 'application/json';
}
}

Expand Down
17 changes: 10 additions & 7 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface Progress {
total?: number;
}

const supportsBrotli = is.string(process.versions.brotli);
const supportsBrotli = is.string(process.versions['brotli']);

const methodsWithoutBody: ReadonlySet<string> = new Set(['GET', 'HEAD']);

Expand Down Expand Up @@ -616,7 +616,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

if (options.responseType === 'json' && !('accept' in options.headers)) {
options.headers.accept = 'application/json';
options.headers['accept'] = 'application/json';
}

this._bodySize = Number(headers['content-length']) || undefined;
Expand Down Expand Up @@ -751,15 +751,18 @@ export default class Request extends Duplex implements RequestEvents<Request> {
// Redirecting to a different site, clear sensitive data.
if (redirectUrl.hostname !== (url as URL).hostname || redirectUrl.port !== (url as URL).port) {
if ('host' in updatedOptions.headers) {
delete updatedOptions.headers.host;
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete updatedOptions.headers['host'];
}

if ('cookie' in updatedOptions.headers) {
delete updatedOptions.headers.cookie;
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete updatedOptions.headers['cookie'];
}

if ('authorization' in updatedOptions.headers) {
delete updatedOptions.headers.authorization;
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete updatedOptions.headers['authorization'];
}

if (updatedOptions.username || updatedOptions.password) {
Expand Down Expand Up @@ -1059,15 +1062,15 @@ export default class Request extends Duplex implements RequestEvents<Request> {

if (username || password) {
const credentials = Buffer.from(`${username}:${password}`).toString('base64');
headers.authorization = `Basic ${credentials}`;
headers['authorization'] = `Basic ${credentials}`;
}

// Set cookies
if (cookieJar) {
const cookieString: string = await cookieJar.getCookieString(options.url!.toString());

if (is.nonEmptyString(cookieString)) {
headers.cookie = cookieString;
headers['cookie'] = cookieString;
}
}

Expand Down
46 changes: 23 additions & 23 deletions source/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,13 +802,13 @@ const defaultInternals: Options['_internals'] = {
return JSON.parse(response.body as string);
},
paginate({response}) {
const rawLinkHeader = response.headers.link;
const rawLinkHeader = response.headers['link'];
if (typeof rawLinkHeader !== 'string' || rawLinkHeader.trim() === '') {
return false;
}

const parsed = parseLinkHeader(rawLinkHeader);
const next = parsed.find(entry => entry.parameters.rel === 'next' || entry.parameters.rel === '"next"');
const next = parsed.find(entry => entry.parameters['rel'] === 'next' || entry.parameters['rel'] === '"next"');

if (next) {
return {
Expand Down Expand Up @@ -2180,10 +2180,10 @@ export default class Options {
set cacheOptions(value: CacheOptions) {
assert.plainObject(value);

assert.any([is.boolean, is.undefined], value.shared);
assert.any([is.number, is.undefined], value.cacheHeuristic);
assert.any([is.number, is.undefined], value.immutableMinTimeToLive);
assert.any([is.boolean, is.undefined], value.ignoreCargoCult);
assert.any([is.boolean, is.undefined], value['shared']);
assert.any([is.number, is.undefined], value['cacheHeuristic']);
assert.any([is.number, is.undefined], value['immutableMinTimeToLive']);
assert.any([is.boolean, is.undefined], value['ignoreCargoCult']);

for (const key in value) {
if (!(key in this._internals.cacheOptions)) {
Expand All @@ -2208,23 +2208,23 @@ export default class Options {
set https(value: HttpsOptions) {
assert.plainObject(value);

assert.any([is.boolean, is.undefined], value.rejectUnauthorized);
assert.any([is.function_, is.undefined], value.checkServerIdentity);
assert.any([is.string, is.object, is.array, is.undefined], value.certificateAuthority);
assert.any([is.string, is.object, is.array, is.undefined], value.key);
assert.any([is.string, is.object, is.array, is.undefined], value.certificate);
assert.any([is.string, is.undefined], value.passphrase);
assert.any([is.string, is.buffer, is.array, is.undefined], value.pfx);
assert.any([is.array, is.undefined], value.alpnProtocols);
assert.any([is.string, is.undefined], value.ciphers);
assert.any([is.string, is.buffer, is.undefined], value.dhparam);
assert.any([is.string, is.undefined], value.signatureAlgorithms);
assert.any([is.string, is.undefined], value.minVersion);
assert.any([is.string, is.undefined], value.maxVersion);
assert.any([is.boolean, is.undefined], value.honorCipherOrder);
assert.any([is.number, is.undefined], value.tlsSessionLifetime);
assert.any([is.string, is.undefined], value.ecdhCurve);
assert.any([is.string, is.buffer, is.array, is.undefined], value.certificateRevocationLists);
assert.any([is.boolean, is.undefined], value['rejectUnauthorized']);
assert.any([is.function_, is.undefined], value['checkServerIdentity']);
assert.any([is.string, is.object, is.array, is.undefined], value['certificateAuthority']);
assert.any([is.string, is.object, is.array, is.undefined], value['key']);
assert.any([is.string, is.object, is.array, is.undefined], value['certificate']);
assert.any([is.string, is.undefined], value['passphrase']);
assert.any([is.string, is.buffer, is.array, is.undefined], value['pfx']);
assert.any([is.array, is.undefined], value['alpnProtocols']);
assert.any([is.string, is.undefined], value['ciphers']);
assert.any([is.string, is.buffer, is.undefined], value['dhparam']);
assert.any([is.string, is.undefined], value['signatureAlgorithms']);
assert.any([is.string, is.undefined], value['minVersion']);
assert.any([is.string, is.undefined], value['maxVersion']);
assert.any([is.boolean, is.undefined], value['honorCipherOrder']);
assert.any([is.number, is.undefined], value['tlsSessionLifetime']);
assert.any([is.string, is.undefined], value['ecdhCurve']);
assert.any([is.string, is.buffer, is.array, is.undefined], value['certificateRevocationLists']);

for (const key in value) {
if (!(key in this._internals.https)) {
Expand Down
10 changes: 5 additions & 5 deletions test/agent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Agent as HttpAgent} from 'http';
import {Agent as HttpsAgent} from 'https';
import test, {Constructor} from 'ava';
import test from 'ava';
import sinon from 'sinon';
import withServer, {withHttpsServer} from './helpers/with-server.js';

const createAgentSpy = <T extends HttpsAgent>(AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => {
const createAgentSpy = <T extends HttpsAgent>(AgentClass: (new (...args: any[]) => any)): {agent: T; spy: sinon.SinonSpy} => {
const agent: T = new AgentClass({keepAlive: true});
// eslint-disable-next-line import/no-named-as-default-member
const spy = sinon.spy(agent, 'addRequest' as any);
Expand Down Expand Up @@ -54,7 +54,7 @@ test('non-object agent option works with https', withHttpsServer(), async (t, se
});

test('redirects from http to https work with an agent object', withServer, async (t, serverHttp) => {
await withHttpsServer()(t, async (t, serverHttps, got) => {
await withHttpsServer().exec(t, async (t, serverHttps, got) => {
serverHttp.get('/', (_request, response) => {
response.end('http');
});
Expand Down Expand Up @@ -90,7 +90,7 @@ test('redirects from http to https work with an agent object', withServer, async
});

test('redirects from https to http work with an agent object', withHttpsServer(), async (t, serverHttps, got) => {
await withServer(t, async (t, serverHttp) => {
await withServer.exec(t, async (t, serverHttp) => {
serverHttp.get('/', (_request, response) => {
response.end('http');
});
Expand Down Expand Up @@ -161,7 +161,7 @@ test('no socket hung up regression', withServer, async (t, server, got) => {
const token = 'helloworld';

server.get('/', (request, response) => {
if (request.headers.token !== token) {
if (request.headers['token'] !== token) {
response.statusCode = 401;
response.end();
return;
Expand Down
10 changes: 5 additions & 5 deletions test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ test('`url` is required', async t => {
);

const firstError = await t.throwsAsync(got(''));
invalidUrl(t, firstError, '');
invalidUrl(t, firstError!, '');

const secondError = await t.throwsAsync(got({url: ''}));
invalidUrl(t, secondError, '');
invalidUrl(t, secondError!, '');
});

test('`url` should be utf-8 encoded', async t => {
Expand Down Expand Up @@ -54,7 +54,7 @@ test('throws if the url option is missing', async t => {

test('throws an error if the protocol is not specified', async t => {
const error = await t.throwsAsync(got('example.com'));
invalidUrl(t, error, 'example.com');
invalidUrl(t, error!, 'example.com');
});

test('properly encodes query string', withServer, async (t, server, got) => {
Expand Down Expand Up @@ -639,10 +639,10 @@ test('throws on too large noise', t => {
});

test('options have url even if some are invalid', async t => {
const error = await t.throwsAsync<RequestError>(got('https://example.com', {
const error = (await t.throwsAsync<RequestError>(got('https://example.com', {
// @ts-expect-error Testing purposes
invalid: true,
}));
})))!;

t.is((error.options.url as URL).href, 'https://example.com/');
t.true(error instanceof Error);
Expand Down
14 changes: 7 additions & 7 deletions test/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ test('custom headers (extend)', withServer, async (t, server, got) => {

const instance = got.extend(options);
const headers = await instance('').json<Headers>();
t.is(headers.unicorn, 'rainbow');
t.is(headers['unicorn'], 'rainbow');
});

test('extend overwrites arrays with a deep clone', t => {
Expand Down Expand Up @@ -109,7 +109,7 @@ test('custom endpoint with custom headers (extend)', withServer, async (t, serve

const instance = got.extend({headers: {unicorn: 'rainbow'}, prefixUrl: server.url});
const headers = await instance('').json<Headers>();
t.is(headers.unicorn, 'rainbow');
t.is(headers['unicorn'], 'rainbow');
t.not(headers['user-agent'], undefined);
});

Expand Down Expand Up @@ -183,7 +183,7 @@ test.skip('defaults are cloned on instance creation', t => {
delete options.hooks!.beforeRequest![0];
});

t.not(options.context!.foo, instance.defaults.options.context.foo);
t.not(options.context!['foo'], instance.defaults.options.context['foo']);
t.not(options.hooks!.beforeRequest, instance.defaults.options.hooks.beforeRequest);
});

Expand Down Expand Up @@ -281,13 +281,13 @@ test('extend with custom handlers', withServer, async (t, server, got) => {
const instance = got.extend({
handlers: [
(options, next) => {
options.headers.unicorn = 'rainbow';
options.headers['unicorn'] = 'rainbow';
return next(options);
},
],
});
const headers = await instance('').json<Headers>();
t.is(headers.unicorn, 'rainbow');
t.is(headers['unicorn'], 'rainbow');
});

test('extend with instances', t => {
Expand All @@ -300,7 +300,7 @@ test('extend with a chain', t => {
const a = got.extend({prefixUrl: 'https://example.com/'});
const b = got.extend(a, {headers: {foo: 'bar'}});
t.is(b.defaults.options.prefixUrl.toString(), 'https://example.com/');
t.is(b.defaults.options.headers.foo, 'bar');
t.is(b.defaults.options.headers['foo'], 'bar');
});

test('async handlers', withServer, async (t, server, got) => {
Expand Down Expand Up @@ -369,7 +369,7 @@ test('waits for handlers to finish', withServer, async (t, server, got) => {
return next(options);
},
async (options, next) => {
options.headers.foo = 'bar';
options.headers['foo'] = 'bar';
return next(options);
},
],
Expand Down
Loading