Skip to content

Commit e3bf5ee

Browse files
authored
refactor: remove deno_core dependency from deno_permissions (#29467)
For making this code more usable without deno_core. Will be useful in deno_resolver.
1 parent 7a86ee0 commit e3bf5ee

File tree

6 files changed

+62
-47
lines changed

6 files changed

+62
-47
lines changed

Cargo.lock

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/permissions/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ path = "lib.rs"
1515

1616
[dependencies]
1717
capacity_builder.workspace = true
18-
deno_core.workspace = true
1918
deno_error.workspace = true
2019
deno_path_util.workspace = true
2120
deno_terminal.workspace = true
21+
deno_unsync.workspace = true
2222
fqdn.workspace = true
2323
libc.workspace = true
2424
log.workspace = true
2525
once_cell.workspace = true
26+
parking_lot.workspace = true
2627
percent-encoding = { workspace = true, features = [] }
2728
serde.workspace = true
29+
serde_json.workspace = true
2830
thiserror.workspace = true
31+
url.workspace = true
2932
which.workspace = true
3033

3134
[target.'cfg(windows)'.dependencies]

runtime/permissions/lib.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,24 @@ use std::string::ToString;
1414
use std::sync::Arc;
1515

1616
use capacity_builder::StringBuilder;
17-
use deno_core::parking_lot::Mutex;
18-
use deno_core::serde::de;
19-
use deno_core::serde::Deserialize;
20-
use deno_core::serde::Deserializer;
21-
use deno_core::serde::Serialize;
22-
use deno_core::serde_json;
23-
use deno_core::unsync::sync::AtomicFlag;
24-
use deno_core::url::Url;
25-
use deno_core::ModuleSpecifier;
2617
use deno_path_util::normalize_path;
2718
use deno_path_util::url_to_file_path;
2819
use deno_terminal::colors;
20+
use deno_unsync::sync::AtomicFlag;
2921
use fqdn::FQDN;
3022
use once_cell::sync::Lazy;
23+
use parking_lot::Mutex;
24+
use serde::de;
25+
use serde::Deserialize;
26+
use serde::Deserializer;
27+
use serde::Serialize;
28+
use url::Url;
3129

3230
pub mod prompter;
3331
use prompter::permission_prompt;
3432
pub use prompter::set_prompt_callbacks;
3533
pub use prompter::set_prompter;
34+
pub use prompter::GetFormattedStackFn;
3635
pub use prompter::PermissionPrompter;
3736
pub use prompter::PromptCallback;
3837
pub use prompter::PromptResponse;
@@ -2537,7 +2536,7 @@ impl PermissionsContainer {
25372536
#[inline(always)]
25382537
pub fn check_specifier(
25392538
&self,
2540-
specifier: &ModuleSpecifier,
2539+
specifier: &Url,
25412540
kind: CheckSpecifierKind,
25422541
) -> Result<(), PermissionCheckError> {
25432542
let mut inner = self.inner.lock();
@@ -3772,9 +3771,9 @@ pub fn is_standalone() -> bool {
37723771
mod tests {
37733772
use std::net::Ipv4Addr;
37743773

3775-
use deno_core::serde_json::json;
37763774
use fqdn::fqdn;
37773775
use prompter::tests::*;
3776+
use serde_json::json;
37783777

37793778
use super::*;
37803779

@@ -4166,56 +4165,56 @@ mod tests {
41664165

41674166
let mut fixtures = vec![
41684167
(
4169-
ModuleSpecifier::parse("http://localhost:4545/mod.ts").unwrap(),
4168+
Url::parse("http://localhost:4545/mod.ts").unwrap(),
41704169
CheckSpecifierKind::Static,
41714170
true,
41724171
),
41734172
(
4174-
ModuleSpecifier::parse("http://localhost:4545/mod.ts").unwrap(),
4173+
Url::parse("http://localhost:4545/mod.ts").unwrap(),
41754174
CheckSpecifierKind::Dynamic,
41764175
true,
41774176
),
41784177
(
4179-
ModuleSpecifier::parse("http://deno.land/x/mod.ts").unwrap(),
4178+
Url::parse("http://deno.land/x/mod.ts").unwrap(),
41804179
CheckSpecifierKind::Dynamic,
41814180
false,
41824181
),
41834182
(
4184-
ModuleSpecifier::parse("data:text/plain,Hello%2C%20Deno!").unwrap(),
4183+
Url::parse("data:text/plain,Hello%2C%20Deno!").unwrap(),
41854184
CheckSpecifierKind::Dynamic,
41864185
true,
41874186
),
41884187
];
41894188

41904189
if cfg!(target_os = "windows") {
41914190
fixtures.push((
4192-
ModuleSpecifier::parse("file:///C:/a/mod.ts").unwrap(),
4191+
Url::parse("file:///C:/a/mod.ts").unwrap(),
41934192
CheckSpecifierKind::Dynamic,
41944193
true,
41954194
));
41964195
fixtures.push((
4197-
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
4196+
Url::parse("file:///C:/b/mod.ts").unwrap(),
41984197
CheckSpecifierKind::Static,
41994198
true,
42004199
));
42014200
fixtures.push((
4202-
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
4201+
Url::parse("file:///C:/b/mod.ts").unwrap(),
42034202
CheckSpecifierKind::Dynamic,
42044203
false,
42054204
));
42064205
} else {
42074206
fixtures.push((
4208-
ModuleSpecifier::parse("file:///a/mod.ts").unwrap(),
4207+
Url::parse("file:///a/mod.ts").unwrap(),
42094208
CheckSpecifierKind::Dynamic,
42104209
true,
42114210
));
42124211
fixtures.push((
4213-
ModuleSpecifier::parse("file:///b/mod.ts").unwrap(),
4212+
Url::parse("file:///b/mod.ts").unwrap(),
42144213
CheckSpecifierKind::Static,
42154214
true,
42164215
));
42174216
fixtures.push((
4218-
ModuleSpecifier::parse("file:///b/mod.ts").unwrap(),
4217+
Url::parse("file:///b/mod.ts").unwrap(),
42194218
CheckSpecifierKind::Dynamic,
42204219
false,
42214220
));

runtime/permissions/prompter.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use std::io::StderrLock;
77
use std::io::StdinLock;
88
use std::io::Write as IoWrite;
99

10-
use deno_core::error::JsStackFrame;
11-
use deno_core::parking_lot::Mutex;
1210
use deno_terminal::colors;
1311
use once_cell::sync::Lazy;
12+
use parking_lot::Mutex;
1413

1514
use crate::is_standalone;
1615

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

58-
static MAYBE_CURRENT_STACKTRACE: Lazy<Mutex<Option<Vec<JsStackFrame>>>> =
57+
static MAYBE_CURRENT_STACKTRACE: Lazy<Mutex<Option<GetFormattedStackFn>>> =
5958
Lazy::new(|| Mutex::new(None));
6059

61-
pub fn set_current_stacktrace(trace: Vec<JsStackFrame>) {
62-
*MAYBE_CURRENT_STACKTRACE.lock() = Some(trace);
60+
pub fn set_current_stacktrace(get_stack: GetFormattedStackFn) {
61+
*MAYBE_CURRENT_STACKTRACE.lock() = Some(get_stack);
6362
}
6463

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

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

97+
pub type GetFormattedStackFn = Box<dyn FnOnce() -> Vec<String> + Send + Sync>;
98+
9899
pub trait PermissionPrompter: Send + Sync {
99100
fn prompt(
100101
&mut self,
101102
message: &str,
102103
name: &str,
103104
api_name: Option<&str>,
104105
is_unary: bool,
105-
stack: Option<Vec<JsStackFrame>>,
106+
get_stack: Option<GetFormattedStackFn>,
106107
) -> PromptResponse;
107108
}
108109

109110
pub struct TtyPrompter;
111+
110112
#[cfg(unix)]
111113
fn clear_stdin(
112114
_stdin_lock: &mut StdinLock,
@@ -294,7 +296,7 @@ impl PermissionPrompter for TtyPrompter {
294296
name: &str,
295297
api_name: Option<&str>,
296298
is_unary: bool,
297-
stack: Option<Vec<JsStackFrame>>,
299+
get_stack: Option<GetFormattedStackFn>,
298300
) -> PromptResponse {
299301
if !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() {
300302
return PromptResponse::Deny;
@@ -351,16 +353,15 @@ impl PermissionPrompter for TtyPrompter {
351353
)
352354
.unwrap();
353355
}
354-
let stack_lines_count = if let Some(stack) = stack {
356+
let stack_lines_count = if let Some(get_stack) = get_stack {
357+
let stack = get_stack();
355358
let len = stack.len();
356359
for (idx, frame) in stack.into_iter().enumerate() {
357360
writeln!(
358361
&mut output,
359362
"┃ {} {}",
360363
colors::gray(if idx != len - 1 { "├─" } else { "└─" }),
361-
colors::gray(deno_core::error::format_frame::<
362-
deno_core::error::NoAnsiColors,
363-
>(&frame))
364+
colors::gray(frame),
364365
)
365366
.unwrap();
366367
}
@@ -490,7 +491,7 @@ pub mod tests {
490491
_name: &str,
491492
_api_name: Option<&str>,
492493
_is_unary: bool,
493-
_stack: Option<Vec<JsStackFrame>>,
494+
_get_stack: Option<GetFormattedStackFn>,
494495
) -> PromptResponse {
495496
if STUB_PROMPT_VALUE.load(Ordering::SeqCst) {
496497
PromptResponse::Allow

runtime/web_worker.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,9 @@ impl WebWorker {
632632
validate_import_attributes_callback,
633633
)),
634634
import_assertions_support: deno_core::ImportAssertionsSupport::Error,
635-
maybe_op_stack_trace_callback: if options.enable_stack_trace_arg_in_ops {
636-
Some(Box::new(|stack| {
637-
deno_permissions::prompter::set_current_stacktrace(stack)
638-
}))
639-
} else {
640-
None
641-
},
635+
maybe_op_stack_trace_callback: options
636+
.enable_stack_trace_arg_in_ops
637+
.then(crate::worker::create_permissions_stack_trace_callback),
642638
..Default::default()
643639
});
644640

runtime/worker.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,15 +1128,28 @@ fn common_runtime(
11281128
validate_import_attributes_callback,
11291129
)),
11301130
import_assertions_support: deno_core::ImportAssertionsSupport::Error,
1131-
maybe_op_stack_trace_callback: enable_stack_trace_arg_in_ops.then(|| {
1132-
Box::new(|stack| {
1133-
deno_permissions::prompter::set_current_stacktrace(stack)
1134-
}) as _
1135-
}),
1131+
maybe_op_stack_trace_callback: enable_stack_trace_arg_in_ops
1132+
.then(create_permissions_stack_trace_callback),
11361133
..Default::default()
11371134
})
11381135
}
11391136

1137+
pub fn create_permissions_stack_trace_callback(
1138+
) -> deno_core::OpStackTraceCallback {
1139+
Box::new(|stack: Vec<deno_core::error::JsStackFrame>| {
1140+
deno_permissions::prompter::set_current_stacktrace(Box::new(|| {
1141+
stack
1142+
.into_iter()
1143+
.map(|frame| {
1144+
deno_core::error::format_frame::<deno_core::error::NoAnsiColors>(
1145+
&frame,
1146+
)
1147+
})
1148+
.collect()
1149+
}))
1150+
}) as _
1151+
}
1152+
11401153
pub struct UnconfiguredRuntime {
11411154
module_loader: Rc<PlaceholderModuleLoader>,
11421155
js_runtime: JsRuntime,

0 commit comments

Comments
 (0)