1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

refactor(compile): remove usage of ProcState and CliOptions (#18855)

This commit is contained in:
David Sherret 2023-04-26 13:07:15 -04:00 committed by GitHub
parent 5f7db93d0b
commit 55a9977c62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 146 additions and 86 deletions

View file

@ -2230,7 +2230,7 @@ fn check_arg(checks_local_by_default: bool) -> Arg {
default, so adding --check is redundant.
If the value of '--check=all' is supplied, diagnostic errors from remote modules
will be included.
Alternatively, the 'deno check' subcommand can be used.",
)
} else {
@ -2745,7 +2745,8 @@ fn run_parse(flags: &mut Flags, matches: &mut ArgMatches) {
fn task_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.config_flag = matches
.remove_one::<String>("config")
.map_or(ConfigFlag::Discover, ConfigFlag::Path);
.map(ConfigFlag::Path)
.unwrap_or(ConfigFlag::Discover);
let mut task_flags = TaskFlags {
cwd: matches.remove_one::<String>("cwd"),

View file

@ -430,8 +430,8 @@ fn create_lsp_structs(
) {
let registry_url = CliNpmRegistryApi::default_url();
let progress_bar = ProgressBar::new(ProgressBarStyle::TextOnly);
let npm_cache = Arc::new(NpmCache::from_deno_dir(
dir,
let npm_cache = Arc::new(NpmCache::new(
dir.npm_folder_path(),
// Use an "only" cache setting in order to make the
// user do an explicit "cache" command and prevent
// the cache from being filled with lots of packages while

View file

@ -129,7 +129,7 @@ impl Default for ReadonlyNpmCache {
// This only gets used when creating the tsc runtime and for testing, and so
// it shouldn't ever actually access the DenoDir, so it doesn't support a
// custom root.
Self::from_deno_dir(&DenoDir::new(None).unwrap())
Self::new(DenoDir::new(None).unwrap().npm_folder_path())
}
}
@ -155,10 +155,6 @@ impl ReadonlyNpmCache {
}
}
pub fn from_deno_dir(dir: &DenoDir) -> Self {
Self::new(dir.npm_folder_path())
}
pub fn root_dir_url(&self) -> &Url {
&self.root_dir_url
}
@ -306,14 +302,14 @@ pub struct NpmCache {
}
impl NpmCache {
pub fn from_deno_dir(
dir: &DenoDir,
pub fn new(
cache_dir_path: PathBuf,
cache_setting: CacheSetting,
http_client: HttpClient,
progress_bar: ProgressBar,
) -> Self {
Self {
readonly: ReadonlyNpmCache::from_deno_dir(dir),
readonly: ReadonlyNpmCache::new(cache_dir_path),
cache_setting,
http_client,
progress_bar,

View file

@ -227,8 +227,8 @@ impl ProcState {
let lockfile = cli_options.maybe_lock_file();
let npm_registry_url = CliNpmRegistryApi::default_url().to_owned();
let npm_cache = Arc::new(NpmCache::from_deno_dir(
&dir,
let npm_cache = Arc::new(NpmCache::new(
dir.npm_folder_path(),
cli_options.cache_setting(),
http_client.clone(),
progress_bar.clone(),
@ -250,7 +250,7 @@ impl ProcState {
let node_fs = Arc::new(deno_node::RealFs);
let npm_fs_resolver = create_npm_fs_resolver(
node_fs.clone(),
npm_cache,
npm_cache.clone(),
&progress_bar,
npm_registry_url,
npm_resolution.clone(),
@ -302,12 +302,6 @@ impl ProcState {
parsed_source_cache.clone(),
emit_options,
));
let npm_cache = Arc::new(NpmCache::from_deno_dir(
&dir,
cli_options.cache_setting(),
http_client.clone(),
progress_bar.clone(),
));
let file_fetcher = Arc::new(file_fetcher);
let node_analysis_cache =
NodeAnalysisCache::new(caches.node_analysis_db(&dir));

View file

@ -1,11 +1,20 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::args::get_root_cert_store;
use crate::args::CaData;
use crate::args::Flags;
use crate::args::CacheSetting;
use crate::cache::DenoDir;
use crate::colors;
use crate::file_fetcher::get_source_from_data_url;
use crate::http_util::HttpClient;
use crate::npm::create_npm_fs_resolver;
use crate::npm::CliNpmRegistryApi;
use crate::npm::CliNpmResolver;
use crate::npm::NpmCache;
use crate::npm::NpmResolution;
use crate::ops;
use crate::proc_state::ProcState;
use crate::util::progress_bar::ProgressBar;
use crate::util::progress_bar::ProgressBarStyle;
use crate::util::v8::construct_v8_flags;
use crate::version;
use crate::CliGraphResolver;
@ -16,11 +25,17 @@ use deno_core::futures::task::LocalFutureObj;
use deno_core::futures::FutureExt;
use deno_core::located_script_name;
use deno_core::v8_set_flags;
use deno_core::CompiledWasmModuleStore;
use deno_core::ModuleLoader;
use deno_core::ModuleSpecifier;
use deno_core::ModuleType;
use deno_core::ResolutionKind;
use deno_core::SharedArrayBufferStore;
use deno_graph::source::Resolver;
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_runtime::deno_node;
use deno_runtime::deno_tls::rustls::RootCertStore;
use deno_runtime::deno_web::BlobStore;
use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
use deno_runtime::ops::worker_host::WorkerEventCb;
@ -122,28 +137,6 @@ impl ModuleLoader for EmbeddedModuleLoader {
}
}
fn metadata_to_flags(metadata: &Metadata) -> Flags {
let permissions = metadata.permissions.clone();
Flags {
argv: metadata.argv.clone(),
unstable: metadata.unstable,
seed: metadata.seed,
location: metadata.location.clone(),
allow_env: permissions.allow_env,
allow_hrtime: permissions.allow_hrtime,
allow_net: permissions.allow_net,
allow_ffi: permissions.allow_ffi,
allow_read: permissions.allow_read,
allow_run: permissions.allow_run,
allow_write: permissions.allow_write,
v8_flags: metadata.v8_flags.clone(),
log_level: metadata.log_level,
ca_stores: metadata.ca_stores.clone(),
ca_data: metadata.ca_data.clone().map(CaData::Bytes),
..Default::default()
}
}
fn web_worker_callback() -> Arc<WorkerEventCb> {
Arc::new(|worker| {
let fut = async move { Ok(worker) };
@ -151,25 +144,41 @@ fn web_worker_callback() -> Arc<WorkerEventCb> {
})
}
struct SharedWorkerState {
npm_resolver: Arc<CliNpmResolver>,
root_cert_store: RootCertStore,
node_fs: Arc<dyn deno_node::NodeFs>,
blob_store: BlobStore,
broadcast_channel: InMemoryBroadcastChannel,
shared_array_buffer_store: SharedArrayBufferStore,
compiled_wasm_module_store: CompiledWasmModuleStore,
// options
argv: Vec<String>,
seed: Option<u64>,
unsafely_ignore_certificate_errors: Option<Vec<String>>,
unstable: bool,
}
fn create_web_worker_callback(
ps: &ProcState,
module_loader: &Rc<EmbeddedModuleLoader>,
shared: &Arc<SharedWorkerState>,
module_loader: &EmbeddedModuleLoader,
) -> Arc<CreateWebWorkerCb> {
let ps = ps.clone();
let module_loader = module_loader.as_ref().clone();
let shared = shared.clone();
let module_loader = module_loader.clone();
Arc::new(move |args| {
let module_loader = Rc::new(module_loader.clone());
let create_web_worker_cb = create_web_worker_callback(&ps, &module_loader);
let create_web_worker_cb =
create_web_worker_callback(&shared, &module_loader);
let web_worker_cb = web_worker_callback();
let options = WebWorkerOptions {
bootstrap: BootstrapOptions {
args: ps.options.argv().clone(),
args: shared.argv.clone(),
cpu_count: std::thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(1),
debug_flag: ps.options.log_level().map_or(false, |l| l == Level::Debug),
debug_flag: false,
enable_testing_features: false,
locale: deno_core::v8::icu::get_language_tag(),
location: Some(args.main_module.clone()),
@ -177,20 +186,19 @@ fn create_web_worker_callback(
is_tty: colors::is_tty(),
runtime_version: version::deno().to_string(),
ts_version: version::TYPESCRIPT.to_string(),
unstable: ps.options.unstable(),
unstable: shared.unstable,
user_agent: version::get_user_agent().to_string(),
inspect: ps.options.is_inspecting(),
inspect: false,
},
extensions: ops::cli_exts(ps.npm_resolver.clone()),
extensions: ops::cli_exts(shared.npm_resolver.clone()),
startup_snapshot: Some(crate::js::deno_isolate_init()),
unsafely_ignore_certificate_errors: ps
.options
.unsafely_ignore_certificate_errors()
unsafely_ignore_certificate_errors: shared
.unsafely_ignore_certificate_errors
.clone(),
root_cert_store: Some(ps.root_cert_store.clone()),
seed: ps.options.seed(),
root_cert_store: Some(shared.root_cert_store.clone()),
seed: shared.seed,
module_loader,
node_fs: Some(ps.node_fs.clone()),
node_fs: Some(shared.node_fs.clone()),
npm_resolver: None, // not currently supported
create_web_worker_cb,
preload_module_cb: web_worker_cb.clone(),
@ -200,10 +208,12 @@ fn create_web_worker_callback(
worker_type: args.worker_type,
maybe_inspector_server: None,
get_error_class_fn: Some(&get_error_class_name),
blob_store: ps.blob_store.clone(),
broadcast_channel: ps.broadcast_channel.clone(),
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
blob_store: shared.blob_store.clone(),
broadcast_channel: shared.broadcast_channel.clone(),
shared_array_buffer_store: Some(shared.shared_array_buffer_store.clone()),
compiled_wasm_module_store: Some(
shared.compiled_wasm_module_store.clone(),
),
cache_storage_dir: None,
stdio: Default::default(),
};
@ -222,13 +232,67 @@ pub async fn run(
eszip: eszip::EszipV2,
metadata: Metadata,
) -> Result<(), AnyError> {
let flags = metadata_to_flags(&metadata);
let main_module = &metadata.entrypoint;
let ps = ProcState::from_flags(flags).await?;
let dir = DenoDir::new(None)?;
let root_cert_store = get_root_cert_store(
None,
metadata.ca_stores,
metadata.ca_data.map(CaData::Bytes),
)?;
let progress_bar = ProgressBar::new(ProgressBarStyle::TextOnly);
let http_client = HttpClient::new(
Some(root_cert_store.clone()),
metadata.unsafely_ignore_certificate_errors.clone(),
)?;
let npm_registry_url = CliNpmRegistryApi::default_url().to_owned();
let npm_cache = Arc::new(NpmCache::new(
dir.npm_folder_path(),
CacheSetting::Use,
http_client.clone(),
progress_bar.clone(),
));
let npm_api = Arc::new(CliNpmRegistryApi::new(
npm_registry_url.clone(),
npm_cache.clone(),
http_client.clone(),
progress_bar.clone(),
));
let node_fs = Arc::new(deno_node::RealFs);
let npm_resolution =
Arc::new(NpmResolution::from_serialized(npm_api.clone(), None, None));
let npm_fs_resolver = create_npm_fs_resolver(
node_fs.clone(),
npm_cache,
&progress_bar,
npm_registry_url,
npm_resolution.clone(),
None,
);
let npm_resolver = Arc::new(CliNpmResolver::new(
npm_resolution.clone(),
npm_fs_resolver,
None,
));
let shared = Arc::new(SharedWorkerState {
npm_resolver,
root_cert_store,
node_fs,
blob_store: BlobStore::default(),
broadcast_channel: InMemoryBroadcastChannel::default(),
shared_array_buffer_store: SharedArrayBufferStore::default(),
compiled_wasm_module_store: CompiledWasmModuleStore::default(),
argv: metadata.argv,
seed: metadata.seed,
unsafely_ignore_certificate_errors: metadata
.unsafely_ignore_certificate_errors,
unstable: metadata.unstable,
});
let permissions = PermissionsContainer::new(Permissions::from_options(
&metadata.permissions,
)?);
let module_loader = Rc::new(EmbeddedModuleLoader {
let module_loader = EmbeddedModuleLoader {
eszip: Arc::new(eszip),
maybe_import_map_resolver: metadata.maybe_import_map.map(
|(base, source)| {
@ -238,21 +302,22 @@ pub async fn run(
parse_from_json(&base, &source).unwrap().import_map,
)),
false,
ps.npm_api.clone(),
ps.npm_resolution.clone(),
ps.package_json_deps_installer.clone(),
npm_api.clone(),
npm_resolution.clone(),
Default::default(),
))
},
),
});
let create_web_worker_cb = create_web_worker_callback(&ps, &module_loader);
};
let create_web_worker_cb =
create_web_worker_callback(&shared, &module_loader);
let web_worker_cb = web_worker_callback();
v8_set_flags(construct_v8_flags(&metadata.v8_flags, vec![]));
let options = WorkerOptions {
bootstrap: BootstrapOptions {
args: metadata.argv,
args: shared.argv.clone(),
cpu_count: std::thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(1),
@ -269,13 +334,14 @@ pub async fn run(
ts_version: version::TYPESCRIPT.to_string(),
unstable: metadata.unstable,
user_agent: version::get_user_agent().to_string(),
inspect: ps.options.is_inspecting(),
inspect: false,
},
extensions: ops::cli_exts(ps.npm_resolver.clone()),
extensions: ops::cli_exts(shared.npm_resolver.clone()),
startup_snapshot: Some(crate::js::deno_isolate_init()),
unsafely_ignore_certificate_errors: metadata
.unsafely_ignore_certificate_errors,
root_cert_store: Some(ps.root_cert_store.clone()),
unsafely_ignore_certificate_errors: shared
.unsafely_ignore_certificate_errors
.clone(),
root_cert_store: Some(shared.root_cert_store.clone()),
seed: metadata.seed,
source_map_getter: None,
format_js_error_fn: Some(Arc::new(format_js_error)),
@ -285,16 +351,16 @@ pub async fn run(
maybe_inspector_server: None,
should_break_on_first_statement: false,
should_wait_for_inspector_session: false,
module_loader,
node_fs: Some(ps.node_fs.clone()),
module_loader: Rc::new(module_loader),
node_fs: Some(shared.node_fs.clone()),
npm_resolver: None, // not currently supported
get_error_class_fn: Some(&get_error_class_name),
cache_storage_dir: None,
origin_storage_dir: None,
blob_store: ps.blob_store.clone(),
broadcast_channel: ps.broadcast_channel.clone(),
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
blob_store: shared.blob_store.clone(),
broadcast_channel: shared.broadcast_channel.clone(),
shared_array_buffer_store: Some(shared.shared_array_buffer_store.clone()),
compiled_wasm_module_store: Some(shared.compiled_wasm_module_store.clone()),
stdio: Default::default(),
};
let mut worker = MainWorker::bootstrap_from_options(

View file

@ -931,7 +931,10 @@ mod mitata {
sysctl.arg("-n");
sysctl.arg("machdep.cpu.brand_string");
return std::str::from_utf8(
&sysctl.output().map_or(Vec::from("unknown"), |x| x.stdout),
&sysctl
.output()
.map(|x| x.stdout)
.unwrap_or(Vec::from("unknown")),
)
.unwrap()
.trim()