File size: 11,239 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 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
use anyhow::{Result, anyhow, bail};
use async_stream::try_stream as generator;
use futures::{
SinkExt, StreamExt, TryStreamExt,
channel::mpsc::{UnboundedSender, unbounded},
pin_mut,
};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
RawVc, ResolvedVc, TaskInput, ValueToString, Vc, VcValueType, duration_span, mark_finished,
prevent_gc, trace::TraceRawVcs, util::SharedError,
};
use turbo_tasks_bytes::{Bytes, Stream};
use turbo_tasks_env::ProcessEnv;
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{ChunkingContext, EvaluatableAsset, EvaluatableAssets},
error::PrettyPrintError,
issue::{IssueExt, StyledString},
module::Module,
};
use turbopack_dev_server::source::{Body, ProxyResult};
use super::{
RenderData, RenderProxyIncomingMessage, RenderProxyOutgoingMessage, ResponseHeaders,
issue::RenderingIssue,
};
use crate::{
get_intermediate_asset, get_renderer_pool_operation, pool::NodeJsOperation,
render::error_page::error_html, source_map::trace_stack,
};
/// Renders a module as static HTML in a node.js process.
#[turbo_tasks::function(operation)]
pub async fn render_proxy_operation(
cwd: FileSystemPath,
env: ResolvedVc<Box<dyn ProcessEnv>>,
path: FileSystemPath,
module: ResolvedVc<Box<dyn EvaluatableAsset>>,
runtime_entries: ResolvedVc<EvaluatableAssets>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
intermediate_output_path: FileSystemPath,
output_root: FileSystemPath,
project_dir: FileSystemPath,
data: ResolvedVc<RenderData>,
body: ResolvedVc<Body>,
debug: bool,
) -> Result<Vc<ProxyResult>> {
let render = render_stream(RenderStreamOptions {
cwd,
env,
path,
module,
runtime_entries,
chunking_context,
intermediate_output_path,
output_root,
project_dir,
data,
body,
debug,
})
.await?;
let mut stream = render.read();
let first = match stream.try_next().await? {
Some(f) => f,
None => {
// If an Error was received first, then it would have been
// transformed into a proxy err error response.
bail!("did not receive response from render");
}
};
let RenderItem::Headers(data) = first else {
bail!("did not receive headers from render");
};
let body = Body::from_stream(stream.map(|item| match item {
Ok(RenderItem::BodyChunk(b)) => Ok(b),
Ok(v) => Err(SharedError::new(anyhow!(
"unexpected render item: {:#?}",
v
))),
Err(e) => Err(e),
}));
let result = ProxyResult {
status: data.status,
headers: data.headers,
body,
};
Ok(result.cell())
}
async fn proxy_error(
path: FileSystemPath,
error: anyhow::Error,
operation: Option<NodeJsOperation>,
) -> Result<(u16, RcStr)> {
let message = format!("{}", PrettyPrintError(&error));
let status = match operation {
Some(operation) => Some(operation.wait_or_kill().await?),
None => None,
};
let mut details = vec![];
if let Some(status) = status {
details.push(format!("status: {status}"));
}
let status_code = 500;
let body = error_html(
status_code,
rcstr!("An error occurred while proxying the request to Node.js"),
format!("{message}\n\n{}", details.join("\n")).into(),
)
.owned()
.await?;
RenderingIssue {
file_path: path,
message: StyledString::Text(message.into()).resolved_cell(),
status: status.and_then(|status| status.code()),
}
.resolved_cell()
.emit();
Ok((status_code, body))
}
#[derive(Clone, Debug)]
#[turbo_tasks::value]
enum RenderItem {
Headers(ResponseHeaders),
BodyChunk(Bytes),
}
type RenderItemResult = Result<RenderItem, SharedError>;
#[turbo_tasks::value(eq = "manual", cell = "new", serialization = "none")]
struct RenderStreamSender {
#[turbo_tasks(trace_ignore, debug_ignore)]
get: Box<dyn Fn() -> UnboundedSender<RenderItemResult> + Send + Sync>,
}
#[turbo_tasks::value(transparent)]
struct RenderStream(#[turbo_tasks(trace_ignore)] Stream<RenderItemResult>);
#[derive(Clone, Debug, TaskInput, PartialEq, Eq, Hash, Serialize, Deserialize, TraceRawVcs)]
struct RenderStreamOptions {
cwd: FileSystemPath,
env: ResolvedVc<Box<dyn ProcessEnv>>,
path: FileSystemPath,
module: ResolvedVc<Box<dyn EvaluatableAsset>>,
runtime_entries: ResolvedVc<EvaluatableAssets>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
intermediate_output_path: FileSystemPath,
output_root: FileSystemPath,
project_dir: FileSystemPath,
data: ResolvedVc<RenderData>,
body: ResolvedVc<Body>,
debug: bool,
}
#[turbo_tasks::function]
fn render_stream(options: RenderStreamOptions) -> Vc<RenderStream> {
// TODO: The way we invoke render_stream_internal as side effect is not
// GC-safe, so we disable GC for this task.
prevent_gc();
// Note the following code uses some hacks to create a child task that produces
// a stream that is returned by this task.
// We create a new cell in this task, which will be updated from the
// [render_stream_internal] task.
let cell = turbo_tasks::macro_helpers::find_cell_by_type(
<RenderStream as VcValueType>::get_value_type_id(),
);
// We initialize the cell with a stream that is open, but has no values.
// The first [render_stream_internal] pipe call will pick up that stream.
let (sender, receiver) = unbounded();
cell.update(RenderStream(Stream::new_open(vec![], Box::new(receiver))));
let initial = Mutex::new(Some(sender));
// run the evaluation as side effect
let _ = render_stream_internal(
options,
RenderStreamSender {
get: Box::new(move || {
if let Some(sender) = initial.lock().take() {
sender
} else {
// In cases when only [render_stream_internal] is (re)executed, we need to
// update the old stream with a new value.
let (sender, receiver) = unbounded();
cell.update(RenderStream(Stream::new_open(vec![], Box::new(receiver))));
sender
}
}),
}
.cell(),
);
let raw: RawVc = cell.into();
raw.into()
}
#[turbo_tasks::function]
async fn render_stream_internal(
options: RenderStreamOptions,
sender: Vc<RenderStreamSender>,
) -> Result<Vc<()>> {
let RenderStreamOptions {
cwd,
env,
path,
module,
runtime_entries,
chunking_context,
intermediate_output_path,
output_root,
project_dir,
data,
body,
debug,
} = options;
mark_finished();
let Ok(sender) = sender.await else {
// Impossible to handle the error in a good way.
return Ok(Default::default());
};
let stream = generator! {
let intermediate_asset = get_intermediate_asset(
*chunking_context,
*module,
*runtime_entries,
).to_resolved().await?;
let pool_op = get_renderer_pool_operation(
cwd,
env,
intermediate_asset,
intermediate_output_path.clone(),
output_root,
project_dir.clone(),
debug,
);
// Read this strongly consistent, since we don't want to run inconsistent
// node.js code.
let pool = pool_op.read_strongly_consistent().await?;
let data = data.await?;
let mut operation = pool.operation().await?;
// First, send the render data.
operation
.send(RenderProxyOutgoingMessage::Headers { data: &data })
.await?;
// Then, send the binary body in chunks.
let mut body = body.await?.read();
while let Some(data) = body.next().await {
operation
.send(RenderProxyOutgoingMessage::BodyChunk { data: &data.unwrap() })
.await?;
}
operation.send(RenderProxyOutgoingMessage::BodyEnd).await?;
let entry = module.ident().to_string().await?;
let guard = duration_span!("Node.js api execution", entry = display(entry));
match operation.recv().await? {
RenderProxyIncomingMessage::Headers { data } => yield RenderItem::Headers(data),
RenderProxyIncomingMessage::Error(error) => {
drop(guard);
// If we don't get headers, then something is very wrong. Instead, we send down a
// 500 proxy error as if it were the proper result.
let trace = trace_stack(
error,
*intermediate_asset,
intermediate_output_path.clone(),
project_dir.clone()
)
.await?;
let (status, body) = proxy_error(path, anyhow!("error rendering: {}", trace), Some(operation)).await?;
yield RenderItem::Headers(ResponseHeaders {
status,
headers: vec![(
rcstr!("content-type"),
rcstr!("text/html; charset=utf-8"),
)],
});
yield RenderItem::BodyChunk(body.into_owned().into_bytes().into());
return;
}
v => {
drop(guard);
Err(anyhow!("unexpected message during rendering: {:#?}", v))?;
return;
},
};
loop {
match operation.recv().await? {
RenderProxyIncomingMessage::BodyChunk { data } => {
yield RenderItem::BodyChunk(data.into());
}
RenderProxyIncomingMessage::BodyEnd => break,
RenderProxyIncomingMessage::Error(error) => {
drop(guard);
// We have already started to send a result, so we can't change the
// headers/body to a proxy error.
operation.disallow_reuse();
let trace =
trace_stack(error, *intermediate_asset, intermediate_output_path.clone(), project_dir.clone()).await?;
Err(anyhow!("error during streaming render: {}", trace))?;
return;
}
v => {
drop(guard);
Err(anyhow!("unexpected message during rendering: {:#?}", v))?;
return;
},
}
}
drop(guard);
};
let mut sender = (sender.get)();
pin_mut!(stream);
while let Some(value) = stream.next().await {
if sender.send(value).await.is_err() {
return Ok(Default::default());
}
if sender.flush().await.is_err() {
return Ok(Default::default());
}
}
Ok(Default::default())
}
|