2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
use crate::permissions::resolve_fs_allowlist;
|
|
|
|
use crate::permissions::PermissionState;
|
2020-05-04 14:10:59 -04:00
|
|
|
use crate::permissions::Permissions;
|
2021-01-06 15:31:16 -05:00
|
|
|
use crate::permissions::UnaryPermission;
|
2020-12-06 22:30:40 -05:00
|
|
|
use crate::web_worker::run_web_worker;
|
2020-11-26 09:17:45 -05:00
|
|
|
use crate::web_worker::WebWorker;
|
|
|
|
use crate::web_worker::WebWorkerHandle;
|
|
|
|
use crate::web_worker::WorkerEvent;
|
2021-01-06 15:31:16 -05:00
|
|
|
use deno_core::error::custom_error;
|
2020-10-20 00:05:42 -04:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-11-20 07:25:18 -05:00
|
|
|
use deno_core::error::JsError;
|
2020-10-20 00:05:42 -04:00
|
|
|
use deno_core::futures::channel::mpsc;
|
2021-01-06 15:31:16 -05:00
|
|
|
use deno_core::serde::de;
|
|
|
|
use deno_core::serde::de::SeqAccess;
|
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Deserializer;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-09-19 19:17:35 -04:00
|
|
|
use std::collections::HashMap;
|
2021-01-06 15:31:16 -05:00
|
|
|
use std::collections::HashSet;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::convert::From;
|
2021-01-06 15:31:16 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::path::PathBuf;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-12-11 12:49:26 -05:00
|
|
|
use std::sync::Arc;
|
2020-02-18 14:47:11 -05:00
|
|
|
use std::thread::JoinHandle;
|
2019-10-11 14:41:54 -04:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
pub struct CreateWebWorkerArgs {
|
|
|
|
pub name: String,
|
|
|
|
pub worker_id: u32,
|
2021-01-06 15:31:16 -05:00
|
|
|
pub parent_permissions: Permissions,
|
2020-12-11 12:49:26 -05:00
|
|
|
pub permissions: Permissions,
|
|
|
|
pub main_module: ModuleSpecifier,
|
|
|
|
pub use_deno_namespace: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type CreateWebWorkerCb =
|
|
|
|
dyn Fn(CreateWebWorkerArgs) -> WebWorker + Sync + Send;
|
|
|
|
|
|
|
|
/// A holder for callback that is used to create a new
|
|
|
|
/// WebWorker. It's a struct instead of a type alias
|
|
|
|
/// because `GothamState` used in `OpState` overrides
|
|
|
|
/// value if type alises have the same underlying type
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct CreateWebWorkerCbHolder(Arc<CreateWebWorkerCb>);
|
|
|
|
|
2020-10-20 00:05:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct HostUnhandledErrorArgs {
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
pub struct WorkerThread {
|
|
|
|
join_handle: JoinHandle<Result<(), AnyError>>,
|
|
|
|
worker_handle: WebWorkerHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type WorkersTable = HashMap<u32, WorkerThread>;
|
|
|
|
pub type WorkerId = u32;
|
|
|
|
|
2020-10-20 00:05:42 -04:00
|
|
|
pub fn init(
|
|
|
|
rt: &mut deno_core::JsRuntime,
|
|
|
|
sender: Option<mpsc::Sender<WorkerEvent>>,
|
2020-12-11 12:49:26 -05:00
|
|
|
create_web_worker_cb: Arc<CreateWebWorkerCb>,
|
2020-10-20 00:05:42 -04:00
|
|
|
) {
|
2020-09-28 06:14:11 -04:00
|
|
|
{
|
|
|
|
let op_state = rt.op_state();
|
|
|
|
let mut state = op_state.borrow_mut();
|
|
|
|
state.put::<WorkersTable>(WorkersTable::default());
|
|
|
|
state.put::<WorkerId>(WorkerId::default());
|
2020-12-11 12:49:26 -05:00
|
|
|
|
|
|
|
let create_module_loader = CreateWebWorkerCbHolder(create_web_worker_cb);
|
|
|
|
state.put::<CreateWebWorkerCbHolder>(create_module_loader);
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_create_worker", op_create_worker);
|
|
|
|
super::reg_json_sync(
|
|
|
|
rt,
|
|
|
|
"op_host_terminate_worker",
|
|
|
|
op_host_terminate_worker,
|
|
|
|
);
|
|
|
|
super::reg_json_sync(rt, "op_host_post_message", op_host_post_message);
|
|
|
|
super::reg_json_async(rt, "op_host_get_message", op_host_get_message);
|
2020-10-20 00:05:42 -04:00
|
|
|
super::reg_json_sync(
|
|
|
|
rt,
|
|
|
|
"op_host_unhandled_error",
|
|
|
|
move |_state, args, _zero_copy| {
|
|
|
|
if let Some(mut sender) = sender.clone() {
|
|
|
|
let args: HostUnhandledErrorArgs = serde_json::from_value(args)?;
|
|
|
|
sender
|
|
|
|
.try_send(WorkerEvent::Error(generic_error(args.message)))
|
|
|
|
.expect("Failed to propagate error event to parent worker");
|
|
|
|
Ok(json!(true))
|
|
|
|
} else {
|
|
|
|
Err(generic_error("Cannot be called from main worker."))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2020-02-11 04:04:59 -05:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
fn merge_permission_state(
|
|
|
|
target: &PermissionState,
|
|
|
|
incoming: Option<PermissionState>,
|
|
|
|
) -> Result<PermissionState, AnyError> {
|
|
|
|
match target {
|
|
|
|
PermissionState::Granted => match incoming {
|
|
|
|
Some(x) => Ok(x),
|
|
|
|
None => Ok(*target),
|
|
|
|
},
|
|
|
|
_ => match incoming {
|
|
|
|
Some(x) => match x {
|
|
|
|
PermissionState::Denied => Ok(x),
|
|
|
|
_ => Err(custom_error(
|
|
|
|
"PermissionDenied",
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
None => Ok(*target),
|
|
|
|
},
|
|
|
|
}
|
2020-02-11 04:04:59 -05:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
fn check_net_permission_contains(
|
|
|
|
a: &HashSet<String>,
|
|
|
|
b: &HashSet<String>,
|
|
|
|
) -> bool {
|
|
|
|
b.iter().all(|x| a.contains(x))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_net_permissions(
|
|
|
|
target: &UnaryPermission<String>,
|
|
|
|
incoming: Option<UnaryPermission<String>>,
|
|
|
|
) -> Result<UnaryPermission<String>, AnyError> {
|
|
|
|
if incoming.is_none() {
|
|
|
|
return Ok(target.clone());
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_permissions = incoming.unwrap();
|
|
|
|
match &target.global_state {
|
|
|
|
PermissionState::Granted => Ok(UnaryPermission::<String> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: new_permissions.denied_list,
|
|
|
|
}),
|
|
|
|
PermissionState::Prompt => match new_permissions.global_state {
|
|
|
|
//Throw
|
|
|
|
PermissionState::Granted => Err(custom_error(
|
|
|
|
"PermissionDenied",
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
)),
|
|
|
|
//Merge
|
|
|
|
PermissionState::Prompt => {
|
|
|
|
if check_net_permission_contains(
|
|
|
|
&target.granted_list,
|
|
|
|
&new_permissions.granted_list,
|
|
|
|
) {
|
|
|
|
Ok(UnaryPermission::<String> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: target.denied_list.clone(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"PermissionDenied",
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Copy
|
|
|
|
PermissionState::Denied => Ok(UnaryPermission::<String> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: new_permissions.denied_list,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
PermissionState::Denied => match new_permissions.global_state {
|
|
|
|
PermissionState::Denied => Ok(UnaryPermission::<String> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: new_permissions.denied_list,
|
|
|
|
}),
|
|
|
|
_ => Err(custom_error(
|
|
|
|
"PermissionDenied",
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum WorkerPermissionType {
|
|
|
|
READ,
|
|
|
|
WRITE,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read_permissions(
|
|
|
|
allow_list: &HashSet<PathBuf>,
|
|
|
|
current_permissions: &Permissions,
|
|
|
|
) -> bool {
|
|
|
|
allow_list
|
|
|
|
.iter()
|
|
|
|
.all(|x| current_permissions.check_read(&x).is_ok())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write_permissions(
|
|
|
|
allow_list: &HashSet<PathBuf>,
|
|
|
|
current_permissions: &Permissions,
|
|
|
|
) -> bool {
|
|
|
|
allow_list
|
|
|
|
.iter()
|
|
|
|
.all(|x| current_permissions.check_write(&x).is_ok())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_read_write_permissions(
|
|
|
|
permission_type: WorkerPermissionType,
|
|
|
|
target: &UnaryPermission<PathBuf>,
|
|
|
|
incoming: Option<UnaryPermission<PathBuf>>,
|
|
|
|
current_permissions: &Permissions,
|
|
|
|
) -> Result<UnaryPermission<PathBuf>, AnyError> {
|
|
|
|
if incoming.is_none() {
|
|
|
|
return Ok(target.clone());
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_permissions = incoming.unwrap();
|
|
|
|
match &target.global_state {
|
|
|
|
PermissionState::Granted => Ok(UnaryPermission::<PathBuf> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: new_permissions.denied_list,
|
|
|
|
}),
|
|
|
|
PermissionState::Prompt => match new_permissions.global_state {
|
|
|
|
//Throw
|
|
|
|
PermissionState::Granted => Err(custom_error(
|
|
|
|
"PermissionDenied",
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
)),
|
|
|
|
//Merge
|
|
|
|
PermissionState::Prompt => {
|
|
|
|
if match permission_type {
|
|
|
|
WorkerPermissionType::READ => check_read_permissions(
|
|
|
|
&new_permissions.granted_list,
|
|
|
|
current_permissions,
|
|
|
|
),
|
|
|
|
WorkerPermissionType::WRITE => check_write_permissions(
|
|
|
|
&new_permissions.granted_list,
|
|
|
|
current_permissions,
|
|
|
|
),
|
|
|
|
} {
|
|
|
|
Ok(UnaryPermission::<PathBuf> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: target.denied_list.clone(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"PermissionDenied",
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Copy
|
|
|
|
PermissionState::Denied => Ok(UnaryPermission::<PathBuf> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: new_permissions.denied_list,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
PermissionState::Denied => match new_permissions.global_state {
|
|
|
|
PermissionState::Denied => Ok(UnaryPermission::<PathBuf> {
|
|
|
|
global_state: new_permissions.global_state,
|
|
|
|
granted_list: new_permissions.granted_list,
|
|
|
|
denied_list: new_permissions.denied_list,
|
|
|
|
}),
|
|
|
|
_ => Err(custom_error(
|
|
|
|
"PermissionDenied",
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_worker_permissions(
|
|
|
|
main_thread_permissions: &Permissions,
|
|
|
|
permission_args: PermissionsArg,
|
|
|
|
) -> Result<Permissions, AnyError> {
|
|
|
|
Ok(Permissions {
|
|
|
|
env: merge_permission_state(
|
|
|
|
&main_thread_permissions.env,
|
|
|
|
permission_args.env,
|
|
|
|
)?,
|
|
|
|
hrtime: merge_permission_state(
|
|
|
|
&main_thread_permissions.hrtime,
|
|
|
|
permission_args.hrtime,
|
|
|
|
)?,
|
|
|
|
net: merge_net_permissions(
|
|
|
|
&main_thread_permissions.net,
|
|
|
|
permission_args.net,
|
|
|
|
)?,
|
|
|
|
plugin: merge_permission_state(
|
|
|
|
&main_thread_permissions.plugin,
|
|
|
|
permission_args.plugin,
|
|
|
|
)?,
|
|
|
|
read: merge_read_write_permissions(
|
|
|
|
WorkerPermissionType::READ,
|
|
|
|
&main_thread_permissions.read,
|
|
|
|
permission_args.read,
|
|
|
|
&main_thread_permissions,
|
|
|
|
)?,
|
|
|
|
run: merge_permission_state(
|
|
|
|
&main_thread_permissions.run,
|
|
|
|
permission_args.run,
|
|
|
|
)?,
|
|
|
|
write: merge_read_write_permissions(
|
|
|
|
WorkerPermissionType::WRITE,
|
|
|
|
&main_thread_permissions.write,
|
|
|
|
permission_args.write,
|
|
|
|
&main_thread_permissions,
|
|
|
|
)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct PermissionsArg {
|
|
|
|
#[serde(default, deserialize_with = "as_permission_state")]
|
|
|
|
env: Option<PermissionState>,
|
|
|
|
#[serde(default, deserialize_with = "as_permission_state")]
|
|
|
|
hrtime: Option<PermissionState>,
|
|
|
|
#[serde(default, deserialize_with = "as_unary_string_permission")]
|
|
|
|
net: Option<UnaryPermission<String>>,
|
|
|
|
#[serde(default, deserialize_with = "as_permission_state")]
|
|
|
|
plugin: Option<PermissionState>,
|
|
|
|
#[serde(default, deserialize_with = "as_unary_path_permission")]
|
|
|
|
read: Option<UnaryPermission<PathBuf>>,
|
|
|
|
#[serde(default, deserialize_with = "as_permission_state")]
|
|
|
|
run: Option<PermissionState>,
|
|
|
|
#[serde(default, deserialize_with = "as_unary_path_permission")]
|
|
|
|
write: Option<UnaryPermission<PathBuf>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_permission_state<'de, D>(
|
|
|
|
deserializer: D,
|
|
|
|
) -> Result<Option<PermissionState>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let value: bool = Deserialize::deserialize(deserializer)?;
|
|
|
|
|
|
|
|
match value {
|
|
|
|
true => Ok(Some(PermissionState::Granted)),
|
|
|
|
false => Ok(Some(PermissionState::Denied)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UnaryPermissionBase {
|
|
|
|
global_state: PermissionState,
|
|
|
|
paths: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ParseBooleanOrStringVec;
|
|
|
|
|
|
|
|
impl<'de> de::Visitor<'de> for ParseBooleanOrStringVec {
|
|
|
|
type Value = UnaryPermissionBase;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a vector of strings or a boolean")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bool<E>(self, v: bool) -> Result<UnaryPermissionBase, E>
|
|
|
|
where
|
|
|
|
E: de::Error,
|
|
|
|
{
|
|
|
|
Ok(UnaryPermissionBase {
|
|
|
|
global_state: match v {
|
|
|
|
true => PermissionState::Granted,
|
|
|
|
false => PermissionState::Denied,
|
|
|
|
},
|
|
|
|
paths: Vec::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<V>(self, mut visitor: V) -> Result<UnaryPermissionBase, V::Error>
|
|
|
|
where
|
|
|
|
V: SeqAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut vec: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
let mut value = visitor.next_element::<String>()?;
|
|
|
|
while value.is_some() {
|
|
|
|
vec.push(value.unwrap());
|
|
|
|
value = visitor.next_element()?;
|
|
|
|
}
|
|
|
|
Ok(UnaryPermissionBase {
|
|
|
|
global_state: PermissionState::Prompt,
|
|
|
|
paths: vec,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_unary_string_permission<'de, D>(
|
|
|
|
deserializer: D,
|
|
|
|
) -> Result<Option<UnaryPermission<String>>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let value: UnaryPermissionBase =
|
|
|
|
deserializer.deserialize_any(ParseBooleanOrStringVec)?;
|
|
|
|
|
|
|
|
let allowed: HashSet<String> = value.paths.into_iter().collect();
|
|
|
|
|
|
|
|
Ok(Some(UnaryPermission::<String> {
|
|
|
|
global_state: value.global_state,
|
|
|
|
granted_list: allowed,
|
|
|
|
..Default::default()
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_unary_path_permission<'de, D>(
|
|
|
|
deserializer: D,
|
|
|
|
) -> Result<Option<UnaryPermission<PathBuf>>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let value: UnaryPermissionBase =
|
|
|
|
deserializer.deserialize_any(ParseBooleanOrStringVec)?;
|
|
|
|
|
|
|
|
let paths: Vec<PathBuf> =
|
|
|
|
value.paths.into_iter().map(PathBuf::from).collect();
|
|
|
|
|
|
|
|
Ok(Some(UnaryPermission::<PathBuf> {
|
|
|
|
global_state: value.global_state,
|
|
|
|
granted_list: resolve_fs_allowlist(&Some(paths)),
|
|
|
|
..Default::default()
|
|
|
|
}))
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CreateWorkerArgs {
|
|
|
|
has_source_code: bool,
|
2021-01-06 15:31:16 -05:00
|
|
|
name: Option<String>,
|
|
|
|
permissions: Option<PermissionsArg>,
|
2019-08-26 08:50:21 -04:00
|
|
|
source_code: String,
|
2021-01-06 15:31:16 -05:00
|
|
|
specifier: String,
|
2020-04-16 17:40:29 -04:00
|
|
|
use_deno_namespace: bool,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create worker as the host
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_create_worker(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_data: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: CreateWorkerArgs = serde_json::from_value(args)?;
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
let specifier = args.specifier.clone();
|
2020-04-16 17:40:29 -04:00
|
|
|
let maybe_source_code = if args.has_source_code {
|
|
|
|
Some(args.source_code.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-02-03 18:08:44 -05:00
|
|
|
let args_name = args.name;
|
2020-04-16 17:40:29 -04:00
|
|
|
let use_deno_namespace = args.use_deno_namespace;
|
2020-05-07 15:15:59 -04:00
|
|
|
if use_deno_namespace {
|
2021-01-06 15:31:16 -05:00
|
|
|
super::check_unstable(state, "Worker.deno.namespace");
|
2020-05-07 15:15:59 -04:00
|
|
|
}
|
2021-01-06 15:31:16 -05:00
|
|
|
let parent_permissions = state.borrow::<Permissions>().clone();
|
|
|
|
let worker_permissions = if let Some(permissions) = args.permissions {
|
|
|
|
super::check_unstable(state, "Worker.deno.permissions");
|
|
|
|
create_worker_permissions(&parent_permissions, permissions)?
|
|
|
|
} else {
|
|
|
|
parent_permissions.clone()
|
|
|
|
};
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
let worker_id = state.take::<WorkerId>();
|
2020-12-11 12:49:26 -05:00
|
|
|
let create_module_loader = state.take::<CreateWebWorkerCbHolder>();
|
|
|
|
state.put::<CreateWebWorkerCbHolder>(create_module_loader.clone());
|
2020-09-19 19:17:35 -04:00
|
|
|
state.put::<WorkerId>(worker_id + 1);
|
2020-02-08 14:34:31 -05:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
let module_specifier = ModuleSpecifier::resolve_url(&specifier)?;
|
2020-04-14 11:41:06 -04:00
|
|
|
let worker_name = args_name.unwrap_or_else(|| "".to_string());
|
2020-12-06 22:30:40 -05:00
|
|
|
|
|
|
|
let (handle_sender, handle_receiver) =
|
|
|
|
std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, AnyError>>(1);
|
|
|
|
|
|
|
|
// Setup new thread
|
|
|
|
let thread_builder =
|
|
|
|
std::thread::Builder::new().name(format!("deno-worker-{}", worker_id));
|
|
|
|
|
|
|
|
// Spawn it
|
|
|
|
let join_handle = thread_builder.spawn(move || {
|
|
|
|
// Any error inside this block is terminal:
|
|
|
|
// - JS worker is useless - meaning it throws an exception and can't do anything else,
|
|
|
|
// all action done upon it should be noops
|
|
|
|
// - newly spawned thread exits
|
2020-12-11 12:49:26 -05:00
|
|
|
|
|
|
|
let worker = (create_module_loader.0)(CreateWebWorkerArgs {
|
|
|
|
name: worker_name,
|
|
|
|
worker_id,
|
2021-01-06 15:31:16 -05:00
|
|
|
parent_permissions,
|
|
|
|
permissions: worker_permissions,
|
2020-12-11 12:49:26 -05:00
|
|
|
main_module: module_specifier.clone(),
|
2020-12-06 22:30:40 -05:00
|
|
|
use_deno_namespace,
|
2020-12-11 12:49:26 -05:00
|
|
|
});
|
2020-12-06 22:30:40 -05:00
|
|
|
|
|
|
|
// Send thread safe handle to newly created worker to host thread
|
|
|
|
handle_sender.send(Ok(worker.thread_safe_handle())).unwrap();
|
|
|
|
drop(handle_sender);
|
|
|
|
|
|
|
|
// At this point the only method of communication with host
|
|
|
|
// is using `worker.internal_channels`.
|
|
|
|
//
|
|
|
|
// Host can already push messages and interact with worker.
|
|
|
|
run_web_worker(worker, module_specifier, maybe_source_code)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let worker_handle = handle_receiver.recv().unwrap()?;
|
|
|
|
|
|
|
|
let worker_thread = WorkerThread {
|
|
|
|
join_handle,
|
|
|
|
worker_handle,
|
|
|
|
};
|
2020-01-17 18:43:53 -05:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
// At this point all interactions with worker happen using thread
|
2020-12-06 22:30:40 -05:00
|
|
|
// safe handler returned from previous function calls
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<WorkersTable>()
|
2020-12-06 22:30:40 -05:00
|
|
|
.insert(worker_id, worker_thread);
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({ "id": worker_id }))
|
2019-11-16 19:17:47 -05:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
2020-01-17 18:43:53 -05:00
|
|
|
struct WorkerArgs {
|
2019-11-09 15:07:14 -05:00
|
|
|
id: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
fn op_host_terminate_worker(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-01-17 18:43:53 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_data: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-01-17 18:43:53 -05:00
|
|
|
let args: WorkerArgs = serde_json::from_value(args)?;
|
|
|
|
let id = args.id as u32;
|
2020-12-06 22:30:40 -05:00
|
|
|
let worker_thread = state
|
2020-09-19 19:17:35 -04:00
|
|
|
.borrow_mut::<WorkersTable>()
|
2020-08-18 12:30:13 -04:00
|
|
|
.remove(&id)
|
|
|
|
.expect("No worker handle found");
|
2020-12-06 22:30:40 -05:00
|
|
|
worker_thread.worker_handle.terminate();
|
|
|
|
worker_thread
|
|
|
|
.join_handle
|
|
|
|
.join()
|
|
|
|
.expect("Panic in worker thread")
|
|
|
|
.expect("Panic in worker event loop");
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-01-17 18:43:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
fn serialize_worker_event(event: WorkerEvent) -> Value {
|
|
|
|
match event {
|
|
|
|
WorkerEvent::Message(buf) => json!({ "type": "msg", "data": buf }),
|
2020-11-20 07:25:18 -05:00
|
|
|
WorkerEvent::TerminalError(error) => match error.downcast::<JsError>() {
|
2020-11-22 08:06:51 -05:00
|
|
|
Ok(js_error) => json!({
|
|
|
|
"type": "terminalError",
|
|
|
|
"error": {
|
|
|
|
"message": js_error.message,
|
|
|
|
"fileName": js_error.script_resource_name,
|
|
|
|
"lineNumber": js_error.line_number,
|
|
|
|
"columnNumber": js_error.start_column,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
Err(error) => json!({
|
|
|
|
"type": "terminalError",
|
|
|
|
"error": {
|
|
|
|
"message": error.to_string(),
|
|
|
|
}
|
|
|
|
}),
|
2020-11-20 07:25:18 -05:00
|
|
|
},
|
|
|
|
WorkerEvent::Error(error) => match error.downcast::<JsError>() {
|
2020-11-22 08:06:51 -05:00
|
|
|
Ok(js_error) => json!({
|
|
|
|
"type": "error",
|
|
|
|
"error": {
|
|
|
|
"message": js_error.message,
|
|
|
|
"fileName": js_error.script_resource_name,
|
|
|
|
"lineNumber": js_error.line_number,
|
|
|
|
"columnNumber": js_error.start_column,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
Err(error) => json!({
|
|
|
|
"type": "error",
|
|
|
|
"error": {
|
|
|
|
"message": error.to_string(),
|
|
|
|
}
|
|
|
|
}),
|
2020-11-20 07:25:18 -05:00
|
|
|
},
|
2020-02-11 04:04:59 -05:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-06 22:30:40 -05:00
|
|
|
/// Try to remove worker from workers table - NOTE: `Worker.terminate()`
|
|
|
|
/// might have been called already meaning that we won't find worker in
|
|
|
|
/// table - in that case ignore.
|
|
|
|
fn try_remove_and_close(state: Rc<RefCell<OpState>>, id: u32) {
|
|
|
|
let mut s = state.borrow_mut();
|
|
|
|
let workers = s.borrow_mut::<WorkersTable>();
|
|
|
|
if let Some(mut worker_thread) = workers.remove(&id) {
|
|
|
|
worker_thread.worker_handle.sender.close_channel();
|
|
|
|
worker_thread
|
|
|
|
.join_handle
|
|
|
|
.join()
|
|
|
|
.expect("Worker thread panicked")
|
|
|
|
.expect("Panic in worker event loop");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 11:03:02 -04:00
|
|
|
/// Get message from guest worker as host
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_host_get_message(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-02-11 04:04:59 -05:00
|
|
|
let args: WorkerArgs = serde_json::from_value(args)?;
|
2019-11-09 15:07:14 -05:00
|
|
|
let id = args.id as u32;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
let worker_handle = {
|
2020-09-19 19:17:35 -04:00
|
|
|
let s = state.borrow();
|
|
|
|
let workers_table = s.borrow::<WorkersTable>();
|
2020-09-10 09:57:45 -04:00
|
|
|
let maybe_handle = workers_table.get(&id);
|
|
|
|
if let Some(handle) = maybe_handle {
|
2020-12-06 22:30:40 -05:00
|
|
|
handle.worker_handle.clone()
|
2020-09-10 09:57:45 -04:00
|
|
|
} else {
|
|
|
|
// If handle was not found it means worker has already shutdown
|
|
|
|
return Ok(json!({ "type": "close" }));
|
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
};
|
|
|
|
|
2020-12-06 22:30:40 -05:00
|
|
|
let maybe_event = worker_handle.get_event().await?;
|
|
|
|
if let Some(event) = maybe_event {
|
|
|
|
// Terminal error means that worker should be removed from worker table.
|
|
|
|
if let WorkerEvent::TerminalError(_) = &event {
|
|
|
|
try_remove_and_close(state, id);
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-12-06 22:30:40 -05:00
|
|
|
return Ok(serialize_worker_event(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was no event from worker it means it has already been closed.
|
|
|
|
try_remove_and_close(state, id);
|
|
|
|
Ok(json!({ "type": "close" }))
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2019-08-14 11:03:02 -04:00
|
|
|
/// Post message to guest worker as host
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_host_post_message(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
data: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(data.len(), 1, "Invalid number of arguments");
|
2020-02-11 04:04:59 -05:00
|
|
|
let args: WorkerArgs = serde_json::from_value(args)?;
|
2019-11-09 15:07:14 -05:00
|
|
|
let id = args.id as u32;
|
2020-06-01 14:20:47 -04:00
|
|
|
let msg = Vec::from(&*data[0]).into_boxed_slice();
|
2019-11-09 15:07:14 -05:00
|
|
|
|
|
|
|
debug!("post message to worker {}", id);
|
2020-12-06 22:30:40 -05:00
|
|
|
let worker_thread = state
|
|
|
|
.borrow::<WorkersTable>()
|
|
|
|
.get(&id)
|
|
|
|
.expect("No worker handle found");
|
|
|
|
worker_thread.worker_handle.post_message(msg)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|