diff --git a/src/backend/extension/messaging.ts b/src/backend/extension/messaging.ts index 3c5480c1..28f80ad2 100644 --- a/src/backend/extension/messaging.ts +++ b/src/backend/extension/messaging.ts @@ -7,18 +7,17 @@ import { isAllowedExtensionVersion } from "@/backend/extension/compatibility"; let activeExtension = false; -export interface ExtensionHello { - version: string; -} - -function sendMessage( - message: keyof MessagesMetadata, - payload: any, +function sendMessage( + message: MessageKey, + payload: MessagesMetadata[MessageKey]["req"], timeout: number = -1, ) { - return new Promise((resolve) => { + return new Promise((resolve) => { if (timeout >= 0) setTimeout(() => resolve(null), timeout); - sendToBackgroundViaRelay({ + sendToBackgroundViaRelay< + MessagesMetadata[MessageKey]["req"], + MessagesMetadata[MessageKey]["res"] + >({ name: message, body: payload, }) @@ -34,21 +33,33 @@ function sendMessage( } export async function sendExtensionRequest( - url: string, - ops: any, -): Promise { - return sendMessage("make-request", { url, ...ops }); + ops: Omit, +): Promise { + return sendMessage("makeRequest", { + requestDomain: window.location.origin, + ...ops, + }); } export async function setDomainRule( - domains: string[], - headers: Record, -): Promise { - return sendMessage("prepare-stream", { domains, headers }); + ops: Omit, +): Promise { + return sendMessage("prepareStream", { + requestDomain: window.location.origin, + ...ops, + }); } -export async function extensionInfo(): Promise { - return sendMessage("hello", null, 300); +export async function extensionInfo(): Promise< + MessagesMetadata["hello"]["res"] | null +> { + return sendMessage( + "hello", + { + requestDomain: window.location.origin, + }, + 300, + ); } export function isExtensionActiveCached(): boolean { @@ -57,7 +68,7 @@ export function isExtensionActiveCached(): boolean { export async function isExtensionActive(): Promise { const info = await extensionInfo(); - if (!info) return false; + if (!info?.success) return false; const allowedVersion = isAllowedExtensionVersion(info.version); if (!allowedVersion) return false; return true; diff --git a/src/backend/extension/plasmo.ts b/src/backend/extension/plasmo.ts index 8c0093f0..6c37ad14 100644 --- a/src/backend/extension/plasmo.ts +++ b/src/backend/extension/plasmo.ts @@ -1,6 +1,37 @@ -export interface PlasmoRequestBody { +export interface ExtensionBaseRequest { + requestDomain: string; +} + +export type ExtensionBaseResponse = + | ({ + success: true; + } & T) + | { + success: false; + error: string; + }; + +export type ExtensionHelloResponse = ExtensionBaseResponse<{ + version: string; +}>; + +export interface ExtensionMakeRequest extends ExtensionBaseRequest { + url: string; + method: string; + headers?: Record; + body?: string | FormData | URLSearchParams | Record; +} + +export type ExtensionMakeRequestResponse = ExtensionBaseResponse<{ + status: number; + requestHeaders: Record; + responseHeaders: Record; + data: string | Record; +}>; + +export interface ExtensionPrepareStreamRequest extends ExtensionBaseRequest { ruleId: number; - domain: string; + targetDomains: string[]; requestHeaders?: Record; responseHeaders?: Record; } @@ -9,28 +40,18 @@ export interface ExtensionHelloReply { version: string; } -export type ExtensionRequestReply = - | { - success: true; - ruleId: number; - } - | { - success: false; - error: string; - }; - -interface MmMetadata { - "prepare-stream": { - req: PlasmoRequestBody; - res: ExtensionRequestReply; - }; - "make-request": { - req: PlasmoRequestBody; - res: ExtensionRequestReply; - }; +export interface MmMetadata { hello: { - req: null; - res: ExtensionHelloReply; + req: ExtensionBaseRequest; + res: ExtensionHelloResponse; + }; + makeRequest: { + req: ExtensionMakeRequest; + res: ExtensionMakeRequestResponse; + }; + prepareStream: { + req: ExtensionPrepareStreamRequest; + res: ExtensionBaseResponse; }; } diff --git a/src/backend/extension/streams.ts b/src/backend/extension/streams.ts index 8d202992..2afb900d 100644 --- a/src/backend/extension/streams.ts +++ b/src/backend/extension/streams.ts @@ -33,8 +33,9 @@ function buildHeadersFromStream(stream: Stream): Record { } export async function prepareStream(stream: Stream) { - await setDomainRule( - extractDomainsFromStream(stream), - buildHeadersFromStream(stream), - ); + await setDomainRule({ + ruleId: 1, + targetDomains: extractDomainsFromStream(stream), + requestHeaders: buildHeadersFromStream(stream), + }); } diff --git a/src/backend/providers/fetchers.ts b/src/backend/providers/fetchers.ts index 596e8376..e133199c 100644 --- a/src/backend/providers/fetchers.ts +++ b/src/backend/providers/fetchers.ts @@ -54,8 +54,13 @@ export function makeLoadBalancedSimpleProxyFetcher() { } export function makeExtensionFetcher() { - const fetcher: Fetcher = async (a, b) => { - return sendExtensionRequest(a, b) as any; + const fetcher: Fetcher = async (url, ops) => { + return sendExtensionRequest({ + url, + method: ops?.method ?? "GET", + headers: ops?.headers, + body: ops?.body, + }) as any; }; return fetcher; }