silverwolf-x's picture
download
raw
15.3 kB
use std::{ops::Deref, sync::LazyLock};
use anyhow::Result;
use once_cell::sync::Lazy;
use regex::Regex;
use rustc_hash::FxHashMap;
use turbo_rcstr::RcStr;
use turbo_tasks_fs::FileSystemPath;
use crate::next_app::{AppPage, AppPath, PageSegment, PageType};
pub mod image;
pub mod route;
pub static STATIC_LOCAL_METADATA: Lazy<FxHashMap<&'static str, &'static [&'static str]>> =
Lazy::new(|| {
FxHashMap::from_iter([
(
"icon",
&["ico", "jpg", "jpeg", "png", "svg"] as &'static [&'static str],
),
("apple-icon", &["jpg", "jpeg", "png"]),
("opengraph-image", &["jpg", "jpeg", "png", "gif"]),
("twitter-image", &["jpg", "jpeg", "png", "gif"]),
("sitemap", &["xml"]),
])
});
pub static STATIC_GLOBAL_METADATA: Lazy<FxHashMap<&'static str, &'static [&'static str]>> =
Lazy::new(|| {
FxHashMap::from_iter([
("favicon", &["ico"] as &'static [&'static str]),
("manifest", &["webmanifest", "json"]),
("robots", &["txt"]),
])
});
pub struct MetadataFileMatch<'a> {
pub metadata_type: &'a str,
pub number: Option<u32>,
pub dynamic: bool,
}
fn match_numbered_metadata(stem: &str) -> Option<(&str, &str)> {
static NUMBERED_METADATA_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new("^(icon|apple-icon|opengraph-image|twitter-image)(\\d+)$").unwrap()
});
let captures = NUMBERED_METADATA_RE.captures(stem)?;
// these captures must be defined if `captures` is `Some(...)`.
let (stem, number) = (
captures.get(1).unwrap().as_str(),
captures.get(2).unwrap().as_str(),
);
Some((stem, number))
}
fn match_metadata_file<'a>(
filename: &'a str,
page_extensions: &[RcStr],
metadata: &FxHashMap<&str, &[&str]>,
) -> Option<MetadataFileMatch<'a>> {
let (stem, ext) = filename.split_once('.')?;
let (stem, number) = match match_numbered_metadata(stem) {
Some((stem, number)) => {
let number: u32 = number.parse().ok()?;
(stem, Some(number))
}
_ => (stem, None),
};
let exts = metadata.get(stem)?;
// favicon can't be dynamic
if stem != "favicon" && page_extensions.iter().any(|e| e == ext) {
return Some(MetadataFileMatch {
metadata_type: stem,
number,
dynamic: true,
});
}
exts.contains(&ext).then_some(MetadataFileMatch {
metadata_type: stem,
number,
dynamic: false,
})
}
pub(crate) async fn get_content_type(path: FileSystemPath) -> Result<String> {
let stem = path.file_stem();
let mut ext = path.extension();
let name = stem.unwrap_or_default();
if ext == Some("jpg") {
ext = Some("jpeg");
}
if name == "favicon" && ext == Some("ico") {
return Ok("image/x-icon".to_string());
}
if name == "sitemap" {
return Ok("application/xml".to_string());
}
if name == "robots" {
return Ok("text/plain".to_string());
}
if name == "manifest" {
return Ok("application/manifest+json".to_string());
}
if let Some(ext) = ext
&& matches!(ext, "png" | "jpeg" | "ico" | "svg")
{
return Ok(mime_guess::from_ext(ext)
.first_or_octet_stream()
.to_string());
}
Ok("text/plain".to_string())
}
pub fn match_local_metadata_file<'a>(
basename: &'a str,
page_extensions: &[RcStr],
) -> Option<MetadataFileMatch<'a>> {
match_metadata_file(basename, page_extensions, STATIC_LOCAL_METADATA.deref())
}
pub struct GlobalMetadataFileMatch<'a> {
pub metadata_type: &'a str,
pub dynamic: bool,
}
pub fn match_global_metadata_file<'a>(
basename: &'a str,
page_extensions: &[RcStr],
) -> Option<GlobalMetadataFileMatch<'a>> {
match_metadata_file(basename, page_extensions, STATIC_GLOBAL_METADATA.deref()).map(|m| {
GlobalMetadataFileMatch {
metadata_type: m.metadata_type,
dynamic: m.dynamic,
}
})
}
/// Regular expression pattern used to match route parameters.
/// Matches both single parameters and parameter groups.
/// Examples:
/// - `[[...slug]]` matches parameter group with key 'slug', repeat: true, optional: true
/// - `[...slug]` matches parameter group with key 'slug', repeat: true, optional: false
/// - `[[foo]]` matches parameter with key 'foo', repeat: false, optional: true
/// - `[bar]` matches parameter with key 'bar', repeat: false, optional: false
static PARAMETER_PATTERN: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^([^\[]*)\[((?:\[[^\]]*\])|[^\]]+)\](.*)$").unwrap());
fn split_directory(path: &str) -> (Option<&str>, &str) {
if let Some((dir, basename)) = path.rsplit_once('/') {
if dir.is_empty() {
return (Some("/"), basename);
}
(Some(dir), basename)
} else {
(None, path)
}
}
fn filename(path: &str) -> &str {
split_directory(path).1
}
pub(crate) fn split_extension(path: &str) -> (&str, Option<&str>) {
let filename = filename(path);
if let Some((filename_before_extension, ext)) = filename.rsplit_once('.') {
if filename_before_extension.is_empty() {
return (filename, None);
}
(filename_before_extension, Some(ext))
} else {
(filename, None)
}
}
fn file_stem(path: &str) -> &str {
split_extension(path).0
}
fn join_path(dir: &str, basename: &str) -> String {
if dir.is_empty() || dir == "/" {
format!("/{basename}")
} else {
format!("{}/{}", dir.trim_end_matches('/'), basename)
}
}
fn normalize_static_metadata_route_segment(segment: &str) -> String {
let mut normalized_segment = segment.to_string();
while let Some(captures) = PARAMETER_PATTERN.captures(&normalized_segment) {
let prefix = captures.get(1).map(|m| m.as_str()).unwrap_or_default();
let suffix = captures.get(3).map(|m| m.as_str()).unwrap_or_default();
normalized_segment = format!("{prefix}-{suffix}");
}
normalized_segment
}
fn get_static_metadata_route(segment: &str) -> String {
let pathname = AppPath::from(AppPage::parse(segment).unwrap_or_default()).to_string();
let mut route = String::new();
for segment in pathname.split('/').filter(|segment| !segment.is_empty()) {
route.push('/');
route.push_str(&normalize_static_metadata_route_segment(segment));
}
if route.is_empty() {
"/".to_string()
} else {
route
}
}
/// When you only pass the file extension as `[]`, it will only match the static
/// convention files e.g. `/robots.txt`, `/sitemap.xml`, `/favicon.ico`,
/// `/manifest.json`.
///
/// When you pass the file extension as `['js', 'jsx', 'ts',
/// 'tsx']`, it will also match the dynamic convention files e.g. /robots.js,
/// /sitemap.tsx, /favicon.jsx, /manifest.ts.
///
/// When `withExtension` is false, it will match the static convention files
/// without the extension, by default it's true e.g. /robots, /sitemap,
/// /favicon, /manifest, use to match dynamic API routes like app/robots.ts.
pub fn is_metadata_route_file(
app_dir_relative_path: &str,
page_extensions: &[RcStr],
with_extension: bool,
) -> bool {
let (dir, filename) = split_directory(app_dir_relative_path);
if with_extension {
if match_local_metadata_file(filename, page_extensions).is_some() {
return true;
}
} else {
let stem = file_stem(filename);
let stem = match_numbered_metadata(stem)
.map(|(stem, _)| stem)
.unwrap_or(stem);
if STATIC_LOCAL_METADATA.contains_key(stem) {
return true;
}
}
if dir != Some("/") {
return false;
}
if with_extension {
if match_global_metadata_file(filename, page_extensions).is_some() {
return true;
}
} else {
let base_name = file_stem(filename);
if STATIC_GLOBAL_METADATA.contains_key(base_name) {
return true;
}
}
false
}
/// Remove the 'app' prefix or '/route' suffix, only check the route name since
/// they're only allowed in root app directory
///
/// e.g.
/// - /app/robots -> /robots
/// - app/robots -> /robots
/// - /robots -> /robots
pub fn is_metadata_route(mut route: &str) -> bool {
if let Some(stripped) = route.strip_prefix("/app/") {
route = stripped;
} else if let Some(stripped) = route.strip_prefix("app/") {
route = stripped;
}
if let Some(stripped) = route.strip_suffix("/route") {
route = stripped;
}
let mut page = route.to_string();
if !page.starts_with('/') {
page = format!("/{page}");
}
!page.ends_with("/page") && is_metadata_route_file(&page, &[], false)
}
/// djb_2 hash implementation referenced from [here](http://www.cse.yorku.ca/~oz/hash.html)
fn djb2_hash(str: &str) -> u32 {
str.chars().fold(5381, |hash, c| {
((hash << 5).wrapping_add(hash)).wrapping_add(c as u32) // hash * 33 + c
})
}
// this is here to mirror next.js behaviour (`toString(36).slice(0, 6)`)
fn format_radix(mut x: u32, radix: u32) -> String {
let mut result = vec![];
loop {
let m = x % radix;
x /= radix;
// will panic if you use a bad radix (< 2 or > 36).
result.push(std::char::from_digit(m, radix).unwrap());
if x == 0 {
break;
}
}
result.reverse();
// We only need the first 6 characters of the hash but sometimes the hash is too short.
// In JavaScript, we use `toString(36).slice(0, 6)` to get the first 6 characters of the hash,
// but it will automatically take the minimum of the length of the hash and 6. Rust will panic.
let len = result.len().min(6);
result[..len].iter().collect()
}
/// If there's special convention like (...) or @ in the page path,
/// Give it a unique hash suffix to avoid conflicts
///
/// e.g.
/// /opengraph-image -> /opengraph-image
/// /(post)/opengraph-image.tsx -> /opengraph-image-[0-9a-z]{6}
///
/// Sitemap is an exception, it should not have a suffix.
/// As the generated urls are for indexer and usually one sitemap contains all the urls of the sub
/// routes. The sitemap should be unique in each level and not have a suffix.
///
/// /sitemap -> /sitemap
/// /(post)/sitemap -> /sitemap
fn get_metadata_route_suffix(page: &str) -> Option<String> {
// skip sitemap
if page.ends_with("/sitemap") || page.ends_with("/sitemap.xml") {
return None;
}
// Get the parent pathname of the page
let parent_pathname = split_directory(page).0.unwrap_or_default();
let segments = parent_pathname.split('/').collect::<Vec<&str>>();
// if any segment is group or parallel route segment, we should add a suffix.
if segments.iter().any(|segment| {
segment.starts_with('(') && segment.ends_with(')')
|| segment.starts_with('@') && *segment != "@children"
}) {
Some(format_radix(djb2_hash(parent_pathname), 36))
} else {
None
}
}
pub fn fill_static_metadata_segment(segment: &str, last_segment: &str) -> String {
let route = get_static_metadata_route(segment);
let (name, ext) = split_extension(last_segment);
let page_path = join_path(segment, name);
let route_suffix = get_metadata_route_suffix(&page_path)
.map(|suffix| format!("-{suffix}"))
.unwrap_or_default();
let filename = format!(
"{}{}{}",
name,
route_suffix,
ext.map(|ext| format!(".{ext}")).unwrap_or_default()
);
join_path(&route, &filename)
}
/// Map metadata page key to the corresponding route
///
/// static file page key: /app/robots.txt -> /robots.txt -> /robots.txt/route
/// dynamic route page key: /app/robots.tsx -> /robots -> /robots.txt/route
pub fn normalize_metadata_route(mut page: AppPage) -> Result<AppPage> {
if !is_metadata_route(&format!("{page}")) {
return Ok(page);
}
let mut route = page.to_string();
let mut suffix: Option<String> = None;
if route == "/robots" {
route += ".txt"
} else if route == "/manifest" {
route += ".webmanifest"
} else if route.ends_with("/sitemap") {
route += ".xml"
} else {
suffix = get_metadata_route_suffix(&route);
}
// Support both /<metadata-route.ext> and custom routes
// /<metadata-route>/route.ts. If it's a metadata file route, we need to
// append /[id]/route to the page.
if !route.ends_with("/route") {
let (base_name, ext) = split_extension(&route);
page.0.pop();
page.push(PageSegment::Static(
format!(
"{}{}{}",
base_name,
suffix
.map(|suffix| format!("-{suffix}"))
.unwrap_or_default(),
ext.map(|ext| format!(".{ext}")).unwrap_or_default(),
)
.into(),
))?;
page.push(PageSegment::PageType(PageType::Route))?;
}
Ok(page)
}
#[cfg(test)]
mod test {
use super::{djb2_hash, fill_static_metadata_segment, format_radix, normalize_metadata_route};
use crate::next_app::AppPage;
#[test]
fn test_normalize_metadata_route() {
let cases = vec![
[
"/client/(meme)/more-route/twitter-image",
"/client/(meme)/more-route/twitter-image-769mad/route",
],
[
"/client/(meme)/more-route/twitter-image2",
"/client/(meme)/more-route/twitter-image2-769mad/route",
],
["/robots.txt", "/robots.txt/route"],
["/manifest.webmanifest", "/manifest.webmanifest/route"],
["/sitemap", "/sitemap.xml/route"],
["/sitemap.xml", "/sitemap.xml/route"],
["/blog/sitemap", "/blog/sitemap.xml/route"],
];
for [input, expected] in cases {
let page = AppPage::parse(input).unwrap();
let normalized = normalize_metadata_route(page).unwrap();
assert_eq!(&normalized.to_string(), expected);
}
}
#[test]
fn test_format_radix_doesnt_panic_with_result_less_than_6_characters() {
let hash = format_radix(djb2_hash("/lookup/[domain]/(dns)"), 36);
assert!(hash.len() < 6);
}
#[test]
fn test_fill_static_metadata_segment() {
assert_eq!(
fill_static_metadata_segment("/", "favicon.ico"),
"/favicon.ico"
);
assert_eq!(
fill_static_metadata_segment("/blog/[slug]", "favicon.ico"),
"/blog/-/favicon.ico"
);
assert_eq!(
fill_static_metadata_segment("/client/(meme)/more-route", "twitter-image.png"),
"/client/more-route/twitter-image-769mad.png"
);
assert_eq!(
fill_static_metadata_segment("/(group)/group", "icon.png"),
"/group/icon-131tc6.png"
);
assert_eq!(
fill_static_metadata_segment("/parallel/@parallel", "icon.png"),
"/parallel/icon-kzjltp.png"
);
}
}

Xet Storage Details

Size:
15.3 kB
·
Xet hash:
4bce45cdfea61dd212d88a5d35f320ff75870641ccf8c33048b1bd6a57eb5f6b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.