Skip to content

Commit

Permalink
chore(get): simplify symlink logic (#1035)
Browse files Browse the repository at this point in the history
* Current implementation: write entry as file, check if entry is
symlink, remove file and create symlink
* Improved implementation: check if entry is symlink, stream entry to
buffer, extract link target and create symlink. Otherwise, stream entry
to file.

Refs: #1030
  • Loading branch information
ayushmanchhabra committed Feb 15, 2024
1 parent 69661c3 commit 4f64307
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/get/decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,23 @@ async function unzip(zippedFile, cacheDir) {

while (entry !== null) {
let entryPathAbs = path.join(cacheDir, entry.filename);
// Create the directory beforehand to prevent `ENOENT: no such file or directory` errors.
/* Create the directory beforehand to prevent `ENOENT: no such file or directory` errors. */
await fs.promises.mkdir(path.dirname(entryPathAbs), { recursive: true });
// Pipe read to write stream
/* Check if entry is a symbolic link */
const isSymlink = ((modeFromEntry(entry) & 0o170000) === 0o120000);
const readStream = await entry.openReadStream();
const writeStream = fs.createWriteStream(entryPathAbs);
await stream.promises.pipeline(readStream, writeStream);
// Get file mode
let fileMode = modeFromEntry(entry);
const isSymlink = ((fileMode & 0o170000) === 0o120000);


if (isSymlink) {
const buffer = await fs.promises.readFile(entryPathAbs);
const link = buffer.toString();
await fs.promises.rm(entryPathAbs);
await fs.promises.symlink(link, entryPathAbs);
const chunks = [];
/* Read stream into Array. */
readStream.on("data", (chunk) => chunks.push(chunk));
await stream.promises.finished(readStream);
const link = Buffer.concat(chunks).toString('utf8').trim();
await fs.promises.symlink(link, entryPathAbs)
} else {
// Pipe read to write stream
const writeStream = fs.createWriteStream(entryPathAbs);
await stream.promises.pipeline(readStream, writeStream);
}

// Read next entry
Expand Down

0 comments on commit 4f64307

Please sign in to comment.