Skip to content

Commit 4f64307

Browse files
chore(get): simplify symlink logic (#1035)
* 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
1 parent 69661c3 commit 4f64307

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/get/decompress.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,23 @@ async function unzip(zippedFile, cacheDir) {
5252

5353
while (entry !== null) {
5454
let entryPathAbs = path.join(cacheDir, entry.filename);
55-
// Create the directory beforehand to prevent `ENOENT: no such file or directory` errors.
55+
/* Create the directory beforehand to prevent `ENOENT: no such file or directory` errors. */
5656
await fs.promises.mkdir(path.dirname(entryPathAbs), { recursive: true });
57-
// Pipe read to write stream
57+
/* Check if entry is a symbolic link */
58+
const isSymlink = ((modeFromEntry(entry) & 0o170000) === 0o120000);
5859
const readStream = await entry.openReadStream();
59-
const writeStream = fs.createWriteStream(entryPathAbs);
60-
await stream.promises.pipeline(readStream, writeStream);
61-
// Get file mode
62-
let fileMode = modeFromEntry(entry);
63-
const isSymlink = ((fileMode & 0o170000) === 0o120000);
64-
60+
6561
if (isSymlink) {
66-
const buffer = await fs.promises.readFile(entryPathAbs);
67-
const link = buffer.toString();
68-
await fs.promises.rm(entryPathAbs);
69-
await fs.promises.symlink(link, entryPathAbs);
62+
const chunks = [];
63+
/* Read stream into Array. */
64+
readStream.on("data", (chunk) => chunks.push(chunk));
65+
await stream.promises.finished(readStream);
66+
const link = Buffer.concat(chunks).toString('utf8').trim();
67+
await fs.promises.symlink(link, entryPathAbs)
68+
} else {
69+
// Pipe read to write stream
70+
const writeStream = fs.createWriteStream(entryPathAbs);
71+
await stream.promises.pipeline(readStream, writeStream);
7072
}
7173

7274
// Read next entry

0 commit comments

Comments
 (0)