revert back to makeRequest

This commit is contained in:
Jorrin 2024-01-12 18:46:51 +01:00
parent ccbf888946
commit a4b925dc1b
3 changed files with 12 additions and 57 deletions

View File

@ -34,7 +34,7 @@ function buildHeadersFromStream(stream: Stream): Record<string, string> {
export async function prepareStream(stream: Stream) { export async function prepareStream(stream: Stream) {
await setDomainRule({ await setDomainRule({
ruleId: 2, ruleId: 1,
targetDomains: extractDomainsFromStream(stream), targetDomains: extractDomainsFromStream(stream),
requestHeaders: buildHeadersFromStream(stream), requestHeaders: buildHeadersFromStream(stream),
}); });

View File

@ -1,11 +1,9 @@
import { Fetcher, makeSimpleProxyFetcher } from "@movie-web/providers"; import { Fetcher, makeSimpleProxyFetcher } from "@movie-web/providers";
import { setDomainRule } from "@/backend/extension/messaging"; import { sendExtensionRequest } from "@/backend/extension/messaging";
import { getApiToken, setApiToken } from "@/backend/helpers/providerApi"; import { getApiToken, setApiToken } from "@/backend/helpers/providerApi";
import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls"; import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls";
import { makeFullUrl } from "./utils";
function makeLoadbalancedList(getter: () => string[]) { function makeLoadbalancedList(getter: () => string[]) {
let listIndex = -1; let listIndex = -1;
return () => { return () => {
@ -69,31 +67,17 @@ function makeFinalHeaders(
export function makeExtensionFetcher() { export function makeExtensionFetcher() {
const fetcher: Fetcher = async (url, ops) => { const fetcher: Fetcher = async (url, ops) => {
const fullUrl = makeFullUrl(url, ops); const result = (await sendExtensionRequest<any>({
const res = await setDomainRule({ url,
ruleId: 1, ...ops,
targetDomains: [fullUrl], })) as any;
requestHeaders: ops.headers, if (!result?.success) throw new Error(`extension error: ${result?.error}`);
}); const res = result.response;
console.log(res, fullUrl);
const response = await fetch(fullUrl, {
method: ops.method,
headers: ops.headers,
body: ops.body as any,
});
const contentType = response.headers.get("content-type");
const body = contentType?.includes("application/json")
? await response.json()
: await response.text();
return { return {
body, body: res.body,
finalUrl: response.url, finalUrl: res.finalUrl,
statusCode: response.status, statusCode: res.statusCode,
headers: makeFinalHeaders( headers: makeFinalHeaders(ops.readHeaders, res.headers),
ops.readHeaders,
Object.fromEntries(response.headers.entries()),
),
}; };
}; };
return fetcher; return fetcher;

View File

@ -1,29 +0,0 @@
import { DefaultedFetcherOptions } from "@movie-web/providers";
export function makeFullUrl(
url: string,
ops?: DefaultedFetcherOptions,
): string {
// glue baseUrl and rest of url together
let leftSide = ops?.baseUrl ?? "";
let rightSide = url;
// left side should always end with slash, if its set
if (leftSide.length > 0 && !leftSide.endsWith("/")) leftSide += "/";
// right side should never start with slash
if (rightSide.startsWith("/")) rightSide = rightSide.slice(1);
const fullUrl = leftSide + rightSide;
if (!fullUrl.startsWith("http://") && !fullUrl.startsWith("https://"))
throw new Error(
`Invald URL -- URL doesn't start with a http scheme: '${fullUrl}'`,
);
const parsedUrl = new URL(fullUrl);
Object.entries(ops?.query ?? {}).forEach(([k, v]) => {
parsedUrl.searchParams.set(k, v as string);
});
return parsedUrl.toString();
}