Skip to content

Commit

Permalink
feat(runtime,js-runtime): use ArrayBuffer for Request/Response body (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Apr 29, 2023
1 parent a2146e2 commit b0b0cc5
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 104 deletions.
6 changes: 6 additions & 0 deletions .changeset/stupid-spies-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lagon/runtime': patch
'@lagon/js-runtime': patch
---

Use ArrayBuffer for Request/response body
8 changes: 4 additions & 4 deletions crates/runtime/tests/async_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function handler() {

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

Expand All @@ -75,7 +75,7 @@ export function handler() {

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

Expand Down Expand Up @@ -115,7 +115,7 @@ export function handler() {

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

Expand Down Expand Up @@ -167,7 +167,7 @@ export function handler() {

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

Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/tests/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async fn crypto_decrypt() {
ciphertext,
);
return new Response(text);
return new Response(new TextDecoder().decode(text));
}"
.into(),
));
Expand Down
5 changes: 4 additions & 1 deletion crates/runtime/tests/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ async fn response_array_buffer() {

assert_eq!(
receiver.recv_async().await.unwrap().as_response(),
Response::from("Hello, World")
Response {
body: "Hello, World".into(),
..Default::default()
}
);
}

Expand Down
67 changes: 59 additions & 8 deletions crates/runtime/tests/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use httptest::bytes::Bytes;
use lagon_runtime_http::{Method, Request, Response};
use lagon_runtime_http::{Method, Request, Response, RunResult, StreamResult};
use lagon_runtime_isolate::options::IsolateOptions;
use std::collections::HashMap;

Expand Down Expand Up @@ -71,7 +71,7 @@ async fn environment_variables() {
}

#[tokio::test]
async fn get_body() {
async fn get_body_streaming() {
utils::setup();
let (send, receiver) = utils::create_isolate(IsolateOptions::new(
"export function handler(request) {
Expand All @@ -81,7 +81,46 @@ async fn get_body() {
));
send(Request {
body: Bytes::from("Hello world"),
headers: None,
headers: Some(HashMap::from([(
"content-type".into(),
Vec::from(["text/plain;charset=UTF-8".into()]),
)])),
method: Method::GET,
url: "".into(),
});

assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Data(vec![
72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100
]))
);
assert!(receiver.recv_async().await.unwrap().as_stream_done());
assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Start(Response {
headers: None,
body: Bytes::from("[object ReadableStream]"),
status: 200,
}))
);
}

#[tokio::test]
async fn get_body() {
utils::setup();
let (send, receiver) = utils::create_isolate(IsolateOptions::new(
"export async function handler(request) {
return new Response(await request.text());
}"
.into(),
));
send(Request {
body: Bytes::from("Hello world"),
headers: Some(HashMap::from([(
"content-type".into(),
Vec::from(["text/plain;charset=UTF-8".into()]),
)])),
method: Method::GET,
url: "".into(),
});
Expand All @@ -103,7 +142,10 @@ async fn get_input() {
));
send(Request {
body: Bytes::new(),
headers: None,
headers: Some(HashMap::from([(
"content-type".into(),
Vec::from(["text/plain;charset=UTF-8".into()]),
)])),
method: Method::GET,
url: "https://hello.world".into(),
});
Expand All @@ -125,7 +167,10 @@ async fn get_method() {
));
send(Request {
body: Bytes::new(),
headers: None,
headers: Some(HashMap::from([(
"content-type".into(),
Vec::from(["text/plain;charset=UTF-8".into()]),
)])),
method: Method::POST,
url: "".into(),
});
Expand Down Expand Up @@ -241,7 +286,10 @@ async fn return_status() {
receiver.recv_async().await.unwrap().as_response(),
Response {
body: "Moved permanently".into(),
headers: None,
headers: Some(HashMap::from([(
"content-type".into(),
Vec::from(["text/plain;charset=UTF-8".into()]),
)])),
status: 302,
}
);
Expand All @@ -262,7 +310,10 @@ async fn return_uint8array() {

assert_eq!(
receiver.recv_async().await.unwrap().as_response(),
Response::from("Hello world")
Response {
body: "Hello world".into(),
..Default::default()
}
);
}

Expand All @@ -285,7 +336,7 @@ async fn console_log() {

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

Expand Down
48 changes: 30 additions & 18 deletions crates/runtime/tests/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ async fn sync_streaming() {

assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Start(Response::from(
"[object ReadableStream]"
)))
RunResult::Stream(StreamResult::Start(Response {
headers: None,
body: Bytes::from("[object ReadableStream]"),
status: 200,
}))
);
}

Expand Down Expand Up @@ -72,9 +74,11 @@ async fn queue_multiple() {
assert!(receiver.recv_async().await.unwrap().as_stream_done());
assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Start(Response::from(
"[object ReadableStream]"
)))
RunResult::Stream(StreamResult::Start(Response {
headers: None,
body: Bytes::from("[object ReadableStream]"),
status: 200,
}))
);
}

Expand Down Expand Up @@ -158,9 +162,11 @@ async fn start_and_pull() {

assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Start(Response::from(
"[object ReadableStream]"
)))
RunResult::Stream(StreamResult::Start(Response {
headers: None,
body: Bytes::from("[object ReadableStream]"),
status: 200,
}))
);
}

Expand Down Expand Up @@ -200,9 +206,11 @@ async fn response_before_write() {

assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Start(Response::from(
"[object ReadableStream]"
)))
RunResult::Stream(StreamResult::Start(Response {
headers: None,
body: Bytes::from("[object ReadableStream]"),
status: 200,
}))
);

assert_eq!(
Expand All @@ -228,9 +236,11 @@ async fn timeout_infinite_streaming() {

assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Start(Response::from(
"[object ReadableStream]"
)))
RunResult::Stream(StreamResult::Start(Response {
headers: None,
body: Bytes::from("[object ReadableStream]"),
status: 200,
}))
);

assert_eq!(receiver.recv_async().await.unwrap(), RunResult::Timeout);
Expand Down Expand Up @@ -289,9 +299,11 @@ async fn promise_reject_callback_after_response() {

assert_eq!(
receiver.recv_async().await.unwrap(),
RunResult::Stream(StreamResult::Start(Response::from(
"[object ReadableStream]"
)))
RunResult::Stream(StreamResult::Start(Response {
headers: None,
body: Bytes::from("[object ReadableStream]"),
status: 200,
}))
);

assert_eq!(receiver.recv_async().await.unwrap(), RunResult::Error("Uncaught ReferenceError: doesNotExists is not defined\n at 12:17\n at stream (11:19)".to_owned()));
Expand Down
7 changes: 4 additions & 3 deletions crates/runtime_http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use hyper::{
Body, Request as HyperRequest,
};
use lagon_runtime_v8_utils::{
extract_v8_headers_object, extract_v8_string, v8_headers_object, v8_string,
extract_v8_headers_object, extract_v8_string, extract_v8_uint8array, v8_headers_object,
v8_string, v8_uint8array,
};
use std::{collections::HashMap, str::FromStr};

Expand Down Expand Up @@ -55,7 +56,7 @@ impl IntoV8 for Request {

if body_exists {
names.push(v8_string(scope, "b").into());
values.push(v8_string(scope, &String::from_utf8(self.body.to_vec()).unwrap()).into());
values.push(v8_uint8array(scope, self.body.to_vec()).into());
}

if let Some(headers) = self.headers {
Expand Down Expand Up @@ -83,7 +84,7 @@ impl FromV8 for Request {

if let Some(body_value) = request.get(scope, body_key.into()) {
if !body_value.is_null_or_undefined() {
body = Bytes::from(extract_v8_string(body_value, scope)?);
body = Bytes::from(extract_v8_uint8array(body_value)?);
}
}

Expand Down
9 changes: 6 additions & 3 deletions crates/runtime_http/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hyper::{
Body, Response as HyperResponse,
};
use lagon_runtime_v8_utils::{
extract_v8_headers_object, extract_v8_integer, extract_v8_string, v8_headers_object,
extract_v8_headers_object, extract_v8_integer, extract_v8_uint8array, v8_headers_object,
v8_integer, v8_string, v8_uint8array,
};
use std::{collections::HashMap, str::FromStr};
Expand Down Expand Up @@ -35,7 +35,10 @@ impl Default for Response {
impl From<&str> for Response {
fn from(body: &str) -> Self {
Response {
headers: None,
headers: Some(HashMap::from([(
"content-type".into(),
Vec::from(["text/plain;charset=UTF-8".into()]),
)])),
body: Bytes::from(body.to_string()),
status: 200,
}
Expand Down Expand Up @@ -81,7 +84,7 @@ impl FromV8 for Response {
let body_key = v8_string(scope, "b");

if let Some(body_value) = response.get(scope, body_key.into()) {
body = extract_v8_string(body_value, scope)?;
body = extract_v8_uint8array(body_value)?;
} else {
return Err(anyhow!("Could not find body"));
}
Expand Down
Loading

0 comments on commit b0b0cc5

Please sign in to comment.