76 lines
10 KiB
JavaScript
76 lines
10 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.assertRoleUrl = exports.assertBaseUrl = exports.assertPageUrl = exports.prepareBaseUrl = exports.getUrl = exports.resolveRelativeUrl = exports.isRelative = void 0;
|
||
|
const url_1 = require("url");
|
||
|
const os_family_1 = __importDefault(require("os-family"));
|
||
|
const runtime_1 = require("../errors/runtime");
|
||
|
const types_1 = require("../errors/types");
|
||
|
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
|
||
|
const path_1 = require("path");
|
||
|
const PROTOCOL_RE = /^([\w-]+?)(?=:\/\/)/;
|
||
|
const SUPPORTED_PROTOCOL_RE = /^(https?|file):/;
|
||
|
const IMPLICIT_PROTOCOL_RE = /^\/\//;
|
||
|
const ABSOLUTE_PATH_RE = /^\/[^/]/;
|
||
|
const WIN_ABSOLUTE_PATH_RE = /^\w:[/\\]/;
|
||
|
const RELATIVE_PATH_RE = /^\.\.?[/\\]/;
|
||
|
function isRelative(url) {
|
||
|
return RELATIVE_PATH_RE.test(url);
|
||
|
}
|
||
|
exports.isRelative = isRelative;
|
||
|
function isAbsolute(url) {
|
||
|
// NOTE: path.isAbsolute treats the '//example.com' path as absolute
|
||
|
return os_family_1.default.win ? WIN_ABSOLUTE_PATH_RE.test(url) : ABSOLUTE_PATH_RE.test(url);
|
||
|
}
|
||
|
function resolveRelativeUrl(path, base) {
|
||
|
const url = new url_1.URL(path, base);
|
||
|
return url.toString();
|
||
|
}
|
||
|
exports.resolveRelativeUrl = resolveRelativeUrl;
|
||
|
function ensureProtocol(url) {
|
||
|
if (SUPPORTED_PROTOCOL_RE.test(url) || url === testcafe_hammerhead_1.SPECIAL_BLANK_PAGE)
|
||
|
return url;
|
||
|
const protocol = IMPLICIT_PROTOCOL_RE.test(url) ? 'http:' : 'http://';
|
||
|
return protocol + url;
|
||
|
}
|
||
|
function getUrl(url, base) {
|
||
|
if (isRelative(url)) {
|
||
|
if (!base)
|
||
|
throw new Error('Relative urls require a base path');
|
||
|
return resolveRelativeUrl(url, base);
|
||
|
}
|
||
|
else if (isAbsolute(url))
|
||
|
return (0, url_1.pathToFileURL)(url).toString();
|
||
|
return ensureProtocol(url);
|
||
|
}
|
||
|
exports.getUrl = getUrl;
|
||
|
function prepareBaseUrl(url) {
|
||
|
url = (0, path_1.join)(url, '/');
|
||
|
return isAbsolute(url) ? (0, url_1.pathToFileURL)(url) : new url_1.URL(url);
|
||
|
}
|
||
|
exports.prepareBaseUrl = prepareBaseUrl;
|
||
|
function assertPageUrl(url, callsiteName) {
|
||
|
assertProtocol(url, callsiteName, 'test page URL');
|
||
|
}
|
||
|
exports.assertPageUrl = assertPageUrl;
|
||
|
function assertProtocol(url, callsiteName, what) {
|
||
|
const protocol = url.match(PROTOCOL_RE);
|
||
|
const hasUnsupportedProtocol = protocol && !SUPPORTED_PROTOCOL_RE.test(url);
|
||
|
const isWinAbsolutePath = os_family_1.default.win && WIN_ABSOLUTE_PATH_RE.test(url);
|
||
|
if (hasUnsupportedProtocol && !isWinAbsolutePath && url !== testcafe_hammerhead_1.SPECIAL_BLANK_PAGE)
|
||
|
throw new runtime_1.APIError(callsiteName, types_1.RUNTIME_ERRORS.unsupportedUrlProtocol, what, url, what, protocol && protocol[0]);
|
||
|
}
|
||
|
function assertBaseUrl(url, callsiteName) {
|
||
|
if (isRelative(url))
|
||
|
throw new runtime_1.APIError(callsiteName, types_1.RUNTIME_ERRORS.relativeBaseUrl, url);
|
||
|
assertProtocol(url, callsiteName, 'base URL');
|
||
|
}
|
||
|
exports.assertBaseUrl = assertBaseUrl;
|
||
|
function assertRoleUrl(url, callsiteName) {
|
||
|
if (isRelative(url))
|
||
|
throw new runtime_1.APIError(callsiteName, types_1.RUNTIME_ERRORS.roleInitializedWithRelativeUrl);
|
||
|
}
|
||
|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