Add better error handling to aero

This commit is contained in:
Ryan Wilson 2024-09-24 12:06:16 -04:00
parent 97d305ddcc
commit 3c3dc996e4
3 changed files with 70 additions and 8 deletions

8
.gitignore vendored
View file

@ -36,12 +36,4 @@ yarn-error.log*
*.tsbuildinfo *.tsbuildinfo
next-env.d.ts 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) # TODO: Add UV public build files here (not the UV config file)

51
public/sw.js Normal file
View file

@ -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);
}

View file

@ -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()
}
}