From 739d029339f3426eb6f6953e325427958a75b697 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sat, 26 Aug 2023 11:46:36 +0300 Subject: [PATCH 1/2] refactor: routes --- src/app.ts | 8 ++++---- src/routes/{ => api}/parse.ts | 6 +++--- src/routes/{ => api}/raw-html.ts | 6 +++--- src/routes/{ => browser}/get.ts | 6 +++--- src/routes/{ => browser}/index.ts | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) rename src/routes/{ => api}/parse.ts (73%) rename src/routes/{ => api}/raw-html.ts (72%) rename src/routes/{ => browser}/get.ts (81%) rename src/routes/{ => browser}/index.ts (69%) diff --git a/src/app.ts b/src/app.ts index 266cdc5..65f5a5d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,10 +9,10 @@ import fastifySwagger from "@fastify/swagger"; import fastifySwaggerUi from "@fastify/swagger-ui"; import ejs from "ejs"; -import getRoute from "./routes/get"; -import parseRoute from "./routes/parse"; -import indexRoute from "./routes/index"; -import rawHtml from "./routes/raw-html"; +import getRoute from "./routes/browser/get"; +import parseRoute from "./routes/api/parse"; +import indexRoute from "./routes/browser/index"; +import rawHtml from "./routes/api/raw-html"; import publicConfig from "./publicConfig"; import errorHandler from "./errors/handler"; diff --git a/src/routes/parse.ts b/src/routes/api/parse.ts similarity index 73% rename from src/routes/parse.ts rename to src/routes/api/parse.ts index bca7d1d..5d51014 100644 --- a/src/routes/parse.ts +++ b/src/routes/api/parse.ts @@ -1,9 +1,9 @@ import { FastifyInstance } from "fastify"; -import { EngineRequest, IParseSchema, parseSchema } from "../types/requests/api"; +import { EngineRequest, IParseSchema, parseSchema } from "../../types/requests/api"; -import handlePage from "../handlers/main"; -import { generateRequestUrl } from "../utils/generate"; +import handlePage from "../../handlers/main"; +import { generateRequestUrl } from "../../utils/generate"; export default async function parseRoute(fastify: FastifyInstance) { fastify.get( diff --git a/src/routes/raw-html.ts b/src/routes/api/raw-html.ts similarity index 72% rename from src/routes/raw-html.ts rename to src/routes/api/raw-html.ts index 32ccfe5..b0f44e2 100644 --- a/src/routes/raw-html.ts +++ b/src/routes/api/raw-html.ts @@ -1,9 +1,9 @@ import { FastifyInstance } from "fastify"; -import { IParseSchema, rawHtmlSchema } from "../types/requests/api"; +import { IParseSchema, rawHtmlSchema } from "../../types/requests/api"; -import handlePage from "../handlers/main"; -import { generateRequestUrl } from "../utils/generate"; +import handlePage from "../../handlers/main"; +import { generateRequestUrl } from "../../utils/generate"; export default async function rawHtml(fastify: FastifyInstance) { fastify.get( diff --git a/src/routes/get.ts b/src/routes/browser/get.ts similarity index 81% rename from src/routes/get.ts rename to src/routes/browser/get.ts index 795b7e2..ef94428 100644 --- a/src/routes/get.ts +++ b/src/routes/browser/get.ts @@ -1,8 +1,8 @@ import { FastifyInstance } from "fastify"; -import { GetSchema, IGetSchema } from "../types/requests/browser"; -import handlePage from "../handlers/main"; -import { generateRequestUrl } from "../utils/generate"; +import { GetSchema, IGetSchema } from "../../types/requests/browser"; +import handlePage from "../../handlers/main"; +import { generateRequestUrl } from "../../utils/generate"; export default async function getRoute(fastify: FastifyInstance) { fastify.get( diff --git a/src/routes/index.ts b/src/routes/browser/index.ts similarity index 69% rename from src/routes/index.ts rename to src/routes/browser/index.ts index 00d6dba..e9f4c5f 100644 --- a/src/routes/index.ts +++ b/src/routes/browser/index.ts @@ -1,6 +1,6 @@ import { FastifyInstance } from "fastify"; -import { engineList } from "../handlers/main"; -import { indexSchema } from "../types/requests/browser"; +import { engineList } from "../../handlers/main"; +import { indexSchema } from "../../types/requests/browser"; export default async function indexRoute(fastify: FastifyInstance) { fastify.get("/", { schema: indexSchema }, async (_, reply) => { From a3567cfb3410fab272ea18c8f11b1075e8b34754 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sat, 26 Aug 2023 12:00:07 +0300 Subject: [PATCH 2/2] fix: raw html links and engine --- src/handlers/main.ts | 5 +++-- src/routes/api/raw-html.ts | 7 +++++-- src/utils/generate.ts | 5 +++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 7a37f5b..46ce881 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -19,7 +19,8 @@ import { export default async function handlePage( url: string, // remote URL requestUrl: URL, // proxy URL - engine?: string + engine?: string, + redirect_path: string = "get" ): Promise { const urlObj = new URL(url); @@ -38,7 +39,7 @@ export default async function handlePage( [...window.document.getElementsByTagName("a")].forEach((link) => { try { - link.href = generateProxyUrl(requestUrl, link.href, engine); + link.href = generateProxyUrl(requestUrl, link.href, engine, redirect_path); } catch (_err) { // ignore TypeError: Invalid URL } diff --git a/src/routes/api/raw-html.ts b/src/routes/api/raw-html.ts index b0f44e2..b9d9de5 100644 --- a/src/routes/api/raw-html.ts +++ b/src/routes/api/raw-html.ts @@ -9,7 +9,8 @@ export default async function rawHtml(fastify: FastifyInstance) { fastify.get( "/api/raw-html", { schema: rawHtmlSchema }, - async (request) => { + async (request, reply) => { + reply.type("text/html; charset=utf-8"); return ( await handlePage( request.query.url, @@ -17,7 +18,9 @@ export default async function rawHtml(fastify: FastifyInstance) { request.protocol, request.hostname, request.originalUrl - ) + ), + request.query.engine, + "api/raw-html" ) ).content; } diff --git a/src/utils/generate.ts b/src/utils/generate.ts index 36412e0..4d5a006 100644 --- a/src/utils/generate.ts +++ b/src/utils/generate.ts @@ -9,7 +9,8 @@ export function generateRequestUrl( export function generateProxyUrl( requestUrl: URL, href: string, - engine?: string + engine?: string, + redirect_url: string = "get" ): string { const parsedHref = new URL(href); @@ -19,5 +20,5 @@ export function generateProxyUrl( const urlParam = `?url=${encodeURIComponent(parsedHref.toString())}`; const engineParam = engine ? `&engine=${engine}` : ""; - return `${requestUrl.origin}/get${urlParam}${engineParam}${hash}`; + return `${requestUrl.origin}/${redirect_url}${urlParam}${engineParam}${hash}`; }