57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.stripLastSlash = exports.assertBeeUrl = exports.isValidBeeUrl = void 0;
|
||
|
const error_1 = require("./error");
|
||
|
const type_1 = require("./type");
|
||
|
function isNodeJsError(e) {
|
||
|
return (0, type_1.isObject)(e) && typeof e.code === 'string';
|
||
|
}
|
||
|
/**
|
||
|
* Validates that passed string is valid URL of Bee.
|
||
|
* We support only HTTP and HTTPS protocols.
|
||
|
*
|
||
|
* @param url
|
||
|
*/
|
||
|
function isValidBeeUrl(url) {
|
||
|
try {
|
||
|
if (typeof url !== 'string') {
|
||
|
return false;
|
||
|
}
|
||
|
const urlObject = new URL(url);
|
||
|
// There can be wide range of protocols passed.
|
||
|
return urlObject.protocol === 'http:' || urlObject.protocol === 'https:';
|
||
|
}
|
||
|
catch (e) {
|
||
|
// URL constructor throws TypeError if not valid URL
|
||
|
// TODO: Drop the `.code` hack for NodeJS environment: https://github.com/ethersphere/bee-js/issues/204
|
||
|
if (e instanceof TypeError || (isNodeJsError(e) && e.code === 'ERR_INVALID_URL')) {
|
||
|
return false;
|
||
|
}
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
exports.isValidBeeUrl = isValidBeeUrl;
|
||
|
/**
|
||
|
* Validates that passed string is valid URL of Bee, if not it throws BeeArgumentError.
|
||
|
* We support only HTTP and HTTPS protocols.
|
||
|
* @param url
|
||
|
* @throws BeeArgumentError if non valid URL
|
||
|
*/
|
||
|
function assertBeeUrl(url) {
|
||
|
if (!isValidBeeUrl(url)) {
|
||
|
throw new error_1.BeeArgumentError('URL is not valid!', url);
|
||
|
}
|
||
|
}
|
||
|
exports.assertBeeUrl = assertBeeUrl;
|
||
|
/**
|
||
|
* Removes trailing slash out of the given string.
|
||
|
* @param url
|
||
|
*/
|
||
|
function stripLastSlash(url) {
|
||
|
if (url.endsWith('/')) {
|
||
|
return url.slice(0, -1);
|
||
|
}
|
||
|
return url;
|
||
|
}
|
||
|
exports.stripLastSlash = stripLastSlash;
|