Custom exceptions, redirect in case of non-html content
This commit is contained in:
parent
676147772f
commit
ebc0552169
3
src/errors.ts
Normal file
3
src/errors.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export class EngineParseError extends Error {}
|
||||||
|
export class InvalidParameterError extends Error {}
|
||||||
|
export class NotHtmlMimetypeError extends Error {}
|
@ -9,7 +9,9 @@ export default async function google(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!googleAnchors) {
|
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];
|
const results = [...googleAnchors];
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@ import readability from "./readability";
|
|||||||
import google from "./google";
|
import google from "./google";
|
||||||
import { generateProxyUrl } from "../utils";
|
import { generateProxyUrl } from "../utils";
|
||||||
|
|
||||||
|
import { InvalidParameterError, NotHtmlMimetypeError } from "../errors";
|
||||||
|
|
||||||
export default async function handlePage(
|
export default async function handlePage(
|
||||||
url: string,
|
url: string,
|
||||||
requestUrl: URL,
|
requestUrl: URL,
|
||||||
@ -15,18 +17,24 @@ export default async function handlePage(
|
|||||||
): Promise<IHandlerOutput> {
|
): Promise<IHandlerOutput> {
|
||||||
|
|
||||||
if (engine && engineList.indexOf(engine) === -1) {
|
if (engine && engineList.indexOf(engine) === -1) {
|
||||||
throw new Error("Invalid engine");
|
throw new InvalidParameterError("Invalid engine");
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.get(url);
|
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;
|
const window = new JSDOM(response.data, { url: url }).window;
|
||||||
|
|
||||||
[...window.document.getElementsByTagName("a")].forEach((link) => {
|
[...window.document.getElementsByTagName("a")].forEach((link) => {
|
||||||
link.href = generateProxyUrl(requestUrl, link.href, engine);
|
link.href = generateProxyUrl(requestUrl, link.href, engine);
|
||||||
});
|
});
|
||||||
|
|
||||||
// maybe implement image proxy?
|
|
||||||
|
|
||||||
if (engine) {
|
if (engine) {
|
||||||
return engines[engine](window);
|
return engines[engine](window);
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ export default async function readability(
|
|||||||
const parsed = reader.parse();
|
const parsed = reader.parse();
|
||||||
|
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
throw new Error("Failed to parse [readability]");
|
throw new EngineParseError("Failed to parse [readability]");
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -4,22 +4,16 @@ import { GetRequest } from "../types/requests";
|
|||||||
import handlePage from "../handlers/main";
|
import handlePage from "../handlers/main";
|
||||||
import { generateRequestUrl } from "../utils";
|
import { generateRequestUrl } from "../utils";
|
||||||
|
|
||||||
|
import { NotHtmlMimetypeError } from "../errors";
|
||||||
|
|
||||||
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) => {
|
||||||
const remoteUrl = request.query.url;
|
const remoteUrl = request.query.url;
|
||||||
const engine = request.query.engine;
|
const engine = request.query.engine;
|
||||||
|
|
||||||
let format: string;
|
let parsed;
|
||||||
|
try {
|
||||||
if (request.query.format === "text") {
|
parsed = await handlePage(
|
||||||
reply.type("text/plain; charset=utf-8");
|
|
||||||
format = "text";
|
|
||||||
} else {
|
|
||||||
reply.type("text/html; charset=utf-8");
|
|
||||||
format = "html";
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsed = await handlePage(
|
|
||||||
remoteUrl,
|
remoteUrl,
|
||||||
generateRequestUrl(
|
generateRequestUrl(
|
||||||
request.protocol,
|
request.protocol,
|
||||||
@ -28,10 +22,21 @@ export default async function getRoute(fastify: FastifyInstance) {
|
|||||||
),
|
),
|
||||||
engine
|
engine
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof NotHtmlMimetypeError) {
|
||||||
|
return reply.redirect(301, remoteUrl);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (format === "text") {
|
if (request.query.format === "text") {
|
||||||
|
reply.type("text/plain; charset=utf-8");
|
||||||
return parsed.textContent;
|
return parsed.textContent;
|
||||||
} else {
|
} else {
|
||||||
|
reply.type("text/html; charset=utf-8");
|
||||||
return reply.view("/templates/get.ejs", { parsed: parsed });
|
return reply.view("/templates/get.ejs", { parsed: parsed });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user