2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2019-06-09 09:08:20 -04:00
|
|
|
use crate::import_map::ImportMap;
|
2020-05-04 14:10:59 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2020-05-08 10:18:00 -04:00
|
|
|
use crate::tsc::TargetLib;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::futures::Future;
|
2020-04-30 08:37:06 -04:00
|
|
|
use deno_core::ModuleLoadId;
|
2020-03-02 13:12:49 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-19 19:17:35 -04:00
|
|
|
use deno_core::OpState;
|
2020-02-08 14:34:31 -05:00
|
|
|
use std::cell::RefCell;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-02-08 14:34:31 -05:00
|
|
|
use std::rc::Rc;
|
2019-07-31 13:16:03 -04:00
|
|
|
use std::str;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::sync::Arc;
|
2020-04-03 13:40:11 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
pub struct CliModuleLoader {
|
2019-06-09 09:08:20 -04: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-01-29 12:54:23 -05:00
|
|
|
pub target_lib: TargetLib,
|
2020-05-11 07:13:27 -04:00
|
|
|
pub is_main: bool,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
impl CliModuleLoader {
|
|
|
|
pub fn new(maybe_import_map: Option<ImportMap>) -> Rc<Self> {
|
|
|
|
Rc::new(CliModuleLoader {
|
|
|
|
import_map: maybe_import_map,
|
|
|
|
target_lib: TargetLib::Main,
|
|
|
|
is_main: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_for_worker() -> Rc<Self> {
|
|
|
|
Rc::new(CliModuleLoader {
|
|
|
|
import_map: None,
|
|
|
|
target_lib: TargetLib::Worker,
|
|
|
|
is_main: false,
|
|
|
|
})
|
|
|
|
}
|
2020-04-27 19:12:38 -04:00
|
|
|
}
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
impl ModuleLoader for CliModuleLoader {
|
2019-06-09 09:08:20 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
2020-10-02 07:13:23 -04:00
|
|
|
op_state: Rc<RefCell<OpState>>,
|
2019-06-09 09:08:20 -04:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-08-07 12:55:39 -04:00
|
|
|
is_main: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = {
|
2020-10-02 07:13:23 -04:00
|
|
|
let state = op_state.borrow();
|
2020-10-13 07:35:35 -04:00
|
|
|
state.borrow::<Arc<ProgramState>>().clone()
|
2020-10-02 07:13:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME(bartlomieju): hacky way to provide compatibility with repl
|
2020-10-13 07:35:35 -04:00
|
|
|
let referrer = if referrer.is_empty() && program_state.flags.repl {
|
2020-10-02 07:13:23 -04:00
|
|
|
"<unknown>"
|
|
|
|
} else {
|
|
|
|
referrer
|
|
|
|
};
|
|
|
|
|
2019-08-07 12:55:39 -04:00
|
|
|
if !is_main {
|
2020-08-18 12:30:13 -04:00
|
|
|
if let Some(import_map) = &self.import_map {
|
2019-06-12 15:00:08 -04:00
|
|
|
let result = import_map.resolve(specifier, referrer)?;
|
2019-10-03 09:16:06 -04:00
|
|
|
if let Some(r) = result {
|
|
|
|
return Ok(r);
|
2019-06-09 09:08:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 07:13:23 -04:00
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_import(specifier, referrer)?;
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
Ok(module_specifier)
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
|
2019-06-12 19:55:59 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
2020-09-19 19:17:35 -04:00
|
|
|
op_state: Rc<RefCell<OpState>>,
|
2019-06-12 19:55:59 -04:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 09:33:23 -05:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-10-22 20:50:15 -04:00
|
|
|
_is_dynamic: bool,
|
2020-03-02 13:12:49 -05:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2020-05-29 10:32:15 -04:00
|
|
|
let module_specifier = module_specifier.to_owned();
|
2019-08-07 12:55:39 -04:00
|
|
|
let module_url_specified = module_specifier.to_string();
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = {
|
2020-09-19 19:17:35 -04:00
|
|
|
let state = op_state.borrow();
|
2020-10-13 07:35:35 -04:00
|
|
|
state.borrow::<Arc<ProgramState>>().clone()
|
2020-09-19 19:17:35 -04:00
|
|
|
};
|
2020-05-11 07:13:27 -04:00
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
// TODO(@kitsonk) this shouldn't be async
|
2020-02-03 18:08:44 -05:00
|
|
|
let fut = async move {
|
2020-10-13 07:35:35 -04:00
|
|
|
let compiled_module = program_state
|
2020-10-22 20:50:15 -04:00
|
|
|
.fetch_compiled_module(module_specifier, maybe_referrer)?;
|
2020-03-02 13:12:49 -05:00
|
|
|
Ok(deno_core::ModuleSource {
|
2019-07-31 13:16:03 -04:00
|
|
|
// Real module name, might be different from initial specifier
|
|
|
|
// due to redirections.
|
|
|
|
code: compiled_module.code,
|
2019-08-07 12:55:39 -04:00
|
|
|
module_url_specified,
|
|
|
|
module_url_found: compiled_module.name,
|
2020-02-03 18:08:44 -05:00
|
|
|
})
|
|
|
|
};
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
fut.boxed_local()
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2020-04-30 08:37:06 -04:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
2020-09-19 19:17:35 -04:00
|
|
|
op_state: Rc<RefCell<OpState>>,
|
2020-04-30 08:37:06 -04:00
|
|
|
_load_id: ModuleLoadId,
|
2020-10-22 20:50:15 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<String>,
|
|
|
|
is_dynamic: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
|
2020-10-22 20:50:15 -04:00
|
|
|
let specifier = specifier.clone();
|
2020-08-18 12:30:13 -04:00
|
|
|
let target_lib = self.target_lib.clone();
|
|
|
|
let maybe_import_map = self.import_map.clone();
|
2020-09-19 19:17:35 -04:00
|
|
|
let state = op_state.borrow();
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
// The permissions that should be applied to any dynamically imported module
|
|
|
|
let dynamic_permissions = state.borrow::<Permissions>().clone();
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = state.borrow::<Arc<ProgramState>>().clone();
|
2020-09-19 19:17:35 -04:00
|
|
|
drop(state);
|
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
|
|
|
|
async move {
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state
|
2020-05-29 10:32:15 -04:00
|
|
|
.prepare_module_load(
|
2020-10-22 20:50:15 -04:00
|
|
|
specifier,
|
2020-05-29 10:32:15 -04:00
|
|
|
target_lib,
|
2020-10-22 20:50:15 -04:00
|
|
|
dynamic_permissions,
|
|
|
|
is_dynamic,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 08:37:06 -04:00
|
|
|
}
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|