Skip to content

Commit

Permalink
Move single use function in line (#82885)
Browse files Browse the repository at this point in the history
## Summary

 * Function rename to follow existing get/fetch convention
    ```diff
    - export async function loadRegistryPackage(
    + export async function getRegistryPackage(
    ```
 * Moved `unpackRegistryPackageToCache` into `getRegistryPackage`
    * In my opinion, those three/four statements are more clear than the previous name
    * It isn't used anywhere else
  • Loading branch information
John Schulz authored Nov 6, 2020
1 parent ae20a3a commit e8ec392
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export async function getPackageInfo(options: {
] = await Promise.all([
getInstallationObject({ savedObjectsClient, pkgName }),
Registry.fetchFindLatestPackage(pkgName),
Registry.loadRegistryPackage(pkgName, pkgVersion),
Registry.getRegistryPackage(pkgName, pkgVersion),
]);

// add properties that aren't (or aren't yet) on Registry response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export async function installPackageFromRegistry({
throw new PackageOutdatedError(`${pkgkey} is out-of-date and cannot be installed or updated`);
}

const { paths, registryPackageInfo } = await Registry.loadRegistryPackage(pkgName, pkgVersion);
const { paths, registryPackageInfo } = await Registry.getRegistryPackage(pkgName, pkgVersion);

const removable = !isRequiredPackage(pkgName);
const { internal = false } = registryPackageInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from '../../../types';
import { unpackArchiveToCache } from '../archive';
import { cacheGet, getArchiveFilelist, setArchiveFilelist } from '../archive';
import { ArchiveEntry } from './extract';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { streamToBuffer } from './streams';
import { getRegistryUrl } from './registry_url';
Expand Down Expand Up @@ -126,27 +125,18 @@ export async function fetchCategories(params?: CategoriesParams): Promise<Catego
return fetchUrl(url.toString()).then(JSON.parse);
}

export async function unpackRegistryPackageToCache(
pkgName: string,
pkgVersion: string,
filter = (entry: ArchiveEntry): boolean => true
): Promise<string[]> {
const { archiveBuffer, archivePath } = await fetchArchiveBuffer(pkgName, pkgVersion);
const contentType = mime.lookup(archivePath);
if (!contentType) {
throw new Error(`Unknown compression format for '${archivePath}'. Please use .zip or .gz`);
}
const paths: string[] = await unpackArchiveToCache(archiveBuffer, contentType);
return paths;
}

export async function loadRegistryPackage(
export async function getRegistryPackage(
pkgName: string,
pkgVersion: string
): Promise<{ paths: string[]; registryPackageInfo: RegistryPackage }> {
let paths = getArchiveFilelist(pkgName, pkgVersion);
if (!paths || paths.length === 0) {
paths = await unpackRegistryPackageToCache(pkgName, pkgVersion);
const { archiveBuffer, archivePath } = await fetchArchiveBuffer(pkgName, pkgVersion);
const contentType = mime.lookup(archivePath);
if (!contentType) {
throw new Error(`Unknown compression format for '${archivePath}'. Please use .zip or .gz`);
}
paths = await unpackArchiveToCache(archiveBuffer, contentType);
setArchiveFilelist(pkgName, pkgVersion, paths);
}

Expand Down Expand Up @@ -194,7 +184,7 @@ export async function ensureCachedArchiveInfo(
const paths = getArchiveFilelist(name, version);
if (!paths || paths.length === 0) {
if (installSource === 'registry') {
await loadRegistryPackage(name, version);
await getRegistryPackage(name, version);
} else {
throw new PackageCacheError(
`Package ${name}-${version} not cached. If it was uploaded, try uninstalling and reinstalling manually.`
Expand Down

0 comments on commit e8ec392

Please sign in to comment.