phoenix-view 0.1.2

React page protocol and rendering modes for Phoenix
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
use aes_gcm::{
    Aes256Gcm, Nonce,
    aead::{Aead, AeadCore, KeyInit, OsRng, Payload},
};
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use futures_util::stream;
use phoenix_http::{
    Bytes, CspNonce, HeaderValue, IntoResponse, Request, Response, ResponseContext, RouteManifest,
    StatusCode, header,
};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::{
    collections::HashMap,
    time::{Duration, SystemTime, UNIX_EPOCH},
};
use thiserror::Error;

mod assets;
mod document;
mod renderer;
mod static_files;

pub use assets::{
    ASSET_MANIFEST_SCHEMA, AssetEntry, AssetManifest, AssetManifestError, RendererManifest,
};
pub use document::{
    DocumentContext, DocumentSlots, DocumentTemplate, DocumentTemplateError, TrustedHtml,
};
pub use renderer::{
    NodeRenderer, RenderContext, RenderFrame, RenderResult, RendererConfig, RendererError,
    RendererHealth, RendererStream,
};
pub use static_files::ServeProductionAssets;

const PAGE_MEDIA_TYPE: &str = "application/vnd.phoenix.page+json";
const PAGE_REQUEST_HEADER: &str = "x-phoenix-page";
const ENVELOPE_PURPOSE: &str = "page-navigation";

#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RenderMode {
    Spa,
    Ssr,
    #[default]
    Islands,
}

impl RenderMode {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Spa => "spa",
            Self::Ssr => "ssr",
            Self::Islands => "islands",
        }
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Island {
    pub id: String,
    pub component: String,
    pub props: Value,
}

impl Island {
    #[must_use]
    pub fn new(component: impl Into<String>, props: impl Serialize) -> Self {
        let component = component.into();
        Self {
            id: component.clone(),
            component,
            props: serde_json::to_value(props).unwrap_or(Value::Null),
        }
    }

    #[must_use]
    pub fn with_id(
        id: impl Into<String>,
        component: impl Into<String>,
        props: impl Serialize,
    ) -> Self {
        Self {
            id: id.into(),
            component: component.into(),
            props: serde_json::to_value(props).unwrap_or(Value::Null),
        }
    }
}

/// Controlled document metadata shared by full documents and page navigation.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct PageHead {
    pub title: Option<String>,
    pub description: Option<String>,
    pub canonical: Option<String>,
    pub robots: Option<String>,
    pub open_graph: Option<OpenGraph>,
}

impl PageHead {
    #[must_use]
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: Some(title.into()),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    #[must_use]
    pub fn canonical(mut self, canonical: impl Into<String>) -> Self {
        self.canonical = Some(canonical.into());
        self
    }

    #[must_use]
    pub fn robots(mut self, robots: impl Into<String>) -> Self {
        self.robots = Some(robots.into());
        self
    }

    #[must_use]
    pub fn open_graph(mut self, open_graph: OpenGraph) -> Self {
        self.open_graph = Some(open_graph);
        self
    }
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct OpenGraph {
    pub title: Option<String>,
    pub description: Option<String>,
    pub image: Option<String>,
    pub kind: Option<String>,
}

impl OpenGraph {
    #[must_use]
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: Some(title.into()),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    #[must_use]
    pub fn image(mut self, image: impl Into<String>) -> Self {
        self.image = Some(image.into());
        self
    }

    #[must_use]
    pub fn kind(mut self, kind: impl Into<String>) -> Self {
        self.kind = Some(kind.into());
        self
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct PageEnvelope {
    pub protocol: u8,
    pub render_mode: RenderMode,
    pub page: String,
    pub props: Value,
    pub shared: Map<String, Value>,
    pub errors: Map<String, Value>,
    pub flash: Map<String, Value>,
    pub contract_hash: Option<String>,
    pub asset_version: Option<String>,
    pub request_id: Option<String>,
    #[serde(default)]
    pub head: PageHead,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub csrf_token: Option<String>,
    pub routes: HashMap<String, String>,
    pub islands: Vec<Island>,
}

#[cfg(test)]
impl PageEnvelope {
    fn new_for_test(props: Value) -> Self {
        Self {
            protocol: 1,
            render_mode: RenderMode::Ssr,
            page: "test/page".to_owned(),
            props,
            shared: Map::new(),
            errors: Map::new(),
            flash: Map::new(),
            contract_hash: None,
            asset_version: None,
            request_id: None,
            head: PageHead::default(),
            csrf_token: None,
            routes: HashMap::new(),
            islands: Vec::new(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Page {
    envelope: PageEnvelope,
    server_html: Option<String>,
    script_src: String,
    stylesheets: Vec<String>,
    document_template: DocumentTemplate,
}

impl Page {
    #[must_use]
    pub fn new(name: impl Into<String>, props: impl Serialize) -> Self {
        Self {
            envelope: PageEnvelope {
                protocol: 1,
                render_mode: RenderMode::default(),
                page: name.into(),
                props: serde_json::to_value(props).unwrap_or(Value::Null),
                shared: Map::new(),
                errors: Map::new(),
                flash: Map::new(),
                contract_hash: None,
                asset_version: None,
                request_id: None,
                head: PageHead::default(),
                csrf_token: None,
                routes: HashMap::new(),
                islands: Vec::new(),
            },
            server_html: None,
            script_src: default_script_src(),
            stylesheets: Vec::new(),
            document_template: DocumentTemplate::default(),
        }
    }

    #[must_use]
    pub fn mode(mut self, mode: RenderMode) -> Self {
        self.envelope.render_mode = mode;
        self
    }

    #[must_use]
    pub fn spa(self) -> Self {
        self.mode(RenderMode::Spa)
    }

    #[must_use]
    pub fn ssr(self) -> Self {
        self.mode(RenderMode::Ssr)
    }

    #[must_use]
    pub fn islands(self) -> Self {
        self.mode(RenderMode::Islands)
    }

    #[must_use]
    pub fn shared(mut self, shared: impl Serialize) -> Self {
        self.envelope.shared = object_value(shared);
        self
    }

    #[must_use]
    pub fn errors(mut self, errors: impl Serialize) -> Self {
        self.envelope.errors = object_value(errors);
        self
    }

    #[must_use]
    pub fn flash(mut self, flash: impl Serialize) -> Self {
        self.envelope.flash = object_value(flash);
        self
    }

    #[must_use]
    pub fn contract_hash(mut self, hash: impl Into<String>) -> Self {
        self.envelope.contract_hash = Some(hash.into());
        self
    }

    #[must_use]
    pub fn asset_version(mut self, version: impl Into<String>) -> Self {
        self.envelope.asset_version = Some(version.into());
        self
    }

    #[must_use]
    pub fn request_id(mut self, request_id: impl Into<String>) -> Self {
        self.envelope.request_id = Some(request_id.into());
        self
    }

    #[must_use]
    pub fn head(mut self, head: PageHead) -> Self {
        self.envelope.head = head;
        self
    }

    #[must_use]
    pub fn csrf_token(mut self, csrf_token: impl Into<String>) -> Self {
        self.envelope.csrf_token = Some(csrf_token.into());
        self
    }

    #[must_use]
    pub fn island(mut self, component: impl Into<String>, props: impl Serialize) -> Self {
        self.envelope.islands.push(Island::new(component, props));
        self
    }

    #[must_use]
    pub fn island_with_id(
        mut self,
        id: impl Into<String>,
        component: impl Into<String>,
        props: impl Serialize,
    ) -> Self {
        self.envelope
            .islands
            .push(Island::with_id(id, component, props));
        self
    }

    /// Attach HTML produced by a trusted React server renderer.
    #[must_use]
    pub fn trusted_server_html(mut self, html: impl Into<String>) -> Self {
        self.server_html = Some(html.into());
        self
    }

    /// Customize the trusted HTML chrome around Phoenix's managed React root.
    #[must_use]
    pub fn document(mut self, template: DocumentTemplate) -> Self {
        self.document_template = template;
        self
    }

    /// Apply HTML and island descriptors returned by the trusted SSR renderer.
    #[must_use]
    pub fn rendered(mut self, result: RenderResult) -> Self {
        self.server_html = Some(result.html);
        self.envelope.islands = result.islands;
        self
    }

    /// Apply one entry from a validated production asset manifest.
    ///
    /// # Errors
    ///
    /// Returns an error when the entry is missing or references an undeclared
    /// asset.
    pub fn production_assets(
        mut self,
        manifest: &AssetManifest,
        entry_name: &str,
    ) -> Result<Self, AssetManifestError> {
        manifest.validate()?;
        let entry = manifest
            .entry(entry_name)
            .ok_or_else(|| AssetManifestError::UnknownAsset(entry_name.to_owned()))?;
        self.script_src = manifest.url(&entry.file)?;
        self.stylesheets = entry
            .css
            .iter()
            .map(|asset| manifest.url(asset))
            .collect::<Result<Vec<_>, _>>()?;
        self.envelope.contract_hash = Some(manifest.contract_hash.clone());
        self.envelope.asset_version = Some(manifest.version.clone());
        Ok(self)
    }

    /// Render an SSR or Islands page and select the document or page-protocol response.
    pub async fn respond_with_renderer(
        self,
        request: &Request,
        renderer: &NodeRenderer,
    ) -> Response {
        if Self::is_page_request(request.headers())
            && self.envelope.render_mode != RenderMode::Islands
        {
            return self
                .respond_to(request, None)
                .unwrap_or_else(PageResponseError::into_response);
        }
        let context = render_context(request);
        match renderer.render(self.envelope(), &context).await {
            Ok(result) => self
                .rendered(result)
                .respond_to(request, None)
                .into_response(),
            Err(error) => {
                eprintln!("SSR renderer failed: {error}");
                Response::text("SSR renderer unavailable")
                    .with_status(StatusCode::SERVICE_UNAVAILABLE)
            }
        }
    }

    /// Stream an SSR/Islands document as renderer chunks arrive. Page-protocol
    /// navigation requests still return the normal atomic JSON envelope.
    pub fn respond_streaming_with_renderer(
        mut self,
        request: &Request,
        renderer: &NodeRenderer,
    ) -> Response {
        if let Some(manifest) = request.extensions().get::<RouteManifest>() {
            self.envelope.routes.clone_from(manifest.routes());
        }
        if Self::is_page_request(request.headers()) {
            return self
                .respond(true, None)
                .unwrap_or_else(PageResponseError::into_response);
        }

        let nonce = request.extensions().get::<CspNonce>().cloned();
        let has_nonce = nonce.is_some();
        let document_slots = match self
            .document_template
            .render(DocumentContext::new(&self.envelope, nonce.as_ref()))
        {
            Ok(slots) => slots,
            Err(error) => return PageResponseError::Document(error).into_response(),
        };
        let context = render_context(request);
        let Ok(mut frame_stream) = renderer.render_stream(&self.envelope, &context) else {
            return Response::text("SSR renderer unavailable")
                .with_status(StatusCode::SERVICE_UNAVAILABLE);
        };
        let prefix = document::document_prefix(
            &self.envelope,
            &self.stylesheets,
            nonce.as_ref(),
            &document_slots,
        );
        let render_mode = self.envelope.render_mode;
        let script_src = self.script_src;
        let mut envelope = self.envelope;
        let (sender, receiver) = tokio::sync::mpsc::channel(16);
        tokio::spawn(async move {
            if sender.send(Bytes::from(prefix)).await.is_err() {
                return;
            }
            let mut completed = false;
            while let Some(frame) = frame_stream.recv().await {
                match frame {
                    Ok(RenderFrame::Chunk(chunk)) => {
                        if sender.send(Bytes::from(chunk)).await.is_err() {
                            return;
                        }
                    }
                    Ok(RenderFrame::Complete { islands, .. }) => {
                        envelope.islands = islands;
                        completed = true;
                        break;
                    }
                    Err(_) => break,
                }
            }
            if !completed {
                let _ = sender
                    .send(Bytes::from_static(
                        b"<!-- Phoenix SSR stream interrupted -->",
                    ))
                    .await;
            } else if let Ok(suffix) =
                document::document_suffix(&envelope, &script_src, nonce.as_ref(), &document_slots)
            {
                let _ = sender.send(Bytes::from(suffix)).await;
            }
        });
        let body = stream::unfold(receiver, |mut receiver| async move {
            receiver.recv().await.map(|chunk| (chunk, receiver))
        });
        let mut response = Response::stream(body);
        response.headers_mut().insert(
            header::CONTENT_TYPE,
            HeaderValue::from_static("text/html; charset=utf-8"),
        );
        response.headers_mut().insert(
            "x-phoenix-render-mode",
            HeaderValue::from_static(render_mode.as_str()),
        );
        response
            .headers_mut()
            .insert("x-phoenix-ssr-stream", HeaderValue::from_static("1"));
        if has_nonce {
            response.headers_mut().insert(
                header::CACHE_CONTROL,
                HeaderValue::from_static("private, no-store"),
            );
            response.headers_mut().remove(header::ETAG);
            response.headers_mut().remove(header::LAST_MODIFIED);
        }
        response
    }

    /// Override the browser entrypoint, for example when using a Vite dev server.
    #[must_use]
    pub fn script_src(mut self, source: impl Into<String>) -> Self {
        self.script_src = source.into();
        self
    }

    /// Select a document or protocol response from the incoming request.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization or the selected codec fails.
    pub fn respond_to(
        self,
        request: &Request,
        codec: Option<&dyn PayloadCodec>,
    ) -> Result<Response, PageResponseError> {
        self.respond_with_context(&ResponseContext::from_request(request), codec)
    }

    #[must_use]
    pub const fn envelope(&self) -> &PageEnvelope {
        &self.envelope
    }

    /// Return either a document response or a page-protocol response.
    ///
    /// Set `X-Phoenix-Page: 1` on client navigation requests. A codec encrypts
    /// only those protocol responses; initial HTML always contains readable
    /// hydration data because the browser must render it.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization or the selected codec fails.
    pub fn respond(
        self,
        page_request: bool,
        codec: Option<&dyn PayloadCodec>,
    ) -> Result<Response, PageResponseError> {
        if page_request {
            return protocol_response(&self.envelope, codec);
        }

        document_response(
            &self.envelope,
            self.server_html.as_deref(),
            &self.script_src,
            &self.stylesheets,
            None,
            &self.document_template,
        )
    }

    #[must_use]
    pub fn is_page_request(headers: &phoenix_http::HeaderMap) -> bool {
        headers
            .get(PAGE_REQUEST_HEADER)
            .is_some_and(|value| value == "1")
    }

    fn respond_with_context(
        mut self,
        context: &ResponseContext,
        codec: Option<&dyn PayloadCodec>,
    ) -> Result<Response, PageResponseError> {
        if let Some(manifest) = context.route_manifest() {
            self.envelope.routes.clone_from(manifest.routes());
        }
        if context.is_page_request() {
            return protocol_response(&self.envelope, codec);
        }
        document_response(
            &self.envelope,
            self.server_html.as_deref(),
            &self.script_src,
            &self.stylesheets,
            context.csp_nonce(),
            &self.document_template,
        )
    }
}

fn default_script_src() -> String {
    if cfg!(debug_assertions) {
        let vite_url =
            std::env::var("VITE_DEV_URL").unwrap_or_else(|_| "http://127.0.0.1:5173".to_owned());
        return format!(
            "{}/@id/__x00__virtual:phoenix/client",
            vite_url.trim_end_matches('/')
        );
    }
    "/assets/phoenix.js".to_owned()
}

impl IntoResponse for Page {
    fn into_response(self) -> Response {
        self.respond(false, None)
            .unwrap_or_else(PageResponseError::into_response)
    }

    fn into_response_with_context(self, context: &ResponseContext) -> Response {
        self.respond_with_context(context, None)
            .unwrap_or_else(PageResponseError::into_response)
    }
}

fn render_context(request: &Request) -> RenderContext {
    let mut context = RenderContext::new(request.uri().to_string());
    if let Some(nonce) = request.extensions().get::<CspNonce>().cloned() {
        context = context.csp_nonce(nonce);
    }
    context
}

pub trait PayloadCodec: Send + Sync {
    /// Encode serialized page JSON for a trusted client or intermediary.
    ///
    /// # Errors
    ///
    /// Returns an error if the payload cannot be encoded.
    fn encode(&self, plaintext: &[u8]) -> Result<EncryptedPayload, EncryptionError>;
}

#[derive(Clone)]
pub struct Aes256GcmCodec {
    key_id: String,
    cipher: Aes256Gcm,
    ttl: Duration,
}

impl Aes256GcmCodec {
    #[must_use]
    pub fn new(key_id: impl Into<String>, key: [u8; 32]) -> Self {
        Self {
            key_id: key_id.into(),
            cipher: Aes256Gcm::new(&key.into()),
            ttl: Duration::from_mins(1),
        }
    }

    #[must_use]
    pub const fn with_ttl(mut self, ttl: Duration) -> Self {
        self.ttl = ttl;
        self
    }

    /// Decode a payload produced by this codec.
    ///
    /// # Errors
    ///
    /// Returns an error for the wrong key id, malformed base64, invalid nonce,
    /// or failed authentication.
    pub fn decode(&self, payload: &EncryptedPayload) -> Result<Vec<u8>, EncryptionError> {
        if payload.version != 1
            || payload.algorithm != "A256GCM"
            || payload.key_id != self.key_id
            || payload.purpose != ENVELOPE_PURPOSE
        {
            return Err(EncryptionError::InvalidEnvelope);
        }
        let now = unix_timestamp()?;
        if payload.expires_at < now || payload.issued_at > now.saturating_add(60) {
            return Err(EncryptionError::Expired);
        }
        let nonce = URL_SAFE_NO_PAD
            .decode(&payload.nonce)
            .map_err(|_| EncryptionError::InvalidEnvelope)?;
        let mut ciphertext = URL_SAFE_NO_PAD
            .decode(&payload.ciphertext)
            .map_err(|_| EncryptionError::InvalidEnvelope)?;
        let tag = URL_SAFE_NO_PAD
            .decode(&payload.tag)
            .map_err(|_| EncryptionError::InvalidEnvelope)?;
        if nonce.len() != 12 {
            return Err(EncryptionError::InvalidEnvelope);
        }
        ciphertext.extend(tag);
        let aad = envelope_aad(payload);

        self.cipher
            .decrypt(
                Nonce::from_slice(&nonce),
                Payload {
                    msg: &ciphertext,
                    aad: aad.as_bytes(),
                },
            )
            .map_err(|_| EncryptionError::AuthenticationFailed)
    }
}

impl PayloadCodec for Aes256GcmCodec {
    fn encode(&self, plaintext: &[u8]) -> Result<EncryptedPayload, EncryptionError> {
        let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
        let issued_at = unix_timestamp()?;
        let expires_at = issued_at.saturating_add(self.ttl.as_secs());
        let mut envelope = EncryptedPayload {
            version: 1,
            algorithm: "A256GCM".to_owned(),
            key_id: self.key_id.clone(),
            purpose: ENVELOPE_PURPOSE.to_owned(),
            issued_at,
            expires_at,
            nonce: URL_SAFE_NO_PAD.encode(nonce),
            ciphertext: String::new(),
            tag: String::new(),
        };
        let aad = envelope_aad(&envelope);
        let mut sealed = self
            .cipher
            .encrypt(
                &nonce,
                Payload {
                    msg: plaintext,
                    aad: aad.as_bytes(),
                },
            )
            .map_err(|_| EncryptionError::EncryptionFailed)?;

        if sealed.len() < 16 {
            return Err(EncryptionError::EncryptionFailed);
        }
        let tag = sealed.split_off(sealed.len() - 16);
        envelope.ciphertext = URL_SAFE_NO_PAD.encode(sealed);
        envelope.tag = URL_SAFE_NO_PAD.encode(tag);
        Ok(envelope)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct EncryptedPayload {
    pub version: u8,
    pub algorithm: String,
    pub key_id: String,
    pub purpose: String,
    pub issued_at: u64,
    pub expires_at: u64,
    pub nonce: String,
    pub ciphertext: String,
    pub tag: String,
}

#[derive(Debug, Error)]
pub enum EncryptionError {
    #[error("page payload encryption failed")]
    EncryptionFailed,
    #[error("the encrypted page envelope is invalid")]
    InvalidEnvelope,
    #[error("page payload authentication failed")]
    AuthenticationFailed,
    #[error("the encrypted page envelope has expired")]
    Expired,
    #[error("the system clock is before the Unix epoch")]
    InvalidClock,
}

#[derive(Debug, Error)]
pub enum PageResponseError {
    #[error("page serialization failed")]
    Serialization(#[from] serde_json::Error),
    #[error(transparent)]
    Encryption(#[from] EncryptionError),
    #[error(transparent)]
    Document(#[from] DocumentTemplateError),
}

impl IntoResponse for PageResponseError {
    fn into_response(self) -> Response {
        Response::text("Internal Server Error").with_status(StatusCode::INTERNAL_SERVER_ERROR)
    }
}

fn object_value(value: impl Serialize) -> Map<String, Value> {
    serde_json::to_value(value)
        .ok()
        .and_then(|value| value.as_object().cloned())
        .unwrap_or_default()
}

fn unix_timestamp() -> Result<u64, EncryptionError> {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_secs())
        .map_err(|_| EncryptionError::InvalidClock)
}

fn envelope_aad(payload: &EncryptedPayload) -> String {
    format!(
        "phoenix.page.v{}|{}|{}|{}|{}",
        payload.version, payload.key_id, payload.purpose, payload.issued_at, payload.expires_at
    )
}

fn protocol_response(
    envelope: &PageEnvelope,
    codec: Option<&dyn PayloadCodec>,
) -> Result<Response, PageResponseError> {
    let plain = serde_json::to_vec(envelope)?;
    let (body, encrypted) = match codec {
        Some(codec) => (serde_json::to_vec(&codec.encode(&plain)?)?, true),
        None => (plain, false),
    };
    let mut response = Response::new(StatusCode::OK, body);
    response.headers_mut().insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static(PAGE_MEDIA_TYPE),
    );
    response.headers_mut().insert(
        "x-phoenix-encrypted",
        HeaderValue::from_static(if encrypted { "1" } else { "0" }),
    );
    Ok(response)
}

fn document_response(
    envelope: &PageEnvelope,
    server_html: Option<&str>,
    script_src: &str,
    stylesheets: &[String],
    nonce: Option<&CspNonce>,
    template: &DocumentTemplate,
) -> Result<Response, PageResponseError> {
    let root_html = if envelope.render_mode == RenderMode::Spa {
        ""
    } else {
        server_html.unwrap_or_default()
    };
    let slots = template.render(DocumentContext::new(envelope, nonce))?;
    let html = format!(
        "{}{root_html}{}",
        document::document_prefix(envelope, stylesheets, nonce, &slots),
        document::document_suffix(envelope, script_src, nonce, &slots)?
    );
    let mut response = Response::new(StatusCode::OK, html);
    response.headers_mut().insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static("text/html; charset=utf-8"),
    );
    response.headers_mut().insert(
        "x-phoenix-render-mode",
        HeaderValue::from_static(envelope.render_mode.as_str()),
    );
    if nonce.is_some() {
        response.headers_mut().insert(
            header::CACHE_CONTROL,
            HeaderValue::from_static("private, no-store"),
        );
        response.headers_mut().remove(header::ETAG);
        response.headers_mut().remove(header::LAST_MODIFIED);
    }
    Ok(response)
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures_util::StreamExt;
    use phoenix_http::{Method, ResponseBody};
    use phoenix_routing::Routes;
    use phoenix_security::NonceSecurityPolicy;
    use serde_json::json;
    use std::ffi::OsString;

    #[test]
    fn islands_is_the_default_and_html_payload_is_context_safe() {
        let response =
            Page::new("articles/show", json!({ "body": "</script><b>" })).into_response();
        let body = String::from_utf8_lossy(response.body());

        assert_eq!(
            response.headers().get("x-phoenix-render-mode").unwrap(),
            "islands"
        );
        assert!(body.contains("data-render-mode=\"islands\""));
        assert!(!body.contains("</script><b>"));
        assert!(body.contains("\\u003c/script\\u003e\\u003cb\\u003e"));
    }

    #[test]
    fn page_head_and_csrf_are_escaped_and_shared_by_the_protocol() {
        let head = PageHead::new("Apps < trusted")
            .description("Install \"safely\" & privately")
            .canonical("https://example.test/apps?kind=ios&view=all")
            .robots("index,follow")
            .open_graph(
                OpenGraph::new("Apps < trusted")
                    .description("No <script> here")
                    .image("https://example.test/cover.png?size=large&safe=1")
                    .kind("website"),
            );
        let page = Page::new("apps/index", json!({}))
            .head(head.clone())
            .csrf_token("csrf-token");
        let response = page.clone().into_response();
        let body = String::from_utf8_lossy(response.body());

        assert!(body.contains("<title data-phoenix-head>Apps &lt; trusted</title>"));
        assert!(
            body.contains(
                "content=\"Install &quot;safely&quot; &amp; privately\" data-phoenix-head"
            )
        );
        assert!(
            body.contains("property=\"og:title\" content=\"Apps &lt; trusted\" data-phoenix-head")
        );
        assert!(!body.contains("<script> here"));

        let protocol = page.respond(true, None).expect("protocol response");
        let envelope: PageEnvelope = serde_json::from_slice(protocol.body()).expect("envelope");
        assert_eq!(envelope.head, head);
        assert_eq!(envelope.csrf_token.as_deref(), Some("csrf-token"));
    }

    #[test]
    fn application_document_template_wraps_the_managed_react_protocol() {
        let template = DocumentTemplate::from_fn(|context| {
            DocumentSlots::new()
                .language("zh-CN")
                .body_attribute("class", "application-shell")
                .unwrap()
                .head(TrustedHtml::new(format!(
                    "<meta name=\"page-name\" content=\"{}\">",
                    context.envelope().page
                )))
                .before_root(TrustedHtml::new("<nav>Navigation</nav>"))
                .after_root(TrustedHtml::new("<footer>Footer</footer>"))
        });
        let response = Page::new("dashboard/show", json!({ "ready": true }))
            .trusted_server_html("<main>Dashboard</main>")
            .document(template)
            .into_response();
        let html = String::from_utf8_lossy(response.body());

        assert!(html.contains("<html lang=\"zh-CN\">"));
        assert!(html.contains("<body class=\"application-shell\"><nav>Navigation</nav>"));
        assert!(html.contains("<main>Dashboard</main></div><footer>Footer</footer>"));
        assert!(html.contains("<meta name=\"page-name\" content=\"dashboard/show\">"));
        assert!(html.contains("id=\"phoenix-page\""));
        assert!(html.contains("type=\"module\""));
    }

    #[test]
    fn document_template_errors_return_a_generic_server_error() {
        let template = DocumentTemplate::try_from_fn(|_| {
            Err(DocumentTemplateError::render("private layout failure"))
        });
        let response = Page::new("dashboard/show", json!({}))
            .document(template)
            .into_response();

        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        assert_eq!(response.body(), "Internal Server Error");
        assert!(!String::from_utf8_lossy(response.body()).contains("private layout failure"));
    }

    #[test]
    fn modes_share_the_same_page_protocol() {
        for mode in [RenderMode::Spa, RenderMode::Ssr, RenderMode::Islands] {
            let response = Page::new("articles/show", json!({ "id": 7 }))
                .mode(mode)
                .respond(true, None)
                .unwrap();
            let envelope: PageEnvelope = serde_json::from_slice(response.body()).unwrap();

            assert_eq!(envelope.render_mode, mode);
            assert_eq!(envelope.page, "articles/show");
            assert_eq!(envelope.props["id"], 7);
        }
    }

    #[tokio::test]
    async fn page_navigation_bypasses_the_ssr_renderer() {
        let renderer = NodeRenderer::new(RendererConfig::command(
            "/definitely/missing/phoenix-renderer",
            std::iter::empty::<OsString>(),
        ));
        let mut request = Request::new(Method::GET, "/articles/7".parse().unwrap());
        request
            .headers_mut()
            .insert(PAGE_REQUEST_HEADER, HeaderValue::from_static("1"));
        request
            .extensions_mut()
            .insert(CspNonce::new("dddddddddddddddddddddddddddddddd").unwrap());

        let response = Page::new("articles/show", json!({ "id": 7 }))
            .ssr()
            .respond_with_renderer(&request, &renderer)
            .await;

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(response.headers()[header::CONTENT_TYPE], PAGE_MEDIA_TYPE);
        assert!(!response.headers().contains_key(header::CACHE_CONTROL));
        let envelope: PageEnvelope = serde_json::from_slice(response.body()).unwrap();
        assert_eq!(envelope.props["id"], 7);
    }

    #[test]
    fn spa_document_keeps_the_client_root_empty() {
        let response = Page::new("dashboard/show", json!({ "ready": true }))
            .spa()
            .trusted_server_html("<h1>server-only</h1>")
            .into_response();

        assert!(!String::from_utf8_lossy(response.body()).contains("server-only"));
    }

    #[test]
    fn custom_script_source_is_attribute_encoded() {
        let response = Page::new("dashboard/show", json!({}))
            .script_src("http://localhost/app.js?mode=dev&name=\"test\"")
            .into_response();
        let html = String::from_utf8_lossy(response.body());

        assert!(
            html.contains("src=\"http://localhost/app.js?mode=dev&amp;name=&quot;test&quot;\"")
        );
    }

    #[test]
    fn production_manifest_selects_hashed_scripts_styles_and_identity() {
        let manifest = AssetManifest {
            schema: ASSET_MANIFEST_SCHEMA,
            version: "sha256-build".to_owned(),
            contract_hash: "fnv1a-contract".to_owned(),
            public_path: "/assets/".to_owned(),
            entries: HashMap::from([(
                "client".to_owned(),
                AssetEntry {
                    file: "phoenix-a1.js".to_owned(),
                    css: vec!["client-b2.css".to_owned()],
                    imports: Vec::new(),
                },
            )]),
        };
        let response = Page::new("dashboard/show", json!({}))
            .production_assets(&manifest, "client")
            .unwrap()
            .into_response();
        let html = String::from_utf8_lossy(response.body());

        assert!(html.contains("src=\"/assets/phoenix-a1.js\""));
        assert!(html.contains("href=\"/assets/client-b2.css\""));
        assert!(html.contains("\"contract_hash\":\"fnv1a-contract\""));
        assert!(html.contains("\"asset_version\":\"sha256-build\""));
    }

    #[tokio::test]
    async fn nonce_policy_automatically_reaches_page_html_but_not_page_json() {
        let manifest = AssetManifest {
            schema: ASSET_MANIFEST_SCHEMA,
            version: "sha256-build".to_owned(),
            contract_hash: "fnv1a-contract".to_owned(),
            public_path: "/assets/".to_owned(),
            entries: HashMap::from([(
                "client".to_owned(),
                AssetEntry {
                    file: "phoenix-a1.js".to_owned(),
                    css: vec!["client-b2.css".to_owned()],
                    imports: Vec::new(),
                },
            )]),
        };
        let router = Routes::new()
            .get("/", move |_request: Request| {
                let manifest = manifest.clone();
                async move {
                    Page::new("dashboard/show", json!({ "ready": true }))
                        .production_assets(&manifest, "client")
                        .unwrap()
                }
            })
            .name("dashboard.show")
            .with_middleware(NonceSecurityPolicy::default())
            .build()
            .unwrap();

        let response = router
            .handle(Request::new(Method::GET, "/".parse().unwrap()))
            .await;
        let policy = response.headers()["content-security-policy"]
            .to_str()
            .unwrap();
        let nonce_source = policy
            .split_once("'nonce-")
            .map(|(_, source)| source)
            .expect("nonce source");
        let nonce = nonce_source
            .split_once('\'')
            .map(|(nonce, _)| nonce)
            .expect("nonce terminator");
        let html = String::from_utf8_lossy(response.body());

        assert!(html.contains(&format!("<meta property=\"csp-nonce\" nonce=\"{nonce}\">")));
        assert!(html.contains(&format!(
            "<link rel=\"stylesheet\" href=\"/assets/client-b2.css\" nonce=\"{nonce}\">"
        )));
        assert!(html.contains(&format!(
            "<script id=\"phoenix-page\" type=\"application/json\" nonce=\"{nonce}\">"
        )));
        assert!(html.contains(&format!(
            "<script type=\"module\" src=\"/assets/phoenix-a1.js\" nonce=\"{nonce}\">"
        )));
        assert!(!html.contains("\"csp_nonce\""));
        assert!(html.contains("\"dashboard.show\":\"/\""));
        assert_eq!(
            response.headers()[header::CACHE_CONTROL],
            "private, no-store"
        );

        let mut navigation = Request::new(Method::GET, "/".parse().unwrap());
        navigation
            .headers_mut()
            .insert(PAGE_REQUEST_HEADER, HeaderValue::from_static("1"));
        let response = router.handle(navigation).await;
        assert_eq!(response.headers()[header::CONTENT_TYPE], PAGE_MEDIA_TYPE);
        assert!(!response.headers().contains_key(header::CACHE_CONTROL));
        assert!(!String::from_utf8_lossy(response.body()).contains("csp_nonce"));
    }

    #[tokio::test]
    async fn page_response_forwards_renderer_chunks_before_hydration_payload() {
        let source = r#"
          const readline = require('node:readline');
          readline.createInterface({input: process.stdin}).on('line', line => {
            const request = JSON.parse(line);
            if (request.kind === 'hello') {
              console.log(JSON.stringify({protocol: 2, id: request.id, ok: true})); return;
            }
            console.log(JSON.stringify({protocol: 2, id: request.id, ok: true, kind: 'chunk', chunk: `<h1 data-nonce="${request.csp_nonce}">`}));
            console.log(JSON.stringify({protocol: 2, id: request.id, ok: true, kind: 'chunk', chunk: 'Streamed</h1>'}));
            console.log(JSON.stringify({protocol: 2, id: request.id, ok: true, kind: 'complete',
              islands: [{id: 'counter', component: 'counter', props: {value: 1}}]}));
          });
        "#;
        let node = std::env::var_os("PATH")
            .and_then(|path| {
                std::env::split_paths(&path)
                    .map(|directory| directory.join("node"))
                    .find(|candidate| candidate.is_file())
            })
            .expect("Node.js is required for streaming tests");
        let renderer = NodeRenderer::new(RendererConfig::command(
            node,
            [OsString::from("--eval"), OsString::from(source)],
        ));
        let nonce = CspNonce::new("cccccccccccccccccccccccccccccccc").unwrap();
        let mut request = Request::new(Method::GET, "/stream".parse().unwrap());
        request.extensions_mut().insert(nonce.clone());
        let response = Page::new("test/page", json!({ "ready": true }))
            .ssr()
            .respond_streaming_with_renderer(&request, &renderer);
        assert!(response.is_streaming());
        assert_eq!(response.headers()["x-phoenix-ssr-stream"], "1");
        assert_eq!(
            response.headers()[header::CACHE_CONTROL],
            "private, no-store"
        );
        let (_, _, body) = response.into_parts();
        let ResponseBody::Stream(stream) = body else {
            panic!("expected streaming body");
        };
        let chunks = stream.collect::<Vec<_>>().await;
        let html = chunks
            .into_iter()
            .map(Result::unwrap)
            .fold(Vec::new(), |mut output, chunk| {
                output.extend_from_slice(&chunk);
                output
            });
        let html = String::from_utf8(html).unwrap();

        assert!(html.contains("Streamed</h1>"));
        assert!(html.contains(&format!("data-nonce=\"{}\"", nonce.as_str())));
        assert!(html.contains(&format!(
            "<meta property=\"csp-nonce\" nonce=\"{}\">",
            nonce.as_str()
        )));
        assert!(html.contains(&format!(
            "id=\"phoenix-page\" type=\"application/json\" nonce=\"{}\"",
            nonce.as_str()
        )));
        assert!(html.contains(&format!(
            "type=\"module\" src=\"{}\" nonce=\"{}\"",
            default_script_src(),
            nonce.as_str()
        )));
        assert!(html.contains("\"component\":\"counter\""));
        assert!(html.find("<h1").unwrap() < html.find("phoenix-page").unwrap());
    }

    #[tokio::test]
    async fn renderer_startup_failure_never_emits_executable_stream_suffix() {
        let renderer = NodeRenderer::new(RendererConfig::command(
            "/definitely/missing/phoenix-renderer",
            std::iter::empty::<OsString>(),
        ));
        let nonce = CspNonce::new("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee").unwrap();
        let mut request = Request::new(Method::GET, "/stream".parse().unwrap());
        request.extensions_mut().insert(nonce.clone());
        let response = Page::new("test/page", json!({ "ready": true }))
            .ssr()
            .respond_streaming_with_renderer(&request, &renderer);
        let (_, _, body) = response.into_parts();
        let ResponseBody::Stream(stream) = body else {
            panic!("expected streaming body");
        };
        let html = stream
            .collect::<Vec<_>>()
            .await
            .into_iter()
            .map(Result::unwrap)
            .fold(Vec::new(), |mut output, chunk| {
                output.extend_from_slice(&chunk);
                output
            });
        let html = String::from_utf8(html).unwrap();

        assert!(html.contains("Phoenix SSR stream interrupted"));
        assert!(html.contains(&format!(
            "<meta property=\"csp-nonce\" nonce=\"{}\">",
            nonce.as_str()
        )));
        assert!(!html.contains("id=\"phoenix-page\""));
        assert!(!html.contains("<script"));
    }

    #[test]
    fn encrypted_protocol_payload_round_trips_and_rejects_wrong_keys() {
        let codec = Aes256GcmCodec::new("primary", [7; 32]);
        let response = Page::new("account/show", json!({ "balance": 42 }))
            .respond(true, Some(&codec))
            .unwrap();
        let encrypted: EncryptedPayload = serde_json::from_slice(response.body()).unwrap();
        let decoded = codec.decode(&encrypted).unwrap();
        let envelope: PageEnvelope = serde_json::from_slice(&decoded).unwrap();

        assert_eq!(response.headers().get("x-phoenix-encrypted").unwrap(), "1");
        assert_eq!(envelope.props["balance"], 42);
        assert!(
            Aes256GcmCodec::new("primary", [8; 32])
                .decode(&encrypted)
                .is_err()
        );
    }
}