From f4294ce88069c0107af22a28bb8430815b167b21 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 18 Oct 2023 07:20:36 -0400 Subject: [PATCH] refactor: pass around the requested url so we have context to merge it absolute with the path --- src/backend/contentProcessor.ts | 12 ++++++++++-- src/backend/filters/urlRewrite.ts | 18 +++++++++--------- src/backend/providerManager.ts | 7 ++++++- src/backend/types.ts | 6 +++++- src/backend/worker.ts | 6 +++++- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/backend/contentProcessor.ts b/src/backend/contentProcessor.ts index 16956f6..531b9bd 100644 --- a/src/backend/contentProcessor.ts +++ b/src/backend/contentProcessor.ts @@ -7,11 +7,19 @@ export class ContentProcessor { this.filters.push(filter); } - async process(response: Response, mimeType: string): Promise { + async process( + response: Response, + mimeType: string, + requestor: string, + ): Promise { let processedResponse = response; for (const filter of this.filters) { - processedResponse = await filter.process(processedResponse, mimeType); + processedResponse = await filter.process( + processedResponse, + mimeType, + requestor, + ); } return processedResponse; diff --git a/src/backend/filters/urlRewrite.ts b/src/backend/filters/urlRewrite.ts index 32966f9..95dc338 100644 --- a/src/backend/filters/urlRewrite.ts +++ b/src/backend/filters/urlRewrite.ts @@ -4,8 +4,14 @@ import tldEnum from "@lumeweb/tld-enum"; import * as cheerio from "cheerio"; import urlJoin from "proper-url-join"; +const swUrl = new URL(self.location.origin); + export default class URLRewriteFilter implements ContentFilter { - async process(response: Response, mimeType: string): Promise { + async process( + response: Response, + mimeType: string, + requestor: string, + ): Promise { if (mimeType !== "text/html") { return response; } @@ -24,15 +30,9 @@ export default class URLRewriteFilter implements ContentFilter { const isExternal = urlValue.startsWith("http"); if (!isExternal || !isICANN(urlValue)) { if (!isExternal) { - //@ts-ignore - urlValue = urlJoin("/browse/", urlValue); - } else { - urlValue = `/browse/${urlValue}`; + urlValue = urlJoin(requestor, urlValue); } - - console.log(urlValue); - - $(element).attr(attrName, urlValue); + urlValue = `${swUrl.protocol}://${swUrl.hostname}/browse/${urlValue}`; } } }); diff --git a/src/backend/providerManager.ts b/src/backend/providerManager.ts index 378133c..4a91308 100644 --- a/src/backend/providerManager.ts +++ b/src/backend/providerManager.ts @@ -15,7 +15,11 @@ export class ProviderManager { this.providers.push(provider); } - async fetch(dnsResult: DNSResult, path: string): Promise { + async fetch( + url: string, + dnsResult: DNSResult, + path: string, + ): Promise { for (const record of dnsResult.records) { for (const provider of this.providers) { if (provider.supports(record.value)) { @@ -25,6 +29,7 @@ export class ProviderManager { return this._processor.process( content, content.headers.get("Content-Type")!, + url, ); } diff --git a/src/backend/types.ts b/src/backend/types.ts index 5ee1294..717aa80 100644 --- a/src/backend/types.ts +++ b/src/backend/types.ts @@ -8,5 +8,9 @@ export interface ContentProvider { } export interface ContentFilter { - process: (response: Response, mineType: string) => Promise; + process: ( + response: Response, + mineType: string, + requestor: string, + ) => Promise; } diff --git a/src/backend/worker.ts b/src/backend/worker.ts index 2c61861..ab83fd8 100644 --- a/src/backend/worker.ts +++ b/src/backend/worker.ts @@ -62,7 +62,11 @@ addEventListener("fetch", (event: any) => { console.log("realUrl", realUrl); 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.");