1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-29 16:07:40 +01:00

Version checking + preparing streams

This commit is contained in:
mrjvs 2024-01-09 21:56:39 +01:00
parent f70d13f2c9
commit 421186cb54
6 changed files with 68 additions and 4 deletions

View file

@ -0,0 +1,5 @@
const allowedExtensionVersion = ["0.0.1"];
export function isAllowedExtensionVersion(version: string): boolean {
return allowedExtensionVersion.includes(version);
}

View file

@ -3,6 +3,8 @@ import {
sendToBackgroundViaRelay,
} from "@plasmohq/messaging";
import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
let activeExtension = false;
export interface ExtensionHello {
@ -35,7 +37,14 @@ export async function sendExtensionRequest(
url: string,
ops: any,
): Promise<ExtensionHello | null> {
return sendMessage("proxy-request", { url, ...ops });
return sendMessage("make-request", { url, ...ops });
}
export async function setDomainRule(
domains: string[],
headers: Record<string, string>,
): Promise<ExtensionHello | null> {
return sendMessage("prepare-stream", { domains, headers });
}
export async function extensionInfo(): Promise<ExtensionHello | null> {
@ -47,5 +56,9 @@ export function isExtensionActiveCached(): boolean {
}
export async function isExtensionActive(): Promise<boolean> {
return !!(await extensionInfo());
const info = await extensionInfo();
if (!info) return false;
const allowedVersion = isAllowedExtensionVersion(info.version);
if (!allowedVersion) return false;
return true;
}

View file

@ -20,11 +20,11 @@ export type ExtensionRequestReply =
};
interface MmMetadata {
"declarative-net-request": {
"prepare-stream": {
req: PlasmoRequestBody;
res: ExtensionRequestReply;
};
"proxy-request": {
"make-request": {
req: PlasmoRequestBody;
res: ExtensionRequestReply;
};

View file

@ -0,0 +1,40 @@
import { Stream } from "@movie-web/providers";
import { setDomainRule } from "@/backend/extension/messaging";
function extractDomain(url: string): string {
try {
const u = new URL(url);
return u.hostname;
} catch {
return url;
}
}
function extractDomainsFromStream(stream: Stream): string[] {
if (stream.type === "hls") {
return [extractDomain(stream.playlist)];
}
if (stream.type === "file") {
return Object.values(stream.qualities).map((v) => extractDomain(v.url));
}
return [];
}
function buildHeadersFromStream(stream: Stream): Record<string, string> {
const headers: Record<string, string> = {};
Object.entries(stream.headers ?? {}).forEach((entry) => {
headers[entry[0]] = entry[1];
});
Object.entries(stream.preferredHeaders ?? {}).forEach((entry) => {
headers[entry[0]] = entry[1];
});
return headers;
}
export async function prepareStream(stream: Stream) {
await setDomainRule(
extractDomainsFromStream(stream),
buildHeadersFromStream(stream),
);
}

View file

@ -5,6 +5,7 @@ import {
} from "@movie-web/providers";
import { useAsyncFn } from "react-use";
import { prepareStream } from "@/backend/extension/streams";
import {
connectServerSideEvents,
makeProviderUrl,
@ -131,6 +132,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
]);
if (result.stream) {
await prepareStream(result.stream[0]);
setCaption(null);
setSource(
convertRunoutputToSource({ stream: result.stream[0] }),
@ -187,6 +189,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
]);
setSourceId(sourceId);
setCaption(null);
await prepareStream(embedResult.stream[0]);
setSource(
convertRunoutputToSource({ stream: embedResult.stream[0] }),
convertProviderCaption(embedResult.stream[0].captions),

View file

@ -5,6 +5,7 @@ import {
} from "@movie-web/providers";
import { RefObject, useCallback, useEffect, useRef, useState } from "react";
import { prepareStream } from "@/backend/extension/streams";
import {
connectServerSideEvents,
getCachedMetadata,
@ -169,6 +170,7 @@ export function useScrape() {
conn.on("update", updateEvent);
conn.on("discoverEmbeds", discoverEmbedsEvent);
const sseOutput = await conn.promise();
if (sseOutput) await prepareStream(sseOutput.stream);
return getResult(sseOutput === "" ? null : sseOutput);
}
@ -184,6 +186,7 @@ export function useScrape() {
discoverEmbeds: discoverEmbedsEvent,
},
});
if (output) await prepareStream(output.stream);
return getResult(output);
},
[