Skip to content

Commit

Permalink
feat(runtime): allow export as for handler function (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored May 15, 2023
1 parent 7a6458f commit 33fa56c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .changeset/nasty-ads-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@lagon/cli': patch
'@lagon/runtime': patch
'@lagon/serverless': patch
'@lagon/js-runtime': patch
---

Allow `export as` for handler function
19 changes: 19 additions & 0 deletions crates/runtime/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ async fn execute_function() {
);
}

#[tokio::test]
async fn execute_function_export_as() {
utils::setup();
let (send, receiver) = utils::create_isolate(IsolateOptions::new(
"function hello() {
return new Response('Hello world');
}
export { hello as handler }"
.into(),
));
send(Request::default());

assert_eq!(
receiver.recv_async().await.unwrap().as_response(),
Response::from("Hello world")
);
}

#[tokio::test]
async fn execute_function_twice() {
utils::setup();
Expand Down
24 changes: 20 additions & 4 deletions crates/runtime_isolate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ impl Heartbeat {
pub struct Isolate {
options: IsolateOptions,
isolate: Option<v8::OwnedIsolate>,
handler: Option<v8::Global<v8::Function>>,
master_handler: Option<v8::Global<v8::Function>>,
handler: Option<v8::Global<v8::Value>>,
compilation_error: Option<String>,
stream_receiver: flume::Receiver<(u32, StreamResult)>,
termination_result: Arc<RwLock<Option<RunResult>>>,
Expand Down Expand Up @@ -200,6 +201,7 @@ impl Isolate {
let mut this = Self {
options,
isolate: Some(isolate),
master_handler: None,
handler: None,
compilation_error: None,
stream_receiver,
Expand Down Expand Up @@ -367,14 +369,21 @@ impl Isolate {
}

if !self.options.snapshot {
let namespace = module.get_module_namespace().to_object(try_catch).unwrap();
let handler_key = v8_string(try_catch, "handler");
let handler = namespace.get(try_catch, handler_key.into()).unwrap();
let handler = v8::Global::new(try_catch, handler);

self.handler = Some(handler);

let global = global.open(try_catch);
let global = global.global(try_catch);
let handler_key = v8_string(try_catch, "masterHandler");
let handler = global.get(try_catch, handler_key.into()).unwrap();
let handler = v8::Local::<v8::Function>::try_from(handler).unwrap();
let handler = v8::Global::new(try_catch, handler);

self.handler = Some(handler);
self.master_handler = Some(handler);
}
}
None => {
Expand All @@ -400,8 +409,11 @@ impl Isolate {
);
let try_catch = &mut v8::TryCatch::new(scope);

let master_handler = self.master_handler.as_ref().unwrap();
let master_handler = master_handler.open(try_catch);

let handler = self.handler.as_ref().unwrap();
let handler = handler.open(try_catch);
let handler = v8::Local::new(try_catch, handler);

let global = global.open(try_catch);
let global = global.global(try_catch);
Expand All @@ -422,7 +434,11 @@ impl Isolate {
},
);

match handler.call(try_catch, global.into(), &[id.into(), request.into()]) {
match master_handler.call(
try_catch,
global.into(),
&[id.into(), handler, request.into()],
) {
Some(response) => {
let promise = v8::Local::<v8::Promise>::try_from(response)
.expect("Handler did not return a promise");
Expand Down
6 changes: 2 additions & 4 deletions crates/runtime_isolate/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ impl IsolateOptions {
scope,
&format!(
r"{environment_variables}
{code}
globalThis.handler = handler;"
{code}"
),
),
environment_variables.lines().count() + 1,
Expand All @@ -146,8 +145,7 @@ globalThis.handler = handler;"
&format!(
r"{JS_RUNTIME}
{environment_variables}
{code}
globalThis.handler = handler;"
{code}"
),
),
JS_RUNTIME.lines().count() + environment_variables.lines().count() + 2,
Expand Down
4 changes: 2 additions & 2 deletions packages/js-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ declare global {
TEXT_DECODER: TextDecoder;
};
var __storage__: Map<AsyncContext, unknown>;
var handler: (request: Request) => Promise<Response>;
var masterHandler: (
id: number,
handler: (request: Request) => Promise<Response>,
request: {
i: string;
m: RequestInit['method'];
Expand All @@ -141,7 +141,7 @@ declare global {
}
}

globalThis.masterHandler = async (id, request) => {
globalThis.masterHandler = async (id, handler, request) => {
if (typeof handler !== 'function') {
throw new Error('Handler function is not defined or is not a function');
}
Expand Down

0 comments on commit 33fa56c

Please sign in to comment.