2021-08-26 15:21:58 -04:00
|
|
|
use crate::tools::test::TestEvent;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2021-12-29 08:30:08 -05:00
|
|
|
use deno_core::op_sync;
|
|
|
|
use deno_core::Extension;
|
2021-04-30 11:56:47 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::OpState;
|
2021-10-13 13:04:44 -04:00
|
|
|
use deno_runtime::permissions::create_child_permissions;
|
|
|
|
use deno_runtime::permissions::ChildPermissionsArg;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-04-28 14:17:04 -04:00
|
|
|
use std::sync::mpsc::Sender;
|
2021-04-25 17:38:59 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-12-29 08:30:08 -05:00
|
|
|
pub fn init(sender: Sender<TestEvent>) -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
|
|
|
(
|
|
|
|
"op_pledge_test_permissions",
|
|
|
|
op_sync(op_pledge_test_permissions),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"op_restore_test_permissions",
|
|
|
|
op_sync(op_restore_test_permissions),
|
|
|
|
),
|
|
|
|
("op_get_test_origin", op_sync(op_get_test_origin)),
|
|
|
|
("op_dispatch_test_event", op_sync(op_dispatch_test_event)),
|
|
|
|
])
|
|
|
|
.state(move |state| {
|
|
|
|
state.put(sender.clone());
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build()
|
2021-04-25 17:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct PermissionsHolder(Uuid, Permissions);
|
|
|
|
|
|
|
|
pub fn op_pledge_test_permissions(
|
|
|
|
state: &mut OpState,
|
2021-10-13 13:04:44 -04:00
|
|
|
args: ChildPermissionsArg,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-25 17:38:59 -04:00
|
|
|
) -> Result<Uuid, AnyError> {
|
|
|
|
let token = Uuid::new_v4();
|
2021-10-13 13:04:44 -04:00
|
|
|
let parent_permissions = state.borrow_mut::<Permissions>();
|
|
|
|
let worker_permissions = create_child_permissions(parent_permissions, args)?;
|
|
|
|
let parent_permissions = parent_permissions.clone();
|
2021-04-25 17:38:59 -04:00
|
|
|
|
|
|
|
state.put::<PermissionsHolder>(PermissionsHolder(token, parent_permissions));
|
|
|
|
|
|
|
|
// NOTE: This call overrides current permission set for the worker
|
|
|
|
state.put::<Permissions>(worker_permissions);
|
|
|
|
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_restore_test_permissions(
|
|
|
|
state: &mut OpState,
|
|
|
|
token: Uuid,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-25 17:38:59 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if let Some(permissions_holder) = state.try_take::<PermissionsHolder>() {
|
|
|
|
if token != permissions_holder.0 {
|
|
|
|
panic!("restore test permissions token does not match the stored token");
|
|
|
|
}
|
|
|
|
|
|
|
|
let permissions = permissions_holder.1;
|
|
|
|
state.put::<Permissions>(permissions);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(generic_error("no permissions to restore"))
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn op_get_test_origin(
|
|
|
|
state: &mut OpState,
|
|
|
|
_: (),
|
|
|
|
_: (),
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
Ok(state.borrow::<ModuleSpecifier>().to_string())
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn op_dispatch_test_event(
|
2021-04-28 14:17:04 -04:00
|
|
|
state: &mut OpState,
|
2021-07-14 15:05:16 -04:00
|
|
|
event: TestEvent,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-07-14 15:05:16 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-04-30 11:56:47 -04:00
|
|
|
let sender = state.borrow::<Sender<TestEvent>>().clone();
|
2021-07-14 15:05:16 -04:00
|
|
|
sender.send(event).ok();
|
2021-04-30 11:56:47 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
Ok(())
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|