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

refactor(http): Import proper OutgoingHttpHeaders on all http clients #9653

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/datasource/docker/index.ts
@@ -1,4 +1,3 @@
import { OutgoingHttpHeaders } from 'http';
import URL from 'url';
import { ECR } from '@aws-sdk/client-ecr';
import hasha from 'hasha';
Expand All @@ -11,6 +10,7 @@ import { ExternalHostError } from '../../types/errors/external-host-error';
import * as packageCache from '../../util/cache/package';
import * as hostRules from '../../util/host-rules';
import { Http, HttpResponse } from '../../util/http';
import type { OutgoingHttpHeaders } from '../../util/http/types';
import { ensureTrailingSlash, trimTrailingSlash } from '../../util/url';
import * as dockerVersioning from '../../versioning/docker';
import type { GetReleasesConfig, ReleaseResult } from '../types';
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/gradle-version/index.ts
@@ -1,5 +1,6 @@
import { ExternalHostError } from '../../types/errors/external-host-error';
import { Http, HttpError } from '../../util/http';
import { Http } from '../../util/http';
import { HttpError } from '../../util/http/types';
import { regEx } from '../../util/regex';
import * as gradleVersioning from '../../versioning/gradle';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/npm/npmrc.ts
@@ -1,11 +1,11 @@
import { OutgoingHttpHeaders } from 'http';
import url from 'url';
import is from '@sindresorhus/is';
import ini from 'ini';
import registryAuthToken from 'registry-auth-token';
import getRegistryUrl from 'registry-auth-token/registry-url';
import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';
import type { OutgoingHttpHeaders } from '../../util/http/types';
import { maskToken } from '../../util/mask';
import { add } from '../../util/sanitize';

Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/pod/index.ts
Expand Up @@ -3,8 +3,9 @@ import { HOST_DISABLED } from '../../constants/error-messages';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as packageCache from '../../util/cache/package';
import { Http, HttpError } from '../../util/http';
import { Http } from '../../util/http';
import { GithubHttp } from '../../util/http/github';
import type { HttpError } from '../../util/http/types';
import type { GetReleasesConfig, ReleaseResult } from '../types';

export const id = 'pod';
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/rubygems/get.ts
@@ -1,7 +1,7 @@
import { OutgoingHttpHeaders } from 'http';
import urlJoin from 'url-join';
import { logger } from '../../logger';
import { Http } from '../../util/http';
import type { OutgoingHttpHeaders } from '../../util/http/types';
import type { ReleaseResult } from '../types';
import { id } from './common';

Expand Down
26 changes: 12 additions & 14 deletions lib/util/http/index.ts
@@ -1,4 +1,5 @@
import crypto from 'crypto';
import merge from 'deepmerge';
import got, { Options, Response } from 'got';
import { HOST_DISABLED } from '../../constants/error-messages';
import { logger } from '../../logger';
Expand All @@ -9,17 +10,11 @@ import { resolveBaseUrl } from '../url';
import { applyAuthorization, removeAuthorization } from './auth';
import { applyHostRules } from './host-rules';
import { getQueue } from './queue';
import { GotOptions, RequestStats } from './types';
import type { GotOptions, OutgoingHttpHeaders, RequestStats } from './types';

// TODO: refactor code to remove this
import './legacy';

export * from './types';

interface OutgoingHttpHeaders {
[header: string]: number | string | string[] | undefined;
}

export interface HttpOptions {
body?: any;
username?: string;
Expand Down Expand Up @@ -95,13 +90,16 @@ export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
if (httpOptions?.baseUrl) {
url = resolveBaseUrl(httpOptions.baseUrl, url);
}
// TODO: deep merge in order to merge headers
let options: GotOptions = {
method: 'get',
...this.options,
hostType: this.hostType,
...httpOptions,
} as unknown; // TODO: fixme

let options: GotOptions = merge<GotOptions>(
{
method: 'get',
...this.options,
hostType: this.hostType,
},
httpOptions
);

if (process.env.NODE_ENV === 'test') {
options.retry = 0;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/util/http/types.ts
Expand Up @@ -23,3 +23,5 @@ export interface RequestStats {
duration: number;
queueDuration: number;
}

export type OutgoingHttpHeaders = Record<string, string | string[] | undefined>;
Copy link
Collaborator Author

@zharinov zharinov Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@viceice Okay, just commit your suggestion, please 🙃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not easy from 📲 😅

2 changes: 1 addition & 1 deletion lib/workers/repository/stats.ts
@@ -1,7 +1,7 @@
import URL from 'url';
import { logger } from '../../logger';
import * as memCache from '../../util/cache/memory';
import { RequestStats } from '../../util/http';
import type { RequestStats } from '../../util/http/types';

export function printRequestStats(): void {
const httpRequests = memCache.get<RequestStats[]>('http-requests');
Expand Down