-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy pathoperation.rs
250 lines (238 loc) · 8.79 KB
/
operation.rs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{
debug::ValueDebugFormat, get_effects, trace::TraceRawVcs, CollectiblesSource, FxIndexMap,
NonLocalValue, OperationValue, OperationVc, ResolvedVc, TaskInput, Vc,
};
use turbopack_core::{diagnostics::Diagnostic, issue::IssueDescriptionExt};
use crate::{
entrypoints::Entrypoints,
route::{Endpoint, Route},
};
/// Based on [`Entrypoints`], but with [`OperationVc<Endpoint>`][OperationVc] for every endpoint.
///
/// This is used when constructing `ExternalEndpoint`s in the `napi` crate.
///
/// This is important as `OperationVc`s can be stored in the VersionedContentMap and can be exposed
/// to JS via napi.
///
/// This is needed to call `write_to_disk` which expects an `OperationVc<Endpoint>`.
#[turbo_tasks::value(shared)]
pub struct EntrypointsOperation {
pub routes: FxIndexMap<RcStr, RouteOperation>,
pub middleware: Option<MiddlewareOperation>,
pub instrumentation: Option<InstrumentationOperation>,
pub pages_document_endpoint: OperationVc<OptionEndpoint>,
pub pages_app_endpoint: OperationVc<OptionEndpoint>,
pub pages_error_endpoint: OperationVc<OptionEndpoint>,
}
/// Removes diagnostics, issues, and effects from the top-level `entrypoints` operation so that
/// they're not duplicated across many different individual entrypoints or routes.
#[turbo_tasks::function(operation)]
async fn entrypoints_without_collectibles_operation(
entrypoints: OperationVc<Entrypoints>,
) -> Result<Vc<Entrypoints>> {
let _ = entrypoints.resolve_strongly_consistent().await?;
let _ = entrypoints.take_collectibles::<Box<dyn Diagnostic>>();
let _ = entrypoints.take_issues_with_path().await?;
let _ = get_effects(entrypoints).await?;
Ok(entrypoints.connect())
}
#[turbo_tasks::value_impl]
impl EntrypointsOperation {
#[turbo_tasks::function(operation)]
pub async fn new(entrypoints: OperationVc<Entrypoints>) -> Result<Vc<Self>> {
let e = entrypoints.connect().await?;
let entrypoints = entrypoints_without_collectibles_operation(entrypoints);
Ok(Self {
routes: e
.routes
.iter()
.map(|(k, v)| (k.clone(), pick_route(entrypoints, k.clone(), v)))
.collect(),
middleware: e.middleware.as_ref().map(|_| MiddlewareOperation {
endpoint: pick_endpoint(entrypoints, EndpointSelector::Middleware),
}),
instrumentation: e
.instrumentation
.as_ref()
.map(|_| InstrumentationOperation {
node_js: pick_endpoint(entrypoints, EndpointSelector::InstrumentationNodeJs),
edge: pick_endpoint(entrypoints, EndpointSelector::InstrumentationEdge),
}),
pages_document_endpoint: pick_endpoint(entrypoints, EndpointSelector::PagesDocument),
pages_app_endpoint: pick_endpoint(entrypoints, EndpointSelector::PagesApp),
pages_error_endpoint: pick_endpoint(entrypoints, EndpointSelector::PagesError),
}
.cell())
}
}
fn pick_route(entrypoints: OperationVc<Entrypoints>, key: RcStr, route: &Route) -> RouteOperation {
match route {
Route::Page { .. } => RouteOperation::Page {
html_endpoint: pick_endpoint(entrypoints, EndpointSelector::RoutePageHtml(key.clone())),
data_endpoint: pick_endpoint(entrypoints, EndpointSelector::RoutePageData(key)),
},
Route::PageApi { .. } => RouteOperation::PageApi {
endpoint: pick_endpoint(entrypoints, EndpointSelector::RoutePageApi(key)),
},
Route::AppPage(pages) => RouteOperation::AppPage(
pages
.iter()
.enumerate()
.map(|(i, p)| AppPageRouteOperation {
original_name: p.original_name.clone(),
html_endpoint: pick_endpoint(
entrypoints,
EndpointSelector::RouteAppPageHtml(key.clone(), i),
),
rsc_endpoint: pick_endpoint(
entrypoints,
EndpointSelector::RouteAppPageRsc(key.clone(), i),
),
})
.collect(),
),
Route::AppRoute { original_name, .. } => RouteOperation::AppRoute {
original_name: original_name.clone(),
endpoint: pick_endpoint(entrypoints, EndpointSelector::RouteAppRoute(key)),
},
Route::Conflict => RouteOperation::Conflict,
}
}
#[derive(
Debug,
Clone,
TaskInput,
Serialize,
Deserialize,
TraceRawVcs,
PartialEq,
Eq,
Hash,
ValueDebugFormat,
NonLocalValue,
OperationValue,
)]
enum EndpointSelector {
RoutePageHtml(RcStr),
RoutePageData(RcStr),
RoutePageApi(RcStr),
RouteAppPageHtml(RcStr, usize),
RouteAppPageRsc(RcStr, usize),
RouteAppRoute(RcStr),
InstrumentationNodeJs,
InstrumentationEdge,
Middleware,
PagesDocument,
PagesApp,
PagesError,
}
#[turbo_tasks::value(transparent)]
pub struct OptionEndpoint(Option<ResolvedVc<Box<dyn Endpoint>>>);
/// Given a selector and the `Entrypoints` operation that it comes from, connect the operation and
/// return an `OperationVc` containing the selected value. The returned operation will keep the
/// entire `Entrypoints` operation alive.
#[turbo_tasks::function(operation)]
async fn pick_endpoint(
op: OperationVc<Entrypoints>,
selector: EndpointSelector,
) -> Result<Vc<OptionEndpoint>> {
let endpoints = op.connect().strongly_consistent().await?;
let endpoint = match selector {
EndpointSelector::InstrumentationNodeJs => {
endpoints.instrumentation.as_ref().map(|i| i.node_js)
}
EndpointSelector::InstrumentationEdge => endpoints.instrumentation.as_ref().map(|i| i.edge),
EndpointSelector::Middleware => endpoints.middleware.as_ref().map(|m| m.endpoint),
EndpointSelector::PagesDocument => Some(endpoints.pages_document_endpoint),
EndpointSelector::PagesApp => Some(endpoints.pages_app_endpoint),
EndpointSelector::PagesError => Some(endpoints.pages_error_endpoint),
EndpointSelector::RoutePageHtml(name) => {
if let Some(Route::Page { html_endpoint, .. }) = endpoints.routes.get(&name) {
Some(*html_endpoint)
} else {
None
}
}
EndpointSelector::RoutePageData(name) => {
if let Some(Route::Page { data_endpoint, .. }) = endpoints.routes.get(&name) {
Some(*data_endpoint)
} else {
None
}
}
EndpointSelector::RoutePageApi(name) => {
if let Some(Route::PageApi { endpoint }) = endpoints.routes.get(&name) {
Some(*endpoint)
} else {
None
}
}
EndpointSelector::RouteAppPageHtml(name, i) => {
if let Some(Route::AppPage(pages)) = endpoints.routes.get(&name) {
pages.get(i).as_ref().map(|p| p.html_endpoint)
} else {
None
}
}
EndpointSelector::RouteAppPageRsc(name, i) => {
if let Some(Route::AppPage(pages)) = endpoints.routes.get(&name) {
pages.get(i).as_ref().map(|p| p.rsc_endpoint)
} else {
None
}
}
EndpointSelector::RouteAppRoute(name) => {
if let Some(Route::AppRoute { endpoint, .. }) = endpoints.routes.get(&name) {
Some(*endpoint)
} else {
None
}
}
};
Ok(Vc::cell(endpoint))
}
#[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, ValueDebugFormat, NonLocalValue)]
pub struct InstrumentationOperation {
pub node_js: OperationVc<OptionEndpoint>,
pub edge: OperationVc<OptionEndpoint>,
}
#[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, ValueDebugFormat, NonLocalValue)]
pub struct MiddlewareOperation {
pub endpoint: OperationVc<OptionEndpoint>,
}
#[turbo_tasks::value(shared)]
#[derive(Clone, Debug)]
pub enum RouteOperation {
Page {
html_endpoint: OperationVc<OptionEndpoint>,
data_endpoint: OperationVc<OptionEndpoint>,
},
PageApi {
endpoint: OperationVc<OptionEndpoint>,
},
AppPage(Vec<AppPageRouteOperation>),
AppRoute {
original_name: RcStr,
endpoint: OperationVc<OptionEndpoint>,
},
Conflict,
}
#[derive(
TraceRawVcs,
Serialize,
Deserialize,
PartialEq,
Eq,
ValueDebugFormat,
Clone,
Debug,
NonLocalValue,
)]
pub struct AppPageRouteOperation {
pub original_name: RcStr,
pub html_endpoint: OperationVc<OptionEndpoint>,
pub rsc_endpoint: OperationVc<OptionEndpoint>,
}