diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-09 10:56:19 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-09 14:56:19 +0000 |
commit | 8f207c0f3f3a43d77e0c88cfdc840b4b742b9708 (patch) | |
tree | a8699bca5ffffff03a9d72f1bed9208a349338ba /runtime/web_worker.rs | |
parent | 99da8a69e7260b72e55d7214ec96f6ac5e759f35 (diff) |
refactor: Split extension registration for runtime and snapshotting (#18095)
This commit splits "<ext_name>::init" functions into "init_ops" and
"init_ops_and_esm". That way we don't have to construct list of
ESM sources on each startup if we're running with a snapshot.
In a follow up commit "deno_core" will be changed to not have a split
between "extensions" and "extensions_with_js" - it will be embedders'
responsibility to pass appropriately configured extensions.
Prerequisite for https://github.com/denoland/deno/pull/18080
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r-- | runtime/web_worker.rs | 235 |
1 files changed, 165 insertions, 70 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 7948dca0a..f16ffd5a0 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -346,6 +346,168 @@ pub struct WebWorkerOptions { pub stdio: Stdio, } +#[cfg(feature = "dont_create_runtime_snapshot")] +fn get_extensions( + options: &mut WebWorkerOptions, + unstable: bool, + main_module: ModuleSpecifier, +) -> Vec<Extension> { + let create_cache = options.cache_storage_dir.take().map(|storage_dir| { + let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); + CreateCache(Arc::new(create_cache_fn)) + }); + + vec![ + // Web APIs + deno_webidl::init(), + deno_console::init(), + deno_url::init_ops(), + deno_web::init_ops::<PermissionsContainer>( + options.blob_store.clone(), + Some(main_module.clone()), + ), + deno_fetch::init_ops::<PermissionsContainer>(deno_fetch::Options { + user_agent: options.bootstrap.user_agent.clone(), + root_cert_store: options.root_cert_store.clone(), + unsafely_ignore_certificate_errors: options + .unsafely_ignore_certificate_errors + .clone(), + file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), + ..Default::default() + }), + deno_cache::init_ops::<SqliteBackedCache>(create_cache), + deno_websocket::init_ops::<PermissionsContainer>( + options.bootstrap.user_agent.clone(), + options.root_cert_store.clone(), + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_webstorage::init_ops(None).disable(), + deno_broadcast_channel::init_ops( + options.broadcast_channel.clone(), + unstable, + ), + deno_crypto::init_ops(options.seed), + deno_webgpu::init_ops(unstable), + // ffi + deno_ffi::init_ops::<PermissionsContainer>(unstable), + // Runtime ops that are always initialized for WebWorkers + ops::web_worker::init(), + ops::runtime::init(main_module), + ops::worker_host::init( + options.create_web_worker_cb.clone(), + options.preload_module_cb.clone(), + options.pre_execute_module_cb.clone(), + options.format_js_error_fn.clone(), + ), + // Extensions providing Deno.* features + ops::fs_events::init(), + deno_fs::init_ops::<PermissionsContainer>(unstable), + deno_io::init_ops(std::mem::take(&mut options.stdio)), + deno_tls::init(), + deno_net::init_ops::<PermissionsContainer>( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_napi::init::<PermissionsContainer>(), + // TODO(bartlomieju): thes two should be conditional on `dont_create_runtime_snapshot` + // cargo feature and should use `init_polyfill_ops` or `init_polyfill_ops_and_esm` + // if the feature is enabled + deno_node::init_polyfill_ops(), + deno_node::init_ops::<PermissionsContainer>(options.npm_resolver.take()), + ops::os::init_for_worker(), + ops::permissions::init(), + ops::process::init_ops(), + ops::signal::init(), + ops::tty::init(), + deno_http::init_ops(), + deno_flash::init_ops::<PermissionsContainer>(unstable), + ops::http::init(), + ] +} + +#[cfg(not(feature = "dont_create_runtime_snapshot"))] +fn get_extensions( + options: &mut WebWorkerOptions, + unstable: bool, + main_module: ModuleSpecifier, +) -> Vec<Extension> { + let create_cache = options.cache_storage_dir.take().map(|storage_dir| { + let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); + CreateCache(Arc::new(create_cache_fn)) + }); + + vec![ + // Web APIs + deno_webidl::init_esm(), + deno_console::init_esm(), + deno_url::init_ops_and_esm(), + deno_web::init_ops_and_esm::<PermissionsContainer>( + options.blob_store.clone(), + Some(main_module.clone()), + ), + deno_fetch::init_ops_and_esm::<PermissionsContainer>(deno_fetch::Options { + user_agent: options.bootstrap.user_agent.clone(), + root_cert_store: options.root_cert_store.clone(), + unsafely_ignore_certificate_errors: options + .unsafely_ignore_certificate_errors + .clone(), + file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), + ..Default::default() + }), + deno_cache::init_ops_and_esm::<SqliteBackedCache>(create_cache), + deno_websocket::init_ops_and_esm::<PermissionsContainer>( + options.bootstrap.user_agent.clone(), + options.root_cert_store.clone(), + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_webstorage::init_ops_and_esm(None).disable(), + deno_broadcast_channel::init_ops_and_esm( + options.broadcast_channel.clone(), + unstable, + ), + deno_crypto::init_ops_and_esm(options.seed), + deno_webgpu::init_ops_and_esm(unstable), + // ffi + deno_ffi::init_ops_and_esm::<PermissionsContainer>(unstable), + // Runtime ops that are always initialized for WebWorkers + ops::web_worker::init(), + ops::runtime::init(main_module), + ops::worker_host::init( + options.create_web_worker_cb.clone(), + options.preload_module_cb.clone(), + options.pre_execute_module_cb.clone(), + options.format_js_error_fn.clone(), + ), + // Extensions providing Deno.* features + ops::fs_events::init(), + deno_fs::init_ops_and_esm::<PermissionsContainer>(unstable), + deno_io::init_ops_and_esm(std::mem::take(&mut options.stdio)), + deno_tls::init(), + deno_net::init_ops_and_esm::<PermissionsContainer>( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_napi::init::<PermissionsContainer>(), + // TODO(bartlomieju): thes two should be conditional on `dont_create_runtime_snapshot` + // cargo feature and should use `init_polyfill_ops` or `init_polyfill_ops_and_esm` + // if the feature is enabled + deno_node::init_polyfill_ops_and_esm(), + deno_node::init_ops_and_esm::<PermissionsContainer>( + options.npm_resolver.take(), + ), + ops::os::init_for_worker(), + ops::permissions::init(), + ops::process::init_ops(), + ops::signal::init(), + ops::tty::init(), + deno_http::init_ops_and_esm(), + deno_flash::init_ops_and_esm::<PermissionsContainer>(unstable), + ops::http::init(), + ] +} + impl WebWorker { pub fn bootstrap_from_options( name: String, @@ -378,77 +540,10 @@ impl WebWorker { state.put(ops::TestingFeaturesEnabled(enable_testing_features)); }) .build(); - let create_cache = options.cache_storage_dir.map(|storage_dir| { - let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone()); - CreateCache(Arc::new(create_cache_fn)) - }); - let mut extensions: Vec<Extension> = vec![ - // Web APIs - deno_webidl::init(), - deno_console::init(), - deno_url::init(), - deno_web::init::<PermissionsContainer>( - options.blob_store.clone(), - Some(main_module.clone()), - ), - deno_fetch::init::<PermissionsContainer>(deno_fetch::Options { - user_agent: options.bootstrap.user_agent.clone(), - root_cert_store: options.root_cert_store.clone(), - unsafely_ignore_certificate_errors: options - .unsafely_ignore_certificate_errors - .clone(), - file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler), - ..Default::default() - }), - deno_cache::init::<SqliteBackedCache>(create_cache), - deno_websocket::init::<PermissionsContainer>( - options.bootstrap.user_agent.clone(), - options.root_cert_store.clone(), - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_webstorage::init(None).disable(), - deno_broadcast_channel::init(options.broadcast_channel.clone(), unstable), - deno_crypto::init(options.seed), - deno_webgpu::init(unstable), - // ffi - deno_ffi::init::<PermissionsContainer>(unstable), - // Runtime ops that are always initialized for WebWorkers - ops::web_worker::init(), - ops::runtime::init(main_module.clone()), - ops::worker_host::init( - options.create_web_worker_cb.clone(), - options.preload_module_cb.clone(), - options.pre_execute_module_cb.clone(), - options.format_js_error_fn.clone(), - ), - // Extensions providing Deno.* features - ops::fs_events::init(), - deno_fs::init::<PermissionsContainer>(unstable), - deno_io::init(options.stdio), - deno_tls::init(), - deno_net::init::<PermissionsContainer>( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init::<PermissionsContainer>(), - // TODO(bartlomieju): this should be conditional on `dont_create_runtime_snapshot` - // cargo feature and should use `init_polyfill_ops` or `init_polyfill_ops_and_esm` - // if the feature is enabled - deno_node::init_polyfill_ops(), - deno_node::init::<PermissionsContainer>(options.npm_resolver), - ops::os::init_for_worker(), - ops::permissions::init(), - ops::process::init_ops(), - ops::signal::init(), - ops::tty::init(), - deno_http::init(), - deno_flash::init::<PermissionsContainer>(unstable), - ops::http::init(), - // Permissions ext (worker specific state) - perm_ext, - ]; + let mut extensions = + get_extensions(&mut options, unstable, main_module.clone()); + extensions.push(perm_ext); // Append exts extensions.extend(std::mem::take(&mut options.extensions)); |