Small code cleanup, generateRequestUrl
This commit is contained in:
parent
6a0eda16d6
commit
367565a799
@ -6,11 +6,12 @@ import { DOMWindow } from "jsdom";
|
|||||||
|
|
||||||
import readability from "./readability";
|
import readability from "./readability";
|
||||||
import google from "./google";
|
import google from "./google";
|
||||||
|
import { generateProxyUrl } from "../utils";
|
||||||
|
|
||||||
export default async function handlePage(
|
export default async function handlePage(
|
||||||
url: string,
|
url: string,
|
||||||
originalUrl: string,
|
requestUrl: URL,
|
||||||
engine?: string
|
engine?: string,
|
||||||
): Promise<IHandlerOutput> {
|
): Promise<IHandlerOutput> {
|
||||||
|
|
||||||
if (engine && engineList.indexOf(engine) === -1) {
|
if (engine && engineList.indexOf(engine) === -1) {
|
||||||
@ -18,16 +19,14 @@ export default async function handlePage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.get(url);
|
const response = await axios.get(url);
|
||||||
|
|
||||||
const window = new JSDOM(response.data, { url: url }).window;
|
const window = new JSDOM(response.data, { url: url }).window;
|
||||||
const UrlParsed = new URL(originalUrl);
|
|
||||||
|
|
||||||
[...window.document.getElementsByTagName("a")].forEach((link) => {
|
[...window.document.getElementsByTagName("a")].forEach((link) => {
|
||||||
link.href = `${UrlParsed.origin}/get?url=${link.href}${
|
link.href = generateProxyUrl(requestUrl, link.href, engine);
|
||||||
engine ? `&engine=${engine}` : ""
|
|
||||||
}`;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// maybe implement image proxy?
|
||||||
|
|
||||||
if (engine) {
|
if (engine) {
|
||||||
return engines[engine](window);
|
return engines[engine](window);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import { FastifyInstance } from "fastify";
|
|||||||
|
|
||||||
import { GetRequest } from "../types/requests";
|
import { GetRequest } from "../types/requests";
|
||||||
import handlePage from "../handlers/main";
|
import handlePage from "../handlers/main";
|
||||||
import { generateOriginUrl } from "../utils";
|
import { generateRequestUrl } from "../utils";
|
||||||
|
|
||||||
export default async function getRoute(fastify: FastifyInstance) {
|
export default async function getRoute(fastify: FastifyInstance) {
|
||||||
fastify.get("/get", async (request: GetRequest, reply) => {
|
fastify.get("/get", async (request: GetRequest, reply) => {
|
||||||
@ -21,7 +21,7 @@ export default async function getRoute(fastify: FastifyInstance) {
|
|||||||
|
|
||||||
const parsed = await handlePage(
|
const parsed = await handlePage(
|
||||||
remoteUrl,
|
remoteUrl,
|
||||||
generateOriginUrl(
|
generateRequestUrl(
|
||||||
request.protocol,
|
request.protocol,
|
||||||
request.hostname,
|
request.hostname,
|
||||||
request.originalUrl
|
request.originalUrl
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import { EngineRequest } from "../types/requests";
|
import { EngineRequest } from "../types/requests";
|
||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
import handlePage from "../handlers/main";
|
import handlePage from "../handlers/main";
|
||||||
import { generateOriginUrl } from "../utils";
|
import { generateRequestUrl } from "../utils";
|
||||||
|
|
||||||
export default async function parseRoute(fastify: FastifyInstance) {
|
export default async function parseRoute(fastify: FastifyInstance) {
|
||||||
fastify.get("/parse", async (request: EngineRequest) => {
|
fastify.get("/parse", async (request: EngineRequest) => {
|
||||||
const parsed = await handlePage(
|
const parsed = await handlePage(
|
||||||
request.query.url,
|
request.query.url,
|
||||||
generateOriginUrl(
|
generateRequestUrl(
|
||||||
request.protocol,
|
request.protocol,
|
||||||
request.hostname,
|
request.hostname,
|
||||||
request.originalUrl
|
request.originalUrl
|
||||||
|
19
src/utils.ts
19
src/utils.ts
@ -1,7 +1,18 @@
|
|||||||
export function generateOriginUrl(
|
export function generateRequestUrl(
|
||||||
protocol: string,
|
protocol: string,
|
||||||
host: string,
|
host: string,
|
||||||
originalUrl: string
|
originalUrl: string,
|
||||||
): string {
|
): URL {
|
||||||
return `${protocol}://${host}${originalUrl}`;
|
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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user