From 5d4a9b40ad1dfa58e35173e5c376eab6be899fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=83=A5=EB=83=90=EC=B1=A0?= Date: Fri, 23 Aug 2024 13:27:03 +0900 Subject: [PATCH] feat: support declarative style fetch handler (#400) * feat: support declarative style fetch handler * chore: add a simple integration test for declarative style fetch handler --- .../serve-declarative-style/index.ts | 5 +++++ crates/base/tests/integration_tests.rs | 18 +++++++++++++++ crates/sb_core/js/00_serve.js | 22 +++++++++++++++++++ crates/sb_core/js/bootstrap.js | 9 ++++++++ crates/sb_core/lib.rs | 1 + examples/serve-declarative-style/index.ts | 5 +++++ 6 files changed, 60 insertions(+) create mode 100644 crates/base/test_cases/serve-declarative-style/index.ts create mode 100644 crates/sb_core/js/00_serve.js create mode 100644 examples/serve-declarative-style/index.ts diff --git a/crates/base/test_cases/serve-declarative-style/index.ts b/crates/base/test_cases/serve-declarative-style/index.ts new file mode 100644 index 00000000..74dae2c2 --- /dev/null +++ b/crates/base/test_cases/serve-declarative-style/index.ts @@ -0,0 +1,5 @@ +export default { + async fetch(_req: Request) { + return new Response("meow"); + } +} diff --git a/crates/base/tests/integration_tests.rs b/crates/base/tests/integration_tests.rs index f768e4cb..df0ab753 100644 --- a/crates/base/tests/integration_tests.rs +++ b/crates/base/tests/integration_tests.rs @@ -2250,6 +2250,24 @@ async fn test_fastify_latest_package() { ); } +#[tokio::test] +#[serial] +async fn test_declarative_style_fetch_handler() { + integration_test!( + "./test_cases/main", + NON_SECURE_PORT, + "serve-declarative-style", + None, + None, + None, + None, + (|resp| async { + assert_eq!(resp.unwrap().text().await.unwrap(), "meow"); + }), + TerminationToken::new() + ); +} + trait AsyncReadWrite: AsyncRead + AsyncWrite + Send + Unpin {} impl AsyncReadWrite for T where T: AsyncRead + AsyncWrite + Send + Unpin {} diff --git a/crates/sb_core/js/00_serve.js b/crates/sb_core/js/00_serve.js new file mode 100644 index 00000000..df7fc4c5 --- /dev/null +++ b/crates/sb_core/js/00_serve.js @@ -0,0 +1,22 @@ +import { primordials } from "ext:core/mod.js"; + +const { ObjectHasOwn } = primordials; + +function registerDeclarativeServer(exports) { + if (ObjectHasOwn(exports, "fetch")) { + if (typeof exports.fetch !== "function") { + throw new TypeError( + "Invalid type for fetch: must be a function with a single or no parameter", + ); + } + Deno.serve({ + handler: (req) => { + return exports.fetch(req); + }, + }); + } +} + +export { + registerDeclarativeServer +} \ No newline at end of file diff --git a/crates/sb_core/js/bootstrap.js b/crates/sb_core/js/bootstrap.js index 25693681..5e3ed028 100644 --- a/crates/sb_core/js/bootstrap.js +++ b/crates/sb_core/js/bootstrap.js @@ -43,6 +43,7 @@ import { import { promiseRejectMacrotaskCallback } from 'ext:sb_core_main_js/js/promises.js'; import { denoOverrides, fsVars } from 'ext:sb_core_main_js/js/denoOverrides.js'; +import { registerDeclarativeServer } from 'ext:sb_core_main_js/js/00_serve.js'; import * as performance from 'ext:deno_web/15_performance.js'; import * as messagePort from 'ext:deno_web/13_message_port.js'; import { SupabaseEventListener } from 'ext:sb_user_event_worker/event_worker.js'; @@ -66,6 +67,7 @@ const { ObjectDefineProperty, ObjectDefineProperties, ObjectSetPrototypeOf, + ObjectHasOwn, SafeSet, StringPrototypeIncludes, StringPrototypeSplit, @@ -547,6 +549,13 @@ globalThis.bootstrapSBEdge = (opts, extraCtx) => { Deno[name] = value; } } + + // find declarative fetch handler + core.addMainModuleHandler(main => { + if (ObjectHasOwn(main, 'default')) { + registerDeclarativeServer(main.default); + } + }); } if (isEventsWorker) { diff --git a/crates/sb_core/lib.rs b/crates/sb_core/lib.rs index ed9722f6..a0b3a6d1 100644 --- a/crates/sb_core/lib.rs +++ b/crates/sb_core/lib.rs @@ -374,6 +374,7 @@ deno_core::extension!( "js/navigator.js", "js/bootstrap.js", "js/main_worker.js", + "js/00_serve.js", "js/01_http.js" ] ); diff --git a/examples/serve-declarative-style/index.ts b/examples/serve-declarative-style/index.ts new file mode 100644 index 00000000..9eb94f85 --- /dev/null +++ b/examples/serve-declarative-style/index.ts @@ -0,0 +1,5 @@ +export default { + async fetch(_req: Request) { + return new Response("Hello, world"); + } +}