From 3c3dc996e4755e540b35cda50950676a911ce7af Mon Sep 17 00:00:00 2001 From: Ryan Wilson Date: Tue, 24 Sep 2024 12:06:16 -0400 Subject: [PATCH] Add better error handling to aero --- .gitignore | 8 ----- public/sw.js | 51 +++++++++++++++++++++++++++ src/app/aero/extras/[extras]/route.ts | 19 ++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 public/sw.js create mode 100644 src/app/aero/extras/[extras]/route.ts diff --git a/.gitignore b/.gitignore index 2199f4f..67e7245 100644 --- a/.gitignore +++ b/.gitignore @@ -36,12 +36,4 @@ yarn-error.log* *.tsbuildinfo next-env.d.ts -# SW proxies - general -public/aeroHandleSimple.js -public/sw.js - -# aero -public/aero/ -**/aeroConfigBuild/ - # TODO: Add UV public build files here (not the UV config file) \ No newline at end of file diff --git a/public/sw.js b/public/sw.js new file mode 100644 index 0000000..150fbd9 --- /dev/null +++ b/public/sw.js @@ -0,0 +1,51 @@ +// Example: aero with other proxies in a SW switcher design + +// Constants +/** + * @type {string} + */ +const dirToAeroConfig = "/aero/"; +/** + * @type {string} + */ +const dirToUvConfigAndBundle = "/uv/"; + +importScripts(`${dirToAeroConfig}config.aero.js`); +importScripts(aeroConfig.bundle["bare-mux"]); +importScripts(aeroConfig.bundle.handle); + +importScripts(`${dirToUvConfigAndBundle}uv.bundle.js`); +importScripts(`${dirToUvConfigAndBundle}uv.config.js`); +importScripts(__uv$config.sw); + +importScripts(`${dirToAeroConfig}/extras/aeroHandleSimple.js`); + +const aeroHandlerWithExtras = patchAeroHandler(handle); +const uv = new UVServiceWorker(); + +addEventListener("install", skipWaiting); + +// Switching +let chosenProxy = defaultProxy; +addEventListener("message", event => { + if ("type" in event.data && event.data.type === "changeDefault") { + const possibleChosenProxy = event.data.data; + if (isValidProxy(possibleChosenProxy)) + chosenProxy = possibleChosenProxy; + else { + console.log( + `Fatal error: tried to set the default proxy, but the proxy to be set isn't supported: ${chosenProxy}` + ); + } + } +}); + +addEventListener("fetch", ev => { + if (ev.request.url.startsWith(__uv$config.prefix)) + return ev.respondWith(uv.fetch(ev)); + if (routeAero(ev)) return ev.respondWith(aeroHandlerWithExtras(ev)); +}); + +function isValidProxy(proxy) { + return ["aero", "uv"].includes(proxy); +} diff --git a/src/app/aero/extras/[extras]/route.ts b/src/app/aero/extras/[extras]/route.ts new file mode 100644 index 0000000..a51e9d8 --- /dev/null +++ b/src/app/aero/extras/[extras]/route.ts @@ -0,0 +1,19 @@ +import fs from 'fs' +import { notFound } from 'next/navigation' +import { NextRequest } from 'next/server' + +export async function GET(_req: NextRequest, { params }: { params: { aero: string } }) { + const requestedFile = params.aero + try { + const res = await fetch(`https://unpkg.com/browse/aero-proxy/extras/${requestedFile}`) + const file = await res.text() + const fileBlob = new Blob([file]) + return new Response(fileBlob, { + headers: { + 'Content-Type': 'application/javascript' + } + }) + } catch { + notFound() + } +}