Skip to content

Commit

Permalink
Fix fileUrlToFsPath on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jan 23, 2018
1 parent 79a7294 commit ae18606
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ exports.ensureTrailingSlash = function (url) {

exports.fileUrlToFsPath = function (fileUrl) {
fileUrl = fileUrl.replace(/[?#].*$/, ''); // Strip query string and fragment identifier
return decodeURI(fileUrl).substr((process.platform === 'win32' ? 'file:///' : 'file://').length).replace(/[#\?].*$/, '');
var fsPath = decodeURI(fileUrl).substr('file://'.length).replace(/[#\?].*$/, '');

if (process.platform === 'win32') {
fsPath = fsPath.replace(/^\/([^/]+)/, function ($0, encodedDriveLetter) {
return decodeURIComponent(encodedDriveLetter);
});
}
return fsPath;
};

exports.fsFilePathToFileUrl = function (fsFilePath) {
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,23 @@ describe('urlTools', function () {
});
});
});

describe('on Windows', function () {
var originalPlatform = process.platform;
beforeEach(function () {
Object.defineProperty(process, 'platform', { value: 'win32' });
});
afterEach(function () {
Object.defineProperty(process, 'platform', { value: originalPlatform });
});

describe('#urlOrFsPathToUrl', function () {
it('should reinstate the drive letter in decoded form', function () {
expect(
urlTools.fileUrlToFsPath('file:///C%3A/foo/bar%20quux.png'),
'to equal',
'C:/foo/bar quux.png'
);
});
});
});

0 comments on commit ae18606

Please sign in to comment.