Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions bin/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
};
4 changes: 4 additions & 0 deletions docs/guide/core-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
32 changes: 32 additions & 0 deletions tests/Integration/Bridge/OutputBridgeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Playwright\Tests\Integration\Bridge;

use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

#[CoversNothing]
final class OutputBridgeTest extends TestCase
{
public function testBridgeBoundariesAndResourceLifetimes(): void
{
$process = new Process(['node', '--test', __DIR__.'/output.test.cjs']);
$process->setTimeout(30);
$process->run();

$this->assertSame(0, $process->getExitCode(), $process->getOutput().$process->getErrorOutput());
}
}
28 changes: 28 additions & 0 deletions tests/Integration/Bridge/output.test.cjs
Original file line number Diff line number Diff line change
@@ -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/);
});
Loading