Fine-tune file protocol filter
This commit is contained in:
parent
06b0544bbe
commit
eec61f5289
3 changed files with 38 additions and 16 deletions
|
@ -1,23 +1,29 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const FILE_SCHEME = /^file:\/\//;
|
function _eliminateAllAfterCharacter(string, character) {
|
||||||
const WINDOWS_PREFIX = /^\/[A-Z]:/;
|
const index = string.indexOf(character);
|
||||||
function _urlToPath(targetUrl) {
|
if (index < 0) {
|
||||||
let withoutScheme = targetUrl.replace(FILE_SCHEME, '');
|
return string;
|
||||||
if (WINDOWS_PREFIX.test(withoutScheme)) {
|
|
||||||
withoutScheme = withoutScheme.slice(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const withoutQuerystring = withoutScheme.replace(/\?.*$/, '');
|
return string.slice(0, index);
|
||||||
const withoutHash = withoutQuerystring.replace(/#.*$/, '');
|
|
||||||
|
|
||||||
return decodeURIComponent(withoutHash);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _createFileHandler({ userDataPath, installPath }) {
|
function _urlToPath(targetUrl, options = {}) {
|
||||||
|
const { isWindows } = options;
|
||||||
|
|
||||||
|
const decoded = decodeURIComponent(targetUrl);
|
||||||
|
const withoutScheme = decoded.slice(isWindows ? 8 : 7);
|
||||||
|
const withoutQuerystring = _eliminateAllAfterCharacter(withoutScheme, '?');
|
||||||
|
const withoutHash = _eliminateAllAfterCharacter(withoutQuerystring, '#');
|
||||||
|
|
||||||
|
return withoutHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _createFileHandler({ userDataPath, installPath, isWindows }) {
|
||||||
return (request, callback) => {
|
return (request, callback) => {
|
||||||
// normalize() is primarily useful here for switching / to \ on windows
|
// normalize() is primarily useful here for switching / to \ on windows
|
||||||
const target = path.normalize(_urlToPath(request.url));
|
const target = path.normalize(_urlToPath(request.url, { isWindows }));
|
||||||
|
|
||||||
if (!path.isAbsolute(target)) {
|
if (!path.isAbsolute(target)) {
|
||||||
return callback();
|
return callback();
|
||||||
|
@ -34,10 +40,15 @@ function _createFileHandler({ userDataPath, installPath }) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function installFileHandler({ protocol, userDataPath, installPath }) {
|
function installFileHandler({
|
||||||
|
protocol,
|
||||||
|
userDataPath,
|
||||||
|
installPath,
|
||||||
|
isWindows,
|
||||||
|
}) {
|
||||||
protocol.interceptFileProtocol(
|
protocol.interceptFileProtocol(
|
||||||
'file',
|
'file',
|
||||||
_createFileHandler({ userDataPath, installPath })
|
_createFileHandler({ userDataPath, installPath, isWindows })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
main.js
1
main.js
|
@ -452,6 +452,7 @@ app.on('ready', () => {
|
||||||
protocol: electronProtocol,
|
protocol: electronProtocol,
|
||||||
userDataPath,
|
userDataPath,
|
||||||
installPath,
|
installPath,
|
||||||
|
isWindows: process.platform === 'win32',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,12 +34,13 @@ describe('Protocol Filter', () => {
|
||||||
expect(actual).to.equal(expected);
|
expect(actual).to.equal(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns proper file path for windows style file URI', () => {
|
it('returns proper file path for file URI on windows', () => {
|
||||||
const path =
|
const path =
|
||||||
'file:///C:/Users/Someone/dev/desktop/background.html?name=Signal&locale=en&version=2.4.0';
|
'file:///C:/Users/Someone/dev/desktop/background.html?name=Signal&locale=en&version=2.4.0';
|
||||||
const expected = 'C:/Users/Someone/dev/desktop/background.html';
|
const expected = 'C:/Users/Someone/dev/desktop/background.html';
|
||||||
|
const isWindows = true;
|
||||||
|
|
||||||
const actual = _urlToPath(path, { isWindows: true });
|
const actual = _urlToPath(path, { isWindows });
|
||||||
expect(actual).to.equal(expected);
|
expect(actual).to.equal(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -72,6 +73,15 @@ describe('Protocol Filter', () => {
|
||||||
expect(actual).to.equal(expected);
|
expect(actual).to.equal(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handles SMB share path on windows', () => {
|
||||||
|
const path = 'file://relative/path';
|
||||||
|
const expected = 'elative/path';
|
||||||
|
const isWindows = true;
|
||||||
|
|
||||||
|
const actual = _urlToPath(path, { isWindows });
|
||||||
|
expect(actual).to.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
it('hands back a path with .. in it', () => {
|
it('hands back a path with .. in it', () => {
|
||||||
const path = 'file://../../..';
|
const path = 'file://../../..';
|
||||||
const expected = '../../..';
|
const expected = '../../..';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue