xo-remote-parser
Version:
Parse and format XO remote URLs
161 lines (160 loc) • 8.74 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parse = exports.format = void 0;
var _filter = _interopRequireDefault(require("lodash/filter"));
var _map = _interopRequireDefault(require("lodash/map"));
var _trim = _interopRequireDefault(require("lodash/trim"));
var _trimStart = _interopRequireDefault(require("lodash/trimStart"));
var _querystring = _interopRequireDefault(require("querystring"));
var _urlParse = _interopRequireDefault(require("url-parse"));
const _excluded = ["type", "host", "path", "port", "username", "password", "domain", "protocol", "region"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
const NFS_RE = /^([^:]+):(?:(\d+):)?([^:?]+)(\?[^?]*)?$/;
const SMB_RE = /^([^:]+):(.+)@([^@]+)\\\\([^\0?]+)(?:\0([^?]*))?(\?[^?]*)?$/;
const sanitizePath = (...paths) => (0, _filter.default)((0, _map.default)(paths, s => s && (0, _filter.default)((0, _map.default)(s.split('/'), _trim.default)).join('/'))).join('/');
const parseOptionList = (optionList = '') => {
if (optionList.startsWith('?')) {
optionList = optionList.substring(1);
}
const parsed = _querystring.default.parse(optionList);
Object.keys(parsed).forEach(key => {
const val = parsed[key];
parsed[key] = val === '' ? false : JSON.parse(val);
});
return parsed;
};
const makeOptionList = options => {
const encoded = {};
Object.keys(options).filter(key => options[key] !== undefined).forEach(key => {
const val = options[key];
encoded[key] = JSON.stringify(val);
});
return _querystring.default.stringify(encoded);
};
const parse = string => {
let object = {};
let _string$split = string.split('://'),
_string$split2 = _slicedToArray(_string$split, 2),
type = _string$split2[0],
rest = _string$split2[1];
if (type === 'file') {
object.type = 'file';
let optionList;
var _rest$split = rest.split('?');
var _rest$split2 = _slicedToArray(_rest$split, 2);
rest = _rest$split2[0];
optionList = _rest$split2[1];
object.path = `/${(0, _trimStart.default)(rest, '/')}`;
object = _objectSpread(_objectSpread({}, parseOptionList(optionList)), object);
} else if (type === 'nfs') {
object.type = 'nfs';
let host, port, path, optionList;
try {
;
var _NFS_RE$exec = NFS_RE.exec(rest);
var _NFS_RE$exec2 = _slicedToArray(_NFS_RE$exec, 5);
host = _NFS_RE$exec2[1];
port = _NFS_RE$exec2[2];
path = _NFS_RE$exec2[3];
optionList = _NFS_RE$exec2[4];
} catch (err) {
;
var _rest$split3 = rest.split(':');
var _rest$split4 = _slicedToArray(_rest$split3, 2);
host = _rest$split4[0];
path = _rest$split4[1];
object.invalidUrl = true;
}
object.host = host;
object.port = port;
object.path = `/${(0, _trimStart.default)(path, '/')}`;
object = _objectSpread(_objectSpread({}, parseOptionList(optionList)), object);
} else if (type === 'smb') {
object.type = 'smb';
const _SMB_RE$exec = SMB_RE.exec(rest),
_SMB_RE$exec2 = _slicedToArray(_SMB_RE$exec, 7),
username = _SMB_RE$exec2[1],
password = _SMB_RE$exec2[2],
domain = _SMB_RE$exec2[3],
host = _SMB_RE$exec2[4],
_SMB_RE$exec2$ = _SMB_RE$exec2[5],
path = _SMB_RE$exec2$ === void 0 ? '' : _SMB_RE$exec2$,
optionList = _SMB_RE$exec2[6];
object.host = host;
object.path = path;
object.domain = domain;
object.username = username;
object.password = password;
object = _objectSpread(_objectSpread({}, parseOptionList(optionList)), object);
} else if (type === 's3' || type === 's3+http') {
const parsed = (0, _urlParse.default)(string, false);
object.protocol = parsed.protocol === 's3:' ? 'https' : 'http';
object.type = 's3';
object.region = parsed.hash.length < 2 ? undefined : parsed.hash.slice(1);
object.host = parsed.host;
object.path = parsed.pathname;
object.username = decodeURIComponent(parsed.username);
object.password = decodeURIComponent(parsed.password);
object = _objectSpread(_objectSpread({}, parseOptionList(parsed.query)), object);
}
return object;
};
exports.parse = parse;
const format = _ref => {
let type = _ref.type,
host = _ref.host,
path = _ref.path,
port = _ref.port,
username = _ref.username,
password = _ref.password,
domain = _ref.domain,
_ref$protocol = _ref.protocol,
protocol = _ref$protocol === void 0 ? type : _ref$protocol,
region = _ref.region,
options = _objectWithoutProperties(_ref, _excluded);
type === 'local' && (type = 'file');
let string = `${type}://`;
if (type === 'nfs') {
string += `${host}:${port !== undefined ? port + ':' : ''}`;
}
if (type === 'smb') {
string += `${username}:${password}@${domain}\\\\${host}`;
}
if (type === 's3') {
string = protocol === 'https' ? 's3://' : 's3+http://';
string += `${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}`;
}
path = sanitizePath(path);
if (type === 'smb') {
path = path.split('/');
path = '\0' + path.join('\\');
} else {
path = `/${path}`;
}
string += path;
const optionsList = makeOptionList(options);
if (optionsList !== '') {
string += '?' + optionsList;
}
if (type === 's3' && region !== undefined) {
string += `#${region}`;
}
return string;
};
exports.format = format;
//# sourceMappingURL=index.js.map
;