2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-08-09 10:53:21 -04:00
|
|
|
use crate::colors;
|
2021-05-10 12:16:39 -04:00
|
|
|
use crate::config_file::ConfigFile;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::deno_dir;
|
2020-11-05 19:38:21 -05:00
|
|
|
use crate::file_fetcher::CacheSetting;
|
|
|
|
use crate::file_fetcher::FileFetcher;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::flags;
|
2020-02-19 08:17:13 -05:00
|
|
|
use crate::http_cache;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::lockfile::Lockfile;
|
2020-11-02 14:41:20 -05:00
|
|
|
use crate::module_graph::CheckOptions;
|
|
|
|
use crate::module_graph::GraphBuilder;
|
|
|
|
use crate::module_graph::TranspileOptions;
|
|
|
|
use crate::module_graph::TypeLib;
|
2020-10-22 20:50:15 -04:00
|
|
|
use crate::source_maps::SourceMapGetter;
|
2020-09-24 18:31:17 -04:00
|
|
|
use crate::specifier_handler::FetchHandler;
|
2021-01-07 21:08:51 -05:00
|
|
|
use crate::version;
|
2020-10-22 20:50:15 -04:00
|
|
|
|
2020-12-15 00:52:55 -05:00
|
|
|
use deno_core::error::anyhow;
|
|
|
|
use deno_core::error::get_custom_error_class;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-01-04 18:15:52 -05:00
|
|
|
use deno_core::error::Context;
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url;
|
2020-10-22 20:50:15 -04:00
|
|
|
use deno_core::url::Url;
|
2021-09-29 04:47:24 -04:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2020-12-15 00:52:55 -05:00
|
|
|
use deno_core::ModuleSource;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-09-10 21:38:24 -04:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
|
|
|
use deno_runtime::deno_web::BlobStore;
|
|
|
|
use deno_runtime::inspector_server::InspectorServer;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-08-07 08:49:38 -04:00
|
|
|
use deno_tls::rustls::RootCertStore;
|
|
|
|
use deno_tls::rustls_native_certs::load_native_certs;
|
|
|
|
use deno_tls::webpki_roots::TLS_SERVER_ROOTS;
|
2021-09-10 21:38:24 -04:00
|
|
|
use import_map::ImportMap;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2021-10-04 19:35:55 -04:00
|
|
|
use log::info;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::warn;
|
2020-12-15 00:52:55 -05:00
|
|
|
use std::collections::HashMap;
|
2021-06-19 10:14:43 -04:00
|
|
|
use std::collections::HashSet;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::env;
|
2021-08-07 08:49:38 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufReader;
|
2021-09-24 11:10:42 -04:00
|
|
|
use std::ops::Deref;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// This structure represents state of single "deno" program.
|
|
|
|
///
|
|
|
|
/// It is shared by all created workers (thus V8 isolates).
|
2021-09-24 11:10:42 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ProcState(Arc<Inner>);
|
|
|
|
|
|
|
|
pub struct Inner {
|
2019-11-04 10:38:52 -05:00
|
|
|
/// Flags parsed from `argv` contents.
|
2020-02-26 05:52:15 -05:00
|
|
|
pub flags: flags::Flags,
|
2019-11-04 10:38:52 -05:00
|
|
|
pub dir: deno_dir::DenoDir,
|
2020-12-21 08:04:25 -05:00
|
|
|
pub coverage_dir: Option<String>,
|
2020-11-05 19:38:21 -05:00
|
|
|
pub file_fetcher: FileFetcher,
|
2020-12-15 00:52:55 -05:00
|
|
|
pub modules:
|
|
|
|
Arc<Mutex<HashMap<ModuleSpecifier, Result<ModuleSource, AnyError>>>>,
|
2020-10-23 17:01:54 -04:00
|
|
|
pub lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2021-05-10 12:16:39 -04:00
|
|
|
pub maybe_config_file: Option<ConfigFile>,
|
2020-05-29 10:32:15 -04:00
|
|
|
pub maybe_import_map: Option<ImportMap>,
|
2020-09-25 04:24:51 -04:00
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2021-08-07 08:49:38 -04:00
|
|
|
pub root_cert_store: Option<RootCertStore>,
|
2021-07-05 09:34:37 -04:00
|
|
|
pub blob_store: BlobStore,
|
2021-05-22 12:08:24 -04:00
|
|
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
2021-07-06 13:42:52 -04:00
|
|
|
pub shared_array_buffer_store: SharedArrayBufferStore,
|
2021-09-29 04:47:24 -04:00
|
|
|
pub compiled_wasm_module_store: CompiledWasmModuleStore,
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
impl Deref for ProcState {
|
|
|
|
type Target = Arc<Inner>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcState {
|
|
|
|
pub async fn build(flags: flags::Flags) -> Result<Self, AnyError> {
|
2021-07-27 17:25:09 -04:00
|
|
|
let maybe_custom_root = flags
|
|
|
|
.cache_path
|
|
|
|
.clone()
|
|
|
|
.or_else(|| env::var("DENO_DIR").map(String::into).ok());
|
|
|
|
let dir = deno_dir::DenoDir::new(maybe_custom_root)?;
|
2020-02-19 08:17:13 -05:00
|
|
|
let deps_cache_location = dir.root.join("deps");
|
2020-05-07 08:32:57 -04:00
|
|
|
let http_cache = http_cache::HttpCache::new(&deps_cache_location);
|
2021-08-07 08:49:38 -04:00
|
|
|
|
|
|
|
let mut root_cert_store = RootCertStore::empty();
|
|
|
|
let ca_stores: Vec<String> = flags
|
|
|
|
.ca_stores
|
|
|
|
.clone()
|
|
|
|
.or_else(|| {
|
|
|
|
let env_ca_store = env::var("DENO_TLS_CA_STORE").ok()?;
|
|
|
|
Some(
|
|
|
|
env_ca_store
|
|
|
|
.split(',')
|
|
|
|
.map(|s| s.trim().to_string())
|
|
|
|
.filter(|s| !s.is_empty())
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| vec!["mozilla".to_string()]);
|
|
|
|
|
|
|
|
for store in ca_stores.iter() {
|
|
|
|
match store.as_str() {
|
|
|
|
"mozilla" => {
|
|
|
|
root_cert_store.add_server_trust_anchors(&TLS_SERVER_ROOTS);
|
|
|
|
}
|
|
|
|
"system" => {
|
|
|
|
let roots = load_native_certs()
|
|
|
|
.expect("could not load platform certs")
|
|
|
|
.roots;
|
|
|
|
root_cert_store.roots.extend(roots);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(anyhow!("Unknown certificate store \"{}\" specified (allowed: \"system,mozilla\")", store));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 18:15:52 -05:00
|
|
|
let ca_file = flags.ca_file.clone().or_else(|| env::var("DENO_CERT").ok());
|
2021-08-07 08:49:38 -04:00
|
|
|
if let Some(ca_file) = ca_file {
|
|
|
|
let certfile = File::open(&ca_file)?;
|
|
|
|
let mut reader = BufReader::new(certfile);
|
|
|
|
|
|
|
|
// This function does not return specific errors, if it fails give a generic message.
|
|
|
|
if let Err(_err) = root_cert_store.add_pem_file(&mut reader) {
|
|
|
|
return Err(anyhow!("Unable to add pem file to certificate store"));
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2021-08-09 10:53:21 -04:00
|
|
|
if let Some(insecure_allowlist) =
|
2021-08-10 07:19:45 -04:00
|
|
|
flags.unsafely_ignore_certificate_errors.as_ref()
|
2021-08-09 10:53:21 -04:00
|
|
|
{
|
|
|
|
let domains = if insecure_allowlist.is_empty() {
|
2021-08-10 07:19:45 -04:00
|
|
|
"for all hostnames".to_string()
|
2021-08-09 10:53:21 -04:00
|
|
|
} else {
|
|
|
|
format!("for: {}", insecure_allowlist.join(", "))
|
|
|
|
};
|
2021-08-10 12:47:44 -04:00
|
|
|
let msg =
|
|
|
|
format!("DANGER: TLS certificate validation is disabled {}", domains);
|
2021-08-09 10:53:21 -04:00
|
|
|
eprintln!("{}", colors::yellow(msg));
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let cache_usage = if flags.cached_only {
|
|
|
|
CacheSetting::Only
|
|
|
|
} else if !flags.cache_blocklist.is_empty() {
|
|
|
|
CacheSetting::ReloadSome(flags.cache_blocklist.clone())
|
|
|
|
} else if flags.reload {
|
|
|
|
CacheSetting::ReloadAll
|
|
|
|
} else {
|
|
|
|
CacheSetting::Use
|
|
|
|
};
|
|
|
|
|
2021-07-05 09:34:37 -04:00
|
|
|
let blob_store = BlobStore::default();
|
2021-05-22 12:08:24 -04:00
|
|
|
let broadcast_channel = InMemoryBroadcastChannel::default();
|
2021-07-06 13:42:52 -04:00
|
|
|
let shared_array_buffer_store = SharedArrayBufferStore::default();
|
2021-09-29 04:47:24 -04:00
|
|
|
let compiled_wasm_module_store = CompiledWasmModuleStore::default();
|
2021-04-07 09:22:14 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher = FileFetcher::new(
|
2020-02-19 08:17:13 -05:00
|
|
|
http_cache,
|
2020-11-05 19:38:21 -05:00
|
|
|
cache_usage,
|
|
|
|
!flags.no_remote,
|
2021-08-07 08:49:38 -04:00
|
|
|
Some(root_cert_store.clone()),
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store.clone(),
|
2021-08-10 07:19:45 -04:00
|
|
|
flags.unsafely_ignore_certificate_errors.clone(),
|
2019-11-04 10:38:52 -05:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let lockfile = if let Some(filename) = &flags.lock {
|
2020-09-24 18:31:17 -04:00
|
|
|
let lockfile = Lockfile::new(filename.clone(), flags.lock_write)?;
|
2020-10-23 17:01:54 -04:00
|
|
|
Some(Arc::new(Mutex::new(lockfile)))
|
2019-11-04 10:38:52 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
let maybe_config_file =
|
|
|
|
if let Some(config_path) = flags.config_path.as_ref() {
|
|
|
|
Some(ConfigFile::read(config_path)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-10-04 19:35:55 -04:00
|
|
|
let mut maybe_import_map: Option<ImportMap> =
|
2020-05-29 10:32:15 -04:00
|
|
|
match flags.import_map_path.as_ref() {
|
|
|
|
None => None,
|
2021-02-17 08:32:57 -05:00
|
|
|
Some(import_map_url) => {
|
|
|
|
let import_map_specifier =
|
2021-07-30 09:03:41 -04:00
|
|
|
deno_core::resolve_url_or_path(import_map_url).context(format!(
|
|
|
|
"Bad URL (\"{}\") for import map.",
|
|
|
|
import_map_url
|
|
|
|
))?;
|
2021-02-17 08:32:57 -05:00
|
|
|
let file = file_fetcher
|
2021-04-11 22:15:43 -04:00
|
|
|
.fetch(&import_map_specifier, &mut Permissions::allow_all())
|
2021-05-04 08:27:20 -04:00
|
|
|
.await
|
|
|
|
.context(format!(
|
|
|
|
"Unable to load '{}' import map",
|
|
|
|
import_map_specifier
|
|
|
|
))?;
|
2021-02-17 08:32:57 -05:00
|
|
|
let import_map =
|
|
|
|
ImportMap::from_json(import_map_specifier.as_str(), &file.source)?;
|
|
|
|
Some(import_map)
|
2020-05-29 10:32:15 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-04 19:35:55 -04:00
|
|
|
if flags.compat {
|
|
|
|
let mut import_map = match maybe_import_map {
|
|
|
|
Some(import_map) => import_map,
|
|
|
|
None => {
|
|
|
|
// INFO: we're creating an empty import map, with its specifier pointing
|
|
|
|
// to `CWD/node_import_map.json` to make sure the map still works as expected.
|
|
|
|
let import_map_specifier =
|
|
|
|
std::env::current_dir()?.join("node_import_map.json");
|
|
|
|
ImportMap::from_json(import_map_specifier.to_str().unwrap(), "{}")
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let node_builtins = crate::compat::get_mapped_node_builtins();
|
|
|
|
let diagnostics = import_map.update_imports(node_builtins)?;
|
|
|
|
|
|
|
|
if !diagnostics.is_empty() {
|
|
|
|
info!("Some Node built-ins were not added to the import map:");
|
|
|
|
for diagnostic in diagnostics {
|
|
|
|
info!(" - {}", diagnostic);
|
|
|
|
}
|
|
|
|
info!("If you want to use Node built-ins provided by Deno remove listed specifiers from \"imports\" mapping in the import map file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_import_map = Some(import_map);
|
|
|
|
}
|
|
|
|
|
2020-09-25 04:24:51 -04:00
|
|
|
let maybe_inspect_host = flags.inspect.or(flags.inspect_brk);
|
2021-03-25 14:17:37 -04:00
|
|
|
let maybe_inspector_server = maybe_inspect_host.map(|host| {
|
|
|
|
Arc::new(InspectorServer::new(host, version::get_user_agent()))
|
|
|
|
});
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2020-12-21 08:04:25 -05:00
|
|
|
let coverage_dir = flags
|
|
|
|
.coverage_dir
|
|
|
|
.clone()
|
|
|
|
.or_else(|| env::var("DENO_UNSTABLE_COVERAGE_DIR").ok());
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
Ok(ProcState(Arc::new(Inner {
|
2019-11-04 10:38:52 -05:00
|
|
|
dir,
|
2020-12-21 08:04:25 -05:00
|
|
|
coverage_dir,
|
2019-11-04 10:38:52 -05:00
|
|
|
flags,
|
|
|
|
file_fetcher,
|
2020-12-15 00:52:55 -05:00
|
|
|
modules: Default::default(),
|
2019-11-04 10:38:52 -05:00
|
|
|
lockfile,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map,
|
2020-09-25 04:24:51 -04:00
|
|
|
maybe_inspector_server,
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store: Some(root_cert_store.clone()),
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store,
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel,
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store,
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store,
|
2021-09-24 11:10:42 -04:00
|
|
|
})))
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
/// Prepares a set of module specifiers for loading in one shot.
|
|
|
|
pub async fn prepare_module_graph(
|
2021-09-24 11:10:42 -04:00
|
|
|
&self,
|
2021-04-28 14:17:04 -04:00
|
|
|
specifiers: Vec<ModuleSpecifier>,
|
|
|
|
lib: TypeLib,
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions: Permissions,
|
|
|
|
dynamic_permissions: Permissions,
|
2021-04-28 14:17:04 -04:00
|
|
|
maybe_import_map: Option<ImportMap>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let handler = Arc::new(Mutex::new(FetchHandler::new(
|
|
|
|
self,
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2021-04-28 14:17:04 -04:00
|
|
|
)?));
|
|
|
|
|
|
|
|
let mut builder =
|
|
|
|
GraphBuilder::new(handler, maybe_import_map, self.lockfile.clone());
|
|
|
|
|
|
|
|
for specifier in specifiers {
|
|
|
|
builder.add(&specifier, false).await?;
|
|
|
|
}
|
2021-06-21 17:18:32 -04:00
|
|
|
builder.analyze_config_file(&self.maybe_config_file).await?;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
|
|
|
let mut graph = builder.get_graph();
|
|
|
|
let debug = self.flags.log_level == Some(log::Level::Debug);
|
2021-05-10 12:16:39 -04:00
|
|
|
let maybe_config_file = self.maybe_config_file.clone();
|
2021-06-19 10:14:43 -04:00
|
|
|
let reload_exclusions = {
|
2021-07-06 23:48:01 -04:00
|
|
|
let modules = self.modules.lock();
|
2021-06-19 10:14:43 -04:00
|
|
|
modules.keys().cloned().collect::<HashSet<_>>()
|
|
|
|
};
|
2021-04-28 14:17:04 -04:00
|
|
|
|
|
|
|
let result_modules = if self.flags.no_check {
|
|
|
|
let result_info = graph.transpile(TranspileOptions {
|
|
|
|
debug,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file,
|
2021-04-28 14:17:04 -04:00
|
|
|
reload: self.flags.reload,
|
2021-06-19 10:14:43 -04:00
|
|
|
reload_exclusions,
|
2021-04-28 14:17:04 -04:00
|
|
|
})?;
|
|
|
|
debug!("{}", result_info.stats);
|
|
|
|
if let Some(ignored_options) = result_info.maybe_ignored_options {
|
|
|
|
warn!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
result_info.loadable_modules
|
|
|
|
} else {
|
|
|
|
let result_info = graph.check(CheckOptions {
|
|
|
|
debug,
|
|
|
|
emit: true,
|
|
|
|
lib,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file,
|
2021-04-28 14:17:04 -04:00
|
|
|
reload: self.flags.reload,
|
2021-06-19 10:14:43 -04:00
|
|
|
reload_exclusions,
|
2021-04-28 14:17:04 -04:00
|
|
|
})?;
|
|
|
|
|
|
|
|
debug!("{}", result_info.stats);
|
|
|
|
if let Some(ignored_options) = result_info.maybe_ignored_options {
|
|
|
|
eprintln!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
if !result_info.diagnostics.is_empty() {
|
|
|
|
return Err(anyhow!(result_info.diagnostics));
|
|
|
|
}
|
|
|
|
result_info.loadable_modules
|
|
|
|
};
|
|
|
|
|
2021-07-06 23:48:01 -04:00
|
|
|
let mut loadable_modules = self.modules.lock();
|
2021-04-28 14:17:04 -04:00
|
|
|
loadable_modules.extend(result_modules);
|
|
|
|
|
|
|
|
if let Some(ref lockfile) = self.lockfile {
|
2021-07-06 23:48:01 -04:00
|
|
|
let g = lockfile.lock();
|
2021-04-28 14:17:04 -04:00
|
|
|
g.write()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
/// This function is called when new module load is initialized by the JsRuntime. Its
|
|
|
|
/// resposibility is to collect all dependencies and if it is required then also perform TS
|
|
|
|
/// typecheck and traspilation.
|
2020-05-29 10:32:15 -04:00
|
|
|
pub async fn prepare_module_load(
|
2021-09-24 11:10:42 -04:00
|
|
|
&self,
|
2020-10-22 20:50:15 -04:00
|
|
|
specifier: ModuleSpecifier,
|
2020-11-01 21:51:56 -05:00
|
|
|
lib: TypeLib,
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions: Permissions,
|
|
|
|
dynamic_permissions: Permissions,
|
2020-10-22 20:50:15 -04:00
|
|
|
is_dynamic: bool,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map: Option<ImportMap>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-10-22 20:50:15 -04:00
|
|
|
let specifier = specifier.clone();
|
2021-05-17 03:44:38 -04:00
|
|
|
let handler = Arc::new(Mutex::new(FetchHandler::new(
|
|
|
|
self,
|
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
|
|
|
)?));
|
2020-10-23 17:01:54 -04:00
|
|
|
let mut builder =
|
2020-11-02 14:41:20 -05:00
|
|
|
GraphBuilder::new(handler, maybe_import_map, self.lockfile.clone());
|
2020-10-22 20:50:15 -04:00
|
|
|
builder.add(&specifier, is_dynamic).await?;
|
2021-06-21 17:18:32 -04:00
|
|
|
builder.analyze_config_file(&self.maybe_config_file).await?;
|
2020-10-23 17:01:54 -04:00
|
|
|
let mut graph = builder.get_graph();
|
2020-10-22 20:50:15 -04:00
|
|
|
let debug = self.flags.log_level == Some(log::Level::Debug);
|
2021-05-10 12:16:39 -04:00
|
|
|
let maybe_config_file = self.maybe_config_file.clone();
|
2021-06-19 10:14:43 -04:00
|
|
|
let reload_exclusions = {
|
2021-07-06 23:48:01 -04:00
|
|
|
let modules = self.modules.lock();
|
2021-06-19 10:14:43 -04:00
|
|
|
modules.keys().cloned().collect::<HashSet<_>>()
|
|
|
|
};
|
2020-05-29 10:32:15 -04:00
|
|
|
|
2020-12-15 00:52:55 -05:00
|
|
|
let result_modules = if self.flags.no_check {
|
|
|
|
let result_info = graph.transpile(TranspileOptions {
|
|
|
|
debug,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file,
|
2020-12-15 00:52:55 -05:00
|
|
|
reload: self.flags.reload,
|
2021-06-19 10:14:43 -04:00
|
|
|
reload_exclusions,
|
2020-12-15 00:52:55 -05:00
|
|
|
})?;
|
|
|
|
debug!("{}", result_info.stats);
|
|
|
|
if let Some(ignored_options) = result_info.maybe_ignored_options {
|
|
|
|
warn!("{}", ignored_options);
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
2020-12-15 00:52:55 -05:00
|
|
|
result_info.loadable_modules
|
2020-09-24 18:31:17 -04:00
|
|
|
} else {
|
2020-11-01 21:51:56 -05:00
|
|
|
let result_info = graph.check(CheckOptions {
|
|
|
|
debug,
|
|
|
|
emit: true,
|
|
|
|
lib,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file,
|
2020-11-01 21:51:56 -05:00
|
|
|
reload: self.flags.reload,
|
2021-06-19 10:14:43 -04:00
|
|
|
reload_exclusions,
|
2020-11-01 21:51:56 -05:00
|
|
|
})?;
|
|
|
|
|
|
|
|
debug!("{}", result_info.stats);
|
|
|
|
if let Some(ignored_options) = result_info.maybe_ignored_options {
|
2020-10-22 20:50:15 -04:00
|
|
|
eprintln!("{}", ignored_options);
|
2020-07-08 05:26:39 -04:00
|
|
|
}
|
2020-11-01 21:51:56 -05:00
|
|
|
if !result_info.diagnostics.is_empty() {
|
2020-12-15 00:52:55 -05:00
|
|
|
return Err(anyhow!(result_info.diagnostics));
|
2020-10-22 20:50:15 -04:00
|
|
|
}
|
2020-12-15 00:52:55 -05:00
|
|
|
result_info.loadable_modules
|
2020-10-22 20:50:15 -04:00
|
|
|
};
|
2020-05-29 10:32:15 -04:00
|
|
|
|
2021-07-06 23:48:01 -04:00
|
|
|
let mut loadable_modules = self.modules.lock();
|
2020-12-15 00:52:55 -05:00
|
|
|
loadable_modules.extend(result_modules);
|
|
|
|
|
2020-07-02 11:54:51 -04:00
|
|
|
if let Some(ref lockfile) = self.lockfile {
|
2021-07-06 23:48:01 -04:00
|
|
|
let g = lockfile.lock();
|
2020-07-02 11:54:51 -04:00
|
|
|
g.write()?;
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:52:55 -05:00
|
|
|
pub fn load(
|
2020-05-29 10:32:15 -04:00
|
|
|
&self,
|
2020-12-15 00:52:55 -05:00
|
|
|
specifier: ModuleSpecifier,
|
2020-10-22 20:50:15 -04:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-12-15 00:52:55 -05:00
|
|
|
) -> Result<ModuleSource, AnyError> {
|
2021-07-06 23:48:01 -04:00
|
|
|
let modules = self.modules.lock();
|
2020-12-15 00:52:55 -05:00
|
|
|
modules
|
|
|
|
.get(&specifier)
|
|
|
|
.map(|r| match r {
|
|
|
|
Ok(module_source) => Ok(module_source.clone()),
|
|
|
|
Err(err) => {
|
|
|
|
// TODO(@kitsonk) this feels a bit hacky but it works, without
|
|
|
|
// introducing another enum to have to try to deal with.
|
|
|
|
if get_custom_error_class(err) == Some("NotFound") {
|
|
|
|
let message = if let Some(referrer) = &maybe_referrer {
|
|
|
|
format!("{}\n From: {}\n If the source module contains only types, use `import type` and `export type` to import it instead.", err, referrer)
|
|
|
|
} else {
|
|
|
|
format!("{}\n If the source module contains only types, use `import type` and `export type` to import it instead.", err)
|
|
|
|
};
|
|
|
|
warn!("{}: {}", crate::colors::yellow("warning"), message);
|
|
|
|
Ok(ModuleSource {
|
|
|
|
code: "".to_string(),
|
|
|
|
module_url_found: specifier.to_string(),
|
|
|
|
module_url_specified: specifier.to_string(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// anyhow errors don't support cloning, so we have to manage this
|
|
|
|
// ourselves
|
|
|
|
Err(anyhow!(err.to_string()))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
if let Some(referrer) = maybe_referrer {
|
|
|
|
Err(anyhow!(
|
|
|
|
"Module \"{}\" is missing from the graph.\n From: {}",
|
|
|
|
specifier,
|
|
|
|
referrer
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Err(anyhow!(
|
|
|
|
"Module \"{}\" is missing from the graph.",
|
|
|
|
specifier
|
|
|
|
))
|
|
|
|
}
|
|
|
|
})
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 00:52:55 -05:00
|
|
|
// TODO(@kitsonk) this should be refactored to get it from the module graph
|
2020-10-22 20:50:15 -04:00
|
|
|
fn get_emit(&self, url: &Url) -> Option<(Vec<u8>, Option<Vec<u8>>)> {
|
|
|
|
match url.scheme() {
|
|
|
|
// we should only be looking for emits for schemes that denote external
|
|
|
|
// modules, which the disk_cache supports
|
2021-04-07 09:22:14 -04:00
|
|
|
"wasm" | "file" | "http" | "https" | "data" | "blob" => (),
|
2020-10-22 20:50:15 -04:00
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let emit_path = self
|
|
|
|
.dir
|
|
|
|
.gen_cache
|
2021-07-30 09:03:41 -04:00
|
|
|
.get_cache_filename_with_extension(url, "js")?;
|
2020-10-22 20:50:15 -04:00
|
|
|
let emit_map_path = self
|
|
|
|
.dir
|
|
|
|
.gen_cache
|
2021-07-30 09:03:41 -04:00
|
|
|
.get_cache_filename_with_extension(url, "js.map")?;
|
2020-10-22 20:50:15 -04:00
|
|
|
if let Ok(code) = self.dir.gen_cache.get(&emit_path) {
|
|
|
|
let maybe_map = if let Ok(map) = self.dir.gen_cache.get(&emit_map_path) {
|
|
|
|
Some(map)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
Some((code, maybe_map))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
// TODO(@kitsonk) this is only temporary, but should be refactored to somewhere
|
|
|
|
// else, like a refactored file_fetcher.
|
2021-09-24 11:10:42 -04:00
|
|
|
impl SourceMapGetter for ProcState {
|
2020-10-22 20:50:15 -04:00
|
|
|
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
|
2021-02-17 13:47:18 -05:00
|
|
|
if let Ok(specifier) = resolve_url(file_name) {
|
|
|
|
if let Some((code, maybe_map)) = self.get_emit(&specifier) {
|
2021-02-07 18:14:05 -05:00
|
|
|
let code = String::from_utf8(code).unwrap();
|
|
|
|
source_map_from_code(code).or(maybe_map)
|
2021-01-05 18:10:36 -05:00
|
|
|
} else if let Ok(source) = self.load(specifier, None) {
|
|
|
|
source_map_from_code(source.code)
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-06-15 11:53:05 -04:00
|
|
|
} else {
|
2020-10-22 20:50:15 -04:00
|
|
|
None
|
2020-06-10 10:02:41 -04:00
|
|
|
}
|
2020-10-22 20:50:15 -04:00
|
|
|
}
|
2020-06-01 15:01:51 -04:00
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
|
|
|
file_name: &str,
|
|
|
|
line_number: usize,
|
|
|
|
) -> Option<String> {
|
2021-02-17 13:47:18 -05:00
|
|
|
if let Ok(specifier) = resolve_url(file_name) {
|
2020-11-15 21:19:31 -05:00
|
|
|
self.file_fetcher.get_source(&specifier).map(|out| {
|
2020-11-05 19:38:21 -05:00
|
|
|
// Do NOT use .lines(): it skips the terminating empty line.
|
|
|
|
// (due to internally using .split_terminator() instead of .split())
|
|
|
|
let lines: Vec<&str> = out.source.split('\n').collect();
|
|
|
|
assert!(lines.len() > line_number);
|
|
|
|
lines[line_number].to_string()
|
|
|
|
})
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 15:01:51 -04:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:10:36 -05:00
|
|
|
fn source_map_from_code(code: String) -> Option<Vec<u8>> {
|
|
|
|
let lines: Vec<&str> = code.split('\n').collect();
|
|
|
|
if let Some(last_line) = lines.last() {
|
|
|
|
if last_line
|
|
|
|
.starts_with("//# sourceMappingURL=data:application/json;base64,")
|
|
|
|
{
|
|
|
|
let input = last_line.trim_start_matches(
|
|
|
|
"//# sourceMappingURL=data:application/json;base64,",
|
|
|
|
);
|
|
|
|
let decoded_map = base64::decode(input)
|
|
|
|
.expect("Unable to decode source map from emitted file.");
|
|
|
|
Some(decoded_map)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|