Skip to content

Commit

Permalink
fix remote-path normalization (gulpjs#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Mar 1, 2017
1 parent 12d62da commit 5cff859
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var cloneable = require('cloneable-readable');
var replaceExt = require('replace-ext');
var cloneStats = require('clone-stats');
var cloneBuffer = require('clone-buffer');
var removeTrailingSep = require('remove-trailing-separator');

var normalize = require('./lib/normalize');
var inspectStream = require('./lib/inspect-stream');
Expand Down Expand Up @@ -195,7 +194,7 @@ Object.defineProperty(File.prototype, 'cwd', {
if (!cwd || typeof cwd !== 'string') {
throw new Error('cwd must be a non-empty string.');
}
this._cwd = removeTrailingSep(normalize(cwd));
this._cwd = normalize(cwd);
},
});

Expand All @@ -211,7 +210,7 @@ Object.defineProperty(File.prototype, 'base', {
if (typeof base !== 'string' || !base) {
throw new Error('base must be a non-empty string, or null/undefined.');
}
base = removeTrailingSep(normalize(base));
base = normalize(base);
if (base !== this._cwd) {
this._base = base;
}
Expand Down Expand Up @@ -300,7 +299,7 @@ Object.defineProperty(File.prototype, 'path', {
if (typeof path !== 'string') {
throw new Error('path should be a string.');
}
path = removeTrailingSep(normalize(path));
path = normalize(path);

// Record history only when path changed
if (path && path !== this.path) {
Expand All @@ -319,7 +318,7 @@ Object.defineProperty(File.prototype, 'symlink', {
throw new Error('symlink should be a string');
}

this._symlink = removeTrailingSep(normalize(symlink));
this._symlink = normalize(symlink);
},
});

Expand Down
18 changes: 17 additions & 1 deletion lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
'use strict';

var path = require('path');
var urlRegex = require('url-regex');
var normalizeUrl = require('normalize-url');
var removeTrailingSep = require('remove-trailing-separator');

function normalize(str) {
return str === '' ? str : path.normalize(str);
if (str === '') {
return str;
}

if (urlRegex().test(str) && !/^www\./.test(str)) {
return normalizeUrl(str, {
normalizeProtocol: false,
stripFragment: false,
stripWWW: false,
removeTrailingSlash: false,
});
}

return removeTrailingSep(path.normalize(str));
}

module.exports = normalize;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"cloneable-readable": "^1.0.0",
"is-stream": "^1.1.0",
"remove-trailing-separator": "^1.0.1",
"replace-ext": "^1.0.0"
"replace-ext": "^1.0.0",
"normalize-url": "1.6.1",
"url-regex": "3.2.0"
},
"devDependencies": {
"eslint": "^1.7.3",
Expand Down
18 changes: 18 additions & 0 deletions test/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ describe('normalize()', function() {
expect(result).toEqual(path.normalize(str));
done();
});

it('keeps remote urls', function(done) {
var checks = [
['https://github.com/gulpjs/vinyl/issues/127', 'https://github.com/gulpjs/vinyl/issues/127'],
['http://github.com/gulpjs/vinyl/issues///127', 'http://github.com/gulpjs/vinyl/issues/127'],
['https://github.com/gulpjs/vinyl/issues/../baz/bar', 'https://github.com/gulpjs/vinyl/baz/bar'],
['//github.com/gulpjs/vinyl/issues/../../gulp/', '//github.com/gulpjs/gulp/'],
['www.github.com/gulpjs/vinyl/issues/../../gulp', 'www.github.com/gulpjs/gulp'],
];

for (var i = 0; i < checks.length; i++) {
var check = checks[i];
var result = normalize(check[0]);
expect(result).toEqual(check[1]);
}

done();
});
});

0 comments on commit 5cff859

Please sign in to comment.