Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,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",
Expand Down Expand Up @@ -853,4 +854,4 @@
"yo": "2.0.6",
"zlib": "^1.0.5"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ 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);
}
});
} 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.`
);
}

// While unpacking a tar.gz file with unzipBuffer() will result in a thrown error in the try-catch above,
// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."}'
);
});

Expand All @@ -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."}'
);
});

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20577,7 +20577,7 @@ [email protected], [email protected], "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==
Expand Down