mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-18 22:32:10 +08:00
3bc1cc248d
* 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
33 lines
1.4 KiB
TypeScript
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();
|
|
};
|