diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 7351a6c..1a77b66 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -6,11 +6,12 @@ import { DOMWindow } from "jsdom"; import readability from "./readability"; import google from "./google"; +import { generateProxyUrl } from "../utils"; export default async function handlePage( url: string, - originalUrl: string, - engine?: string + requestUrl: URL, + engine?: string, ): Promise { if (engine && engineList.indexOf(engine) === -1) { @@ -18,16 +19,14 @@ export default async function handlePage( } const response = await axios.get(url); - const window = new JSDOM(response.data, { url: url }).window; - const UrlParsed = new URL(originalUrl); [...window.document.getElementsByTagName("a")].forEach((link) => { - link.href = `${UrlParsed.origin}/get?url=${link.href}${ - engine ? `&engine=${engine}` : "" - }`; + link.href = generateProxyUrl(requestUrl, link.href, engine); }); + // maybe implement image proxy? + if (engine) { return engines[engine](window); } diff --git a/src/routes/get.ts b/src/routes/get.ts index 58db838..227908f 100644 --- a/src/routes/get.ts +++ b/src/routes/get.ts @@ -2,7 +2,7 @@ import { FastifyInstance } from "fastify"; import { GetRequest } from "../types/requests"; import handlePage from "../handlers/main"; -import { generateOriginUrl } from "../utils"; +import { generateRequestUrl } from "../utils"; export default async function getRoute(fastify: FastifyInstance) { fastify.get("/get", async (request: GetRequest, reply) => { @@ -21,7 +21,7 @@ export default async function getRoute(fastify: FastifyInstance) { const parsed = await handlePage( remoteUrl, - generateOriginUrl( + generateRequestUrl( request.protocol, request.hostname, request.originalUrl diff --git a/src/routes/parse.ts b/src/routes/parse.ts index 789ec58..87858ec 100644 --- a/src/routes/parse.ts +++ b/src/routes/parse.ts @@ -1,13 +1,13 @@ import { EngineRequest } from "../types/requests"; import { FastifyInstance } from "fastify"; import handlePage from "../handlers/main"; -import { generateOriginUrl } from "../utils"; +import { generateRequestUrl } from "../utils"; export default async function parseRoute(fastify: FastifyInstance) { fastify.get("/parse", async (request: EngineRequest) => { const parsed = await handlePage( request.query.url, - generateOriginUrl( + generateRequestUrl( request.protocol, request.hostname, request.originalUrl diff --git a/src/utils.ts b/src/utils.ts index cdc4bea..d65f7f8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,18 @@ -export function generateOriginUrl( +export function generateRequestUrl( protocol: string, host: string, - originalUrl: string -): string { - return `${protocol}://${host}${originalUrl}`; + originalUrl: string, +): URL { + return new URL(`${protocol}://${host}${originalUrl}`); +} + +export function generateProxyUrl( + requestUrl: URL, + href: string, + engine?: string, +): string { + const urlParam = `?url=${encodeURIComponent(href)}`; + const engineParam = engine ? `&engine=${engine}` : ""; + // formatParam is not needed for now + return requestUrl.origin + "/get" + urlParam + engineParam; }