@applitools/execution-grid-client
Advanced tools
Comparing version 1.1.7 to 1.1.8
@@ -57,7 +57,7 @@ "use strict"; | ||
requestLogger.log('Passthrough request'); | ||
return await (0, proxy_1.proxy)(request, response, { target: forwardingUrl }); | ||
return await (0, proxy_1.proxy)({ request, response, options: { target: forwardingUrl }, logger: requestLogger }); | ||
} | ||
} | ||
catch (err) { | ||
logger.error(`Error during processing request:`, err); | ||
requestLogger.error(`Error during processing request:`, err); | ||
response | ||
@@ -84,3 +84,3 @@ .writeHead(500) | ||
async function handleNewSession({ request, response, logger, }) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u; | ||
var _a, _b, _c, _d, _e; | ||
const requestBody = await (0, raw_body_1.default)(request, 'utf-8').then(body => (body ? JSON.parse(body) : undefined)); | ||
@@ -91,11 +91,7 @@ if (!requestBody) | ||
const session = {}; | ||
session.serverUrl = | ||
(_e = (_c = (_b = (_a = requestBody.capabilities) === null || _a === void 0 ? void 0 : _a.alwaysMatch) === null || _b === void 0 ? void 0 : _b['applitools:eyesServerUrl']) !== null && _c !== void 0 ? _c : (_d = requestBody.desiredCapabilities) === null || _d === void 0 ? void 0 : _d['applitools:eyesServerUrl']) !== null && _e !== void 0 ? _e : serverUrl; | ||
session.apiKey = | ||
(_k = (_h = (_g = (_f = requestBody.capabilities) === null || _f === void 0 ? void 0 : _f.alwaysMatch) === null || _g === void 0 ? void 0 : _g['applitools:apiKey']) !== null && _h !== void 0 ? _h : (_j = requestBody.desiredCapabilities) === null || _j === void 0 ? void 0 : _j['applitools:apiKey']) !== null && _k !== void 0 ? _k : apiKey; | ||
session.tunnelId = | ||
((_m = (_l = requestBody.capabilities) === null || _l === void 0 ? void 0 : _l.alwaysMatch) === null || _m === void 0 ? void 0 : _m['applitools:tunnel']) || | ||
((_o = requestBody.desiredCapabilities) === null || _o === void 0 ? void 0 : _o['applitools:tunnel']) | ||
? await createTunnel(session) | ||
: undefined; | ||
session.serverUrl = (_a = extractCapability(requestBody, 'applitools:eyesServerUrl')) !== null && _a !== void 0 ? _a : serverUrl; | ||
session.apiKey = (_b = extractCapability(requestBody, 'applitools:apiKey')) !== null && _b !== void 0 ? _b : apiKey; | ||
session.tunnelId = extractCapability(requestBody, 'applitools:tunnel') ? await createTunnel(session) : undefined; | ||
const timeout = (_c = extractCapability(requestBody, 'applitools:timeout')) !== null && _c !== void 0 ? _c : process.env.APPLITOOLS_EG_TIMEOUT; | ||
const inactivityTimeout = (_d = extractCapability(requestBody, 'applitools:inactivityTimeout')) !== null && _d !== void 0 ? _d : process.env.APPLITOOLS_EG_INACTIVITY_TIMEOUT; | ||
const applitoolsCapabilities = { | ||
@@ -105,7 +101,7 @@ 'applitools:eyesServerUrl': session.serverUrl, | ||
'applitools:x-tunnel-id-0': session.tunnelId, | ||
'applitools:timeout': process.env.APPLITOOLS_EG_TIMEOUT, | ||
'applitools:inactivityTimeout': process.env.APPLITOOLS_EG_INACTIVITY_TIMEOUT, | ||
'applitools:timeout': timeout, | ||
'applitools:inactivityTimeout': inactivityTimeout, | ||
}; | ||
if (((_p = requestBody.capabilities) === null || _p === void 0 ? void 0 : _p.alwaysMatch) || ((_q = requestBody.capabilities) === null || _q === void 0 ? void 0 : _q.firstMatch)) { | ||
requestBody.capabilities.alwaysMatch = Object.assign(Object.assign({}, (_r = requestBody.capabilities) === null || _r === void 0 ? void 0 : _r.alwaysMatch), applitoolsCapabilities); | ||
if (requestBody.capabilities) { | ||
requestBody.capabilities.alwaysMatch = Object.assign(Object.assign({}, (_e = requestBody.capabilities) === null || _e === void 0 ? void 0 : _e.alwaysMatch), applitoolsCapabilities); | ||
} | ||
@@ -116,6 +112,23 @@ if (requestBody.desiredCapabilities) { | ||
logger.log('Request body has modified:', requestBody); | ||
// const queue = queues.get(`${session.serverUrl}:${session.apiKey}`) ?? [] | ||
// queue.push({request, response, requestBody}) | ||
// if (queue.length === 1) { | ||
// queues.set(`${session.serverUrl}:${session.apiKey}`, queue) | ||
// return | ||
// } | ||
return proxyNewSession({ session, requestBody, request, response, logger }); | ||
} | ||
async function proxyNewSession({ session, requestBody, request, response, logger, }) { | ||
var _a, _b, _c, _d; | ||
let attempt = 0; | ||
while (true) { | ||
const proxyResponse = await (0, proxy_1.proxy)(request, response, { target: forwardingUrl, body: requestBody, handle: true }); | ||
const responseBody = await (0, raw_body_1.default)(proxyResponse, 'utf-8').then(body => (body ? JSON.parse(body) : undefined)); | ||
const proxyResponse = await (0, proxy_1.proxy)({ | ||
request, | ||
response, | ||
options: { target: forwardingUrl, body: requestBody, handle: true }, | ||
logger, | ||
}); | ||
const responseBody = | ||
// @ts-ignore | ||
(_a = proxyResponse.body) !== null && _a !== void 0 ? _a : (await (0, raw_body_1.default)(proxyResponse, 'utf-8').then(body => (body ? JSON.parse(body) : undefined))); | ||
if (!responseBody) { | ||
@@ -126,4 +139,4 @@ response.writeHead(proxyResponse.statusCode, proxyResponse.headers).end(); | ||
logger.log(`Response was intercepted with body:`, responseBody); | ||
if (!RETRY_ERROR_CODES.includes((_t = (_s = responseBody.value) === null || _s === void 0 ? void 0 : _s.data) === null || _t === void 0 ? void 0 : _t.appliErrorCode)) { | ||
if ((_u = responseBody.value) === null || _u === void 0 ? void 0 : _u.sessionId) | ||
if (!RETRY_ERROR_CODES.includes((_c = (_b = responseBody.value) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.appliErrorCode)) { | ||
if ((_d = responseBody.value) === null || _d === void 0 ? void 0 : _d.sessionId) | ||
sessions.set(responseBody.value.sessionId, session); | ||
@@ -142,3 +155,3 @@ response.writeHead(proxyResponse.statusCode, proxyResponse.headers).end(JSON.stringify(responseBody)); | ||
logger.log(`Request was intercepted with sessionId:`, sessionId); | ||
await (0, proxy_1.proxy)(request, response, { target: forwardingUrl }); | ||
await (0, proxy_1.proxy)({ request, response, options: { target: forwardingUrl }, logger }); | ||
const session = sessions.get(sessionId); | ||
@@ -151,3 +164,7 @@ if (session.tunnelId) { | ||
} | ||
function extractCapability(data, capabilityName) { | ||
var _a, _b, _c, _d; | ||
return (_c = (_b = (_a = data.capabilities) === null || _a === void 0 ? void 0 : _a.alwaysMatch) === null || _b === void 0 ? void 0 : _b[capabilityName]) !== null && _c !== void 0 ? _c : (_d = data.desiredCapabilities) === null || _d === void 0 ? void 0 : _d[capabilityName]; | ||
} | ||
} | ||
exports.makeServer = makeServer; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.proxy = void 0; | ||
const stream_1 = require("stream"); | ||
const http_proxy_1 = require("http-proxy"); | ||
const stream_1 = require("stream"); | ||
const raw_body_1 = __importDefault(require("raw-body")); | ||
const utils = __importStar(require("@applitools/utils")); | ||
// TODO: get rid of http-proxy library | ||
async function proxy(request, response, options) { | ||
async function proxy({ request, response, options, logger, }) { | ||
const proxy = (0, http_proxy_1.createProxy)(); | ||
return new Promise((resolve, reject) => { | ||
const content = options.body ? JSON.stringify(options.body) : undefined; | ||
const settings = { | ||
target: options.target, | ||
selfHandleResponse: options.handle, | ||
ws: true, | ||
changeOrigin: true, | ||
buffer: content | ||
? new stream_1.Readable({ | ||
read() { | ||
this.push(content); | ||
this.push(null); | ||
}, | ||
}) | ||
: undefined, | ||
headers: content | ||
? Object.assign(Object.assign({}, options.headers), { 'Content-Length': Buffer.byteLength(content).toString() }) : options.headers, | ||
}; | ||
const content = options.body ? JSON.stringify(options.body) : undefined; | ||
const settings = { | ||
target: options.target, | ||
selfHandleResponse: true, | ||
ws: true, | ||
changeOrigin: true, | ||
buffer: content | ||
? new stream_1.Readable({ | ||
read() { | ||
this.push(content); | ||
this.push(null); | ||
}, | ||
}) | ||
: undefined, | ||
headers: content ? Object.assign(Object.assign({}, options.headers), { 'Content-Length': Buffer.byteLength(content).toString() }) : options.headers, | ||
}; | ||
return new Promise((resolve, _reject) => { | ||
// prevent modification of the request headers in the http-proxy library | ||
Object.freeze(request.headers); | ||
proxy.on('proxyRes', proxyResponse => resolve(proxyResponse)); | ||
proxy.web(request, response, settings, reject); | ||
proxy.on('proxyRes', async (proxyResponse) => { | ||
let responseBody; | ||
if (proxyResponse.statusCode >= 400) { | ||
const rawBody = await (0, raw_body_1.default)(proxyResponse, 'utf-8'); | ||
try { | ||
responseBody = rawBody ? JSON.parse(rawBody) : undefined; | ||
} | ||
catch (_a) { | ||
responseBody = null; | ||
} | ||
if (!(responseBody === null || responseBody === void 0 ? void 0 : responseBody.value)) { | ||
logger.error(`Request respond with unexpected status and body (status ${proxyResponse.statusCode})`, rawBody); | ||
logger.log(`Retry on a network error`); | ||
retry(); | ||
return; | ||
} | ||
} | ||
if (!options.handle) { | ||
response.writeHead(proxyResponse.statusCode, proxyResponse.headers); | ||
if (responseBody) | ||
response.end(JSON.stringify(responseBody)); | ||
else | ||
proxyResponse.pipe(response); | ||
} | ||
// @ts-ignore | ||
proxyResponse.body = responseBody; | ||
resolve(proxyResponse); | ||
}); | ||
proxy.web(request, response, settings, err => { | ||
logger.error(`Unexpected error during proxying`, err); | ||
logger.log(`Retry on a unxpected error`); | ||
retry(); | ||
}); | ||
}); | ||
async function retry() { | ||
await utils.general.sleep(3000); | ||
proxy.web(request, response, settings, err => { | ||
logger.error(`Unexpected error during proxying`, err); | ||
logger.log(`Retry on a unxpected error`); | ||
retry(); | ||
}); | ||
} | ||
} | ||
exports.proxy = proxy; |
{ | ||
"name": "@applitools/execution-grid-client", | ||
"version": "1.1.7", | ||
"version": "1.1.8", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "license": "SEE LICENSE IN LICENSE", |
/// <reference types="node" /> | ||
import { type IncomingMessage, type ServerResponse } from 'http'; | ||
import { type Logger } from '@applitools/logger'; | ||
declare type ProxyOptions = { | ||
@@ -9,3 +10,8 @@ target: string; | ||
}; | ||
export declare function proxy(request: IncomingMessage, response: ServerResponse, options: ProxyOptions): Promise<IncomingMessage>; | ||
export declare function proxy({ request, response, options, logger, }: { | ||
request: IncomingMessage; | ||
response: ServerResponse; | ||
options: ProxyOptions; | ||
logger: Logger; | ||
}): Promise<IncomingMessage>; | ||
export {}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39719
15
493