Innovenergy_trunk/frontend/node_modules/testcafe/lib/browser/connection/gateway.js

205 lines
37 KiB
JavaScript
Raw Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const load_assets_1 = __importDefault(require("../../load-assets"));
const http_1 = require("../../utils/http");
const remotes_queue_1 = __importDefault(require("./remotes-queue"));
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
const service_routes_1 = __importDefault(require("./service-routes"));
const empty_page_markup_1 = __importDefault(require("../../proxyless/empty-page-markup"));
const error_route_1 = __importDefault(require("../../proxyless/error-route"));
class BrowserConnectionGateway {
constructor(proxy, options) {
this._connections = {};
this._remotesQueue = new remotes_queue_1.default();
this.connectUrl = proxy.resolveRelativeServiceUrl(service_routes_1.default.connect);
this.retryTestPages = options.retryTestPages;
this.proxyless = options.proxyless;
this.proxy = proxy;
this._registerRoutes(proxy);
}
_dispatch(url, proxy, handler, method = 'GET', shouldAcceptCrossOrigin) {
// @ts-ignore Need to improve typings of the 'testcafe-hammerhead' module
proxy[method](url, (req, res, serverInfo, params) => {
const connection = this._connections[params.id];
(0, http_1.preventCaching)(res);
if (shouldAcceptCrossOrigin)
(0, testcafe_hammerhead_1.acceptCrossOrigin)(res);
if (connection)
handler(req, res, connection);
else
(0, http_1.respond404)(res);
});
}
_registerRoutes(proxy) {
const { idlePageScript, idlePageStyle, idlePageLogo, serviceWorkerScript, } = (0, load_assets_1.default)();
this._dispatch(`${service_routes_1.default.connect}/{id}`, proxy, BrowserConnectionGateway._onConnection);
this._dispatch(`${service_routes_1.default.heartbeat}/{id}`, proxy, BrowserConnectionGateway._onHeartbeat, 'GET', this.proxyless);
this._dispatch(`${service_routes_1.default.idle}/{id}`, proxy, BrowserConnectionGateway._onIdle);
this._dispatch(`${service_routes_1.default.idleForced}/{id}`, proxy, BrowserConnectionGateway._onIdleForced);
this._dispatch(`${service_routes_1.default.status}/{id}`, proxy, BrowserConnectionGateway._onStatusRequest);
this._dispatch(`${service_routes_1.default.statusDone}/{id}`, proxy, BrowserConnectionGateway._onStatusRequestOnTestDone, 'GET', this.proxyless);
this._dispatch(`${service_routes_1.default.initScript}/{id}`, proxy, BrowserConnectionGateway._onInitScriptRequest);
this._dispatch(`${service_routes_1.default.initScript}/{id}`, proxy, BrowserConnectionGateway._onInitScriptResponse, 'POST');
this._dispatch(`${service_routes_1.default.activeWindowId}/{id}`, proxy, BrowserConnectionGateway._onGetActiveWindowIdRequest, 'GET', this.proxyless);
this._dispatch(`${service_routes_1.default.activeWindowId}/{id}`, proxy, BrowserConnectionGateway._onSetActiveWindowIdRequest, 'POST', this.proxyless);
this._dispatch(`${service_routes_1.default.closeWindow}/{id}`, proxy, BrowserConnectionGateway._onCloseWindowRequest, 'POST');
this._dispatch(`${service_routes_1.default.openFileProtocol}/{id}`, proxy, BrowserConnectionGateway._onOpenFileProtocolRequest, 'POST');
this._dispatch(`${service_routes_1.default.dispatchProxylessEvent}/{id}`, proxy, BrowserConnectionGateway._onDispatchProxylessEvent, 'POST', this.proxyless);
proxy.GET(service_routes_1.default.connect, (req, res) => this._connectNextRemoteBrowser(req, res));
proxy.GET(service_routes_1.default.connectWithTrailingSlash, (req, res) => this._connectNextRemoteBrowser(req, res));
proxy.GET(service_routes_1.default.serviceWorker, { content: serviceWorkerScript, contentType: 'application/x-javascript' });
proxy.GET(service_routes_1.default.assets.index, { content: idlePageScript, contentType: 'application/x-javascript' });
proxy.GET(service_routes_1.default.assets.styles, { content: idlePageStyle, contentType: 'text/css' });
proxy.GET(service_routes_1.default.assets.logo, { content: idlePageLogo, contentType: 'image/svg+xml' });
if (this.proxyless) {
proxy.GET(error_route_1.default, (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(empty_page_markup_1.default);
});
}
}
// Helpers
static _ensureConnectionReady(res, connection) {
if (!connection.isReady()) {
(0, http_1.respond500)(res, 'The connection is not ready yet.');
return false;
}
return true;
}
static _fetchRequestData(req, callback) {
let data = '';
req.on('data', chunk => {
data += chunk;
});
req.on('end', () => {
callback(data.toString());
});
}
// Route handlers
static async _onConnection(req, res, connection) {
if (connection.isReady())
(0, http_1.respond500)(res, 'The connection is already established.');
else {
const userAgent = req.headers['user-agent'];
await connection.establish(userAgent);
(0, http_1.redirect)(res, connection.idleUrl);
}
}
static _onHeartbeat(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
const status = connection.heartbeat();
(0, http_1.respondWithJSON)(res, status);
}
}
static _onIdle(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection))
res.end(connection.renderIdlePage());
}
static async _onIdleForced(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
const status = await connection.getStatus(true);
(0, http_1.redirect)(res, status.url);
}
}
static async _onStatusRequest(req, res, connection) {
return BrowserConnectionGateway._onStatusRequestCore(req, res, connection, false);
}
static async _onStatusRequestOnTestDone(req, res, connection) {
return BrowserConnectionGateway._onStatusRequestCore(req, res, connection, true);
}
static async _onStatusRequestCore(req, res, connection, isTestDone) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
const status = await connection.getStatus(isTestDone);
(0, http_1.respondWithJSON)(res, status);
}
}
static _onInitScriptRequest(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
const script = connection.getInitScript();
(0, http_1.respondWithJSON)(res, script);
}
}
static _onInitScriptResponse(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
BrowserConnectionGateway._fetchRequestData(req, data => {
connection.handleInitScriptResult(data);
res.end();
});
}
}
static _onGetActiveWindowIdRequest(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
(0, http_1.respondWithJSON)(res, {
activeWindowId: connection.activeWindowId,
});
}
}
static _onSetActiveWindowIdRequest(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
BrowserConnectionGateway._fetchRequestData(req, data => {
const parsedData = JSON.parse(data);
connection.activeWindowId = parsedData.windowId;
(0, http_1.respondWithJSON)(res);
});
}
}
static _onCloseWindowRequest(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
connection.provider.closeBrowserChildWindow(connection.id)
.then(() => {
(0, http_1.respondWithJSON)(res);
});
}
}
static _onOpenFileProtocolRequest(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
BrowserConnectionGateway._fetchRequestData(req, data => {
const parsedData = JSON.parse(data);
connection.openFileProtocol(parsedData.url)
.then(() => {
(0, http_1.respondWithJSON)(res);
});
});
}
}
static _onDispatchProxylessEvent(req, res, connection) {
if (BrowserConnectionGateway._ensureConnectionReady(res, connection)) {
BrowserConnectionGateway._fetchRequestData(req, data => {
const { type, options } = JSON.parse(data);
connection.dispatchProxylessEvent(type, options)
.then(() => {
(0, http_1.respondWithJSON)(res);
});
});
}
}
async _connectNextRemoteBrowser(req, res) {
(0, http_1.preventCaching)(res);
const remoteConnection = await this._remotesQueue.shift();
if (remoteConnection)
(0, http_1.redirect)(res, remoteConnection.url);
else
(0, http_1.respond500)(res, 'There are no available _connections to establish.');
}
// API
startServingConnection(connection) {
this._connections[connection.id] = connection;
if (connection.browserInfo.providerName === 'remote')
this._remotesQueue.add(connection);
}
stopServingConnection(connection) {
delete this._connections[connection.id];
if (connection.browserInfo.providerName === 'remote')
this._remotesQueue.remove(connection);
}
async close() {
for (const id in this._connections)
await this._connections[id].close();
}
}
exports.default = BrowserConnectionGateway;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2F0ZXdheS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9icm93c2VyL2Nvbm5lY3Rpb24vZ2F0ZXdheS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLG9FQUEyQztBQUMzQywyQ0F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