Skip to content

Commit

Permalink
Update tar and add tests (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldudak committed May 4, 2022
1 parent c112661 commit d2599f9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 43 deletions.
3 changes: 2 additions & 1 deletion packages/utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
logs
logs
test/data/pack.tgz
3 changes: 1 addition & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"charm": "^1.0.2",
"fs-extra": "^8.1.0",
"fstream": "^1.0.12",
"tar": "^2.2.2",
"tar": "^6.1.11",
"tar-stream": "^2.1.4"
},
"devDependencies": {
"@types/charm": "^1.0.1",
"@types/fs-extra": "^8.1.0",
"@types/tar": "^4.0.3",
"@types/tar-stream": "^2.1.0"
},
"publishConfig": {
Expand Down
21 changes: 13 additions & 8 deletions packages/utils/src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
writeJson as writeJsonRaw,
createWriteStream,
} from "fs-extra";
import { FStreamEntry, Reader } from "fstream";
import { Pack } from "tar";
import tarStream from "tar-stream";
import https, { Agent, request } from "https";
import { resolve, sep } from "path";
import zlib from "zlib";
import { request as httpRequest } from "http";
import { Readable as ReadableStream } from "stream";
Expand All @@ -19,6 +19,7 @@ import { parseJson, withoutStart, sleep, tryParseJson, isObject } from "./miscel
import { FS, Dir, InMemoryFS } from "./fs";
import { assertDefined } from "./assertions";
import { LoggerWithErrors } from "./logging";
import { Stats } from "fs";

export async function readFile(path: string): Promise<string> {
const res = await readFileWithEncoding(path, { encoding: "utf8" });
Expand Down Expand Up @@ -268,20 +269,24 @@ export function createTgz(dir: string, onError: (error: Error) => void): NodeJS.
}

function createTar(dir: string, onError: (error: Error) => void): NodeJS.ReadableStream {
const packer = Pack({ noProprietary: true, path: dir }).on("error", onError);
const dirSegments = resolve(dir).split(sep);
const parentDir = dirSegments.slice(0, dirSegments.length - 1).join(sep);
const entryToAdd = dirSegments[dirSegments.length - 1];
const packer = new Pack({ cwd: parentDir, filter: addDirectoryExecutablePermission });
packer.on("error", onError);
const stream = packer.add(entryToAdd);
packer.end();

return Reader({ path: dir, type: "Directory", filter: addDirectoryExecutablePermission })
.on("error", onError)
.pipe(packer);
return stream;
}

/**
* Work around a bug where directories bundled on Windows do not have executable permission when extracted on Linux.
* https://github.com/npm/node-tar/issues/7#issuecomment-17572926
*/
function addDirectoryExecutablePermission(entry: FStreamEntry): boolean {
if (entry.props.type === "Directory") {
entry.props.mode = addExecutePermissionsFromReadPermissions(entry.props.mode);
function addDirectoryExecutablePermission(_: string, stat: Stats): boolean {
if (stat.isDirectory()) {
stat.mode = addExecutePermissionsFromReadPermissions(stat.mode);
}
return true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/types/tar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "tar";
1 change: 1 addition & 0 deletions packages/utils/test/data/pack/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test file
27 changes: 27 additions & 0 deletions packages/utils/test/io.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from "path";
import fs from "fs";
import { list } from "tar";
import { createTgz } from "../src/io";

describe("io", () => {
describe(createTgz, () => {
it("packs a directory", (done) => {
const dir = path.join(__dirname, "data", "pack");
const archivePath = path.join(__dirname, "data", "pack.tgz");

createTgz(dir, (err) => {
throw err;
})
.pipe(fs.createWriteStream(archivePath))
.on("finish", () => {
expect(fs.existsSync(archivePath)).toBe(true);
const entries: string[] = [];
list({ file: archivePath, onentry: (e) => entries.push(e.path) }, null, () => {
expect(entries[0]).toBe("pack/");
expect(entries[1]).toBe("pack/test.txt");
done();
});
});
});
});
});
33 changes: 1 addition & 32 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1809,13 +1809,6 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==

"@types/minipass@*":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@types/minipass/-/minipass-2.2.0.tgz#51ad404e8eb1fa961f75ec61205796807b6f9651"
integrity sha512-wuzZksN4w4kyfoOv/dlpov4NOunwutLA/q7uc00xU02ZyUY+aoM5PWIXEKBMnm0NHd4a+N71BMjq+x7+2Af1fg==
dependencies:
"@types/node" "*"

"@types/mz@^0.0.31":
version "0.0.31"
resolved "https://registry.yarnpkg.com/@types/mz/-/mz-0.0.31.tgz#a4d80c082fefe71e40a7c0f07d1e6555bbbc7b52"
Expand Down Expand Up @@ -1937,14 +1930,6 @@
dependencies:
"@types/node" "*"

"@types/tar@^4.0.3":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@types/tar/-/tar-4.0.3.tgz#e2cce0b8ff4f285293243f5971bd7199176ac489"
integrity sha512-Z7AVMMlkI8NTWF0qGhC4QIX0zkV/+y0J8x7b/RsHrN0310+YNjoJd8UrApCiGBCWtKjxS9QhNqLi2UJNToh5hA==
dependencies:
"@types/minipass" "*"
"@types/node" "*"

"@types/tmp@^0.2.0":
version "0.2.2"
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.2.2.tgz#424537a3b91828cb26aaf697f21ae3cd1b69f7e7"
Expand Down Expand Up @@ -2524,13 +2509,6 @@ bl@^4.0.3:
inherits "^2.0.4"
readable-stream "^3.4.0"

block-stream@*:
version "0.0.9"
resolved "https://registry.yarnpkg.com/block-stream/-/block-stream-0.0.9.tgz#13ebfe778a03205cfe03751481ebb4b3300c126a"
integrity sha1-E+v+d4oDIFz+A3UUgeu0szAMEmo=
dependencies:
inherits "~2.0.0"

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -8027,15 +8005,6 @@ tar-stream@^2.1.4:
inherits "^2.0.3"
readable-stream "^3.1.1"

tar@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/tar/-/tar-2.2.2.tgz#0ca8848562c7299b8b446ff6a4d60cdbb23edc40"
integrity sha512-FCEhQ/4rE1zYv9rYXJw/msRqsnmlje5jHP6huWeBZ704jUTy02c5AZyWujpMR1ax6mVw9NyJMfuK2CMDWVIfgA==
dependencies:
block-stream "*"
fstream "^1.0.12"
inherits "2"

tar@^4.4.12:
version "4.4.13"
resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.13.tgz#43b364bc52888d555298637b10d60790254ab525"
Expand All @@ -8049,7 +8018,7 @@ tar@^4.4.12:
safe-buffer "^5.1.2"
yallist "^3.0.3"

tar@^6.0.2, tar@^6.1.0:
tar@^6.0.2, tar@^6.1.0, tar@^6.1.11:
version "6.1.11"
resolved "https://registry.yarnpkg.com/tar/-/tar-6.1.11.tgz#6760a38f003afa1b2ffd0ffe9e9abbd0eab3d621"
integrity sha512-an/KZQzQUkZCkuoAA64hM92X0Urb6VpRhAFllDzz44U2mcD5scmT3zBc4VgVpkugF580+DQn8eAFSyoQt0tznA==
Expand Down

0 comments on commit d2599f9

Please sign in to comment.