Skip to content

Commit

Permalink
[Ops] Upgrade to axios 1.4 (elastic#163732)
Browse files Browse the repository at this point in the history
upgrade `axios` to 1.4

- adjust to header usage, and config optionality
- Axios' adapters are now resolved from a string key by axios, no need
to import/instantiate adapters
- most of the changed code stems from changes in Axios' types
  - `response.config` is now optional
- there was a change in the type of AxiosHeaders <->
InternalAxiosHeaders

Closes: elastic#162661
Closes: elastic#162414

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 9d2696b)
  • Loading branch information
delanni committed Aug 22, 2023
1 parent 155ee2d commit 909b5d2
Show file tree
Hide file tree
Showing 18 changed files with 1,865 additions and 2,987 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -191,7 +191,7 @@
"JSONStream": "1.3.5",
"antlr4ts": "^0.5.0-alpha.3",
"archiver": "^5.2.0",
"axios": "^0.27.2",
"axios": "^1.4.0",
"base64-js": "^1.3.1",
"bluebird": "3.5.5",
"brace": "0.11.1",
Expand Down
Expand Up @@ -14,14 +14,13 @@ import crypto from 'crypto';

import execa from 'execa';
import Axios, { AxiosRequestConfig } from 'axios';
// @ts-expect-error not "public", but necessary to prevent Jest shimming from breaking things
import httpAdapter from 'axios/lib/adapters/http';

import { ToolingLog } from '../tooling_log';
import { parseConfig, Config } from './ci_stats_config';
import type { CiStatsTestGroupInfo, CiStatsTestRun } from './ci_stats_test_group_types';
import { CiStatsMetadata } from './ci_stats_metadata';

import type { CiStatsTestGroupInfo, CiStatsTestRun } from './ci_stats_test_group_types';

const BASE_URL = 'https://ci-stats.kibana.dev';
const SECOND = 1000;
const MINUTE = 60 * SECOND;
Expand Down Expand Up @@ -346,7 +345,7 @@ export class CiStatsReporter {
headers,
data: body,
params: query,
adapter: httpAdapter,
adapter: 'http',

// if it can be serialized into a string, send it
maxBodyLength: Infinity,
Expand Down
8 changes: 4 additions & 4 deletions packages/kbn-test/src/failed_tests_reporter/github_api.ts
Expand Up @@ -8,7 +8,7 @@

import Url from 'url';

import Axios, { AxiosRequestConfig, AxiosInstance } from 'axios';
import Axios, { AxiosRequestConfig, AxiosInstance, AxiosHeaders, AxiosHeaderValue } from 'axios';
import { ToolingLog, isAxiosResponseError, isAxiosRequestError } from '@kbn/dev-utils';

const BASE_URL = 'https://api.github.com/repos/elastic/kibana/';
Expand Down Expand Up @@ -129,7 +129,7 @@ export class GithubApi {
): Promise<{
status: number;
statusText: string;
headers: Record<string, string | string[] | undefined>;
headers: Record<string, AxiosHeaderValue | undefined>;
data: T;
}> {
const executeRequest = !this.dryRun || options.safeForDryRun;
Expand All @@ -144,7 +144,7 @@ export class GithubApi {
return {
status: 200,
statusText: 'OK',
headers: {},
headers: new AxiosHeaders(),
data: dryRunResponse,
};
}
Expand All @@ -157,7 +157,7 @@ export class GithubApi {
const githubApiFailed = isAxiosResponseError(error) && error.response.status >= 500;
const errorResponseLog =
isAxiosResponseError(error) &&
`[${error.config.method} ${error.config.url}] ${error.response.status} ${error.response.statusText} Error`;
`[${error.config?.method} ${error.config?.url}] ${error.response.status} ${error.response.statusText} Error`;

if ((unableToReachGithub || githubApiFailed) && attempt < maxAttempts) {
const waitMs = 1000 * attempt;
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-test/src/kbn_client/kbn_client_requester.ts
Expand Up @@ -15,7 +15,7 @@ import { ToolingLog, isAxiosRequestError, isAxiosResponseError } from '@kbn/dev-

const isConcliftOnGetError = (error: any) => {
return (
isAxiosResponseError(error) && error.config.method === 'GET' && error.response.status === 409
isAxiosResponseError(error) && error.config?.method === 'GET' && error.response.status === 409
);
};

Expand Down
8 changes: 2 additions & 6 deletions src/dev/build/lib/download.ts
Expand Up @@ -15,10 +15,6 @@ import { createHash } from 'crypto';
import Axios from 'axios';
import { ToolingLog, isAxiosResponseError } from '@kbn/dev-utils';

// https://github.com/axios/axios/tree/ffea03453f77a8176c51554d5f6c3c6829294649/lib/adapters
// @ts-expect-error untyped internal module used to prevent axios from using xhr adapter in tests
import AxiosHttpAdapter from 'axios/lib/adapters/http';

import { mkdirp } from './fs';

function tryUnlink(path: string) {
Expand Down Expand Up @@ -75,7 +71,7 @@ export async function downloadToDisk({
const response = await Axios.request({
url,
responseType: 'stream',
adapter: AxiosHttpAdapter,
adapter: 'http',
});

if (response.status !== 200) {
Expand Down Expand Up @@ -151,7 +147,7 @@ export async function downloadToString({
const resp = await Axios.request<string>({
url,
method: 'GET',
adapter: AxiosHttpAdapter,
adapter: 'http',
responseType: 'text',
validateStatus: !expectStatus ? undefined : (status) => status === expectStatus,
});
Expand Down
6 changes: 1 addition & 5 deletions src/dev/typescript/ref_output_cache/archives.ts
Expand Up @@ -16,10 +16,6 @@ import { ToolingLog } from '@kbn/dev-utils';
import Axios from 'axios';
import del from 'del';

// https://github.com/axios/axios/tree/ffea03453f77a8176c51554d5f6c3c6829294649/lib/adapters
// @ts-expect-error untyped internal module used to prevent axios from using xhr adapter in tests
import AxiosHttpAdapter from 'axios/lib/adapters/http';

interface Archive {
sha: string;
path: string;
Expand Down Expand Up @@ -116,7 +112,7 @@ export class Archives {
const resp = await Axios.request({
url,
responseType: 'stream',
adapter: AxiosHttpAdapter,
adapter: 'http',
});

await Fs.mkdir(Path.dirname(target), { recursive: true });
Expand Down
Expand Up @@ -6,7 +6,7 @@
*/

import { isObjectLike, isEmpty } from 'lodash';
import { AxiosInstance, Method, AxiosResponse, AxiosBasicCredentials } from 'axios';
import { AxiosInstance, Method, AxiosResponse, AxiosHeaders, AxiosHeaderValue } from 'axios';
import { Logger } from '../../../../../../src/core/server';
import { getCustomAgents } from './get_custom_agents';
import { ActionsConfigurationUtilities } from '../../actions_config';
Expand All @@ -28,18 +28,16 @@ export const request = async <T = unknown>({
data?: T;
params?: unknown;
configurationUtilities: ActionsConfigurationUtilities;
headers?: Record<string, string> | null;
headers?: Record<string, AxiosHeaderValue>;
validateStatus?: (status: number) => boolean;
auth?: AxiosBasicCredentials;
}): Promise<AxiosResponse> => {
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger, url);
const { maxContentLength, timeout } = configurationUtilities.getResponseSettings();

return await axios(url, {
...rest,
method,
// Axios doesn't support `null` value for `headers` property.
headers: headers ?? undefined,
headers,
data: data ?? {},
// use httpAgent and httpsAgent and set axios proxy: false, to be able to handle fail on invalid certs
httpAgent,
Expand Down Expand Up @@ -144,6 +142,10 @@ export const createAxiosResponse = (res: Partial<AxiosResponse>): AxiosResponse
status: 200,
statusText: 'OK',
headers: { ['content-type']: 'application/json' },
config: { method: 'GET', url: 'https://example.com' },
config: {
method: 'GET',
url: 'https://example.com',
headers: new AxiosHeaders(),
},
...res,
});
Expand Up @@ -45,7 +45,7 @@ describe('axios connections', () => {
// needed to prevent the dreaded Error: Cross origin http://localhost forbidden
// see: https://github.com/axios/axios/issues/1754#issuecomment-572778305
savedAxiosDefaultsAdapter = axios.defaults.adapter;
axios.defaults.adapter = require('axios/lib/adapters/http');
axios.defaults.adapter = 'http';
});

afterAll(() => {
Expand Down
Expand Up @@ -8,10 +8,10 @@
import { fromNullable, Option, map, filter } from 'fp-ts/lib/Option';
import { pipe } from 'fp-ts/lib/pipeable';

export function getRetryAfterIntervalFromHeaders(headers: Record<string, string>): Option<number> {
export function getRetryAfterIntervalFromHeaders(headers: Record<string, unknown>): Option<number> {
return pipe(
fromNullable(headers['retry-after']),
map((retryAfter) => parseInt(retryAfter, 10)),
map((retryAfter) => parseInt(retryAfter as string, 10)),
filter((retryAfter) => !isNaN(retryAfter))
);
}
Expand Up @@ -6,6 +6,7 @@
*/
jest.mock('axios', () => ({
create: jest.fn(),
AxiosHeaders: jest.requireActual('axios').AxiosHeaders,
}));

import axios from 'axios';
Expand Down
Expand Up @@ -165,7 +165,7 @@ export async function executor(
url,
logger,
...basicAuth,
headers,
headers: headers ? headers : {},
data,
configurationUtilities,
})
Expand Down
Expand Up @@ -40,9 +40,6 @@ const CA = fsReadFileSync(CA_FILE, 'utf8');
const Auth = 'elastic:changeme';
const AuthB64 = Buffer.from(Auth).toString('base64');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const AxiosDefaultsAadapter = require('axios/lib/adapters/http');

describe('axios connections', () => {
let testServer: http.Server | https.Server | null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -52,7 +49,7 @@ describe('axios connections', () => {
// needed to prevent the dreaded Error: Cross origin http://localhost forbidden
// see: https://github.com/axios/axios/issues/1754#issuecomment-572778305
savedAxiosDefaultsAdapter = axios.defaults.adapter;
axios.defaults.adapter = AxiosDefaultsAadapter;
axios.defaults.adapter = 'http';
});

afterEach(() => {
Expand Down
Expand Up @@ -40,9 +40,6 @@ const CA = fsReadFileSync(CA_FILE, 'utf8');
const Auth = 'elastic:changeme';
const AuthB64 = Buffer.from(Auth).toString('base64');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const AxiosDefaultsAadapter = require('axios/lib/adapters/http');

const ServerResponse = 'A unique response returned by the server!';

describe('axios connections', () => {
Expand All @@ -54,7 +51,7 @@ describe('axios connections', () => {
// needed to prevent the dreaded Error: Cross origin http://localhost forbidden
// see: https://github.com/axios/axios/issues/1754#issuecomment-572778305
savedAxiosDefaultsAdapter = axios.defaults.adapter;
axios.defaults.adapter = AxiosDefaultsAadapter;
axios.defaults.adapter = 'http';
});

afterEach(() => {
Expand Down
Expand Up @@ -72,7 +72,7 @@ init().catch((e) => {
console.error(e.message);
} else if (isAxiosError(e)) {
console.error(
`${e.config.method?.toUpperCase() || 'GET'} ${e.config.url} (Code: ${
`${e.config?.method?.toUpperCase() || 'GET'} ${e.config?.url} (Code: ${
e.response?.status
})`
);
Expand Down
Expand Up @@ -31,17 +31,17 @@ export async function getKibanaVersion({
switch (e.response?.status) {
case 401:
throw new AbortError(
`Could not access Kibana with the provided credentials. Username: "${e.config.auth?.username}". Password: "${e.config.auth?.password}"`
`Could not access Kibana with the provided credentials. Username: "${e.config?.auth?.username}". Password: "${e.config?.auth?.password}"`
);

case 404:
throw new AbortError(
`Could not get version on ${e.config.url} (Code: 404)`
`Could not get version on ${e.config?.url} (Code: 404)`
);

default:
throw new AbortError(
`Cannot access Kibana on ${e.config.baseURL}. Please specify Kibana with: "--kibana-url <url>"`
`Cannot access Kibana on ${e.config?.baseURL}. Please specify Kibana with: "--kibana-url <url>"`
);
}
}
Expand Down
Expand Up @@ -30,7 +30,7 @@ describe.each(packageInfos)('Chromium archive: %s/%s', (architecture, platform)

const originalAxios = axios.defaults.adapter;
beforeAll(async () => {
axios.defaults.adapter = require('axios/lib/adapters/http'); // allow Axios to send actual requests
axios.defaults.adapter = 'http';
});

afterAll(() => {
Expand Down
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import axios, { AxiosInstance, AxiosRequestHeaders } from 'axios';
import axios, { AxiosInstance } from 'axios';
import type { Capabilities as UICapabilities } from 'src/core/types';
import { format as formatUrl } from 'url';
import util from 'util';
Expand Down Expand Up @@ -61,7 +61,7 @@ export class UICapabilitiesService {
this.log.debug(
`requesting ${spaceUrlPrefix}/api/core/capabilities to parse the uiCapabilities`
);
const requestHeaders: AxiosRequestHeaders = credentials
const requestHeaders: Record<string, string> = credentials
? {
Authorization: `Basic ${Buffer.from(
`${credentials.username}:${credentials.password}`
Expand Down

0 comments on commit 909b5d2

Please sign in to comment.