mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-11-28 04:00:19 +08:00
* style: use cloudflare/oxlint-config * style: fix no-known-value-widening * style: fix typescript/only-throw-error * style: fix typescript/no-floating-promises * style: fix typescript/use-unknown-in-catch-callback-variable * style: fix typescript/no-misused-promises * style: fix typescript/no-unsafe-unary-minus * style: fix typescript/no-for-in-array * style: fix react/purity * style: fix typescript/switch-exhaustiveness-check * style: fix typescript/unbound-method * style: fix react/rules-of-hooks * style: fix typescript/return-await * style: fix react/button-has-type * style: fix react/no-unescaped-entities * style: fix import/no-namespace * style: fix typescript/no-import-type-side-effects * style: fix no-promise-executor-return * style: fix no-useless-assignment * style: fix typescript/no-misused-spread * style: fix react/jsx-no-target-blank * style: fix typescript/await-thenable * style: fix typescript/no-unnecessary-type-assertion * style: fix import/consistent-type-specifier-style * style: fix react/no-unknown-property * style: remove unused disable directive * style: merge region * style: enable prefer-smaller-scope * style: add react/no-danger * style: add unicorn-js/prefer-uint8array-base64/hex * style: move non-recommended unicorn rules to custom region
57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import type { BrowserWorker } from '@cloudflare/playwright';
|
|
import type { DurableObjectState } from '@cloudflare/workers-types';
|
|
|
|
async function settle(operation: Promise<unknown>) {
|
|
try {
|
|
await operation;
|
|
} catch {
|
|
// Each caller observes its own failure; later requests can retry acquisition.
|
|
}
|
|
}
|
|
|
|
// Store only the session ID: the browser can outlive both a request and this object.
|
|
export class BrowserSession {
|
|
private sessionId?: string;
|
|
private ready: Promise<void>;
|
|
private queue: Promise<void> = Promise.resolve();
|
|
|
|
constructor(
|
|
private state: DurableObjectState,
|
|
private env: { BROWSER: BrowserWorker }
|
|
) {
|
|
this.ready = state.blockConcurrencyWhile(async () => {
|
|
this.sessionId = await state.storage.get<string>('sessionId');
|
|
});
|
|
}
|
|
|
|
async fetch(request: Request) {
|
|
if (request.method !== 'POST' || new URL(request.url).pathname !== '/session') {
|
|
return new Response('Use POST /session.', { status: 405 });
|
|
}
|
|
const body = (await request.json()) as { invalidSessionId?: unknown };
|
|
if (body.invalidSessionId !== undefined && typeof body.invalidSessionId !== 'string') {
|
|
return new Response('invalidSessionId must be a string.', { status: 400 });
|
|
}
|
|
const invalidSessionId = body.invalidSessionId;
|
|
// Serializing acquisition also coalesces simultaneous reports of an expired ID.
|
|
const queued = this.queue;
|
|
const operation = (async () => {
|
|
await queued;
|
|
await this.ready;
|
|
if (invalidSessionId && this.sessionId === invalidSessionId) {
|
|
this.sessionId = undefined;
|
|
await this.state.storage.delete('sessionId');
|
|
}
|
|
if (!this.sessionId) {
|
|
const { acquire } = await import('@cloudflare/playwright');
|
|
const { sessionId } = await acquire(this.env.BROWSER, { keep_alive: 60000 });
|
|
await this.state.storage.put('sessionId', sessionId);
|
|
this.sessionId = sessionId;
|
|
}
|
|
return this.sessionId;
|
|
})();
|
|
this.queue = settle(operation);
|
|
return Response.json({ sessionId: await operation });
|
|
}
|
|
}
|