Skip to content

Commit

Permalink
prevent process.chdir() from affecting this module
Browse files Browse the repository at this point in the history
by always using absolute paths
  • Loading branch information
shinnn committed Oct 2, 2018
1 parent 820ef8b commit bafb616
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {inspect, promisify} = require('util');
const {resolve} = require('path');
const {Transform} = require('stream');

const cancelablePump = require('cancelable-pump');
Expand Down Expand Up @@ -94,7 +95,7 @@ module.exports = function dlTar(...args) {
} arguments instead.`);
}

const [url, dest] = args;
const [url, dest, options = {}] = args;

return new Observable(observer => {
if (typeof url !== 'string') {
Expand All @@ -113,8 +114,6 @@ module.exports = function dlTar(...args) {
throw new Error(`${DEST_ERROR}, but got '' (empty string).`);
}

const options = argLen === 3 ? args[2] : {};

if (argLen === 3) {
if (!isPlainObj(options)) {
throw new TypeError(`Expected an object to specify \`dl-tar\` options, but got ${inspect(options)}.`);
Expand Down Expand Up @@ -167,23 +166,25 @@ module.exports = function dlTar(...args) {
}
}

const cwd = process.cwd();
const absoluteDest = resolve(cwd, dest);
let ended = false;
let cancel;

(async () => {
try {
const [request] = await Promise.all([
const request = absoluteDest === cwd ? await loadRequestFromCwdOrNpm() : (await Promise.all([
loadRequestFromCwdOrNpm(),
promisifiedMkdirp(dest)
]);
promisifiedMkdirp(absoluteDest)
]))[0];

if (ended) {
return;
}

const unpackStream = new InternalUnpack({
...options,
cwd: dest,
cwd: absoluteDest,
observer
});

Expand Down

0 comments on commit bafb616

Please sign in to comment.