refactor: pass around the requested url so we have context to merge it absolute with the path
This commit is contained in:
parent
6c5ec99dff
commit
f4294ce880
|
@ -7,11 +7,19 @@ export class ContentProcessor {
|
||||||
this.filters.push(filter);
|
this.filters.push(filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
async process(response: Response, mimeType: string): Promise<Response> {
|
async process(
|
||||||
|
response: Response,
|
||||||
|
mimeType: string,
|
||||||
|
requestor: string,
|
||||||
|
): Promise<Response> {
|
||||||
let processedResponse = response;
|
let processedResponse = response;
|
||||||
|
|
||||||
for (const filter of this.filters) {
|
for (const filter of this.filters) {
|
||||||
processedResponse = await filter.process(processedResponse, mimeType);
|
processedResponse = await filter.process(
|
||||||
|
processedResponse,
|
||||||
|
mimeType,
|
||||||
|
requestor,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return processedResponse;
|
return processedResponse;
|
||||||
|
|
|
@ -4,8 +4,14 @@ import tldEnum from "@lumeweb/tld-enum";
|
||||||
import * as cheerio from "cheerio";
|
import * as cheerio from "cheerio";
|
||||||
import urlJoin from "proper-url-join";
|
import urlJoin from "proper-url-join";
|
||||||
|
|
||||||
|
const swUrl = new URL(self.location.origin);
|
||||||
|
|
||||||
export default class URLRewriteFilter implements ContentFilter {
|
export default class URLRewriteFilter implements ContentFilter {
|
||||||
async process(response: Response, mimeType: string): Promise<Response> {
|
async process(
|
||||||
|
response: Response,
|
||||||
|
mimeType: string,
|
||||||
|
requestor: string,
|
||||||
|
): Promise<Response> {
|
||||||
if (mimeType !== "text/html") {
|
if (mimeType !== "text/html") {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
@ -24,15 +30,9 @@ export default class URLRewriteFilter implements ContentFilter {
|
||||||
const isExternal = urlValue.startsWith("http");
|
const isExternal = urlValue.startsWith("http");
|
||||||
if (!isExternal || !isICANN(urlValue)) {
|
if (!isExternal || !isICANN(urlValue)) {
|
||||||
if (!isExternal) {
|
if (!isExternal) {
|
||||||
//@ts-ignore
|
urlValue = urlJoin(requestor, urlValue);
|
||||||
urlValue = urlJoin("/browse/", urlValue);
|
|
||||||
} else {
|
|
||||||
urlValue = `/browse/${urlValue}`;
|
|
||||||
}
|
}
|
||||||
|
urlValue = `${swUrl.protocol}://${swUrl.hostname}/browse/${urlValue}`;
|
||||||
console.log(urlValue);
|
|
||||||
|
|
||||||
$(element).attr(attrName, urlValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -15,7 +15,11 @@ export class ProviderManager {
|
||||||
this.providers.push(provider);
|
this.providers.push(provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch(dnsResult: DNSResult, path: string): Promise<Response> {
|
async fetch(
|
||||||
|
url: string,
|
||||||
|
dnsResult: DNSResult,
|
||||||
|
path: string,
|
||||||
|
): Promise<Response> {
|
||||||
for (const record of dnsResult.records) {
|
for (const record of dnsResult.records) {
|
||||||
for (const provider of this.providers) {
|
for (const provider of this.providers) {
|
||||||
if (provider.supports(record.value)) {
|
if (provider.supports(record.value)) {
|
||||||
|
@ -25,6 +29,7 @@ export class ProviderManager {
|
||||||
return this._processor.process(
|
return this._processor.process(
|
||||||
content,
|
content,
|
||||||
content.headers.get("Content-Type")!,
|
content.headers.get("Content-Type")!,
|
||||||
|
url,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,5 +8,9 @@ export interface ContentProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ContentFilter {
|
export interface ContentFilter {
|
||||||
process: (response: Response, mineType: string) => Promise<Response>;
|
process: (
|
||||||
|
response: Response,
|
||||||
|
mineType: string,
|
||||||
|
requestor: string,
|
||||||
|
) => Promise<Response>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,11 @@ addEventListener("fetch", (event: any) => {
|
||||||
console.log("realUrl", realUrl);
|
console.log("realUrl", realUrl);
|
||||||
|
|
||||||
if (!dnsResult.error && dnsResult.records.length > 0) {
|
if (!dnsResult.error && dnsResult.records.length > 0) {
|
||||||
return providerManager.fetch(dnsResult, new URL(realUrl).pathname);
|
return providerManager.fetch(
|
||||||
|
realUrl,
|
||||||
|
dnsResult,
|
||||||
|
new URL(realUrl).pathname,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response("Sorry, that is not a valid web3 website.");
|
return new Response("Sorry, that is not a valid web3 website.");
|
||||||
|
|
Loading…
Reference in New Issue