Created HandlerInput class

This commit is contained in:
DarkCat09 2023-09-11 12:59:19 +04:00
parent 62d408ef98
commit 3a7abd93d9
No known key found for this signature in database
GPG Key ID: 0A26CD5B3345D6E3
3 changed files with 59 additions and 19 deletions

View File

@ -0,0 +1,46 @@
import { JSDOM } from "jsdom";
import { generateProxyUrl } from "../utils/generate";
export class HandlerInput {
private data: string;
private url: string;
private requestUrl: URL;
private engine?: string;
private redirectPath: string;
constructor(
data: string,
url: string,
requestUrl: URL,
engine?: string,
redirectPath: string = "get",
) {
this.data = data;
this.url = url;
this.requestUrl = requestUrl;
this.engine = engine;
this.redirectPath = redirectPath;
}
parseDom() {
const dom = new JSDOM(this.data, { url: this.url });
const links = dom.window.document.getElementsByTagName("a");
for (const link of links) {
try {
link.href = generateProxyUrl(
this.requestUrl,
link.href,
this.engine,
this.redirectPath,
);
} catch (_err) {
// ignore TypeError: Invalid URL
}
}
}
getUrl() {
return this.url;
}
}

View File

@ -14,12 +14,13 @@ import { generateProxyUrl } from "../utils/generate";
import isLocalResource from "../utils/islocal"; import isLocalResource from "../utils/islocal";
import { LocalResourceError, NotHtmlMimetypeError } from "../errors/main"; import { LocalResourceError, NotHtmlMimetypeError } from "../errors/main";
import { HandlerInput } from "./handler-input";
export default async function handlePage( export default async function handlePage(
url: string, // remote URL url: string, // remote URL
requestUrl: URL, // proxy URL requestUrl: URL, // proxy URL
engine?: string, engine?: string,
redirect_path: string = "get", redirectPath: string = "get",
): Promise<IHandlerOutput> { ): Promise<IHandlerOutput> {
const urlObj = new URL(url); const urlObj = new URL(url);
@ -34,22 +35,15 @@ export default async function handlePage(
throw new NotHtmlMimetypeError(url); throw new NotHtmlMimetypeError(url);
} }
const window = new JSDOM(response.data, { url }).window; return getFallbackEngine(urlObj.hostname, engine)(
new HandlerInput(
[...window.document.getElementsByTagName("a")].forEach((link) => { response.data,
try { url,
link.href = generateProxyUrl(
requestUrl, requestUrl,
link.href,
engine, engine,
redirect_path, redirectPath,
)
); );
} catch (_err) {
// ignore TypeError: Invalid URL
}
});
return getFallbackEngine(urlObj.hostname, engine)(window);
} }
function getFallbackEngine(host: string, specified?: string): EngineFunction { function getFallbackEngine(host: string, specified?: string): EngineFunction {

View File

@ -1,4 +1,4 @@
import { DOMWindow } from "jsdom"; import { HandlerInput } from "../handlers/handler-input";
import { IHandlerOutput } from "../handlers/handler.interface"; import { IHandlerOutput } from "../handlers/handler.interface";
export interface Engines { export interface Engines {
@ -10,5 +10,5 @@ export type EngineMatch = {
engine: EngineFunction; engine: EngineFunction;
}; };
export type EngineFunction = (window: DOMWindow) => Promise<IHandlerOutput>; export type EngineFunction = (input: HandlerInput) => Promise<IHandlerOutput>;
export type EnginesMatch = EngineMatch[]; export type EnginesMatch = EngineMatch[];