diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/lib.rs | 1 | ||||
-rw-r--r-- | core/module_specifier.rs | 33 | ||||
-rw-r--r-- | core/ops_builtin_v8.rs | 4 |
3 files changed, 29 insertions, 9 deletions
diff --git a/core/lib.rs b/core/lib.rs index 08df6e44d..b48a77f69 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -72,6 +72,7 @@ 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::resolve_url_or_path_deprecated; pub use crate::module_specifier::ModuleResolutionError; pub use crate::module_specifier::ModuleSpecifier; pub use crate::module_specifier::DUMMY_SPECIFIER; diff --git a/core/module_specifier.rs b/core/module_specifier.rs index 72d693723..6c6dbad95 100644 --- a/core/module_specifier.rs +++ b/core/module_specifier.rs @@ -123,7 +123,7 @@ pub fn resolve_url( /// e.g. 'http:' or 'file:' or 'git+ssh:'. If not, it's interpreted as a /// file path; if it is a relative path it's resolved relative to the current /// working directory. -pub fn resolve_url_or_path( +pub fn resolve_url_or_path_deprecated( specifier: &str, ) -> Result<ModuleSpecifier, ModuleResolutionError> { if specifier_has_uri_scheme(specifier) { @@ -135,9 +135,26 @@ pub fn resolve_url_or_path( } } +/// Takes a string representing either an absolute URL or a file path, +/// as it may be passed to deno as a command line argument. +/// The string is interpreted as a URL if it starts with a valid URI scheme, +/// e.g. 'http:' or 'file:' or 'git+ssh:'. If not, it's interpreted as a +/// file path; if it is a relative path it's resolved relative to passed +/// `current_dir`. +pub fn resolve_url_or_path( + specifier: &str, + current_dir: &Path, +) -> Result<ModuleSpecifier, ModuleResolutionError> { + if specifier_has_uri_scheme(specifier) { + resolve_url(specifier) + } else { + resolve_path(specifier, current_dir) + } +} + /// Converts a string representing a relative or absolute path into a -/// ModuleSpecifier. A relative path is considered relative to the current -/// working directory. +/// ModuleSpecifier. A relative path is considered relative to the passed +/// `current_dir`. pub fn resolve_path( path_str: &str, current_dir: &Path, @@ -344,7 +361,7 @@ mod tests { } #[test] - fn test_resolve_url_or_path() { + fn test_resolve_url_or_path_deprecated() { // Absolute URL. let mut tests: Vec<(&str, String)> = vec![ ( @@ -440,13 +457,15 @@ mod tests { } for (specifier, expected_url) in tests { - let url = resolve_url_or_path(specifier).unwrap().to_string(); + let url = resolve_url_or_path_deprecated(specifier) + .unwrap() + .to_string(); assert_eq!(url, expected_url); } } #[test] - fn test_resolve_url_or_path_error() { + fn test_resolve_url_or_path_deprecated_error() { use url::ParseError::*; use ModuleResolutionError::*; @@ -460,7 +479,7 @@ mod tests { } for (specifier, expected_err) in tests { - let err = resolve_url_or_path(specifier).unwrap_err(); + let err = resolve_url_or_path_deprecated(specifier).unwrap_err(); assert_eq!(err, expected_err); } } diff --git a/core/ops_builtin_v8.rs b/core/ops_builtin_v8.rs index c3c4ec092..e00ed5a29 100644 --- a/core/ops_builtin_v8.rs +++ b/core/ops_builtin_v8.rs @@ -6,7 +6,7 @@ use crate::error::range_error; use crate::error::type_error; use crate::error::JsError; use crate::ops_builtin::WasmStreamingResource; -use crate::resolve_url_or_path; +use crate::resolve_url_or_path_deprecated; use crate::serde_v8::from_v8; use crate::source_map::apply_source_map as apply_source_map_; use crate::JsRealm; @@ -165,7 +165,7 @@ fn op_eval_context<'a>( let source = v8::Local::<v8::String>::try_from(source.v8_value) .map_err(|_| type_error("Invalid source"))?; let specifier = match specifier { - Some(s) => resolve_url_or_path(&s)?.to_string(), + Some(s) => resolve_url_or_path_deprecated(&s)?.to_string(), None => crate::DUMMY_SPECIFIER.to_string(), }; let specifier = v8::String::new(tc_scope, &specifier).unwrap(); |