|
|
pub mod asset_graph; |
|
|
pub mod combined; |
|
|
pub mod conditional; |
|
|
pub mod headers; |
|
|
pub mod issue_context; |
|
|
pub mod lazy_instantiated; |
|
|
pub mod query; |
|
|
pub mod request; |
|
|
pub(crate) mod resolve; |
|
|
pub mod route_tree; |
|
|
pub mod router; |
|
|
pub mod static_assets; |
|
|
pub mod wrapping_source; |
|
|
|
|
|
use std::collections::BTreeSet; |
|
|
|
|
|
use anyhow::Result; |
|
|
use futures::{TryStreamExt, stream::Stream as StreamTrait}; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
use turbo_rcstr::RcStr; |
|
|
use turbo_tasks::{ |
|
|
Completion, NonLocalValue, OperationVc, ResolvedVc, TaskInput, Upcast, ValueDefault, Vc, |
|
|
trace::TraceRawVcs, util::SharedError, |
|
|
}; |
|
|
use turbo_tasks_bytes::{Bytes, Stream, StreamRead}; |
|
|
use turbo_tasks_fs::FileSystemPath; |
|
|
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher, Xxh3Hash64Hasher}; |
|
|
use turbopack_core::version::{Version, VersionedContent}; |
|
|
|
|
|
use self::{ |
|
|
headers::Headers, issue_context::IssueFilePathContentSource, query::Query, |
|
|
route_tree::RouteTree, |
|
|
}; |
|
|
|
|
|
|
|
|
#[turbo_tasks::value(shared, operation)] |
|
|
pub struct ProxyResult { |
|
|
|
|
|
pub status: u16, |
|
|
|
|
|
pub headers: Vec<(RcStr, RcStr)>, |
|
|
|
|
|
#[turbo_tasks(trace_ignore)] |
|
|
pub body: Body, |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl Version for ProxyResult { |
|
|
#[turbo_tasks::function] |
|
|
async fn id(&self) -> Result<Vc<RcStr>> { |
|
|
let mut hash = Xxh3Hash64Hasher::new(); |
|
|
hash.write_u16(self.status); |
|
|
for (name, value) in &self.headers { |
|
|
name.deterministic_hash(&mut hash); |
|
|
value.deterministic_hash(&mut hash); |
|
|
} |
|
|
let mut read = self.body.read(); |
|
|
while let Some(chunk) = read.try_next().await? { |
|
|
hash.write_bytes(&chunk); |
|
|
} |
|
|
Ok(Vc::cell(hash.finish().to_string().into())) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
pub trait GetContentSourceContent { |
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
fn vary(self: Vc<Self>) -> Vc<ContentSourceDataVary> { |
|
|
ContentSourceDataVary::default().cell() |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
fn get(self: Vc<Self>, path: RcStr, data: ContentSourceData) -> Vc<ContentSourceContent>; |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value(transparent)] |
|
|
pub struct GetContentSourceContents(Vec<ResolvedVc<Box<dyn GetContentSourceContent>>>); |
|
|
|
|
|
#[turbo_tasks::value] |
|
|
pub struct StaticContent { |
|
|
pub content: ResolvedVc<Box<dyn VersionedContent>>, |
|
|
pub status_code: u16, |
|
|
pub headers: ResolvedVc<HeaderList>, |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value(shared)] |
|
|
|
|
|
|
|
|
pub enum ContentSourceContent { |
|
|
NotFound, |
|
|
Static(ResolvedVc<StaticContent>), |
|
|
HttpProxy(OperationVc<ProxyResult>), |
|
|
Rewrite(ResolvedVc<Rewrite>), |
|
|
|
|
|
Next, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
pub trait ContentSourceSideEffect { |
|
|
#[turbo_tasks::function] |
|
|
fn apply(self: Vc<Self>) -> Vc<Completion>; |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl GetContentSourceContent for ContentSourceContent { |
|
|
#[turbo_tasks::function] |
|
|
fn get(self: Vc<Self>, _path: RcStr, _data: ContentSourceData) -> Vc<ContentSourceContent> { |
|
|
self |
|
|
} |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl ContentSourceContent { |
|
|
#[turbo_tasks::function] |
|
|
pub async fn static_content( |
|
|
content: ResolvedVc<Box<dyn VersionedContent>>, |
|
|
) -> Result<Vc<ContentSourceContent>> { |
|
|
Ok(ContentSourceContent::Static( |
|
|
StaticContent { |
|
|
content, |
|
|
status_code: 200, |
|
|
headers: HeaderList::empty().to_resolved().await?, |
|
|
} |
|
|
.resolved_cell(), |
|
|
) |
|
|
.cell()) |
|
|
} |
|
|
|
|
|
#[turbo_tasks::function] |
|
|
pub fn static_with_headers( |
|
|
content: ResolvedVc<Box<dyn VersionedContent>>, |
|
|
status_code: u16, |
|
|
headers: ResolvedVc<HeaderList>, |
|
|
) -> Vc<ContentSourceContent> { |
|
|
ContentSourceContent::Static( |
|
|
StaticContent { |
|
|
content, |
|
|
status_code, |
|
|
headers, |
|
|
} |
|
|
.resolved_cell(), |
|
|
) |
|
|
.cell() |
|
|
} |
|
|
|
|
|
#[turbo_tasks::function] |
|
|
pub fn not_found() -> Vc<ContentSourceContent> { |
|
|
ContentSourceContent::NotFound.cell() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::value(transparent)] |
|
|
pub struct HeaderList(Vec<(RcStr, RcStr)>); |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl HeaderList { |
|
|
#[turbo_tasks::function] |
|
|
pub fn new(headers: Vec<(RcStr, RcStr)>) -> Vc<Self> { |
|
|
HeaderList(headers).cell() |
|
|
} |
|
|
|
|
|
#[turbo_tasks::function] |
|
|
pub fn empty() -> Vc<Self> { |
|
|
HeaderList(vec![]).cell() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive( |
|
|
PartialEq, |
|
|
Eq, |
|
|
NonLocalValue, |
|
|
TraceRawVcs, |
|
|
Serialize, |
|
|
Deserialize, |
|
|
Clone, |
|
|
Debug, |
|
|
Hash, |
|
|
Default, |
|
|
TaskInput, |
|
|
)] |
|
|
pub struct ContentSourceData { |
|
|
|
|
|
pub method: Option<RcStr>, |
|
|
|
|
|
pub url: Option<RcStr>, |
|
|
|
|
|
|
|
|
pub original_url: Option<RcStr>, |
|
|
|
|
|
pub query: Option<Query>, |
|
|
|
|
|
pub raw_query: Option<RcStr>, |
|
|
|
|
|
|
|
|
pub headers: Option<Headers>, |
|
|
|
|
|
|
|
|
pub raw_headers: Option<Vec<(RcStr, RcStr)>>, |
|
|
|
|
|
pub body: Option<ResolvedVc<Body>>, |
|
|
|
|
|
pub cache_buster: u64, |
|
|
} |
|
|
|
|
|
pub type BodyChunk = Result<Bytes, SharedError>; |
|
|
|
|
|
|
|
|
#[turbo_tasks::value(shared)] |
|
|
#[derive(Default, Clone, Debug)] |
|
|
pub struct Body { |
|
|
#[turbo_tasks(trace_ignore)] |
|
|
chunks: Stream<BodyChunk>, |
|
|
} |
|
|
|
|
|
impl Body { |
|
|
|
|
|
pub fn new(chunks: Vec<BodyChunk>) -> Self { |
|
|
Self { |
|
|
chunks: Stream::new_closed(chunks), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn read(&self) -> StreamRead<BodyChunk> { |
|
|
self.chunks.read() |
|
|
} |
|
|
|
|
|
pub fn from_stream<T: StreamTrait<Item = BodyChunk> + Send + Unpin + 'static>( |
|
|
source: T, |
|
|
) -> Self { |
|
|
Self { |
|
|
chunks: Stream::from(source), |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T: Into<Bytes>> From<T> for Body { |
|
|
fn from(value: T) -> Self { |
|
|
Body::new(vec![Ok(value.into())]) |
|
|
} |
|
|
} |
|
|
|
|
|
impl ValueDefault for Body { |
|
|
fn value_default() -> Vc<Self> { |
|
|
Body::default().cell() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, TraceRawVcs, Hash, Serialize, Deserialize, NonLocalValue)] |
|
|
pub enum ContentSourceDataFilter { |
|
|
All, |
|
|
Subset(BTreeSet<String>), |
|
|
} |
|
|
|
|
|
impl ContentSourceDataFilter { |
|
|
|
|
|
pub fn extend(&mut self, other: &ContentSourceDataFilter) { |
|
|
match self { |
|
|
ContentSourceDataFilter::All => {} |
|
|
ContentSourceDataFilter::Subset(set) => match other { |
|
|
ContentSourceDataFilter::All => *self = ContentSourceDataFilter::All, |
|
|
ContentSourceDataFilter::Subset(set2) => set.extend(set2.iter().cloned()), |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn extend_options( |
|
|
this: &mut Option<ContentSourceDataFilter>, |
|
|
other: &Option<ContentSourceDataFilter>, |
|
|
) { |
|
|
if let Some(this) = this.as_mut() { |
|
|
if let Some(other) = other.as_ref() { |
|
|
this.extend(other); |
|
|
} |
|
|
} else { |
|
|
this.clone_from(other); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn contains(&self, key: &str) -> bool { |
|
|
match self { |
|
|
ContentSourceDataFilter::All => true, |
|
|
ContentSourceDataFilter::Subset(set) => set.contains(key), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn fulfills( |
|
|
this: &Option<ContentSourceDataFilter>, |
|
|
other: &Option<ContentSourceDataFilter>, |
|
|
) -> bool { |
|
|
match (this, other) { |
|
|
(_, None) => true, |
|
|
(None, Some(_)) => false, |
|
|
(Some(this), Some(other)) => match (this, other) { |
|
|
(ContentSourceDataFilter::All, _) => true, |
|
|
(_, ContentSourceDataFilter::All) => false, |
|
|
(ContentSourceDataFilter::Subset(this), ContentSourceDataFilter::Subset(other)) => { |
|
|
this.is_superset(other) |
|
|
} |
|
|
}, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value(shared)] |
|
|
#[derive(Debug, Default, Clone, Hash)] |
|
|
pub struct ContentSourceDataVary { |
|
|
pub method: bool, |
|
|
pub url: bool, |
|
|
pub original_url: bool, |
|
|
pub query: Option<ContentSourceDataFilter>, |
|
|
pub raw_query: bool, |
|
|
pub headers: Option<ContentSourceDataFilter>, |
|
|
pub raw_headers: bool, |
|
|
pub body: bool, |
|
|
|
|
|
|
|
|
|
|
|
pub cache_buster: bool, |
|
|
pub placeholder_for_future_extensions: (), |
|
|
} |
|
|
|
|
|
impl ContentSourceDataVary { |
|
|
|
|
|
|
|
|
pub fn extend(&mut self, other: &ContentSourceDataVary) { |
|
|
let ContentSourceDataVary { |
|
|
method, |
|
|
url, |
|
|
original_url, |
|
|
query, |
|
|
raw_query, |
|
|
headers, |
|
|
raw_headers, |
|
|
body, |
|
|
cache_buster, |
|
|
placeholder_for_future_extensions: _, |
|
|
} = self; |
|
|
*method = *method || other.method; |
|
|
*url = *url || other.url; |
|
|
*original_url = *original_url || other.original_url; |
|
|
*body = *body || other.body; |
|
|
*cache_buster = *cache_buster || other.cache_buster; |
|
|
*raw_query = *raw_query || other.raw_query; |
|
|
*raw_headers = *raw_headers || other.raw_headers; |
|
|
ContentSourceDataFilter::extend_options(query, &other.query); |
|
|
ContentSourceDataFilter::extend_options(headers, &other.headers); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn fulfills(&self, other: &ContentSourceDataVary) -> bool { |
|
|
|
|
|
let ContentSourceDataVary { |
|
|
method, |
|
|
url, |
|
|
original_url, |
|
|
query, |
|
|
raw_query, |
|
|
headers, |
|
|
raw_headers, |
|
|
body, |
|
|
cache_buster, |
|
|
placeholder_for_future_extensions: _, |
|
|
} = self; |
|
|
if other.method && !method { |
|
|
return false; |
|
|
} |
|
|
if other.url && !url { |
|
|
return false; |
|
|
} |
|
|
if other.original_url && !original_url { |
|
|
return false; |
|
|
} |
|
|
if other.body && !body { |
|
|
return false; |
|
|
} |
|
|
if other.raw_query && !raw_query { |
|
|
return false; |
|
|
} |
|
|
if other.raw_headers && !raw_headers { |
|
|
return false; |
|
|
} |
|
|
if other.cache_buster && !cache_buster { |
|
|
return false; |
|
|
} |
|
|
if !ContentSourceDataFilter::fulfills(query, &other.query) { |
|
|
return false; |
|
|
} |
|
|
if !ContentSourceDataFilter::fulfills(headers, &other.headers) { |
|
|
return false; |
|
|
} |
|
|
true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
pub trait ContentSource { |
|
|
#[turbo_tasks::function] |
|
|
fn get_routes(self: Vc<Self>) -> Vc<RouteTree>; |
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
fn get_children(self: Vc<Self>) -> Vc<ContentSources> { |
|
|
ContentSources::empty() |
|
|
} |
|
|
} |
|
|
|
|
|
pub trait ContentSourceExt { |
|
|
fn issue_file_path( |
|
|
self: Vc<Self>, |
|
|
file_path: FileSystemPath, |
|
|
description: RcStr, |
|
|
) -> Vc<Box<dyn ContentSource>>; |
|
|
} |
|
|
|
|
|
impl<T> ContentSourceExt for T |
|
|
where |
|
|
T: Upcast<Box<dyn ContentSource>>, |
|
|
{ |
|
|
fn issue_file_path( |
|
|
self: Vc<Self>, |
|
|
file_path: FileSystemPath, |
|
|
description: RcStr, |
|
|
) -> Vc<Box<dyn ContentSource>> { |
|
|
Vc::upcast(IssueFilePathContentSource::new_file_path( |
|
|
file_path, |
|
|
description, |
|
|
Vc::upcast(self), |
|
|
)) |
|
|
} |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value(transparent)] |
|
|
pub struct ContentSources(Vec<ResolvedVc<Box<dyn ContentSource>>>); |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl ContentSources { |
|
|
#[turbo_tasks::function] |
|
|
pub fn empty() -> Vc<Self> { |
|
|
Vc::cell(Vec::new()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value] |
|
|
pub struct NoContentSource; |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl NoContentSource { |
|
|
#[turbo_tasks::function] |
|
|
pub fn new() -> Vc<Self> { |
|
|
NoContentSource.cell() |
|
|
} |
|
|
} |
|
|
#[turbo_tasks::value_impl] |
|
|
impl ContentSource for NoContentSource { |
|
|
#[turbo_tasks::function] |
|
|
fn get_routes(&self) -> Vc<RouteTree> { |
|
|
RouteTree::empty() |
|
|
} |
|
|
} |
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue)] |
|
|
pub enum RewriteType { |
|
|
Location { |
|
|
|
|
|
|
|
|
path_and_query: RcStr, |
|
|
}, |
|
|
ContentSource { |
|
|
|
|
|
|
|
|
source: OperationVc<Box<dyn ContentSource>>, |
|
|
|
|
|
|
|
|
path_and_query: RcStr, |
|
|
}, |
|
|
Sources { |
|
|
|
|
|
|
|
|
sources: OperationVc<GetContentSourceContents>, |
|
|
}, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value(shared)] |
|
|
#[derive(Debug)] |
|
|
pub struct Rewrite { |
|
|
pub ty: RewriteType, |
|
|
|
|
|
|
|
|
|
|
|
pub response_headers: Option<ResolvedVc<HeaderList>>, |
|
|
|
|
|
|
|
|
|
|
|
pub request_headers: Option<ResolvedVc<HeaderList>>, |
|
|
} |
|
|
|
|
|
pub struct RewriteBuilder { |
|
|
rewrite: Rewrite, |
|
|
} |
|
|
|
|
|
impl RewriteBuilder { |
|
|
pub fn new(path_and_query: RcStr) -> Self { |
|
|
Self { |
|
|
rewrite: Rewrite { |
|
|
ty: RewriteType::Location { path_and_query }, |
|
|
response_headers: None, |
|
|
request_headers: None, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn new_source_with_path_and_query( |
|
|
source: OperationVc<Box<dyn ContentSource>>, |
|
|
path_and_query: RcStr, |
|
|
) -> Self { |
|
|
Self { |
|
|
rewrite: Rewrite { |
|
|
ty: RewriteType::ContentSource { |
|
|
source, |
|
|
path_and_query, |
|
|
}, |
|
|
response_headers: None, |
|
|
request_headers: None, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn new_sources(sources: OperationVc<GetContentSourceContents>) -> Self { |
|
|
Self { |
|
|
rewrite: Rewrite { |
|
|
ty: RewriteType::Sources { sources }, |
|
|
response_headers: None, |
|
|
request_headers: None, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn response_headers(mut self, headers: ResolvedVc<HeaderList>) -> Self { |
|
|
self.rewrite.response_headers = Some(headers); |
|
|
self |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn request_headers(mut self, headers: ResolvedVc<HeaderList>) -> Self { |
|
|
self.rewrite.request_headers = Some(headers); |
|
|
self |
|
|
} |
|
|
|
|
|
pub fn build(self) -> Vc<Rewrite> { |
|
|
self.rewrite.cell() |
|
|
} |
|
|
} |
|
|
|