From e3e7c9fc1ec60a7fd9414adfbe80b181e3fa059b Mon Sep 17 00:00:00 2001 From: Okiki Ojo Date: Fri, 28 Jul 2023 07:29:07 +0000 Subject: [PATCH] chore: temporarily disable cors --- mod.ts | 195 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 127 insertions(+), 68 deletions(-) diff --git a/mod.ts b/mod.ts index 56a7eab..06ef70f 100644 --- a/mod.ts +++ b/mod.ts @@ -1,10 +1,10 @@ -import { serve, dewy, cors, path, media_types } from "./deps.ts"; +import { cors, dewy, media_types, path, serve } from "./deps.ts"; import { twoslasher } from "./vendor/twoslash.ts"; import type { TwoSlashOptions } from "./vendor/twoslash.ts"; const { extname, basename, dirname, fromFileUrl, join } = path; const { Router } = dewy; -const { contentType } = media_types +const { contentType } = media_types; const __dirname = dirname(fromFileUrl(import.meta.url)); @@ -14,16 +14,30 @@ interface TwoslashRequestOptions extends TwoSlashOptions { } const router = new Router(); -router.use(cors()); +// router.use(cors()); router.get("/", () => { - return Response.json({ - message: "Hello World! The API is working!", - POST: "/twoslash - Accepts a JSON & Form Data body with the following properties: code, extension, and options." - }) + try { + return Response.json({ + hey: "Hello World! The API is working!", + message: + "/twoslash - Accepts a JSON & Form Data body with the following properties: code, extension, and options.", + }); + } catch (e) { + return new Response(e.toString(), { + status: 400, + headers: new Headers([ + ["Content-Type", "text/plain"], + ]), + }); + } }); router.get("/twoslash", async ({ request }) => { try { + console.log({ + method: "get", + url: request.url, + }); const url = new URL(request.url); if (!url.search) { return Response.json({ @@ -32,19 +46,21 @@ router.get("/twoslash", async ({ request }) => { `GET: /twoslash?options={"code":"import { hasTransferables } from \"transferables\"","extension":"ts"}`, `GET: /twoslash?code=import { hasTransferables } from \"transferables\"&extension=ts&options={"defaultOptions":{"noErrors":true}}`, `POST: /twoslash --> JSON Body={"code":"import { hasTransferables } from \"transferables\"","extension":"ts"}`, - `POST: /twoslash --> FormData Body={"code":"import { hasTransferables } from \"transferables\"","extension":"ts"}` - ] - }) + `POST: /twoslash --> FormData Body={"code":"import { hasTransferables } from \"transferables\"","extension":"ts"}`, + ], + }); } const codeParam = url.searchParams.get("code")! || undefined; - const extensionParam = url.searchParams.get("extension")! || url.searchParams.get("ext")! || undefined; - const optionsParam = url.searchParams.get("options")! || url.searchParams.get("option")! || "{}"; - + const extensionParam = url.searchParams.get("extension")! || + url.searchParams.get("ext")! || undefined; + const optionsParam = url.searchParams.get("options")! || + url.searchParams.get("option")! || "{}"; + const decodedQuery: TwoslashRequestOptions = Object.assign( - JSON.parse(optionsParam), - { code: codeParam, extension: extensionParam } - ) + JSON.parse(optionsParam), + { code: codeParam, extension: extensionParam }, + ); const { code, extension = "ts", ...opts } = decodedQuery; const twoslash = await twoslasher(code, extension, opts); @@ -54,7 +70,7 @@ router.get("/twoslash", async ({ request }) => { return new Response(e.toString(), { status: 400, headers: new Headers([ - ['Content-Type', "text/plain"] + ["Content-Type", "text/plain"], ]), }); } @@ -64,9 +80,15 @@ router.post("/twoslash", async ({ request }) => { try { const headers = request.headers; const contentType = headers.get("content-type"); - const { code, extension, ...opts }: TwoslashRequestOptions = contentType && /multipart\/form\-data/.test(contentType) ? - Object.fromEntries((await request.formData()).entries()): - await request.json(); + console.log({ + method: "post", + url: request.url, + contentType, + }); + const { code, extension, ...opts }: TwoslashRequestOptions = + contentType && /multipart\/form\-data/.test(contentType) + ? Object.fromEntries((await request.formData()).entries()) + : await request.json(); const twoslash = await twoslasher(code, extension, opts); console.log("twoslash ", twoslash); @@ -75,65 +97,102 @@ router.post("/twoslash", async ({ request }) => { return new Response(e.toString(), { status: 400, headers: new Headers([ - ['Content-Type', "text/plain"] + ["Content-Type", "text/plain"], ]), }); } }); -router.get("/.well-known/*", async ({ request }) => { - const url = new URL(request.url); - const ext = extname(url.pathname); - const fileName = basename(url.pathname); - return new Response(await Deno.readFile(join(__dirname, "./.well-known", fileName)), { - status: 200, - headers: new Headers([ - ['Content-Type', contentType(ext) ?? "application/json"] - ]), - }) +router.get("/.well-known/*", async ({ request }) => { + try { + const url = new URL(request.url); + const ext = extname(url.pathname); + const fileName = basename(url.pathname); + return new Response( + await Deno.readFile(join(__dirname, "./.well-known", fileName)), + { + status: 200, + headers: new Headers([ + ["Content-Type", contentType(ext) ?? "application/json"], + ]), + }, + ); + } catch (e) { + return new Response(e.toString(), { + status: 400, + headers: new Headers([ + ["Content-Type", "text/plain"], + ]), + }); + } }); -router.get("/favicon/*", async ({ request }) => { - const url = new URL(request.url); - const ext = extname(url.pathname); - const fileName = basename(url.pathname); - return new Response(await Deno.readFile(join(__dirname, "./favicon", fileName)), { - status: 200, - headers: new Headers([ - ['Content-Type', contentType(ext) ?? "text/plain"] - ]), - }) +router.get("/favicon/*", async ({ request }) => { + try { + const url = new URL(request.url); + const ext = extname(url.pathname); + const fileName = basename(url.pathname); + return new Response( + await Deno.readFile(join(__dirname, "./favicon", fileName)), + { + status: 200, + headers: new Headers([ + ["Content-Type", contentType(ext) ?? "text/plain"], + ]), + }, + ); + } catch (e) { + return new Response(e.toString(), { + status: 400, + headers: new Headers([ + ["Content-Type", "text/plain"], + ]), + }); + } }); -router.get("/favicon.ico", async () => { - const ext = ".ico"; - return new Response(await Deno.readFile(join(__dirname, "./favicon/favicon.ico")), { - status: 200, - headers: new Headers([ - ['Content-Type', contentType(ext)] - ]), - }) +router.get("/favicon.ico", async () => { + try { + const ext = ".ico"; + return new Response( + await Deno.readFile(join(__dirname, "./favicon/favicon.ico")), + { + status: 200, + headers: new Headers([ + ["Content-Type", contentType(ext)], + ]), + }, + ); + } catch (e) { + return new Response(e.toString(), { + status: 400, + headers: new Headers([ + ["Content-Type", "text/plain"], + ]), + }); + } }); -router.get("/favicon.svg", async () => { - const ext = ".svg"; - return new Response(await Deno.readFile(join(__dirname, "./favicon/favicon.svg")), { - status: 200, - headers: new Headers([ - ['Content-Type', contentType(ext)] - ]), - }) +const ext = ".svg"; +router.get("/favicon.svg", async () => { + try { + return new Response( + await Deno.readFile(join(__dirname, "./favicon/favicon.svg")), + { + status: 200, + headers: new Headers([ + ["Content-Type", contentType(ext)], + ]), + }, + ); + } catch (e) { + return new Response(e.toString(), { + status: 400, + headers: new Headers([ + ["Content-Type", "text/plain"], + ]), + }); + } }); serve(router.dispatch.bind(router)); - -function isDecodeableURI(param: string) { - let decodeUriFirst = false; - try { - JSON.parse(decodeURIComponent(param)); - decodeUriFirst = true; - } catch(e) { - console.error("Error Decoding URI, Skip Decoding URI", e); - } - return decodeUriFirst; -} -- 2.51.2