Innovenergy_trunk/frontend/node_modules/testcafe/lib/custom-client-scripts/client-script.js

108 lines
15 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const promisified_functions_1 = require("../utils/promisified-functions");
const runtime_1 = require("../errors/runtime");
const types_1 = require("../errors/types");
const path_1 = require("path");
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
const crypto_1 = require("crypto");
const BEAUTIFY_REGEXP = /[/.:\s\\]/g;
const BEAUTIFY_CHAR = '_';
const EMPTY_CONTENT_STR = '{ content: <empty> }';
const CONTENT_STR_MAX_LENGTH = 30;
const CONTENT_ELLIPSIS_STR = '...';
const URL_UNIQUE_PART_LENGTH = 7;
class ClientScript {
constructor(init, basePath) {
this.init = init || null;
this.url = (0, testcafe_hammerhead_1.generateUniqueId)(URL_UNIQUE_PART_LENGTH);
this.content = '';
this.path = null;
this.module = null;
this.hash = null;
this.page = testcafe_hammerhead_1.RequestFilterRule.ANY;
this.basePath = basePath;
}
_resolvePath(path) {
let resolvedPath = null;
if ((0, path_1.isAbsolute)(path))
resolvedPath = path;
else {
if (!this.basePath)
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.clientScriptBasePathIsNotSpecified);
resolvedPath = (0, path_1.join)(this.basePath, path);
}
return resolvedPath;
}
async _loadFromPath(path) {
const resolvedPath = this._resolvePath(path);
try {
this.path = resolvedPath;
this.content = await (0, promisified_functions_1.readFile)(this.path);
this.content = this.content.toString();
this.url = path || this.url;
}
catch (e) {
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.cannotLoadClientScriptFromPath, path, e.message);
}
}
async _loadFromModule(name) {
let resolvedPath = null;
try {
resolvedPath = require.resolve(name);
}
catch (e) {
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.clientScriptModuleEntryPointPathCalculationError, e.message);
}
await this._loadFromPath(resolvedPath);
this.module = name;
}
_prepareUrl() {
this.url = this.url.replace(BEAUTIFY_REGEXP, BEAUTIFY_CHAR).toLowerCase();
}
async load() {
if (this.init === null)
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.clientScriptInitializerIsNotSpecified);
else if (typeof this.init === 'string')
await this._loadFromPath(this.init);
else {
const { path: initPath, content: initContent, module: initModule, page: initPage } = this.init;
if (initPath && initContent || initPath && initModule || initContent && initModule)
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.clientScriptInitializerMultipleContentSources);
if (initPath)
await this._loadFromPath(initPath);
else if (initModule)
await this._loadFromModule(initModule);
else
this.content = initContent;
if (initPage)
this.page = new testcafe_hammerhead_1.RequestFilterRule(initPage);
}
this._calculateHash();
this._prepareUrl();
}
_calculateHash() {
this.hash = (0, crypto_1.createHash)('md5').update(this.content).digest();
}
_contentToString() {
let displayContent;
if (this.content.length <= CONTENT_STR_MAX_LENGTH - CONTENT_ELLIPSIS_STR.length)
displayContent = this.content;
else
displayContent = this.content.substring(0, CONTENT_STR_MAX_LENGTH - CONTENT_ELLIPSIS_STR.length) + CONTENT_ELLIPSIS_STR;
return `{ content: '${displayContent}' }`;
}
toString() {
if (!this.content)
return EMPTY_CONTENT_STR;
else if (this.content && !this.path)
return this._contentToString();
return `{ path: '${this.path}' }`;
}
static get URL_UNIQUE_PART_LENGTH() {
return URL_UNIQUE_PART_LENGTH;
}
}
exports.default = ClientScript;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xpZW50LXNjcmlwdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jdXN0b20tY2xpZW50LXNjcmlwdHMvY2xpZW50LXNjcmlwdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBFQUEwRDtBQUMxRCwrQ0FBaUQ7QUFDakQsMkNBQWlEO0FBQ2pELCtCQUF3QztBQUN4Qyw2REFBMEU7QUFDMUUsbUNBQW9DO0FBR3BDLE1BQU0sZUFBZSxHQUFH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