diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-09-28 07:55:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-28 07:55:01 -0400 |
commit | fc739dc5eb2769e4608ccf08d23ca8ff0fcc19c5 (patch) | |
tree | 9c5bca411f4b9a6aea5a190d88217f4925563ad3 /runtime/permissions | |
parent | b694efb3849c4737e8ad617a9a48d5488e21d5da (diff) |
refactor: use deno_path_util (#25918)
Diffstat (limited to 'runtime/permissions')
-rw-r--r-- | runtime/permissions/Cargo.toml | 1 | ||||
-rw-r--r-- | runtime/permissions/lib.rs | 85 |
2 files changed, 4 insertions, 82 deletions
diff --git a/runtime/permissions/Cargo.toml b/runtime/permissions/Cargo.toml index 9821b6148..37500d3eb 100644 --- a/runtime/permissions/Cargo.toml +++ b/runtime/permissions/Cargo.toml @@ -15,6 +15,7 @@ path = "lib.rs" [dependencies] deno_core.workspace = true +deno_path_util.workspace = true deno_terminal.workspace = true fqdn = "0.3.4" libc.workspace = true diff --git a/runtime/permissions/lib.rs b/runtime/permissions/lib.rs index 5d8e76125..e919b81b6 100644 --- a/runtime/permissions/lib.rs +++ b/runtime/permissions/lib.rs @@ -6,7 +6,6 @@ use deno_core::error::custom_error; use deno_core::error::type_error; use deno_core::error::uri_error; use deno_core::error::AnyError; -use deno_core::normalize_path; use deno_core::parking_lot::Mutex; use deno_core::serde::de; use deno_core::serde::Deserialize; @@ -16,6 +15,8 @@ use deno_core::serde_json; use deno_core::unsync::sync::AtomicFlag; use deno_core::url::Url; use deno_core::ModuleSpecifier; +use deno_path_util::normalize_path; +use deno_path_util::url_to_file_path; use deno_terminal::colors; use fqdn::FQDN; use once_cell::sync::Lazy; @@ -2032,52 +2033,6 @@ impl Permissions { } } -/// Attempts to convert a specifier to a file path. By default, uses the Url -/// crate's `to_file_path()` method, but falls back to try and resolve unix-style -/// paths on Windows. -pub fn specifier_to_file_path( - specifier: &ModuleSpecifier, -) -> Result<PathBuf, AnyError> { - let result = if specifier.scheme() != "file" { - Err(()) - } else if cfg!(windows) { - if specifier.host().is_some() { - Err(()) - } else { - match specifier.to_file_path() { - Ok(path) => Ok(path), - Err(()) => { - // This might be a unix-style path which is used in the tests even on Windows. - // Attempt to see if we can convert it to a `PathBuf`. This code should be removed - // once/if https://github.com/servo/rust-url/issues/730 is implemented. - if specifier.scheme() == "file" - && specifier.port().is_none() - && specifier.path_segments().is_some() - { - let path_str = specifier.path(); - match String::from_utf8( - percent_encoding::percent_decode(path_str.as_bytes()).collect(), - ) { - Ok(path_str) => Ok(PathBuf::from(path_str)), - Err(_) => Err(()), - } - } else { - Err(()) - } - } - } - } - } else { - specifier.to_file_path() - }; - match result { - Ok(path) => Ok(path), - Err(()) => Err(uri_error(format!( - "Invalid file path.\n Specifier: {specifier}" - ))), - } -} - #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum CheckSpecifierKind { Static, @@ -2130,7 +2085,7 @@ impl PermissionsContainer { return Ok(()); } - match specifier_to_file_path(specifier) { + match url_to_file_path(specifier) { Ok(path) => inner.read.check( &PathQueryDescriptor { requested: path.to_string_lossy().into_owned(), @@ -4453,38 +4408,4 @@ mod tests { ); } } - - #[test] - fn test_specifier_to_file_path() { - run_success_test("file:///", "/"); - run_success_test("file:///test", "/test"); - run_success_test("file:///dir/test/test.txt", "/dir/test/test.txt"); - run_success_test( - "file:///dir/test%20test/test.txt", - "/dir/test test/test.txt", - ); - - assert_no_panic_specifier_to_file_path("file:/"); - assert_no_panic_specifier_to_file_path("file://"); - assert_no_panic_specifier_to_file_path("file://asdf/"); - assert_no_panic_specifier_to_file_path("file://asdf/66666/a.ts"); - - fn run_success_test(specifier: &str, expected_path: &str) { - let result = - specifier_to_file_path(&ModuleSpecifier::parse(specifier).unwrap()) - .unwrap(); - assert_eq!(result, PathBuf::from(expected_path)); - } - fn assert_no_panic_specifier_to_file_path(specifier: &str) { - let result = - specifier_to_file_path(&ModuleSpecifier::parse(specifier).unwrap()); - match result { - Ok(_) => (), - Err(err) => assert_eq!( - err.to_string(), - format!("Invalid file path.\n Specifier: {specifier}") - ), - } - } - } } |