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
|
|
|
|
2020-05-07 13:57:10 +00:00
|
|
|
use crate::file_fetcher::SourceFileFetcher;
|
2020-02-07 04:05:02 +00:00
|
|
|
use crate::global_state::GlobalState;
|
2019-06-09 13:08:20 +00:00
|
|
|
use crate::import_map::ImportMap;
|
2020-05-04 18:10:59 +00:00
|
|
|
use crate::permissions::Permissions;
|
2020-05-08 14:18:00 +00:00
|
|
|
use crate::tsc::TargetLib;
|
2020-04-09 22:15:17 +00:00
|
|
|
use crate::web_worker::WebWorkerHandle;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-16 18:28:07 +00:00
|
|
|
use deno_core::url;
|
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;
|
2019-11-17 00:17:47 +00:00
|
|
|
use futures::future::FutureExt;
|
2020-04-30 12:37:06 +00:00
|
|
|
use futures::Future;
|
2020-08-18 16:30:13 +00:00
|
|
|
use std::cell::Cell;
|
2020-02-08 19:34:31 +00:00
|
|
|
use std::cell::RefCell;
|
2019-04-01 19:09:59 +00:00
|
|
|
use std::collections::HashMap;
|
2020-01-20 14:45:44 +00:00
|
|
|
use std::path::Path;
|
2020-09-18 13:20:55 +00:00
|
|
|
use std::path::PathBuf;
|
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-02-18 19:47:11 +00:00
|
|
|
use std::thread::JoinHandle;
|
2019-04-08 20:22:40 +00:00
|
|
|
use std::time::Instant;
|
2020-04-03 17:40:11 +00:00
|
|
|
|
2020-09-15 01:22:32 +00:00
|
|
|
// This is named "CliState" instead of just "State" to avoid confusion with all
|
|
|
|
// other state structs (GlobalState, OpState, GothamState).
|
|
|
|
// TODO(ry) Many of the items in this struct should be moved out and into
|
|
|
|
// OpState, removing redundant RefCell wrappers if possible.
|
|
|
|
pub struct CliState {
|
2020-08-18 16:30:13 +00:00
|
|
|
pub global_state: Arc<GlobalState>,
|
|
|
|
pub permissions: RefCell<Permissions>,
|
2020-02-04 19:24:33 +00:00
|
|
|
pub main_module: ModuleSpecifier,
|
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-08-18 16:30:13 +00:00
|
|
|
pub workers: RefCell<HashMap<u32, (JoinHandle<()>, WebWorkerHandle)>>,
|
|
|
|
pub next_worker_id: Cell<u32>,
|
2019-04-08 20:22:40 +00:00
|
|
|
pub start_time: Instant,
|
2020-01-29 17:54:23 +00:00
|
|
|
pub target_lib: TargetLib,
|
2020-05-11 11:13:27 +00:00
|
|
|
pub is_main: bool,
|
2020-05-12 19:08:22 +00:00
|
|
|
pub is_internal: bool,
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 10:59:29 +00:00
|
|
|
pub fn exit_unstable(api_name: &str) {
|
2020-04-27 23:12:38 +00:00
|
|
|
eprintln!(
|
|
|
|
"Unstable API '{}'. The --unstable flag must be provided.",
|
|
|
|
api_name
|
|
|
|
);
|
|
|
|
std::process::exit(70);
|
|
|
|
}
|
|
|
|
|
2020-09-15 01:22:32 +00:00
|
|
|
impl ModuleLoader for CliState {
|
2019-06-09 13:08:20 +00:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
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> {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 14:33:23 +00:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-05-29 14:32:15 +00:00
|
|
|
_is_dyn_import: bool,
|
2020-03-02 18:12:49 +00:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2020-05-29 14:32:15 +00:00
|
|
|
let module_specifier = module_specifier.to_owned();
|
2019-08-07 16:55:39 +00:00
|
|
|
let module_url_specified = module_specifier.to_string();
|
2020-08-18 16:30:13 +00:00
|
|
|
let global_state = self.global_state.clone();
|
2020-05-11 11:13:27 +00:00
|
|
|
|
2020-05-29 14:32:15 +00:00
|
|
|
// TODO(bartlomieju): `fetch_compiled_module` should take `load_id` param
|
2020-02-03 23:08:44 +00:00
|
|
|
let fut = async move {
|
|
|
|
let compiled_module = global_state
|
2020-05-29 14:32:15 +00:00
|
|
|
.fetch_compiled_module(module_specifier, maybe_referrer)
|
2020-02-03 23:08:44 +00:00
|
|
|
.await?;
|
2020-03-02 18:12:49 +00:00
|
|
|
Ok(deno_core::ModuleSource {
|
2019-07-31 17:16:03 +00:00
|
|
|
// Real module name, might be different from initial specifier
|
|
|
|
// due to redirections.
|
|
|
|
code: compiled_module.code,
|
2019-08-07 16:55:39 +00:00
|
|
|
module_url_specified,
|
|
|
|
module_url_found: compiled_module.name,
|
2020-02-03 23:08:44 +00:00
|
|
|
})
|
|
|
|
};
|
2019-11-04 15:38:52 +00:00
|
|
|
|
2020-02-03 23:08:44 +00:00
|
|
|
fut.boxed_local()
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
2020-04-30 12:37:06 +00:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
|
|
|
_load_id: ModuleLoadId,
|
2020-05-29 14:32:15 +00:00
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
maybe_referrer: Option<String>,
|
|
|
|
is_dyn_import: bool,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
|
2020-05-29 14:32:15 +00:00
|
|
|
let module_specifier = module_specifier.clone();
|
2020-08-18 16:30:13 +00:00
|
|
|
let target_lib = self.target_lib.clone();
|
|
|
|
let maybe_import_map = self.import_map.clone();
|
2020-05-29 14:32:15 +00:00
|
|
|
// Only "main" module is loaded without permission check,
|
|
|
|
// ie. module that is associated with "is_main" state
|
|
|
|
// and is not a dynamic import.
|
2020-08-18 16:30:13 +00:00
|
|
|
let permissions = if self.is_main && !is_dyn_import {
|
2020-05-29 14:32:15 +00:00
|
|
|
Permissions::allow_all()
|
|
|
|
} else {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().clone()
|
2020-05-29 14:32:15 +00:00
|
|
|
};
|
2020-08-18 16:30:13 +00:00
|
|
|
let global_state = self.global_state.clone();
|
2020-05-29 14:32:15 +00:00
|
|
|
// TODO(bartlomieju): I'm not sure if it's correct to ignore
|
|
|
|
// bad referrer - this is the case for `Deno.core.evalContext()` where
|
|
|
|
// `ref_str` is `<unknown>`.
|
|
|
|
let maybe_referrer = if let Some(ref_str) = maybe_referrer {
|
|
|
|
ModuleSpecifier::resolve_url(&ref_str).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
|
|
|
|
async move {
|
|
|
|
global_state
|
|
|
|
.prepare_module_load(
|
|
|
|
module_specifier,
|
|
|
|
maybe_referrer,
|
|
|
|
target_lib,
|
|
|
|
permissions,
|
|
|
|
is_dyn_import,
|
|
|
|
maybe_import_map,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 12:37:06 +00:00
|
|
|
}
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 01:22:32 +00:00
|
|
|
impl CliState {
|
2020-01-29 17:54:23 +00:00
|
|
|
/// If `shared_permission` is None then permissions from globa state are used.
|
2019-11-09 20:07:14 +00:00
|
|
|
pub fn new(
|
2020-08-18 16:30:13 +00:00
|
|
|
global_state: &Arc<GlobalState>,
|
2020-05-04 18:10:59 +00:00
|
|
|
shared_permissions: Option<Permissions>,
|
2020-02-04 19:24:33 +00:00
|
|
|
main_module: ModuleSpecifier,
|
2020-05-29 14:32:15 +00:00
|
|
|
maybe_import_map: Option<ImportMap>,
|
2020-05-12 19:08:22 +00:00
|
|
|
is_internal: bool,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<Rc<Self>, AnyError> {
|
2020-09-15 01:22:32 +00:00
|
|
|
let state = CliState {
|
2020-08-18 16:30:13 +00:00
|
|
|
global_state: global_state.clone(),
|
2019-11-04 15:38:52 +00:00
|
|
|
main_module,
|
2020-08-18 16:30:13 +00:00
|
|
|
permissions: shared_permissions
|
|
|
|
.unwrap_or_else(|| global_state.permissions.clone())
|
|
|
|
.into(),
|
2020-05-29 14:32:15 +00:00
|
|
|
import_map: maybe_import_map,
|
2020-08-18 16:30:13 +00:00
|
|
|
workers: Default::default(),
|
|
|
|
next_worker_id: Default::default(),
|
2019-04-08 20:22:40 +00:00
|
|
|
start_time: Instant::now(),
|
2020-01-29 17:54:23 +00:00
|
|
|
target_lib: TargetLib::Main,
|
2020-05-11 11:13:27 +00:00
|
|
|
is_main: true,
|
2020-05-12 19:08:22 +00:00
|
|
|
is_internal,
|
2020-08-18 16:30:13 +00:00
|
|
|
};
|
|
|
|
Ok(Rc::new(state))
|
2020-01-29 17:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If `shared_permission` is None then permissions from globa state are used.
|
|
|
|
pub fn new_for_worker(
|
2020-08-18 16:30:13 +00:00
|
|
|
global_state: &Arc<GlobalState>,
|
2020-05-04 18:10:59 +00:00
|
|
|
shared_permissions: Option<Permissions>,
|
2020-02-04 19:24:33 +00:00
|
|
|
main_module: ModuleSpecifier,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<Rc<Self>, AnyError> {
|
2020-09-15 01:22:32 +00:00
|
|
|
let state = CliState {
|
2020-08-18 16:30:13 +00:00
|
|
|
global_state: global_state.clone(),
|
2020-01-29 17:54:23 +00:00
|
|
|
main_module,
|
2020-08-18 16:30:13 +00:00
|
|
|
permissions: shared_permissions
|
|
|
|
.unwrap_or_else(|| global_state.permissions.clone())
|
|
|
|
.into(),
|
2020-01-29 17:54:23 +00:00
|
|
|
import_map: None,
|
2020-08-18 16:30:13 +00:00
|
|
|
workers: Default::default(),
|
|
|
|
next_worker_id: Default::default(),
|
2020-01-29 17:54:23 +00:00
|
|
|
start_time: Instant::now(),
|
|
|
|
target_lib: TargetLib::Worker,
|
2020-05-11 11:13:27 +00:00
|
|
|
is_main: false,
|
2020-05-12 19:08:22 +00:00
|
|
|
is_internal: false,
|
2020-08-18 16:30:13 +00:00
|
|
|
};
|
|
|
|
Ok(Rc::new(state))
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:55:59 +00:00
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_read(&self, path: &Path) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_read(path)
|
2019-03-20 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 15:27:43 +00:00
|
|
|
/// As `check_read()`, but permission error messages will anonymize the path
|
|
|
|
/// by replacing it with the given `display`.
|
|
|
|
#[inline]
|
|
|
|
pub fn check_read_blind(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
display: &str,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_read_blind(path, display)
|
2020-05-29 15:27:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:55:59 +00:00
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_write(&self, path: &Path) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_write(path)
|
2019-03-20 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_env(&self) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_env()
|
2019-03-20 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_net(&self, hostname: &str, port: u16) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_net(hostname, port)
|
2019-05-08 23:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_net_url(&self, url: &url::Url) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_net_url(url)
|
2019-03-20 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_run(&self) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_run()
|
2019-03-20 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:29:32 +00:00
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_hrtime(&self) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_hrtime()
|
2020-08-18 20:29:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:30:20 +00:00
|
|
|
#[inline]
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn check_plugin(&self, filename: &Path) -> Result<(), AnyError> {
|
2020-08-18 16:30:13 +00:00
|
|
|
self.permissions.borrow().check_plugin(filename)
|
2019-12-05 20:30:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 18:51:15 +00:00
|
|
|
pub fn check_dyn_import(
|
2020-01-04 10:20:52 +00:00
|
|
|
&self,
|
2019-08-13 18:51:15 +00:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2019-08-13 18:51:15 +00:00
|
|
|
let u = module_specifier.as_url();
|
2020-05-07 13:57:10 +00:00
|
|
|
// TODO(bartlomieju): temporary fix to prevent hitting `unreachable`
|
|
|
|
// statement that is actually reachable...
|
|
|
|
SourceFileFetcher::check_if_supported_scheme(u)?;
|
|
|
|
|
2019-08-13 18:51:15 +00:00
|
|
|
match u.scheme() {
|
|
|
|
"http" | "https" => {
|
|
|
|
self.check_net_url(u)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"file" => {
|
2020-01-20 14:45:44 +00:00
|
|
|
let path = u
|
2019-08-13 18:51:15 +00:00
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap();
|
2020-01-20 14:45:44 +00:00
|
|
|
self.check_read(Path::new(&path))?;
|
2019-08-13 18:51:15 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-02-23 19:51:29 +00:00
|
|
|
_ => unreachable!(),
|
2019-08-13 18:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:17:52 +00:00
|
|
|
#[cfg(test)]
|
2020-09-06 00:34:02 +00:00
|
|
|
pub fn mock(main_module: &str) -> Rc<Self> {
|
2020-02-04 19:24:33 +00:00
|
|
|
let module_specifier = ModuleSpecifier::resolve_url_or_path(main_module)
|
|
|
|
.expect("Invalid entry module");
|
2020-09-15 01:22:32 +00:00
|
|
|
CliState::new(
|
2020-08-18 16:30:13 +00:00
|
|
|
&GlobalState::mock(vec!["deno".to_string()], None),
|
2019-11-24 15:42:30 +00:00
|
|
|
None,
|
2019-11-04 15:38:52 +00:00
|
|
|
module_specifier,
|
2020-05-29 14:32:15 +00:00
|
|
|
None,
|
2020-05-12 19:08:22 +00:00
|
|
|
false,
|
2019-07-31 21:11:37 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
2020-09-15 01:22:32 +00:00
|
|
|
|
|
|
|
/// Quits the process if the --unstable flag was not provided.
|
|
|
|
///
|
|
|
|
/// This is intentionally a non-recoverable check so that people cannot probe
|
|
|
|
/// for unstable APIs from stable programs.
|
|
|
|
pub fn check_unstable(&self, api_name: &str) {
|
|
|
|
// TODO(ry) Maybe use IsolateHandle::terminate_execution here to provide a
|
|
|
|
// stack trace in JS.
|
|
|
|
if !self.global_state.flags.unstable {
|
|
|
|
exit_unstable(api_name);
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
2020-09-18 13:20:55 +00:00
|
|
|
|
|
|
|
impl deno_fetch::FetchPermissions for CliState {
|
|
|
|
fn check_net_url(&self, url: &url::Url) -> Result<(), AnyError> {
|
|
|
|
CliState::check_net_url(self, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read(&self, p: &PathBuf) -> Result<(), AnyError> {
|
|
|
|
CliState::check_read(self, p)
|
|
|
|
}
|
|
|
|
}
|