Commit e67b725f authored by Luke Sandberg's avatar Luke Sandberg

Upgrade turbopack-dev-server from hyper 0.14 to hyper 1.x

parent 4d5015d3
......@@ -480,7 +480,7 @@ dependencies = [
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"http-body-util",
"itoa",
"matchit",
......@@ -506,7 +506,7 @@ dependencies = [
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"http-body-util",
"mime",
"pin-project-lite",
......@@ -531,12 +531,6 @@ dependencies = [
"rustc-demangle",
]
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.21.4"
......@@ -2634,25 +2628,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3531d702d6c1a3ba92a5fb55a404c7b8c476c8e7ca249951077afcbe4bc807f"
[[package]]
name = "h2"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9"
dependencies = [
"bytes",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http 0.2.11",
"indexmap 2.13.0",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "h2"
version = "0.4.7"
......@@ -2887,17 +2862,6 @@ dependencies = [
"itoa",
]
[[package]]
name = "http-body"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1"
dependencies = [
"bytes",
"http 0.2.11",
"pin-project-lite",
]
[[package]]
name = "http-body"
version = "1.0.1"
......@@ -2917,7 +2881,7 @@ dependencies = [
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"pin-project-lite",
]
......@@ -2939,30 +2903,6 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"h2 0.3.24",
"http 0.2.11",
"http-body 0.4.5",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"socket2 0.5.10",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]]
name = "hyper"
version = "1.7.0"
......@@ -2973,9 +2913,9 @@ dependencies = [
"bytes",
"futures-channel",
"futures-core",
"h2 0.4.7",
"h2",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"httparse",
"httpdate",
"itoa",
......@@ -2993,7 +2933,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58"
dependencies = [
"http 1.1.0",
"hyper 1.7.0",
"hyper",
"hyper-util",
"rustls",
"rustls-pki-types",
......@@ -3008,7 +2948,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0"
dependencies = [
"hyper 1.7.0",
"hyper",
"hyper-util",
"pin-project-lite",
"tokio",
......@@ -3023,7 +2963,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [
"bytes",
"http-body-util",
"hyper 1.7.0",
"hyper",
"hyper-util",
"native-tls",
"tokio",
......@@ -3033,15 +2973,17 @@ dependencies = [
[[package]]
name = "hyper-tungstenite"
version = "0.9.0"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "880b8b1c98a5ec2a505c7c90db6d3f6f1f480af5655d9c5b55facc9382a5a5b5"
checksum = "bc778da281a749ed28d2be73a9f2cd13030680a1574bc729debd1195e44f00e9"
dependencies = [
"hyper 0.14.32",
"pin-project",
"http-body-util",
"hyper",
"hyper-util",
"pin-project-lite",
"tokio",
"tokio-tungstenite",
"tungstenite 0.18.0",
"tungstenite 0.28.0",
]
[[package]]
......@@ -3056,13 +2998,13 @@ dependencies = [
"futures-core",
"futures-util",
"http 1.1.0",
"http-body 1.0.1",
"hyper 1.7.0",
"http-body",
"hyper",
"ipnet",
"libc",
"percent-encoding",
"pin-project-lite",
"socket2 0.5.10",
"socket2 0.6.2",
"tokio",
"tower-service",
"tracing",
......@@ -4270,9 +4212,9 @@ dependencies = [
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"http-body-util",
"hyper 1.7.0",
"hyper",
"hyper-util",
"log",
"rand 0.9.0",
......@@ -5740,7 +5682,7 @@ dependencies = [
"quinn-udp",
"rustc-hash 2.1.1",
"rustls",
"socket2 0.5.10",
"socket2 0.6.2",
"thiserror 2.0.12",
"tokio",
"tracing",
......@@ -5778,7 +5720,7 @@ dependencies = [
"cfg_aliases",
"libc",
"once_cell",
"socket2 0.5.10",
"socket2 0.6.2",
"tracing",
"windows-sys 0.60.2",
]
......@@ -6128,9 +6070,9 @@ dependencies = [
"futures-core",
"futures-util",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"http-body-util",
"hyper 1.7.0",
"hyper",
"hyper-rustls",
"hyper-tls",
"hyper-util",
......@@ -7035,16 +6977,6 @@ dependencies = [
"managed",
]
[[package]]
name = "socket2"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "socket2"
version = "0.5.10"
......@@ -8989,14 +8921,14 @@ dependencies = [
[[package]]
name = "tokio-tungstenite"
version = "0.18.0"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54319c93411147bced34cb5609a80e0a8e44c5999c93903a81cd866630ec0bfd"
checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857"
dependencies = [
"futures-util",
"log",
"tokio",
"tungstenite 0.18.0",
"tungstenite 0.28.0",
]
[[package]]
......@@ -9144,11 +9076,11 @@ dependencies = [
"axum",
"base64 0.22.1",
"bytes",
"h2 0.4.7",
"h2",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"http-body-util",
"hyper 1.7.0",
"hyper",
"hyper-timeout",
"hyper-util",
"percent-encoding",
......@@ -9208,7 +9140,7 @@ dependencies = [
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.1",
"http-body",
"iri-string",
"pin-project-lite",
"tower 0.5.2",
......@@ -9356,13 +9288,13 @@ dependencies = [
[[package]]
name = "tungstenite"
version = "0.18.0"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ee6ab729cd4cf0fd55218530c4522ed30b7b6081752839b68fcec8d0960788"
checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9"
dependencies = [
"base64 0.13.1",
"byteorder",
"bytes",
"data-encoding",
"http 0.2.11",
"httparse",
"log",
......@@ -9375,14 +9307,14 @@ dependencies = [
[[package]]
name = "tungstenite"
version = "0.20.1"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9"
checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1"
dependencies = [
"byteorder",
"bytes",
"data-encoding",
"http 0.2.11",
"http 1.1.0",
"httparse",
"log",
"rand 0.8.5",
......@@ -9394,20 +9326,18 @@ dependencies = [
[[package]]
name = "tungstenite"
version = "0.21.0"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1"
checksum = "8628dcc84e5a09eb3d8423d6cb682965dea9133204e8fb3efee74c2a0c259442"
dependencies = [
"byteorder",
"bytes",
"data-encoding",
"http 1.1.0",
"httparse",
"log",
"rand 0.8.5",
"rand 0.9.0",
"sha1",
"thiserror 1.0.69",
"url",
"thiserror 2.0.12",
"utf-8",
]
......@@ -10056,8 +9986,10 @@ dependencies = [
"bincode 2.0.1",
"flate2",
"futures",
"hyper 0.14.32",
"http-body-util",
"hyper",
"hyper-tungstenite",
"hyper-util",
"mime",
"mime_guess",
"parking_lot",
......@@ -10066,7 +9998,7 @@ dependencies = [
"serde",
"serde_json",
"serde_qs",
"socket2 0.4.9",
"socket2 0.5.10",
"tokio",
"tokio-stream",
"tokio-util",
......
......@@ -21,8 +21,10 @@ auto-hash-map = { workspace = true }
bincode = { workspace = true }
flate2 = { workspace = true }
futures = { workspace = true }
hyper = { version = "0.14", features = ["full"] }
hyper-tungstenite = "0.9.0"
http-body-util = "0.1"
hyper = { version = "1", features = ["server", "http1"] }
hyper-tungstenite = "0.19"
hyper-util = { version = "0.1", features = ["tokio", "server-auto", "server-graceful"] }
mime = { workspace = true }
mime_guess = "2.0.4"
parking_lot = { workspace = true }
......@@ -31,7 +33,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_qs = { workspace = true }
socket2 = "0.4.9"
socket2 = "0.5"
tokio = { workspace = true }
tokio-stream = "0.1.9"
tokio-util = { workspace = true }
......
use std::{io::Read, iter};
use std::io::Read;
use anyhow::{Result, anyhow};
use auto_hash_map::AutoSet;
use flate2::{Compression, bufread::GzEncoder};
use futures::{StreamExt, TryStreamExt, stream};
use futures::TryStreamExt;
use http_body_util::BodyExt;
use hyper::{
Request, Response,
header::{CONTENT_ENCODING, CONTENT_LENGTH, HeaderName},
http::HeaderValue,
body::Incoming,
header::{CONTENT_ENCODING, CONTENT_LENGTH, HeaderName, HeaderValue},
};
use mime::Mime;
use turbo_tasks::{
CollectiblesSource, OperationVc, ReadRef, ResolvedVc, TransientInstance, Vc, apply_effects,
util::SharedError,
};
use turbo_tasks_bytes::Bytes;
use turbo_tasks_fs::FileContent;
......@@ -22,10 +22,13 @@ use turbopack_core::{
version::VersionedContent,
};
use crate::source::{
Body, ContentSource, ContentSourceSideEffect, HeaderList, ProxyResult,
request::SourceRequest,
resolve::{ResolveSourceRequestResult, resolve_source_request},
use crate::{
ResponseBody, empty_body, full_body,
source::{
Body, ContentSource, ContentSourceSideEffect, HeaderList, ProxyResult,
request::SourceRequest,
resolve::{ResolveSourceRequestResult, resolve_source_request},
},
};
#[turbo_tasks::value(serialization = "none")]
......@@ -75,10 +78,10 @@ async fn get_from_source_operation(
/// response.
pub async fn process_request_with_content_source(
source: OperationVc<Box<dyn ContentSource>>,
request: Request<hyper::Body>,
request: Request<Incoming>,
issue_reporter: Vc<Box<dyn IssueReporter>>,
) -> Result<(
Response<hyper::Body>,
Response<ResponseBody>,
AutoSet<ResolvedVc<Box<dyn ContentSourceSideEffect>>>,
)> {
let original_path = request.uri().path().to_string();
......@@ -111,14 +114,14 @@ pub async fn process_request_with_content_source(
for (header_name, header_value) in headers {
header_map.append(
HeaderName::try_from(header_name.as_str())?,
hyper::header::HeaderValue::try_from(header_value.as_str())?,
HeaderValue::try_from(header_value.as_str())?,
);
}
for (header_name, header_value) in header_overwrites.iter() {
header_map.insert(
HeaderName::try_from(header_name.as_str())?,
hyper::header::HeaderValue::try_from(header_value.as_str())?,
HeaderValue::try_from(header_value.as_str())?,
);
}
......@@ -141,7 +144,7 @@ pub async fn process_request_with_content_source(
if let Some(content_type) = file.content_type() {
header_map.append(
"content-type",
hyper::header::HeaderValue::try_from(content_type.to_string())?,
HeaderValue::try_from(content_type.to_string())?,
);
should_compress = should_compress_predicate(content_type);
......@@ -152,7 +155,7 @@ pub async fn process_request_with_content_source(
// If a text type, application/javascript, or application/json was
// guessed, use a utf-8 charset as we most likely generated it as
// such.
entry.insert(hyper::header::HeaderValue::try_from(
entry.insert(HeaderValue::try_from(
if (guess.type_() == mime::TEXT
|| guess.subtype() == mime::JAVASCRIPT
|| guess.subtype() == mime::JSON)
......@@ -167,40 +170,28 @@ pub async fn process_request_with_content_source(
if !header_map.contains_key("cache-control") {
// The dev server contents might change at any time, we can't cache them.
header_map.append(
"cache-control",
hyper::header::HeaderValue::try_from("must-revalidate")?,
);
header_map.append("cache-control", HeaderValue::try_from("must-revalidate")?);
}
let content = file.content();
let response = if should_compress {
header_map.insert(CONTENT_ENCODING, HeaderValue::from_static("gzip"));
// Hyper requires an owned reader... We could do this with streaming by cloning
// each `Bytes` and implementing `BufRead` for `Iterator<bytes::Bytes>`, but
// it's not really worth it, just compressing the whole thing up-front is fine.
//
// Use fast compression, since we're likely just tranferring data over
// localhost.
let mut gz_bytes = Vec::new();
GzEncoder::new(content.read(), Compression::fast())
.read_to_end(&mut gz_bytes)
.expect("read of Rope should never fail");
response.body(hyper::Body::wrap_stream(stream::iter(iter::once(
hyper::Result::Ok(gz_bytes),
))))?
response.body(full_body(gz_bytes))?
} else {
// hyper requires an owned stream, so we must clone the iterator items
// this is relatively cheap: each chunk is a `Bytes`, so `Clone` updates a
// refcount
let owned_chunks: Vec<_> =
content.read().cloned().map(hyper::Result::Ok).collect();
let mut all_bytes = Vec::new();
for chunk in content.read() {
all_bytes.extend_from_slice(chunk);
}
header_map.insert(
CONTENT_LENGTH,
hyper::header::HeaderValue::try_from(content.len().to_string())?,
HeaderValue::try_from(all_bytes.len().to_string())?,
);
response.body(hyper::Body::wrap_stream(stream::iter(owned_chunks)))?
response.body(full_body(all_bytes))?
};
return Ok((response, side_effects));
......@@ -213,40 +204,42 @@ pub async fn process_request_with_content_source(
for (name, value) in &proxy_result.headers {
headers.append(
HeaderName::from_bytes(name.as_bytes())?,
hyper::header::HeaderValue::from_str(value)?,
HeaderValue::from_str(value)?,
);
}
return Ok((
response.body(hyper::Body::wrap_stream(proxy_result.body.read()))?,
side_effects,
));
// Collect proxy body from async stream into bytes
let mut body_bytes = Vec::new();
let mut body_stream = proxy_result.body.read();
while let Some(chunk) = body_stream.try_next().await? {
body_bytes.extend_from_slice(&chunk);
}
return Ok((response.body(full_body(body_bytes))?, side_effects));
}
GetFromSourceResult::NotFound => {}
}
Ok((
Response::builder().status(404).body(hyper::Body::empty())?,
Response::builder().status(404).body(empty_body())?,
side_effects,
))
}
async fn http_request_to_source_request(request: Request<hyper::Body>) -> Result<SourceRequest> {
async fn http_request_to_source_request(request: Request<Incoming>) -> Result<SourceRequest> {
let (parts, body) = request.into_parts();
// For simplicity, we fully consume the body now and early return if there were
// any errors.
let bytes: Vec<_> = body
.map(|bytes| {
bytes.map_or_else(
|e| Err(SharedError::new(anyhow!(e))),
// The outer Ok is consumed by try_collect, but the Body type requires a Result, so
// we need to double wrap.
|b| Ok(Ok(Bytes::from(b))),
)
})
.try_collect::<Vec<_>>()
.await?;
// Collect the entire body
let collected = body
.collect()
.await
.map_err(|e| anyhow!("failed to collect request body: {e}"))?;
let body_bytes = collected.to_bytes();
let bytes = if body_bytes.is_empty() {
vec![]
} else {
vec![Ok(Bytes::from(body_bytes))]
};
Ok(SourceRequest {
method: parts.method.to_string(),
......
......@@ -8,6 +8,7 @@ use anyhow::{Context as _, Error, Result};
use futures::{SinkExt, prelude::*, ready, stream::FusedStream};
use hyper::{HeaderMap, Uri, upgrade::Upgraded};
use hyper_tungstenite::{HyperWebsocket, WebSocketStream, tungstenite::Message};
use hyper_util::rt::TokioIo;
use pin_project_lite::pin_project;
use tokio::select;
use tokio_stream::StreamMap;
......@@ -239,7 +240,7 @@ fn resource_to_request(resource: &ResourceIdentifier) -> Result<SourceRequest> {
pin_project! {
struct UpdateClient {
#[pin]
ws: WebSocketStream<Upgraded>,
ws: WebSocketStream<TokioIo<Upgraded>>,
ended: bool,
}
}
......@@ -332,8 +333,8 @@ impl<'a> Sink<ClientUpdateInstruction<'a>> for UpdateClient {
}
}
impl From<WebSocketStream<Upgraded>> for UpdateClient {
fn from(ws: WebSocketStream<Upgraded>) -> Self {
impl From<WebSocketStream<TokioIo<Upgraded>>> for UpdateClient {
fn from(ws: WebSocketStream<TokioIo<Upgraded>>) -> Self {
Self { ws, ended: false }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment