2019-03-14 19:17:52 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-07-31 13:16:03 -04:00
|
|
|
use crate::compilers::CompiledModule;
|
|
|
|
use crate::compilers::JsCompiler;
|
|
|
|
use crate::compilers::JsonCompiler;
|
|
|
|
use crate::compilers::TsCompiler;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::deno_dir;
|
2019-08-13 14:51:15 -04:00
|
|
|
use crate::deno_error::permission_denied;
|
2019-07-31 07:58:41 -04:00
|
|
|
use crate::file_fetcher::SourceFileFetcher;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::flags;
|
|
|
|
use crate::global_timer::GlobalTimer;
|
2019-06-09 09:08:20 -04:00
|
|
|
use crate::import_map::ImportMap;
|
2019-07-31 13:16:03 -04:00
|
|
|
use crate::msg;
|
2019-10-01 18:51:05 -04:00
|
|
|
use crate::ops::JsonOp;
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::permissions::DenoPermissions;
|
2019-05-11 10:23:19 -04:00
|
|
|
use crate::progress::Progress;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::resources;
|
2019-04-01 15:09:59 -04:00
|
|
|
use crate::resources::ResourceId;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::worker::Worker;
|
2019-03-30 19:30:40 -04:00
|
|
|
use deno::Buf;
|
2019-06-17 21:02:08 -04:00
|
|
|
use deno::CoreOp;
|
2019-07-10 18:53:48 -04:00
|
|
|
use deno::ErrBox;
|
2019-06-05 16:35:38 -04:00
|
|
|
use deno::Loader;
|
2019-06-12 19:55:59 -04:00
|
|
|
use deno::ModuleSpecifier;
|
2019-10-01 18:51:05 -04:00
|
|
|
use deno::Op;
|
2019-04-28 15:31:10 -04:00
|
|
|
use deno::PinnedBuf;
|
2019-04-01 15:09:59 -04:00
|
|
|
use futures::future::Shared;
|
2019-06-05 16:35:38 -04:00
|
|
|
use futures::Future;
|
2019-06-11 10:34:39 -04:00
|
|
|
use rand::rngs::StdRng;
|
|
|
|
use rand::SeedableRng;
|
2019-10-01 18:51:05 -04:00
|
|
|
use serde_json::Value;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std;
|
2019-04-01 15:09:59 -04:00
|
|
|
use std::collections::HashMap;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::env;
|
2019-04-09 13:11:25 -04:00
|
|
|
use std::ops::Deref;
|
2019-07-31 13:16:03 -04:00
|
|
|
use std::str;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2019-04-09 13:11:25 -04:00
|
|
|
use std::sync::Arc;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::sync::Mutex;
|
2019-04-08 16:22:40 -04:00
|
|
|
use std::time::Instant;
|
2019-04-08 17:10:00 -04:00
|
|
|
use tokio::sync::mpsc as async_mpsc;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
pub type WorkerSender = async_mpsc::Sender<Buf>;
|
|
|
|
pub type WorkerReceiver = async_mpsc::Receiver<Buf>;
|
|
|
|
pub type WorkerChannels = (WorkerSender, WorkerReceiver);
|
2019-04-08 17:10:00 -04:00
|
|
|
pub type UserWorkerTable = HashMap<ResourceId, Shared<Worker>>;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Metrics {
|
|
|
|
pub ops_dispatched: AtomicUsize,
|
|
|
|
pub ops_completed: AtomicUsize,
|
|
|
|
pub bytes_sent_control: AtomicUsize,
|
|
|
|
pub bytes_sent_data: AtomicUsize,
|
|
|
|
pub bytes_received: AtomicUsize,
|
|
|
|
pub resolve_count: AtomicUsize,
|
2019-05-20 12:06:57 -04:00
|
|
|
pub compiler_starts: AtomicUsize,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-05-11 10:23:19 -04:00
|
|
|
/// Isolate cannot be passed between threads but ThreadSafeState can.
|
|
|
|
/// ThreadSafeState satisfies Send and Sync. So any state that needs to be
|
|
|
|
/// accessed outside the main V8 thread should be inside ThreadSafeState.
|
2019-04-09 13:11:25 -04:00
|
|
|
pub struct ThreadSafeState(Arc<State>);
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2019-04-09 13:11:25 -04:00
|
|
|
pub struct State {
|
2019-06-11 14:35:03 -04:00
|
|
|
pub modules: Arc<Mutex<deno::Modules>>,
|
2019-06-12 15:00:08 -04:00
|
|
|
pub main_module: Option<ModuleSpecifier>,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub dir: deno_dir::DenoDir,
|
|
|
|
pub argv: Vec<String>,
|
2019-03-19 20:55:59 -04:00
|
|
|
pub permissions: DenoPermissions,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub flags: flags::DenoFlags,
|
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>,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub metrics: Metrics,
|
2019-04-08 17:10:00 -04:00
|
|
|
pub worker_channels: Mutex<WorkerChannels>,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub global_timer: Mutex<GlobalTimer>,
|
2019-04-01 15:09:59 -04:00
|
|
|
pub workers: Mutex<UserWorkerTable>,
|
2019-04-08 16:22:40 -04:00
|
|
|
pub start_time: Instant,
|
2019-05-11 10:23:19 -04:00
|
|
|
/// A reference to this worker's resource.
|
2019-04-08 17:10:00 -04:00
|
|
|
pub resource: resources::Resource,
|
2019-05-11 10:23:19 -04:00
|
|
|
/// Reference to global progress bar.
|
|
|
|
pub progress: Progress,
|
2019-06-11 10:34:39 -04:00
|
|
|
pub seeded_rng: Option<Mutex<StdRng>>,
|
2019-05-20 12:06:57 -04:00
|
|
|
|
2019-07-31 07:58:41 -04:00
|
|
|
pub file_fetcher: SourceFileFetcher,
|
2019-07-31 13:16:03 -04:00
|
|
|
pub js_compiler: JsCompiler,
|
|
|
|
pub json_compiler: JsonCompiler,
|
2019-07-17 18:15:30 -04:00
|
|
|
pub ts_compiler: TsCompiler,
|
2019-08-05 07:23:41 -04:00
|
|
|
|
|
|
|
pub include_deno_namespace: bool,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 13:11:25 -04:00
|
|
|
impl Clone for ThreadSafeState {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ThreadSafeState(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for ThreadSafeState {
|
|
|
|
type Target = Arc<State>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 18:58:00 -04:00
|
|
|
impl ThreadSafeState {
|
2019-10-01 18:51:05 -04:00
|
|
|
/// Wrap core `OpDispatcher` to collect metrics.
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn core_op<D>(
|
2019-06-17 21:02:08 -04:00
|
|
|
&self,
|
2019-10-01 18:51:05 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&[u8], Option<PinnedBuf>) -> CoreOp
|
|
|
|
where
|
|
|
|
D: Fn(&[u8], Option<PinnedBuf>) -> CoreOp,
|
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
|
|
|
move |control: &[u8], zero_copy: Option<PinnedBuf>| -> CoreOp {
|
|
|
|
let bytes_sent_control = control.len();
|
|
|
|
let bytes_sent_zero_copy =
|
|
|
|
zero_copy.as_ref().map(|b| b.len()).unwrap_or(0);
|
|
|
|
|
|
|
|
let op = dispatcher(control, zero_copy);
|
|
|
|
state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);
|
|
|
|
|
|
|
|
match op {
|
|
|
|
Op::Sync(buf) => {
|
|
|
|
state.metrics_op_completed(buf.len());
|
|
|
|
Op::Sync(buf)
|
|
|
|
}
|
|
|
|
Op::Async(fut) => {
|
|
|
|
let state = state.clone();
|
|
|
|
let result_fut = Box::new(fut.map(move |buf: Buf| {
|
|
|
|
state.clone().metrics_op_completed(buf.len());
|
|
|
|
buf
|
|
|
|
}));
|
|
|
|
Op::Async(result_fut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a special function that provides `state` argument to dispatcher.
|
|
|
|
///
|
|
|
|
/// NOTE: This only works with JSON dispatcher.
|
|
|
|
/// This is a band-aid for transition to `Isolate.register_op` API as most of our
|
|
|
|
/// ops require `state` argument.
|
|
|
|
pub fn stateful_op<D>(
|
|
|
|
&self,
|
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(Value, Option<PinnedBuf>) -> Result<JsonOp, ErrBox>
|
|
|
|
where
|
|
|
|
D: Fn(&ThreadSafeState, Value, Option<PinnedBuf>) -> Result<JsonOp, ErrBox>,
|
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
|
|
|
move |args: Value, zero_copy: Option<PinnedBuf>| -> Result<JsonOp, ErrBox> {
|
|
|
|
dispatcher(&state, args, zero_copy)
|
|
|
|
}
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:35:38 -04:00
|
|
|
impl Loader for ThreadSafeState {
|
2019-06-09 09:08:20 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-08-07 12:55:39 -04:00
|
|
|
is_main: bool,
|
2019-08-13 14:51:15 -04:00
|
|
|
is_dyn_import: bool,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
2019-08-07 12:55:39 -04:00
|
|
|
if !is_main {
|
2019-06-09 09:08:20 -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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
if is_dyn_import {
|
|
|
|
self.check_dyn_import(&module_specifier)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(module_specifier)
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an absolute url, load its source code.
|
2019-06-12 19:55:59 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> Box<deno::SourceCodeInfoFuture> {
|
2019-06-05 16:35:38 -04:00
|
|
|
self.metrics.resolve_count.fetch_add(1, Ordering::SeqCst);
|
2019-08-07 12:55:39 -04:00
|
|
|
let module_url_specified = module_specifier.to_string();
|
2019-07-31 13:16:03 -04:00
|
|
|
Box::new(self.fetch_compiled_module(module_specifier).map(
|
|
|
|
|compiled_module| deno::SourceCodeInfo {
|
|
|
|
// 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,
|
2019-07-31 13:16:03 -04:00
|
|
|
},
|
|
|
|
))
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:11:25 -04:00
|
|
|
impl ThreadSafeState {
|
2019-04-11 10:58:31 -04:00
|
|
|
pub fn new(
|
|
|
|
flags: flags::DenoFlags,
|
|
|
|
argv_rest: Vec<String>,
|
2019-05-11 10:23:19 -04:00
|
|
|
progress: Progress,
|
2019-08-05 07:23:41 -04:00
|
|
|
include_deno_namespace: bool,
|
2019-07-31 07:58:41 -04:00
|
|
|
) -> Result<Self, ErrBox> {
|
2019-04-17 09:25:51 -04:00
|
|
|
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
let (worker_in_tx, worker_in_rx) = async_mpsc::channel::<Buf>(1);
|
|
|
|
let (worker_out_tx, worker_out_rx) = async_mpsc::channel::<Buf>(1);
|
|
|
|
let internal_channels = (worker_out_tx, worker_in_rx);
|
|
|
|
let external_channels = (worker_in_tx, worker_out_rx);
|
|
|
|
let resource = resources::add_worker(external_channels);
|
|
|
|
|
2019-07-31 07:58:41 -04:00
|
|
|
let dir = deno_dir::DenoDir::new(custom_root)?;
|
|
|
|
|
|
|
|
let file_fetcher = SourceFileFetcher::new(
|
|
|
|
dir.deps_cache.clone(),
|
2019-07-17 18:15:30 -04:00
|
|
|
progress.clone(),
|
|
|
|
!flags.reload,
|
|
|
|
flags.no_fetch,
|
2019-07-31 07:58:41 -04:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let ts_compiler = TsCompiler::new(
|
|
|
|
file_fetcher.clone(),
|
|
|
|
dir.gen_cache.clone(),
|
|
|
|
!flags.reload,
|
|
|
|
flags.config_path.clone(),
|
2019-07-31 13:16:03 -04:00
|
|
|
)?;
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-06-12 15:00:08 -04:00
|
|
|
let main_module: Option<ModuleSpecifier> = if argv_rest.len() <= 1 {
|
2019-06-09 09:08:20 -04:00
|
|
|
None
|
|
|
|
} else {
|
2019-06-12 15:00:08 -04:00
|
|
|
let root_specifier = argv_rest[1].clone();
|
2019-07-31 13:16:03 -04:00
|
|
|
Some(ModuleSpecifier::resolve_url_or_path(&root_specifier)?)
|
2019-06-09 09:08:20 -04:00
|
|
|
};
|
|
|
|
|
2019-07-31 13:16:03 -04:00
|
|
|
let import_map: Option<ImportMap> = match &flags.import_map_path {
|
|
|
|
None => None,
|
2019-08-15 13:58:04 -04:00
|
|
|
Some(file_path) => Some(ImportMap::load(file_path)?),
|
2019-07-31 13:16:03 -04:00
|
|
|
};
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-06-11 10:34:39 -04:00
|
|
|
let mut seeded_rng = None;
|
|
|
|
if let Some(seed) = flags.seed {
|
|
|
|
seeded_rng = Some(Mutex::new(StdRng::seed_from_u64(seed)));
|
|
|
|
};
|
|
|
|
|
2019-06-11 14:35:03 -04:00
|
|
|
let modules = Arc::new(Mutex::new(deno::Modules::new()));
|
|
|
|
|
2019-07-31 07:58:41 -04:00
|
|
|
let state = State {
|
2019-06-09 09:08:20 -04:00
|
|
|
main_module,
|
2019-06-11 14:35:03 -04:00
|
|
|
modules,
|
2019-06-09 09:08:20 -04:00
|
|
|
dir,
|
2019-03-14 19:17:52 -04:00
|
|
|
argv: argv_rest,
|
2019-03-19 20:55:59 -04:00
|
|
|
permissions: DenoPermissions::from_flags(&flags),
|
2019-03-14 19:17:52 -04:00
|
|
|
flags,
|
2019-06-09 09:08:20 -04:00
|
|
|
import_map,
|
2019-03-14 19:17:52 -04:00
|
|
|
metrics: Metrics::default(),
|
2019-04-08 17:10:00 -04:00
|
|
|
worker_channels: Mutex::new(internal_channels),
|
2019-03-14 19:17:52 -04:00
|
|
|
global_timer: Mutex::new(GlobalTimer::new()),
|
2019-04-01 15:09:59 -04:00
|
|
|
workers: Mutex::new(UserWorkerTable::new()),
|
2019-04-08 16:22:40 -04:00
|
|
|
start_time: Instant::now(),
|
2019-04-08 17:10:00 -04:00
|
|
|
resource,
|
2019-05-11 10:23:19 -04:00
|
|
|
progress,
|
2019-06-11 10:34:39 -04:00
|
|
|
seeded_rng,
|
2019-07-31 07:58:41 -04:00
|
|
|
file_fetcher,
|
2019-07-17 18:15:30 -04:00
|
|
|
ts_compiler,
|
2019-07-31 13:16:03 -04:00
|
|
|
js_compiler: JsCompiler {},
|
|
|
|
json_compiler: JsonCompiler {},
|
2019-08-05 07:23:41 -04:00
|
|
|
include_deno_namespace,
|
2019-07-31 07:58:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ThreadSafeState(Arc::new(state)))
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:16:03 -04:00
|
|
|
pub fn fetch_compiled_module(
|
|
|
|
self: &Self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> impl Future<Item = CompiledModule, Error = ErrBox> {
|
|
|
|
let state_ = self.clone();
|
|
|
|
|
|
|
|
self
|
|
|
|
.file_fetcher
|
|
|
|
.fetch_source_file_async(&module_specifier)
|
|
|
|
.and_then(move |out| match out.media_type {
|
|
|
|
msg::MediaType::Unknown => {
|
|
|
|
state_.js_compiler.compile_async(state_.clone(), &out)
|
|
|
|
}
|
|
|
|
msg::MediaType::Json => {
|
|
|
|
state_.json_compiler.compile_async(state_.clone(), &out)
|
|
|
|
}
|
2019-10-02 10:46:36 -04:00
|
|
|
msg::MediaType::TypeScript
|
|
|
|
| msg::MediaType::TSX
|
|
|
|
| msg::MediaType::JSX => {
|
2019-07-31 13:16:03 -04:00
|
|
|
state_.ts_compiler.compile_async(state_.clone(), &out)
|
|
|
|
}
|
|
|
|
msg::MediaType::JavaScript => {
|
|
|
|
if state_.ts_compiler.compile_js {
|
|
|
|
state_.ts_compiler.compile_async(state_.clone(), &out)
|
|
|
|
} else {
|
|
|
|
state_.js_compiler.compile_async(state_.clone(), &out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
/// Read main module from argv
|
2019-06-12 15:00:08 -04:00
|
|
|
pub fn main_module(&self) -> Option<ModuleSpecifier> {
|
2019-06-09 09:08:20 -04:00
|
|
|
match &self.main_module {
|
2019-06-12 15:00:08 -04:00
|
|
|
Some(module_specifier) => Some(module_specifier.clone()),
|
2019-06-09 09:08:20 -04:00
|
|
|
None => None,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
#[inline]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn check_read(&self, filename: &str) -> Result<(), ErrBox> {
|
2019-03-19 20:55:59 -04:00
|
|
|
self.permissions.check_read(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn check_write(&self, filename: &str) -> Result<(), ErrBox> {
|
2019-03-19 20:55:59 -04:00
|
|
|
self.permissions.check_write(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn check_env(&self) -> Result<(), ErrBox> {
|
2019-03-19 20:55:59 -04:00
|
|
|
self.permissions.check_env()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn check_net(&self, host_and_port: &str) -> Result<(), ErrBox> {
|
2019-05-08 19:20:30 -04:00
|
|
|
self.permissions.check_net(host_and_port)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-08-13 14:51:15 -04:00
|
|
|
pub fn check_net_url(&self, url: &url::Url) -> Result<(), ErrBox> {
|
2019-05-08 19:20:30 -04:00
|
|
|
self.permissions.check_net_url(url)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn check_run(&self) -> Result<(), ErrBox> {
|
2019-03-19 20:55:59 -04:00
|
|
|
self.permissions.check_run()
|
|
|
|
}
|
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
pub fn check_dyn_import(
|
|
|
|
self: &Self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let u = module_specifier.as_url();
|
|
|
|
match u.scheme() {
|
|
|
|
"http" | "https" => {
|
|
|
|
self.check_net_url(u)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"file" => {
|
|
|
|
let filename = u
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap();
|
|
|
|
self.check_read(&filename)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(permission_denied()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg(test)]
|
2019-06-08 14:42:28 -04:00
|
|
|
pub fn mock(argv: Vec<String>) -> ThreadSafeState {
|
2019-04-21 11:34:18 -04:00
|
|
|
ThreadSafeState::new(
|
|
|
|
flags::DenoFlags::default(),
|
|
|
|
argv,
|
2019-05-11 10:23:19 -04:00
|
|
|
Progress::new(),
|
2019-08-05 07:23:41 -04:00
|
|
|
true,
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.unwrap()
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metrics_op_dispatched(
|
|
|
|
&self,
|
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
|
|
|
) {
|
|
|
|
self.metrics.ops_dispatched.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_control
|
|
|
|
.fetch_add(bytes_sent_control, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_data
|
|
|
|
.fetch_add(bytes_sent_data, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metrics_op_completed(&self, bytes_received: usize) {
|
|
|
|
self.metrics.ops_completed.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_received
|
|
|
|
.fetch_add(bytes_received, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 13:11:25 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thread_safe() {
|
|
|
|
fn f<S: Send + Sync>(_: S) {}
|
2019-06-08 14:42:28 -04:00
|
|
|
f(ThreadSafeState::mock(vec![
|
|
|
|
String::from("./deno"),
|
|
|
|
String::from("hello.js"),
|
|
|
|
]));
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|
2019-08-15 13:58:04 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import_map_given_for_repl() {
|
|
|
|
let _result = ThreadSafeState::new(
|
|
|
|
flags::DenoFlags {
|
|
|
|
import_map_path: Some("import_map.json".to_string()),
|
|
|
|
..flags::DenoFlags::default()
|
|
|
|
},
|
|
|
|
vec![String::from("./deno")],
|
|
|
|
Progress::new(),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|