sprinter-patchright-compat
v0.6.0
Published
Compatibility patch + viewport helper for Patchright CDP connections through Sprinter.
Maintainers
Readme
sprinter-patchright-compat
Compatibility patch for Patchright when connecting over CDP to a Sprinter browser session.
What It Fixes
Sprinter's CDP gateway emits internal traffic with rewritten or unrelated response IDs (for example 1900000016, or success/error frames for the gateway's own Browser.getVersion warmup). Patchright's CRSession._onMessage asserts on any unmatched id-bearing frame, which crashes the process during chromium.connectOverCDP(...) — typically only on the first run, since a second attempt finds the gateway in steady state.
This package rewrites patchright-core/lib/server/chromium/crConnection.js so the CDP client:
- maps Sprinter's
1900000000 + idresponse IDs back to the pending Patchright callback, - silently drops every other id-bearing response (success or error) that Patchright never registered,
- still raises real CDP errors that belong to outstanding Patchright calls.
The wider drop covers all error codes (the previous v0.1 patch only ignored -32001), which is what eliminates the "first run panics, second run works" pattern.
Usage
From a project that has patchright installed:
npx sprinter-patchright-compatFor local development from this checkout:
node bin/sprinter-patchright-compat.js --root /path/to/projectWire it into a consuming project's package.json so reinstalls don't wipe the patch:
{
"scripts": {
"postinstall": "sprinter-patchright-compat"
}
}The patcher is idempotent and will upgrade installs that still carry the v0.1 patch.
Auto Viewport
After running npx sprinter-patchright-compat, every page created on a Patchright BrowserContext is automatically pinned to 1920x947 — Sprinter's expected viewport (1080p Xvfb minus chrome's tab + URL bars). Customer code is unchanged:
const { chromium } = require("patchright");
const browser = await chromium.connectOverCDP(sessionUrl);
const context = browser.contexts()[0] || await browser.newContext();
const page = await context.newPage(); // viewport already 1920x947
await page.goto("https://example.com/");Opt out per-process: SPRINTER_DISABLE_VIEWPORT=1. If a single page needs a different viewport, just call page.setViewportSize(...) after newPage() — the customer's call wins.
For programmatic access (constant, or applying to a page you got via Target.attachedToTarget or another non-newPage path):
const { SPRINTER_VIEWPORT, applyViewport, autoApplyViewport } = require("sprinter-patchright-compat");Smoke Test
const { chromium } = require("patchright");
(async () => {
const browser = await chromium.connectOverCDP("ws://127.0.0.1:19012/devtools/browser");
const context = browser.contexts()[0] || await browser.newContext();
const page = await context.newPage();
await page.goto("https://example.com/", { waitUntil: "domcontentloaded" });
console.log(await page.viewportSize()); // { width: 1920, height: 947 }
console.log(await page.title());
await browser.close();
})();Notes
Both patch sites are runtime modifications of Patchright's generated package files. Treat as narrow compatibility shims until upstream support lands. The patcher is idempotent and self-detects already-patched files.
