mirror of
https://github.com/denoland/deno.git
synced 2025-01-10 16:11:13 -05:00
perf(runtime): optimize PermissionState::check (#9993)
This commit is contained in:
parent
875ac73f1e
commit
5c2a8cdbdc
1 changed files with 47 additions and 29 deletions
|
@ -34,6 +34,37 @@ pub enum PermissionState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PermissionState {
|
impl PermissionState {
|
||||||
|
#[inline(always)]
|
||||||
|
fn log_perm_access(name: &str, info: Option<&str>) {
|
||||||
|
debug!(
|
||||||
|
"{}",
|
||||||
|
colors::bold(&format!(
|
||||||
|
"{}️ Granted {}",
|
||||||
|
PERMISSION_EMOJI,
|
||||||
|
Self::fmt_access(name, info)
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fmt_access(name: &str, info: Option<&str>) -> String {
|
||||||
|
format!(
|
||||||
|
"{} access{}",
|
||||||
|
name,
|
||||||
|
info.map_or(String::new(), |info| { format!(" to {}", info) }),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn error(name: &str, info: Option<&str>) -> AnyError {
|
||||||
|
custom_error(
|
||||||
|
"PermissionDenied",
|
||||||
|
format!(
|
||||||
|
"Requires {}, run again with the --allow-{} flag",
|
||||||
|
Self::fmt_access(name, info),
|
||||||
|
name
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Check the permission state. bool is whether a prompt was issued.
|
/// Check the permission state. bool is whether a prompt was issued.
|
||||||
fn check(
|
fn check(
|
||||||
self,
|
self,
|
||||||
|
@ -41,28 +72,22 @@ impl PermissionState {
|
||||||
info: Option<&str>,
|
info: Option<&str>,
|
||||||
prompt: bool,
|
prompt: bool,
|
||||||
) -> (Result<(), AnyError>, bool) {
|
) -> (Result<(), AnyError>, bool) {
|
||||||
let access = format!(
|
match self {
|
||||||
"{} access{}",
|
PermissionState::Granted => {
|
||||||
name,
|
Self::log_perm_access(name, info);
|
||||||
info.map_or(String::new(), |info| { format!(" to {}", info) }),
|
(Ok(()), false)
|
||||||
);
|
}
|
||||||
let result = if self == PermissionState::Granted
|
PermissionState::Prompt if prompt => {
|
||||||
|| (prompt
|
let msg = Self::fmt_access(name, info);
|
||||||
&& self == PermissionState::Prompt
|
if permission_prompt(&msg) {
|
||||||
&& permission_prompt(&access))
|
Self::log_perm_access(name, info);
|
||||||
{
|
(Ok(()), true)
|
||||||
log_perm_access(&access);
|
|
||||||
Ok(())
|
|
||||||
} else {
|
} else {
|
||||||
Err(custom_error(
|
(Err(Self::error(name, info)), true)
|
||||||
"PermissionDenied",
|
}
|
||||||
format!(
|
}
|
||||||
"Requires {}, run again with the --allow-{} flag",
|
_ => (Err(Self::error(name, info)), false),
|
||||||
access, name
|
}
|
||||||
),
|
|
||||||
))
|
|
||||||
};
|
|
||||||
(result, prompt && self == PermissionState::Prompt)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -820,13 +845,6 @@ impl deno_websocket::WebSocketPermissions for Permissions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log_perm_access(message: &str) {
|
|
||||||
debug!(
|
|
||||||
"{}",
|
|
||||||
colors::bold(&format!("{}️ Granted {}", PERMISSION_EMOJI, message))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn boolean_permission_from_flag_bool(
|
fn boolean_permission_from_flag_bool(
|
||||||
flag: bool,
|
flag: bool,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
|
|
Loading…
Reference in a new issue