1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -05:00

Make ModuleSpecifier a type alias, not wrapper struct (#9531)

This commit is contained in:
Ryan Dahl 2021-02-17 13:47:18 -05:00 committed by GitHub
parent f6d6b24506
commit c7dabc99ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 599 additions and 806 deletions

View file

@ -4,6 +4,7 @@ use crate::media_type::MediaType;
use crate::tsc_config; use crate::tsc_config;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::resolve_url_or_path;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use std::error::Error; use std::error::Error;
@ -78,7 +79,7 @@ impl Into<Location> for swc_common::Loc {
impl Into<ModuleSpecifier> for Location { impl Into<ModuleSpecifier> for Location {
fn into(self) -> ModuleSpecifier { fn into(self) -> ModuleSpecifier {
ModuleSpecifier::resolve_url_or_path(&self.filename).unwrap() resolve_url_or_path(&self.filename).unwrap()
} }
} }
@ -593,9 +594,7 @@ mod tests {
#[test] #[test]
fn test_parsed_module_analyze_dependencies() { fn test_parsed_module_analyze_dependencies() {
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/mod.js").unwrap();
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/mod.js")
.unwrap();
let source = r#"import * as bar from "./test.ts"; let source = r#"import * as bar from "./test.ts";
const foo = await import("./foo.ts"); const foo = await import("./foo.ts");
"#; "#;
@ -634,8 +633,7 @@ mod tests {
#[test] #[test]
fn test_transpile() { fn test_transpile() {
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/mod.ts")
.expect("could not resolve specifier"); .expect("could not resolve specifier");
let source = r#" let source = r#"
enum D { enum D {
@ -668,8 +666,7 @@ mod tests {
#[test] #[test]
fn test_transpile_tsx() { fn test_transpile_tsx() {
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/mod.ts")
.expect("could not resolve specifier"); .expect("could not resolve specifier");
let source = r#" let source = r#"
export class A { export class A {
@ -688,8 +685,7 @@ mod tests {
#[test] #[test]
fn test_transpile_decorators() { fn test_transpile_decorators() {
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/mod.ts")
.expect("could not resolve specifier"); .expect("could not resolve specifier");
let source = r#" let source = r#"
function enumerable(value: boolean) { function enumerable(value: boolean) {

View file

@ -52,11 +52,10 @@ impl AuthTokens {
/// matching is case insensitive. /// matching is case insensitive.
pub fn get(&self, specifier: &ModuleSpecifier) -> Option<AuthToken> { pub fn get(&self, specifier: &ModuleSpecifier) -> Option<AuthToken> {
self.0.iter().find_map(|t| { self.0.iter().find_map(|t| {
let url = specifier.as_url(); let hostname = if let Some(port) = specifier.port() {
let hostname = if let Some(port) = url.port() { format!("{}:{}", specifier.host_str()?, port)
format!("{}:{}", url.host_str()?, port)
} else { } else {
url.host_str()?.to_string() specifier.host_str()?.to_string()
}; };
if hostname.to_lowercase().ends_with(&t.host) { if hostname.to_lowercase().ends_with(&t.host) {
Some(t.clone()) Some(t.clone())
@ -70,31 +69,27 @@ impl AuthTokens {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use deno_core::resolve_url;
#[test] #[test]
fn test_auth_token() { fn test_auth_token() {
let auth_tokens = AuthTokens::new(Some("abc123@deno.land".to_string())); let auth_tokens = AuthTokens::new(Some("abc123@deno.land".to_string()));
let fixture = let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap();
assert_eq!( assert_eq!(
auth_tokens.get(&fixture).unwrap().to_string(), auth_tokens.get(&fixture).unwrap().to_string(),
"Bearer abc123" "Bearer abc123"
); );
let fixture = let fixture = resolve_url("https://www.deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://www.deno.land/x/mod.ts").unwrap();
assert_eq!( assert_eq!(
auth_tokens.get(&fixture).unwrap().to_string(), auth_tokens.get(&fixture).unwrap().to_string(),
"Bearer abc123".to_string() "Bearer abc123".to_string()
); );
let fixture = let fixture = resolve_url("http://127.0.0.1:8080/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("http://127.0.0.1:8080/x/mod.ts").unwrap();
assert_eq!(auth_tokens.get(&fixture), None); assert_eq!(auth_tokens.get(&fixture), None);
let fixture = let fixture =
ModuleSpecifier::resolve_url("https://deno.land.example.com/x/mod.ts") resolve_url("https://deno.land.example.com/x/mod.ts").unwrap();
.unwrap();
assert_eq!(auth_tokens.get(&fixture), None); assert_eq!(auth_tokens.get(&fixture), None);
let fixture = let fixture = resolve_url("https://deno.land:8080/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land:8080/x/mod.ts").unwrap();
assert_eq!(auth_tokens.get(&fixture), None); assert_eq!(auth_tokens.get(&fixture), None);
} }
@ -102,14 +97,12 @@ mod tests {
fn test_auth_tokens_multiple() { fn test_auth_tokens_multiple() {
let auth_tokens = let auth_tokens =
AuthTokens::new(Some("abc123@deno.land;def456@example.com".to_string())); AuthTokens::new(Some("abc123@deno.land;def456@example.com".to_string()));
let fixture = let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap();
assert_eq!( assert_eq!(
auth_tokens.get(&fixture).unwrap().to_string(), auth_tokens.get(&fixture).unwrap().to_string(),
"Bearer abc123".to_string() "Bearer abc123".to_string()
); );
let fixture = let fixture = resolve_url("http://example.com/a/file.ts").unwrap();
ModuleSpecifier::resolve_url("http://example.com/a/file.ts").unwrap();
assert_eq!( assert_eq!(
auth_tokens.get(&fixture).unwrap().to_string(), auth_tokens.get(&fixture).unwrap().to_string(),
"Bearer def456".to_string() "Bearer def456".to_string()
@ -120,11 +113,9 @@ mod tests {
fn test_auth_tokens_port() { fn test_auth_tokens_port() {
let auth_tokens = let auth_tokens =
AuthTokens::new(Some("abc123@deno.land:8080".to_string())); AuthTokens::new(Some("abc123@deno.land:8080".to_string()));
let fixture = let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap();
assert_eq!(auth_tokens.get(&fixture), None); assert_eq!(auth_tokens.get(&fixture), None);
let fixture = let fixture = resolve_url("http://deno.land:8080/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("http://deno.land:8080/x/mod.ts").unwrap();
assert_eq!( assert_eq!(
auth_tokens.get(&fixture).unwrap().to_string(), auth_tokens.get(&fixture).unwrap().to_string(),
"Bearer abc123".to_string() "Bearer abc123".to_string()
@ -134,8 +125,7 @@ mod tests {
#[test] #[test]
fn test_auth_tokens_contain_at() { fn test_auth_tokens_contain_at() {
let auth_tokens = AuthTokens::new(Some("abc@123@deno.land".to_string())); let auth_tokens = AuthTokens::new(Some("abc@123@deno.land".to_string()));
let fixture = let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap();
assert_eq!( assert_eq!(
auth_tokens.get(&fixture).unwrap().to_string(), auth_tokens.get(&fixture).unwrap().to_string(),
"Bearer abc@123".to_string() "Bearer abc@123".to_string()

View file

@ -94,7 +94,7 @@ impl CacheSetting {
CacheSetting::ReloadAll => false, CacheSetting::ReloadAll => false,
CacheSetting::Use | CacheSetting::Only => true, CacheSetting::Use | CacheSetting::Only => true,
CacheSetting::ReloadSome(list) => { CacheSetting::ReloadSome(list) => {
let mut url = specifier.as_url().clone(); let mut url = specifier.clone();
url.set_fragment(None); url.set_fragment(None);
if list.contains(&url.as_str().to_string()) { if list.contains(&url.as_str().to_string()) {
return false; return false;
@ -117,7 +117,7 @@ impl CacheSetting {
/// Fetch a source file from the local file system. /// Fetch a source file from the local file system.
fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> { fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> {
let local = specifier.as_url().to_file_path().map_err(|_| { let local = specifier.to_file_path().map_err(|_| {
uri_error(format!("Invalid file path.\n Specifier: {}", specifier)) uri_error(format!("Invalid file path.\n Specifier: {}", specifier))
})?; })?;
let bytes = fs::read(local.clone())?; let bytes = fs::read(local.clone())?;
@ -152,14 +152,13 @@ pub fn get_source_from_bytes(
fn get_source_from_data_url( fn get_source_from_data_url(
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
) -> Result<(String, MediaType, String), AnyError> { ) -> Result<(String, MediaType, String), AnyError> {
let url = specifier.as_url(); if specifier.scheme() != "data" {
if url.scheme() != "data" {
return Err(custom_error( return Err(custom_error(
"BadScheme", "BadScheme",
format!("Unexpected scheme of \"{}\"", url.scheme()), format!("Unexpected scheme of \"{}\"", specifier.scheme()),
)); ));
} }
let path = url.path(); let path = specifier.path();
let mut parts = path.splitn(2, ','); let mut parts = path.splitn(2, ',');
let media_type_part = let media_type_part =
percent_encoding::percent_decode_str(parts.next().unwrap()) percent_encoding::percent_decode_str(parts.next().unwrap())
@ -188,7 +187,7 @@ fn get_source_from_data_url(
fn get_validated_scheme( fn get_validated_scheme(
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
) -> Result<String, AnyError> { ) -> Result<String, AnyError> {
let scheme = specifier.as_url().scheme(); let scheme = specifier.scheme();
if !SUPPORTED_SCHEMES.contains(&scheme) { if !SUPPORTED_SCHEMES.contains(&scheme) {
Err(generic_error(format!( Err(generic_error(format!(
"Unsupported scheme \"{}\" for module \"{}\". Supported schemes: {:#?}", "Unsupported scheme \"{}\" for module \"{}\". Supported schemes: {:#?}",
@ -252,15 +251,14 @@ fn map_js_like_extension(
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
default: MediaType, default: MediaType,
) -> MediaType { ) -> MediaType {
let url = specifier.as_url(); let path = if specifier.scheme() == "file" {
let path = if url.scheme() == "file" { if let Ok(path) = specifier.to_file_path() {
if let Ok(path) = url.to_file_path() {
path path
} else { } else {
PathBuf::from(url.path()) PathBuf::from(specifier.path())
} }
} else { } else {
PathBuf::from(url.path()) PathBuf::from(specifier.path())
}; };
match path.extension() { match path.extension() {
None => default, None => default,
@ -344,9 +342,10 @@ impl FileFetcher {
bytes: Vec<u8>, bytes: Vec<u8>,
headers: &HashMap<String, String>, headers: &HashMap<String, String>,
) -> Result<File, AnyError> { ) -> Result<File, AnyError> {
let local = self let local =
self
.http_cache .http_cache
.get_cache_filename(specifier.as_url()) .get_cache_filename(specifier)
.ok_or_else(|| { .ok_or_else(|| {
generic_error("Cannot convert specifier to cached filename.") generic_error("Cannot convert specifier to cached filename.")
})?; })?;
@ -378,8 +377,7 @@ impl FileFetcher {
return Err(custom_error("Http", "Too many redirects.")); return Err(custom_error("Http", "Too many redirects."));
} }
let (mut source_file, headers) = let (mut source_file, headers) = match self.http_cache.get(specifier) {
match self.http_cache.get(specifier.as_url()) {
Err(err) => { Err(err) => {
if let Some(err) = err.downcast_ref::<std::io::Error>() { if let Some(err) = err.downcast_ref::<std::io::Error>() {
if err.kind() == std::io::ErrorKind::NotFound { if err.kind() == std::io::ErrorKind::NotFound {
@ -392,7 +390,7 @@ impl FileFetcher {
}; };
if let Some(redirect_to) = headers.get("location") { if let Some(redirect_to) = headers.get("location") {
let redirect = let redirect =
ModuleSpecifier::resolve_import(redirect_to, specifier.as_str())?; deno_core::resolve_import(redirect_to, specifier.as_str())?;
return self.fetch_cached(&redirect, redirect_limit - 1); return self.fetch_cached(&redirect, redirect_limit - 1);
} }
let mut bytes = Vec::new(); let mut bytes = Vec::new();
@ -427,17 +425,16 @@ impl FileFetcher {
let (source, media_type, content_type) = let (source, media_type, content_type) =
get_source_from_data_url(specifier)?; get_source_from_data_url(specifier)?;
let local = self let local =
self
.http_cache .http_cache
.get_cache_filename(specifier.as_url()) .get_cache_filename(specifier)
.ok_or_else(|| { .ok_or_else(|| {
generic_error("Cannot convert specifier to cached filename.") generic_error("Cannot convert specifier to cached filename.")
})?; })?;
let mut headers = HashMap::new(); let mut headers = HashMap::new();
headers.insert("content-type".to_string(), content_type); headers.insert("content-type".to_string(), content_type);
self self.http_cache.set(specifier, headers, source.as_bytes())?;
.http_cache
.set(specifier.as_url(), headers, source.as_bytes())?;
Ok(File { Ok(File {
local, local,
@ -494,7 +491,7 @@ impl FileFetcher {
info!("{} {}", colors::green("Download"), specifier); info!("{} {}", colors::green("Download"), specifier);
let maybe_etag = match self.http_cache.get(specifier.as_url()) { let maybe_etag = match self.http_cache.get(specifier) {
Ok((_, headers)) => headers.get("etag").cloned(), Ok((_, headers)) => headers.get("etag").cloned(),
_ => None, _ => None,
}; };
@ -507,7 +504,7 @@ impl FileFetcher {
async move { async move {
match fetch_once(FetchOnceArgs { match fetch_once(FetchOnceArgs {
client, client,
url: specifier.as_url().clone(), url: specifier.clone(),
maybe_etag, maybe_etag,
maybe_auth_token, maybe_auth_token,
}) })
@ -518,20 +515,15 @@ impl FileFetcher {
Ok(file) Ok(file)
} }
FetchOnceResult::Redirect(redirect_url, headers) => { FetchOnceResult::Redirect(redirect_url, headers) => {
file_fetcher.http_cache.set(&specifier, headers, &[])?;
file_fetcher file_fetcher
.http_cache .fetch_remote(&redirect_url, &permissions, redirect_limit - 1)
.set(specifier.as_url(), headers, &[])?;
let redirect_specifier = ModuleSpecifier::from(redirect_url);
file_fetcher
.fetch_remote(&redirect_specifier, &permissions, redirect_limit - 1)
.await .await
} }
FetchOnceResult::Code(bytes, headers) => { FetchOnceResult::Code(bytes, headers) => {
file_fetcher.http_cache.set( file_fetcher
specifier.as_url(), .http_cache
headers.clone(), .set(&specifier, headers.clone(), &bytes)?;
&bytes,
)?;
let file = let file =
file_fetcher.build_remote_file(&specifier, bytes, &headers)?; file_fetcher.build_remote_file(&specifier, bytes, &headers)?;
Ok(file) Ok(file)
@ -587,7 +579,7 @@ impl FileFetcher {
pub fn get_source(&self, specifier: &ModuleSpecifier) -> Option<File> { pub fn get_source(&self, specifier: &ModuleSpecifier) -> Option<File> {
let maybe_file = self.cache.get(specifier); let maybe_file = self.cache.get(specifier);
if maybe_file.is_none() { if maybe_file.is_none() {
let is_local = specifier.as_url().scheme() == "file"; let is_local = specifier.scheme() == "file";
if is_local { if is_local {
if let Ok(file) = fetch_local(specifier) { if let Ok(file) = fetch_local(specifier) {
return Some(file); return Some(file);
@ -609,6 +601,8 @@ impl FileFetcher {
mod tests { mod tests {
use super::*; use super::*;
use deno_core::error::get_custom_error_class; use deno_core::error::get_custom_error_class;
use deno_core::resolve_url;
use deno_core::resolve_url_or_path;
use std::rc::Rc; use std::rc::Rc;
use tempfile::TempDir; use tempfile::TempDir;
@ -654,7 +648,7 @@ mod tests {
.fetch_remote(specifier, &Permissions::allow_all(), 1) .fetch_remote(specifier, &Permissions::allow_all(), 1)
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let (_, headers) = file_fetcher.http_cache.get(specifier.as_url()).unwrap(); let (_, headers) = file_fetcher.http_cache.get(specifier).unwrap();
(result.unwrap(), headers) (result.unwrap(), headers)
} }
@ -665,7 +659,7 @@ mod tests {
) { ) {
let url_str = let url_str =
format!("http://127.0.0.1:4545/cli/tests/encoding/{}", fixture); format!("http://127.0.0.1:4545/cli/tests/encoding/{}", fixture);
let specifier = ModuleSpecifier::resolve_url(&url_str).unwrap(); let specifier = resolve_url(&url_str).unwrap();
let (file, headers) = test_fetch_remote(&specifier).await; let (file, headers) = test_fetch_remote(&specifier).await;
assert_eq!(file.source, expected); assert_eq!(file.source, expected);
assert_eq!(file.media_type, MediaType::TypeScript); assert_eq!(file.media_type, MediaType::TypeScript);
@ -678,8 +672,7 @@ mod tests {
async fn test_fetch_local_encoded(charset: &str, expected: String) { async fn test_fetch_local_encoded(charset: &str, expected: String) {
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join(format!("tests/encoding/{}.ts", charset)); .join(format!("tests/encoding/{}.ts", charset));
let specifier = let specifier = resolve_url_or_path(p.to_str().unwrap()).unwrap();
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
let (file, _) = test_fetch(&specifier).await; let (file, _) = test_fetch(&specifier).await;
assert_eq!(file.source, expected); assert_eq!(file.source, expected);
} }
@ -704,7 +697,7 @@ mod tests {
expected, expected,
) in fixtures ) in fixtures
{ {
let specifier = ModuleSpecifier::resolve_url(url_str).unwrap(); let specifier = resolve_url(url_str).unwrap();
let actual = get_source_from_data_url(&specifier); let actual = get_source_from_data_url(&specifier);
assert_eq!(actual.is_ok(), expected_ok); assert_eq!(actual.is_ok(), expected_ok);
if expected_ok { if expected_ok {
@ -730,7 +723,7 @@ mod tests {
]; ];
for (specifier, is_ok, expected) in fixtures { for (specifier, is_ok, expected) in fixtures {
let specifier = ModuleSpecifier::resolve_url_or_path(specifier).unwrap(); let specifier = resolve_url_or_path(specifier).unwrap();
let actual = get_validated_scheme(&specifier); let actual = get_validated_scheme(&specifier);
assert_eq!(actual.is_ok(), is_ok); assert_eq!(actual.is_ok(), is_ok);
if is_ok { if is_ok {
@ -909,7 +902,7 @@ mod tests {
]; ];
for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures { for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures {
let specifier = ModuleSpecifier::resolve_url_or_path(specifier).unwrap(); let specifier = resolve_url_or_path(specifier).unwrap();
assert_eq!( assert_eq!(
map_content_type(&specifier, maybe_content_type), map_content_type(&specifier, maybe_content_type),
(media_type, maybe_charset) (media_type, maybe_charset)
@ -922,8 +915,7 @@ mod tests {
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None); let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
let local = temp_dir.path().join("a.ts"); let local = temp_dir.path().join("a.ts");
let specifier = let specifier =
ModuleSpecifier::resolve_url_or_path(local.as_os_str().to_str().unwrap()) resolve_url_or_path(local.as_os_str().to_str().unwrap()).unwrap();
.unwrap();
let file = File { let file = File {
local, local,
maybe_types: None, maybe_types: None,
@ -945,7 +937,7 @@ mod tests {
async fn test_get_source() { async fn test_get_source() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (file_fetcher, _) = setup(CacheSetting::Use, None); let (file_fetcher, _) = setup(CacheSetting::Use, None);
let specifier = ModuleSpecifier::resolve_url( let specifier = resolve_url(
"http://localhost:4548/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4548/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
@ -961,7 +953,7 @@ mod tests {
assert_eq!(file.source, "export const redirect = 1;\n"); assert_eq!(file.source, "export const redirect = 1;\n");
assert_eq!( assert_eq!(
file.specifier, file.specifier,
ModuleSpecifier::resolve_url( resolve_url(
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js" "http://localhost:4545/cli/tests/subdir/redirects/redirect1.js"
) )
.unwrap() .unwrap()
@ -979,7 +971,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_fetch_data_url() { async fn test_fetch_data_url() {
let (file_fetcher, _) = setup(CacheSetting::Use, None); let (file_fetcher, _) = setup(CacheSetting::Use, None);
let specifier = ModuleSpecifier::resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap(); let specifier = resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap();
let result = file_fetcher let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all()) .fetch(&specifier, &Permissions::allow_all())
@ -1001,9 +993,8 @@ mod tests {
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None); let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
let (file_fetcher_01, _) = setup(CacheSetting::Use, Some(temp_dir.clone())); let (file_fetcher_01, _) = setup(CacheSetting::Use, Some(temp_dir.clone()));
let (file_fetcher_02, _) = setup(CacheSetting::Use, Some(temp_dir.clone())); let (file_fetcher_02, _) = setup(CacheSetting::Use, Some(temp_dir.clone()));
let specifier = ModuleSpecifier::resolve_url_or_path( let specifier =
"http://localhost:4545/cli/tests/subdir/mod2.ts", resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
)
.unwrap(); .unwrap();
let result = file_fetcher let result = file_fetcher
@ -1019,7 +1010,7 @@ mod tests {
let cache_filename = file_fetcher let cache_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(specifier.as_url()) .get_cache_filename(&specifier)
.unwrap(); .unwrap();
let mut metadata = let mut metadata =
crate::http_cache::Metadata::read(&cache_filename).unwrap(); crate::http_cache::Metadata::read(&cache_filename).unwrap();
@ -1042,8 +1033,7 @@ mod tests {
// the value above. // the value above.
assert_eq!(file.media_type, MediaType::JavaScript); assert_eq!(file.media_type, MediaType::JavaScript);
let (_, headers) = let (_, headers) = file_fetcher_02.http_cache.get(&specifier).unwrap();
file_fetcher_02.http_cache.get(specifier.as_url()).unwrap();
assert_eq!(headers.get("content-type").unwrap(), "text/javascript"); assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
metadata.headers = HashMap::new(); metadata.headers = HashMap::new();
metadata metadata
@ -1098,13 +1088,12 @@ mod tests {
None, None,
) )
.expect("could not create file fetcher"); .expect("could not create file fetcher");
let specifier = ModuleSpecifier::resolve_url( let specifier =
"http://localhost:4545/cli/tests/subdir/mismatch_ext.ts", resolve_url("http://localhost:4545/cli/tests/subdir/mismatch_ext.ts")
)
.unwrap(); .unwrap();
let cache_filename = file_fetcher_01 let cache_filename = file_fetcher_01
.http_cache .http_cache
.get_cache_filename(specifier.as_url()) .get_cache_filename(&specifier)
.unwrap(); .unwrap();
let result = file_fetcher_01 let result = file_fetcher_01
@ -1148,21 +1137,21 @@ mod tests {
async fn test_fetch_redirected() { async fn test_fetch_redirected() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (file_fetcher, _) = setup(CacheSetting::Use, None); let (file_fetcher, _) = setup(CacheSetting::Use, None);
let specifier = ModuleSpecifier::resolve_url( let specifier = resolve_url(
"http://localhost:4546/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4546/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
let cached_filename = file_fetcher let cached_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(specifier.as_url()) .get_cache_filename(&specifier)
.unwrap(); .unwrap();
let redirected_specifier = ModuleSpecifier::resolve_url( let redirected_specifier = resolve_url(
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4545/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
let redirected_cached_filename = file_fetcher let redirected_cached_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(redirected_specifier.as_url()) .get_cache_filename(&redirected_specifier)
.unwrap(); .unwrap();
let result = file_fetcher let result = file_fetcher
@ -1179,7 +1168,7 @@ mod tests {
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(specifier.as_url()) .get(&specifier)
.expect("could not get file"); .expect("could not get file");
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
@ -1192,7 +1181,7 @@ mod tests {
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(redirected_specifier.as_url()) .get(&redirected_specifier)
.expect("could not get file"); .expect("could not get file");
assert!(headers.get("location").is_none()); assert!(headers.get("location").is_none());
} }
@ -1201,29 +1190,29 @@ mod tests {
async fn test_fetch_multiple_redirects() { async fn test_fetch_multiple_redirects() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (file_fetcher, _) = setup(CacheSetting::Use, None); let (file_fetcher, _) = setup(CacheSetting::Use, None);
let specifier = ModuleSpecifier::resolve_url( let specifier = resolve_url(
"http://localhost:4548/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4548/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
let cached_filename = file_fetcher let cached_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(specifier.as_url()) .get_cache_filename(&specifier)
.unwrap(); .unwrap();
let redirected_01_specifier = ModuleSpecifier::resolve_url( let redirected_01_specifier = resolve_url(
"http://localhost:4546/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4546/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
let redirected_01_cached_filename = file_fetcher let redirected_01_cached_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(redirected_01_specifier.as_url()) .get_cache_filename(&redirected_01_specifier)
.unwrap(); .unwrap();
let redirected_02_specifier = ModuleSpecifier::resolve_url( let redirected_02_specifier = resolve_url(
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4545/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
let redirected_02_cached_filename = file_fetcher let redirected_02_cached_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(redirected_02_specifier.as_url()) .get_cache_filename(&redirected_02_specifier)
.unwrap(); .unwrap();
let result = file_fetcher let result = file_fetcher
@ -1240,7 +1229,7 @@ mod tests {
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(specifier.as_url()) .get(&specifier)
.expect("could not get file"); .expect("could not get file");
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
@ -1254,7 +1243,7 @@ mod tests {
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(redirected_01_specifier.as_url()) .get(&redirected_01_specifier)
.expect("could not get file"); .expect("could not get file");
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
@ -1267,7 +1256,7 @@ mod tests {
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(redirected_02_specifier.as_url()) .get(&redirected_02_specifier)
.expect("could not get file"); .expect("could not get file");
assert!(headers.get("location").is_none()); assert!(headers.get("location").is_none());
} }
@ -1286,17 +1275,15 @@ mod tests {
None, None,
) )
.expect("could not create file fetcher"); .expect("could not create file fetcher");
let specifier = ModuleSpecifier::resolve_url( let specifier =
"http://localhost:4548/cli/tests/subdir/mismatch_ext.ts", resolve_url("http://localhost:4548/cli/tests/subdir/mismatch_ext.ts")
)
.unwrap(); .unwrap();
let redirected_specifier = ModuleSpecifier::resolve_url( let redirected_specifier =
"http://localhost:4546/cli/tests/subdir/mismatch_ext.ts", resolve_url("http://localhost:4546/cli/tests/subdir/mismatch_ext.ts")
)
.unwrap(); .unwrap();
let redirected_cache_filename = file_fetcher_01 let redirected_cache_filename = file_fetcher_01
.http_cache .http_cache
.get_cache_filename(redirected_specifier.as_url()) .get_cache_filename(&redirected_specifier)
.unwrap(); .unwrap();
let result = file_fetcher_01 let result = file_fetcher_01
@ -1340,7 +1327,7 @@ mod tests {
async fn test_fetcher_limits_redirects() { async fn test_fetcher_limits_redirects() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (file_fetcher, _) = setup(CacheSetting::Use, None); let (file_fetcher, _) = setup(CacheSetting::Use, None);
let specifier = ModuleSpecifier::resolve_url( let specifier = resolve_url(
"http://localhost:4548/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4548/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
@ -1366,21 +1353,21 @@ mod tests {
async fn test_fetch_same_host_redirect() { async fn test_fetch_same_host_redirect() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (file_fetcher, _) = setup(CacheSetting::Use, None); let (file_fetcher, _) = setup(CacheSetting::Use, None);
let specifier = ModuleSpecifier::resolve_url( let specifier = resolve_url(
"http://localhost:4550/REDIRECT/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4550/REDIRECT/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
let cached_filename = file_fetcher let cached_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(specifier.as_url()) .get_cache_filename(&specifier)
.unwrap(); .unwrap();
let redirected_specifier = ModuleSpecifier::resolve_url( let redirected_specifier = resolve_url(
"http://localhost:4550/cli/tests/subdir/redirects/redirect1.js", "http://localhost:4550/cli/tests/subdir/redirects/redirect1.js",
) )
.unwrap(); .unwrap();
let redirected_cached_filename = file_fetcher let redirected_cached_filename = file_fetcher
.http_cache .http_cache
.get_cache_filename(redirected_specifier.as_url()) .get_cache_filename(&redirected_specifier)
.unwrap(); .unwrap();
let result = file_fetcher let result = file_fetcher
@ -1397,7 +1384,7 @@ mod tests {
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(specifier.as_url()) .get(&specifier)
.expect("could not get file"); .expect("could not get file");
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
@ -1410,7 +1397,7 @@ mod tests {
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(redirected_specifier.as_url()) .get(&redirected_specifier)
.expect("could not get file"); .expect("could not get file");
assert!(headers.get("location").is_none()); assert!(headers.get("location").is_none());
} }
@ -1427,10 +1414,8 @@ mod tests {
None, None,
) )
.expect("could not create file fetcher"); .expect("could not create file fetcher");
let specifier = ModuleSpecifier::resolve_url( let specifier =
"http://localhost:4545/cli/tests/002_hello.ts", resolve_url("http://localhost:4545/cli/tests/002_hello.ts").unwrap();
)
.unwrap();
let result = file_fetcher let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all()) .fetch(&specifier, &Permissions::allow_all())
@ -1462,10 +1447,8 @@ mod tests {
None, None,
) )
.expect("could not create file fetcher"); .expect("could not create file fetcher");
let specifier = ModuleSpecifier::resolve_url( let specifier =
"http://localhost:4545/cli/tests/002_hello.ts", resolve_url("http://localhost:4545/cli/tests/002_hello.ts").unwrap();
)
.unwrap();
let result = file_fetcher_01 let result = file_fetcher_01
.fetch(&specifier, &Permissions::allow_all()) .fetch(&specifier, &Permissions::allow_all())
@ -1495,8 +1478,7 @@ mod tests {
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None); let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
let fixture_path = temp_dir.path().join("mod.ts"); let fixture_path = temp_dir.path().join("mod.ts");
let specifier = let specifier =
ModuleSpecifier::resolve_url_or_path(&fixture_path.to_string_lossy()) resolve_url_or_path(&fixture_path.to_string_lossy()).unwrap();
.unwrap();
fs::write(fixture_path.clone(), r#"console.log("hello deno");"#) fs::write(fixture_path.clone(), r#"console.log("hello deno");"#)
.expect("could not write file"); .expect("could not write file");
let result = file_fetcher let result = file_fetcher
@ -1545,10 +1527,8 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_fetch_remote_with_types() { async fn test_fetch_remote_with_types() {
let specifier = ModuleSpecifier::resolve_url_or_path( let specifier =
"http://127.0.0.1:4545/xTypeScriptTypes.js", resolve_url_or_path("http://127.0.0.1:4545/xTypeScriptTypes.js").unwrap();
)
.unwrap();
let (file, _) = test_fetch_remote(&specifier).await; let (file, _) = test_fetch_remote(&specifier).await;
assert_eq!( assert_eq!(
file.maybe_types, file.maybe_types,

View file

@ -172,7 +172,7 @@ impl ImportMap {
continue; continue;
} }
normalized_addresses.push(url.into()); normalized_addresses.push(url);
} }
normalized_addresses normalized_addresses
@ -367,13 +367,13 @@ impl ImportMap {
if address_vec.is_empty() { if address_vec.is_empty() {
return Err(ImportMapError::new(&format!("Specifier {:?} was mapped to no addresses (via prefix specifier key {:?}).", normalized_specifier, specifier_key))); return Err(ImportMapError::new(&format!("Specifier {:?} was mapped to no addresses (via prefix specifier key {:?}).", normalized_specifier, specifier_key)));
} else if address_vec.len() == 1 { } else if address_vec.len() == 1 {
let address = address_vec.first().unwrap(); let base_url = address_vec.first().unwrap();
let after_prefix = &normalized_specifier[specifier_key.len()..]; let after_prefix = &normalized_specifier[specifier_key.len()..];
let base_url = address.as_url();
if let Ok(url) = base_url.join(after_prefix) { if let Ok(url) = base_url.join(after_prefix) {
debug!("Specifier {:?} was mapped to {:?} (via prefix specifier key {:?}).", normalized_specifier, url, address); debug!("Specifier {:?} was mapped to {:?} (via prefix specifier key {:?}).",
return Ok(Some(ModuleSpecifier::from(url))); normalized_specifier, url, base_url);
return Ok(Some(url));
} }
unreachable!(); unreachable!();
@ -434,7 +434,7 @@ impl ImportMap {
// no match in import map but we got resolvable URL // no match in import map but we got resolvable URL
if let Some(resolved_url) = resolved_url { if let Some(resolved_url) = resolved_url {
return Ok(Some(ModuleSpecifier::from(resolved_url))); return Ok(Some(resolved_url));
} }
Err(ImportMapError::new(&format!( Err(ImportMapError::new(&format!(
@ -513,18 +513,18 @@ mod tests {
.imports .imports
.get("https://base.example/path1/path2/foo") .get("https://base.example/path1/path2/foo")
.unwrap()[0], .unwrap()[0],
"https://base.example/dotslash".to_string() Url::parse("https://base.example/dotslash").unwrap()
); );
assert_eq!( assert_eq!(
import_map import_map
.imports .imports
.get("https://base.example/path1/foo") .get("https://base.example/path1/foo")
.unwrap()[0], .unwrap()[0],
"https://base.example/dotdotslash".to_string() Url::parse("https://base.example/dotdotslash").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://base.example/foo").unwrap()[0], import_map.imports.get("https://base.example/foo").unwrap()[0],
"https://base.example/slash".to_string() Url::parse("https://base.example/slash").unwrap()
); );
// Should absolutize the literal strings ./, ../, or / with no suffix.. // Should absolutize the literal strings ./, ../, or / with no suffix..
@ -543,18 +543,18 @@ mod tests {
.imports .imports
.get("https://base.example/path1/path2/") .get("https://base.example/path1/path2/")
.unwrap()[0], .unwrap()[0],
"https://base.example/dotslash/".to_string() Url::parse("https://base.example/dotslash/").unwrap()
); );
assert_eq!( assert_eq!(
import_map import_map
.imports .imports
.get("https://base.example/path1/") .get("https://base.example/path1/")
.unwrap()[0], .unwrap()[0],
"https://base.example/dotdotslash/".to_string() Url::parse("https://base.example/dotdotslash/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://base.example/").unwrap()[0], import_map.imports.get("https://base.example/").unwrap()[0],
"https://base.example/slash/".to_string() Url::parse("https://base.example/slash/").unwrap()
); );
// Should treat percent-encoded variants of ./, ../, or / as bare specifiers.. // Should treat percent-encoded variants of ./, ../, or / as bare specifiers..
@ -574,31 +574,31 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
import_map.imports.get("%2E/").unwrap()[0], import_map.imports.get("%2E/").unwrap()[0],
"https://base.example/dotSlash1/".to_string() Url::parse("https://base.example/dotSlash1/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("%2E%2E/").unwrap()[0], import_map.imports.get("%2E%2E/").unwrap()[0],
"https://base.example/dotDotSlash1/".to_string() Url::parse("https://base.example/dotDotSlash1/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get(".%2F").unwrap()[0], import_map.imports.get(".%2F").unwrap()[0],
"https://base.example/dotSlash2".to_string() Url::parse("https://base.example/dotSlash2").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("..%2F").unwrap()[0], import_map.imports.get("..%2F").unwrap()[0],
"https://base.example/dotDotSlash2".to_string() Url::parse("https://base.example/dotDotSlash2").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("%2F").unwrap()[0], import_map.imports.get("%2F").unwrap()[0],
"https://base.example/slash2".to_string() Url::parse("https://base.example/slash2").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("%2E%2F").unwrap()[0], import_map.imports.get("%2E%2F").unwrap()[0],
"https://base.example/dotSlash3".to_string() Url::parse("https://base.example/dotSlash3").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("%2E%2E%2F").unwrap()[0], import_map.imports.get("%2E%2E%2F").unwrap()[0],
"https://base.example/dotDotSlash3".to_string() Url::parse("https://base.example/dotDotSlash3").unwrap()
); );
} }
@ -627,47 +627,47 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
import_map.imports.get("http://good/").unwrap()[0], import_map.imports.get("http://good/").unwrap()[0],
"https://base.example/http/".to_string() Url::parse("https://base.example/http/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://good/").unwrap()[0], import_map.imports.get("https://good/").unwrap()[0],
"https://base.example/https/".to_string() Url::parse("https://base.example/https/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("file:///good").unwrap()[0], import_map.imports.get("file:///good").unwrap()[0],
"https://base.example/file".to_string() Url::parse("https://base.example/file").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("http://good/").unwrap()[0], import_map.imports.get("http://good/").unwrap()[0],
"https://base.example/http/".to_string() Url::parse("https://base.example/http/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("import:bad").unwrap()[0], import_map.imports.get("import:bad").unwrap()[0],
"https://base.example/import".to_string() Url::parse("https://base.example/import").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("mailto:bad").unwrap()[0], import_map.imports.get("mailto:bad").unwrap()[0],
"https://base.example/mailto".to_string() Url::parse("https://base.example/mailto").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("javascript:bad").unwrap()[0], import_map.imports.get("javascript:bad").unwrap()[0],
"https://base.example/javascript".to_string() Url::parse("https://base.example/javascript").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("wss:bad").unwrap()[0], import_map.imports.get("wss:bad").unwrap()[0],
"https://base.example/wss".to_string() Url::parse("https://base.example/wss").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("about:bad").unwrap()[0], import_map.imports.get("about:bad").unwrap()[0],
"https://base.example/about".to_string() Url::parse("https://base.example/about").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("blob:bad").unwrap()[0], import_map.imports.get("blob:bad").unwrap()[0],
"https://base.example/blob".to_string() Url::parse("https://base.example/blob").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("data:bad").unwrap()[0], import_map.imports.get("data:bad").unwrap()[0],
"https://base.example/data".to_string() Url::parse("https://base.example/data").unwrap()
); );
// Should parse absolute URLs, treating unparseable ones as bare specifiers.. // Should parse absolute URLs, treating unparseable ones as bare specifiers..
@ -688,35 +688,35 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
import_map.imports.get("https://ex ample.org/").unwrap()[0], import_map.imports.get("https://ex ample.org/").unwrap()[0],
"https://base.example/unparseable1/".to_string() Url::parse("https://base.example/unparseable1/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://example.com:demo").unwrap()[0], import_map.imports.get("https://example.com:demo").unwrap()[0],
"https://base.example/unparseable2".to_string() Url::parse("https://base.example/unparseable2").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("http://[www.example.com]/").unwrap()[0], import_map.imports.get("http://[www.example.com]/").unwrap()[0],
"https://base.example/unparseable3/".to_string() Url::parse("https://base.example/unparseable3/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://example.org/").unwrap()[0], import_map.imports.get("https://example.org/").unwrap()[0],
"https://base.example/invalidButParseable1/".to_string() Url::parse("https://base.example/invalidButParseable1/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://example.com///").unwrap()[0], import_map.imports.get("https://example.com///").unwrap()[0],
"https://base.example/invalidButParseable2/".to_string() Url::parse("https://base.example/invalidButParseable2/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://example.net/").unwrap()[0], import_map.imports.get("https://example.net/").unwrap()[0],
"https://base.example/prettyNormal/".to_string() Url::parse("https://base.example/prettyNormal/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://example.com/").unwrap()[0], import_map.imports.get("https://example.com/").unwrap()[0],
"https://base.example/percentDecoding/".to_string() Url::parse("https://base.example/percentDecoding/").unwrap()
); );
assert_eq!( assert_eq!(
import_map.imports.get("https://example.com/%41").unwrap()[0], import_map.imports.get("https://example.com/%41").unwrap()[0],
"https://base.example/noPercentDecoding".to_string() Url::parse("https://base.example/noPercentDecoding").unwrap()
); );
} }
@ -893,15 +893,15 @@ mod tests {
assert_eq!( assert_eq!(
import_map.imports.get("dotSlash").unwrap(), import_map.imports.get("dotSlash").unwrap(),
&vec!["https://base.example/path1/path2/foo".to_string()] &vec![Url::parse("https://base.example/path1/path2/foo").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("dotDotSlash").unwrap(), import_map.imports.get("dotDotSlash").unwrap(),
&vec!["https://base.example/path1/foo".to_string()] &vec![Url::parse("https://base.example/path1/foo").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("slash").unwrap(), import_map.imports.get("slash").unwrap(),
&vec!["https://base.example/foo".to_string()] &vec![Url::parse("https://base.example/foo").unwrap()]
); );
// Should accept the literal strings ./, ../, or / with no suffix.. // Should accept the literal strings ./, ../, or / with no suffix..
@ -918,15 +918,15 @@ mod tests {
assert_eq!( assert_eq!(
import_map.imports.get("dotSlash").unwrap(), import_map.imports.get("dotSlash").unwrap(),
&vec!["https://base.example/path1/path2/".to_string()] &vec![Url::parse("https://base.example/path1/path2/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("dotDotSlash").unwrap(), import_map.imports.get("dotDotSlash").unwrap(),
&vec!["https://base.example/path1/".to_string()] &vec![Url::parse("https://base.example/path1/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("slash").unwrap(), import_map.imports.get("slash").unwrap(),
&vec!["https://base.example/".to_string()] &vec![Url::parse("https://base.example/").unwrap()]
); );
// Should ignore percent-encoded variants of ./, ../, or /.. // Should ignore percent-encoded variants of ./, ../, or /..
@ -979,19 +979,19 @@ mod tests {
assert_eq!( assert_eq!(
import_map.imports.get("file").unwrap(), import_map.imports.get("file").unwrap(),
&vec!["file:///good".to_string()] &vec![Url::parse("file:///good").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("http").unwrap(), import_map.imports.get("http").unwrap(),
&vec!["http://good/".to_string()] &vec![Url::parse("http://good/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("https").unwrap(), import_map.imports.get("https").unwrap(),
&vec!["https://good/".to_string()] &vec![Url::parse("https://good/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("data").unwrap(), import_map.imports.get("data").unwrap(),
&vec!["data:good".to_string()] &vec![Url::parse("data:good").unwrap()]
); );
assert!(import_map.imports.get("about").unwrap().is_empty()); assert!(import_map.imports.get("about").unwrap().is_empty());
@ -1029,19 +1029,19 @@ mod tests {
assert_eq!( assert_eq!(
import_map.imports.get("file").unwrap(), import_map.imports.get("file").unwrap(),
&vec!["file:///good".to_string()] &vec![Url::parse("file:///good").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("http").unwrap(), import_map.imports.get("http").unwrap(),
&vec!["http://good/".to_string()] &vec![Url::parse("http://good/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("https").unwrap(), import_map.imports.get("https").unwrap(),
&vec!["https://good/".to_string()] &vec![Url::parse("https://good/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("data").unwrap(), import_map.imports.get("data").unwrap(),
&vec!["data:good".to_string()] &vec![Url::parse("data:good").unwrap()]
); );
assert!(import_map.imports.get("about").unwrap().is_empty()); assert!(import_map.imports.get("about").unwrap().is_empty());
@ -1075,23 +1075,23 @@ mod tests {
assert_eq!( assert_eq!(
import_map.imports.get("invalidButParseable1").unwrap(), import_map.imports.get("invalidButParseable1").unwrap(),
&vec!["https://example.org/".to_string()] &vec![Url::parse("https://example.org/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("invalidButParseable2").unwrap(), import_map.imports.get("invalidButParseable2").unwrap(),
&vec!["https://example.com///".to_string()] &vec![Url::parse("https://example.com///").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("prettyNormal").unwrap(), import_map.imports.get("prettyNormal").unwrap(),
&vec!["https://example.net/".to_string()] &vec![Url::parse("https://example.net/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("percentDecoding").unwrap(), import_map.imports.get("percentDecoding").unwrap(),
&vec!["https://example.com/".to_string()] &vec![Url::parse("https://example.com/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("noPercentDecoding").unwrap(), import_map.imports.get("noPercentDecoding").unwrap(),
&vec!["https://example.com/%41".to_string()] &vec![Url::parse("https://example.com/%41").unwrap()]
); );
assert!(import_map.imports.get("unparseable1").unwrap().is_empty()); assert!(import_map.imports.get("unparseable1").unwrap().is_empty());
@ -1120,23 +1120,23 @@ mod tests {
assert_eq!( assert_eq!(
import_map.imports.get("invalidButParseable1").unwrap(), import_map.imports.get("invalidButParseable1").unwrap(),
&vec!["https://example.org/".to_string()] &vec![Url::parse("https://example.org/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("invalidButParseable2").unwrap(), import_map.imports.get("invalidButParseable2").unwrap(),
&vec!["https://example.com///".to_string()] &vec![Url::parse("https://example.com///").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("prettyNormal").unwrap(), import_map.imports.get("prettyNormal").unwrap(),
&vec!["https://example.net/".to_string()] &vec![Url::parse("https://example.net/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("percentDecoding").unwrap(), import_map.imports.get("percentDecoding").unwrap(),
&vec!["https://example.com/".to_string()] &vec![Url::parse("https://example.com/").unwrap()]
); );
assert_eq!( assert_eq!(
import_map.imports.get("noPercentDecoding").unwrap(), import_map.imports.get("noPercentDecoding").unwrap(),
&vec!["https://example.com/%41".to_string()] &vec![Url::parse("https://example.com/%41").unwrap()]
); );
assert!(import_map.imports.get("unparseable1").unwrap().is_empty()); assert!(import_map.imports.get("unparseable1").unwrap().is_empty());
@ -1190,7 +1190,7 @@ mod tests {
assert_eq!( assert_eq!(
import_map.imports.get("trailer/").unwrap(), import_map.imports.get("trailer/").unwrap(),
&vec!["https://base.example/atrailer/".to_string()] &vec![Url::parse("https://base.example/atrailer/").unwrap()]
); );
// TODO: I'd be good to assert that warning was shown // TODO: I'd be good to assert that warning was shown
} }
@ -1230,7 +1230,7 @@ mod tests {
.unwrap_or_else(|err| panic!("ImportMap::resolve failed: {:?}", err)); .unwrap_or_else(|err| panic!("ImportMap::resolve failed: {:?}", err));
let resolved_url = let resolved_url =
maybe_url.unwrap_or_else(|| panic!("Unexpected None resolved URL")); maybe_url.unwrap_or_else(|| panic!("Unexpected None resolved URL"));
assert_eq!(resolved_url, expected_url.to_string()); assert_eq!(resolved_url.as_str(), expected_url);
} }
#[test] #[test]

View file

@ -202,6 +202,7 @@ pub fn human_size(size: f64) -> String {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use deno_core::resolve_url_or_path;
use deno_core::serde_json::json; use deno_core::serde_json::json;
#[test] #[test]
@ -218,12 +219,8 @@ mod test {
} }
fn get_fixture() -> ModuleGraphInfo { fn get_fixture() -> ModuleGraphInfo {
let spec_c = let spec_c = resolve_url_or_path("https://deno.land/x/a/b/c.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/a/b/c.ts") let spec_d = resolve_url_or_path("https://deno.land/x/a/b/c.ts").unwrap();
.unwrap();
let spec_d =
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/a/b/c.ts")
.unwrap();
let deps = vec![ModuleInfo { let deps = vec![ModuleInfo {
deps: Vec::new(), deps: Vec::new(),
name: spec_d.clone(), name: spec_d.clone(),
@ -261,10 +258,7 @@ mod test {
info, info,
local: PathBuf::from("/a/b/c.ts"), local: PathBuf::from("/a/b/c.ts"),
map: None, map: None,
module: ModuleSpecifier::resolve_url_or_path( module: resolve_url_or_path("https://deno.land/x/a/b/c.ts").unwrap(),
"https://deno.land/x/a/b/c.ts",
)
.unwrap(),
total_size: 999999, total_size: 999999,
} }
} }

View file

@ -211,7 +211,7 @@ pub fn resolve_import(
let specifier = if let Some(remapped) = maybe_mapped { let specifier = if let Some(remapped) = maybe_mapped {
remapped remapped
} else { } else {
match ModuleSpecifier::resolve_import(specifier, referrer.as_str()) { match deno_core::resolve_import(specifier, referrer.as_str()) {
Ok(resolved) => resolved, Ok(resolved) => resolved,
Err(err) => { Err(err) => {
return ResolvedDependency::Err( return ResolvedDependency::Err(
@ -220,8 +220,8 @@ pub fn resolve_import(
} }
} }
}; };
let referrer_scheme = referrer.as_url().scheme(); let referrer_scheme = referrer.scheme();
let specifier_scheme = specifier.as_url().scheme(); let specifier_scheme = specifier.scheme();
if referrer_scheme == "https" && specifier_scheme == "http" { if referrer_scheme == "https" && specifier_scheme == "http" {
return ResolvedDependency::Err(ResolvedDependencyErr::InvalidDowngrade); return ResolvedDependency::Err(ResolvedDependencyErr::InvalidDowngrade);
} }
@ -647,6 +647,7 @@ impl CodeActionCollection {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use deno_core::resolve_url;
#[test] #[test]
fn test_as_lsp_range() { fn test_as_lsp_range() {
@ -680,8 +681,7 @@ mod tests {
#[test] #[test]
fn test_analyze_dependencies() { fn test_analyze_dependencies() {
let specifier = let specifier = resolve_url("file:///a.ts").expect("bad specifier");
ModuleSpecifier::resolve_url("file:///a.ts").expect("bad specifier");
let source = r#"import { let source = r#"import {
Application, Application,
Context, Context,
@ -703,14 +703,10 @@ mod tests {
Some(Dependency { Some(Dependency {
is_dynamic: false, is_dynamic: false,
maybe_code: Some(ResolvedDependency::Resolved( maybe_code: Some(ResolvedDependency::Resolved(
ModuleSpecifier::resolve_url("https://cdn.skypack.dev/react") resolve_url("https://cdn.skypack.dev/react").unwrap()
.unwrap()
)), )),
maybe_type: Some(ResolvedDependency::Resolved( maybe_type: Some(ResolvedDependency::Resolved(
ModuleSpecifier::resolve_url( resolve_url("https://deno.land/x/types/react/index.d.ts").unwrap()
"https://deno.land/x/types/react/index.d.ts"
)
.unwrap()
)), )),
maybe_code_specifier_range: Some(Range { maybe_code_specifier_range: Some(Range {
start: Position { start: Position {
@ -729,8 +725,7 @@ mod tests {
Some(Dependency { Some(Dependency {
is_dynamic: false, is_dynamic: false,
maybe_code: Some(ResolvedDependency::Resolved( maybe_code: Some(ResolvedDependency::Resolved(
ModuleSpecifier::resolve_url("https://deno.land/x/oak@v6.3.2/mod.ts") resolve_url("https://deno.land/x/oak@v6.3.2/mod.ts").unwrap()
.unwrap()
)), )),
maybe_type: None, maybe_type: None,
maybe_code_specifier_range: Some(Range { maybe_code_specifier_range: Some(Range {

View file

@ -248,7 +248,7 @@ pub async fn generate_ts_diagnostics(
let res = ts_server.request(state_snapshot.clone(), req).await?; let res = ts_server.request(state_snapshot.clone(), req).await?;
let ts_diagnostic_map: TsDiagnostics = serde_json::from_value(res)?; let ts_diagnostic_map: TsDiagnostics = serde_json::from_value(res)?;
for (specifier_str, ts_diagnostics) in ts_diagnostic_map.iter() { for (specifier_str, ts_diagnostics) in ts_diagnostic_map.iter() {
let specifier = ModuleSpecifier::resolve_url(specifier_str)?; let specifier = deno_core::resolve_url(specifier_str)?;
let version = state_snapshot.documents.version(&specifier); let version = state_snapshot.documents.version(&specifier);
diagnostics.push(( diagnostics.push((
specifier, specifier,
@ -295,7 +295,7 @@ pub async fn generate_dependency_diagnostics(
} }
ResolvedDependency::Resolved(specifier) => { ResolvedDependency::Resolved(specifier) => {
if !(state_snapshot.documents.contains_key(&specifier) || sources.contains_key(&specifier)) { if !(state_snapshot.documents.contains_key(&specifier) || sources.contains_key(&specifier)) {
let is_local = specifier.as_url().scheme() == "file"; let is_local = specifier.scheme() == "file";
let (code, message) = if is_local { let (code, message) = if is_local {
(Some(lsp::NumberOrString::String("no-local".to_string())), format!("Unable to load a local module: \"{}\".\n Please check the file path.", specifier)) (Some(lsp::NumberOrString::String("no-local".to_string())), format!("Unable to load a local module: \"{}\".\n Please check the file path.", specifier))
} else { } else {

View file

@ -204,14 +204,14 @@ impl DocumentCache {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use deno_core::resolve_url;
use lspower::lsp; use lspower::lsp;
#[test] #[test]
fn test_document_cache_contains() { fn test_document_cache_contains() {
let mut document_cache = DocumentCache::default(); let mut document_cache = DocumentCache::default();
let specifier = ModuleSpecifier::resolve_url("file:///a/b.ts").unwrap(); let specifier = resolve_url("file:///a/b.ts").unwrap();
let missing_specifier = let missing_specifier = resolve_url("file:///a/c.ts").unwrap();
ModuleSpecifier::resolve_url("file:///a/c.ts").unwrap();
document_cache.open(specifier.clone(), 1, "console.log(\"Hello Deno\");\n"); document_cache.open(specifier.clone(), 1, "console.log(\"Hello Deno\");\n");
assert!(document_cache.contains_key(&specifier)); assert!(document_cache.contains_key(&specifier));
assert!(!document_cache.contains_key(&missing_specifier)); assert!(!document_cache.contains_key(&missing_specifier));
@ -220,7 +220,7 @@ mod tests {
#[test] #[test]
fn test_document_cache_change() { fn test_document_cache_change() {
let mut document_cache = DocumentCache::default(); let mut document_cache = DocumentCache::default();
let specifier = ModuleSpecifier::resolve_url("file:///a/b.ts").unwrap(); let specifier = resolve_url("file:///a/b.ts").unwrap();
document_cache.open(specifier.clone(), 1, "console.log(\"Hello deno\");\n"); document_cache.open(specifier.clone(), 1, "console.log(\"Hello deno\");\n");
document_cache document_cache
.change( .change(
@ -251,7 +251,7 @@ mod tests {
#[test] #[test]
fn test_document_cache_change_utf16() { fn test_document_cache_change_utf16() {
let mut document_cache = DocumentCache::default(); let mut document_cache = DocumentCache::default();
let specifier = ModuleSpecifier::resolve_url("file:///a/b.ts").unwrap(); let specifier = resolve_url("file:///a/b.ts").unwrap();
document_cache.open(specifier.clone(), 1, "console.log(\"Hello 🦕\");\n"); document_cache.open(specifier.clone(), 1, "console.log(\"Hello 🦕\");\n");
document_cache document_cache
.change( .change(

View file

@ -2,6 +2,7 @@
use deno_core::error::anyhow; use deno_core::error::anyhow;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::resolve_url;
use deno_core::serde::Deserialize; use deno_core::serde::Deserialize;
use deno_core::serde::Serialize; use deno_core::serde::Serialize;
use deno_core::serde_json; use deno_core::serde_json;
@ -172,7 +173,7 @@ impl Inner {
specifier: ModuleSpecifier, specifier: ModuleSpecifier,
) -> Result<LineIndex, AnyError> { ) -> Result<LineIndex, AnyError> {
let mark = self.performance.mark("get_line_index"); let mark = self.performance.mark("get_line_index");
let result = if specifier.as_url().scheme() == "asset" { let result = if specifier.scheme() == "asset" {
if let Some(asset) = self.get_asset(&specifier).await? { if let Some(asset) = self.get_asset(&specifier).await? {
Ok(asset.line_index) Ok(asset.line_index)
} else { } else {
@ -196,7 +197,7 @@ impl Inner {
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
) -> Option<LineIndex> { ) -> Option<LineIndex> {
let mark = self.performance.mark("get_line_index_sync"); let mark = self.performance.mark("get_line_index_sync");
let maybe_line_index = if specifier.as_url().scheme() == "asset" { let maybe_line_index = if specifier.scheme() == "asset" {
if let Some(Some(asset)) = self.assets.get(specifier) { if let Some(Some(asset)) = self.assets.get(specifier) {
Some(asset.line_index.clone()) Some(asset.line_index.clone())
} else { } else {
@ -374,7 +375,7 @@ impl Inner {
.cloned(), .cloned(),
); );
} }
let uri = specifier.as_url().clone(); let uri = specifier.clone();
let version = self.documents.version(&specifier); let version = self.documents.version(&specifier);
self self
.client .client
@ -1200,7 +1201,7 @@ impl Inner {
if let Some(implementations) = maybe_implementations { if let Some(implementations) = maybe_implementations {
let mut locations = Vec::new(); let mut locations = Vec::new();
for implementation in implementations { for implementation in implementations {
let implementation_specifier = ModuleSpecifier::resolve_url( let implementation_specifier = resolve_url(
&implementation.document_span.file_name, &implementation.document_span.file_name,
) )
.map_err(|err| { .map_err(|err| {
@ -1285,7 +1286,7 @@ impl Inner {
if reference.is_definition { if reference.is_definition {
continue; continue;
} }
let reference_specifier = ModuleSpecifier::resolve_url( let reference_specifier = resolve_url(
&reference.document_span.file_name, &reference.document_span.file_name,
) )
.map_err(|err| { .map_err(|err| {
@ -1439,8 +1440,7 @@ impl Inner {
continue; continue;
} }
let reference_specifier = let reference_specifier =
ModuleSpecifier::resolve_url(&reference.document_span.file_name) resolve_url(&reference.document_span.file_name).unwrap();
.unwrap();
// TODO(lucacasonato): handle error correctly // TODO(lucacasonato): handle error correctly
let line_index = let line_index =
self.get_line_index(reference_specifier).await.unwrap(); self.get_line_index(reference_specifier).await.unwrap();
@ -1585,8 +1585,7 @@ impl Inner {
let mut results = Vec::new(); let mut results = Vec::new();
for impl_ in implementations { for impl_ in implementations {
let document_span = impl_.document_span; let document_span = impl_.document_span;
let impl_specifier = let impl_specifier = resolve_url(&document_span.file_name).unwrap();
ModuleSpecifier::resolve_url(&document_span.file_name).unwrap();
let impl_line_index = let impl_line_index =
&self.get_line_index(impl_specifier).await.unwrap(); &self.get_line_index(impl_specifier).await.unwrap();
if let Some(link) = document_span.to_link(impl_line_index, self).await { if let Some(link) = document_span.to_link(impl_line_index, self).await {
@ -1981,8 +1980,7 @@ impl Inner {
) -> LspResult<Option<String>> { ) -> LspResult<Option<String>> {
let mark = self.performance.mark("virtual_text_document"); let mark = self.performance.mark("virtual_text_document");
let specifier = utils::normalize_url(params.text_document.uri); let specifier = utils::normalize_url(params.text_document.uri);
let url = specifier.as_url(); let contents = if specifier.as_str() == "deno:/status.md" {
let contents = if url.as_str() == "deno:/status.md" {
let mut contents = String::new(); let mut contents = String::new();
contents.push_str(&format!( contents.push_str(&format!(
@ -2001,7 +1999,7 @@ impl Inner {
} }
Some(contents) Some(contents)
} else { } else {
match url.scheme() { match specifier.scheme() {
"asset" => { "asset" => {
if let Some(asset) = self if let Some(asset) = self
.get_asset(&specifier) .get_asset(&specifier)

View file

@ -55,12 +55,12 @@ fn resolve_remote_specifier(
http_cache: &HttpCache, http_cache: &HttpCache,
redirect_limit: isize, redirect_limit: isize,
) -> Option<ModuleSpecifier> { ) -> Option<ModuleSpecifier> {
let cache_filename = http_cache.get_cache_filename(specifier.as_url())?; let cache_filename = http_cache.get_cache_filename(specifier)?;
if redirect_limit >= 0 && cache_filename.is_file() { if redirect_limit >= 0 && cache_filename.is_file() {
let headers = get_remote_headers(&cache_filename)?; let headers = get_remote_headers(&cache_filename)?;
if let Some(location) = headers.get("location") { if let Some(location) = headers.get("location") {
let redirect = let redirect =
ModuleSpecifier::resolve_import(location, specifier.as_str()).ok()?; deno_core::resolve_import(location, specifier.as_str()).ok()?;
resolve_remote_specifier(&redirect, http_cache, redirect_limit - 1) resolve_remote_specifier(&redirect, http_cache, redirect_limit - 1)
} else { } else {
Some(specifier.clone()) Some(specifier.clone())
@ -75,7 +75,7 @@ fn resolve_specifier(
redirects: &mut HashMap<ModuleSpecifier, ModuleSpecifier>, redirects: &mut HashMap<ModuleSpecifier, ModuleSpecifier>,
http_cache: &HttpCache, http_cache: &HttpCache,
) -> Option<ModuleSpecifier> { ) -> Option<ModuleSpecifier> {
let scheme = specifier.as_url().scheme(); let scheme = specifier.scheme();
if !SUPPORTED_SCHEMES.contains(&scheme) { if !SUPPORTED_SCHEMES.contains(&scheme) {
return None; return None;
} }
@ -83,7 +83,7 @@ fn resolve_specifier(
if scheme == "data" { if scheme == "data" {
Some(specifier.clone()) Some(specifier.clone())
} else if scheme == "file" { } else if scheme == "file" {
let path = specifier.as_url().to_file_path().ok()?; let path = specifier.to_file_path().ok()?;
if path.is_file() { if path.is_file() {
Some(specifier.clone()) Some(specifier.clone())
} else { } else {
@ -295,15 +295,14 @@ impl Inner {
let version = self.calculate_script_version(specifier)?; let version = self.calculate_script_version(specifier)?;
let path = self.get_path(specifier)?; let path = self.get_path(specifier)?;
let bytes = fs::read(path).ok()?; let bytes = fs::read(path).ok()?;
let scheme = specifier.as_url().scheme(); let scheme = specifier.scheme();
let (source, media_type, maybe_types) = if scheme == "file" { let (source, media_type, maybe_types) = if scheme == "file" {
let maybe_charset = let maybe_charset =
Some(text_encoding::detect_charset(&bytes).to_string()); Some(text_encoding::detect_charset(&bytes).to_string());
let source = get_source_from_bytes(bytes, maybe_charset).ok()?; let source = get_source_from_bytes(bytes, maybe_charset).ok()?;
(source, MediaType::from(specifier), None) (source, MediaType::from(specifier), None)
} else { } else {
let cache_filename = let cache_filename = self.http_cache.get_cache_filename(specifier)?;
self.http_cache.get_cache_filename(specifier.as_url())?;
let headers = get_remote_headers(&cache_filename)?; let headers = get_remote_headers(&cache_filename)?;
let maybe_content_type = headers.get("content-type").cloned(); let maybe_content_type = headers.get("content-type").cloned();
let (media_type, maybe_charset) = let (media_type, maybe_charset) =
@ -329,12 +328,12 @@ impl Inner {
} }
fn get_path(&mut self, specifier: &ModuleSpecifier) -> Option<PathBuf> { fn get_path(&mut self, specifier: &ModuleSpecifier) -> Option<PathBuf> {
if specifier.as_url().scheme() == "file" { if specifier.scheme() == "file" {
specifier.as_url().to_file_path().ok() specifier.to_file_path().ok()
} else if let Some(path) = self.remotes.get(&specifier) { } else if let Some(path) = self.remotes.get(&specifier) {
Some(path.clone()) Some(path.clone())
} else { } else {
let path = self.http_cache.get_cache_filename(&specifier.as_url())?; let path = self.http_cache.get_cache_filename(&specifier)?;
if path.is_file() { if path.is_file() {
self.remotes.insert(specifier.clone(), path.clone()); self.remotes.insert(specifier.clone(), path.clone());
Some(path) Some(path)
@ -436,6 +435,8 @@ impl Inner {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use deno_core::resolve_path;
use deno_core::resolve_url;
use std::env; use std::env;
use tempfile::TempDir; use tempfile::TempDir;
@ -451,10 +452,8 @@ mod tests {
let (sources, _) = setup(); let (sources, _) = setup();
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let tests = c.join("tests"); let tests = c.join("tests");
let specifier = ModuleSpecifier::resolve_path( let specifier =
&tests.join("001_hello.js").to_string_lossy(), resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap();
)
.unwrap();
let actual = sources.get_script_version(&specifier); let actual = sources.get_script_version(&specifier);
assert!(actual.is_some()); assert!(actual.is_some());
} }
@ -464,10 +463,8 @@ mod tests {
let (sources, _) = setup(); let (sources, _) = setup();
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let tests = c.join("tests"); let tests = c.join("tests");
let specifier = ModuleSpecifier::resolve_path( let specifier =
&tests.join("001_hello.js").to_string_lossy(), resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap();
)
.unwrap();
let actual = sources.get_source(&specifier); let actual = sources.get_source(&specifier);
assert!(actual.is_some()); assert!(actual.is_some());
let actual = actual.unwrap(); let actual = actual.unwrap();
@ -479,10 +476,8 @@ mod tests {
let (sources, _) = setup(); let (sources, _) = setup();
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let tests = c.join("tests"); let tests = c.join("tests");
let specifier = ModuleSpecifier::resolve_path( let specifier =
&tests.join("001_hello.js").to_string_lossy(), resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap();
)
.unwrap();
let actual = sources.get_length_utf16(&specifier); let actual = sources.get_length_utf16(&specifier);
assert!(actual.is_some()); assert!(actual.is_some());
let actual = actual.unwrap(); let actual = actual.unwrap();
@ -493,32 +488,25 @@ mod tests {
fn test_resolve_dependency_types() { fn test_resolve_dependency_types() {
let (sources, location) = setup(); let (sources, location) = setup();
let cache = HttpCache::new(&location); let cache = HttpCache::new(&location);
let specifier_dep = let specifier_dep = resolve_url("https://deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap();
cache cache
.set( .set(
specifier_dep.as_url(), &specifier_dep,
Default::default(), Default::default(),
b"export * from \"https://deno.land/x/lib.js\";", b"export * from \"https://deno.land/x/lib.js\";",
) )
.unwrap(); .unwrap();
let specifier_code = let specifier_code = resolve_url("https://deno.land/x/lib.js").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/lib.js").unwrap();
let mut headers_code = HashMap::new(); let mut headers_code = HashMap::new();
headers_code headers_code
.insert("x-typescript-types".to_string(), "./lib.d.ts".to_string()); .insert("x-typescript-types".to_string(), "./lib.d.ts".to_string());
cache cache
.set( .set(&specifier_code, headers_code, b"export const a = 1;")
specifier_code.as_url(),
headers_code,
b"export const a = 1;",
)
.unwrap(); .unwrap();
let specifier_type = let specifier_type = resolve_url("https://deno.land/x/lib.d.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/lib.d.ts").unwrap();
cache cache
.set( .set(
specifier_type.as_url(), &specifier_type,
Default::default(), Default::default(),
b"export const a: number;", b"export const a: number;",
) )
@ -532,19 +520,15 @@ mod tests {
fn test_resolve_dependency_evil_redirect() { fn test_resolve_dependency_evil_redirect() {
let (sources, location) = setup(); let (sources, location) = setup();
let cache = HttpCache::new(&location); let cache = HttpCache::new(&location);
let evil_specifier = let evil_specifier = resolve_url("https://deno.land/x/evil.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/evil.ts").unwrap();
let mut evil_headers = HashMap::new(); let mut evil_headers = HashMap::new();
evil_headers evil_headers
.insert("location".to_string(), "file:///etc/passwd".to_string()); .insert("location".to_string(), "file:///etc/passwd".to_string());
cache cache.set(&evil_specifier, evil_headers, b"").unwrap();
.set(evil_specifier.as_url(), evil_headers, b"") let remote_specifier = resolve_url("https://deno.land/x/mod.ts").unwrap();
.unwrap();
let remote_specifier =
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap();
cache cache
.set( .set(
remote_specifier.as_url(), &remote_specifier,
Default::default(), Default::default(),
b"export * from \"./evil.ts\";", b"export * from \"./evil.ts\";",
) )
@ -556,8 +540,8 @@ mod tests {
#[test] #[test]
fn test_sources_resolve_specifier_non_supported_schema() { fn test_sources_resolve_specifier_non_supported_schema() {
let (sources, _) = setup(); let (sources, _) = setup();
let specifier = ModuleSpecifier::resolve_url("foo://a/b/c.ts") let specifier =
.expect("could not create specifier"); resolve_url("foo://a/b/c.ts").expect("could not create specifier");
let sources = sources.0.lock().unwrap(); let sources = sources.0.lock().unwrap();
let mut redirects = sources.redirects.clone(); let mut redirects = sources.redirects.clone();
let http_cache = sources.http_cache.clone(); let http_cache = sources.http_cache.clone();

View file

@ -19,6 +19,7 @@ use deno_core::error::anyhow;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::json_op_sync; use deno_core::json_op_sync;
use deno_core::resolve_url;
use deno_core::serde::Deserialize; use deno_core::serde::Deserialize;
use deno_core::serde::Serialize; use deno_core::serde::Serialize;
use deno_core::serde_json; use deno_core::serde_json;
@ -113,7 +114,7 @@ impl Default for Assets {
.iter() .iter()
.map(|(k, v)| { .map(|(k, v)| {
let url_str = format!("asset:///{}", k); let url_str = format!("asset:///{}", k);
let specifier = ModuleSpecifier::resolve_url(&url_str).unwrap(); let specifier = resolve_url(&url_str).unwrap();
let asset = AssetDocument::new(v); let asset = AssetDocument::new(v);
(specifier, Some(asset)) (specifier, Some(asset))
}) })
@ -478,8 +479,7 @@ impl DocumentSpan {
line_index: &LineIndex, line_index: &LineIndex,
language_server: &mut language_server::Inner, language_server: &mut language_server::Inner,
) -> Option<lsp::LocationLink> { ) -> Option<lsp::LocationLink> {
let target_specifier = let target_specifier = resolve_url(&self.file_name).unwrap();
ModuleSpecifier::resolve_url(&self.file_name).unwrap();
if let Ok(target_line_index) = if let Ok(target_line_index) =
language_server.get_line_index(target_specifier).await language_server.get_line_index(target_specifier).await
{ {
@ -615,8 +615,7 @@ impl RenameLocations {
HashMap::new(); HashMap::new();
for location in self.locations.iter() { for location in self.locations.iter() {
let uri = utils::normalize_file_name(&location.document_span.file_name)?; let uri = utils::normalize_file_name(&location.document_span.file_name)?;
let specifier = let specifier = resolve_url(&location.document_span.file_name)?;
ModuleSpecifier::resolve_url(&location.document_span.file_name)?;
// ensure TextDocumentEdit for `location.file_name`. // ensure TextDocumentEdit for `location.file_name`.
if text_document_edit_map.get(&uri).is_none() { if text_document_edit_map.get(&uri).is_none() {
@ -778,7 +777,7 @@ impl FileTextChanges {
&self, &self,
language_server: &mut language_server::Inner, language_server: &mut language_server::Inner,
) -> Result<lsp::TextDocumentEdit, AnyError> { ) -> Result<lsp::TextDocumentEdit, AnyError> {
let specifier = ModuleSpecifier::resolve_url(&self.file_name)?; let specifier = resolve_url(&self.file_name)?;
let line_index = language_server.get_line_index(specifier.clone()).await?; let line_index = language_server.get_line_index(specifier.clone()).await?;
let edits = self let edits = self
.text_changes .text_changes
@ -787,7 +786,7 @@ impl FileTextChanges {
.collect(); .collect();
Ok(lsp::TextDocumentEdit { Ok(lsp::TextDocumentEdit {
text_document: lsp::OptionalVersionedTextDocumentIdentifier { text_document: lsp::OptionalVersionedTextDocumentIdentifier {
uri: specifier.as_url().clone(), uri: specifier.clone(),
version: language_server.document_version(specifier), version: language_server.document_version(specifier),
}, },
edits, edits,
@ -1063,7 +1062,7 @@ fn cache_snapshot(
.snapshots .snapshots
.contains_key(&(specifier.clone().into(), version.clone().into())) .contains_key(&(specifier.clone().into(), version.clone().into()))
{ {
let s = ModuleSpecifier::resolve_url(&specifier)?; let s = resolve_url(&specifier)?;
let content = state.state_snapshot.documents.content(&s)?.unwrap(); let content = state.state_snapshot.documents.content(&s)?.unwrap();
state state
.snapshots .snapshots
@ -1156,7 +1155,7 @@ fn get_change_range(state: &mut State, args: Value) -> Result<Value, AnyError> {
fn get_length(state: &mut State, args: Value) -> Result<Value, AnyError> { fn get_length(state: &mut State, args: Value) -> Result<Value, AnyError> {
let mark = state.state_snapshot.performance.mark("op_get_length"); let mark = state.state_snapshot.performance.mark("op_get_length");
let v: SourceSnapshotArgs = serde_json::from_value(args)?; let v: SourceSnapshotArgs = serde_json::from_value(args)?;
let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; let specifier = resolve_url(&v.specifier)?;
if let Some(Some(asset)) = state.state_snapshot.assets.get(&specifier) { if let Some(Some(asset)) = state.state_snapshot.assets.get(&specifier) {
Ok(json!(asset.length)) Ok(json!(asset.length))
} else if state.state_snapshot.documents.contains_key(&specifier) { } else if state.state_snapshot.documents.contains_key(&specifier) {
@ -1186,7 +1185,7 @@ struct GetTextArgs {
fn get_text(state: &mut State, args: Value) -> Result<Value, AnyError> { fn get_text(state: &mut State, args: Value) -> Result<Value, AnyError> {
let mark = state.state_snapshot.performance.mark("op_get_text"); let mark = state.state_snapshot.performance.mark("op_get_text");
let v: GetTextArgs = serde_json::from_value(args)?; let v: GetTextArgs = serde_json::from_value(args)?;
let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; let specifier = resolve_url(&v.specifier)?;
let content = let content =
if let Some(Some(content)) = state.state_snapshot.assets.get(&specifier) { if let Some(Some(content)) = state.state_snapshot.assets.get(&specifier) {
content.text.clone() content.text.clone()
@ -1208,7 +1207,7 @@ fn resolve(state: &mut State, args: Value) -> Result<Value, AnyError> {
let mark = state.state_snapshot.performance.mark("op_resolve"); let mark = state.state_snapshot.performance.mark("op_resolve");
let v: ResolveArgs = serde_json::from_value(args)?; let v: ResolveArgs = serde_json::from_value(args)?;
let mut resolved = Vec::<Option<(String, String)>>::new(); let mut resolved = Vec::<Option<(String, String)>>::new();
let referrer = ModuleSpecifier::resolve_url(&v.base)?; let referrer = resolve_url(&v.base)?;
let sources = &mut state.state_snapshot.sources; let sources = &mut state.state_snapshot.sources;
if state.state_snapshot.documents.contains_key(&referrer) { if state.state_snapshot.documents.contains_key(&referrer) {
@ -1311,8 +1310,8 @@ struct ScriptVersionArgs {
fn script_version(state: &mut State, args: Value) -> Result<Value, AnyError> { fn script_version(state: &mut State, args: Value) -> Result<Value, AnyError> {
let mark = state.state_snapshot.performance.mark("op_script_version"); let mark = state.state_snapshot.performance.mark("op_script_version");
let v: ScriptVersionArgs = serde_json::from_value(args)?; let v: ScriptVersionArgs = serde_json::from_value(args)?;
let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; let specifier = resolve_url(&v.specifier)?;
if specifier.as_url().scheme() == "asset" { if specifier.scheme() == "asset" {
return if state.state_snapshot.assets.contains_key(&specifier) { return if state.state_snapshot.assets.contains_key(&specifier) {
Ok(json!("1")) Ok(json!("1"))
} else { } else {
@ -1673,8 +1672,8 @@ mod tests {
fn mock_state_snapshot(sources: Vec<(&str, &str, i32)>) -> StateSnapshot { fn mock_state_snapshot(sources: Vec<(&str, &str, i32)>) -> StateSnapshot {
let mut documents = DocumentCache::default(); let mut documents = DocumentCache::default();
for (specifier, content, version) in sources { for (specifier, content, version) in sources {
let specifier = ModuleSpecifier::resolve_url(specifier) let specifier =
.expect("failed to create specifier"); resolve_url(specifier).expect("failed to create specifier");
documents.open(specifier, version, content); documents.open(specifier, version, content);
} }
StateSnapshot { StateSnapshot {
@ -1769,8 +1768,7 @@ mod tests {
}), }),
vec![("file:///a.ts", r#"console.log("hello deno");"#, 1)], vec![("file:///a.ts", r#"console.log("hello deno");"#, 1)],
); );
let specifier = ModuleSpecifier::resolve_url("file:///a.ts") let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
.expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,
@ -1815,8 +1813,7 @@ mod tests {
}), }),
vec![("file:///a.ts", r#"console.log(document.location);"#, 1)], vec![("file:///a.ts", r#"console.log(document.location);"#, 1)],
); );
let specifier = ModuleSpecifier::resolve_url("file:///a.ts") let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
.expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,
@ -1849,8 +1846,7 @@ mod tests {
1, 1,
)], )],
); );
let specifier = ModuleSpecifier::resolve_url("file:///a.ts") let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
.expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,
@ -1879,8 +1875,7 @@ mod tests {
1, 1,
)], )],
); );
let specifier = ModuleSpecifier::resolve_url("file:///a.ts") let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
.expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,
@ -1933,8 +1928,7 @@ mod tests {
1, 1,
)], )],
); );
let specifier = ModuleSpecifier::resolve_url("file:///a.ts") let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
.expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,
@ -1970,8 +1964,7 @@ mod tests {
1, 1,
)], )],
); );
let specifier = ModuleSpecifier::resolve_url("file:///a.ts") let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
.expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,
@ -2028,8 +2021,7 @@ mod tests {
}), }),
vec![("file:///a.ts", r#"const url = new URL("b.js", import."#, 1)], vec![("file:///a.ts", r#"const url = new URL("b.js", import."#, 1)],
); );
let specifier = ModuleSpecifier::resolve_url("file:///a.ts") let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
.expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,
@ -2052,8 +2044,8 @@ mod tests {
}), }),
vec![], vec![],
); );
let specifier = ModuleSpecifier::resolve_url("asset:///lib.esnext.d.ts") let specifier =
.expect("could not resolve url"); resolve_url("asset:///lib.esnext.d.ts").expect("could not resolve url");
let result = request( let result = request(
&mut runtime, &mut runtime,
state_snapshot, state_snapshot,

View file

@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::resolve_url;
use deno_core::url::Position; use deno_core::url::Position;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
@ -19,12 +20,11 @@ pub fn normalize_file_name(file_name: &str) -> Result<Url, AnyError> {
pub fn normalize_specifier( pub fn normalize_specifier(
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
) -> Result<Url, AnyError> { ) -> Result<Url, AnyError> {
let url = specifier.as_url(); if specifier.scheme() == "file" {
if url.scheme() == "file" { Ok(specifier.clone())
Ok(url.clone())
} else { } else {
let specifier_str = let specifier_str =
format!("deno:///{}", url.as_str().replacen("://", "/", 1)); format!("deno:///{}", specifier.as_str().replacen("://", "/", 1));
Url::parse(&specifier_str).map_err(|err| err.into()) Url::parse(&specifier_str).map_err(|err| err.into())
} }
} }
@ -41,12 +41,12 @@ pub fn normalize_url(url: Url) -> ModuleSpecifier {
if let Ok(specifier) = if let Ok(specifier) =
percent_encoding::percent_decode_str(&specifier_str).decode_utf8() percent_encoding::percent_decode_str(&specifier_str).decode_utf8()
{ {
if let Ok(specifier) = ModuleSpecifier::resolve_url(&specifier) { if let Ok(specifier) = resolve_url(&specifier) {
return specifier; return specifier;
} }
} }
} }
ModuleSpecifier::from(url) url
} }
#[cfg(test)] #[cfg(test)]
@ -63,8 +63,7 @@ mod tests {
#[test] #[test]
fn test_normalize_specifier() { fn test_normalize_specifier() {
let fixture = let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap();
let actual = normalize_specifier(&fixture).unwrap(); let actual = normalize_specifier(&fixture).unwrap();
let expected = Url::parse("deno:///https/deno.land/x/mod.ts").unwrap(); let expected = Url::parse("deno:///https/deno.land/x/mod.ts").unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
@ -74,9 +73,6 @@ mod tests {
fn test_normalize_url() { fn test_normalize_url() {
let fixture = Url::parse("deno:///https/deno.land/x/mod.ts").unwrap(); let fixture = Url::parse("deno:///https/deno.land/x/mod.ts").unwrap();
let actual = normalize_url(fixture); let actual = normalize_url(fixture);
assert_eq!( assert_eq!(actual, resolve_url("https://deno.land/x/mod.ts").unwrap());
actual,
ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap()
);
} }
} }

View file

@ -61,6 +61,7 @@ use deno_core::error::generic_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::futures::future::FutureExt; use deno_core::futures::future::FutureExt;
use deno_core::futures::Future; use deno_core::futures::Future;
use deno_core::resolve_url_or_path;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::v8_set_flags; use deno_core::v8_set_flags;
@ -312,12 +313,12 @@ async fn compile_command(
let run_flags = let run_flags =
tools::standalone::compile_to_runtime_flags(flags.clone(), args)?; tools::standalone::compile_to_runtime_flags(flags.clone(), args)?;
let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?; let module_specifier = resolve_url_or_path(&source_file)?;
let program_state = ProgramState::build(flags.clone()).await?; let program_state = ProgramState::build(flags.clone()).await?;
let deno_dir = &program_state.dir; let deno_dir = &program_state.dir;
let output = output.or_else(|| { let output = output.or_else(|| {
infer_name_from_url(module_specifier.as_url()).map(PathBuf::from) infer_name_from_url(&module_specifier).map(PathBuf::from)
}).ok_or_else(|| generic_error( }).ok_or_else(|| generic_error(
"An executable name was not provided. One could not be inferred from the URL. Aborting.", "An executable name was not provided. One could not be inferred from the URL. Aborting.",
))?; ))?;
@ -369,7 +370,7 @@ async fn info_command(
} }
let program_state = ProgramState::build(flags).await?; let program_state = ProgramState::build(flags).await?;
if let Some(specifier) = maybe_specifier { if let Some(specifier) = maybe_specifier {
let specifier = ModuleSpecifier::resolve_url_or_path(&specifier)?; let specifier = resolve_url_or_path(&specifier)?;
let handler = Arc::new(Mutex::new(specifier_handler::FetchHandler::new( let handler = Arc::new(Mutex::new(specifier_handler::FetchHandler::new(
&program_state, &program_state,
// info accesses dynamically imported modules just for their information // info accesses dynamically imported modules just for their information
@ -410,7 +411,7 @@ async fn install_command(
preload_flags.inspect_brk = None; preload_flags.inspect_brk = None;
let permissions = Permissions::from_options(&preload_flags.clone().into()); let permissions = Permissions::from_options(&preload_flags.clone().into());
let program_state = ProgramState::build(preload_flags).await?; let program_state = ProgramState::build(preload_flags).await?;
let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?; let main_module = resolve_url_or_path(&module_url)?;
let mut worker = let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions); create_main_worker(&program_state, main_module.clone(), permissions);
// First, fetch and compile the module; this step ensures that the module exists. // First, fetch and compile the module; this step ensures that the module exists.
@ -453,7 +454,7 @@ async fn cache_command(
let program_state = ProgramState::build(flags).await?; let program_state = ProgramState::build(flags).await?;
for file in files { for file in files {
let specifier = ModuleSpecifier::resolve_url_or_path(&file)?; let specifier = resolve_url_or_path(&file)?;
program_state program_state
.prepare_module_load( .prepare_module_load(
specifier, specifier,
@ -475,13 +476,11 @@ async fn eval_command(
print: bool, print: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
// Force TypeScript compile. // Force TypeScript compile.
let main_module = let main_module = resolve_url_or_path("./$deno$eval.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("./$deno$eval.ts").unwrap();
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let program_state = ProgramState::build(flags).await?; let program_state = ProgramState::build(flags).await?;
let mut worker = let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions); create_main_worker(&program_state, main_module.clone(), permissions);
let main_module_url = main_module.as_url().to_owned();
// Create a dummy source file. // Create a dummy source file.
let source_code = if print { let source_code = if print {
format!("console.log({})", code) format!("console.log({})", code)
@ -491,7 +490,7 @@ async fn eval_command(
.into_bytes(); .into_bytes();
let file = File { let file = File {
local: main_module_url.to_file_path().unwrap(), local: main_module.clone().to_file_path().unwrap(),
maybe_types: None, maybe_types: None,
media_type: if as_typescript { media_type: if as_typescript {
MediaType::TypeScript MediaType::TypeScript
@ -499,7 +498,7 @@ async fn eval_command(
MediaType::JavaScript MediaType::JavaScript
}, },
source: String::from_utf8(source_code)?, source: String::from_utf8(source_code)?,
specifier: ModuleSpecifier::from(main_module_url), specifier: main_module.clone(),
}; };
// Save our fake file into file fetcher cache // Save our fake file into file fetcher cache
@ -592,8 +591,7 @@ async fn bundle_command(
let source_file1 = source_file.clone(); let source_file1 = source_file.clone();
let source_file2 = source_file.clone(); let source_file2 = source_file.clone();
async move { async move {
let module_specifier = let module_specifier = resolve_url_or_path(&source_file1)?;
ModuleSpecifier::resolve_url_or_path(&source_file1)?;
debug!(">>>>> bundle START"); debug!(">>>>> bundle START");
let program_state = ProgramState::build(flags.clone()).await?; let program_state = ProgramState::build(flags.clone()).await?;
@ -614,7 +612,7 @@ async fn bundle_command(
let mut paths_to_watch: Vec<PathBuf> = module_graph let mut paths_to_watch: Vec<PathBuf> = module_graph
.get_modules() .get_modules()
.iter() .iter()
.filter_map(|specifier| specifier.as_url().to_file_path().ok()) .filter_map(|specifier| specifier.to_file_path().ok())
.collect(); .collect();
if let Some(import_map) = program_state.flags.import_map_path.as_ref() { if let Some(import_map) = program_state.flags.import_map_path.as_ref() {
@ -705,7 +703,7 @@ impl DocFileLoader for DocLoader {
let resolved_specifier = if let Some(resolved) = maybe_resolved { let resolved_specifier = if let Some(resolved) = maybe_resolved {
resolved resolved
} else { } else {
ModuleSpecifier::resolve_import(specifier, referrer) deno_core::resolve_import(specifier, referrer)
.map_err(|e| doc::DocError::Resolve(e.to_string()))? .map_err(|e| doc::DocError::Resolve(e.to_string()))?
}; };
@ -717,8 +715,8 @@ impl DocFileLoader for DocLoader {
specifier: &str, specifier: &str,
) -> Pin<Box<dyn Future<Output = Result<String, doc::DocError>>>> { ) -> Pin<Box<dyn Future<Output = Result<String, doc::DocError>>>> {
let fetcher = self.fetcher.clone(); let fetcher = self.fetcher.clone();
let specifier = ModuleSpecifier::resolve_url_or_path(specifier) let specifier =
.expect("Expected valid specifier"); resolve_url_or_path(specifier).expect("Expected valid specifier");
async move { async move {
let source_file = fetcher let source_file = fetcher
.fetch(&specifier, &Permissions::allow_all()) .fetch(&specifier, &Permissions::allow_all())
@ -762,8 +760,7 @@ async fn doc_command(
let path = PathBuf::from(&source_file); let path = PathBuf::from(&source_file);
let media_type = MediaType::from(&path); let media_type = MediaType::from(&path);
let syntax = ast::get_syntax(&media_type); let syntax = ast::get_syntax(&media_type);
let module_specifier = let module_specifier = resolve_url_or_path(&source_file).unwrap();
ModuleSpecifier::resolve_url_or_path(&source_file).unwrap();
doc_parser doc_parser
.parse_with_reexports(&module_specifier.to_string(), syntax) .parse_with_reexports(&module_specifier.to_string(), syntax)
.await .await
@ -819,8 +816,7 @@ async fn format_command(
} }
async fn run_repl(flags: Flags) -> Result<(), AnyError> { async fn run_repl(flags: Flags) -> Result<(), AnyError> {
let main_module = let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("./$deno$repl.ts").unwrap();
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let program_state = ProgramState::build(flags).await?; let program_state = ProgramState::build(flags).await?;
let mut worker = let mut worker =
@ -833,8 +829,7 @@ async fn run_repl(flags: Flags) -> Result<(), AnyError> {
async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> { async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags.clone()).await?; let program_state = ProgramState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let main_module = let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("./$deno$stdin.ts").unwrap();
let mut worker = create_main_worker( let mut worker = create_main_worker(
&program_state.clone(), &program_state.clone(),
main_module.clone(), main_module.clone(),
@ -843,10 +838,9 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
let mut source = Vec::new(); let mut source = Vec::new();
std::io::stdin().read_to_end(&mut source)?; std::io::stdin().read_to_end(&mut source)?;
let main_module_url = main_module.as_url().to_owned();
// Create a dummy source file. // Create a dummy source file.
let source_file = File { let source_file = File {
local: main_module_url.to_file_path().unwrap(), local: main_module.clone().to_file_path().unwrap(),
maybe_types: None, maybe_types: None,
media_type: MediaType::TypeScript, media_type: MediaType::TypeScript,
source: String::from_utf8(source)?, source: String::from_utf8(source)?,
@ -870,7 +864,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
let script2 = script.clone(); let script2 = script.clone();
let flags = flags.clone(); let flags = flags.clone();
async move { async move {
let main_module = ModuleSpecifier::resolve_url_or_path(&script1)?; let main_module = resolve_url_or_path(&script1)?;
let program_state = ProgramState::build(flags).await?; let program_state = ProgramState::build(flags).await?;
let handler = Arc::new(Mutex::new(FetchHandler::new( let handler = Arc::new(Mutex::new(FetchHandler::new(
&program_state, &program_state,
@ -888,7 +882,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
let mut paths_to_watch: Vec<PathBuf> = module_graph let mut paths_to_watch: Vec<PathBuf> = module_graph
.get_modules() .get_modules()
.iter() .iter()
.filter_map(|specifier| specifier.as_url().to_file_path().ok()) .filter_map(|specifier| specifier.to_file_path().ok())
.collect(); .collect();
if let Some(import_map) = program_state.flags.import_map_path.as_ref() { if let Some(import_map) = program_state.flags.import_map_path.as_ref() {
@ -947,7 +941,7 @@ async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> {
return run_with_watch(flags, script).await; return run_with_watch(flags, script).await;
} }
let main_module = ModuleSpecifier::resolve_url_or_path(&script)?; let main_module = resolve_url_or_path(&script)?;
let program_state = ProgramState::build(flags.clone()).await?; let program_state = ProgramState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let mut worker = let mut worker =
@ -1003,10 +997,10 @@ async fn test_command(
} }
return Ok(()); return Ok(());
} }
let main_module = ModuleSpecifier::resolve_path("$deno$test.ts")?; let main_module = deno_core::resolve_path("$deno$test.ts")?;
// Create a dummy source file. // Create a dummy source file.
let source_file = File { let source_file = File {
local: main_module.as_url().to_file_path().unwrap(), local: main_module.to_file_path().unwrap(),
maybe_types: None, maybe_types: None,
media_type: MediaType::TypeScript, media_type: MediaType::TypeScript,
source: tools::test_runner::render_test_file( source: tools::test_runner::render_test_file(
@ -1074,8 +1068,7 @@ async fn test_command(
// For now, we'll only report for the command that passed --coverage as a flag. // For now, we'll only report for the command that passed --coverage as a flag.
if flags.coverage_dir.is_some() { if flags.coverage_dir.is_some() {
let mut exclude = test_modules.clone(); let mut exclude = test_modules.clone();
let main_module_url = main_module.as_url().to_owned(); exclude.push(main_module.clone());
exclude.push(main_module_url);
tools::coverage::report_coverages( tools::coverage::report_coverages(
program_state.clone(), program_state.clone(),
&coverage_collector.dir, &coverage_collector.dir,

View file

@ -63,15 +63,14 @@ impl<'a> From<&'a String> for MediaType {
impl<'a> From<&'a ModuleSpecifier> for MediaType { impl<'a> From<&'a ModuleSpecifier> for MediaType {
fn from(specifier: &'a ModuleSpecifier) -> Self { fn from(specifier: &'a ModuleSpecifier) -> Self {
let url = specifier.as_url(); let path = if specifier.scheme() == "file" {
let path = if url.scheme() == "file" { if let Ok(path) = specifier.to_file_path() {
if let Ok(path) = url.to_file_path() {
path path
} else { } else {
PathBuf::from(url.path()) PathBuf::from(specifier.path())
} }
} else { } else {
PathBuf::from(url.path()) PathBuf::from(specifier.path())
}; };
MediaType::from_path(&path) MediaType::from_path(&path)
} }
@ -245,7 +244,7 @@ mod tests {
]; ];
for (specifier, expected) in fixtures { for (specifier, expected) in fixtures {
let actual = ModuleSpecifier::resolve_url_or_path(specifier).unwrap(); let actual = deno_core::resolve_url_or_path(specifier).unwrap();
assert_eq!(MediaType::from(&actual), expected); assert_eq!(MediaType::from(&actual), expected);
} }
} }

View file

@ -33,6 +33,7 @@ use deno_core::error::get_custom_error_class;
use deno_core::error::Context; use deno_core::error::Context;
use deno_core::futures::stream::FuturesUnordered; use deno_core::futures::stream::FuturesUnordered;
use deno_core::futures::stream::StreamExt; use deno_core::futures::stream::StreamExt;
use deno_core::resolve_url_or_path;
use deno_core::serde::Deserialize; use deno_core::serde::Deserialize;
use deno_core::serde::Deserializer; use deno_core::serde::Deserializer;
use deno_core::serde::Serialize; use deno_core::serde::Serialize;
@ -150,7 +151,7 @@ impl swc_bundler::Load for BundleLoader<'_> {
) -> Result<swc_bundler::ModuleData, AnyError> { ) -> Result<swc_bundler::ModuleData, AnyError> {
match file { match file {
swc_common::FileName::Custom(filename) => { swc_common::FileName::Custom(filename) => {
let specifier = ModuleSpecifier::resolve_url_or_path(filename) let specifier = resolve_url_or_path(filename)
.context("Failed to convert swc FileName to ModuleSpecifier.")?; .context("Failed to convert swc FileName to ModuleSpecifier.")?;
if let Some(src) = self.graph.get_source(&specifier) { if let Some(src) = self.graph.get_source(&specifier) {
let media_type = self let media_type = self
@ -259,7 +260,7 @@ impl Default for Module {
maybe_types: None, maybe_types: None,
maybe_version: None, maybe_version: None,
media_type: MediaType::Unknown, media_type: MediaType::Unknown,
specifier: ModuleSpecifier::resolve_url("file:///example.js").unwrap(), specifier: deno_core::resolve_url("file:///example.js").unwrap(),
source: "".to_string(), source: "".to_string(),
source_path: PathBuf::new(), source_path: PathBuf::new(),
} }
@ -449,11 +450,11 @@ impl Module {
remapped_import = true; remapped_import = true;
module_specifier module_specifier
} else { } else {
ModuleSpecifier::resolve_import(specifier, self.specifier.as_str())? deno_core::resolve_import(specifier, self.specifier.as_str())?
}; };
let referrer_scheme = self.specifier.as_url().scheme(); let referrer_scheme = self.specifier.scheme();
let specifier_scheme = specifier.as_url().scheme(); let specifier_scheme = specifier.scheme();
let location = maybe_location.unwrap_or(Location { let location = maybe_location.unwrap_or(Location {
filename: self.specifier.to_string(), filename: self.specifier.to_string(),
line: 0, line: 0,
@ -1769,7 +1770,7 @@ impl swc_bundler::Resolve for Graph {
specifier: &str, specifier: &str,
) -> Result<swc_common::FileName, AnyError> { ) -> Result<swc_common::FileName, AnyError> {
let referrer = if let swc_common::FileName::Custom(referrer) = referrer { let referrer = if let swc_common::FileName::Custom(referrer) = referrer {
ModuleSpecifier::resolve_url_or_path(referrer) resolve_url_or_path(referrer)
.context("Cannot resolve swc FileName to a module specifier")? .context("Cannot resolve swc FileName to a module specifier")?
} else { } else {
unreachable!( unreachable!(
@ -1995,7 +1996,7 @@ pub mod tests {
let media_type = MediaType::from(&source_path); let media_type = MediaType::from(&source_path);
let source = fs::read_to_string(&source_path) let source = fs::read_to_string(&source_path)
.map_err(|err| (specifier.clone(), err.into()))?; .map_err(|err| (specifier.clone(), err.into()))?;
let is_remote = specifier.as_url().scheme() != "file"; let is_remote = specifier.scheme() != "file";
Ok(CachedModule { Ok(CachedModule {
source, source,
@ -2200,7 +2201,7 @@ pub mod tests {
let fixtures = c.join("tests/bundle"); let fixtures = c.join("tests/bundle");
for (specifier, expected_str) in tests { for (specifier, expected_str) in tests {
let specifier = ModuleSpecifier::resolve_url_or_path(specifier).unwrap(); let specifier = resolve_url_or_path(specifier).unwrap();
let handler = Arc::new(Mutex::new(MockSpecifierHandler { let handler = Arc::new(Mutex::new(MockSpecifierHandler {
fixtures: fixtures.clone(), fixtures: fixtures.clone(),
..MockSpecifierHandler::default() ..MockSpecifierHandler::default()
@ -2224,8 +2225,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_check_emit() { async fn test_graph_check_emit() {
let specifier = let specifier = resolve_url_or_path("file:///tests/main.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/main.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, handler) = setup(specifier).await; let (graph, handler) = setup(specifier).await;
let result_info = graph let result_info = graph
@ -2247,8 +2247,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_check_ignores_dynamic_import_errors() { async fn test_graph_check_ignores_dynamic_import_errors() {
let specifier = let specifier = resolve_url_or_path("file:///tests/dynamicimport.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/dynamicimport.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, _) = setup(specifier).await; let (graph, _) = setup(specifier).await;
let result_info = graph let result_info = graph
@ -2265,8 +2264,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn fix_graph_check_emit_diagnostics() { async fn fix_graph_check_emit_diagnostics() {
let specifier = let specifier = resolve_url_or_path("file:///tests/diag.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/diag.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, handler) = setup(specifier).await; let (graph, handler) = setup(specifier).await;
let result_info = graph let result_info = graph
@ -2290,8 +2288,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_check_no_emit() { async fn test_graph_check_no_emit() {
let specifier = let specifier = resolve_url_or_path("file:///tests/main.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/main.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, handler) = setup(specifier).await; let (graph, handler) = setup(specifier).await;
let result_info = graph let result_info = graph
@ -2313,7 +2310,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn fix_graph_check_mjs_root() { async fn fix_graph_check_mjs_root() {
let specifier = ModuleSpecifier::resolve_url_or_path("file:///tests/a.mjs") let specifier = resolve_url_or_path("file:///tests/a.mjs")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, handler) = setup(specifier).await; let (graph, handler) = setup(specifier).await;
let result_info = graph let result_info = graph
@ -2334,7 +2331,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn fix_graph_check_types_root() { async fn fix_graph_check_types_root() {
let specifier = ModuleSpecifier::resolve_url_or_path("file:///typesref.js") let specifier = resolve_url_or_path("file:///typesref.js")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, _) = setup(specifier).await; let (graph, _) = setup(specifier).await;
let result_info = graph let result_info = graph
@ -2351,8 +2348,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_check_user_config() { async fn test_graph_check_user_config() {
let specifier = let specifier = resolve_url_or_path("file:///tests/checkwithconfig.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/checkwithconfig.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, handler) = setup(specifier.clone()).await; let (graph, handler) = setup(specifier.clone()).await;
let result_info = graph let result_info = graph
@ -2397,8 +2393,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_emit() { async fn test_graph_emit() {
let specifier = let specifier = resolve_url_or_path("file:///a.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///a.ts").unwrap();
let graph = setup_memory( let graph = setup_memory(
specifier, specifier,
map!( map!(
@ -2438,8 +2433,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_emit_bundle() { async fn test_graph_emit_bundle() {
let specifier = let specifier = resolve_url_or_path("file:///a.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///a.ts").unwrap();
let graph = setup_memory( let graph = setup_memory(
specifier, specifier,
map!( map!(
@ -2474,8 +2468,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn fix_graph_emit_declaration() { async fn fix_graph_emit_declaration() {
let specifier = let specifier = resolve_url_or_path("file:///a.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///a.ts").unwrap();
let graph = setup_memory( let graph = setup_memory(
specifier, specifier,
map!( map!(
@ -2519,8 +2512,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_info() { async fn test_graph_info() {
let specifier = let specifier = resolve_url_or_path("file:///tests/main.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/main.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let (graph, _) = setup(specifier).await; let (graph, _) = setup(specifier).await;
let info = graph.info().expect("could not get info"); let info = graph.info().expect("could not get info");
@ -2532,15 +2524,14 @@ pub mod tests {
assert!(info.map.is_none()); assert!(info.map.is_none());
assert_eq!( assert_eq!(
info.module, info.module,
ModuleSpecifier::resolve_url_or_path("file:///tests/main.ts").unwrap() resolve_url_or_path("file:///tests/main.ts").unwrap()
); );
assert_eq!(info.total_size, 344); assert_eq!(info.total_size, 344);
} }
#[tokio::test] #[tokio::test]
async fn test_graph_import_json() { async fn test_graph_import_json() {
let specifier = let specifier = resolve_url_or_path("file:///tests/importjson.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/importjson.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let fixtures = c.join("tests/module_graph"); let fixtures = c.join("tests/module_graph");
@ -2564,8 +2555,7 @@ pub mod tests {
// to be actually emitted. // to be actually emitted.
// //
// This also exercises "@deno-types" and type references. // This also exercises "@deno-types" and type references.
let specifier = let specifier = resolve_url_or_path("file:///tests/main.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/main.ts")
.expect("could not resolve module"); .expect("could not resolve module");
let (mut graph, handler) = setup(specifier).await; let (mut graph, handler) = setup(specifier).await;
let result_info = graph.transpile(TranspileOptions::default()).unwrap(); let result_info = graph.transpile(TranspileOptions::default()).unwrap();
@ -2592,19 +2582,17 @@ pub mod tests {
assert_eq!(h.deps_calls.len(), 7); assert_eq!(h.deps_calls.len(), 7);
assert_eq!( assert_eq!(
h.deps_calls[0].0, h.deps_calls[0].0,
ModuleSpecifier::resolve_url_or_path("file:///tests/main.ts").unwrap() resolve_url_or_path("file:///tests/main.ts").unwrap()
); );
assert_eq!(h.deps_calls[0].1.len(), 1); assert_eq!(h.deps_calls[0].1.len(), 1);
assert_eq!( assert_eq!(
h.deps_calls[1].0, h.deps_calls[1].0,
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/lib/mod.js") resolve_url_or_path("https://deno.land/x/lib/mod.js").unwrap()
.unwrap()
); );
assert_eq!(h.deps_calls[1].1.len(), 3); assert_eq!(h.deps_calls[1].1.len(), 3);
assert_eq!( assert_eq!(
h.deps_calls[2].0, h.deps_calls[2].0,
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/lib/mod.d.ts") resolve_url_or_path("https://deno.land/x/lib/mod.d.ts").unwrap()
.unwrap()
); );
assert_eq!(h.deps_calls[2].1.len(), 3, "should have 3 dependencies"); assert_eq!(h.deps_calls[2].1.len(), 3, "should have 3 dependencies");
// sometimes the calls are not deterministic, and so checking the contents // sometimes the calls are not deterministic, and so checking the contents
@ -2617,8 +2605,7 @@ pub mod tests {
#[tokio::test] #[tokio::test]
async fn test_graph_transpile_user_config() { async fn test_graph_transpile_user_config() {
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/transpile.tsx")
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/transpile.tsx")
.expect("could not resolve module"); .expect("could not resolve module");
let (mut graph, handler) = setup(specifier).await; let (mut graph, handler) = setup(specifier).await;
let result_info = graph let result_info = graph
@ -2667,8 +2654,7 @@ pub mod tests {
..Default::default() ..Default::default()
})); }));
let mut builder = GraphBuilder::new(handler, maybe_import_map, None); let mut builder = GraphBuilder::new(handler, maybe_import_map, None);
let specifier = let specifier = resolve_url_or_path("file:///tests/importremap.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/importremap.ts")
.expect("could not resolve module"); .expect("could not resolve module");
builder.add(&specifier, false).await.expect("could not add"); builder.add(&specifier, false).await.expect("could not add");
builder.get_graph(); builder.get_graph();
@ -2687,8 +2673,7 @@ pub mod tests {
..MockSpecifierHandler::default() ..MockSpecifierHandler::default()
})); }));
let mut builder = GraphBuilder::new(handler.clone(), None, maybe_lockfile); let mut builder = GraphBuilder::new(handler.clone(), None, maybe_lockfile);
let specifier = let specifier = resolve_url_or_path("file:///tests/main.ts")
ModuleSpecifier::resolve_url_or_path("file:///tests/main.ts")
.expect("could not resolve module"); .expect("could not resolve module");
builder builder
.add(&specifier, false) .add(&specifier, false)

View file

@ -76,7 +76,7 @@ impl ModuleLoader for CliModuleLoader {
) -> Result<ModuleSpecifier, AnyError> { ) -> Result<ModuleSpecifier, AnyError> {
// FIXME(bartlomieju): hacky way to provide compatibility with repl // FIXME(bartlomieju): hacky way to provide compatibility with repl
let referrer = if referrer.is_empty() && self.program_state.flags.repl { let referrer = if referrer.is_empty() && self.program_state.flags.repl {
"<unknown>" deno_core::DUMMY_SPECIFIER
} else { } else {
referrer referrer
}; };
@ -90,8 +90,7 @@ impl ModuleLoader for CliModuleLoader {
} }
} }
let module_specifier = let module_specifier = deno_core::resolve_import(specifier, referrer)?;
ModuleSpecifier::resolve_import(specifier, referrer)?;
Ok(module_specifier) Ok(module_specifier)
} }

View file

@ -13,11 +13,11 @@ use deno_core::error::generic_error;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::Context; use deno_core::error::Context;
use deno_core::resolve_url_or_path;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::serde_json::Value; use deno_core::serde_json::Value;
use deno_core::BufVec; use deno_core::BufVec;
use deno_core::ModuleSpecifier;
use deno_core::OpState; use deno_core::OpState;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use serde::Deserialize; use serde::Deserialize;
@ -79,12 +79,10 @@ async fn op_emit(
)?)) )?))
}; };
let maybe_import_map = if let Some(import_map_str) = args.import_map_path { let maybe_import_map = if let Some(import_map_str) = args.import_map_path {
let import_map_specifier = let import_map_specifier = resolve_url_or_path(&import_map_str)
ModuleSpecifier::resolve_url_or_path(&import_map_str)
.context(format!("Bad URL (\"{}\") for import map.", import_map_str))?; .context(format!("Bad URL (\"{}\") for import map.", import_map_str))?;
let import_map_url = import_map_specifier.as_url();
let import_map = if let Some(value) = args.import_map { let import_map = if let Some(value) = args.import_map {
ImportMap::from_json(&import_map_url.to_string(), &value.to_string())? ImportMap::from_json(import_map_specifier.as_str(), &value.to_string())?
} else { } else {
let file = program_state let file = program_state
.file_fetcher .file_fetcher
@ -99,7 +97,7 @@ async fn op_emit(
None None
}; };
let mut builder = GraphBuilder::new(handler, maybe_import_map, None); let mut builder = GraphBuilder::new(handler, maybe_import_map, None);
let root_specifier = ModuleSpecifier::resolve_url_or_path(&root_specifier)?; let root_specifier = resolve_url_or_path(&root_specifier)?;
builder builder
.add(&root_specifier, is_dynamic) .add(&root_specifier, is_dynamic)
.await .await

View file

@ -21,6 +21,7 @@ use deno_core::error::anyhow;
use deno_core::error::get_custom_error_class; use deno_core::error::get_custom_error_class;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::Context; use deno_core::error::Context;
use deno_core::resolve_url;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::ModuleSource; use deno_core::ModuleSource;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
@ -99,7 +100,7 @@ impl ProgramState {
exit_unstable("--import-map") exit_unstable("--import-map")
} }
let import_map_specifier = let import_map_specifier =
ModuleSpecifier::resolve_url_or_path(&import_map_url).context( deno_core::resolve_url_or_path(&import_map_url).context(
format!("Bad URL (\"{}\") for import map.", import_map_url), format!("Bad URL (\"{}\") for import map.", import_map_url),
)?; )?;
let file = file_fetcher let file = file_fetcher
@ -291,8 +292,8 @@ impl ProgramState {
// else, like a refactored file_fetcher. // else, like a refactored file_fetcher.
impl SourceMapGetter for ProgramState { impl SourceMapGetter for ProgramState {
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> { fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
if let Ok(specifier) = ModuleSpecifier::resolve_url(file_name) { if let Ok(specifier) = resolve_url(file_name) {
if let Some((code, maybe_map)) = self.get_emit(&specifier.as_url()) { if let Some((code, maybe_map)) = self.get_emit(&specifier) {
let code = String::from_utf8(code).unwrap(); let code = String::from_utf8(code).unwrap();
source_map_from_code(code).or(maybe_map) source_map_from_code(code).or(maybe_map)
} else if let Ok(source) = self.load(specifier, None) { } else if let Ok(source) = self.load(specifier, None) {
@ -310,7 +311,7 @@ impl SourceMapGetter for ProgramState {
file_name: &str, file_name: &str,
line_number: usize, line_number: usize,
) -> Option<String> { ) -> Option<String> {
if let Ok(specifier) = ModuleSpecifier::resolve_url(file_name) { if let Ok(specifier) = resolve_url(file_name) {
self.file_fetcher.get_source(&specifier).map(|out| { self.file_fetcher.get_source(&specifier).map(|out| {
// Do NOT use .lines(): it skips the terminating empty line. // Do NOT use .lines(): it skips the terminating empty line.
// (due to internally using .split_terminator() instead of .split()) // (due to internally using .split_terminator() instead of .split())

View file

@ -3,7 +3,6 @@
//! This mod provides functions to remap a `JsError` based on a source map. //! This mod provides functions to remap a `JsError` based on a source map.
use deno_core::error::JsError; use deno_core::error::JsError;
use deno_core::ModuleSpecifier;
use sourcemap::SourceMap; use sourcemap::SourceMap;
use std::collections::HashMap; use std::collections::HashMap;
use std::str; use std::str;
@ -129,8 +128,7 @@ pub fn get_orig_position<G: SourceMapGetter>(
// sometimes only the basename of the URL, or has unwanted `<`/`>` // sometimes only the basename of the URL, or has unwanted `<`/`>`
// around it. Use the `file_name` we get from V8 if // around it. Use the `file_name` we get from V8 if
// `source_file_name` does not parse as a URL. // `source_file_name` does not parse as a URL.
let file_name = match ModuleSpecifier::resolve_url(source_file_name) let file_name = match deno_core::resolve_url(source_file_name) {
{
Ok(m) => m.to_string(), Ok(m) => m.to_string(),
Err(_) => file_name, Err(_) => file_name,
}; };

View file

@ -70,7 +70,7 @@ pub struct CachedModule {
impl Default for CachedModule { impl Default for CachedModule {
fn default() -> Self { fn default() -> Self {
let specifier = ModuleSpecifier::resolve_url("file:///example.js").unwrap(); let specifier = deno_core::resolve_url("file:///example.js").unwrap();
CachedModule { CachedModule {
is_remote: false, is_remote: false,
maybe_dependencies: None, maybe_dependencies: None,
@ -304,7 +304,7 @@ impl SpecifierHandler for FetchHandler {
(requested_specifier.clone(), err) (requested_specifier.clone(), err)
} }
})?; })?;
let url = source_file.specifier.as_url(); let url = &source_file.specifier;
let is_remote = !(url.scheme() == "file" || url.scheme() == "data"); let is_remote = !(url.scheme() == "file" || url.scheme() == "data");
let filename = disk_cache.get_cache_filename_with_extension(url, "meta"); let filename = disk_cache.get_cache_filename_with_extension(url, "meta");
let maybe_version = if let Some(filename) = filename { let maybe_version = if let Some(filename) = filename {
@ -371,7 +371,7 @@ impl SpecifierHandler for FetchHandler {
) -> Result<Option<String>, AnyError> { ) -> Result<Option<String>, AnyError> {
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(specifier.as_url(), "buildinfo"); .get_cache_filename_with_extension(specifier, "buildinfo");
if let Some(filename) = filename { if let Some(filename) = filename {
if let Ok(tsbuildinfo) = self.disk_cache.get(&filename) { if let Ok(tsbuildinfo) = self.disk_cache.get(&filename) {
Ok(Some(String::from_utf8(tsbuildinfo)?)) Ok(Some(String::from_utf8(tsbuildinfo)?))
@ -390,7 +390,7 @@ impl SpecifierHandler for FetchHandler {
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(specifier.as_url(), "buildinfo") .get_cache_filename_with_extension(specifier, "buildinfo")
.unwrap(); .unwrap();
debug!("set_tsbuildinfo - filename {:?}", filename); debug!("set_tsbuildinfo - filename {:?}", filename);
self self
@ -406,17 +406,16 @@ impl SpecifierHandler for FetchHandler {
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
match emit { match emit {
Emit::Cli((code, maybe_map)) => { Emit::Cli((code, maybe_map)) => {
let url = specifier.as_url();
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(url, "js") .get_cache_filename_with_extension(specifier, "js")
.unwrap(); .unwrap();
self.disk_cache.set(&filename, code.as_bytes())?; self.disk_cache.set(&filename, code.as_bytes())?;
if let Some(map) = maybe_map { if let Some(map) = maybe_map {
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(url, "js.map") .get_cache_filename_with_extension(specifier, "js.map")
.unwrap(); .unwrap();
self.disk_cache.set(&filename, map.as_bytes())?; self.disk_cache.set(&filename, map.as_bytes())?;
} }
@ -452,7 +451,7 @@ impl SpecifierHandler for FetchHandler {
let compiled_file_metadata = CompiledFileMetadata { version_hash }; let compiled_file_metadata = CompiledFileMetadata { version_hash };
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(specifier.as_url(), "meta") .get_cache_filename_with_extension(specifier, "meta")
.unwrap(); .unwrap();
self self
@ -492,7 +491,7 @@ impl SpecifierHandler for MemoryHandler {
} }
let result = if let Some(source) = self.sources.get(&specifier_text) { let result = if let Some(source) = self.sources.get(&specifier_text) {
let media_type = MediaType::from(&specifier); let media_type = MediaType::from(&specifier);
let is_remote = specifier.as_url().scheme() != "file"; let is_remote = specifier.scheme() != "file";
Ok(CachedModule { Ok(CachedModule {
source: source.to_string(), source: source.to_string(),
@ -568,6 +567,7 @@ pub mod tests {
use super::*; use super::*;
use crate::file_fetcher::CacheSetting; use crate::file_fetcher::CacheSetting;
use crate::http_cache::HttpCache; use crate::http_cache::HttpCache;
use deno_core::resolve_url_or_path;
use tempfile::TempDir; use tempfile::TempDir;
macro_rules! map ( macro_rules! map (
@ -609,9 +609,8 @@ pub mod tests {
async fn test_fetch_handler_fetch() { async fn test_fetch_handler_fetch() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (_, mut file_fetcher) = setup(); let (_, mut file_fetcher) = setup();
let specifier = ModuleSpecifier::resolve_url_or_path( let specifier =
"http://localhost:4545/cli/tests/subdir/mod2.ts", resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
)
.unwrap(); .unwrap();
let cached_module: CachedModule = file_fetcher let cached_module: CachedModule = file_fetcher
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
@ -631,9 +630,8 @@ pub mod tests {
async fn test_fetch_handler_set_cache() { async fn test_fetch_handler_set_cache() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (_, mut file_fetcher) = setup(); let (_, mut file_fetcher) = setup();
let specifier = ModuleSpecifier::resolve_url_or_path( let specifier =
"http://localhost:4545/cli/tests/subdir/mod2.ts", resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
)
.unwrap(); .unwrap();
let cached_module: CachedModule = file_fetcher let cached_module: CachedModule = file_fetcher
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
@ -658,15 +656,14 @@ pub mod tests {
async fn test_fetch_handler_is_remote() { async fn test_fetch_handler_is_remote() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let (_, mut file_fetcher) = setup(); let (_, mut file_fetcher) = setup();
let specifier = ModuleSpecifier::resolve_url_or_path( let specifier =
"http://localhost:4545/cli/tests/subdir/mod2.ts", resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
)
.unwrap(); .unwrap();
let cached_module: CachedModule = let cached_module: CachedModule =
file_fetcher.fetch(specifier, None, false).await.unwrap(); file_fetcher.fetch(specifier, None, false).await.unwrap();
assert_eq!(cached_module.is_remote, true); assert_eq!(cached_module.is_remote, true);
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let specifier = ModuleSpecifier::resolve_url_or_path( let specifier = resolve_url_or_path(
c.join("tests/subdir/mod1.ts").as_os_str().to_str().unwrap(), c.join("tests/subdir/mod1.ts").as_os_str().to_str().unwrap(),
) )
.unwrap(); .unwrap();
@ -701,8 +698,7 @@ pub mod tests {
.map(|(k, v)| (k.to_string(), v.to_string())) .map(|(k, v)| (k.to_string(), v.to_string()))
.collect(); .collect();
let mut handler = MemoryHandler::new(sources); let mut handler = MemoryHandler::new(sources);
let specifier = let specifier = resolve_url_or_path("file:///a.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///a.ts").unwrap();
let actual: CachedModule = handler let actual: CachedModule = handler
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
.await .await
@ -713,8 +709,7 @@ pub mod tests {
assert_eq!(actual.media_type, MediaType::TypeScript); assert_eq!(actual.media_type, MediaType::TypeScript);
assert_eq!(actual.is_remote, false); assert_eq!(actual.is_remote, false);
let specifier = let specifier = resolve_url_or_path("file:///b.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///b.ts").unwrap();
let actual: CachedModule = handler let actual: CachedModule = handler
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
.await .await
@ -725,8 +720,7 @@ pub mod tests {
assert_eq!(actual.media_type, MediaType::TypeScript); assert_eq!(actual.media_type, MediaType::TypeScript);
assert_eq!(actual.is_remote, false); assert_eq!(actual.is_remote, false);
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/c.js").unwrap();
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/c.js").unwrap();
let actual: CachedModule = handler let actual: CachedModule = handler
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
.await .await
@ -737,9 +731,7 @@ pub mod tests {
assert_eq!(actual.media_type, MediaType::JavaScript); assert_eq!(actual.media_type, MediaType::JavaScript);
assert_eq!(actual.is_remote, true); assert_eq!(actual.is_remote, true);
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/d.d.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/d.d.ts")
.unwrap();
let actual: CachedModule = handler let actual: CachedModule = handler
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
.await .await
@ -751,14 +743,13 @@ pub mod tests {
assert_eq!(actual.is_remote, true); assert_eq!(actual.is_remote, true);
let specifier = let specifier =
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/missing.ts") resolve_url_or_path("https://deno.land/x/missing.ts").unwrap();
.unwrap();
handler handler
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
.await .await
.expect_err("should have errored"); .expect_err("should have errored");
let specifier = ModuleSpecifier::resolve_url_or_path("/a.ts").unwrap(); let specifier = resolve_url_or_path("/a.ts").unwrap();
let actual: CachedModule = handler let actual: CachedModule = handler
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
.await .await
@ -769,8 +760,7 @@ pub mod tests {
assert_eq!(actual.media_type, MediaType::TypeScript); assert_eq!(actual.media_type, MediaType::TypeScript);
assert_eq!(actual.is_remote, false); assert_eq!(actual.is_remote, false);
let specifier = let specifier = resolve_url_or_path("file:///C:/a.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///C:/a.ts").unwrap();
let actual: CachedModule = handler let actual: CachedModule = handler
.fetch(specifier.clone(), None, false) .fetch(specifier.clone(), None, false)
.await .await

View file

@ -6,6 +6,7 @@ use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::Context; use deno_core::error::Context;
use deno_core::futures::FutureExt; use deno_core::futures::FutureExt;
use deno_core::resolve_url;
use deno_core::serde::Deserialize; use deno_core::serde::Deserialize;
use deno_core::serde::Serialize; use deno_core::serde::Serialize;
use deno_core::serde_json; use deno_core::serde_json;
@ -123,7 +124,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
"Self-contained binaries don't support module loading", "Self-contained binaries don't support module loading",
)); ));
} }
Ok(ModuleSpecifier::resolve_url(specifier)?) Ok(resolve_url(specifier)?)
} }
fn load( fn load(
@ -155,7 +156,7 @@ pub async fn run(
source_code: String, source_code: String,
metadata: Metadata, metadata: Metadata,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let main_module = ModuleSpecifier::resolve_url(SPECIFIER)?; let main_module = resolve_url(SPECIFIER)?;
let permissions = Permissions::from_options(&metadata.permissions); let permissions = Permissions::from_options(&metadata.permissions);
let module_loader = Rc::new(EmbeddedModuleLoader(source_code)); let module_loader = Rc::new(EmbeddedModuleLoader(source_code));
let create_web_worker_cb = Arc::new(|_| { let create_web_worker_cb = Arc::new(|_| {

View file

@ -11,7 +11,6 @@ use deno_core::error::AnyError;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::ModuleSpecifier;
use deno_runtime::inspector::InspectorSession; use deno_runtime::inspector::InspectorSession;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use serde::Deserialize; use serde::Deserialize;
@ -389,7 +388,7 @@ pub async fn report_coverages(
let mut coverage_reporter = PrettyCoverageReporter::new(quiet); let mut coverage_reporter = PrettyCoverageReporter::new(quiet);
for script_coverage in coverages { for script_coverage in coverages {
let module_specifier = let module_specifier =
ModuleSpecifier::resolve_url_or_path(&script_coverage.url)?; deno_core::resolve_url_or_path(&script_coverage.url)?;
program_state program_state
.prepare_module_load( .prepare_module_load(
module_specifier.clone(), module_specifier.clone(),

View file

@ -11,6 +11,7 @@ use deno_core::error::bail;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::Context; use deno_core::error::Context;
use deno_core::json_op_sync; use deno_core::json_op_sync;
use deno_core::resolve_url_or_path;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::serde_json::Value; use deno_core::serde_json::Value;
@ -96,11 +97,11 @@ fn hash_data_url(
media_type: &MediaType, media_type: &MediaType,
) -> String { ) -> String {
assert_eq!( assert_eq!(
specifier.as_url().scheme(), specifier.scheme(),
"data", "data",
"Specifier must be a data: specifier." "Specifier must be a data: specifier."
); );
let hash = crate::checksum::gen(&[specifier.as_url().path().as_bytes()]); let hash = crate::checksum::gen(&[specifier.path().as_bytes()]);
format!("data:///{}{}", hash, media_type.as_ts_extension()) format!("data:///{}{}", hash, media_type.as_ts_extension())
} }
@ -108,15 +109,14 @@ fn hash_data_url(
/// and so we have to detect the apparent media type based on extensions it /// and so we have to detect the apparent media type based on extensions it
/// supports. /// supports.
fn get_tsc_media_type(specifier: &ModuleSpecifier) -> MediaType { fn get_tsc_media_type(specifier: &ModuleSpecifier) -> MediaType {
let url = specifier.as_url(); let path = if specifier.scheme() == "file" {
let path = if url.scheme() == "file" { if let Ok(path) = specifier.to_file_path() {
if let Ok(path) = url.to_file_path() {
path path
} else { } else {
PathBuf::from(url.path()) PathBuf::from(specifier.path())
} }
} else { } else {
PathBuf::from(url.path()) PathBuf::from(specifier.path())
}; };
match path.extension() { match path.extension() {
None => MediaType::Unknown, None => MediaType::Unknown,
@ -262,7 +262,7 @@ fn emit(state: &mut State, args: Value) -> Result<Value, AnyError> {
} else if let Some(remapped_specifier) = state.root_map.get(s) { } else if let Some(remapped_specifier) = state.root_map.get(s) {
remapped_specifier.clone() remapped_specifier.clone()
} else { } else {
ModuleSpecifier::resolve_url_or_path(s).unwrap() resolve_url_or_path(s).unwrap()
} }
}) })
.collect(); .collect();
@ -286,7 +286,7 @@ struct LoadArgs {
fn load(state: &mut State, args: Value) -> Result<Value, AnyError> { fn load(state: &mut State, args: Value) -> Result<Value, AnyError> {
let v: LoadArgs = serde_json::from_value(args) let v: LoadArgs = serde_json::from_value(args)
.context("Invalid request from JavaScript for \"op_load\".")?; .context("Invalid request from JavaScript for \"op_load\".")?;
let specifier = ModuleSpecifier::resolve_url_or_path(&v.specifier) let specifier = resolve_url_or_path(&v.specifier)
.context("Error converting a string module specifier for \"op_load\".")?; .context("Error converting a string module specifier for \"op_load\".")?;
let mut hash: Option<String> = None; let mut hash: Option<String> = None;
let mut media_type = MediaType::Unknown; let mut media_type = MediaType::Unknown;
@ -349,7 +349,7 @@ fn resolve(state: &mut State, args: Value) -> Result<Value, AnyError> {
} else if let Some(remapped_base) = state.root_map.get(&v.base) { } else if let Some(remapped_base) = state.root_map.get(&v.base) {
remapped_base.clone() remapped_base.clone()
} else { } else {
ModuleSpecifier::resolve_url_or_path(&v.base).context( resolve_url_or_path(&v.base).context(
"Error converting a string module specifier for \"op_resolve\".", "Error converting a string module specifier for \"op_resolve\".",
)? )?
}; };
@ -373,8 +373,7 @@ fn resolve(state: &mut State, args: Value) -> Result<Value, AnyError> {
resolved_specifier resolved_specifier
) )
}; };
let resolved_specifier_str = if resolved_specifier.as_url().scheme() let resolved_specifier_str = if resolved_specifier.scheme() == "data"
== "data"
{ {
let specifier_str = hash_data_url(&resolved_specifier, &media_type); let specifier_str = hash_data_url(&resolved_specifier, &media_type);
state state
@ -433,7 +432,7 @@ pub fn exec(request: Request) -> Result<Response, AnyError> {
.root_names .root_names
.iter() .iter()
.map(|(s, mt)| { .map(|(s, mt)| {
if s.as_url().scheme() == "data" { if s.scheme() == "data" {
let specifier_str = hash_data_url(s, mt); let specifier_str = hash_data_url(s, mt);
data_url_map.insert(specifier_str.clone(), s.clone()); data_url_map.insert(specifier_str.clone(), s.clone());
specifier_str specifier_str
@ -520,9 +519,8 @@ mod tests {
maybe_hash_data: Option<Vec<Vec<u8>>>, maybe_hash_data: Option<Vec<Vec<u8>>>,
maybe_tsbuildinfo: Option<String>, maybe_tsbuildinfo: Option<String>,
) -> State { ) -> State {
let specifier = maybe_specifier.unwrap_or_else(|| { let specifier = maybe_specifier
ModuleSpecifier::resolve_url_or_path("file:///main.ts").unwrap() .unwrap_or_else(|| resolve_url_or_path("file:///main.ts").unwrap());
});
let hash_data = maybe_hash_data.unwrap_or_else(|| vec![b"".to_vec()]); let hash_data = maybe_hash_data.unwrap_or_else(|| vec![b"".to_vec()]);
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let fixtures = c.join("tests/tsc2"); let fixtures = c.join("tests/tsc2");
@ -619,7 +617,7 @@ mod tests {
#[test] #[test]
fn test_hash_data_url() { fn test_hash_data_url() {
let specifier = ModuleSpecifier::resolve_url( let specifier = deno_core::resolve_url(
"data:application/javascript,console.log(\"Hello%20Deno\");", "data:application/javascript,console.log(\"Hello%20Deno\");",
) )
.unwrap(); .unwrap();
@ -642,7 +640,7 @@ mod tests {
("file:///.tsbuildinfo", MediaType::Unknown), ("file:///.tsbuildinfo", MediaType::Unknown),
]; ];
for (specifier, media_type) in fixtures { for (specifier, media_type) in fixtures {
let specifier = ModuleSpecifier::resolve_url_or_path(specifier).unwrap(); let specifier = resolve_url_or_path(specifier).unwrap();
assert_eq!(get_tsc_media_type(&specifier), media_type); assert_eq!(get_tsc_media_type(&specifier), media_type);
} }
} }
@ -666,7 +664,7 @@ mod tests {
state.emitted_files[0], state.emitted_files[0],
EmittedFile { EmittedFile {
data: "some file content".to_string(), data: "some file content".to_string(),
maybe_specifiers: Some(vec![ModuleSpecifier::resolve_url_or_path( maybe_specifiers: Some(vec![resolve_url_or_path(
"file:///some/file.ts" "file:///some/file.ts"
) )
.unwrap()]), .unwrap()]),
@ -697,10 +695,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_load() { async fn test_load() {
let mut state = setup( let mut state = setup(
Some( Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/mod.ts")
.unwrap(),
),
None, None,
Some("some content".to_string()), Some("some content".to_string()),
) )
@ -731,10 +726,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_load_asset() { async fn test_load_asset() {
let mut state = setup( let mut state = setup(
Some( Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/mod.ts")
.unwrap(),
),
None, None,
Some("some content".to_string()), Some("some content".to_string()),
) )
@ -753,10 +745,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_load_tsbuildinfo() { async fn test_load_tsbuildinfo() {
let mut state = setup( let mut state = setup(
Some( Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/mod.ts")
.unwrap(),
),
None, None,
Some("some content".to_string()), Some("some content".to_string()),
) )
@ -795,10 +784,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_resolve() { async fn test_resolve() {
let mut state = setup( let mut state = setup(
Some( Some(resolve_url_or_path("https://deno.land/x/a.ts").unwrap()),
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/a.ts")
.unwrap(),
),
None, None,
None, None,
) )
@ -814,10 +800,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_resolve_empty() { async fn test_resolve_empty() {
let mut state = setup( let mut state = setup(
Some( Some(resolve_url_or_path("https://deno.land/x/a.ts").unwrap()),
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/a.ts")
.unwrap(),
),
None, None,
None, None,
) )
@ -874,8 +857,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_exec_basic() { async fn test_exec_basic() {
let specifier = let specifier = resolve_url_or_path("https://deno.land/x/a.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("https://deno.land/x/a.ts").unwrap();
let actual = test_exec(&specifier) let actual = test_exec(&specifier)
.await .await
.expect("exec should not have errored"); .expect("exec should not have errored");
@ -887,8 +869,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_exec_reexport_dts() { async fn test_exec_reexport_dts() {
let specifier = let specifier = resolve_url_or_path("file:///reexports.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///reexports.ts").unwrap();
let actual = test_exec(&specifier) let actual = test_exec(&specifier)
.await .await
.expect("exec should not have errored"); .expect("exec should not have errored");
@ -900,8 +881,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn fix_lib_ref() { async fn fix_lib_ref() {
let specifier = let specifier = resolve_url_or_path("file:///libref.ts").unwrap();
ModuleSpecifier::resolve_url_or_path("file:///libref.ts").unwrap();
let actual = test_exec(&specifier) let actual = test_exec(&specifier)
.await .await
.expect("exec should not have errored"); .expect("exec should not have errored");

View file

@ -454,8 +454,10 @@ fn eval_context(
} }
*/ */
let tc_scope = &mut v8::TryCatch::new(scope); let tc_scope = &mut v8::TryCatch::new(scope);
let name = let name = v8::String::new(
v8::String::new(tc_scope, url.as_ref().map_or("<unknown>", Url::as_str)) tc_scope,
url.as_ref().map_or(crate::DUMMY_SPECIFIER, Url::as_str),
)
.unwrap(); .unwrap();
let origin = script_origin(tc_scope, name); let origin = script_origin(tc_scope, name);
let maybe_script = v8::Script::compile(tc_scope, source, Some(&origin)); let maybe_script = v8::Script::compile(tc_scope, source, Some(&origin));

View file

@ -42,8 +42,13 @@ pub use crate::async_cell::AsyncRefFuture;
pub use crate::async_cell::RcLike; pub use crate::async_cell::RcLike;
pub use crate::async_cell::RcRef; pub use crate::async_cell::RcRef;
pub use crate::flags::v8_set_flags; pub use crate::flags::v8_set_flags;
pub use crate::module_specifier::resolve_import;
pub use crate::module_specifier::resolve_path;
pub use crate::module_specifier::resolve_url;
pub use crate::module_specifier::resolve_url_or_path;
pub use crate::module_specifier::ModuleResolutionError; pub use crate::module_specifier::ModuleResolutionError;
pub use crate::module_specifier::ModuleSpecifier; pub use crate::module_specifier::ModuleSpecifier;
pub use crate::module_specifier::DUMMY_SPECIFIER;
pub use crate::modules::FsModuleLoader; pub use crate::modules::FsModuleLoader;
pub use crate::modules::ModuleId; pub use crate::modules::ModuleId;
pub use crate::modules::ModuleLoadId; pub use crate::modules::ModuleLoadId;

View file

@ -1,9 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use crate::normalize_path; use crate::normalize_path;
use serde::de;
use serde::Deserialize;
use serde::Deserializer;
use std::env::current_dir; use std::env::current_dir;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
@ -11,6 +8,8 @@ use std::path::PathBuf;
use url::ParseError; use url::ParseError;
use url::Url; use url::Url;
pub const DUMMY_SPECIFIER: &str = "<unknown>";
/// Error indicating the reason resolving a module specifier failed. /// Error indicating the reason resolving a module specifier failed.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum ModuleResolutionError { pub enum ModuleResolutionError {
@ -51,24 +50,8 @@ impl fmt::Display for ModuleResolutionError {
} }
} }
#[derive(
Debug, Clone, Eq, Hash, PartialEq, serde::Serialize, Ord, PartialOrd,
)]
/// Resolved module specifier /// Resolved module specifier
pub struct ModuleSpecifier(Url); pub type ModuleSpecifier = Url;
impl ModuleSpecifier {
fn is_dummy_specifier(specifier: &str) -> bool {
specifier == "<unknown>"
}
pub fn as_url(&self) -> &Url {
&self.0
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
/// Resolves module using this algorithm: /// Resolves module using this algorithm:
/// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier /// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
@ -101,7 +84,7 @@ impl ModuleSpecifier {
// 3. Return the result of applying the URL parser to specifier with base // 3. Return the result of applying the URL parser to specifier with base
// URL as the base URL. // URL as the base URL.
Err(ParseError::RelativeUrlWithoutBase) => { Err(ParseError::RelativeUrlWithoutBase) => {
let base = if ModuleSpecifier::is_dummy_specifier(base) { let base = if base == DUMMY_SPECIFIER {
// Handle <unknown> case, happening under e.g. repl. // Handle <unknown> case, happening under e.g. repl.
// Use CWD for such case. // Use CWD for such case.
@ -123,16 +106,14 @@ impl ModuleSpecifier {
Err(err) => return Err(InvalidUrl(err)), Err(err) => return Err(InvalidUrl(err)),
}; };
Ok(ModuleSpecifier(url)) Ok(url)
} }
/// Converts a string representing an absolute URL into a ModuleSpecifier. /// Converts a string representing an absolute URL into a ModuleSpecifier.
pub fn resolve_url( pub fn resolve_url(
url_str: &str, url_str: &str,
) -> Result<ModuleSpecifier, ModuleResolutionError> { ) -> Result<ModuleSpecifier, ModuleResolutionError> {
Url::parse(url_str) Url::parse(url_str).map_err(ModuleResolutionError::InvalidUrl)
.map(ModuleSpecifier)
.map_err(ModuleResolutionError::InvalidUrl)
} }
/// Takes a string representing either an absolute URL or a file path, /// Takes a string representing either an absolute URL or a file path,
@ -144,10 +125,10 @@ impl ModuleSpecifier {
pub fn resolve_url_or_path( pub fn resolve_url_or_path(
specifier: &str, specifier: &str,
) -> Result<ModuleSpecifier, ModuleResolutionError> { ) -> Result<ModuleSpecifier, ModuleResolutionError> {
if Self::specifier_has_uri_scheme(specifier) { if specifier_has_uri_scheme(specifier) {
Self::resolve_url(specifier) resolve_url(specifier)
} else { } else {
Self::resolve_path(specifier) resolve_path(specifier)
} }
} }
@ -160,7 +141,6 @@ impl ModuleSpecifier {
let path = current_dir().unwrap().join(path_str); let path = current_dir().unwrap().join(path_str);
let path = normalize_path(&path); let path = normalize_path(&path);
Url::from_file_path(path.clone()) Url::from_file_path(path.clone())
.map(ModuleSpecifier)
.map_err(|()| ModuleResolutionError::InvalidPath(path)) .map_err(|()| ModuleResolutionError::InvalidPath(path))
} }
@ -192,35 +172,6 @@ impl ModuleSpecifier {
} }
} }
} }
}
impl fmt::Display for ModuleSpecifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<Url> for ModuleSpecifier {
fn from(url: Url) -> Self {
ModuleSpecifier(url)
}
}
impl PartialEq<String> for ModuleSpecifier {
fn eq(&self, other: &String) -> bool {
&self.to_string() == other
}
}
impl<'de> Deserialize<'de> for ModuleSpecifier {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let url_str: String = Deserialize::deserialize(deserializer)?;
ModuleSpecifier::resolve_url(&url_str).map_err(de::Error::custom)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -306,9 +257,7 @@ mod tests {
]; ];
for (specifier, base, expected_url) in tests { for (specifier, base, expected_url) in tests {
let url = ModuleSpecifier::resolve_import(specifier, base) let url = resolve_import(specifier, base).unwrap().to_string();
.unwrap()
.to_string();
assert_eq!(url, expected_url); assert_eq!(url, expected_url);
} }
} }
@ -385,7 +334,7 @@ mod tests {
]; ];
for (specifier, base, expected_err) in tests { for (specifier, base, expected_err) in tests {
let err = ModuleSpecifier::resolve_import(specifier, base).unwrap_err(); let err = resolve_import(specifier, base).unwrap_err();
assert_eq!(err, expected_err); assert_eq!(err, expected_err);
} }
} }
@ -487,9 +436,7 @@ mod tests {
} }
for (specifier, expected_url) in tests { for (specifier, expected_url) in tests {
let url = ModuleSpecifier::resolve_url_or_path(specifier) let url = resolve_url_or_path(specifier).unwrap().to_string();
.unwrap()
.to_string();
assert_eq!(url, expected_url); assert_eq!(url, expected_url);
} }
} }
@ -509,7 +456,7 @@ mod tests {
} }
for (specifier, expected_err) in tests { for (specifier, expected_err) in tests {
let err = ModuleSpecifier::resolve_url_or_path(specifier).unwrap_err(); let err = resolve_url_or_path(specifier).unwrap_err();
assert_eq!(err, expected_err); assert_eq!(err, expected_err);
} }
} }
@ -539,7 +486,7 @@ mod tests {
]; ];
for (specifier, expected) in tests { for (specifier, expected) in tests {
let result = ModuleSpecifier::specifier_has_uri_scheme(specifier); let result = specifier_has_uri_scheme(specifier);
assert_eq!(result, expected); assert_eq!(result, expected);
} }
} }
@ -565,8 +512,7 @@ mod tests {
fn test_deserialize_module_specifier() { fn test_deserialize_module_specifier() {
let actual: ModuleSpecifier = let actual: ModuleSpecifier =
from_value(json!("http://deno.land/x/mod.ts")).unwrap(); from_value(json!("http://deno.land/x/mod.ts")).unwrap();
let expected = let expected = resolve_url("http://deno.land/x/mod.ts").unwrap();
ModuleSpecifier::resolve_url("http://deno.land/x/mod.ts").unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
} }

View file

@ -145,7 +145,7 @@ impl ModuleLoader for FsModuleLoader {
referrer: &str, referrer: &str,
_is_main: bool, _is_main: bool,
) -> Result<ModuleSpecifier, AnyError> { ) -> Result<ModuleSpecifier, AnyError> {
Ok(ModuleSpecifier::resolve_import(specifier, referrer)?) Ok(crate::resolve_import(specifier, referrer)?)
} }
fn load( fn load(
@ -157,7 +157,7 @@ impl ModuleLoader for FsModuleLoader {
) -> Pin<Box<ModuleSourceFuture>> { ) -> Pin<Box<ModuleSourceFuture>> {
let module_specifier = module_specifier.clone(); let module_specifier = module_specifier.clone();
async move { async move {
let path = module_specifier.as_url().to_file_path().map_err(|_| { let path = module_specifier.to_file_path().map_err(|_| {
generic_error(format!( generic_error(format!(
"Provided module specifier \"{}\" is not a file URL.", "Provided module specifier \"{}\" is not a file URL.",
module_specifier module_specifier
@ -649,8 +649,7 @@ mod tests {
eprintln!(">> RESOLVING, S: {}, R: {}", specifier, referrer); eprintln!(">> RESOLVING, S: {}, R: {}", specifier, referrer);
let output_specifier = let output_specifier = match crate::resolve_import(specifier, referrer) {
match ModuleSpecifier::resolve_import(specifier, referrer) {
Ok(specifier) => specifier, Ok(specifier) => specifier,
Err(..) => return Err(MockError::ResolveErr.into()), Err(..) => return Err(MockError::ResolveErr.into()),
}; };
@ -715,7 +714,7 @@ mod tests {
module_loader: Some(loader), module_loader: Some(loader),
..Default::default() ..Default::default()
}); });
let spec = ModuleSpecifier::resolve_url("file:///a.js").unwrap(); let spec = crate::resolve_url("file:///a.js").unwrap();
let a_id_fut = runtime.load_module(&spec, None); let a_id_fut = runtime.load_module(&spec, None);
let a_id = futures::executor::block_on(a_id_fut).expect("Failed to load"); let a_id = futures::executor::block_on(a_id_fut).expect("Failed to load");
@ -741,17 +740,17 @@ mod tests {
assert_eq!( assert_eq!(
modules.get_children(a_id), modules.get_children(a_id),
Some(&vec![ Some(&vec![
ModuleSpecifier::resolve_url("file:///b.js").unwrap(), crate::resolve_url("file:///b.js").unwrap(),
ModuleSpecifier::resolve_url("file:///c.js").unwrap() crate::resolve_url("file:///c.js").unwrap()
]) ])
); );
assert_eq!( assert_eq!(
modules.get_children(b_id), modules.get_children(b_id),
Some(&vec![ModuleSpecifier::resolve_url("file:///c.js").unwrap()]) Some(&vec![crate::resolve_url("file:///c.js").unwrap()])
); );
assert_eq!( assert_eq!(
modules.get_children(c_id), modules.get_children(c_id),
Some(&vec![ModuleSpecifier::resolve_url("file:///d.js").unwrap()]) Some(&vec![crate::resolve_url("file:///d.js").unwrap()])
); );
assert_eq!(modules.get_children(d_id), Some(&vec![])); assert_eq!(modules.get_children(d_id), Some(&vec![]));
} }
@ -782,7 +781,7 @@ mod tests {
}); });
let fut = async move { let fut = async move {
let spec = ModuleSpecifier::resolve_url("file:///circular1.js").unwrap(); let spec = crate::resolve_url("file:///circular1.js").unwrap();
let result = runtime.load_module(&spec, None).await; let result = runtime.load_module(&spec, None).await;
assert!(result.is_ok()); assert!(result.is_ok());
let circular1_id = result.unwrap(); let circular1_id = result.unwrap();
@ -807,16 +806,12 @@ mod tests {
assert_eq!( assert_eq!(
modules.get_children(circular1_id), modules.get_children(circular1_id),
Some(&vec![ Some(&vec![crate::resolve_url("file:///circular2.js").unwrap()])
ModuleSpecifier::resolve_url("file:///circular2.js").unwrap()
])
); );
assert_eq!( assert_eq!(
modules.get_children(circular2_id), modules.get_children(circular2_id),
Some(&vec![ Some(&vec![crate::resolve_url("file:///circular3.js").unwrap()])
ModuleSpecifier::resolve_url("file:///circular3.js").unwrap()
])
); );
assert!(modules.get_id("file:///circular3.js").is_some()); assert!(modules.get_id("file:///circular3.js").is_some());
@ -824,8 +819,8 @@ mod tests {
assert_eq!( assert_eq!(
modules.get_children(circular3_id), modules.get_children(circular3_id),
Some(&vec![ Some(&vec![
ModuleSpecifier::resolve_url("file:///circular1.js").unwrap(), crate::resolve_url("file:///circular1.js").unwrap(),
ModuleSpecifier::resolve_url("file:///circular2.js").unwrap() crate::resolve_url("file:///circular2.js").unwrap()
]) ])
); );
} }
@ -858,7 +853,7 @@ mod tests {
}); });
let fut = async move { let fut = async move {
let spec = ModuleSpecifier::resolve_url("file:///redirect1.js").unwrap(); let spec = crate::resolve_url("file:///redirect1.js").unwrap();
let result = runtime.load_module(&spec, None).await; let result = runtime.load_module(&spec, None).await;
println!(">> result {:?}", result); println!(">> result {:?}", result);
assert!(result.is_ok()); assert!(result.is_ok());
@ -923,7 +918,7 @@ mod tests {
module_loader: Some(loader), module_loader: Some(loader),
..Default::default() ..Default::default()
}); });
let spec = ModuleSpecifier::resolve_url("file:///main.js").unwrap(); let spec = crate::resolve_url("file:///main.js").unwrap();
let mut recursive_load = runtime.load_module(&spec, None).boxed_local(); let mut recursive_load = runtime.load_module(&spec, None).boxed_local();
let result = recursive_load.poll_unpin(&mut cx); let result = recursive_load.poll_unpin(&mut cx);
@ -971,7 +966,7 @@ mod tests {
module_loader: Some(loader), module_loader: Some(loader),
..Default::default() ..Default::default()
}); });
let spec = ModuleSpecifier::resolve_url("file:///bad_import.js").unwrap(); let spec = crate::resolve_url("file:///bad_import.js").unwrap();
let mut load_fut = runtime.load_module(&spec, None).boxed_local(); let mut load_fut = runtime.load_module(&spec, None).boxed_local();
let result = load_fut.poll_unpin(&mut cx); let result = load_fut.poll_unpin(&mut cx);
if let Poll::Ready(Err(err)) = result { if let Poll::Ready(Err(err)) = result {
@ -1005,8 +1000,7 @@ mod tests {
// In default resolution code should be empty. // In default resolution code should be empty.
// Instead we explicitly pass in our own code. // Instead we explicitly pass in our own code.
// The behavior should be very similar to /a.js. // The behavior should be very similar to /a.js.
let spec = let spec = crate::resolve_url("file:///main_with_code.js").unwrap();
ModuleSpecifier::resolve_url("file:///main_with_code.js").unwrap();
let main_id_fut = runtime let main_id_fut = runtime
.load_module(&spec, Some(MAIN_WITH_CODE_SRC.to_owned())) .load_module(&spec, Some(MAIN_WITH_CODE_SRC.to_owned()))
.boxed_local(); .boxed_local();
@ -1033,17 +1027,17 @@ mod tests {
assert_eq!( assert_eq!(
modules.get_children(main_id), modules.get_children(main_id),
Some(&vec![ Some(&vec![
ModuleSpecifier::resolve_url("file:///b.js").unwrap(), crate::resolve_url("file:///b.js").unwrap(),
ModuleSpecifier::resolve_url("file:///c.js").unwrap() crate::resolve_url("file:///c.js").unwrap()
]) ])
); );
assert_eq!( assert_eq!(
modules.get_children(b_id), modules.get_children(b_id),
Some(&vec![ModuleSpecifier::resolve_url("file:///c.js").unwrap()]) Some(&vec![crate::resolve_url("file:///c.js").unwrap()])
); );
assert_eq!( assert_eq!(
modules.get_children(c_id), modules.get_children(c_id),
Some(&vec![ModuleSpecifier::resolve_url("file:///d.js").unwrap()]) Some(&vec![crate::resolve_url("file:///d.js").unwrap()])
); );
assert_eq!(modules.get_children(d_id), Some(&vec![])); assert_eq!(modules.get_children(d_id), Some(&vec![]));
} }

View file

@ -1224,8 +1224,7 @@ impl JsRuntime {
let is_main = let is_main =
load.state == LoadState::LoadingRoot && !load.is_dynamic_import(); load.state == LoadState::LoadingRoot && !load.is_dynamic_import();
let referrer_specifier = let referrer_specifier = crate::resolve_url(&module_url_found).unwrap();
ModuleSpecifier::resolve_url(&module_url_found).unwrap();
let state_rc = Self::state(self.v8_isolate()); let state_rc = Self::state(self.v8_isolate());
// #A There are 3 cases to handle at this moment: // #A There are 3 cases to handle at this moment:
@ -2200,7 +2199,7 @@ pub mod tests {
self.count.fetch_add(1, Ordering::Relaxed); self.count.fetch_add(1, Ordering::Relaxed);
assert_eq!(specifier, "./b.js"); assert_eq!(specifier, "./b.js");
assert_eq!(referrer, "file:///a.js"); assert_eq!(referrer, "file:///a.js");
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap(); let s = crate::resolve_import(specifier, referrer).unwrap();
Ok(s) Ok(s)
} }
@ -2272,7 +2271,7 @@ pub mod tests {
let imports = state.modules.get_children(mod_a); let imports = state.modules.get_children(mod_a);
assert_eq!( assert_eq!(
imports, imports,
Some(&vec![ModuleSpecifier::resolve_url("file:///b.js").unwrap()]) Some(&vec![crate::resolve_url("file:///b.js").unwrap()])
); );
} }
let mod_b = runtime let mod_b = runtime
@ -2313,7 +2312,7 @@ pub mod tests {
self.count.fetch_add(1, Ordering::Relaxed); self.count.fetch_add(1, Ordering::Relaxed);
assert_eq!(specifier, "/foo.js"); assert_eq!(specifier, "/foo.js");
assert_eq!(referrer, "file:///dyn_import2.js"); assert_eq!(referrer, "file:///dyn_import2.js");
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap(); let s = crate::resolve_import(specifier, referrer).unwrap();
Ok(s) Ok(s)
} }
@ -2377,7 +2376,7 @@ pub mod tests {
assert!(c < 4); assert!(c < 4);
assert_eq!(specifier, "./b.js"); assert_eq!(specifier, "./b.js");
assert_eq!(referrer, "file:///dyn_import3.js"); assert_eq!(referrer, "file:///dyn_import3.js");
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap(); let s = crate::resolve_import(specifier, referrer).unwrap();
Ok(s) Ok(s)
} }
@ -2504,7 +2503,7 @@ pub mod tests {
) -> Result<ModuleSpecifier, AnyError> { ) -> Result<ModuleSpecifier, AnyError> {
assert_eq!(specifier, "file:///main.js"); assert_eq!(specifier, "file:///main.js");
assert_eq!(referrer, "."); assert_eq!(referrer, ".");
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap(); let s = crate::resolve_import(specifier, referrer).unwrap();
Ok(s) Ok(s)
} }
@ -2526,7 +2525,7 @@ pub mod tests {
..Default::default() ..Default::default()
}); });
let specifier = ModuleSpecifier::resolve_url("file:///main.js").unwrap(); let specifier = crate::resolve_url("file:///main.js").unwrap();
let source_code = "Deno.core.print('hello\\n')".to_string(); let source_code = "Deno.core.print('hello\\n')".to_string();
let module_id = futures::executor::block_on( let module_id = futures::executor::block_on(

View file

@ -2,7 +2,6 @@
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::FsModuleLoader; use deno_core::FsModuleLoader;
use deno_core::ModuleSpecifier;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use deno_runtime::worker::MainWorker; use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions; use deno_runtime::worker::WorkerOptions;
@ -44,7 +43,7 @@ async fn main() -> Result<(), AnyError> {
let js_path = let js_path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js"); Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js");
let main_module = ModuleSpecifier::resolve_path(&js_path.to_string_lossy())?; let main_module = deno_core::resolve_path(&js_path.to_string_lossy())?;
let permissions = Permissions::allow_all(); let permissions = Permissions::allow_all();
let mut worker = let mut worker =

View file

@ -26,8 +26,8 @@ fn op_main_module(
_zero_copy: &mut [ZeroCopyBuf], _zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> { ) -> Result<Value, AnyError> {
let main = state.borrow::<ModuleSpecifier>().to_string(); let main = state.borrow::<ModuleSpecifier>().to_string();
let main_url = ModuleSpecifier::resolve_url_or_path(&main)?; let main_url = deno_core::resolve_url_or_path(&main)?;
if main_url.as_url().scheme() == "file" { if main_url.scheme() == "file" {
let main_path = std::env::current_dir().unwrap().join(main_url.to_string()); let main_path = std::env::current_dir().unwrap().join(main_url.to_string());
state state
.borrow::<Permissions>() .borrow::<Permissions>()

View file

@ -482,7 +482,7 @@ fn op_create_worker(
state.put::<CreateWebWorkerCbHolder>(create_module_loader.clone()); state.put::<CreateWebWorkerCbHolder>(create_module_loader.clone());
state.put::<WorkerId>(worker_id + 1); state.put::<WorkerId>(worker_id + 1);
let module_specifier = ModuleSpecifier::resolve_url(&specifier)?; let module_specifier = deno_core::resolve_url(&specifier)?;
let worker_name = args_name.unwrap_or_else(|| "".to_string()); let worker_name = args_name.unwrap_or_else(|| "".to_string());
let (handle_sender, handle_receiver) = let (handle_sender, handle_receiver) =

View file

@ -580,9 +580,8 @@ impl Permissions {
&self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let url = specifier.as_url(); match specifier.scheme() {
match url.scheme() { "file" => match specifier.to_file_path() {
"file" => match url.to_file_path() {
Ok(path) => self.check_read(&path), Ok(path) => self.check_read(&path),
Err(_) => Err(uri_error(format!( Err(_) => Err(uri_error(format!(
"Invalid file path.\n Specifier: {}", "Invalid file path.\n Specifier: {}",
@ -590,7 +589,7 @@ impl Permissions {
))), ))),
}, },
"data" => Ok(()), "data" => Ok(()),
_ => self.check_net_url(url), _ => self.check_net_url(specifier),
} }
} }
@ -749,6 +748,7 @@ fn format_host<T: AsRef<str>>(host: &(T, Option<u16>)) -> String {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use deno_core::resolve_url_or_path;
// Creates vector of strings, Vec<String> // Creates vector of strings, Vec<String>
macro_rules! svec { macro_rules! svec {
@ -1000,42 +1000,27 @@ mod tests {
let mut fixtures = vec![ let mut fixtures = vec![
( (
ModuleSpecifier::resolve_url_or_path("http://localhost:4545/mod.ts") resolve_url_or_path("http://localhost:4545/mod.ts").unwrap(),
.unwrap(),
true, true,
), ),
( (
ModuleSpecifier::resolve_url_or_path("http://deno.land/x/mod.ts") resolve_url_or_path("http://deno.land/x/mod.ts").unwrap(),
.unwrap(),
false, false,
), ),
( (
ModuleSpecifier::resolve_url_or_path( resolve_url_or_path("data:text/plain,Hello%2C%20Deno!").unwrap(),
"data:text/plain,Hello%2C%20Deno!",
)
.unwrap(),
true, true,
), ),
]; ];
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
fixtures.push(( fixtures
ModuleSpecifier::resolve_url_or_path("file:///C:/a/mod.ts").unwrap(), .push((resolve_url_or_path("file:///C:/a/mod.ts").unwrap(), true));
true, fixtures
)); .push((resolve_url_or_path("file:///C:/b/mod.ts").unwrap(), false));
fixtures.push((
ModuleSpecifier::resolve_url_or_path("file:///C:/b/mod.ts").unwrap(),
false,
));
} else { } else {
fixtures.push(( fixtures.push((resolve_url_or_path("file:///a/mod.ts").unwrap(), true));
ModuleSpecifier::resolve_url_or_path("file:///a/mod.ts").unwrap(), fixtures.push((resolve_url_or_path("file:///b/mod.ts").unwrap(), false));
true,
));
fixtures.push((
ModuleSpecifier::resolve_url_or_path("file:///b/mod.ts").unwrap(),
false,
));
} }
for (specifier, expected) in fixtures { for (specifier, expected) in fixtures {
@ -1058,7 +1043,7 @@ mod tests {
for url in test_cases { for url in test_cases {
assert!(perms assert!(perms
.check_specifier(&ModuleSpecifier::resolve_url_or_path(url).unwrap()) .check_specifier(&resolve_url_or_path(url).unwrap())
.is_err()); .is_err());
} }
} }

View file

@ -476,8 +476,7 @@ mod tests {
use deno_core::serde_json::json; use deno_core::serde_json::json;
fn create_test_web_worker() -> WebWorker { fn create_test_web_worker() -> WebWorker {
let main_module = let main_module = deno_core::resolve_url_or_path("./hello.js").unwrap();
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
let module_loader = Rc::new(deno_core::NoopModuleLoader); let module_loader = Rc::new(deno_core::NoopModuleLoader);
let create_web_worker_cb = Arc::new(|_| unreachable!()); let create_web_worker_cb = Arc::new(|_| unreachable!());

View file

@ -260,10 +260,10 @@ impl Drop for MainWorker {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use deno_core::resolve_url_or_path;
fn create_test_worker() -> MainWorker { fn create_test_worker() -> MainWorker {
let main_module = let main_module = resolve_url_or_path("./hello.js").unwrap();
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
let permissions = Permissions::default(); let permissions = Permissions::default();
let options = WorkerOptions { let options = WorkerOptions {
@ -296,8 +296,7 @@ mod tests {
.parent() .parent()
.unwrap() .unwrap()
.join("cli/tests/esm_imports_a.js"); .join("cli/tests/esm_imports_a.js");
let module_specifier = let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
let mut worker = create_test_worker(); let mut worker = create_test_worker();
let result = worker.execute_module(&module_specifier).await; let result = worker.execute_module(&module_specifier).await;
if let Err(err) = result { if let Err(err) = result {
@ -314,8 +313,7 @@ mod tests {
.parent() .parent()
.unwrap() .unwrap()
.join("tests/circular1.js"); .join("tests/circular1.js");
let module_specifier = let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
let mut worker = create_test_worker(); let mut worker = create_test_worker();
let result = worker.execute_module(&module_specifier).await; let result = worker.execute_module(&module_specifier).await;
if let Err(err) = result { if let Err(err) = result {
@ -330,8 +328,7 @@ mod tests {
async fn execute_mod_resolve_error() { async fn execute_mod_resolve_error() {
// "foo" is not a valid module specifier so this should return an error. // "foo" is not a valid module specifier so this should return an error.
let mut worker = create_test_worker(); let mut worker = create_test_worker();
let module_specifier = let module_specifier = resolve_url_or_path("does-not-exist").unwrap();
ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap();
let result = worker.execute_module(&module_specifier).await; let result = worker.execute_module(&module_specifier).await;
assert!(result.is_err()); assert!(result.is_err());
} }
@ -345,8 +342,7 @@ mod tests {
.parent() .parent()
.unwrap() .unwrap()
.join("cli/tests/001_hello.js"); .join("cli/tests/001_hello.js");
let module_specifier = let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
let result = worker.execute_module(&module_specifier).await; let result = worker.execute_module(&module_specifier).await;
assert!(result.is_ok()); assert!(result.is_ok());
} }