From bc1f6272d8a551e809669ca95610afcb98c928cd Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 25 Aug 2023 16:13:46 -0700 Subject: [PATCH] chore: deflate large web socket messages (#26720) --- .../playwright-core/src/remote/playwrightServer.ts | 8 +++++++- packages/playwright-core/src/server/transport.ts | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/playwright-core/src/remote/playwrightServer.ts b/packages/playwright-core/src/remote/playwrightServer.ts index 384b3cbda1..ac84722522 100644 --- a/packages/playwright-core/src/remote/playwrightServer.ts +++ b/packages/playwright-core/src/remote/playwrightServer.ts @@ -28,6 +28,7 @@ import type { AndroidDevice } from '../server/android/android'; import type { SocksProxy } from '../common/socksProxy'; import { debugLogger } from '../common/debugLogger'; import { createHttpServer } from '../utils'; +import { perMessageDeflate } from '../server/transport'; let lastConnectionId = 0; const kConnectionSymbol = Symbol('kConnection'); @@ -82,7 +83,11 @@ export class PlaywrightServer { }); debugLogger.log('server', 'Listening at ' + wsEndpoint); - this._wsServer = new wsServer({ server, path: this._options.path }); + this._wsServer = new wsServer({ + server, + path: this._options.path, + perMessageDeflate, + }); const browserSemaphore = new Semaphore(this._options.maxConnections); const controllerSemaphore = new Semaphore(1); const reuseBrowserSemaphore = new Semaphore(1); @@ -92,6 +97,7 @@ export class PlaywrightServer { }); } this._wsServer.on('connection', (ws, request) => { + debugLogger.log('server', 'Connected client ws.extension=' + ws.extensions); const url = new URL('http://localhost' + (request.url || '')); const browserHeader = request.headers['x-playwright-browser']; const browserName = url.searchParams.get('browser') || (Array.isArray(browserHeader) ? browserHeader[0] : browserHeader) || null; diff --git a/packages/playwright-core/src/server/transport.ts b/packages/playwright-core/src/server/transport.ts index 7b164a2e4f..7fcdb92c12 100644 --- a/packages/playwright-core/src/server/transport.ts +++ b/packages/playwright-core/src/server/transport.ts @@ -23,6 +23,16 @@ import { makeWaitForNextTask } from '../utils'; import { httpHappyEyeballsAgent, httpsHappyEyeballsAgent } from '../utils/happy-eyeballs'; import type { HeadersArray } from './types'; +export const perMessageDeflate = { + zlibDeflateOptions: { + level: 3, + }, + zlibInflateOptions: { + chunkSize: 10 * 1024 + }, + threshold: 10 * 1024, +}; + export type ProtocolRequest = { id: number; method: string; @@ -117,13 +127,13 @@ export class WebSocketTransport implements ConnectionTransport { this.wsEndpoint = url; this._logUrl = logUrl; this._ws = new ws(url, [], { - perMessageDeflate: false, maxPayload: 256 * 1024 * 1024, // 256Mb, // Prevent internal http client error when passing negative timeout. handshakeTimeout: Math.max(progress?.timeUntilDeadline() ?? 30_000, 1), headers, followRedirects, - agent: (/^(https|wss):\/\//.test(url)) ? httpsHappyEyeballsAgent : httpHappyEyeballsAgent + agent: (/^(https|wss):\/\//.test(url)) ? httpsHappyEyeballsAgent : httpHappyEyeballsAgent, + perMessageDeflate, }); this._ws.on('upgrade', response => { for (let i = 0; i < response.rawHeaders.length; i += 2) {