txtdot/src/utils/generate.ts
Artemy Egorov 8f707c800e
Timeout (#70)
* fix: long response time due to many <a>...

... without hrefs. It's a temporary measure until it's clear how to deal with such performance issues.

* perf: remove jsdom install linkedom

* feat: timeout

But still this timeout works only for the time of transfer of the page itself, not its processing by the server

* fix: links

* format
2023-12-13 21:08:24 +04:00

43 lines
1.0 KiB
TypeScript

export function generateRequestUrl(
protocol: string,
host: string,
originalUrl: string
): URL {
return new URL(`${protocol}://${host}${originalUrl}`);
}
export function generateParserUrl(
requestUrl: URL,
remoteUrl: URL,
href: string,
engine?: string,
redirect_url: string = 'get'
): string {
const realURL = getRealURL(href, remoteUrl);
const hash = realURL.hash; // save #hash
realURL.hash = ''; // remove
const urlParam = `?url=${encodeURIComponent(realURL.toString())}`;
const engineParam = engine ? `&engine=${engine}` : '';
return `${requestUrl.origin}/${redirect_url}${urlParam}${engineParam}${hash}`;
}
export function generateProxyUrl(
requestUrl: URL,
remoteUrl: URL,
href: string
): string {
const realHref = getRealURL(href, remoteUrl);
const urlParam = `?url=${encodeURIComponent(realHref.href)}`;
return `${requestUrl.origin}/proxy${urlParam}`;
}
function getRealURL(href: string, remoteUrl: URL) {
return href.startsWith('http')
? new URL(href)
: new URL(href, remoteUrl.href);
}