refactor(maven): Clarify HTTP resource check function (#12640)

This commit is contained in:
Sergei Zharinov 2021-11-14 14:16:54 +03:00 committed by GitHub
parent 811f81d3a2
commit 60d03add71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 15 deletions

View file

@ -10,11 +10,11 @@ import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import { MAVEN_REPO } from './common'; import { MAVEN_REPO } from './common';
import type { MavenDependency, ReleaseMap } from './types'; import type { MavenDependency, ReleaseMap } from './types';
import { import {
checkHttpResource,
downloadMavenXml, downloadMavenXml,
getDependencyInfo, getDependencyInfo,
getDependencyParts, getDependencyParts,
getMavenUrl, getMavenUrl,
isHttpResourceExists,
} from './util'; } from './util';
export { id } from './common'; export { id } from './common';
@ -198,19 +198,21 @@ async function addReleasesUsingHeadRequests(
repoUrl repoUrl
); );
const artifactUrl = getMavenUrl(dependency, repoUrl, pomUrl); const artifactUrl = getMavenUrl(dependency, repoUrl, pomUrl);
const res = await isHttpResourceExists(artifactUrl);
const release: Release = { version }; const release: Release = { version };
if (is.string(res)) { const res = await checkHttpResource(artifactUrl);
release.releaseTimestamp = res;
}
// Retry earlier for error status other than 404 if (res === 'error') {
if (res === null) {
retryEarlier = true; retryEarlier = true;
} }
workingReleaseMap[version] = res ? release : null; if (is.date(res)) {
release.releaseTimestamp = res.toISOString();
}
if (res !== 'not-found' && res !== 'error') {
workingReleaseMap[version] = release;
}
} }
); );

View file

@ -14,3 +14,5 @@ export interface MavenXml {
} }
export type ReleaseMap = Record<string, Release | null>; export type ReleaseMap = Record<string, Release | null>;
export type HttpResourceCheckResult = 'found' | 'not-found' | 'error' | Date;

View file

@ -1,14 +1,20 @@
import url from 'url'; import url from 'url';
import { DateTime } from 'luxon';
import { XmlDocument } from 'xmldoc'; import { XmlDocument } from 'xmldoc';
import { HOST_DISABLED } from '../../constants/error-messages'; import { HOST_DISABLED } from '../../constants/error-messages';
import { logger } from '../../logger'; import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error'; import { ExternalHostError } from '../../types/errors/external-host-error';
import { Http, HttpResponse } from '../../util/http'; import { Http, HttpResponse } from '../../util/http';
import { regEx } from '../../util/regex'; import { regEx } from '../../util/regex';
import { normalizeDate } from '../metadata';
import type { ReleaseResult } from '../types'; import type { ReleaseResult } from '../types';
import { MAVEN_REPO, id } from './common'; import { MAVEN_REPO, id } from './common';
import type { MavenDependency, MavenXml } from './types'; import type {
HttpResourceCheckResult,
MavenDependency,
MavenXml,
} from './types';
const http: Record<string, Http> = {}; const http: Record<string, Http> = {};
@ -100,18 +106,27 @@ export async function downloadHttpProtocol(
} }
} }
export async function isHttpResourceExists( export async function checkHttpResource(
pkgUrl: url.URL | string, pkgUrl: url.URL | string,
hostType = id hostType = id
): Promise<boolean | string | null> { ): Promise<HttpResourceCheckResult> {
try { try {
const httpClient = httpByHostType(hostType); const httpClient = httpByHostType(hostType);
const res = await httpClient.head(pkgUrl.toString()); const res = await httpClient.head(pkgUrl.toString());
const timestamp = res?.headers?.['last-modified'] as string; const timestamp = res?.headers?.['last-modified'] as string;
return timestamp || true; if (timestamp) {
const isoTimestamp = normalizeDate(timestamp);
if (isoTimestamp) {
const releaseDate = DateTime.fromISO(isoTimestamp, {
zone: 'UTC',
}).toJSDate();
return releaseDate;
}
}
return 'found';
} catch (err) { } catch (err) {
if (isNotFoundError(err)) { if (isNotFoundError(err)) {
return false; return 'not-found';
} }
const failedUrl = pkgUrl.toString(); const failedUrl = pkgUrl.toString();
@ -119,7 +134,7 @@ export async function isHttpResourceExists(
{ failedUrl, statusCode: err.statusCode }, { failedUrl, statusCode: err.statusCode },
`Can't check HTTP resource existence` `Can't check HTTP resource existence`
); );
return null; return 'error';
} }
} }

View file

@ -126,7 +126,7 @@ function massageGitlabUrl(url: string): string {
.replace('.git', ''); .replace('.git', '');
} }
function normalizeDate(input: any): string | null { export function normalizeDate(input: any): string | null {
if ( if (
typeof input === 'number' && typeof input === 'number' &&
!Number.isNaN(input) && !Number.isNaN(input) &&