Skip to content

Commit fa4b4bf

Browse files
authored
refactor: use resolve_media_type_and_charset_from_content_type from deno_media_type (#550)
1 parent ead9457 commit fa4b4bf

File tree

3 files changed

+8
-192
lines changed

3 files changed

+8
-192
lines changed

Cargo.lock

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ async-trait = "0.1.68"
4444
capacity_builder = "0.5.0"
4545
data-url = "0.3.0"
4646
deno_ast = { version = "0.44.0", features = ["dep_analysis", "emit"] }
47+
deno_media_type = "0.2.3"
4748
deno_unsync.workspace = true
4849
deno_path_util = "0.3.0"
4950
deno_semver = "0.7.1"

src/source/mod.rs

+4-190
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,11 @@ pub fn resolve_media_type_and_charset_from_headers<'a>(
782782
specifier: &ModuleSpecifier,
783783
maybe_headers: Option<&'a HashMap<String, String>>,
784784
) -> (MediaType, Option<&'a str>) {
785-
resolve_media_type_and_charset_from_content_type(
785+
deno_media_type::resolve_media_type_and_charset_from_content_type(
786786
specifier,
787-
maybe_headers.and_then(|h| h.get("content-type")),
787+
maybe_headers
788+
.and_then(|h| h.get("content-type"))
789+
.map(|v| v.as_str()),
788790
)
789791
}
790792

@@ -987,194 +989,6 @@ pub mod tests {
987989
);
988990
}
989991

990-
macro_rules! file_url {
991-
($path:expr) => {
992-
if cfg!(target_os = "windows") {
993-
concat!("file:///C:", $path)
994-
} else {
995-
concat!("file://", $path)
996-
}
997-
};
998-
}
999-
1000-
#[test]
1001-
fn test_resolve_media_type_and_charset_from_content_type() {
1002-
let fixtures = vec![
1003-
// Extension only
1004-
(file_url!("/foo/bar.ts"), None, MediaType::TypeScript, None),
1005-
(file_url!("/foo/bar.tsx"), None, MediaType::Tsx, None),
1006-
(file_url!("/foo/bar.d.cts"), None, MediaType::Dcts, None),
1007-
(file_url!("/foo/bar.d.mts"), None, MediaType::Dmts, None),
1008-
(file_url!("/foo/bar.d.ts"), None, MediaType::Dts, None),
1009-
(file_url!("/foo/bar.js"), None, MediaType::JavaScript, None),
1010-
(file_url!("/foo/bar.jsx"), None, MediaType::Jsx, None),
1011-
(file_url!("/foo/bar.json"), None, MediaType::Json, None),
1012-
(file_url!("/foo/bar.wasm"), None, MediaType::Wasm, None),
1013-
(file_url!("/foo/bar.cjs"), None, MediaType::Cjs, None),
1014-
(file_url!("/foo/bar.mjs"), None, MediaType::Mjs, None),
1015-
(file_url!("/foo/bar.cts"), None, MediaType::Cts, None),
1016-
(file_url!("/foo/bar.mts"), None, MediaType::Mts, None),
1017-
(file_url!("/foo/bar"), None, MediaType::Unknown, None),
1018-
// Media type no extension
1019-
(
1020-
"https://deno.land/x/mod",
1021-
Some("application/typescript".to_string()),
1022-
MediaType::TypeScript,
1023-
None,
1024-
),
1025-
(
1026-
"https://deno.land/x/mod",
1027-
Some("text/typescript".to_string()),
1028-
MediaType::TypeScript,
1029-
None,
1030-
),
1031-
(
1032-
"https://deno.land/x/mod",
1033-
Some("video/vnd.dlna.mpeg-tts".to_string()),
1034-
MediaType::TypeScript,
1035-
None,
1036-
),
1037-
(
1038-
"https://deno.land/x/mod",
1039-
Some("video/mp2t".to_string()),
1040-
MediaType::TypeScript,
1041-
None,
1042-
),
1043-
(
1044-
"https://deno.land/x/mod",
1045-
Some("application/x-typescript".to_string()),
1046-
MediaType::TypeScript,
1047-
None,
1048-
),
1049-
(
1050-
"https://deno.land/x/mod",
1051-
Some("application/javascript".to_string()),
1052-
MediaType::JavaScript,
1053-
None,
1054-
),
1055-
(
1056-
"https://deno.land/x/mod",
1057-
Some("text/javascript".to_string()),
1058-
MediaType::JavaScript,
1059-
None,
1060-
),
1061-
(
1062-
"https://deno.land/x/mod",
1063-
Some("application/ecmascript".to_string()),
1064-
MediaType::JavaScript,
1065-
None,
1066-
),
1067-
(
1068-
"https://deno.land/x/mod",
1069-
Some("text/ecmascript".to_string()),
1070-
MediaType::JavaScript,
1071-
None,
1072-
),
1073-
(
1074-
"https://deno.land/x/mod",
1075-
Some("application/x-javascript".to_string()),
1076-
MediaType::JavaScript,
1077-
None,
1078-
),
1079-
(
1080-
"https://deno.land/x/mod",
1081-
Some("application/node".to_string()),
1082-
MediaType::JavaScript,
1083-
None,
1084-
),
1085-
(
1086-
"https://deno.land/x/mod",
1087-
Some("text/jsx".to_string()),
1088-
MediaType::Jsx,
1089-
None,
1090-
),
1091-
(
1092-
"https://deno.land/x/mod",
1093-
Some("text/tsx".to_string()),
1094-
MediaType::Tsx,
1095-
None,
1096-
),
1097-
(
1098-
"https://deno.land/x/mod",
1099-
Some("text/json".to_string()),
1100-
MediaType::Json,
1101-
None,
1102-
),
1103-
(
1104-
"https://deno.land/x/mod",
1105-
Some("text/json; charset=utf-8".to_string()),
1106-
MediaType::Json,
1107-
Some("utf-8".to_string()),
1108-
),
1109-
// Extension with media type
1110-
(
1111-
"https://deno.land/x/mod.ts",
1112-
Some("text/plain".to_string()),
1113-
MediaType::TypeScript,
1114-
None,
1115-
),
1116-
(
1117-
"https://deno.land/x/mod.ts",
1118-
Some("foo/bar".to_string()),
1119-
MediaType::Unknown,
1120-
None,
1121-
),
1122-
(
1123-
"https://deno.land/x/mod.tsx",
1124-
Some("application/typescript".to_string()),
1125-
MediaType::Tsx,
1126-
None,
1127-
),
1128-
(
1129-
"https://deno.land/x/mod.tsx",
1130-
Some("application/javascript".to_string()),
1131-
MediaType::Tsx,
1132-
None,
1133-
),
1134-
(
1135-
"https://deno.land/x/mod.jsx",
1136-
Some("application/javascript".to_string()),
1137-
MediaType::Jsx,
1138-
None,
1139-
),
1140-
(
1141-
"https://deno.land/x/mod.jsx",
1142-
Some("application/x-typescript".to_string()),
1143-
MediaType::Jsx,
1144-
None,
1145-
),
1146-
(
1147-
"https://deno.land/x/mod.d.ts",
1148-
Some("application/javascript".to_string()),
1149-
MediaType::Dts,
1150-
None,
1151-
),
1152-
(
1153-
"https://deno.land/x/mod.d.ts",
1154-
Some("text/plain".to_string()),
1155-
MediaType::Dts,
1156-
None,
1157-
),
1158-
(
1159-
"https://deno.land/x/mod.d.ts",
1160-
Some("application/x-typescript".to_string()),
1161-
MediaType::Dts,
1162-
None,
1163-
),
1164-
];
1165-
1166-
for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures {
1167-
let specifier = ModuleSpecifier::parse(specifier).unwrap();
1168-
assert_eq!(
1169-
resolve_media_type_and_charset_from_content_type(
1170-
&specifier,
1171-
maybe_content_type.as_ref()
1172-
),
1173-
(media_type, maybe_charset.as_deref())
1174-
);
1175-
}
1176-
}
1177-
1178992
#[test]
1179993
fn test_parse_valid_data_url() {
1180994
let valid_data_url = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==";

0 commit comments

Comments
 (0)