mirror of
https://github.com/txtdot/txtdot
synced 2024-10-18 14:40:19 +03:00
Created HandlerInput class
This commit is contained in:
parent
62d408ef98
commit
3a7abd93d9
46
src/handlers/handler-input.ts
Normal file
46
src/handlers/handler-input.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -14,12 +14,13 @@ import { generateProxyUrl } from "../utils/generate";
|
||||
import isLocalResource from "../utils/islocal";
|
||||
|
||||
import { LocalResourceError, NotHtmlMimetypeError } from "../errors/main";
|
||||
import { HandlerInput } from "./handler-input";
|
||||
|
||||
export default async function handlePage(
|
||||
url: string, // remote URL
|
||||
requestUrl: URL, // proxy URL
|
||||
engine?: string,
|
||||
redirect_path: string = "get",
|
||||
redirectPath: string = "get",
|
||||
): Promise<IHandlerOutput> {
|
||||
const urlObj = new URL(url);
|
||||
|
||||
@ -34,22 +35,15 @@ export default async function handlePage(
|
||||
throw new NotHtmlMimetypeError(url);
|
||||
}
|
||||
|
||||
const window = new JSDOM(response.data, { url }).window;
|
||||
|
||||
[...window.document.getElementsByTagName("a")].forEach((link) => {
|
||||
try {
|
||||
link.href = generateProxyUrl(
|
||||
return getFallbackEngine(urlObj.hostname, engine)(
|
||||
new HandlerInput(
|
||||
response.data,
|
||||
url,
|
||||
requestUrl,
|
||||
link.href,
|
||||
engine,
|
||||
redirect_path,
|
||||
redirectPath,
|
||||
)
|
||||
);
|
||||
} catch (_err) {
|
||||
// ignore TypeError: Invalid URL
|
||||
}
|
||||
});
|
||||
|
||||
return getFallbackEngine(urlObj.hostname, engine)(window);
|
||||
}
|
||||
|
||||
function getFallbackEngine(host: string, specified?: string): EngineFunction {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { DOMWindow } from "jsdom";
|
||||
import { HandlerInput } from "../handlers/handler-input";
|
||||
import { IHandlerOutput } from "../handlers/handler.interface";
|
||||
|
||||
export interface Engines {
|
||||
@ -10,5 +10,5 @@ export type EngineMatch = {
|
||||
engine: EngineFunction;
|
||||
};
|
||||
|
||||
export type EngineFunction = (window: DOMWindow) => Promise<IHandlerOutput>;
|
||||
export type EngineFunction = (input: HandlerInput) => Promise<IHandlerOutput>;
|
||||
export type EnginesMatch = EngineMatch[];
|
||||
|
Loading…
Reference in New Issue
Block a user