diff options
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())); |