Skip to content

Commit

Permalink
feat: support declarative style fetch handler (#400)
Browse files Browse the repository at this point in the history
* feat: support declarative style fetch handler

* chore: add a simple integration test for declarative style fetch handler
  • Loading branch information
nyannyacha authored Aug 23, 2024
1 parent 53061f6 commit 5d4a9b4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/base/test_cases/serve-declarative-style/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch(_req: Request) {
return new Response("meow");
}
}
18 changes: 18 additions & 0 deletions crates/base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> AsyncReadWrite for T where T: AsyncRead + AsyncWrite + Send + Unpin {}
Expand Down
22 changes: 22 additions & 0 deletions crates/sb_core/js/00_serve.js
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions crates/sb_core/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -66,6 +67,7 @@ const {
ObjectDefineProperty,
ObjectDefineProperties,
ObjectSetPrototypeOf,
ObjectHasOwn,
SafeSet,
StringPrototypeIncludes,
StringPrototypeSplit,
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions crates/sb_core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
);
5 changes: 5 additions & 0 deletions examples/serve-declarative-style/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch(_req: Request) {
return new Response("Hello, world");
}
}

0 comments on commit 5d4a9b4

Please sign in to comment.