Files
RSSHub/lib/utils/playwright-remote.worker.ts
Tony 3bc1cc248d chore: bump to msw v3 (#23463)
* chore: bump to msw v3

* refactor: anti-slop

* test: use vi.stubEnv for REDIS_URL in redis error test

* refactor: move remote debugging registration into a conditional block

* chore: add code coverage upload for github
2026-10-03 06:36:19 +08:00

33 lines
1.4 KiB
TypeScript

import type { BrowserType } from 'patchright';
import { getPlaywrightCore } from '../../assets/build/playwright-worker.mjs';
import { installNativeTransport, type TransportFetch, withTransportFetch } from './playwright-transport.worker';
export type PlaywrightService = { fetch: TransportFetch };
let playwrightService: PlaywrightService | undefined;
let serviceOrigin: string | undefined;
let clientPromise: Promise<BrowserType> | undefined;
export const setPlaywrightServiceBinding = (binding?: PlaywrightService, origin?: string) => {
playwrightService = binding;
serviceOrigin = origin;
};
const getClient = () =>
(clientPromise ??= (async () => {
const core = await getPlaywrightCore();
installNativeTransport(core.server);
return core.inprocess.playwright.chromium;
})());
export const connectRemotePlaywright = async (endpoint: string) => {
const url = new URL(endpoint);
url.protocol = url.protocol === 'wss:' ? 'https:' : url.protocol === 'ws:' ? 'http:' : url.protocol;
// Only route credentials through a service binding when its configured origin matches.
const binding = playwrightService;
const useBinding = binding && serviceOrigin && url.origin === new URL(serviceOrigin).origin;
const client = await getClient();
const connect = () => client.connect(endpoint, { timeout: 20000 });
return useBinding ? withTransportFetch(binding.fetch.bind(binding), connect) : connect();
};