Skip to content

Commit

Permalink
Resolve latest package version from bundled packages if possible (#12…
Browse files Browse the repository at this point in the history
…6492) (#126579)

(cherry picked from commit 50e8972)
  • Loading branch information
kpollich authored Mar 1, 2022
1 parent 23423b0 commit 14001b5
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions x-pack/plugins/fleet/server/services/epm/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { URL } from 'url';

import mime from 'mime-types';
import semverGte from 'semver/functions/gte';

import type { Response } from 'node-fetch';

Expand Down Expand Up @@ -74,64 +75,73 @@ async function _fetchFindLatestPackage(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
const logger = appContextService.getLogger();
const { ignoreConstraints = false } = options ?? {};

const bundledPackage = await getBundledPackageByName(packageName);

const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search?package=${packageName}&experimental=true`);

if (!ignoreConstraints) {
setKibanaVersion(url);
}

const res = await fetchUrl(url.toString(), 1);
const searchResults: RegistryPackage[] = JSON.parse(res);

return searchResults;
}

export async function fetchFindLatestPackageOrThrow(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
try {
const searchResults = await _fetchFindLatestPackage(packageName, options);
const res = await fetchUrl(url.toString(), 1);
const searchResults: RegistryPackage[] = JSON.parse(res);

const latestPackageFromRegistry = searchResults[0] ?? null;

if (!searchResults.length) {
throw new PackageNotFoundError(`[${packageName}] package not found in registry`);
if (bundledPackage && semverGte(bundledPackage.version, latestPackageFromRegistry.version)) {
return bundledPackage;
}

return searchResults[0];
return latestPackageFromRegistry;
} catch (error) {
const bundledPackage = await getBundledPackageByName(packageName);
logger.error(
`Failed to fetch latest version of ${packageName} from registry: ${error.message}`
);

if (!bundledPackage) {
throw error;
// Fall back to the bundled version of the package if it exists
if (bundledPackage) {
return bundledPackage;
}

return bundledPackage;
// Otherwise, return null and allow callers to determine whether they'll consider this an error or not
return null;
}
}

export async function fetchFindLatestPackageOrThrow(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
const latestPackage = await _fetchFindLatestPackage(packageName, options);

if (!latestPackage) {
throw new PackageNotFoundError(`[${packageName}] package not found in registry`);
}

return latestPackage;
}

export async function fetchFindLatestPackageOrUndefined(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
const logger = appContextService.getLogger();

try {
const searchResults = await _fetchFindLatestPackage(packageName, options);
const latestPackage = await _fetchFindLatestPackage(packageName, options);

if (!searchResults.length) {
if (!latestPackage) {
return undefined;
}

return searchResults[0];
return latestPackage;
} catch (error) {
const bundledPackage = await getBundledPackageByName(packageName);

if (!bundledPackage) {
return undefined;
}

return bundledPackage;
logger.warn(`Error fetching latest package for ${packageName}: ${error.message}`);
return undefined;
}
}

Expand Down

0 comments on commit 14001b5

Please sign in to comment.