Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/utils.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -127,7 +127,7 @@
const src = path.join(destDir, header.name);
const target = path.resolve(path.dirname(src), header.linkname);
entryCount++;
fs.symlink(target, src, err =>{

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions/ Node.js / Test (macos-latest, 18)

Use 'fs.promises.symlink()' instead

Check warning on line 130 in lib/utils.js

View workflow job for this annotation

GitHub Actions/ Node.js / Test (macos-latest, 18)

Use 'fs.promises.symlink()' instead
if (err) return reject(err);
successCount++;
stream.resume();
Expand DownExpand Up@@ -168,6 +168,13 @@

exports.safePipe = safePipe;

function normalizePath(fileName){
fileName = path.normalize(fileName);
// https://nodejs.org/api/path.html#path_path_normalize_path
if (process.platform === 'win32') fileName = fileName.replace(/\\+/g, '/');
return fileName;
}

exports.stripFileName = (strip, fileName, type) =>{
// before
// node/package.json
Expand All@@ -189,15 +196,18 @@
// /foo => foo
if (fileName[0] === '/') fileName = fileName.replace(/^\/+/, '');

// fix case
// ./foo/bar => foo/bar
if (fileName){
fileName = normalizePath(fileName);
}

let s = fileName.split('/');

// fix relative path
// foo/../bar/../../asdf/
// => asdf/
if (s.indexOf('..') !== -1){
fileName = path.normalize(fileName);
// https://npm.taobao.org/mirrors/node/latest/docs/api/path.html#path_path_normalize_path
if (process.platform === 'win32') fileName = fileName.replace(/\\+/g, '/');
// replace '../' on ../../foo/bar
fileName = fileName.replace(/(\.\.\/)+/, '');
if (type === 'directory' && fileName && fileName[fileName.length - 1] !== '/'){
Expand Down
4 changes: 4 additions & 0 deletions test/utils.test.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -52,6 +52,10 @@ describe('test/utils.test.js', () =>{
assert(utils.stripFileName(0, '/home/../../../foo.txt', 'file') === 'foo.txt');
assert(utils.stripFileName(0, '///../../../../foo', 'file') === 'foo');
assert(utils.stripFileName(0, '../../../../etc/hosts', 'file') === 'etc/hosts');

assert(utils.stripFileName(0, './etc/hosts', 'file') === 'etc/hosts');
assert(utils.stripFileName(0, './././etc/hosts', 'file') === 'etc/hosts');
assert(utils.stripFileName(1, './././etc/hosts', 'file') === 'hosts');
});

it('should replace \\', () =>{
Expand Down
Loading