proa_framework_axum 0.4.1

Axum and Tokio adapter for Proa SSR applications.
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::future::Future;
use std::sync::{Arc, Mutex};

use axum::body::Body;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use futures::{stream, StreamExt as _};
use proa_core::{
    DataLoader, DynDataLoader, DynLoaderHandle, LoadKey, LoadRequest, NoLoader, RenderBuffer,
    RenderMode, RequestContext, RequestLocation,
};
use proa_framework_core::{
    finalize_route_response, finalize_route_response_stream, finalize_route_response_sync,
    FrameworkBody, FrameworkError, FrameworkRequestContext, FrameworkResponse, FrameworkSendStream,
    FrameworkStreamFactory, RouteFinalizeMode, RouteResponse as CoreRouteResponse,
};
use tracing::error;

use super::context::{RouteContext, ROUTE_CONTEXT};
use super::local_executor::{LocalStreamExecutor, LocalStreamPrepareError};
pub use proa_framework_core::{
    drive_dyn_render, drive_dyn_send_render, DynRenderOutcome, DynSendRenderOutcome, DynWebRender,
    DynWebRenderSend,
};

/// Axum adapter for the framework-independent route render plan.
///
/// Defaults to `NoLoader` so handlers that don't install a loader keep
/// writing `-> RouteResponse` unchanged. Handlers that install a typed
/// loader through the typed or dyn loader constructors should declare their return type as
/// `-> impl IntoResponse`, which lets axum infer the concrete `L` without
/// the caller having to spell it out.
pub struct RouteResponse<L: DataLoader = NoLoader>(CoreRouteResponse<L>);

impl RouteResponse<NoLoader> {
    pub fn ssr<T>(component: T) -> Self
    where
        T: proa_core::WebRenderSync<NoLoader, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr(component))
    }

    /// Construct a buffered async route response with `L = NoLoader`.
    ///
    /// Rendering completes before Axum emits the response, so root errors and
    /// response metadata are known before body delivery begins. Use this when
    /// the component never calls `cx.load_json(...)`. For routes
    /// that install a loader, use
    /// [`ssr_async_with_loader`](RouteResponse::<L>::ssr_async_with_loader)
    /// (which fixes `L` to the supplied loader's concrete type) or the
    /// dyn escape hatch
    /// [`ssr_async_with_loader_dyn`](RouteResponse::<DynLoaderHandle>::ssr_async_with_loader_dyn).
    pub fn ssr_async<T>(component: T) -> Self
    where
        T: proa_core::WebRender<NoLoader, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_async(component))
    }

    /// Construct a streamed async route response with `L = NoLoader`.
    ///
    /// Use this when the component never calls `cx.load_json(...)`. For routes
    /// that install a loader, see
    /// [`ssr_stream_with_loader`](RouteResponse::<L>::ssr_stream_with_loader).
    pub fn ssr_stream<T>(component: T) -> Self
    where
        T: proa_core::WebRender<NoLoader, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_stream(component))
    }

    /// Construct a buffered response with a compiler-proved `Send` render graph.
    pub fn ssr_async_send<T>(component: T) -> Self
    where
        T: proa_core::WebRenderSend<NoLoader, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_async_send(component))
    }

    /// Construct a direct Axum stream with no local-executor dispatch.
    pub fn ssr_stream_send<T>(component: T) -> Self
    where
        T: proa_core::WebRenderSend<NoLoader, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_stream_send(component))
    }
}

impl<L: DataLoader + 'static> RouteResponse<L> {
    /// Construct a typed-loader buffered async route response.
    ///
    /// `L` is inferred from `loader`'s type. The component must implement
    /// `WebRender<L>`; `cx.load_json(...)` calls inside it become
    /// direct, monomorphized calls on `L`.
    pub fn ssr_async_with_loader<T>(component: T, loader: Arc<L>) -> Self
    where
        T: proa_core::WebRender<L, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_async_with_loader(component, loader))
    }

    /// Construct a typed-loader streamed async route response.
    pub fn ssr_stream_with_loader<T>(component: T, loader: Arc<L>) -> Self
    where
        T: proa_core::WebRender<L, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_stream_with_loader(component, loader))
    }

    pub fn ssr_async_send_with_loader<T>(component: T, loader: Arc<L>) -> Self
    where
        T: proa_core::WebRenderSend<L, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_async_send_with_loader(
            component, loader,
        ))
    }

    pub fn ssr_stream_send_with_loader<T>(component: T, loader: Arc<L>) -> Self
    where
        T: proa_core::WebRenderSend<L, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_stream_send_with_loader(
            component, loader,
        ))
    }

    /// Enable the request-local document head lifecycle.
    pub fn with_head(self, config: proa_core::HeadConfig) -> Self {
        Self(self.0.with_head(config))
    }

    /// Override app defaults for a manually authored document.
    pub fn without_head(self) -> Self {
        Self(self.0.without_head())
    }

    /// Install a typed loader on the response.
    ///
    /// `L` is the loader's concrete type. `cx.load_json(...)` calls inside the
    /// component become direct, monomorphized calls on `L` rather than going
    /// through a `Box::pin` at the dyn boundary.
    pub fn with_loader(self, loader: Arc<L>) -> Self {
        Self(self.0.with_loader(loader))
    }

    /// Select both render semantics and response delivery for an async plan.
    ///
    /// Chunked modes stream; static and agent modes buffer.
    pub fn with_render_mode(self, render_mode: RenderMode) -> Self {
        Self(self.0.with_render_mode(render_mode))
    }

    /// Render this response to a complete HTML document without an HTTP
    /// server or an async runtime.
    ///
    /// Forwards to [`proa_framework_core::RouteResponse::render_to_bytes`],
    /// which documents how async plans, status codes, and runtimes are
    /// handled. No route context is installed here: the base path is the
    /// origin root, route parameters are absent, and `render_metadata_head`
    /// resolves to nothing. For a page that reads any of those, send a request
    /// through the built router with `tower::ServiceExt::oneshot` instead.
    pub fn render_to_bytes(self) -> Result<bytes::Bytes, FrameworkError> {
        self.0.render_to_bytes()
    }

    /// Render a complete document using the catalog's public URLs and base path.
    /// This performs the same buffered render as [`render_to_bytes`](Self::render_to_bytes).
    ///
    /// A page with RSJS islands links their modules at the canonical
    /// `/rsjs-runtime.js` paths here. When
    /// [`RsjsAssets`](crate::RsjsAssets) serves them, use
    /// [`render_to_bytes_with_assets`](Self::render_to_bytes_with_assets) so
    /// the page links the names it publishes.
    pub fn render_to_bytes_with_public_assets(
        self,
        assets: &crate::PublicAssets,
    ) -> Result<bytes::Bytes, FrameworkError> {
        self.render_to_bytes_with_assets(assets, None)
    }

    /// [`render_to_bytes_with_public_assets`](Self::render_to_bytes_with_public_assets),
    /// linking RSJS modules by the names `rsjs` publishes them under.
    pub fn render_to_bytes_with_assets(
        self,
        assets: &crate::PublicAssets,
        rsjs: Option<&crate::RsjsAssets>,
    ) -> Result<bytes::Bytes, FrameworkError> {
        let mut context = FrameworkRequestContext::default();
        context.request.set_base_path(assets.base_path().clone());
        context
            .request
            .set_public_asset_urls(Some(assets.render_urls()));
        context
            .request
            .set_asset_prefix(assets.asset_prefix().map(crate::AssetPrefix::shared));
        context
            .request
            .set_framework_assets(rsjs.map(|rsjs| rsjs.published().clone()));
        self.0.render_to_bytes_with_context(&context)
    }

    /// [`render_to_bytes`](Self::render_to_bytes) as an owned UTF-8 `String`.
    pub fn render_to_string(self) -> Result<String, FrameworkError> {
        self.0.render_to_string()
    }
}

impl<L: DataLoader> From<CoreRouteResponse<L>> for RouteResponse<L> {
    fn from(response: CoreRouteResponse<L>) -> Self {
        Self(response)
    }
}

impl RouteResponse<DynLoaderHandle> {
    /// Construct an async route response with a type-erased loader.
    ///
    /// This is the dyn-boundary escape hatch constructor: `L` is fixed to
    /// `DynLoaderHandle`, so `cx.load_json(...)` calls inside the component pay
    /// one `Box::pin` at the dyn boundary. Use only when the loader's
    /// concrete type is genuinely not statically known at the route's
    /// construction site; prefer
    /// [`ssr_async_with_loader`](RouteResponse::<L>::ssr_async_with_loader)
    /// for the monomorphized fast path.
    pub fn ssr_async_with_loader_dyn<T>(component: T, loader: Arc<dyn DynDataLoader>) -> Self
    where
        T: proa_core::WebRender<DynLoaderHandle, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_async_with_loader_dyn(
            component, loader,
        ))
    }

    /// Construct a streaming route response with a type-erased loader.
    pub fn ssr_stream_with_loader_dyn<T>(component: T, loader: Arc<dyn DynDataLoader>) -> Self
    where
        T: proa_core::WebRender<DynLoaderHandle, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_stream_with_loader_dyn(
            component, loader,
        ))
    }

    pub fn ssr_async_send_with_loader_dyn<T>(component: T, loader: Arc<dyn DynDataLoader>) -> Self
    where
        T: proa_core::WebRenderSend<DynLoaderHandle, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_async_send_with_loader_dyn(
            component, loader,
        ))
    }

    pub fn ssr_stream_send_with_loader_dyn<T>(component: T, loader: Arc<dyn DynDataLoader>) -> Self
    where
        T: proa_core::WebRenderSend<DynLoaderHandle, RenderBuffer> + Send + 'static,
    {
        Self(CoreRouteResponse::ssr_stream_send_with_loader_dyn(
            component, loader,
        ))
    }

    /// Replace the loader on an existing type-erased response.
    ///
    /// The response must already use `L = DynLoaderHandle`; this method does
    /// not change its loader type. Every `cx.load_json(...)` inside the component
    /// pays one `Box::pin` at the dyn boundary.
    ///
    /// Use [`with_loader`](Self::with_loader) on a typed `L` for the
    /// monomorphized fast path; use this only when the loader's concrete
    /// type is genuinely not statically known at the route's construction
    /// site.
    ///
    /// The supplied loader is wrapped with a request-scoped `DataResolver`
    /// for per-request dedupe.
    pub fn with_loader_dyn(self, loader: Arc<dyn DynDataLoader>) -> Self {
        Self(self.0.with_loader_dyn(loader))
    }

    /// Install in-order preload keys. Only meaningful on the dyn path; the
    /// keys are warmed by a `PreparedLoader<dyn>` wrapper.
    pub fn with_preload_keys<I>(self, preload_keys: I) -> Self
    where
        I: IntoIterator<Item = LoadKey>,
    {
        Self(self.0.with_preload_keys(preload_keys))
    }

    /// Install in-order preload requests. Use this when preload hints need to
    /// reach the underlying loader.
    pub fn with_preload_requests<I>(self, preload_requests: I) -> Self
    where
        I: IntoIterator<Item = LoadRequest>,
    {
        Self(self.0.with_preload_requests(preload_requests))
    }
}

pub(super) fn framework_response_to_axum(response: FrameworkResponse) -> Response {
    let FrameworkResponse {
        status,
        headers,
        body,
    } = response;
    let mut axum_response = match body {
        FrameworkBody::Managed(factory) => {
            let (factory, head) = proa_framework_core::managed_response::adapt(factory);
            let mut response = Response::builder()
                .status(status)
                .body(Body::empty())
                .unwrap();
            response
                .extensions_mut()
                .insert(DeferredFrameworkStream::new(factory));
            response.extensions_mut().insert(ManagedPreparedHead(head));
            response
        }
        FrameworkBody::Full(bytes) => Response::builder()
            .status(status)
            .body(Body::from(bytes))
            .expect("framework full response should build"),
        FrameworkBody::BufferedPages(buffer) => Response::builder()
            .status(status)
            .body(Body::from_stream(stream::iter(
                buffer.into_pages().map(Ok::<_, std::convert::Infallible>),
            )))
            .expect("framework buffered pages response should build"),
        FrameworkBody::Stream(factory) => {
            let mut response = Response::builder()
                .status(status)
                .body(Body::empty())
                .expect("framework deferred stream response should build");
            response
                .extensions_mut()
                .insert(DeferredFrameworkStream::new(factory));
            response
        }
    };
    *axum_response.headers_mut() = headers;
    axum_response
}

#[derive(Clone)]
struct ManagedPreparedHead(proa_framework_core::managed_response::PreparedHead);

/// Response extension overriding the managed-head preparation deadline (default: 30 seconds).
/// Install before the outer `RouteContextLayer` prepares the response. This limits
/// time to publication; it does not change a body stream's lifetime.
#[derive(Clone, Copy)]
pub struct ManagedPreparationTimeout(pub std::time::Duration);

#[derive(Clone)]
pub(super) struct DeferredFrameworkStream {
    factory: Arc<Mutex<Option<FrameworkStreamFactory>>>,
}

impl DeferredFrameworkStream {
    fn new(factory: FrameworkStreamFactory) -> Self {
        Self {
            factory: Arc::new(Mutex::new(Some(factory))),
        }
    }

    fn take(&self) -> Option<FrameworkStreamFactory> {
        self.factory
            .lock()
            .expect("deferred stream mutex poisoned")
            .take()
    }
}

enum StreamPrepareError {
    Overloaded,
    Unavailable(Arc<str>),
    FirstItem(proa_framework_core::FrameworkError),
    TimedOut,
}

pub(super) async fn prepare_deferred_framework_stream(
    mut response: Response,
    executor: LocalStreamExecutor,
) -> Response {
    let Some(deferred) = response
        .extensions_mut()
        .remove::<DeferredFrameworkStream>()
    else {
        return response;
    };
    let Some(factory) = deferred.take() else {
        error!("framework stream factory was already consumed");
        return framework_response_to_axum(FrameworkResponse::plain_text(
            StatusCode::INTERNAL_SERVER_ERROR,
            "render error",
        ));
    };

    let readiness = response
        .extensions()
        .get::<ManagedPreparedHead>()
        .map(|h| h.0.clone());
    let prepared = if readiness.is_some() {
        let deadline = response
            .extensions()
            .get::<ManagedPreparationTimeout>()
            .map_or(std::time::Duration::from_secs(30), |v| v.0);
        match tokio::time::timeout(deadline, prepare_stream(factory, &executor, readiness)).await {
            Ok(result) => result,
            Err(_) => Err(StreamPrepareError::TimedOut),
        }
    } else {
        prepare_stream(factory, &executor, readiness).await
    };
    match prepared {
        Ok(stream) => {
            if let Some(prepared) = response.extensions_mut().remove::<ManagedPreparedHead>() {
                let Some(decisions) = prepared.0.get() else {
                    return framework_response_to_axum(FrameworkResponse::plain_text(
                        StatusCode::INTERNAL_SERVER_ERROR,
                        "missing prepared response head",
                    ));
                };
                if let Some(status) = decisions.status() {
                    *response.status_mut() = status;
                }
                // A deferred head must not weaken a cache bypass already applied
                // by middleware before preparation. Vary and cookies accumulate.
                let cache_headers = [
                    "cache-control",
                    "cdn-cache-control",
                    "cloudflare-cdn-cache-control",
                    "vercel-cdn-cache-control",
                    "surrogate-control",
                ];
                // Targeted CDN headers can take precedence over Cache-Control.
                // A bypass in any family member also constrains newly added
                // policies, even when middleware did not set that exact name.
                let restricted_cache = cache_headers.iter().any(|name| {
                    response.headers().get_all(*name).iter().any(|value| {
                        value.to_str().map_or(true, |value| {
                            value.split(',').any(|directive| {
                                let name = directive.split('=').next().unwrap_or("").trim();
                                ["private", "no-store", "no-cache"]
                                    .iter()
                                    .any(|restricted| name.eq_ignore_ascii_case(restricted))
                            })
                        })
                    })
                });
                for name in decisions.headers().keys() {
                    if name != http::header::SET_COOKIE && name != http::header::VARY {
                        response.headers_mut().remove(name);
                    }
                }
                for (name, value) in decisions.headers() {
                    response.headers_mut().append(name.clone(), value.clone());
                }
                for name in cache_headers {
                    if restricted_cache && decisions.headers().contains_key(name) {
                        response
                            .headers_mut()
                            .insert(name, http::HeaderValue::from_static("private, no-store"));
                    }
                }
                // The final streamed representation has no known byte length.
                // Framing belongs to the HTTP transport, never a head provider.
                response.headers_mut().remove(http::header::CONTENT_LENGTH);
                response
                    .headers_mut()
                    .remove(http::header::TRANSFER_ENCODING);
                // These statuses cannot carry the generated document body.
                if response.status() == StatusCode::NO_CONTENT
                    || response.status() == StatusCode::NOT_MODIFIED
                    || response.status() == StatusCode::RESET_CONTENT
                {
                    drop(stream);
                    *response.body_mut() = Body::empty();
                    return response;
                }
                if response.status().is_informational() {
                    drop(stream);
                    return framework_response_to_axum(FrameworkResponse::plain_text(
                        StatusCode::INTERNAL_SERVER_ERROR,
                        "invalid document status",
                    ));
                }
            }
            *response.body_mut() = Body::from_stream(stream);
            response
        }
        Err(StreamPrepareError::TimedOut) => {
            error!("managed head preparation timed out before response headers");
            framework_response_to_axum(FrameworkResponse::plain_text(
                StatusCode::GATEWAY_TIMEOUT,
                "render timeout",
            ))
        }
        Err(StreamPrepareError::Overloaded) => {
            error!("local stream executor admission limit reached");
            framework_response_to_axum(FrameworkResponse::plain_text(
                StatusCode::SERVICE_UNAVAILABLE,
                "server busy",
            ))
        }
        Err(StreamPrepareError::Unavailable(reason)) => {
            error!(%reason, "local stream executor unavailable");
            framework_response_to_axum(FrameworkResponse::plain_text(
                StatusCode::INTERNAL_SERVER_ERROR,
                "render error",
            ))
        }
        Err(StreamPrepareError::FirstItem(err)) => {
            error!(?err, "framework stream failed before response headers");
            framework_response_to_axum(FrameworkResponse::plain_text(
                StatusCode::INTERNAL_SERVER_ERROR,
                "render error",
            ))
        }
    }
}

async fn prepare_stream(
    factory: FrameworkStreamFactory,
    executor: &LocalStreamExecutor,
    readiness: Option<proa_framework_core::managed_response::PreparedHead>,
) -> Result<FrameworkSendStream, StreamPrepareError> {
    match factory {
        FrameworkStreamFactory::Send(make) => {
            let mut tail = make();
            match proa_framework_core::managed_response::first_or_ready(
                &mut tail,
                readiness.as_ref(),
            )
            .await
            {
                None => Ok(tail),
                Some(Err(error)) => Err(StreamPrepareError::FirstItem(error)),
                Some(Ok(first)) => Ok(Box::pin(
                    stream::once(std::future::ready(Ok(first))).chain(tail),
                )),
            }
        }
        FrameworkStreamFactory::Local(make) => executor
            .prepare_with_head(make, readiness)
            .await
            .map_err(|error| match error {
                LocalStreamPrepareError::Overloaded => StreamPrepareError::Overloaded,
                LocalStreamPrepareError::Unavailable(reason) => {
                    StreamPrepareError::Unavailable(reason)
                }
                LocalStreamPrepareError::FirstItem(error) => StreamPrepareError::FirstItem(error),
            }),
    }
}

fn framework_request_context(ctx: &mut RouteContext) -> FrameworkRequestContext {
    let request_location = ctx.current_uri().cloned().map(RequestLocation::from);
    let route_params = Some(ctx.current_route_params().clone());
    let mut request = RequestContext::new().with_base_path(ctx.base_path().clone());
    request.set_public_asset_urls(ctx.public_asset_urls());
    request.set_framework_assets(ctx.framework_assets());
    request.set_asset_prefix(ctx.asset_prefix());
    if let Some(method) = ctx.current_method().cloned() {
        request.set_method(method);
    }
    request.set_headers(ctx.current_headers().clone());
    if let Some(location) = request_location.clone() {
        request.set_original_pathname(location.path());
        request.set_location(location);
    }
    if let Some(params) = route_params.clone() {
        request.set_route_params(params);
    }
    FrameworkRequestContext {
        head: ctx.head_config().cloned().map(Into::into),
        request,
        request_location,
        route_params,
        render_mode: RenderMode::Static,
        csp_nonce: ctx.csp_nonce().cloned(),
    }
}

pub(super) fn current_framework_request_context() -> FrameworkRequestContext {
    ROUTE_CONTEXT
        .try_with(|ctx| framework_request_context(&mut ctx.borrow_mut()))
        .unwrap_or_default()
}

impl<L: DataLoader + 'static> IntoResponse for RouteResponse<L> {
    fn into_response(self) -> Response {
        ROUTE_CONTEXT
            .try_with(|ctx| {
                let (is_client_request, page_title, request) = {
                    let mut ctx_ref = ctx.borrow_mut();
                    let is_client = ctx_ref.is_client_request();
                    let request = framework_request_context(&mut ctx_ref);
                    let page_title = if is_client {
                        ctx_ref.resolve_metadata().and_then(|meta| meta.title)
                    } else {
                        None
                    };
                    (is_client, page_title, request)
                };

                let mode = if is_client_request {
                    RouteFinalizeMode::ClientPayload { page_title }
                } else {
                    RouteFinalizeMode::Document
                };
                let response = self.0;
                let streamed_document =
                    matches!(&mode, RouteFinalizeMode::Document) && response.is_streaming();
                let finalized = if response.is_synchronous() {
                    finalize_route_response_sync(response, &request, mode)
                } else if streamed_document {
                    finalize_route_response_stream(response, &request)
                } else {
                    block_on_current_runtime(finalize_route_response::<L>(response, &request, mode))
                };

                match finalized {
                    Ok(response) => framework_response_to_axum(response),
                    Err(err) => {
                        error!(?err, "route finalization failed");
                        framework_response_to_axum(FrameworkResponse::plain_text(
                            StatusCode::INTERNAL_SERVER_ERROR,
                            "render error",
                        ))
                    }
                }
            })
            .unwrap_or_else(|_| {
                error!("RouteResponse used outside of routing context");
                (StatusCode::INTERNAL_SERVER_ERROR, "invalid route context").into_response()
            })
    }
}

pub(super) fn block_on_current_runtime<F>(fut: F) -> F::Output
where
    F: Future,
{
    let handle = tokio::runtime::Handle::try_current().expect(
        "RouteResponse async finalization was invoked outside a tokio runtime; \
         axum's tokio runtime is required to drive the future",
    );
    match handle.runtime_flavor() {
        tokio::runtime::RuntimeFlavor::MultiThread => {
            tokio::task::block_in_place(|| handle.block_on(fut))
        }
        _ => handle.block_on(fut),
    }
}

#[cfg(test)]
mod tests {
    use axum::body::to_bytes;
    use http_body_util::BodyExt as _;

    use super::*;
    use crate::route::LocalStreamExecutorConfig;

    fn unavailable_local_executor() -> LocalStreamExecutor {
        LocalStreamExecutor::new(LocalStreamExecutorConfig {
            worker_threads: 0,
            ..LocalStreamExecutorConfig::default()
        })
    }

    #[tokio::test]
    async fn send_stream_first_error_becomes_http_500() {
        let framework = FrameworkResponse::send_stream(|| -> FrameworkSendStream {
            Box::pin(stream::iter([Err(
                proa_framework_core::FrameworkError::Invariant("first item failed"),
            )]))
        });
        let deferred = framework_response_to_axum(framework);
        let response =
            prepare_deferred_framework_stream(deferred, unavailable_local_executor()).await;

        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        assert_eq!(
            to_bytes(response.into_body(), usize::MAX).await.unwrap(),
            "render error"
        );
    }

    #[tokio::test]
    async fn send_stream_chains_first_item_and_preserves_later_body_error() {
        let framework = FrameworkResponse::send_stream(|| -> FrameworkSendStream {
            Box::pin(stream::iter([
                Ok(bytes::Bytes::from_static(b"first")),
                Err(proa_framework_core::FrameworkError::Invariant(
                    "later item failed",
                )),
            ]))
        });
        let deferred = framework_response_to_axum(framework);
        let response =
            prepare_deferred_framework_stream(deferred, unavailable_local_executor()).await;

        assert_eq!(response.status(), StatusCode::OK);
        let mut body = response.into_body();
        let first = body
            .frame()
            .await
            .expect("first frame")
            .expect("first frame succeeds")
            .into_data()
            .expect("data frame");
        assert_eq!(first, "first");
        let error = body
            .frame()
            .await
            .expect("later error frame")
            .expect_err("later stream error must reach the body");
        assert!(error.to_string().contains("later item failed"));
    }

    #[tokio::test]
    async fn send_stream_clean_eof_keeps_status_and_empty_body() {
        let framework =
            FrameworkResponse::send_stream(|| -> FrameworkSendStream { Box::pin(stream::empty()) });
        let deferred = framework_response_to_axum(framework);
        let response =
            prepare_deferred_framework_stream(deferred, unavailable_local_executor()).await;

        assert_eq!(response.status(), StatusCode::OK);
        assert!(to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap()
            .is_empty());
    }
}

#[cfg(test)]
#[path = "managed_response_tests.rs"]
mod managed_response_tests;