Innovenergy_trunk/frontend/node_modules/testcafe/lib/api/test-page-url.js

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);
}
exports.assertRoleUrl = assertRoleUrl;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC1wYWdlLXVybC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9hcGkvdGVzdC1wYWdlLXVybC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQSw2QkFBeUM7QUFDekMsMERBQTJCO0FBQzNCLCtDQUE2QztBQUM3QywyQ0FBaUQ7QUFDakQsNkRBQXlEO0FBQ3pELCtCQUE0QjtBQUU1QixNQUFNLFdBQVcsR0FBYSxxQkFBcUIsQ0FBQztBQUNwRCxNQUFNLHFCQUFxQixHQUFHLGlCQUFpQixDQUFDO0FBQ2hELE1BQU0sb0JBQW9CLEdB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