File size: 6,081 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::iter::once;

use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::introspect::{Introspectable, IntrospectableChildren};

use super::{
    ContentSource, ContentSourceContent, ContentSourceData, ContentSourceDataVary,
    GetContentSourceContent,
    route_tree::{BaseSegment, RouteTree, RouteTrees},
};
use crate::source::{ContentSources, route_tree::MapGetContentSourceContent};

/// Binds different ContentSources to different subpaths.
///
/// The request path must begin with the prefix, which will be stripped (along with the subpath)
/// before querying the ContentSource. A fallback ContentSource will serve all
/// other subpaths, including if the request path does not include the prefix.
#[turbo_tasks::value(shared)]
pub struct PrefixedRouterContentSource {
    pub prefix: ResolvedVc<RcStr>,
    pub routes: Vec<(RcStr, ResolvedVc<Box<dyn ContentSource>>)>,
    pub fallback: ResolvedVc<Box<dyn ContentSource>>,
}

#[turbo_tasks::value_impl]
impl PrefixedRouterContentSource {
    #[turbo_tasks::function]
    pub fn new(
        prefix: ResolvedVc<RcStr>,
        routes: Vec<(RcStr, ResolvedVc<Box<dyn ContentSource>>)>,
        fallback: ResolvedVc<Box<dyn ContentSource>>,
    ) -> Vc<Self> {
        PrefixedRouterContentSource {
            prefix,
            routes,
            fallback,
        }
        .cell()
    }
}

fn get_children(
    routes: &[(RcStr, ResolvedVc<Box<dyn ContentSource>>)],
    fallback: &ResolvedVc<Box<dyn ContentSource>>,
) -> Vc<ContentSources> {
    Vc::cell(
        routes
            .iter()
            .map(|r| r.1)
            .chain(std::iter::once(*fallback))
            .collect(),
    )
}

async fn get_introspection_children(
    routes: &[(RcStr, ResolvedVc<Box<dyn ContentSource>>)],
    fallback: &ResolvedVc<Box<dyn ContentSource>>,
) -> Result<Vc<IntrospectableChildren>> {
    Ok(Vc::cell(
        routes
            .iter()
            .cloned()
            .chain(std::iter::once((RcStr::default(), *fallback)))
            .filter_map(|(path, source)| {
                ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(source).map(|i| (path, i))
            })
            .collect(),
    ))
}

#[turbo_tasks::value_impl]
impl ContentSource for PrefixedRouterContentSource {
    #[turbo_tasks::function]
    async fn get_routes(&self) -> Result<Vc<RouteTree>> {
        let prefix = &*self.prefix.await?;
        if cfg!(debug_assertions) {
            debug_assert!(prefix.is_empty() || prefix.ends_with('/'));
            debug_assert!(!prefix.starts_with('/'));
        }

        let prefix = if prefix.is_empty() {
            Vec::new()
        } else {
            BaseSegment::from_static_pathname(prefix.as_str()).collect()
        };

        let inner_trees = self.routes.iter().map(|(path, source)| {
            let prepended_base = prefix
                .iter()
                .cloned()
                .chain(BaseSegment::from_static_pathname(path))
                .collect();
            source
                .get_routes()
                .with_prepended_base(prepended_base)
                .map_routes(Vc::upcast(
                    PrefixedRouterContentSourceMapper {
                        prefix: self.prefix,
                        path: path.clone(),
                    }
                    .cell(),
                ))
        });
        Ok(Vc::<RouteTrees>::cell(
            inner_trees
                .chain(once(self.fallback.get_routes()))
                .map(|v| async move { v.to_resolved().await })
                .try_join()
                .await?,
        )
        .merge())
    }

    #[turbo_tasks::function]
    fn get_children(&self) -> Vc<ContentSources> {
        get_children(&self.routes, &self.fallback)
    }
}

#[turbo_tasks::value]
struct PrefixedRouterContentSourceMapper {
    prefix: ResolvedVc<RcStr>,
    path: RcStr,
}

#[turbo_tasks::value_impl]
impl MapGetContentSourceContent for PrefixedRouterContentSourceMapper {
    #[turbo_tasks::function]
    fn map_get_content(
        self: ResolvedVc<Self>,
        get_content: ResolvedVc<Box<dyn GetContentSourceContent>>,
    ) -> Vc<Box<dyn GetContentSourceContent>> {
        Vc::upcast(
            PrefixedRouterGetContentSourceContent {
                mapper: self,
                get_content,
            }
            .cell(),
        )
    }
}

#[turbo_tasks::value]
struct PrefixedRouterGetContentSourceContent {
    mapper: ResolvedVc<PrefixedRouterContentSourceMapper>,
    get_content: ResolvedVc<Box<dyn GetContentSourceContent>>,
}

#[turbo_tasks::value_impl]
impl GetContentSourceContent for PrefixedRouterGetContentSourceContent {
    #[turbo_tasks::function]
    fn vary(&self) -> Vc<ContentSourceDataVary> {
        self.get_content.vary()
    }

    #[turbo_tasks::function]
    async fn get(&self, path: RcStr, data: ContentSourceData) -> Result<Vc<ContentSourceContent>> {
        let prefix = self.mapper.await?.prefix.await?;
        if let Some(path) = path.strip_prefix(&**prefix) {
            if path.is_empty() {
                return Ok(self.get_content.get(RcStr::default(), data));
            } else if prefix.is_empty() {
                return Ok(self.get_content.get(path.into(), data));
            } else if let Some(path) = path.strip_prefix('/') {
                return Ok(self.get_content.get(path.into(), data));
            }
        }
        Ok(ContentSourceContent::not_found())
    }
}

#[turbo_tasks::value_impl]
impl Introspectable for PrefixedRouterContentSource {
    #[turbo_tasks::function]
    fn ty(&self) -> Vc<RcStr> {
        Vc::cell(rcstr!("prefixed router content source"))
    }

    #[turbo_tasks::function]
    async fn details(&self) -> Result<Vc<RcStr>> {
        let prefix = self.prefix.await?;
        Ok(Vc::cell(format!("prefix: '{prefix}'").into()))
    }

    #[turbo_tasks::function]
    async fn children(&self) -> Result<Vc<IntrospectableChildren>> {
        get_introspection_children(&self.routes, &self.fallback).await
    }
}