Custom exceptions, redirect in case of non-html content

This commit is contained in:
DarkCat09 2023-08-16 13:17:47 +04:00
parent 676147772f
commit ebc0552169
No known key found for this signature in database
GPG Key ID: 4785B6FB1C50A540
5 changed files with 41 additions and 23 deletions

3
src/errors.ts Normal file
View File

@ -0,0 +1,3 @@
export class EngineParseError extends Error {}
export class InvalidParameterError extends Error {}
export class NotHtmlMimetypeError extends Error {}

View File

@ -9,7 +9,9 @@ export default async function google(
);
if (!googleAnchors) {
throw new Error("Failed to find anchors in search result [google]");
throw new EngineParseError(
"Failed to find anchors in search result [google]"
);
}
const results = [...googleAnchors];

View File

@ -8,6 +8,8 @@ import readability from "./readability";
import google from "./google";
import { generateProxyUrl } from "../utils";
import { InvalidParameterError, NotHtmlMimetypeError } from "../errors";
export default async function handlePage(
url: string,
requestUrl: URL,
@ -15,18 +17,24 @@ export default async function handlePage(
): Promise<IHandlerOutput> {
if (engine && engineList.indexOf(engine) === -1) {
throw new Error("Invalid engine");
throw new InvalidParameterError("Invalid engine");
}
const response = await axios.get(url);
const mime: string | undefined = (
response.headers["content-type"]?.toString()
);
if (mime && mime.indexOf("text/html") === -1) {
throw new NotHtmlMimetypeError();
}
const window = new JSDOM(response.data, { url: url }).window;
[...window.document.getElementsByTagName("a")].forEach((link) => {
link.href = generateProxyUrl(requestUrl, link.href, engine);
});
// maybe implement image proxy?
if (engine) {
return engines[engine](window);
}

View File

@ -9,7 +9,7 @@ export default async function readability(
const parsed = reader.parse();
if (!parsed) {
throw new Error("Failed to parse [readability]");
throw new EngineParseError("Failed to parse [readability]");
}
return {

View File

@ -4,34 +4,39 @@ import { GetRequest } from "../types/requests";
import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils";
import { NotHtmlMimetypeError } from "../errors";
export default async function getRoute(fastify: FastifyInstance) {
fastify.get("/get", async (request: GetRequest, reply) => {
const remoteUrl = request.query.url;
const engine = request.query.engine;
let format: string;
let parsed;
try {
parsed = await handlePage(
remoteUrl,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
engine
);
}
catch (err) {
if (err instanceof NotHtmlMimetypeError) {
return reply.redirect(301, remoteUrl);
}
else {
throw err;
}
}
if (request.query.format === "text") {
reply.type("text/plain; charset=utf-8");
format = "text";
} else {
reply.type("text/html; charset=utf-8");
format = "html";
}
const parsed = await handlePage(
remoteUrl,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
engine
);
if (format === "text") {
return parsed.textContent;
} else {
reply.type("text/html; charset=utf-8");
return reply.view("/templates/get.ejs", { parsed: parsed });
}
});