2021-01-12 02:13:41 +09:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 02:34:02 +02:00
|
|
|
|
2021-10-11 08:26:22 +11:00
|
|
|
use crate::emit::TypeLib;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2021-10-11 08:26:22 +11:00
|
|
|
|
2020-09-14 18:48:57 +02:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 18:36:37 +02:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::futures::Future;
|
2020-04-30 14:37:06 +02:00
|
|
|
use deno_core::ModuleLoadId;
|
2020-03-02 19:12:49 +01:00
|
|
|
use deno_core::ModuleLoader;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-20 01:17:35 +02:00
|
|
|
use deno_core::OpState;
|
2020-12-13 19:45:53 +01:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-02-08 20:34:31 +01:00
|
|
|
use std::cell::RefCell;
|
2019-11-17 01:17:47 +01:00
|
|
|
use std::pin::Pin;
|
2020-02-08 20:34:31 +01:00
|
|
|
use std::rc::Rc;
|
2019-07-31 19:16:03 +02:00
|
|
|
use std::str;
|
2020-04-03 19:40:11 +02:00
|
|
|
|
2021-10-11 08:26:22 +11:00
|
|
|
pub(crate) struct CliModuleLoader {
|
2020-11-02 13:51:56 +11:00
|
|
|
pub lib: TypeLib,
|
2021-05-17 09:44:38 +02:00
|
|
|
/// The initial set of permissions used to resolve the static imports in the
|
|
|
|
/// worker. They are decoupled from the worker (dynamic) permissions since
|
|
|
|
/// read access errors must be raised based on the parent thread permissions.
|
|
|
|
pub root_permissions: Permissions,
|
2021-09-24 11:10:42 -04:00
|
|
|
pub ps: ProcState,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2020-09-20 01:17:35 +02:00
|
|
|
impl CliModuleLoader {
|
2021-09-24 11:10:42 -04:00
|
|
|
pub fn new(ps: ProcState) -> Rc<Self> {
|
|
|
|
let lib = if ps.flags.unstable {
|
2020-12-07 11:03:03 +01:00
|
|
|
TypeLib::UnstableDenoWindow
|
|
|
|
} else {
|
|
|
|
TypeLib::DenoWindow
|
|
|
|
};
|
|
|
|
|
2020-09-20 01:17:35 +02:00
|
|
|
Rc::new(CliModuleLoader {
|
2020-12-07 11:03:03 +01:00
|
|
|
lib,
|
2021-05-17 09:44:38 +02:00
|
|
|
root_permissions: Permissions::allow_all(),
|
2021-09-24 11:10:42 -04:00
|
|
|
ps,
|
2020-09-20 01:17:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
pub fn new_for_worker(ps: ProcState, permissions: Permissions) -> Rc<Self> {
|
|
|
|
let lib = if ps.flags.unstable {
|
2020-12-07 11:03:03 +01:00
|
|
|
TypeLib::UnstableDenoWorker
|
|
|
|
} else {
|
|
|
|
TypeLib::DenoWorker
|
|
|
|
};
|
|
|
|
|
2020-09-20 01:17:35 +02:00
|
|
|
Rc::new(CliModuleLoader {
|
2020-12-07 11:03:03 +01:00
|
|
|
lib,
|
2021-05-17 09:44:38 +02:00
|
|
|
root_permissions: permissions,
|
2021-09-24 11:10:42 -04:00
|
|
|
ps,
|
2020-09-20 01:17:35 +02:00
|
|
|
})
|
|
|
|
}
|
2020-04-28 01:12:38 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 01:17:35 +02:00
|
|
|
impl ModuleLoader for CliModuleLoader {
|
2019-06-09 15:08:20 +02:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2021-10-11 08:26:22 +11:00
|
|
|
_is_main: bool,
|
2020-09-14 18:48:57 +02:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2021-10-11 08:26:22 +11:00
|
|
|
self.ps.resolve(specifier, referrer)
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 01:55:59 +02:00
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 15:33:23 +01:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2021-10-11 08:26:22 +11:00
|
|
|
is_dynamic: bool,
|
2020-03-02 19:12:49 +01:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2020-12-15 16:52:55 +11:00
|
|
|
let module_specifier = module_specifier.clone();
|
2021-09-24 11:10:42 -04:00
|
|
|
let ps = self.ps.clone();
|
2020-05-11 13:13:27 +02:00
|
|
|
|
2021-10-11 08:26:22 +11:00
|
|
|
// NOTE: this block is async only because of `deno_core` interface
|
|
|
|
// requirements; module was already loaded when constructing module graph
|
|
|
|
// during call to `prepare_load`.
|
|
|
|
async move { ps.load(module_specifier, maybe_referrer, is_dynamic) }
|
|
|
|
.boxed_local()
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2020-04-30 14:37:06 +02:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
2020-09-20 01:17:35 +02:00
|
|
|
op_state: Rc<RefCell<OpState>>,
|
2020-04-30 14:37:06 +02:00
|
|
|
_load_id: ModuleLoadId,
|
2020-10-23 11:50:15 +11:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<String>,
|
|
|
|
is_dynamic: bool,
|
2020-09-14 18:48:57 +02:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
|
2020-10-23 11:50:15 +11:00
|
|
|
let specifier = specifier.clone();
|
2021-09-24 11:10:42 -04:00
|
|
|
let ps = self.ps.clone();
|
2020-09-20 01:17:35 +02:00
|
|
|
let state = op_state.borrow();
|
|
|
|
|
2021-05-17 09:44:38 +02:00
|
|
|
let dynamic_permissions = state.borrow::<Permissions>().clone();
|
2021-10-11 08:26:22 +11:00
|
|
|
let root_permissions = if is_dynamic {
|
|
|
|
dynamic_permissions.clone()
|
|
|
|
} else {
|
|
|
|
self.root_permissions.clone()
|
|
|
|
};
|
2021-01-06 15:31:16 -05:00
|
|
|
|
2021-10-11 08:26:22 +11:00
|
|
|
let lib = match self.lib {
|
|
|
|
TypeLib::DenoWindow => crate::emit::TypeLib::DenoWindow,
|
|
|
|
TypeLib::DenoWorker => crate::emit::TypeLib::DenoWorker,
|
|
|
|
TypeLib::UnstableDenoWindow => crate::emit::TypeLib::UnstableDenoWindow,
|
|
|
|
TypeLib::UnstableDenoWorker => crate::emit::TypeLib::UnstableDenoWorker,
|
|
|
|
};
|
2020-09-20 01:17:35 +02:00
|
|
|
drop(state);
|
|
|
|
|
2020-05-29 16:32:15 +02:00
|
|
|
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
|
|
|
|
async move {
|
2021-09-24 11:10:42 -04:00
|
|
|
ps.prepare_module_load(
|
2021-10-11 08:26:22 +11:00
|
|
|
vec![specifier],
|
|
|
|
is_dynamic,
|
2021-09-24 11:10:42 -04:00
|
|
|
lib,
|
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2021-11-15 23:25:52 +00:00
|
|
|
false,
|
2021-09-24 11:10:42 -04:00
|
|
|
)
|
|
|
|
.await
|
2020-05-29 16:32:15 +02:00
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 14:37:06 +02:00
|
|
|
}
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|