1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

build: allow disabling snapshots for dev (#20048)

Closes #19399 (running without snapshots at all was suggested as an
alternative solution).

Adds a `__runtime_js_sources` pseudo-private feature to load extension
JS sources at runtime for faster development, instead of building and
loading snapshots or embedding sources in the binary. Will only work in
a development environment obviously.

Try running `cargo test --features __runtime_js_sources
integration::node_unit_tests::os_test`. Then break some behaviour in
`ext/node/polyfills/os.ts` e.g. make `function cpus() {}` return an
empty array, and run it again. Fix and then run again. No more build
time in between.
This commit is contained in:
Nayeem Rahman 2023-08-06 00:47:15 +01:00 committed by GitHub
parent b96f283064
commit c1c8eb3d55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 274 additions and 111 deletions

View file

@ -26,6 +26,11 @@ name = "lsp_bench_standalone"
harness = false harness = false
path = "./bench/lsp_bench_standalone.rs" path = "./bench/lsp_bench_standalone.rs"
[features]
# A dev feature to disable creations and loading of snapshots in favor of
# loading JS sources at runtime.
__runtime_js_sources = ["deno_runtime/__runtime_js_sources"]
[build-dependencies] [build-dependencies]
deno_runtime = { workspace = true, features = ["snapshot_from_snapshot", "include_js_files_for_snapshotting"] } deno_runtime = { workspace = true, features = ["snapshot_from_snapshot", "include_js_files_for_snapshotting"] }
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] } deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }

View file

@ -2,16 +2,10 @@
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc;
use deno_core::snapshot_util::*; use deno_core::snapshot_util::*;
use deno_core::Extension;
use deno_core::ExtensionFileSource; use deno_core::ExtensionFileSource;
use deno_core::ExtensionFileSourceCode; use deno_core::ExtensionFileSourceCode;
use deno_runtime::deno_cache::SqliteBackedCache;
use deno_runtime::deno_http::DefaultHttpPropertyExtractor;
use deno_runtime::deno_kv::sqlite::SqliteDbHandler;
use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::*; use deno_runtime::*;
mod ts { mod ts {
@ -304,12 +298,10 @@ mod ts {
} }
} }
// FIXME(bartlomieju): information about which extensions were // Duplicated in `ops/mod.rs`. Keep in sync!
// already snapshotted is not preserved in the snapshot. This should be
// fixed, so we can reliably depend on that information.
// deps = [runtime]
deno_core::extension!( deno_core::extension!(
cli, cli,
deps = [runtime],
esm_entry_point = "ext:cli/99_main.js", esm_entry_point = "ext:cli/99_main.js",
esm = [ esm = [
dir "js", dir "js",
@ -326,8 +318,16 @@ deno_core::extension!(
} }
); );
#[cfg(not(feature = "__runtime_js_sources"))]
#[must_use = "The files listed by create_cli_snapshot should be printed as 'cargo:rerun-if-changed' lines"] #[must_use = "The files listed by create_cli_snapshot should be printed as 'cargo:rerun-if-changed' lines"]
fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput { fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
use deno_core::Extension;
use deno_runtime::deno_cache::SqliteBackedCache;
use deno_runtime::deno_http::DefaultHttpPropertyExtractor;
use deno_runtime::deno_kv::sqlite::SqliteDbHandler;
use deno_runtime::permissions::PermissionsContainer;
use std::sync::Arc;
// NOTE(bartlomieju): ordering is important here, keep it in sync with // NOTE(bartlomieju): ordering is important here, keep it in sync with
// `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/build.rs`! // `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/build.rs`!
let fs = Arc::new(deno_fs::RealFs); let fs = Arc::new(deno_fs::RealFs);
@ -367,13 +367,14 @@ fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
deno_io::deno_io::init_ops(Default::default()), deno_io::deno_io::init_ops(Default::default()),
deno_fs::deno_fs::init_ops::<PermissionsContainer>(false, fs.clone()), deno_fs::deno_fs::init_ops::<PermissionsContainer>(false, fs.clone()),
deno_node::deno_node::init_ops::<PermissionsContainer>(None, fs), deno_node::deno_node::init_ops::<PermissionsContainer>(None, fs),
deno_runtime::runtime::init_ops(),
cli::init_ops_and_esm(), // NOTE: This needs to be init_ops_and_esm! cli::init_ops_and_esm(), // NOTE: This needs to be init_ops_and_esm!
]; ];
create_snapshot(CreateSnapshotOptions { create_snapshot(CreateSnapshotOptions {
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"), cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
snapshot_path, snapshot_path,
startup_snapshot: Some(deno_runtime::js::deno_isolate_init()), startup_snapshot: deno_runtime::js::deno_isolate_init(),
extensions, extensions,
compression_cb: None, compression_cb: None,
with_runtime_cb: None, with_runtime_cb: None,
@ -483,10 +484,13 @@ fn main() {
let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin"); let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin");
ts::create_compiler_snapshot(compiler_snapshot_path, &c); ts::create_compiler_snapshot(compiler_snapshot_path, &c);
let cli_snapshot_path = o.join("CLI_SNAPSHOT.bin"); #[cfg(not(feature = "__runtime_js_sources"))]
let output = create_cli_snapshot(cli_snapshot_path); {
for path in output.files_loaded_during_snapshot { let cli_snapshot_path = o.join("CLI_SNAPSHOT.bin");
println!("cargo:rerun-if-changed={}", path.display()) let output = create_cli_snapshot(cli_snapshot_path);
for path in output.files_loaded_during_snapshot {
println!("cargo:rerun-if-changed={}", path.display())
}
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]

View file

@ -3,12 +3,20 @@
use deno_core::Snapshot; use deno_core::Snapshot;
use log::debug; use log::debug;
#[cfg(not(feature = "__runtime_js_sources"))]
static CLI_SNAPSHOT: &[u8] = static CLI_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/CLI_SNAPSHOT.bin")); include_bytes!(concat!(env!("OUT_DIR"), "/CLI_SNAPSHOT.bin"));
pub fn deno_isolate_init() -> Snapshot { pub fn deno_isolate_init() -> Option<Snapshot> {
debug!("Deno isolate init with snapshots."); debug!("Deno isolate init with snapshots.");
Snapshot::Static(CLI_SNAPSHOT) #[cfg(not(feature = "__runtime_js_sources"))]
{
Some(Snapshot::Static(CLI_SNAPSHOT))
}
#[cfg(feature = "__runtime_js_sources")]
{
None
}
} }
#[cfg(test)] #[cfg(test)]
@ -18,7 +26,7 @@ mod tests {
#[test] #[test]
fn runtime_snapshot() { fn runtime_snapshot() {
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions { let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
startup_snapshot: Some(deno_isolate_init()), startup_snapshot: deno_isolate_init(),
..Default::default() ..Default::default()
}); });
js_runtime js_runtime

View file

@ -12,17 +12,38 @@ pub mod bench;
pub mod testing; pub mod testing;
pub fn cli_exts(npm_resolver: Arc<CliNpmResolver>) -> Vec<Extension> { pub fn cli_exts(npm_resolver: Arc<CliNpmResolver>) -> Vec<Extension> {
vec![deno_cli::init_ops(npm_resolver)] vec![
#[cfg(not(feature = "__runtime_js_sources"))]
cli::init_ops(npm_resolver),
#[cfg(feature = "__runtime_js_sources")]
cli::init_ops_and_esm(npm_resolver),
]
} }
deno_core::extension!(deno_cli, // ESM parts duplicated in `../build.rs`. Keep in sync!
deno_core::extension!(cli,
deps = [runtime],
ops = [op_npm_process_state], ops = [op_npm_process_state],
esm_entry_point = "ext:cli/99_main.js",
esm = [
dir "js",
"40_testing.js",
"99_main.js"
],
options = { options = {
npm_resolver: Arc<CliNpmResolver>, npm_resolver: Arc<CliNpmResolver>,
}, },
state = |state, options| { state = |state, options| {
state.put(options.npm_resolver); state.put(options.npm_resolver);
}, },
customizer = |ext: &mut deno_core::Extension| {
ext.esm_files.to_mut().push(deno_core::ExtensionFileSource {
specifier: "ext:cli/runtime/js/99_main.js",
code: deno_core::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
deno_runtime::js::PATH_FOR_99_MAIN_JS,
),
});
},
); );
#[op] #[op]

View file

@ -450,7 +450,7 @@ impl CliMainWorkerFactory {
inspect: shared.options.is_inspecting, inspect: shared.options.is_inspecting,
}, },
extensions, extensions,
startup_snapshot: Some(crate::js::deno_isolate_init()), startup_snapshot: crate::js::deno_isolate_init(),
create_params: None, create_params: None,
unsafely_ignore_certificate_errors: shared unsafely_ignore_certificate_errors: shared
.options .options
@ -638,7 +638,7 @@ fn create_web_worker_callback(
inspect: shared.options.is_inspecting, inspect: shared.options.is_inspecting,
}, },
extensions, extensions,
startup_snapshot: Some(crate::js::deno_isolate_init()), startup_snapshot: crate::js::deno_isolate_init(),
unsafely_ignore_certificate_errors: shared unsafely_ignore_certificate_errors: shared
.options .options
.unsafely_ignore_certificate_errors .unsafely_ignore_certificate_errors
@ -688,7 +688,7 @@ mod tests {
let permissions = PermissionsContainer::new(Permissions::default()); let permissions = PermissionsContainer::new(Permissions::default());
let options = WorkerOptions { let options = WorkerOptions {
startup_snapshot: Some(crate::js::deno_isolate_init()), startup_snapshot: crate::js::deno_isolate_init(),
..Default::default() ..Default::default()
}; };

7
ext/node/build.rs Normal file
View file

@ -0,0 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::env;
fn main() {
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
}

View file

@ -132,12 +132,7 @@ pub static NODE_ENV_VAR_ALLOWLIST: Lazy<HashSet<String>> = Lazy::new(|| {
#[op] #[op]
fn op_node_build_os() -> String { fn op_node_build_os() -> String {
std::env::var("TARGET") env!("TARGET").split('-').nth(2).unwrap().to_string()
.unwrap()
.split('-')
.nth(2)
.unwrap()
.to_string()
} }
#[op(fast)] #[op(fast)]

View file

@ -23,6 +23,9 @@ snapshot_from_snapshot = []
include_js_files_for_snapshotting = [ include_js_files_for_snapshotting = [
"deno_core/include_js_files_for_snapshotting", "deno_core/include_js_files_for_snapshotting",
] ]
# A dev feature to disable creations and loading of snapshots in favor of
# loading JS sources at runtime.
__runtime_js_sources = ["dont_create_runtime_snapshot"]
[lib] [lib]
name = "deno_runtime" name = "deno_runtime"
@ -64,6 +67,7 @@ winres.workspace = true
winapi.workspace = true winapi.workspace = true
[dependencies] [dependencies]
deno_ast.workspace = true
deno_broadcast_channel.workspace = true deno_broadcast_channel.workspace = true
deno_cache.workspace = true deno_cache.workspace = true
deno_console.workspace = true deno_console.workspace = true

View file

@ -21,6 +21,7 @@ mod startup_snapshot {
use deno_http::DefaultHttpPropertyExtractor; use deno_http::DefaultHttpPropertyExtractor;
use std::path::Path; use std::path::Path;
// Duplicated in `worker.rs`. Keep in sync!
fn maybe_transpile_source( fn maybe_transpile_source(
source: &mut ExtensionFileSource, source: &mut ExtensionFileSource,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
@ -240,6 +241,7 @@ mod startup_snapshot {
} }
} }
// Duplicated in `worker.rs`. Keep in sync!
deno_core::extension!(runtime, deno_core::extension!(runtime,
deps = [ deps = [
deno_webidl, deno_webidl,

View file

@ -1,17 +1,21 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
use deno_core::Snapshot; use deno_core::Snapshot;
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
use log::debug; use log::debug;
#[cfg(not(feature = "dont_create_runtime_snapshot"))] #[cfg(not(feature = "dont_create_runtime_snapshot"))]
static RUNTIME_SNAPSHOT: &[u8] = static RUNTIME_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/RUNTIME_SNAPSHOT.bin")); include_bytes!(concat!(env!("OUT_DIR"), "/RUNTIME_SNAPSHOT.bin"));
#[cfg(not(feature = "dont_create_runtime_snapshot"))] pub fn deno_isolate_init() -> Option<Snapshot> {
pub fn deno_isolate_init() -> Snapshot {
debug!("Deno isolate init with snapshots."); debug!("Deno isolate init with snapshots.");
Snapshot::Static(RUNTIME_SNAPSHOT) #[cfg(not(feature = "dont_create_runtime_snapshot"))]
{
Some(Snapshot::Static(RUNTIME_SNAPSHOT))
}
#[cfg(feature = "dont_create_runtime_snapshot")]
{
None
}
} }
#[cfg(not(feature = "include_js_files_for_snapshotting"))] #[cfg(not(feature = "include_js_files_for_snapshotting"))]

View file

@ -34,5 +34,6 @@ pub mod web_worker;
pub mod worker; pub mod worker;
mod worker_bootstrap; mod worker_bootstrap;
pub use worker::runtime;
pub use worker_bootstrap::BootstrapOptions; pub use worker_bootstrap::BootstrapOptions;
pub use worker_bootstrap::WorkerLogLevel; pub use worker_bootstrap::WorkerLogLevel;

View file

@ -4,6 +4,7 @@ use crate::inspector_server::InspectorServer;
use crate::ops; use crate::ops;
use crate::permissions::PermissionsContainer; use crate::permissions::PermissionsContainer;
use crate::tokio_util::create_and_run_current_thread; use crate::tokio_util::create_and_run_current_thread;
use crate::worker::runtime;
use crate::worker::FormatJsErrorFn; use crate::worker::FormatJsErrorFn;
use crate::BootstrapOptions; use crate::BootstrapOptions;
use deno_broadcast_channel::InMemoryBroadcastChannel; use deno_broadcast_channel::InMemoryBroadcastChannel;
@ -397,16 +398,17 @@ impl WebWorker {
// NOTE(bartlomieju): ordering is important here, keep it in sync with // NOTE(bartlomieju): ordering is important here, keep it in sync with
// `runtime/build.rs`, `runtime/worker.rs` and `cli/build.rs`! // `runtime/build.rs`, `runtime/worker.rs` and `cli/build.rs`!
let mut extensions: Vec<Extension> = vec![
let mut extensions = vec![
// Web APIs // Web APIs
deno_webidl::deno_webidl::init_ops(), deno_webidl::deno_webidl::init_ops_and_esm(),
deno_console::deno_console::init_ops(), deno_console::deno_console::init_ops_and_esm(),
deno_url::deno_url::init_ops(), deno_url::deno_url::init_ops_and_esm(),
deno_web::deno_web::init_ops::<PermissionsContainer>( deno_web::deno_web::init_ops_and_esm::<PermissionsContainer>(
options.blob_store.clone(), options.blob_store.clone(),
Some(main_module.clone()), Some(main_module.clone()),
), ),
deno_fetch::deno_fetch::init_ops::<PermissionsContainer>( deno_fetch::deno_fetch::init_ops_and_esm::<PermissionsContainer>(
deno_fetch::Options { deno_fetch::Options {
user_agent: options.bootstrap.user_agent.clone(), user_agent: options.bootstrap.user_agent.clone(),
root_cert_store_provider: options.root_cert_store_provider.clone(), root_cert_store_provider: options.root_cert_store_provider.clone(),
@ -417,73 +419,89 @@ impl WebWorker {
..Default::default() ..Default::default()
}, },
), ),
deno_cache::deno_cache::init_ops::<SqliteBackedCache>(create_cache), deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(
deno_websocket::deno_websocket::init_ops::<PermissionsContainer>( create_cache,
),
deno_websocket::deno_websocket::init_ops_and_esm::<PermissionsContainer>(
options.bootstrap.user_agent.clone(), options.bootstrap.user_agent.clone(),
options.root_cert_store_provider.clone(), options.root_cert_store_provider.clone(),
options.unsafely_ignore_certificate_errors.clone(), options.unsafely_ignore_certificate_errors.clone(),
), ),
deno_webstorage::deno_webstorage::init_ops(None).disable(), deno_webstorage::deno_webstorage::init_ops_and_esm(None).disable(),
deno_crypto::deno_crypto::init_ops(options.seed), deno_crypto::deno_crypto::init_ops_and_esm(options.seed),
deno_broadcast_channel::deno_broadcast_channel::init_ops( deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm(
options.broadcast_channel.clone(), options.broadcast_channel.clone(),
unstable, unstable,
), ),
deno_ffi::deno_ffi::init_ops::<PermissionsContainer>(unstable), deno_ffi::deno_ffi::init_ops_and_esm::<PermissionsContainer>(unstable),
deno_net::deno_net::init_ops::<PermissionsContainer>( deno_net::deno_net::init_ops_and_esm::<PermissionsContainer>(
options.root_cert_store_provider.clone(), options.root_cert_store_provider.clone(),
unstable, unstable,
options.unsafely_ignore_certificate_errors.clone(), options.unsafely_ignore_certificate_errors.clone(),
), ),
deno_tls::deno_tls::init_ops(), deno_tls::deno_tls::init_ops_and_esm(),
deno_kv::deno_kv::init_ops( deno_kv::deno_kv::init_ops_and_esm(
SqliteDbHandler::<PermissionsContainer>::new(None), SqliteDbHandler::<PermissionsContainer>::new(None),
unstable, unstable,
), ),
deno_napi::deno_napi::init_ops::<PermissionsContainer>(), deno_napi::deno_napi::init_ops_and_esm::<PermissionsContainer>(),
deno_http::deno_http::init_ops::<DefaultHttpPropertyExtractor>(), deno_http::deno_http::init_ops_and_esm::<DefaultHttpPropertyExtractor>(),
deno_io::deno_io::init_ops(Some(options.stdio)), deno_io::deno_io::init_ops_and_esm(Some(options.stdio)),
deno_fs::deno_fs::init_ops::<PermissionsContainer>( deno_fs::deno_fs::init_ops_and_esm::<PermissionsContainer>(
unstable, unstable,
options.fs.clone(), options.fs.clone(),
), ),
deno_node::deno_node::init_ops::<PermissionsContainer>( deno_node::deno_node::init_ops_and_esm::<PermissionsContainer>(
options.npm_resolver, options.npm_resolver,
options.fs, options.fs,
), ),
// Runtime ops that are always initialized for WebWorkers // Runtime ops that are always initialized for WebWorkers
ops::web_worker::deno_web_worker::init_ops(), ops::web_worker::deno_web_worker::init_ops_and_esm(),
ops::runtime::deno_runtime::init_ops(main_module.clone()), ops::runtime::deno_runtime::init_ops_and_esm(main_module.clone()),
ops::worker_host::deno_worker_host::init_ops( ops::worker_host::deno_worker_host::init_ops_and_esm(
options.create_web_worker_cb.clone(), options.create_web_worker_cb.clone(),
options.preload_module_cb.clone(), options.preload_module_cb.clone(),
options.pre_execute_module_cb.clone(), options.pre_execute_module_cb.clone(),
options.format_js_error_fn.clone(), options.format_js_error_fn.clone(),
), ),
ops::fs_events::deno_fs_events::init_ops(), ops::fs_events::deno_fs_events::init_ops_and_esm(),
ops::os::deno_os_worker::init_ops(), ops::os::deno_os_worker::init_ops_and_esm(),
ops::permissions::deno_permissions::init_ops(), ops::permissions::deno_permissions::init_ops_and_esm(),
ops::process::deno_process::init_ops(), ops::process::deno_process::init_ops_and_esm(),
ops::signal::deno_signal::init_ops(), ops::signal::deno_signal::init_ops_and_esm(),
ops::tty::deno_tty::init_ops(), ops::tty::deno_tty::init_ops_and_esm(),
ops::http::deno_http_runtime::init_ops(), ops::http::deno_http_runtime::init_ops_and_esm(),
deno_permissions_web_worker::init_ops( deno_permissions_web_worker::init_ops_and_esm(
permissions, permissions,
unstable, unstable,
enable_testing_features, enable_testing_features,
), ),
runtime::init_ops_and_esm(),
]; ];
// Append exts for extension in &mut extensions {
#[cfg(not(feature = "__runtime_js_sources"))]
{
extension.js_files = std::borrow::Cow::Borrowed(&[]);
extension.esm_files = std::borrow::Cow::Borrowed(&[]);
extension.esm_entry_point = None;
}
#[cfg(feature = "__runtime_js_sources")]
{
use crate::worker::maybe_transpile_source;
for source in extension.esm_files.to_mut() {
maybe_transpile_source(source).unwrap();
}
for source in extension.js_files.to_mut() {
maybe_transpile_source(source).unwrap();
}
}
}
extensions.extend(std::mem::take(&mut options.extensions)); extensions.extend(std::mem::take(&mut options.extensions));
#[cfg(not(feature = "dont_create_runtime_snapshot"))] #[cfg(all(feature = "include_js_files_for_snapshotting", feature = "dont_create_runtime_snapshot", not(feature = "__runtime_js_sources")))]
let startup_snapshot = options options.startup_snapshot.as_ref().expect("Sources are not embedded, snapshotting was disabled and a user snapshot was not provided.");
.startup_snapshot
.unwrap_or_else(crate::js::deno_isolate_init);
#[cfg(feature = "dont_create_runtime_snapshot")]
let startup_snapshot = options.startup_snapshot
.expect("deno_runtime startup snapshot is not available with 'create_runtime_snapshot' Cargo feature.");
// Clear extension modules from the module map, except preserve `node:*` // Clear extension modules from the module map, except preserve `node:*`
// modules as `node:` specifiers. // modules as `node:` specifiers.
@ -492,7 +510,9 @@ impl WebWorker {
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(options.module_loader.clone()), module_loader: Some(options.module_loader.clone()),
startup_snapshot: Some(startup_snapshot), startup_snapshot: options
.startup_snapshot
.or_else(crate::js::deno_isolate_init),
source_map_getter: options.source_map_getter, source_map_getter: options.source_map_getter,
get_error_class_fn: options.get_error_class_fn, get_error_class_fn: options.get_error_class_fn,
shared_array_buffer_store: options.shared_array_buffer_store.clone(), shared_array_buffer_store: options.shared_array_buffer_store.clone(),

View file

@ -18,6 +18,8 @@ use deno_core::futures::Future;
use deno_core::v8; use deno_core::v8;
use deno_core::CompiledWasmModuleStore; use deno_core::CompiledWasmModuleStore;
use deno_core::Extension; use deno_core::Extension;
#[cfg(feature = "__runtime_js_sources")]
use deno_core::ExtensionFileSource;
use deno_core::FsModuleLoader; use deno_core::FsModuleLoader;
use deno_core::GetErrorClassFn; use deno_core::GetErrorClassFn;
use deno_core::JsRuntime; use deno_core::JsRuntime;
@ -59,6 +61,78 @@ impl ExitCode {
} }
} }
// Duplicated in `build.rs`. Keep in sync!
deno_core::extension!(
runtime,
esm_entry_point = "ext:runtime/90_deno_ns.js",
esm = [
dir "js",
"01_errors.js",
"01_version.ts",
"06_util.js",
"10_permissions.js",
"11_workers.js",
"13_buffer.js",
"30_os.js",
"40_fs_events.js",
"40_http.js",
"40_process.js",
"40_signals.js",
"40_tty.js",
"41_prompt.js",
"90_deno_ns.js",
"98_global_scope.js"
],
);
// Duplicated in `build.rs`. Keep in sync!
#[cfg(feature = "__runtime_js_sources")]
pub fn maybe_transpile_source(
source: &mut ExtensionFileSource,
) -> Result<(), AnyError> {
use deno_ast::MediaType;
use deno_ast::ParseParams;
use deno_ast::SourceTextInfo;
use deno_core::ExtensionFileSourceCode;
use std::path::Path;
// Always transpile `node:` built-in modules, since they might be TypeScript.
let media_type = if source.specifier.starts_with("node:") {
MediaType::TypeScript
} else {
MediaType::from_path(Path::new(&source.specifier))
};
match media_type {
MediaType::TypeScript => {}
MediaType::JavaScript => return Ok(()),
MediaType::Mjs => return Ok(()),
_ => panic!(
"Unsupported media type for snapshotting {media_type:?} for file {}",
source.specifier
),
}
let code = source.load()?;
let parsed = deno_ast::parse_module(ParseParams {
specifier: source.specifier.to_string(),
text_info: SourceTextInfo::from_string(code.as_str().to_owned()),
media_type,
capture_tokens: false,
scope_analysis: false,
maybe_syntax: None,
})?;
let transpiled_source = parsed.transpile(&deno_ast::EmitOptions {
imports_not_used_as_values: deno_ast::ImportsNotUsedAsValues::Remove,
inline_source_map: false,
..Default::default()
})?;
source.code =
ExtensionFileSourceCode::Computed(transpiled_source.text.into());
Ok(())
}
/// This worker is created and used by almost all /// This worker is created and used by almost all
/// subcommands in Deno executable. /// subcommands in Deno executable.
/// ///
@ -226,14 +300,14 @@ impl MainWorker {
// `runtime/build.rs`, `runtime/web_worker.rs` and `cli/build.rs`! // `runtime/build.rs`, `runtime/web_worker.rs` and `cli/build.rs`!
let mut extensions = vec![ let mut extensions = vec![
// Web APIs // Web APIs
deno_webidl::deno_webidl::init_ops(), deno_webidl::deno_webidl::init_ops_and_esm(),
deno_console::deno_console::init_ops(), deno_console::deno_console::init_ops_and_esm(),
deno_url::deno_url::init_ops(), deno_url::deno_url::init_ops_and_esm(),
deno_web::deno_web::init_ops::<PermissionsContainer>( deno_web::deno_web::init_ops_and_esm::<PermissionsContainer>(
options.blob_store.clone(), options.blob_store.clone(),
options.bootstrap.location.clone(), options.bootstrap.location.clone(),
), ),
deno_fetch::deno_fetch::init_ops::<PermissionsContainer>( deno_fetch::deno_fetch::init_ops_and_esm::<PermissionsContainer>(
deno_fetch::Options { deno_fetch::Options {
user_agent: options.bootstrap.user_agent.clone(), user_agent: options.bootstrap.user_agent.clone(),
root_cert_store_provider: options.root_cert_store_provider.clone(), root_cert_store_provider: options.root_cert_store_provider.clone(),
@ -244,75 +318,91 @@ impl MainWorker {
..Default::default() ..Default::default()
}, },
), ),
deno_cache::deno_cache::init_ops::<SqliteBackedCache>(create_cache), deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(
deno_websocket::deno_websocket::init_ops::<PermissionsContainer>( create_cache,
),
deno_websocket::deno_websocket::init_ops_and_esm::<PermissionsContainer>(
options.bootstrap.user_agent.clone(), options.bootstrap.user_agent.clone(),
options.root_cert_store_provider.clone(), options.root_cert_store_provider.clone(),
options.unsafely_ignore_certificate_errors.clone(), options.unsafely_ignore_certificate_errors.clone(),
), ),
deno_webstorage::deno_webstorage::init_ops( deno_webstorage::deno_webstorage::init_ops_and_esm(
options.origin_storage_dir.clone(), options.origin_storage_dir.clone(),
), ),
deno_crypto::deno_crypto::init_ops(options.seed), deno_crypto::deno_crypto::init_ops_and_esm(options.seed),
deno_broadcast_channel::deno_broadcast_channel::init_ops( deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm(
options.broadcast_channel.clone(), options.broadcast_channel.clone(),
unstable, unstable,
), ),
deno_ffi::deno_ffi::init_ops::<PermissionsContainer>(unstable), deno_ffi::deno_ffi::init_ops_and_esm::<PermissionsContainer>(unstable),
deno_net::deno_net::init_ops::<PermissionsContainer>( deno_net::deno_net::init_ops_and_esm::<PermissionsContainer>(
options.root_cert_store_provider.clone(), options.root_cert_store_provider.clone(),
unstable, unstable,
options.unsafely_ignore_certificate_errors.clone(), options.unsafely_ignore_certificate_errors.clone(),
), ),
deno_tls::deno_tls::init_ops(), deno_tls::deno_tls::init_ops_and_esm(),
deno_kv::deno_kv::init_ops( deno_kv::deno_kv::init_ops_and_esm(
SqliteDbHandler::<PermissionsContainer>::new( SqliteDbHandler::<PermissionsContainer>::new(
options.origin_storage_dir.clone(), options.origin_storage_dir.clone(),
), ),
unstable, unstable,
), ),
deno_napi::deno_napi::init_ops::<PermissionsContainer>(), deno_napi::deno_napi::init_ops_and_esm::<PermissionsContainer>(),
deno_http::deno_http::init_ops::<DefaultHttpPropertyExtractor>(), deno_http::deno_http::init_ops_and_esm::<DefaultHttpPropertyExtractor>(),
deno_io::deno_io::init_ops(Some(options.stdio)), deno_io::deno_io::init_ops_and_esm(Some(options.stdio)),
deno_fs::deno_fs::init_ops::<PermissionsContainer>( deno_fs::deno_fs::init_ops_and_esm::<PermissionsContainer>(
unstable, unstable,
options.fs.clone(), options.fs.clone(),
), ),
deno_node::deno_node::init_ops::<PermissionsContainer>( deno_node::deno_node::init_ops_and_esm::<PermissionsContainer>(
options.npm_resolver, options.npm_resolver,
options.fs, options.fs,
), ),
// Ops from this crate // Ops from this crate
ops::runtime::deno_runtime::init_ops(main_module.clone()), ops::runtime::deno_runtime::init_ops_and_esm(main_module.clone()),
ops::worker_host::deno_worker_host::init_ops( ops::worker_host::deno_worker_host::init_ops_and_esm(
options.create_web_worker_cb.clone(), options.create_web_worker_cb.clone(),
options.web_worker_preload_module_cb.clone(), options.web_worker_preload_module_cb.clone(),
options.web_worker_pre_execute_module_cb.clone(), options.web_worker_pre_execute_module_cb.clone(),
options.format_js_error_fn.clone(), options.format_js_error_fn.clone(),
), ),
ops::fs_events::deno_fs_events::init_ops(), ops::fs_events::deno_fs_events::init_ops_and_esm(),
ops::os::deno_os::init_ops(exit_code.clone()), ops::os::deno_os::init_ops_and_esm(exit_code.clone()),
ops::permissions::deno_permissions::init_ops(), ops::permissions::deno_permissions::init_ops_and_esm(),
ops::process::deno_process::init_ops(), ops::process::deno_process::init_ops_and_esm(),
ops::signal::deno_signal::init_ops(), ops::signal::deno_signal::init_ops_and_esm(),
ops::tty::deno_tty::init_ops(), ops::tty::deno_tty::init_ops_and_esm(),
ops::http::deno_http_runtime::init_ops(), ops::http::deno_http_runtime::init_ops_and_esm(),
deno_permissions_worker::init_ops( deno_permissions_worker::init_ops_and_esm(
permissions, permissions,
unstable, unstable,
enable_testing_features, enable_testing_features,
), ),
runtime::init_ops_and_esm(),
]; ];
for extension in &mut extensions {
#[cfg(not(feature = "__runtime_js_sources"))]
{
extension.js_files = std::borrow::Cow::Borrowed(&[]);
extension.esm_files = std::borrow::Cow::Borrowed(&[]);
extension.esm_entry_point = None;
}
#[cfg(feature = "__runtime_js_sources")]
{
for source in extension.esm_files.to_mut() {
maybe_transpile_source(source).unwrap();
}
for source in extension.js_files.to_mut() {
maybe_transpile_source(source).unwrap();
}
}
}
extensions.extend(std::mem::take(&mut options.extensions)); extensions.extend(std::mem::take(&mut options.extensions));
#[cfg(not(feature = "dont_create_runtime_snapshot"))] #[cfg(all(feature = "include_js_files_for_snapshotting", feature = "dont_create_runtime_snapshot", not(feature = "__runtime_js_sources")))]
let startup_snapshot = options options.startup_snapshot.as_ref().expect("Sources are not embedded, snapshotting was disabled and a user snapshot was not provided.");
.startup_snapshot
.unwrap_or_else(crate::js::deno_isolate_init);
#[cfg(feature = "dont_create_runtime_snapshot")]
let startup_snapshot = options.startup_snapshot
.expect("deno_runtime startup snapshot is not available with 'create_runtime_snapshot' Cargo feature.");
// Clear extension modules from the module map, except preserve `node:*` // Clear extension modules from the module map, except preserve `node:*`
// modules. // modules.
@ -321,7 +411,9 @@ impl MainWorker {
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(options.module_loader.clone()), module_loader: Some(options.module_loader.clone()),
startup_snapshot: Some(startup_snapshot), startup_snapshot: options
.startup_snapshot
.or_else(crate::js::deno_isolate_init),
create_params: options.create_params, create_params: options.create_params,
source_map_getter: options.source_map_getter, source_map_getter: options.source_map_getter,
get_error_class_fn: options.get_error_class_fn, get_error_class_fn: options.get_error_class_fn,