2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2019-06-09 13:08:20 +00:00
|
|
|
use crate::import_map::ImportMap;
|
2020-11-02 19:41:20 +00:00
|
|
|
use crate::module_graph::TypeLib;
|
2020-10-13 11:35:35 +00:00
|
|
|
use crate::program_state::ProgramState;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::futures::Future;
|
2020-04-30 12:37:06 +00:00
|
|
|
use deno_core::ModuleLoadId;
|
2020-03-02 18:12:49 +00:00
|
|
|
use deno_core::ModuleLoader;
|
2020-01-05 16:56:18 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-19 23:17:35 +00:00
|
|
|
use deno_core::OpState;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-02-08 19:34:31 +00:00
|
|
|
use std::cell::RefCell;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::pin::Pin;
|
2020-02-08 19:34:31 +00:00
|
|
|
use std::rc::Rc;
|
2019-07-31 17:16:03 +00:00
|
|
|
use std::str;
|
2020-08-18 16:30:13 +00:00
|
|
|
use std::sync::Arc;
|
2020-04-03 17:40:11 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
pub struct CliModuleLoader {
|
2019-06-09 13:08:20 +00:00
|
|
|
/// When flags contains a `.import_map_path` option, the content of the
|
|
|
|
/// import map file will be resolved and set.
|
|
|
|
pub import_map: Option<ImportMap>,
|
2020-11-02 02:51:56 +00:00
|
|
|
pub lib: TypeLib,
|
2020-12-07 10:03:03 +00:00
|
|
|
pub program_state: Arc<ProgramState>,
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
impl CliModuleLoader {
|
2020-12-07 10:03:03 +00:00
|
|
|
pub fn new(program_state: Arc<ProgramState>) -> Rc<Self> {
|
|
|
|
let lib = if program_state.flags.unstable {
|
|
|
|
TypeLib::UnstableDenoWindow
|
|
|
|
} else {
|
|
|
|
TypeLib::DenoWindow
|
|
|
|
};
|
|
|
|
|
|
|
|
let import_map = program_state.maybe_import_map.clone();
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
Rc::new(CliModuleLoader {
|
2020-12-07 10:03:03 +00:00
|
|
|
import_map,
|
|
|
|
lib,
|
|
|
|
program_state,
|
2020-09-19 23:17:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:03:03 +00:00
|
|
|
pub fn new_for_worker(program_state: Arc<ProgramState>) -> Rc<Self> {
|
|
|
|
let lib = if program_state.flags.unstable {
|
|
|
|
TypeLib::UnstableDenoWorker
|
|
|
|
} else {
|
|
|
|
TypeLib::DenoWorker
|
|
|
|
};
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
Rc::new(CliModuleLoader {
|
|
|
|
import_map: None,
|
2020-12-07 10:03:03 +00:00
|
|
|
lib,
|
|
|
|
program_state,
|
2020-09-19 23:17:35 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-27 23:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
impl ModuleLoader for CliModuleLoader {
|
2019-06-09 13:08:20 +00:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
2020-12-07 10:03:03 +00:00
|
|
|
_op_state: Rc<RefCell<OpState>>,
|
2019-06-09 13:08:20 +00:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-08-07 16:55:39 +00:00
|
|
|
is_main: bool,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2020-10-02 11:13:23 +00:00
|
|
|
// FIXME(bartlomieju): hacky way to provide compatibility with repl
|
2020-12-07 10:03:03 +00:00
|
|
|
let referrer = if referrer.is_empty() && self.program_state.flags.repl {
|
2020-10-02 11:13:23 +00:00
|
|
|
"<unknown>"
|
|
|
|
} else {
|
|
|
|
referrer
|
|
|
|
};
|
|
|
|
|
2019-08-07 16:55:39 +00:00
|
|
|
if !is_main {
|
2020-08-18 16:30:13 +00:00
|
|
|
if let Some(import_map) = &self.import_map {
|
2019-06-12 19:00:08 +00:00
|
|
|
let result = import_map.resolve(specifier, referrer)?;
|
2019-10-03 13:16:06 +00:00
|
|
|
if let Some(r) = result {
|
|
|
|
return Ok(r);
|
2019-06-09 13:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 11:13:23 +00:00
|
|
|
|
2019-08-13 18:51:15 +00:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_import(specifier, referrer)?;
|
2019-06-09 13:08:20 +00:00
|
|
|
|
2019-08-13 18:51:15 +00:00
|
|
|
Ok(module_specifier)
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 23:55:59 +00:00
|
|
|
fn load(
|
|
|
|
&self,
|
2020-12-07 10:03:03 +00:00
|
|
|
_op_state: Rc<RefCell<OpState>>,
|
2019-06-12 23:55:59 +00:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 14:33:23 +00:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-10-23 00:50:15 +00:00
|
|
|
_is_dynamic: bool,
|
2020-03-02 18:12:49 +00:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2020-12-15 05:52:55 +00:00
|
|
|
let module_specifier = module_specifier.clone();
|
2020-12-07 10:03:03 +00:00
|
|
|
let program_state = self.program_state.clone();
|
2020-05-11 11:13:27 +00:00
|
|
|
|
2020-12-07 10:03:03 +00: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`.
|
2020-12-15 05:52:55 +00:00
|
|
|
async move { program_state.load(module_specifier, maybe_referrer) }
|
|
|
|
.boxed_local()
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
2020-04-30 12:37:06 +00:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
2020-09-19 23:17:35 +00:00
|
|
|
op_state: Rc<RefCell<OpState>>,
|
2020-04-30 12:37:06 +00:00
|
|
|
_load_id: ModuleLoadId,
|
2020-10-23 00:50:15 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<String>,
|
|
|
|
is_dynamic: bool,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
|
2020-10-23 00:50:15 +00:00
|
|
|
let specifier = specifier.clone();
|
2020-12-07 10:03:03 +00:00
|
|
|
let program_state = self.program_state.clone();
|
2020-08-18 16:30:13 +00:00
|
|
|
let maybe_import_map = self.import_map.clone();
|
2020-09-19 23:17:35 +00:00
|
|
|
let state = op_state.borrow();
|
|
|
|
|
2020-10-23 00:50:15 +00:00
|
|
|
// The permissions that should be applied to any dynamically imported module
|
|
|
|
let dynamic_permissions = state.borrow::<Permissions>().clone();
|
2020-12-07 10:03:03 +00:00
|
|
|
let lib = self.lib.clone();
|
2020-09-19 23:17:35 +00:00
|
|
|
drop(state);
|
|
|
|
|
2020-05-29 14:32:15 +00:00
|
|
|
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
|
|
|
|
async move {
|
2020-10-13 11:35:35 +00:00
|
|
|
program_state
|
2020-05-29 14:32:15 +00:00
|
|
|
.prepare_module_load(
|
2020-10-23 00:50:15 +00:00
|
|
|
specifier,
|
2020-11-02 02:51:56 +00:00
|
|
|
lib,
|
2020-10-23 00:50:15 +00:00
|
|
|
dynamic_permissions,
|
|
|
|
is_dynamic,
|
2020-05-29 14:32:15 +00:00
|
|
|
maybe_import_map,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 12:37:06 +00:00
|
|
|
}
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|