Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix remote-path normalization (#127) #128

Closed
wants to merge 2 commits into from
Closed
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
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');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to normalize to allow remote paths with trailing slash

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)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url-regex matches on strings like www.google.de which are also a valid directory.
So let path.normalize do the normalization

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"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix versions for normalize-urland url-regex as the latest version only support node >= 4.
Could be updated when vinyl drops node 0.10...0.12 support

},
"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', path.normalize('www.github.com/gulpjs/vinyl/issues/../../gulp')],
];

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

done();
});
});