Skip to content

Commit

Permalink
refactor: use resolve_media_type_and_charset_from_content_type from d…
Browse files Browse the repository at this point in the history
…eno_media_type (#550)
  • Loading branch information
dsherret authored Jan 7, 2025
1 parent ead9457 commit fa4b4bf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 192 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async-trait = "0.1.68"
capacity_builder = "0.5.0"
data-url = "0.3.0"
deno_ast = { version = "0.44.0", features = ["dep_analysis", "emit"] }
deno_media_type = "0.2.3"
deno_unsync.workspace = true
deno_path_util = "0.3.0"
deno_semver = "0.7.1"
Expand Down
194 changes: 4 additions & 190 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,11 @@ pub fn resolve_media_type_and_charset_from_headers<'a>(
specifier: &ModuleSpecifier,
maybe_headers: Option<&'a HashMap<String, String>>,
) -> (MediaType, Option<&'a str>) {
resolve_media_type_and_charset_from_content_type(
deno_media_type::resolve_media_type_and_charset_from_content_type(
specifier,
maybe_headers.and_then(|h| h.get("content-type")),
maybe_headers
.and_then(|h| h.get("content-type"))
.map(|v| v.as_str()),
)
}

Expand Down Expand Up @@ -987,194 +989,6 @@ pub mod tests {
);
}

macro_rules! file_url {
($path:expr) => {
if cfg!(target_os = "windows") {
concat!("file:///C:", $path)
} else {
concat!("file://", $path)
}
};
}

#[test]
fn test_resolve_media_type_and_charset_from_content_type() {
let fixtures = vec![
// Extension only
(file_url!("/foo/bar.ts"), None, MediaType::TypeScript, None),
(file_url!("/foo/bar.tsx"), None, MediaType::Tsx, None),
(file_url!("/foo/bar.d.cts"), None, MediaType::Dcts, None),
(file_url!("/foo/bar.d.mts"), None, MediaType::Dmts, None),
(file_url!("/foo/bar.d.ts"), None, MediaType::Dts, None),
(file_url!("/foo/bar.js"), None, MediaType::JavaScript, None),
(file_url!("/foo/bar.jsx"), None, MediaType::Jsx, None),
(file_url!("/foo/bar.json"), None, MediaType::Json, None),
(file_url!("/foo/bar.wasm"), None, MediaType::Wasm, None),
(file_url!("/foo/bar.cjs"), None, MediaType::Cjs, None),
(file_url!("/foo/bar.mjs"), None, MediaType::Mjs, None),
(file_url!("/foo/bar.cts"), None, MediaType::Cts, None),
(file_url!("/foo/bar.mts"), None, MediaType::Mts, None),
(file_url!("/foo/bar"), None, MediaType::Unknown, None),
// Media type no extension
(
"https://deno.land/x/mod",
Some("application/typescript".to_string()),
MediaType::TypeScript,
None,
),
(
"https://deno.land/x/mod",
Some("text/typescript".to_string()),
MediaType::TypeScript,
None,
),
(
"https://deno.land/x/mod",
Some("video/vnd.dlna.mpeg-tts".to_string()),
MediaType::TypeScript,
None,
),
(
"https://deno.land/x/mod",
Some("video/mp2t".to_string()),
MediaType::TypeScript,
None,
),
(
"https://deno.land/x/mod",
Some("application/x-typescript".to_string()),
MediaType::TypeScript,
None,
),
(
"https://deno.land/x/mod",
Some("application/javascript".to_string()),
MediaType::JavaScript,
None,
),
(
"https://deno.land/x/mod",
Some("text/javascript".to_string()),
MediaType::JavaScript,
None,
),
(
"https://deno.land/x/mod",
Some("application/ecmascript".to_string()),
MediaType::JavaScript,
None,
),
(
"https://deno.land/x/mod",
Some("text/ecmascript".to_string()),
MediaType::JavaScript,
None,
),
(
"https://deno.land/x/mod",
Some("application/x-javascript".to_string()),
MediaType::JavaScript,
None,
),
(
"https://deno.land/x/mod",
Some("application/node".to_string()),
MediaType::JavaScript,
None,
),
(
"https://deno.land/x/mod",
Some("text/jsx".to_string()),
MediaType::Jsx,
None,
),
(
"https://deno.land/x/mod",
Some("text/tsx".to_string()),
MediaType::Tsx,
None,
),
(
"https://deno.land/x/mod",
Some("text/json".to_string()),
MediaType::Json,
None,
),
(
"https://deno.land/x/mod",
Some("text/json; charset=utf-8".to_string()),
MediaType::Json,
Some("utf-8".to_string()),
),
// Extension with media type
(
"https://deno.land/x/mod.ts",
Some("text/plain".to_string()),
MediaType::TypeScript,
None,
),
(
"https://deno.land/x/mod.ts",
Some("foo/bar".to_string()),
MediaType::Unknown,
None,
),
(
"https://deno.land/x/mod.tsx",
Some("application/typescript".to_string()),
MediaType::Tsx,
None,
),
(
"https://deno.land/x/mod.tsx",
Some("application/javascript".to_string()),
MediaType::Tsx,
None,
),
(
"https://deno.land/x/mod.jsx",
Some("application/javascript".to_string()),
MediaType::Jsx,
None,
),
(
"https://deno.land/x/mod.jsx",
Some("application/x-typescript".to_string()),
MediaType::Jsx,
None,
),
(
"https://deno.land/x/mod.d.ts",
Some("application/javascript".to_string()),
MediaType::Dts,
None,
),
(
"https://deno.land/x/mod.d.ts",
Some("text/plain".to_string()),
MediaType::Dts,
None,
),
(
"https://deno.land/x/mod.d.ts",
Some("application/x-typescript".to_string()),
MediaType::Dts,
None,
),
];

for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures {
let specifier = ModuleSpecifier::parse(specifier).unwrap();
assert_eq!(
resolve_media_type_and_charset_from_content_type(
&specifier,
maybe_content_type.as_ref()
),
(media_type, maybe_charset.as_deref())
);
}
}

#[test]
fn test_parse_valid_data_url() {
let valid_data_url = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==";
Expand Down

0 comments on commit fa4b4bf

Please sign in to comment.