diff --git a/bin/lib/core.js b/bin/lib/core.js index ae07dff..52a8e60 100644 --- a/bin/lib/core.js +++ b/bin/lib/core.js @@ -146,10 +146,36 @@ class LspFraming { } } +const MAX_QUEUED_EVENT_BYTES = 16 * 1024 * 1024; + +class FramedWriter { + constructor(stream) { + this.stream = stream; + this.queuedEventBytes = 0; + } + + write(data) { + const framed = LspFraming.encode(JSON.stringify(data)); + const eventBytes = data.event ? Buffer.byteLength(framed) : 0; + + if (this.queuedEventBytes + eventBytes > MAX_QUEUED_EVENT_BYTES) { + throw new Error('Browser event output exceeded the 16 MiB queue limit.'); + } + + this.queuedEventBytes += eventBytes; + this.stream.write(framed, () => { this.queuedEventBytes -= eventBytes; }); + } +} + +const output = new FramedWriter(process.stdout); + const sendFramedResponse = (data) => { - const json = JSON.stringify(data); - const framed = LspFraming.encode(json); - process.stdout.write(framed); + try { + output.write(data); + } catch { + process.stderr.write('Playwright bridge output failed or exceeded its event queue limit.\n'); + process.exit(1); + } }; class CommandRegistry { @@ -378,6 +404,6 @@ class RouteUtils { const logger = new Logger(); module.exports = { - logger, ErrorHandler, LspFraming, sendFramedResponse, + logger, ErrorHandler, LspFraming, FramedWriter, sendFramedResponse, CommandRegistry, NavigationRedirects, BaseHandler, PromiseUtils, FrameUtils, RouteUtils, }; diff --git a/docs/guide/core-concepts.md b/docs/guide/core-concepts.md index afa1ac0..fa4c844 100644 --- a/docs/guide/core-concepts.md +++ b/docs/guide/core-concepts.md @@ -94,3 +94,7 @@ $signInButton->click(); By understanding and using these four core concepts, you can build powerful, reliable, and maintainable browser automation scripts and tests. + +## Pending browser events + +The bridge permits up to 16 MiB of pending browser-event output. It exits with an explicit error if the PHP client stops consuming events and the limit is exceeded. Large screenshot, PDF, and response-body command results do not count toward that event limit. diff --git a/tests/Integration/Bridge/OutputBridgeTest.php b/tests/Integration/Bridge/OutputBridgeTest.php new file mode 100644 index 0000000..3d695e4 --- /dev/null +++ b/tests/Integration/Bridge/OutputBridgeTest.php @@ -0,0 +1,32 @@ +setTimeout(30); + $process->run(); + + $this->assertSame(0, $process->getExitCode(), $process->getOutput().$process->getErrorOutput()); + } +} diff --git a/tests/Integration/Bridge/output.test.cjs b/tests/Integration/Bridge/output.test.cjs new file mode 100644 index 0000000..9f89c49 --- /dev/null +++ b/tests/Integration/Bridge/output.test.cjs @@ -0,0 +1,28 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { spawnSync } = require('node:child_process'); +const { FramedWriter } = require('../../../bin/lib/core'); + +test('event buffering is bounded while large command results remain supported', () => { + const callbacks = []; + const output = new FramedWriter({ write: (_frame, callback) => callbacks.push(callback) }); + const content = 'x'.repeat(10 * 1024 * 1024); + output.write({ event: 'console', params: { text: content } }); + assert.throws(() => output.write({ event: 'console', params: { text: content } }), /queue limit/); + assert.doesNotThrow(() => output.write({ binary: content + content })); + callbacks.splice(0).forEach(callback => callback()); + assert.doesNotThrow(() => output.write({ event: 'console', params: { text: content } })); +}); + +test('event overflow terminates the bridge explicitly', () => { + const core = require.resolve('../../../bin/lib/core'); + const result = spawnSync(process.execPath, ['-e', ` + process.stdout.write = () => false; + const { sendFramedResponse } = require(${JSON.stringify(core)}); + const event = { event: 'console', params: { text: 'x'.repeat(10 * 1024 * 1024) } }; + sendFramedResponse(event); + sendFramedResponse(event); + `], { encoding: 'utf8' }); + assert.equal(result.status, 1); + assert.match(result.stderr, /event queue limit/); +});