Skip to content

Commit

Permalink
feat(runtime,docs): support HTTP/2 in fetch() (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Jun 8, 2023
1 parent 079c008 commit 2dcce72
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 133 deletions.
8 changes: 8 additions & 0 deletions .changeset/new-insects-retire.md
@@ -0,0 +1,8 @@
---
'@lagon/runtime': patch
'@lagon/docs': patch
'@lagon/cli': patch
'@lagon/serverless': patch
---

Support HTTP/2 (along with HTTP/1.1) in `fetch()`
102 changes: 89 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 4 additions & 26 deletions crates/runtime/tests/fetch.rs
Expand Up @@ -342,7 +342,7 @@ async fn throw_invalid_url() {

utils::assert_run_result(
&receiver,
RunResult::Error("Uncaught Error: client requires absolute-form URIs".into()),
RunResult::Error("Uncaught Error: builder error: relative URL without a base".into()),
)
.await;
}
Expand Down Expand Up @@ -469,30 +469,6 @@ async fn redirect_relative_url() {
.await;
}

#[tokio::test]
async fn redirect_without_location_header() {
utils::setup();
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(status_code(301)),
);
let url = server.url("/");

let (send, receiver) = utils::create_isolate(IsolateOptions::new(format!(
"export async function handler() {{
const status = (await fetch('{url}')).status;
return new Response(status);
}}"
)));
send(Request::default());

utils::assert_run_result(
&receiver,
RunResult::Error("Uncaught Error: Got a redirect without Location header".into()),
)
.await;
}

#[tokio::test]
async fn redirect_loop() {
utils::setup();
Expand Down Expand Up @@ -529,7 +505,7 @@ async fn redirect_loop() {

utils::assert_run_result(
&receiver,
RunResult::Error("Uncaught Error: Too many redirects".into()),
RunResult::Error("Uncaught Error: error following redirect: Too many redirects".into()),
)
.await;
}
Expand Down Expand Up @@ -606,6 +582,8 @@ async fn fetch_https() {
.unwrap(),
)
.await;

tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}

#[tokio::test]
Expand Down
10 changes: 5 additions & 5 deletions crates/runtime_http/src/response.rs
@@ -1,26 +1,26 @@
use anyhow::{anyhow, Result};
use hyper::{body::Bytes, http::response::Parts, Body, Response};
use hyper::{body::Bytes, Body, HeaderMap, Response};
use lagon_runtime_v8_utils::{
extract_v8_headers_object, extract_v8_integer, extract_v8_string, v8_headers_object,
v8_integer, v8_string,
};

pub fn response_to_v8<'a>(
response: (Parts, Bytes),
response: (u16, HeaderMap, Bytes),
scope: &mut v8::HandleScope<'a>,
) -> v8::Local<'a, v8::Object> {
let len = 3;
let mut names = Vec::with_capacity(len);
let mut values = Vec::with_capacity(len);

names.push(v8_string(scope, "b").into());
values.push(v8_string(scope, unsafe { std::str::from_utf8_unchecked(&response.1) }).into());
values.push(v8_string(scope, unsafe { std::str::from_utf8_unchecked(&response.2) }).into());

names.push(v8_string(scope, "s").into());
values.push(v8_integer(scope, response.0.status.as_u16().into()).into());
values.push(v8_integer(scope, response.0.into()).into());

names.push(v8_string(scope, "h").into());
values.push(v8_headers_object(scope, response.0.headers).into());
values.push(v8_headers_object(scope, response.1).into());

let null = v8::null(scope).into();
v8::Object::with_prototype_and_properties(scope, null, &names, &values)
Expand Down
3 changes: 1 addition & 2 deletions crates/runtime_isolate/Cargo.toml
Expand Up @@ -8,15 +8,14 @@ v8 = "0.73.0"
tokio = { version = "1", features = ["rt-multi-thread"] }
futures = "0.3.28"
hyper = { version = "0.14.26", features = ["client"] }
hyper-tls = { version = "0.5.0", features = ["vendored"] }
flume = "0.10.14"
anyhow = "1.0.71"
log = { version = "0.4.18", features = ["std", "kv_unstable"] }
async-recursion = "1.0.4"
linked-hash-map = "0.5.6"
lagon-runtime-v8-utils = { path = "../runtime_v8_utils" }
lagon-runtime-http = { path = "../runtime_http" }
lagon-runtime-crypto = { path = "../runtime_crypto" }
reqwest = { version = "0.11.18", features = ["rustls-tls"] }

[features]
default = []
Expand Down

1 comment on commit 2dcce72

@vercel
Copy link

@vercel vercel bot commented on 2dcce72 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./packages/docs

lagon-docs.vercel.app
docs.lagon.app
docs-git-main-lagon.vercel.app
docs-lagon.vercel.app

Please sign in to comment.