Skip to content

Commit

Permalink
fix(cli): import maps handles data URLs (denoland#9437)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored Feb 9, 2021
1 parent 36e9e53 commit 97d5ef2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
36 changes: 27 additions & 9 deletions cli/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl fmt::Display for ImportMapError {

impl Error for ImportMapError {}

// NOTE: here is difference between deno and reference implementation - deno currently
// can't resolve URL with other schemes (eg. data:, about:, blob:)
const SUPPORTED_FETCH_SCHEMES: [&str; 3] = ["http", "https", "file"];
// NOTE: here is difference between deno and reference implementation - Deno
// doesn't resolve URLs outside of the supported schemes.
const SUPPORTED_FETCH_SCHEMES: [&str; 4] = ["http", "https", "file", "data"];

type SpecifierMap = IndexMap<String, Vec<ModuleSpecifier>>;
type ScopesMap = IndexMap<String, SpecifierMap>;
Expand Down Expand Up @@ -863,9 +863,9 @@ mod tests {
"https://good/": {},
"https://good/": {},
"file:https:///good": {},
"data:good": {},
"about:bad": {},
"blob:bad": {},
"data:bad": {},
"filesystem:bad": {},
"ftp:https://bad/": {},
"import:bad": {},
Expand All @@ -880,7 +880,8 @@ mod tests {
assert!(import_map.scopes.contains_key("https://good/"));
assert!(import_map.scopes.contains_key("https://good/"));
assert!(import_map.scopes.contains_key("file:https:///good"));
assert_eq!(import_map.scopes.len(), 3);
assert!(import_map.scopes.contains_key("data:good"));
assert_eq!(import_map.scopes.len(), 4);

// Should parse absolute URL scope keys, ignoring unparseable ones..
let json_map = r#"{
Expand Down Expand Up @@ -994,9 +995,9 @@ mod tests {
"http": "https://good/",
"https": "https://good/",
"file": "file:https:///good",
"data": "data:good",
"about": "about:bad",
"blob": "blob:bad",
"data": "data:bad",
"filesystem": "filesystem:bad",
"ftp": "ftp:https://good/",
"import": "import:bad",
Expand All @@ -1021,10 +1022,13 @@ mod tests {
import_map.imports.get("https").unwrap(),
&vec!["https://good/".to_string()]
);
assert_eq!(
import_map.imports.get("data").unwrap(),
&vec!["data:good".to_string()]
);

assert!(import_map.imports.get("about").unwrap().is_empty());
assert!(import_map.imports.get("blob").unwrap().is_empty());
assert!(import_map.imports.get("data").unwrap().is_empty());
assert!(import_map.imports.get("filesystem").unwrap().is_empty());
assert!(import_map.imports.get("ftp").unwrap().is_empty());
assert!(import_map.imports.get("import").unwrap().is_empty());
Expand All @@ -1041,9 +1045,9 @@ mod tests {
"http": ["https://good/"],
"https": ["https://good/"],
"file": ["file:https:///good"],
"data": ["data:good"],
"about": ["about:bad"],
"blob": ["blob:bad"],
"data": ["data:bad"],
"filesystem": ["filesystem:bad"],
"ftp": ["ftp:https://good/"],
"import": ["import:bad"],
Expand All @@ -1068,10 +1072,13 @@ mod tests {
import_map.imports.get("https").unwrap(),
&vec!["https://good/".to_string()]
);
assert_eq!(
import_map.imports.get("data").unwrap(),
&vec!["data:good".to_string()]
);

assert!(import_map.imports.get("about").unwrap().is_empty());
assert!(import_map.imports.get("blob").unwrap().is_empty());
assert!(import_map.imports.get("data").unwrap().is_empty());
assert!(import_map.imports.get("filesystem").unwrap().is_empty());
assert!(import_map.imports.get("ftp").unwrap().is_empty());
assert!(import_map.imports.get("import").unwrap().is_empty());
Expand Down Expand Up @@ -2069,4 +2076,15 @@ mod tests {
"https://example.com/app/none.mjs",
);
}

#[test]
fn resolve_data_urls() {
let base_url = "https://example.com/app/main.ts";
let json_map = r#"{}"#;
let import_map = ImportMap::from_json(base_url, json_map).unwrap();
assert_resolve(
import_map.resolve("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=", base_url),
"data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=",
);
}
}
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3577,6 +3577,11 @@ console.log("finish");
exit_code: 1,
});

itest!(import_data_url_import_map {
args: "run --quiet --reload --unstable --import-map import_maps/import_map.json import_data_url.ts",
output: "import_data_url.ts.out",
});

itest!(import_data_url_imports {
args: "run --quiet --reload import_data_url_imports.ts",
output: "import_data_url_imports.ts.out",
Expand Down

0 comments on commit 97d5ef2

Please sign in to comment.