vorma 0.86.0-pre.3

Vorma framework.
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
use std::collections::BTreeMap;
#[cfg(test)]
use std::sync::Arc;

use bytes::Bytes;
use http::header::{CACHE_CONTROL, CONTENT_TYPE, HeaderValue};
use http::{Response, StatusCode, Uri};
use serde::Serialize;
#[cfg(test)]
use serde_json::Value;
use url::{Url, form_urlencoded};
#[cfg(test)]
use vorma_tasks::ExecCtx;

use crate::constants::{
	CSS_BUNDLE_ATTR, QUERY_KEY_VORMA_JSON, VORMA_DATA_JSON_SCRIPT_EL_ID, VORMA_ROOT_EL_ID,
	X_VORMA_BUILD_SKEW,
};
use crate::document::{Document, DocumentRenderInput};
use crate::envutil::is_dev;
use crate::error::ViewErrorClientMsg;
use crate::htmlutil::{
	Element, render_element, render_element_to_string, render_module_script_to_string,
};
use crate::manifest::Manifest;
#[cfg(test)]
use crate::mux::NestedRouter;
use crate::mux::{RawRequest, RouteExecutionError, TaskRouteResult, ViewStackExecution};
use crate::response::{
	ResponsePlan, ResponseStatusPolicy, finalize_response_plan, insert_header,
	internal_server_error_response, json_response, plain_text_response,
	response_with_client_build_id,
};
use crate::view_payload::{SsrPayload, ViewPayload, build_view_payload};

/////////////////////////////////////////////////////////////////////
/////// View Handler
/////////////////////////////////////////////////////////////////////

pub(crate) fn is_json_request(uri: &Uri) -> bool {
	submitted_client_build_id(uri).is_some()
}

pub(crate) fn build_view_skew_response(
	request_uri: &Uri,
	expected_client_build_id: &str,
) -> Result<Option<Response<Bytes>>, String> {
	if !is_json_request(request_uri) {
		return Ok(None);
	}

	let submitted_client_build_id = submitted_client_build_id(request_uri);
	if submitted_client_build_id.as_deref() == Some(expected_client_build_id)
		|| submitted_client_build_id.as_deref() == Some("_")
	{
		return Ok(None);
	}

	let mut response = response_with_client_build_id(
		json_response(StatusCode::OK, Bytes::from_static(br#"{"ok":true}"#)),
		expected_client_build_id,
	)?;
	insert_header(response.headers_mut(), X_VORMA_BUILD_SKEW, "1")?;
	response
		.headers_mut()
		.insert(CACHE_CONTROL, HeaderValue::from_static("no-store"));
	Ok(Some(response))
}

#[cfg(test)]
pub(crate) struct ViewResponseInput<'a, S, E> {
	pub(crate) expected_client_build_id: &'a str,
	pub(crate) request: RawRequest,
	pub(crate) state: Arc<S>,
	pub(crate) exec_ctx: ExecCtx<E>,
	pub(crate) views: &'a NestedRouter<S, E>,
	pub(crate) manifest: &'a Manifest,
	pub(crate) document: &'a Document,
	pub(crate) public_filemap: Arc<BTreeMap<String, String>>,
}

#[cfg(test)]
pub(crate) async fn build_view_response<S, E>(
	input: ViewResponseInput<'_, S, E>,
) -> Result<Response<Bytes>, String>
where
	S: Send + Sync + 'static,
	E: ViewErrorClientMsg + Send + Sync + 'static,
{
	if let Some(response) =
		build_view_skew_response(input.request.uri(), input.expected_client_build_id)?
	{
		return Ok(response);
	}

	let Some(match_results) = input
		.views
		.find_nested_matches(input.request.path())
		.map_err(|err| err.to_string())?
	else {
		return view_not_found_response(input.expected_client_build_id);
	};

	let view_stack = input
		.views
		.execute_view_stack(
			input.state,
			input.exec_ctx,
			input.request.clone(),
			match_results.clone(),
			input.public_filemap.clone(),
		)
		.await
		.map_err(|err| err.to_string())?;
	build_view_response_from_results(ViewResponseResultsInput {
		expected_client_build_id: input.expected_client_build_id,
		request: &input.request,
		manifest: input.manifest,
		document: input.document,
		view_stack: &view_stack,
	})
}

pub(crate) struct ViewResponseResultsInput<'a, E> {
	pub(crate) expected_client_build_id: &'a str,
	pub(crate) request: &'a RawRequest,
	pub(crate) manifest: &'a Manifest,
	pub(crate) document: &'a Document,
	pub(crate) view_stack: &'a ViewStackExecution<E>,
}

pub(crate) fn view_not_found_response(
	expected_client_build_id: &str,
) -> Result<Response<Bytes>, String> {
	response_with_client_build_id(
		plain_text_response(StatusCode::NOT_FOUND, Bytes::new()),
		expected_client_build_id,
	)
}

pub(crate) fn build_view_response_from_results<E>(
	input: ViewResponseResultsInput<'_, E>,
) -> Result<Response<Bytes>, String>
where
	E: ViewErrorClientMsg,
{
	let is_json = is_json_request(input.request.uri());
	let (payload, merged_effects) =
		build_view_payload(input.manifest, input.document, input.view_stack, !is_json)?;

	if merged_effects.is_terminal_response() {
		return finalize_response_plan(
			ResponsePlan::ShortCircuit {
				effects: &merged_effects,
			},
			input.expected_client_build_id,
		);
	}

	let response = if is_json {
		json_response(
			StatusCode::OK,
			Bytes::from(serde_json::to_vec(&payload).map_err(|err| err.to_string())?),
		)
	} else {
		html_response(
			input.expected_client_build_id,
			input.manifest,
			input.document,
			&payload,
		)?
	};

	let status_policy = if payload.outermost_server_err_idx.is_some() {
		ResponseStatusPolicy::Suppress
	} else {
		ResponseStatusPolicy::Apply
	};
	let mut response = finalize_response_plan(
		ResponsePlan::Respond {
			effects: &merged_effects,
			response,
			status_policy,
		},
		input.expected_client_build_id,
	)?;
	if !response.headers().contains_key(CACHE_CONTROL) {
		response.headers_mut().insert(
			CACHE_CONTROL,
			HeaderValue::from_static("private, max-age=0, must-revalidate, no-cache"),
		);
	}
	Ok(response)
}

fn submitted_client_build_id(uri: &Uri) -> Option<String> {
	form_urlencoded::parse(uri.query().unwrap_or_default().as_bytes()).find_map(|(key, value)| {
		if key == QUERY_KEY_VORMA_JSON && !value.is_empty() {
			return Some(value.into_owned());
		}
		None
	})
}

fn html_response(
	expected_client_build_id: &str,
	manifest: &Manifest,
	document: &Document,
	payload: &ViewPayload,
) -> Result<Response<Bytes>, String> {
	let mut vorma_head = String::new();
	let prepared = crate::head::Prepared {
		title: payload.title.clone(),
		meta: payload.meta_head_els.clone(),
		rest: payload.rest_head_els.clone(),
	};
	vorma_head.push_str(&document.render_head(&prepared)?);
	vorma_head.push('\n');
	vorma_head.push_str(&render_element(&manifest.critical_css_el())?);
	vorma_head.push('\n');

	if !is_dev() {
		for css in &payload.css_bundles {
			let css_bundle_el = Element {
				tag: "link".to_owned(),
				attributes: BTreeMap::from([
					("rel".to_owned(), "stylesheet".to_owned()),
					("href".to_owned(), css.clone()),
					(CSS_BUNDLE_ATTR.to_owned(), css.clone()),
				]),
				self_closing: true,
				..Element::default()
			};
			vorma_head.push('\n');
			vorma_head.push_str(&render_element(&css_bundle_el)?);
		}
	}

	let mut vorma_body = String::new();
	let ssr_payload = SsrPayload {
		client_build_id: expected_client_build_id.to_owned(),
		is_dev: is_dev(),
		deployment_id: vercel_deployment_id(),
		view_payload: payload.clone(),
	};
	let payload_json = json_for_script(&ssr_payload)?;
	let data_json_el = Element {
		tag: "script".to_owned(),
		attributes: BTreeMap::from([
			("type".to_owned(), "application/json".to_owned()),
			("id".to_owned(), VORMA_DATA_JSON_SCRIPT_EL_ID.to_owned()),
		]),
		dangerous_inner_html: payload_json,
		..Element::default()
	};
	vorma_body.push_str(&render_element(&data_json_el)?);
	vorma_body.push('\n');
	vorma_body.push_str(&format!(r#"<div id="{VORMA_ROOT_EL_ID}"></div>"#));
	vorma_body.push('\n');
	if !is_dev() {
		render_module_script_to_string(&manifest.client_entry.url, &mut vorma_body)?;
	} else {
		vorma_body.push_str(&to_dev_scripts(
			manifest.dev_vite_server_port,
			&manifest.client_entry.url,
			manifest.ui_variant == crate::UiVariant::React.as_str(),
		)?);
		vorma_body.push('\n');
		vorma_body.push_str(&format!(
			"<script>{}</script>",
			refresh_script_inner_html(manifest)
		));
	}

	let html = document.render(DocumentRenderInput {
		vorma_head: &vorma_head,
		vorma_body: &vorma_body,
	})?;
	let mut response = Response::new(Bytes::from(html));
	response.headers_mut().insert(
		CONTENT_TYPE,
		HeaderValue::from_static("text/html; charset=utf-8"),
	);
	Ok(response)
}

fn vercel_deployment_id() -> String {
	let enabled = std::env::var("VERCEL_SKEW_PROTECTION_ENABLED")
		.ok()
		.is_some_and(|value| matches!(value.as_str(), "1" | "t" | "T" | "TRUE" | "true" | "True"));
	if enabled {
		return std::env::var("VERCEL_DEPLOYMENT_ID").unwrap_or_default();
	}
	String::new()
}

fn to_dev_scripts(port: i32, client_entry: &str, is_react: bool) -> Result<String, String> {
	let mut out = String::new();
	let vite_origin = vite_dev_origin(port, client_entry);
	if is_react {
		render_element_to_string(
			&Element {
				tag: "script".to_owned(),
				attributes: BTreeMap::from([("type".to_owned(), "module".to_owned())]),
				dangerous_inner_html: format!(
					"\nimport RefreshRuntime from \"{vite_origin}/@react-refresh\";\nRefreshRuntime.injectIntoGlobalHook(window);\nwindow.$RefreshReg$ = () => {{}};\nwindow.$RefreshSig$ = () => (type) => type;\nwindow.__vite_plugin_react_preamble_installed__ = true;\n"
				),
				..Element::default()
			},
			&mut out,
		)?;
		out.push('\n');
	}
	render_module_script_to_string(&format!("{vite_origin}/@vite/client"), &mut out)?;
	out.push('\n');
	render_module_script_to_string(client_entry, &mut out)?;
	Ok(out)
}

fn vite_dev_origin(port: i32, client_entry: &str) -> String {
	Url::parse(client_entry)
		.ok()
		.filter(|url| matches!(url.scheme(), "http" | "https"))
		.map(|url| url.origin().ascii_serialization())
		.filter(|origin| origin != "null")
		.unwrap_or_else(|| format!("http://127.0.0.1:{port}"))
}

pub(crate) fn refresh_script_inner_html(manifest: &Manifest) -> String {
	let script = include_str!("refresh_script.js")
		.replace(
			"__REPLACE_ME_WITH_REFRESH_PORT__",
			&manifest.dev_mux_port.to_string(),
		)
		.replace(
			"__REPLACE_ME_WITH_REFRESH_TOKEN__",
			&manifest.dev_refresh_token,
		);
	format!("\n{script}")
}

fn json_for_script(value: impl Serialize) -> Result<String, String> {
	let json = serde_json::to_string(&value).map_err(|err| err.to_string())?;
	Ok(json
		.replace('&', "\\u0026")
		.replace('<', "\\u003c")
		.replace('>', "\\u003e")
		.replace('\u{2028}', "\\u2028")
		.replace('\u{2029}', "\\u2029"))
}

/////////////////////////////////////////////////////////////////////
/////// API Handler
/////////////////////////////////////////////////////////////////////

pub(crate) struct ApiResponseInput<'a, E> {
	pub(crate) expected_client_build_id: &'a str,
	pub(crate) result: &'a TaskRouteResult<E>,
}

pub(crate) fn build_api_response<E>(
	input: ApiResponseInput<'_, E>,
) -> Result<Response<Bytes>, String> {
	let effects = input.result.response_effects();
	if let Some(error) = input.result.error() {
		if effects.is_terminal_response() {
			return finalize_response_plan(
				ResponsePlan::ShortCircuit { effects },
				input.expected_client_build_id,
			);
		}

		if matches!(error, RouteExecutionError::Input(input_error) if input_error.is_bad_request())
		{
			return finalize_response_plan(
				ResponsePlan::Respond {
					effects: input.result.middleware_effects(),
					response: plain_text_response(
						StatusCode::BAD_REQUEST,
						Bytes::from(format!("{error}\n")),
					),
					status_policy: ResponseStatusPolicy::Suppress,
				},
				input.expected_client_build_id,
			);
		}

		return finalize_response_plan(
			ResponsePlan::Respond {
				effects: input.result.middleware_effects(),
				response: internal_server_error_response(),
				status_policy: ResponseStatusPolicy::Suppress,
			},
			input.expected_client_build_id,
		);
	}

	let response = if effects.is_terminal_response() {
		return finalize_response_plan(
			ResponsePlan::ShortCircuit { effects },
			input.expected_client_build_id,
		);
	} else {
		let data = input
			.result
			.data()
			.ok_or_else(|| "missing resource data".to_owned())?;
		json_response(
			StatusCode::OK,
			Bytes::from(serde_json::to_vec(data).map_err(|err| err.to_string())?),
		)
	};

	finalize_response_plan(
		ResponsePlan::Respond {
			effects,
			response,
			status_policy: ResponseStatusPolicy::Apply,
		},
		input.expected_client_build_id,
	)
}

/////////////////////////////////////////////////////////////////////
/////// Response Helpers
/////////////////////////////////////////////////////////////////////

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