1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/utils/tar.ts
dependabot[bot] 10c3a452a4
Bump @types/tar from 6.1.2 to 6.1.3 (#6380)
* Bump @types/tar from 6.1.2 to 6.1.3

Bumps [@types/tar](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/tar) from 6.1.2 to 6.1.3.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/tar)

---
updated-dependencies:
- dependency-name: "@types/tar"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Fix type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Sebastian Malton <sebastian@malton.name>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Sebastian Malton <sebastian@malton.name>
2022-10-11 09:06:07 -04:00

71 lines
1.9 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
// Helper for working with tarball files (.tar, .tgz)
// Docs: https://github.com/npm/node-tar
import tar from "tar";
import path from "path";
import { parse } from "./json";
import type { JsonValue } from "type-fest";
export type ReadFileFromTarOpts<ParseJson extends boolean> = {
tarPath: string;
filePath: string;
} & (
ParseJson extends true
? {
parseJson: true;
}
: {
parseJson?: false;
}
);
export function readFileFromTar(opts: ReadFileFromTarOpts<false>): Promise<Buffer>;
export function readFileFromTar(opts: ReadFileFromTarOpts<true>): Promise<JsonValue>;
export function readFileFromTar<ParseJson extends boolean>({ tarPath, filePath, parseJson = false }: ReadFileFromTarOpts<ParseJson>): Promise<JsonValue | Buffer> {
return new Promise((resolve, reject) => {
const fileChunks: Buffer[] = [];
tar.list({
file: tarPath,
filter: entryPath => path.normalize(entryPath) === filePath,
sync: true,
onentry(entry) {
entry.on("data", chunk => {
fileChunks.push(chunk);
});
entry.once("error", err => {
reject(new Error(`reading file has failed ${entry.path}: ${err}`));
});
entry.once("end", () => {
const data = Buffer.concat(fileChunks);
const result = parseJson ? parse(data.toString("utf8")) : data;
resolve(result);
});
},
});
if (!fileChunks.length) {
reject(new Error("Not found"));
}
});
}
export async function listTarEntries(filePath: string): Promise<string[]> {
const entries: string[] = [];
await tar.list({
file: filePath,
onentry: (entry) => {
entries.push(path.normalize(entry.path));
},
});
return entries;
}