diff options
author | Satya Rohith <me@satyarohith.com> | 2021-09-13 09:49:23 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-13 09:49:23 +0530 |
commit | 84f874715763df71bb3bbf77f0714f8afdc17bb3 (patch) | |
tree | 1d4b2b5da1a79fab2f6ccec4fd15601057afbc77 /cli/file_fetcher.rs | |
parent | a442821d9790489242e8cdb9286ae3b237e4705c (diff) |
fix(lsp): support data urls in `deno.importMap` option (#11397)
Diffstat (limited to 'cli/file_fetcher.rs')
-rw-r--r-- | cli/file_fetcher.rs | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index 1e4c5d453..3e259ed54 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -143,6 +143,24 @@ fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> { }) } +/// Returns the decoded body and content-type of a provided +/// data URL. +pub fn get_source_from_data_url( + specifier: &ModuleSpecifier, +) -> Result<(String, String), AnyError> { + let data_url = DataUrl::process(specifier.as_str()) + .map_err(|e| uri_error(format!("{:?}", e)))?; + let mime = data_url.mime_type(); + let charset = mime.get_parameter("charset").map(|v| v.to_string()); + let (bytes, _) = data_url + .decode_to_vec() + .map_err(|e| uri_error(format!("{:?}", e)))?; + Ok(( + strip_shebang(get_source_from_bytes(bytes, charset)?), + format!("{}", mime), + )) +} + /// Given a vector of bytes and optionally a charset, decode the bytes to a /// string. pub fn get_source_from_bytes( @@ -340,15 +358,7 @@ impl FileFetcher { )); } - let data_url = DataUrl::process(specifier.as_str()) - .map_err(|e| uri_error(format!("{:?}", e)))?; - let mime = data_url.mime_type(); - let charset = mime.get_parameter("charset").map(|v| v.to_string()); - let (bytes, _) = data_url - .decode_to_vec() - .map_err(|e| uri_error(format!("{:?}", e)))?; - let source = strip_shebang(get_source_from_bytes(bytes, charset)?); - let content_type = format!("{}", mime); + let (source, content_type) = get_source_from_data_url(specifier)?; let (media_type, _) = map_content_type(specifier, Some(content_type.clone())); |