Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mayant15 authored and amitksingh1490 committed May 4, 2024
1 parent cc1d6c0 commit 09c15ee
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 90 deletions.
17 changes: 12 additions & 5 deletions src/cli/javascript/js_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,20 @@ mod tests {
let uri = object.get::<&str, Uri>("uri").unwrap();
let method = object.get::<&str, String>("method").unwrap();
let body = object.get::<&str, Option<String>>("body").unwrap();
let js_headers = object.get::<&str, BTreeMap<String, String>>("headers").unwrap();
let js_headers = object
.get::<&str, BTreeMap<String, String>>("headers")
.unwrap();

assert_eq!(uri.to_string(), "http://example.com/");
assert_eq!(method, "GET");
assert_eq!(body, Some("Hello, World!".to_string()));
assert_eq!(js_headers.get("content-type"), Some(&"application/json".to_string()));
assert_eq!(
js_headers.get("content-type"),
Some(&"application/json".to_string())
);
});
}


#[test]
fn test_js_request_from_js() {
let runtime = Runtime::new().unwrap();
Expand All @@ -326,11 +330,14 @@ mod tests {
let value = js_request.into_js(&ctx).unwrap();

let js_request = JsRequest::from_js(&ctx, value).unwrap();

assert_eq!(js_request.uri.to_string(), "http://example.com/");
assert_eq!(js_request.method, "GET");
assert_eq!(js_request.body, Some("Hello, World!".to_string()));
assert_eq!(js_request.headers.get("content-type"), Some(&"application/json".to_string()));
assert_eq!(
js_request.headers.get("content-type"),
Some(&"application/json".to_string())
);
});
}
}
16 changes: 12 additions & 4 deletions src/cli/javascript/js_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,18 @@ mod test {
let object = value.as_object().unwrap();

let status = object.get::<&str, u16>("status").unwrap();
let headers = object.get::<&str, BTreeMap<String, String>>("headers").unwrap();
let headers = object
.get::<&str, BTreeMap<String, String>>("headers")
.unwrap();
let body = object.get::<&str, Option<String>>("body").unwrap();

assert_eq!(status, reqwest::StatusCode::OK);
assert_eq!(body, Some("Hello, World!".to_owned()));
assert!(headers.contains_key("content-type"));
assert_eq!(headers.get("content-type"), Some(&"application/json".to_owned()));
assert_eq!(
headers.get("content-type"),
Some(&"application/json".to_owned())
);
});
}

Expand All @@ -178,12 +183,15 @@ mod test {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let js_response = create_test_response().unwrap().into_js(&ctx).unwrap();
let js_response = create_test_response().unwrap().into_js(&ctx).unwrap();
let response = JsResponse::from_js(&ctx, js_response).unwrap();

assert_eq!(response.status, reqwest::StatusCode::OK.as_u16());
assert_eq!(response.body, Some("Hello, World!".to_owned()));
assert_eq!(response.headers.get("content-type"), Some(&"application/json".to_owned()));
assert_eq!(
response.headers.get("content-type"),
Some(&"application/json".to_owned())
);
});
}
}
115 changes: 58 additions & 57 deletions src/cli/javascript/request_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,63 +49,6 @@ impl<'js> FromJs<'js> for Command {
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;

use rquickjs::{Context, FromJs, IntoJs, Object, Runtime, String as JsString};
use crate::cli::javascript::{request_filter::Command, JsRequest, JsResponse};

#[test]
fn test_command_from_invalid_object() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let value = JsString::from_str(ctx.clone(), "invalid").unwrap().into_value();
assert!(Command::from_js(&ctx, value).is_err());
});
}

#[test]
fn test_command_from_request() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let request = reqwest::Request::new(reqwest::Method::GET, "http://example.com/".parse().unwrap());
let js_request: JsRequest = (&request).try_into().unwrap();
let value = Object::new(ctx.clone()).unwrap();
value.set("request", js_request.into_js(&ctx)).unwrap();
assert!(Command::from_js(&ctx, value.into_value()).is_ok());
});
}

#[test]
fn test_command_from_response() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let js_response = JsResponse {
status: 200,
headers: BTreeMap::new(),
body: None,
};
let value = Object::new(ctx.clone()).unwrap();
value.set("response", js_response).unwrap();
assert!(Command::from_js(&ctx, value.into_value()).is_ok());
});
}

#[test]
fn test_command_from_arbitrary_object() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let value = Object::new(ctx.clone()).unwrap();
assert!(Command::from_js(&ctx, value.into_value()).is_err());
});
}
}

pub struct RequestFilter {
worker: Arc<dyn WorkerIO<Event, Command>>,
client: Arc<dyn HttpIO>,
Expand Down Expand Up @@ -158,3 +101,61 @@ impl HttpIO for RequestFilter {
self.on_request(request).await
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;

use rquickjs::{Context, FromJs, IntoJs, Object, Runtime, String as JsString};

use crate::cli::javascript::request_filter::Command;
use crate::cli::javascript::{JsRequest, JsResponse};

#[test]
fn test_command_from_invalid_object() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let value = JsString::from_str(ctx.clone(), "invalid")
.unwrap()
.into_value();
assert!(Command::from_js(&ctx, value).is_err());
});
}

#[test]
fn test_command_from_request() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let request =
reqwest::Request::new(reqwest::Method::GET, "http://example.com/".parse().unwrap());
let js_request: JsRequest = (&request).try_into().unwrap();
let value = Object::new(ctx.clone()).unwrap();
value.set("request", js_request.into_js(&ctx)).unwrap();
assert!(Command::from_js(&ctx, value.into_value()).is_ok());
});
}

#[test]
fn test_command_from_response() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let js_response = JsResponse { status: 200, headers: BTreeMap::new(), body: None };
let value = Object::new(ctx.clone()).unwrap();
value.set("response", js_response).unwrap();
assert!(Command::from_js(&ctx, value.into_value()).is_ok());
});
}

#[test]
fn test_command_from_arbitrary_object() {
let runtime = Runtime::new().unwrap();
let context = Context::base(&runtime).unwrap();
context.with(|ctx| {
let value = Object::new(ctx.clone()).unwrap();
assert!(Command::from_js(&ctx, value.into_value()).is_err());
});
}
}
45 changes: 23 additions & 22 deletions src/cli/javascript/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ impl WorkerIO<Event, Command> for Runtime {
// exit if failed to initialize
LOCAL_RUNTIME.with(move |cell| {
if cell.borrow().get().is_none() {
LocalRuntime::try_new(script)
.and_then(|runtime| {
cell.borrow().set(runtime).map_err(|_| anyhow::anyhow!("trying to reinitialize an already initialized QuickJS runtime"))
LocalRuntime::try_new(script).and_then(|runtime| {
cell.borrow().set(runtime).map_err(|_| {
anyhow::anyhow!(
"trying to reinitialize an already initialized QuickJS runtime"
)
})
})
} else {
Ok(())
}
Expand All @@ -107,25 +110,23 @@ fn call(name: String, event: Event) -> anyhow::Result<Option<Command>> {
let runtime = cell
.get_mut()
.ok_or(anyhow::anyhow!("JS runtime not initialized"))?;
runtime.context.with(|ctx| {
match event {
Event::Request(req) => {
let fn_as_value = ctx
.globals()
.get::<&str, Function>(name.as_str())
.map_err(|_| anyhow::anyhow!("globalThis not initialized"))?;

let function = fn_as_value
.as_function()
.ok_or(anyhow::anyhow!("`{name}` is not a function"))?;

let args = prepare_args(&ctx, req)?;
let command: Option<Value> = function.call(args).ok();
command
.map(|output| Command::from_js(&ctx, output))
.transpose()
.map_err(|e| anyhow::anyhow!("deserialize failed: {e}"))
}
runtime.context.with(|ctx| match event {
Event::Request(req) => {
let fn_as_value = ctx
.globals()
.get::<&str, Function>(name.as_str())
.map_err(|_| anyhow::anyhow!("globalThis not initialized"))?;

let function = fn_as_value
.as_function()
.ok_or(anyhow::anyhow!("`{name}` is not a function"))?;

let args = prepare_args(&ctx, req)?;
let command: Option<Value> = function.call(args).ok();
command
.map(|output| Command::from_js(&ctx, output))
.transpose()
.map_err(|e| anyhow::anyhow!("deserialize failed: {e}"))
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions tailcall-cloudflare/src/handle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Borrow;

use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn get_app_ctx(

if let Some(file_path) = &file_path {
if let Some(app_ctx) = read_app_ctx() {
if app_ctx.0 == file_path.borrow() {
if app_ctx.0.eq(file_path) {
tracing::info!("Using cached application context");
return Ok(Ok(app_ctx.clone().1));
}
Expand Down

0 comments on commit 09c15ee

Please sign in to comment.