use php_std::path::pathinfo_extension;
pub trait Rendering: Send + Sync {
fn name(&self) -> &str;
fn files(&self, framework: &str) -> &[&str];
fn fallback_file(&self) -> Option<&str>;
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Ssr {
fallback_file: Option<String>,
}
impl Ssr {
pub fn new() -> Self {
Ssr::default()
}
pub fn with_fallback(fallback_file: impl Into<String>) -> Self {
Ssr { fallback_file: Some(fallback_file.into()) }
}
pub const FRAMEWORK_FILES: &'static [(&'static str, &'static [&'static str])] = &[
("nextjs", &[".next/server/webpack-runtime.js", ".next/turbopack", "server.js"]),
("nuxt", &["server/index.mjs"]),
("sveltekit", &["handler.js"]),
("astro", &["server/entry.mjs"]),
("remix", &["build/server/index.js"]),
("angular", &["server/server.mjs"]),
("analog", &["server/index.mjs"]),
("tanstack-start", &["server/server.js", "server/index.mjs"]),
("flutter", &[]),
("jaspr", &["app"]),
("lynx", &[]),
];
}
impl Rendering for Ssr {
fn name(&self) -> &str {
"ssr"
}
fn files(&self, framework: &str) -> &[&str] {
Ssr::FRAMEWORK_FILES.iter().find(|(name, _)| *name == framework).map_or(&[], |(_, files)| files)
}
fn fallback_file(&self) -> Option<&str> {
self.fallback_file.as_deref()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Static {
fallback_file: Option<String>,
}
impl Static {
pub fn new() -> Self {
Static::default()
}
pub fn with_fallback(fallback_file: impl Into<String>) -> Self {
Static { fallback_file: Some(fallback_file.into()) }
}
}
impl Rendering for Static {
fn name(&self) -> &str {
"static"
}
fn files(&self, _framework: &str) -> &[&str] {
&[]
}
fn fallback_file(&self) -> Option<&str> {
self.fallback_file.as_deref()
}
}
#[derive(Debug, Clone)]
pub enum Rendered<'a> {
Option(&'a dyn Rendering),
Static(Static),
}
impl Rendered<'_> {
pub fn rendering(&self) -> &dyn Rendering {
match self {
Rendered::Option(r) => *r,
Rendered::Static(s) => s,
}
}
pub fn name(&self) -> &str {
self.rendering().name()
}
pub fn fallback_file(&self) -> Option<&str> {
self.rendering().fallback_file()
}
}
impl std::fmt::Debug for dyn Rendering + '_ {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Rendering").field("name", &self.name()).field("fallback_file", &self.fallback_file()).finish()
}
}
pub fn html_fallback<'a>(inputs: impl IntoIterator<Item = &'a [u8]>) -> Option<&'a [u8]> {
let mut html = inputs.into_iter().filter(|f| pathinfo_extension(f) == b"html");
match (html.next(), html.next()) {
(Some(only), None) => Some(only),
_ => None,
}
}
pub struct RenderingDetector {
framework: String,
options: Vec<Box<dyn Rendering>>,
inputs: Vec<String>,
}
impl RenderingDetector {
pub fn new(framework: impl Into<String>) -> Self {
RenderingDetector { framework: framework.into(), options: Vec::new(), inputs: Vec::new() }
}
pub fn add_option(&mut self, option: impl Rendering + 'static) -> &mut Self {
self.options.push(Box::new(option));
self
}
pub fn add_input(&mut self, file: impl Into<String>) -> &mut Self {
self.inputs.push(file.into());
self
}
fn static_fallback(&self) -> Static {
match html_fallback(self.inputs.iter().map(String::as_bytes)) {
Some(only) => Static::with_fallback(String::from_utf8_lossy(only)),
None => Static::new(),
}
}
pub fn detect(&self) -> Rendered<'_> {
self.options
.iter()
.find(|r| r.files(&self.framework).iter().any(|f| self.inputs.iter().any(|i| i == f)))
.map_or_else(|| Rendered::Static(self.static_fallback()), |r| Rendered::Option(r.as_ref()))
}
}
impl std::fmt::Debug for RenderingDetector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RenderingDetector")
.field("framework", &self.framework)
.field("options", &self.options.iter().map(|o| o.name()).collect::<Vec<_>>())
.field("inputs", &self.inputs)
.finish()
}
}