2022-03-03 09:35:26 +00:00
|
|
|
import { ExternalHostError } from '../../types/errors/external-host-error';
|
2023-06-05 03:39:27 +00:00
|
|
|
import { Http, HttpError } from '../../util/http';
|
2021-05-21 05:40:09 +00:00
|
|
|
import type {
|
|
|
|
DatasourceApi,
|
|
|
|
DigestConfig,
|
|
|
|
GetReleasesConfig,
|
2022-08-01 11:05:17 +00:00
|
|
|
RegistryStrategy,
|
2021-05-21 05:40:09 +00:00
|
|
|
ReleaseResult,
|
|
|
|
} from './types';
|
|
|
|
|
|
|
|
export abstract class Datasource implements DatasourceApi {
|
|
|
|
protected constructor(public readonly id: string) {
|
|
|
|
this.http = new Http(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
caching: boolean | undefined;
|
|
|
|
|
|
|
|
customRegistrySupport = true;
|
|
|
|
|
|
|
|
defaultConfig: Record<string, unknown> | undefined;
|
|
|
|
|
2022-06-16 11:00:11 +00:00
|
|
|
defaultRegistryUrls?: string[] | (() => string[]);
|
2021-05-21 05:40:09 +00:00
|
|
|
|
2023-03-19 16:09:46 +00:00
|
|
|
defaultVersioning?: string | undefined;
|
2021-05-21 05:40:09 +00:00
|
|
|
|
2022-08-01 11:05:17 +00:00
|
|
|
registryStrategy: RegistryStrategy | undefined = 'first';
|
2021-05-21 05:40:09 +00:00
|
|
|
|
|
|
|
protected http: Http;
|
|
|
|
|
|
|
|
abstract getReleases(
|
|
|
|
getReleasesConfig: GetReleasesConfig
|
|
|
|
): Promise<ReleaseResult | null>;
|
|
|
|
|
2022-02-05 05:54:18 +00:00
|
|
|
getDigest?(config: DigestConfig, newValue?: string): Promise<string | null>;
|
2021-05-21 05:40:09 +00:00
|
|
|
|
2023-06-05 03:39:27 +00:00
|
|
|
handleHttpErrors(err: HttpError): void {}
|
2021-05-21 05:40:09 +00:00
|
|
|
|
2023-06-05 03:39:27 +00:00
|
|
|
protected handleGenericErrors(err: Error): never {
|
2021-07-09 07:06:50 +00:00
|
|
|
// istanbul ignore if: not easy testable with nock
|
|
|
|
if (err instanceof ExternalHostError) {
|
|
|
|
throw err;
|
|
|
|
}
|
2023-06-05 03:39:27 +00:00
|
|
|
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
this.handleHttpErrors(err);
|
|
|
|
|
|
|
|
const statusCode = err.response?.statusCode;
|
|
|
|
if (statusCode) {
|
2023-09-08 10:56:56 +00:00
|
|
|
if (statusCode === 429 || (statusCode >= 500 && statusCode < 600)) {
|
2023-06-05 03:39:27 +00:00
|
|
|
throw new ExternalHostError(err);
|
|
|
|
}
|
2021-05-21 05:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-05 03:39:27 +00:00
|
|
|
|
2021-05-21 05:40:09 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|