diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/compiler.rs | 4 | ||||
-rw-r--r-- | cli/flags.rs | 2 | ||||
-rw-r--r-- | cli/import_map.rs | 2 | ||||
-rw-r--r-- | cli/main.rs | 3 | ||||
-rw-r--r-- | cli/module_specifier.rs | 81 | ||||
-rw-r--r-- | cli/ops.rs | 18 | ||||
-rw-r--r-- | cli/state.rs | 19 | ||||
-rw-r--r-- | cli/worker.rs | 2 |
8 files changed, 23 insertions, 108 deletions
diff --git a/cli/compiler.rs b/cli/compiler.rs index a33ccfcb0..de5b5c609 100644 --- a/cli/compiler.rs +++ b/cli/compiler.rs @@ -247,7 +247,7 @@ mod tests { fn test_compile_sync() { tokio_util::init(|| { let specifier = "./tests/002_hello.ts"; - use crate::module_specifier::ModuleSpecifier; + use deno::ModuleSpecifier; let module_name = ModuleSpecifier::resolve_root(specifier) .unwrap() .to_string(); @@ -294,7 +294,7 @@ mod tests { #[test] fn test_bundle_async() { let specifier = "./tests/002_hello.ts"; - use crate::module_specifier::ModuleSpecifier; + use deno::ModuleSpecifier; let module_name = ModuleSpecifier::resolve_root(specifier) .unwrap() .to_string(); diff --git a/cli/flags.rs b/cli/flags.rs index 6d4a95f9e..8b95aefae 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -502,7 +502,7 @@ pub enum DenoSubcommand { } fn get_default_bundle_filename(source_file: &str) -> String { - use crate::module_specifier::ModuleSpecifier; + use deno::ModuleSpecifier; let url = ModuleSpecifier::resolve_root(source_file).unwrap().to_url(); let path_segments = url.path_segments().unwrap(); let last = path_segments.last().unwrap(); diff --git a/cli/import_map.rs b/cli/import_map.rs index 6f4095a90..4321cacad 100644 --- a/cli/import_map.rs +++ b/cli/import_map.rs @@ -1,4 +1,4 @@ -use crate::module_specifier::ModuleSpecifier; +use deno::ModuleSpecifier; use indexmap::IndexMap; use serde_json::Map; use serde_json::Value; diff --git a/cli/main.rs b/cli/main.rs index 11575ef63..ae453f70f 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -27,7 +27,6 @@ mod http_body; mod http_util; mod import_map; pub mod js_errors; -mod module_specifier; pub mod msg; pub mod msg_util; pub mod ops; @@ -46,11 +45,11 @@ pub mod worker; use crate::compiler::bundle_async; use crate::errors::RustOrJsError; -use crate::module_specifier::ModuleSpecifier; use crate::progress::Progress; use crate::state::ThreadSafeState; use crate::worker::Worker; use deno::v8_set_flags; +use deno::ModuleSpecifier; use flags::DenoFlags; use flags::DenoSubcommand; use futures::future; diff --git a/cli/module_specifier.rs b/cli/module_specifier.rs deleted file mode 100644 index 475211289..000000000 --- a/cli/module_specifier.rs +++ /dev/null @@ -1,81 +0,0 @@ -use std::fmt; -use url::Url; - -#[derive(Debug, Clone, PartialEq)] -/// Resolved module specifier -pub struct ModuleSpecifier(Url); - -impl ModuleSpecifier { - pub fn to_url(&self) -> Url { - self.0.clone() - } - /// Resolves module using this algorithm: - /// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier - pub fn resolve( - specifier: &str, - base: &str, - ) -> Result<ModuleSpecifier, url::ParseError> { - // 1. Apply the URL parser to specifier. If the result is not failure, return - // the result. - // let specifier = parse_local_or_remote(specifier)?.to_string(); - if let Ok(url) = Url::parse(specifier) { - return Ok(ModuleSpecifier(url)); - } - - // 2. If specifier does not start with the character U+002F SOLIDUS (/), the - // two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./), or the - // three-character sequence U+002E FULL STOP, U+002E FULL STOP, U+002F - // SOLIDUS (../), return failure. - if !specifier.starts_with('/') - && !specifier.starts_with("./") - && !specifier.starts_with("../") - { - // TODO This is (probably) not the correct error to return here. - // TODO: This error is very not-user-friendly - return Err(url::ParseError::RelativeUrlWithCannotBeABaseBase); - } - - // 3. Return the result of applying the URL parser to specifier with base URL - // as the base URL. - let base_url = Url::parse(base)?; - let u = base_url.join(&specifier)?; - Ok(ModuleSpecifier(u)) - } - - /// Takes a string representing a path or URL to a module, but of the type - /// passed through the command-line interface for the main module. This is - /// slightly different than specifiers used in import statements: "foo.js" for - /// example is allowed here, whereas in import statements a leading "./" is - /// required ("./foo.js"). This function is aware of the current working - /// directory and returns an absolute URL. - pub fn resolve_root( - root_specifier: &str, - ) -> Result<ModuleSpecifier, url::ParseError> { - if let Ok(url) = Url::parse(root_specifier) { - Ok(ModuleSpecifier(url)) - } else { - let cwd = std::env::current_dir().unwrap(); - let base = Url::from_directory_path(cwd).unwrap(); - let url = base.join(root_specifier)?; - Ok(ModuleSpecifier(url)) - } - } -} - -impl From<Url> for ModuleSpecifier { - fn from(url: Url) -> Self { - ModuleSpecifier(url) - } -} - -impl fmt::Display for ModuleSpecifier { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - -impl PartialEq<String> for ModuleSpecifier { - fn eq(&self, other: &String) -> bool { - &self.to_string() == other - } -} diff --git a/cli/ops.rs b/cli/ops.rs index e9fd48d0d..e8fa47aad 100644 --- a/cli/ops.rs +++ b/cli/ops.rs @@ -10,7 +10,6 @@ use crate::fs as deno_fs; use crate::http_util; use crate::js_errors::apply_source_map; use crate::js_errors::JSErrorColor; -use crate::module_specifier::ModuleSpecifier; use crate::msg; use crate::msg_util; use crate::rand; @@ -29,6 +28,7 @@ use crate::worker::Worker; use deno::js_check; use deno::Buf; use deno::JSError; +use deno::ModuleSpecifier; use deno::Op; use deno::PinnedBuf; use flatbuffers::FlatBufferBuilder; @@ -505,15 +505,13 @@ fn op_fetch_module_meta_data( // import map - why it is not always resolved? Eg. "bad-module.ts" will return NotFound // error whilst it should return RelativeUrlWithCannotBeABaseBase error let resolved_specifier = match &state.import_map { - Some(import_map) => { - match import_map.resolve(specifier, referrer) { - Ok(result) => match result { - Some(module_specifier) => module_specifier.to_string(), - None => specifier.to_string(), - }, - Err(err) => panic!("error resolving using import map: {:?}", err), // TODO: this should be coerced to DenoError - } - } + Some(import_map) => match import_map.resolve(specifier, referrer) { + Ok(result) => match result { + Some(module_specifier) => module_specifier.to_string(), + None => specifier.to_string(), + }, + Err(err) => return odd_future(DenoError::from(err)), + }, None => specifier.to_string(), }; diff --git a/cli/state.rs b/cli/state.rs index df1bf1cc5..f5eb8ae7a 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -7,7 +7,6 @@ use crate::errors::DenoResult; use crate::flags; use crate::global_timer::GlobalTimer; use crate::import_map::ImportMap; -use crate::module_specifier::ModuleSpecifier; use crate::msg; use crate::ops; use crate::permissions::DenoPermissions; @@ -17,6 +16,7 @@ use crate::resources::ResourceId; use crate::worker::Worker; use deno::Buf; use deno::Loader; +use deno::ModuleSpecifier; use deno::Op; use deno::PinnedBuf; use futures::future::Either; @@ -157,28 +157,27 @@ impl Loader for ThreadSafeState { specifier: &str, referrer: &str, is_root: bool, - ) -> Result<String, Self::Error> { + ) -> Result<ModuleSpecifier, Self::Error> { if !is_root { if let Some(import_map) = &self.import_map { let result = import_map.resolve(specifier, referrer)?; if result.is_some() { - return Ok(result.unwrap().to_string()); + return Ok(result.unwrap()); } } } - let module_specifier = - ModuleSpecifier::resolve(specifier, referrer).map_err(DenoError::from)?; - Ok(module_specifier.to_string()) + ModuleSpecifier::resolve(specifier, referrer).map_err(DenoError::from) } /// Given an absolute url, load its source code. - fn load(&self, url: &str) -> Box<deno::SourceCodeInfoFuture<Self::Error>> { + fn load( + &self, + module_specifier: &ModuleSpecifier, + ) -> Box<deno::SourceCodeInfoFuture<Self::Error>> { self.metrics.resolve_count.fetch_add(1, Ordering::SeqCst); - let module_specifier = ModuleSpecifier::resolve_root(url) - .expect("should already been properly resolved"); Box::new( - fetch_module_meta_data_and_maybe_compile_async(self, &module_specifier) + fetch_module_meta_data_and_maybe_compile_async(self, module_specifier) .map_err(|err| { eprintln!("{}", err); err diff --git a/cli/worker.rs b/cli/worker.rs index f11aa93e9..9170a293f 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -2,11 +2,11 @@ use crate::errors::DenoError; use crate::errors::RustOrJsError; use crate::js_errors; -use crate::module_specifier::ModuleSpecifier; use crate::state::ThreadSafeState; use crate::tokio_util; use deno; use deno::JSError; +use deno::ModuleSpecifier; use deno::StartupData; use futures::Async; use futures::Future; |