Skip to content

Commit

Permalink
Remove pify
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Jan 13, 2022
1 parent 5691b35 commit 8eac0f7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
29 changes: 13 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import decompressTargz from 'decompress-targz';
import decompressUnzip from 'decompress-unzip';
import fs from 'graceful-fs';
import makeDir from 'make-dir';
import pify from 'pify';
import stripDirs from 'strip-dirs';

const fsP = pify(fs);

const runPlugins = (input, options) => {
if (options.plugins.length === 0) {
return Promise.resolve([]);
Expand All @@ -21,7 +18,7 @@ const runPlugins = (input, options) => {
return Promise.all(options.plugins.map(x => x(input, options))).then(files => files.reduce((a, b) => a.concat(b)));
};

const safeMakeDir = (dir, realOutputPath) => fsP.realpath(dir)
const safeMakeDir = (dir, realOutputPath) => fs.promises.realpath(dir)
.catch(_ => {
const parent = path.dirname(dir);
return safeMakeDir(parent, realOutputPath);
Expand All @@ -31,10 +28,10 @@ const safeMakeDir = (dir, realOutputPath) => fsP.realpath(dir)
throw new Error('Refusing to create a directory outside the output path.');
}

return makeDir(dir).then(fsP.realpath);
return makeDir(dir).then(fs.promises.realpath);
});

const preventWritingThroughSymlink = (destination, realOutputPath) => fsP.readlink(destination)
const preventWritingThroughSymlink = (destination, realOutputPath) => fs.promises.readlink(destination)
// Either no file exists, or it's not a symlink. In either case, this is
// not an escape we need to worry about in this phase.
.catch(_ => null)
Expand Down Expand Up @@ -76,14 +73,14 @@ const extractFile = (input, output, options) => runPlugins(input, options).then(

if (x.type === 'directory') {
return makeDir(output)
.then(outputPath => fsP.realpath(outputPath))
.then(outputPath => fs.promises.realpath(outputPath))
.then(realOutputPath => safeMakeDir(dest, realOutputPath))
.then(() => fsP.utimes(dest, now, x.mtime))
.then(() => fs.promises.utimes(dest, now, x.mtime))
.then(() => x);
}

return makeDir(output)
.then(outputPath => fsP.realpath(outputPath))
.then(outputPath => fs.promises.realpath(outputPath))
.then(realOutputPath =>
// Attempt to ensure parent directory exists (failing if it's outside the output dir)
safeMakeDir(path.dirname(dest), realOutputPath).then(() => realOutputPath),
Expand All @@ -95,28 +92,28 @@ const extractFile = (input, output, options) => runPlugins(input, options).then(

return realOutputPath;
})
.then(realOutputPath => fsP.realpath(path.dirname(dest))
.then(realOutputPath => fs.promises.realpath(path.dirname(dest))
.then(realDestinationDir => {
if (realDestinationDir.indexOf(realOutputPath) !== 0) {
throw new Error(`Refusing to write outside output directory: ${realDestinationDir}`);
}
}))
.then(() => {
if (x.type === 'link') {
return fsP.link(x.linkname, dest);
return fs.promises.link(x.linkname, dest);
}

if (x.type === 'symlink' && process.platform === 'win32') {
return fsP.link(x.linkname, dest);
return fs.promises.link(x.linkname, dest);
}

if (x.type === 'symlink') {
return fsP.symlink(x.linkname, dest);
return fs.promises.symlink(x.linkname, dest);
}

return fsP.writeFile(dest, x.data, {mode});
return fs.promises.writeFile(dest, x.data, {mode});
})
.then(() => x.type === 'file' && fsP.utimes(dest, now, x.mtime))
.then(() => x.type === 'file' && fs.promises.utimes(dest, now, x.mtime))
.then(() => x);
}));
});
Expand All @@ -141,7 +138,7 @@ const decompress = (input, output, options) => {
...options,
};

const read = typeof input === 'string' ? fsP.readFile(input) : Promise.resolve(input);
const read = typeof input === 'string' ? fs.promises.readFile(input) : Promise.resolve(input);

return read.then(buf => extractFile(buf, output, options));
};
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"decompress-unzip": "^4.0.1",
"graceful-fs": "^4.2.9",
"make-dir": "^3.1.0",
"pify": "^5.0.0",
"strip-dirs": "^3.0.0"
},
"devDependencies": {
Expand Down
16 changes: 7 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import fs from 'node:fs';
import {promises as fs} from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {promisify} from 'node:util';
import isJpg from 'is-jpg';
import {pathExists} from 'path-exists';
import pify from 'pify';
import rimraf from 'rimraf';
import test from 'ava';
import decompress from './index.js';

const fsP = pify(fs);
const rimrafP = promisify(rimraf);
const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -40,13 +38,13 @@ test('extract file', async t => {
});

test('extract file using buffer', async t => {
const tarBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar'));
const tarBuf = await fs.readFile(path.join(__dirname, 'fixtures', 'file.tar'));
const tarFiles = await decompress(tarBuf);
const tarbzBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar.bz2'));
const tarbzBuf = await fs.readFile(path.join(__dirname, 'fixtures', 'file.tar.bz2'));
const tarbzFiles = await decompress(tarbzBuf);
const targzBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar.gz'));
const targzBuf = await fs.readFile(path.join(__dirname, 'fixtures', 'file.tar.gz'));
const targzFiles = await decompress(targzBuf);
const zipBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.zip'));
const zipBuf = await fs.readFile(path.join(__dirname, 'fixtures', 'file.zip'));
const zipFiles = await decompress(zipBuf);

t.is(tarFiles[0].path, 'test.jpg');
Expand All @@ -65,7 +63,7 @@ test.serial('extract file to directory', async t => {

test.serial('extract symlink', async t => {
await decompress(path.join(__dirname, 'fixtures', 'symlink.tar'), __dirname, {strip: 1});
t.is(await fsP.realpath(path.join(__dirname, 'symlink')), path.join(__dirname, 'file.txt'));
t.is(await fs.realpath(path.join(__dirname, 'symlink')), path.join(__dirname, 'file.txt'));
});

test.serial('extract directory', async t => {
Expand Down Expand Up @@ -104,7 +102,7 @@ test('map option', async t => {

test.serial('set mtime', async t => {
const files = await decompress(path.join(__dirname, 'fixtures', 'file.tar'), __dirname);
const stat = await fsP.stat(path.join(__dirname, 'test.jpg'));
const stat = await fs.stat(path.join(__dirname, 'test.jpg'));
t.deepEqual(files[0].mtime, stat.mtime);
});

Expand Down

0 comments on commit 8eac0f7

Please sign in to comment.