-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
util.ts
88 lines (81 loc) · 2.61 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import url from 'url';
import got from '../../util/got';
import { logger } from '../../logger';
import { DATASOURCE_MAVEN } from '../../constants/data-binary-source';
import { DatasourceError } from '../common';
function isMavenCentral(pkgUrl: url.URL | string): boolean {
return (
(typeof pkgUrl === 'string' ? pkgUrl : pkgUrl.host) === 'central.maven.org'
);
}
function isTemporalError(err: { code: string; statusCode: number }): boolean {
return (
err.code === 'ECONNRESET' ||
err.statusCode === 429 ||
(err.statusCode >= 500 && err.statusCode < 600)
);
}
function isHostError(err: { code: string }): boolean {
return err.code === 'ETIMEDOUT';
}
function isNotFoundError(err: { code: string; statusCode: number }): boolean {
return err.code === 'ENOTFOUND' || err.statusCode === 404;
}
function isPermissionsIssue(err: { statusCode: number }): boolean {
return err.statusCode === 401 || err.statusCode === 403;
}
function isConnectionError(err: { code: string }): boolean {
return (
err.code === 'ERR_TLS_CERT_ALTNAME_INVALID' || err.code === 'ECONNREFUSED'
);
}
export async function downloadHttpProtocol(
pkgUrl: url.URL | string,
hostType = DATASOURCE_MAVEN
): Promise<string | null> {
let raw: { body: string };
try {
raw = await got(pkgUrl, {
hostType,
hooks: {
beforeRedirect: [
(options: any): void => {
if (
options.search &&
options.search.indexOf('X-Amz-Algorithm') !== -1
) {
// maven repository is hosted on amazon, redirect url includes authentication.
// eslint-disable-next-line no-param-reassign
delete options.auth;
}
},
],
},
});
} catch (err) {
const failedUrl = pkgUrl.toString();
if (isNotFoundError(err)) {
logger.debug({ failedUrl }, `Url not found`);
} else if (isHostError(err)) {
// istanbul ignore next
logger.warn({ failedUrl }, `Cannot connect to ${hostType} host`);
} else if (isPermissionsIssue(err)) {
logger.warn(
{ failedUrl },
'Dependency lookup unauthorized. Please add authentication with a hostRule'
);
} else if (isTemporalError(err)) {
logger.info({ failedUrl, err }, 'Temporary error');
if (isMavenCentral(pkgUrl)) {
throw new DatasourceError(err);
}
} else if (isConnectionError(err)) {
// istanbul ignore next
logger.info({ failedUrl }, 'Connection refused to maven registry');
} else {
logger.warn({ failedUrl, err }, 'Unknown error');
}
return null;
}
return raw.body;
}