2024-03-19 23:42:22 -04:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use deno_core::op2;
|
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_fs::FileSystemRc;
|
2024-10-04 15:55:41 -04:00
|
|
|
use std::borrow::Cow;
|
2024-03-19 23:42:22 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use crate::NodePermissions;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::NodeRequireLoaderRc;
|
2024-03-19 23:42:22 -04:00
|
|
|
|
2024-10-04 15:55:41 -04:00
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
|
|
|
fn ensure_read_permission<'a, P>(
|
2024-03-19 23:42:22 -04:00
|
|
|
state: &mut OpState,
|
2024-10-04 15:55:41 -04:00
|
|
|
file_path: &'a Path,
|
2024-10-24 13:45:17 -04:00
|
|
|
) -> Result<Cow<'a, Path>, deno_core::error::AnyError>
|
2024-03-19 23:42:22 -04:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
2024-11-01 12:27:00 -04:00
|
|
|
let loader = state.borrow::<NodeRequireLoaderRc>().clone();
|
2024-06-06 23:37:53 -04:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2024-11-01 12:27:00 -04:00
|
|
|
loader.ensure_read_permission(permissions, file_path)
|
2024-03-19 23:42:22 -04:00
|
|
|
}
|
|
|
|
|
2024-10-24 13:45:17 -04:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum WorkerThreadsFilenameError {
|
|
|
|
#[error(transparent)]
|
|
|
|
Permission(deno_core::error::AnyError),
|
|
|
|
#[error("{0}")]
|
|
|
|
UrlParse(#[from] url::ParseError),
|
|
|
|
#[error("Relative path entries must start with '.' or '..'")]
|
|
|
|
InvalidRelativeUrl,
|
|
|
|
#[error("URL from Path-String")]
|
|
|
|
UrlFromPathString,
|
|
|
|
#[error("URL to Path-String")]
|
|
|
|
UrlToPathString,
|
|
|
|
#[error("URL to Path")]
|
|
|
|
UrlToPath,
|
|
|
|
#[error("File not found [{0:?}]")]
|
|
|
|
FileNotFound(PathBuf),
|
|
|
|
#[error(transparent)]
|
|
|
|
Fs(#[from] deno_io::fs::FsError),
|
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
// todo(dsherret): we should remove this and do all this work inside op_create_worker
|
2024-11-20 16:24:04 -05:00
|
|
|
#[op2(stack_trace)]
|
2024-03-19 23:42:22 -04:00
|
|
|
#[string]
|
|
|
|
pub fn op_worker_threads_filename<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
#[string] specifier: String,
|
2024-10-24 13:45:17 -04:00
|
|
|
) -> Result<String, WorkerThreadsFilenameError>
|
2024-03-19 23:42:22 -04:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
if specifier.starts_with("data:") {
|
|
|
|
return Ok(specifier);
|
|
|
|
}
|
|
|
|
let url: Url = if specifier.starts_with("file:") {
|
|
|
|
Url::parse(&specifier)?
|
|
|
|
} else {
|
|
|
|
let path = PathBuf::from(&specifier);
|
|
|
|
if path.is_relative() && !specifier.starts_with('.') {
|
2024-10-24 13:45:17 -04:00
|
|
|
return Err(WorkerThreadsFilenameError::InvalidRelativeUrl);
|
2024-03-19 23:42:22 -04:00
|
|
|
}
|
2024-10-24 13:45:17 -04:00
|
|
|
let path = ensure_read_permission::<P>(state, &path)
|
|
|
|
.map_err(WorkerThreadsFilenameError::Permission)?;
|
2024-03-19 23:42:22 -04:00
|
|
|
let fs = state.borrow::<FileSystemRc>();
|
|
|
|
let canonicalized_path =
|
2024-10-14 08:31:51 -04:00
|
|
|
deno_path_util::strip_unc_prefix(fs.realpath_sync(&path)?);
|
2024-03-19 23:42:22 -04:00
|
|
|
Url::from_file_path(canonicalized_path)
|
2024-10-24 13:45:17 -04:00
|
|
|
.map_err(|_| WorkerThreadsFilenameError::UrlFromPathString)?
|
2024-03-19 23:42:22 -04:00
|
|
|
};
|
|
|
|
let url_path = url
|
|
|
|
.to_file_path()
|
2024-10-24 13:45:17 -04:00
|
|
|
.map_err(|_| WorkerThreadsFilenameError::UrlToPathString)?;
|
|
|
|
let url_path = ensure_read_permission::<P>(state, &url_path)
|
|
|
|
.map_err(WorkerThreadsFilenameError::Permission)?;
|
2024-03-19 23:42:22 -04:00
|
|
|
let fs = state.borrow::<FileSystemRc>();
|
|
|
|
if !fs.exists_sync(&url_path) {
|
2024-10-24 13:45:17 -04:00
|
|
|
return Err(WorkerThreadsFilenameError::FileNotFound(
|
|
|
|
url_path.to_path_buf(),
|
|
|
|
));
|
2024-03-19 23:42:22 -04:00
|
|
|
}
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(url.to_string())
|
2024-03-19 23:42:22 -04:00
|
|
|
}
|