diff --git a/src/routes.js b/src/routes.js index 9bf2d45..2cc06ef 100644 --- a/src/routes.js +++ b/src/routes.js @@ -22,16 +22,15 @@ router.get("/", async (ctx) => { }); router.get("/:uuid", async (ctx, next) => { - const gist = getGist(ctx.params.uuid); + if (ctx.get("x-requested-with") !== "XMLHttpRequest") { + return await ctx.render("view"); + } + const gist = getGist(ctx.params.uuid); if (!gist) { return next(); } - if (ctx.get("x-requested-with") !== "XMLHttpRequest") { - return await ctx.render("view", gist); - } - ctx.set("Cache-Control", "max-age=2592000"); // 30d in seconds ctx.set("X-IV", gist.iv); ctx.body = Buffer.from(gist.cipherText); diff --git a/static/view.mjs b/static/view.mjs index 9ebdce8..93d4131 100644 --- a/static/view.mjs +++ b/static/view.mjs @@ -84,6 +84,11 @@ addEventListener("DOMContentLoaded", async () => { "X-Requested-With": "XMLHttpRequest", }, }); + + if (!response.ok) { + throw response.status; + } + const iv = new Uint8Array(response.headers.get("X-IV")?.split(",")); const cipherText = await response.arrayBuffer(); const buffer = await decrypt(iv, k, cipherText); @@ -91,7 +96,8 @@ addEventListener("DOMContentLoaded", async () => { childElement.textContent = new TextDecoder().decode(buffer) || "empty 👀"; } catch (err) { console.error(err); - childElement.textContent = "failed 💀"; + const mappedMessages = new Map([[404, "not found 🙈"]]); + childElement.textContent = mappedMessages.get(err) || "failed 💀"; return; } finally { preElement.appendChild(childElement);