Skip to content

refactor: remove deno_core dependency from deno_permissions #29467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion runtime/permissions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ path = "lib.rs"

[dependencies]
capacity_builder.workspace = true
deno_core.workspace = true
deno_error.workspace = true
deno_path_util.workspace = true
deno_terminal.workspace = true
deno_unsync.workspace = true
fqdn.workspace = true
libc.workspace = true
log.workspace = true
once_cell.workspace = true
parking_lot.workspace = true
percent-encoding = { workspace = true, features = [] }
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
url.workspace = true
which.workspace = true

[target.'cfg(windows)'.dependencies]
Expand Down
41 changes: 20 additions & 21 deletions runtime/permissions/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,24 @@ use std::string::ToString;
use std::sync::Arc;

use capacity_builder::StringBuilder;
use deno_core::parking_lot::Mutex;
use deno_core::serde::de;
use deno_core::serde::Deserialize;
use deno_core::serde::Deserializer;
use deno_core::serde::Serialize;
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 deno_unsync::sync::AtomicFlag;
use fqdn::FQDN;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use serde::de;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use url::Url;

pub mod prompter;
use prompter::permission_prompt;
pub use prompter::set_prompt_callbacks;
pub use prompter::set_prompter;
pub use prompter::GetFormattedStackFn;
pub use prompter::PermissionPrompter;
pub use prompter::PromptCallback;
pub use prompter::PromptResponse;
Expand Down Expand Up @@ -2537,7 +2536,7 @@ impl PermissionsContainer {
#[inline(always)]
pub fn check_specifier(
&self,
specifier: &ModuleSpecifier,
specifier: &Url,
kind: CheckSpecifierKind,
) -> Result<(), PermissionCheckError> {
let mut inner = self.inner.lock();
Expand Down Expand Up @@ -3772,9 +3771,9 @@ pub fn is_standalone() -> bool {
mod tests {
use std::net::Ipv4Addr;

use deno_core::serde_json::json;
use fqdn::fqdn;
use prompter::tests::*;
use serde_json::json;

use super::*;

Expand Down Expand Up @@ -4166,56 +4165,56 @@ mod tests {

let mut fixtures = vec![
(
ModuleSpecifier::parse("http://localhost:4545/mod.ts").unwrap(),
Url::parse("http://localhost:4545/mod.ts").unwrap(),
CheckSpecifierKind::Static,
true,
),
(
ModuleSpecifier::parse("http://localhost:4545/mod.ts").unwrap(),
Url::parse("http://localhost:4545/mod.ts").unwrap(),
CheckSpecifierKind::Dynamic,
true,
),
(
ModuleSpecifier::parse("http://deno.land/x/mod.ts").unwrap(),
Url::parse("http://deno.land/x/mod.ts").unwrap(),
CheckSpecifierKind::Dynamic,
false,
),
(
ModuleSpecifier::parse("data:text/plain,Hello%2C%20Deno!").unwrap(),
Url::parse("data:text/plain,Hello%2C%20Deno!").unwrap(),
CheckSpecifierKind::Dynamic,
true,
),
];

if cfg!(target_os = "windows") {
fixtures.push((
ModuleSpecifier::parse("file:///C:/a/mod.ts").unwrap(),
Url::parse("file:///C:/a/mod.ts").unwrap(),
CheckSpecifierKind::Dynamic,
true,
));
fixtures.push((
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
Url::parse("file:///C:/b/mod.ts").unwrap(),
CheckSpecifierKind::Static,
true,
));
fixtures.push((
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
Url::parse("file:///C:/b/mod.ts").unwrap(),
CheckSpecifierKind::Dynamic,
false,
));
} else {
fixtures.push((
ModuleSpecifier::parse("file:///a/mod.ts").unwrap(),
Url::parse("file:///a/mod.ts").unwrap(),
CheckSpecifierKind::Dynamic,
true,
));
fixtures.push((
ModuleSpecifier::parse("file:///b/mod.ts").unwrap(),
Url::parse("file:///b/mod.ts").unwrap(),
CheckSpecifierKind::Static,
true,
));
fixtures.push((
ModuleSpecifier::parse("file:///b/mod.ts").unwrap(),
Url::parse("file:///b/mod.ts").unwrap(),
CheckSpecifierKind::Dynamic,
false,
));
Expand Down
25 changes: 13 additions & 12 deletions runtime/permissions/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use std::io::StderrLock;
use std::io::StdinLock;
use std::io::Write as IoWrite;

use deno_core::error::JsStackFrame;
use deno_core::parking_lot::Mutex;
use deno_terminal::colors;
use once_cell::sync::Lazy;
use parking_lot::Mutex;

use crate::is_standalone;

Expand Down Expand Up @@ -55,11 +54,11 @@ static MAYBE_BEFORE_PROMPT_CALLBACK: Lazy<Mutex<Option<PromptCallback>>> =
static MAYBE_AFTER_PROMPT_CALLBACK: Lazy<Mutex<Option<PromptCallback>>> =
Lazy::new(|| Mutex::new(None));

static MAYBE_CURRENT_STACKTRACE: Lazy<Mutex<Option<Vec<JsStackFrame>>>> =
static MAYBE_CURRENT_STACKTRACE: Lazy<Mutex<Option<GetFormattedStackFn>>> =
Lazy::new(|| Mutex::new(None));

pub fn set_current_stacktrace(trace: Vec<JsStackFrame>) {
*MAYBE_CURRENT_STACKTRACE.lock() = Some(trace);
pub fn set_current_stacktrace(get_stack: GetFormattedStackFn) {
*MAYBE_CURRENT_STACKTRACE.lock() = Some(get_stack);
}

pub fn permission_prompt(
Expand Down Expand Up @@ -95,18 +94,21 @@ pub fn set_prompter(prompter: Box<dyn PermissionPrompter>) {

pub type PromptCallback = Box<dyn FnMut() + Send + Sync>;

pub type GetFormattedStackFn = Box<dyn FnOnce() -> Vec<String> + Send + Sync>;

pub trait PermissionPrompter: Send + Sync {
fn prompt(
&mut self,
message: &str,
name: &str,
api_name: Option<&str>,
is_unary: bool,
stack: Option<Vec<JsStackFrame>>,
get_stack: Option<GetFormattedStackFn>,
) -> PromptResponse;
}

pub struct TtyPrompter;

#[cfg(unix)]
fn clear_stdin(
_stdin_lock: &mut StdinLock,
Expand Down Expand Up @@ -294,7 +296,7 @@ impl PermissionPrompter for TtyPrompter {
name: &str,
api_name: Option<&str>,
is_unary: bool,
stack: Option<Vec<JsStackFrame>>,
get_stack: Option<GetFormattedStackFn>,
) -> PromptResponse {
if !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() {
return PromptResponse::Deny;
Expand Down Expand Up @@ -351,16 +353,15 @@ impl PermissionPrompter for TtyPrompter {
)
.unwrap();
}
let stack_lines_count = if let Some(stack) = stack {
let stack_lines_count = if let Some(get_stack) = get_stack {
let stack = get_stack();
let len = stack.len();
for (idx, frame) in stack.into_iter().enumerate() {
writeln!(
&mut output,
"┃ {} {}",
colors::gray(if idx != len - 1 { "├─" } else { "└─" }),
colors::gray(deno_core::error::format_frame::<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a functional change? We're no longer formatting the frame here

deno_core::error::NoAnsiColors,
>(&frame))
colors::gray(frame),
)
.unwrap();
}
Expand Down Expand Up @@ -490,7 +491,7 @@ pub mod tests {
_name: &str,
_api_name: Option<&str>,
_is_unary: bool,
_stack: Option<Vec<JsStackFrame>>,
_get_stack: Option<GetFormattedStackFn>,
) -> PromptResponse {
if STUB_PROMPT_VALUE.load(Ordering::SeqCst) {
PromptResponse::Allow
Expand Down
10 changes: 3 additions & 7 deletions runtime/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,9 @@ impl WebWorker {
validate_import_attributes_callback,
)),
import_assertions_support: deno_core::ImportAssertionsSupport::Error,
maybe_op_stack_trace_callback: if options.enable_stack_trace_arg_in_ops {
Some(Box::new(|stack| {
deno_permissions::prompter::set_current_stacktrace(stack)
}))
} else {
None
},
maybe_op_stack_trace_callback: options
.enable_stack_trace_arg_in_ops
.then(crate::worker::create_permissions_stack_trace_callback),
..Default::default()
});

Expand Down
23 changes: 18 additions & 5 deletions runtime/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,15 +1128,28 @@ fn common_runtime(
validate_import_attributes_callback,
)),
import_assertions_support: deno_core::ImportAssertionsSupport::Error,
maybe_op_stack_trace_callback: enable_stack_trace_arg_in_ops.then(|| {
Box::new(|stack| {
deno_permissions::prompter::set_current_stacktrace(stack)
}) as _
}),
maybe_op_stack_trace_callback: enable_stack_trace_arg_in_ops
.then(create_permissions_stack_trace_callback),
..Default::default()
})
}

pub fn create_permissions_stack_trace_callback(
) -> deno_core::OpStackTraceCallback {
Box::new(|stack: Vec<deno_core::error::JsStackFrame>| {
deno_permissions::prompter::set_current_stacktrace(Box::new(|| {
stack
.into_iter()
.map(|frame| {
deno_core::error::format_frame::<deno_core::error::NoAnsiColors>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, it's rewritten here. Maybe add a comment in runtime/permissions/ that it's expected the frames are in readable format?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the name from GetStackFn to GetFormattedStackFn.

&frame,
)
})
.collect()
}))
}) as _
}

pub struct UnconfiguredRuntime {
module_loader: Rc<PlaceholderModuleLoader>,
js_runtime: JsRuntime,
Expand Down
Loading