diff options
author | Satya Rohith <me@satyarohith.com> | 2021-04-24 00:13:13 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-23 20:43:13 +0200 |
commit | 13f7592b8a7a7f79e9d89117a1bdbeaf999622c4 (patch) | |
tree | e53978e022ac6b0f0e63017d44de77a9687f3069 /cli/lsp/urls.rs | |
parent | feb6af77323bfbcb8409ae529c8d152362d1c203 (diff) |
refactor: use 'data-url' crate to process data URLs in lsp & file_fetcher (#10196)
Closes: #10118
Diffstat (limited to 'cli/lsp/urls.rs')
-rw-r--r-- | cli/lsp/urls.rs | 34 |
1 files changed, 7 insertions, 27 deletions
diff --git a/cli/lsp/urls.rs b/cli/lsp/urls.rs index 770dd98c3..559f1652a 100644 --- a/cli/lsp/urls.rs +++ b/cli/lsp/urls.rs @@ -3,6 +3,8 @@ use crate::file_fetcher::map_content_type; use crate::media_type::MediaType; +use data_url::DataUrl; +use deno_core::error::uri_error; use deno_core::error::AnyError; use deno_core::url::Position; use deno_core::url::Url; @@ -49,17 +51,6 @@ fn hash_data_specifier(specifier: &ModuleSpecifier) -> String { crate::checksum::gen(&[file_name_str.as_bytes()]) } -fn data_url_media_type(specifier: &ModuleSpecifier) -> MediaType { - let path = specifier.path(); - let mut parts = path.splitn(2, ','); - let media_type_part = - percent_encoding::percent_decode_str(parts.next().unwrap()) - .decode_utf8_lossy(); - let (media_type, _) = - map_content_type(specifier, Some(media_type_part.into())); - media_type -} - /// A bi-directional map of URLs sent to the LSP client and internal module /// specifiers. We need to map internal specifiers into `deno:` schema URLs /// to allow the Deno language server to manage these as virtual documents. @@ -96,7 +87,11 @@ impl LspUrlMap { specifier.clone() } else { let specifier_str = if specifier.scheme() == "data" { - let media_type = data_url_media_type(specifier); + let data_url = DataUrl::process(specifier.as_str()) + .map_err(|e| uri_error(format!("{:?}", e)))?; + let mime = data_url.mime_type(); + let (media_type, _) = + map_content_type(specifier, Some(format!("{}", mime))); let extension = if media_type == MediaType::Unknown { "" } else { @@ -160,21 +155,6 @@ mod tests { } #[test] - fn test_data_url_media_type() { - let fixture = resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap(); - let actual = data_url_media_type(&fixture); - assert_eq!(actual, MediaType::TypeScript); - - let fixture = resolve_url("data:application/javascript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap(); - let actual = data_url_media_type(&fixture); - assert_eq!(actual, MediaType::JavaScript); - - let fixture = resolve_url("data:text/plain;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap(); - let actual = data_url_media_type(&fixture); - assert_eq!(actual, MediaType::Unknown); - } - - #[test] fn test_lsp_url_map() { let mut map = LspUrlMap::default(); let fixture = resolve_url("https://deno.land/x/pkg@1.0.0/mod.ts").unwrap(); |