filefive
Version:
SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux
56 lines (55 loc) • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLocal = isLocal;
exports.connectionID = connectionID;
exports.parseURI = parseURI;
exports.createURI = createURI;
const types_1 = require("../types");
const whatwg_url_1 = require("whatwg-url");
function isLocal(uri) {
return uri.startsWith('file:');
}
function connectionID(scheme, user, host, port) {
switch (scheme) {
case 'file':
return types_1.LocalFileSystemID;
case 's3':
scheme = host.match(/^https?:\/\//)[0].slice(0, -3);
host = host.substring(scheme.length + 3);
break;
}
return `${scheme}://${user}@${host}:${port}`;
}
// https://url.spec.whatwg.org/#special-scheme
// https://jsdom.github.io/whatwg-url/#url=bmF0czovL2xvY2FsaG9zdDo0MjAwLw==&base=ZmlsZTovLy8=
function parseURI(uri) {
const { protocol, pathname, username, hostname, port: p } = new whatwg_url_1.URL(uri);
const port = p ? parseInt(p) : defaultPort(protocol.slice(0, -1));
return {
id: connectionID(protocol.slice(0, -1), username, hostname, port),
scheme: protocol.slice(0, -1),
path: decodeURI(pathname),
user: username,
host: hostname,
port
};
}
function createURI(id, path) {
const u = (0, whatwg_url_1.parseURL)(id);
u.path = path;
if (!u.port) {
u.port = defaultPort(u.scheme);
}
return (0, whatwg_url_1.serializeURL)(u);
}
function defaultPort(protocol) {
switch (protocol) {
case 'file': return null;
case 'ftp': return 21;
case 'sftp': return 22;
case 'http': return 80;
case 'https': return 443;
}
console.error(`Default port for ${protocol} is not defined`);
return null;
}