[Ingest Manager] Remove dupe code between two unpack*ToCache fns (#82801) (#82952)

## Summary

Update `unpackRegistryPackageToCache` to call `unpackArchiveToCache` instead of duplicating much of it.

Now an archive is iterated & put in cache via the same function regardless of its initial source.
This commit is contained in:
John Schulz 2020-11-09 10:54:37 -05:00 committed by GitHub
parent 694c322166
commit aa4ccc3113
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 24 deletions

View file

@ -234,6 +234,7 @@
"markdown-it": "^10.0.0",
"md5": "^2.1.0",
"mime": "^2.4.4",
"mime-types": "^2.1.27",
"mini-css-extract-plugin": "0.8.0",
"minimatch": "^3.0.4",
"moment": "^2.24.0",
@ -849,4 +850,4 @@
"yargs": "^15.4.1",
"zlib": "^1.0.5"
}
}
}

View file

@ -43,7 +43,7 @@ export async function unpackArchiveToCache(
await bufferExtractor(archiveBuffer, filter, (entry: ArchiveEntry) => {
const { path, buffer } = entry;
// skip directories
if (path.slice(-1) === '/') return;
if (path.endsWith('/')) return;
if (buffer) {
cacheSet(path, buffer);
paths.push(path);
@ -51,7 +51,7 @@ export async function unpackArchiveToCache(
});
} catch (error) {
throw new PackageInvalidArchiveError(
`Error during extraction of uploaded package: ${error}. Assumed content type was ${contentType}, check if this matches the archive type.`
`Error during extraction of package: ${error}. Assumed content type was ${contentType}, check if this matches the archive type.`
);
}
@ -59,7 +59,7 @@ export async function unpackArchiveToCache(
// unpacking a zip file with untarBuffer() just results in nothing.
if (paths.length === 0) {
throw new PackageInvalidArchiveError(
`Uploaded archive seems empty. Assumed content type was ${contentType}, check if this matches the archive type.`
`Archive seems empty. Assumed content type was ${contentType}, check if this matches the archive type.`
);
}
return paths;

View file

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import mime from 'mime-types';
import semver from 'semver';
import { Response } from 'node-fetch';
import { URL } from 'url';
@ -18,15 +18,15 @@ import {
RegistrySearchResults,
RegistrySearchResult,
} from '../../../types';
import { unpackArchiveToCache } from '../archive';
import {
cacheGet,
cacheSet,
cacheDelete,
getArchiveFilelist,
setArchiveFilelist,
deleteArchiveFilelist,
} from './cache';
import { ArchiveEntry, getBufferExtractor } from './extract';
import { ArchiveEntry } from './extract';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { streamToBuffer } from './streams';
import { getRegistryUrl } from './registry_url';
@ -137,22 +137,12 @@ export async function unpackRegistryPackageToCache(
pkgVersion: string,
filter = (entry: ArchiveEntry): boolean => true
): Promise<string[]> {
const paths: string[] = [];
const { archiveBuffer, archivePath } = await fetchArchiveBuffer(pkgName, pkgVersion);
const bufferExtractor = getBufferExtractor({ archivePath });
if (!bufferExtractor) {
throw new Error('Unknown compression format. Please use .zip or .gz');
const contentType = mime.lookup(archivePath);
if (!contentType) {
throw new Error(`Unknown compression format for '${archivePath}'. Please use .zip or .gz`);
}
await bufferExtractor(archiveBuffer, filter, (entry: ArchiveEntry) => {
const { path, buffer } = entry;
const { file } = pathParts(path);
if (!file) return;
if (buffer) {
cacheSet(path, buffer);
paths.push(path);
}
});
const paths: string[] = await unpackArchiveToCache(archiveBuffer, contentType);
return paths;
}

View file

@ -92,7 +92,7 @@ export default function (providerContext: FtrProviderContext) {
.send(buf)
.expect(400);
expect(res.error.text).to.equal(
'{"statusCode":400,"error":"Bad Request","message":"Uploaded archive seems empty. Assumed content type was application/gzip, check if this matches the archive type."}'
'{"statusCode":400,"error":"Bad Request","message":"Archive seems empty. Assumed content type was application/gzip, check if this matches the archive type."}'
);
});
@ -105,7 +105,7 @@ export default function (providerContext: FtrProviderContext) {
.send(buf)
.expect(400);
expect(res.error.text).to.equal(
'{"statusCode":400,"error":"Bad Request","message":"Error during extraction of uploaded package: Error: end of central directory record signature not found. Assumed content type was application/zip, check if this matches the archive type."}'
'{"statusCode":400,"error":"Bad Request","message":"Error during extraction of package: Error: end of central directory record signature not found. Assumed content type was application/zip, check if this matches the archive type."}'
);
});

View file

@ -20000,7 +20000,7 @@ mime-db@1.44.0, mime-db@1.x.x, "mime-db@>= 1.40.0 < 2":
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.44.0.tgz#fa11c5eb0aca1334b4233cb4d52f10c5a6272f92"
integrity sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==
mime-types@^2.1.12, mime-types@^2.1.25, mime-types@^2.1.26, mime-types@~2.1.17, mime-types@~2.1.19, mime-types@~2.1.24:
mime-types@^2.1.12, mime-types@^2.1.25, mime-types@^2.1.26, mime-types@^2.1.27, mime-types@~2.1.17, mime-types@~2.1.19, mime-types@~2.1.24:
version "2.1.27"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.27.tgz#47949f98e279ea53119f5722e0f34e529bec009f"
integrity sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==