@applitools/execution-grid-client
Advanced tools
Comparing version 1.1.11 to 1.1.12
@@ -32,3 +32,3 @@ "use strict"; | ||
const proxy_1 = require("./proxy"); | ||
const parse_body_1 = require("./parse-body"); | ||
const incoming_message_1 = require("./incoming-message"); | ||
const utils = __importStar(require("@applitools/utils")); | ||
@@ -46,8 +46,3 @@ const RETRY_BACKOFF = [].concat(Array(5).fill(2000), // 5 tries with delay 2s (total 10s) | ||
shouldRetry: async (proxyResponse) => { | ||
var _a; | ||
if (proxyResponse.statusCode <= 400) | ||
return false; | ||
//@ts-ignore | ||
proxyResponse.body = await (0, parse_body_1.parseBody)(proxyResponse); | ||
return !((_a = proxyResponse.body) === null || _a === void 0 ? void 0 : _a.value); | ||
return proxyResponse.statusCode >= 500 && !utils.types.has(await proxyResponse.json(), 'value'); | ||
}, | ||
@@ -58,3 +53,4 @@ }); | ||
const queues = new Map(); | ||
const server = (0, http_1.createServer)(async (request, response) => { | ||
const server = (0, http_1.createServer)(async (message, response) => { | ||
const request = (0, incoming_message_1.modifyIncomingMessage)(message); | ||
const requestLogger = logger.extend({ | ||
@@ -99,4 +95,4 @@ tags: { request: `[${request.method}] ${request.url}`, requestId: utils.general.guid() }, | ||
async function createSession({ request, response, logger, }) { | ||
var _a, _b, _c, _d, _e; | ||
const requestBody = await (0, parse_body_1.parseBody)(request); | ||
var _a, _b, _c, _d, _e, _f; | ||
const requestBody = await request.json(); | ||
logger.log(`Request was intercepted with body:`, requestBody); | ||
@@ -107,2 +103,3 @@ const session = {}; | ||
session.tunnelId = extractCapability(requestBody, 'applitools:tunnel') ? await createTunnel(session) : undefined; | ||
session.key = `${(_c = session.eyesServerUrl) !== null && _c !== void 0 ? _c : 'default'}:${session.apiKey}`; | ||
const applitoolsCapabilities = { | ||
@@ -112,7 +109,7 @@ 'applitools:eyesServerUrl': session.eyesServerUrl, | ||
'applitools:x-tunnel-id-0': session.tunnelId, | ||
'applitools:timeout': (_c = extractCapability(requestBody, 'applitools:timeout')) !== null && _c !== void 0 ? _c : egTimeout, | ||
'applitools:inactivityTimeout': (_d = extractCapability(requestBody, 'applitools:inactivityTimeout')) !== null && _d !== void 0 ? _d : egInactivityTimeout, | ||
'applitools:timeout': (_d = extractCapability(requestBody, 'applitools:timeout')) !== null && _d !== void 0 ? _d : egTimeout, | ||
'applitools:inactivityTimeout': (_e = extractCapability(requestBody, 'applitools:inactivityTimeout')) !== null && _e !== void 0 ? _e : egInactivityTimeout, | ||
}; | ||
if (requestBody.capabilities) { | ||
requestBody.capabilities.alwaysMatch = Object.assign(Object.assign({}, (_e = requestBody.capabilities) === null || _e === void 0 ? void 0 : _e.alwaysMatch), applitoolsCapabilities); | ||
requestBody.capabilities.alwaysMatch = Object.assign(Object.assign({}, (_f = requestBody.capabilities) === null || _f === void 0 ? void 0 : _f.alwaysMatch), applitoolsCapabilities); | ||
} | ||
@@ -123,11 +120,11 @@ if (requestBody.desiredCapabilities) { | ||
logger.log('Request body has modified:', requestBody); | ||
let queue = queues.get(`${session.eyesServerUrl}:${session.apiKey}`); | ||
let queue = queues.get(session.key); | ||
if (!queue) { | ||
queue = (0, queue_1.makeQueue)({ logger }); | ||
queues.set(`${session.eyesServerUrl}:${session.apiKey}`, queue); | ||
queue = (0, queue_1.makeQueue)({ logger: logger.extend({ tags: { queue: session.key } }) }); | ||
queues.set(session.key, queue); | ||
} | ||
request.socket.on('close', () => queue.cancel(task)); | ||
await queue.run(task); | ||
async function task(signal, attempt = 0) { | ||
var _a, _b, _c, _d; | ||
async function task(signal, attempt = 1) { | ||
var _a, _b, _c; | ||
// do not start the task if it is already aborted | ||
@@ -142,6 +139,5 @@ if (signal.aborted) | ||
}); | ||
// to decide if we get an expected response we might already parse the body | ||
const responseBody = (_a = proxyResponse.body) !== null && _a !== void 0 ? _a : (await (0, parse_body_1.parseBody)(proxyResponse)); | ||
const responseBody = await proxyResponse.json(); | ||
logger.log(`Response was intercepted with body:`, responseBody); | ||
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 (RETRY_ERROR_CODES.includes((_b = (_a = responseBody.value) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.appliErrorCode)) { | ||
queue.cork(); | ||
@@ -152,3 +148,3 @@ // after query is corked the task might be aborted | ||
await utils.general.sleep(RETRY_BACKOFF[Math.min(attempt, RETRY_BACKOFF.length - 1)]); | ||
logger.log(`Retrying sending the request (attempt ${attempt})`); | ||
logger.log(`Attempt (${attempt}) to create session was failed with applitools status code:`, responseBody.value.data.appliErrorCode); | ||
return task(signal, attempt + 1); | ||
@@ -158,5 +154,5 @@ } | ||
queue.uncork(); | ||
if ((_d = responseBody.value) === null || _d === void 0 ? void 0 : _d.sessionId) | ||
if ((_c = responseBody.value) === null || _c === void 0 ? void 0 : _c.sessionId) | ||
sessions.set(responseBody.value.sessionId, session); | ||
response.end(JSON.stringify(responseBody)); | ||
proxyResponse.pipe(response); | ||
return; | ||
@@ -163,0 +159,0 @@ } |
@@ -30,2 +30,3 @@ "use strict"; | ||
exports.makeProxy = void 0; | ||
const incoming_message_1 = require("./incoming-message"); | ||
const http_1 = require("http"); | ||
@@ -90,13 +91,5 @@ const https_1 = require("https"); | ||
request.on('error', reject); | ||
request.on('response', resolve); | ||
request.on('response', response => resolve((0, incoming_message_1.modifyIncomingMessage)(response))); | ||
if (requestOptions.body && utils.types.isFunction(requestOptions.body, 'pipe')) { | ||
const chunks = []; | ||
requestOptions.body.on('data', chunk => { | ||
chunks.push(chunk); | ||
request.write(chunk); | ||
}); | ||
requestOptions.body.on('end', () => { | ||
requestOptions.body = Buffer.concat(chunks); | ||
request.end(); | ||
}); | ||
requestOptions.body.pipe(request); | ||
} | ||
@@ -103,0 +96,0 @@ else { |
@@ -29,3 +29,3 @@ "use strict"; | ||
const utils = __importStar(require("@applitools/utils")); | ||
function makeQueue(_options) { | ||
function makeQueue({ logger }) { | ||
const pool = []; | ||
@@ -54,2 +54,3 @@ const map = new Map(); | ||
if (handle.running) { | ||
map.delete(task); | ||
pool.splice(pool.indexOf(handle), 1); | ||
@@ -79,2 +80,3 @@ handle.resolve(result); | ||
map.set(task, handle); | ||
logger.log('Task was added to the queue'); | ||
if (!corked) | ||
@@ -86,3 +88,3 @@ handle.start(); | ||
const handle = map.get(task); | ||
if (!handle) | ||
if (!(handle === null || handle === void 0 ? void 0 : handle.running)) | ||
return; | ||
@@ -92,2 +94,3 @@ handle.abort(); | ||
pool.splice(pool.indexOf(handle), 1); | ||
logger.log('Task was cancelled'); | ||
} | ||
@@ -99,2 +102,3 @@ function cork() { | ||
pool.slice(1).forEach(handle => handle.abort()); | ||
logger.log('Queue was corked'); | ||
} | ||
@@ -106,4 +110,5 @@ function uncork() { | ||
pool.forEach(handle => handle.start()); | ||
logger.log('Queue was uncorked'); | ||
} | ||
} | ||
exports.makeQueue = makeQueue; |
{ | ||
"name": "@applitools/execution-grid-client", | ||
"version": "1.1.11", | ||
"version": "1.1.12", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "license": "SEE LICENSE IN LICENSE", |
@@ -5,3 +5,4 @@ /// <reference types="node" /> | ||
import { type Logger } from '@applitools/logger'; | ||
import { type IncomingMessage, type ServerResponse } from 'http'; | ||
import { type ModifiedIncomingMessage } from './incoming-message'; | ||
import { type ServerResponse } from 'http'; | ||
declare type RequestOptions = { | ||
@@ -18,3 +19,3 @@ url?: URL | string; | ||
modifyRequest?: (options: RequestOptions) => Promise<RequestOptions> | RequestOptions; | ||
shouldRetry?: (proxyResponse: IncomingMessage) => Promise<boolean> | boolean; | ||
shouldRetry?: (proxyResponse: ModifiedIncomingMessage) => Promise<boolean> | boolean; | ||
retryTimeout?: number; | ||
@@ -25,7 +26,7 @@ }; | ||
}): ({ request, response, options, logger, }: { | ||
request: IncomingMessage; | ||
request: ModifiedIncomingMessage; | ||
response: ServerResponse; | ||
options?: ProxyOptions; | ||
logger: Logger; | ||
}) => Promise<IncomingMessage>; | ||
}) => Promise<ModifiedIncomingMessage>; | ||
export {}; |
@@ -11,5 +11,5 @@ import { type Logger } from '@applitools/logger'; | ||
declare type Task<TResult> = (signal: AbortSignal) => Promise<TResult>; | ||
export declare function makeQueue(_options: { | ||
export declare function makeQueue({ logger }: { | ||
logger: Logger; | ||
}): Queue; | ||
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
49211
723