From 67d94d1c47874d773136fccc4aa56ddeafa01176 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 13 Aug 2022 13:16:53 -0400 Subject: [PATCH] *Add temp hack to filter out inline source maps --- src/contentProviders/ipfsProvider.ts | 59 ++++++++++++++++------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/contentProviders/ipfsProvider.ts b/src/contentProviders/ipfsProvider.ts index 80bd8ea..16d51c8 100644 --- a/src/contentProviders/ipfsProvider.ts +++ b/src/contentProviders/ipfsProvider.ts @@ -103,9 +103,11 @@ export default class IpfsProvider extends BaseProvider { return false; } + async handleProxy(details: OnRequestDetailsType): Promise { return requestProxies; } + async handleReqHeaders( details: OnBeforeSendHeadersDetailsType ): Promise { @@ -247,35 +249,42 @@ export default class IpfsProvider extends BaseProvider { if (buffer.length) { let mode = contentModes[contentType as string]; if (mode === CONTENT_MODE_BUFFERED) { - filter.write( - Uint8Array.from( - buffer.reduce( - (previousValue: Uint8Array, currentValue: Uint8Array) => { - return Uint8Array.from([...previousValue, ...currentValue]); - } - ) + let data: string | Uint8Array = Uint8Array.from( + buffer.reduce( + (previousValue: Uint8Array, currentValue: Uint8Array) => { + return Uint8Array.from([...previousValue, ...currentValue]); + } ) ); + if (contentType === "application/javascript") { + data = new TextDecoder("utf-8", { fatal: true }).decode(data); + data = data.replace( + /\/\/#\s*sourceMappingURL=([^\.]+)\.js.map/, + "" + ); + data = new TextEncoder().encode(data); + } + filter.write(data); } else if (mode == CONTENT_MODE_CHUNKED) { - buffer.forEach(filter.write); + buffer.forEach((data) => filter.write(data)); } } filter.close(); }) .catch((e) => { - console.log("page error", urlPath, e.message); + console.error("page error", urlPath, e.message); /* if ( - urlPath.endsWith(".html") || - urlPath.endsWith(".htm") || - urlPath.endsWith(".xhtml") || - urlPath.endsWith(".shtml") - ) { - this.setData(details, "contentType", "text/html"); - let template = serverErrorTemplate(); - contentLength = template.length; - receiveUpdate(new TextEncoder().encode(template)); - this.setData(details, "contentLength", contentLength); - }*/ + urlPath.endsWith(".html") || + urlPath.endsWith(".htm") || + urlPath.endsWith(".xhtml") || + urlPath.endsWith(".shtml") + ) { + this.setData(details, "contentType", "text/html"); + let template = serverErrorTemplate(); + contentLength = template.length; + receiveUpdate(new TextEncoder().encode(template)); + this.setData(details, "contentLength", contentLength); + }*/ filterPromise.then(() => streamPromise).then(() => filter.close()); }); @@ -294,11 +303,11 @@ export default class IpfsProvider extends BaseProvider { }); /* if (contentLength) { - headers.push({ - name: "content-length", - value: contentLength, - }); - }*/ + headers.push({ + name: "content-length", + value: contentLength, + }); + }*/ return { responseHeaders: headers,