diff --git a/cli/build.rs b/cli/build.rs index 956293b78d..6687b1e443 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -322,36 +322,40 @@ mod ts { fn create_cli_snapshot(snapshot_path: PathBuf) { let extensions: Vec = vec![ - deno_webidl::init(), - deno_console::init(), - deno_url::init(), + deno_webidl::init_esm(), + deno_console::init_esm(), + deno_url::init_ops_and_esm(), deno_tls::init(), - deno_web::init::( + deno_web::init_ops_and_esm::( deno_web::BlobStore::default(), Default::default(), ), - deno_fetch::init::(Default::default()), - deno_cache::init::(None), - deno_websocket::init::("".to_owned(), None, None), - deno_webstorage::init(None), - deno_crypto::init(None), - deno_webgpu::init(false), - deno_broadcast_channel::init( + deno_fetch::init_ops_and_esm::(Default::default()), + deno_cache::init_ops_and_esm::(None), + deno_websocket::init_ops_and_esm::( + "".to_owned(), + None, + None, + ), + deno_webstorage::init_ops_and_esm(None), + deno_crypto::init_ops_and_esm(None), + deno_webgpu::init_ops_and_esm(false), + deno_broadcast_channel::init_ops_and_esm( deno_broadcast_channel::InMemoryBroadcastChannel::default(), false, // No --unstable. ), - deno_io::init(Default::default()), - deno_fs::init::(false), - deno_node::init::(None), // No --unstable. + deno_io::init_ops_and_esm(Default::default()), + deno_fs::init_ops_and_esm::(false), + deno_node::init_ops_and_esm::(None), // No --unstable. deno_node::init_polyfill_ops_and_esm(), - deno_ffi::init::(false), - deno_net::init::( + deno_ffi::init_ops_and_esm::(false), + deno_net::init_ops_and_esm::( None, false, // No --unstable. None, ), deno_napi::init::(), - deno_http::init(), - deno_flash::init::(false), // No --unstable + deno_http::init_ops_and_esm(), + deno_flash::init_ops_and_esm::(false), // No --unstable ]; let mut esm_files = include_js_files!( diff --git a/ext/broadcast_channel/lib.rs b/ext/broadcast_channel/lib.rs index 3698cc8855..14884e99c0 100644 --- a/ext/broadcast_channel/lib.rs +++ b/ext/broadcast_channel/lib.rs @@ -2,6 +2,7 @@ mod in_memory_broadcast_channel; +use deno_core::ExtensionBuilder; pub use in_memory_broadcast_channel::InMemoryBroadcastChannel; pub use in_memory_broadcast_channel::InMemoryBroadcastChannelResource; @@ -106,26 +107,45 @@ where bc.recv(&resource).await } -pub fn init( - bc: BC, - unstable: bool, -) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_web"], ) - .esm(include_js_files!("01_broadcast_channel.js",)) - .ops(vec![ - op_broadcast_subscribe::decl::(), - op_broadcast_unsubscribe::decl::(), - op_broadcast_send::decl::(), - op_broadcast_recv::decl::(), - ]) - .state(move |state| { - state.put(bc.clone()); - state.put(Unstable(unstable)); - }) - .build() +} + +fn ops( + ext: &mut ExtensionBuilder, + bc: BC, + unstable: bool, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_broadcast_subscribe::decl::(), + op_broadcast_unsubscribe::decl::(), + op_broadcast_send::decl::(), + op_broadcast_recv::decl::(), + ]) + .state(move |state| { + state.put(bc.clone()); + state.put(Unstable(unstable)); + }) +} + +pub fn init_ops_and_esm( + bc: BC, + unstable: bool, +) -> Extension { + ops::(&mut ext(), bc, unstable) + .esm(include_js_files!("01_broadcast_channel.js",)) + .build() +} + +pub fn init_ops( + bc: BC, + unstable: bool, +) -> Extension { + ops::(&mut ext(), bc, unstable).build() } pub fn get_declaration() -> PathBuf { diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs index 477bbcb3e1..a89296df92 100644 --- a/ext/cache/lib.rs +++ b/ext/cache/lib.rs @@ -13,6 +13,7 @@ use deno_core::serde::Deserialize; use deno_core::serde::Serialize; use deno_core::ByteString; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::Resource; use deno_core::ResourceId; @@ -22,28 +23,45 @@ pub use sqlite::SqliteBackedCache; #[derive(Clone)] pub struct CreateCache(pub Arc C>); -pub fn init( - maybe_create_cache: Option>, -) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_web", "deno_url", "deno_fetch"], ) - .esm(include_js_files!("01_cache.js",)) - .ops(vec![ - op_cache_storage_open::decl::(), - op_cache_storage_has::decl::(), - op_cache_storage_delete::decl::(), - op_cache_put::decl::(), - op_cache_match::decl::(), - op_cache_delete::decl::(), - ]) - .state(move |state| { - if let Some(create_cache) = maybe_create_cache.clone() { - state.put(create_cache); - } - }) - .build() +} + +fn ops( + ext: &mut ExtensionBuilder, + maybe_create_cache: Option>, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_cache_storage_open::decl::(), + op_cache_storage_has::decl::(), + op_cache_storage_delete::decl::(), + op_cache_put::decl::(), + op_cache_match::decl::(), + op_cache_delete::decl::(), + ]) + .state(move |state| { + if let Some(create_cache) = maybe_create_cache.clone() { + state.put(create_cache); + } + }) +} + +pub fn init_ops_and_esm( + maybe_create_cache: Option>, +) -> Extension { + ops::(&mut ext(), maybe_create_cache) + .esm(include_js_files!("01_cache.js",)) + .build() +} + +pub fn init_ops( + maybe_create_cache: Option>, +) -> Extension { + ops::(&mut ext(), maybe_create_cache).build() } pub fn get_declaration() -> PathBuf { diff --git a/ext/console/lib.rs b/ext/console/lib.rs index 158a1a05ee..a9a30c3289 100644 --- a/ext/console/lib.rs +++ b/ext/console/lib.rs @@ -2,10 +2,18 @@ use deno_core::include_js_files; use deno_core::Extension; +use deno_core::ExtensionBuilder; use std::path::PathBuf; -pub fn init() -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder(env!("CARGO_PKG_NAME")) +} +pub fn init() -> Extension { + ext().build() +} + +pub fn init_esm() -> Extension { + ext() .esm(include_js_files!("01_colors.js", "02_console.js",)) .build() } diff --git a/ext/crypto/lib.rs b/ext/crypto/lib.rs index f406e64ec1..89ca8e0d64 100644 --- a/ext/crypto/lib.rs +++ b/ext/crypto/lib.rs @@ -10,6 +10,7 @@ use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::include_js_files; use deno_core::op; +use deno_core::ExtensionBuilder; use deno_core::Extension; use deno_core::OpState; @@ -72,49 +73,64 @@ use crate::key::CryptoNamedCurve; use crate::key::HkdfOutput; use crate::shared::RawKeyData; -pub fn init(maybe_seed: Option) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_web"], ) - .esm(include_js_files!("00_crypto.js", "01_webidl.js",)) - .ops(vec![ - op_crypto_get_random_values::decl(), - op_crypto_generate_key::decl(), - op_crypto_sign_key::decl(), - op_crypto_verify_key::decl(), - op_crypto_derive_bits::decl(), - op_crypto_import_key::decl(), - op_crypto_export_key::decl(), - op_crypto_encrypt::decl(), - op_crypto_decrypt::decl(), - op_crypto_subtle_digest::decl(), - op_crypto_random_uuid::decl(), - op_crypto_wrap_key::decl(), - op_crypto_unwrap_key::decl(), - op_crypto_base64url_decode::decl(), - op_crypto_base64url_encode::decl(), - x25519::op_generate_x25519_keypair::decl(), - x25519::op_derive_bits_x25519::decl(), - x25519::op_import_spki_x25519::decl(), - x25519::op_import_pkcs8_x25519::decl(), - ed25519::op_generate_ed25519_keypair::decl(), - ed25519::op_import_spki_ed25519::decl(), - ed25519::op_import_pkcs8_ed25519::decl(), - ed25519::op_sign_ed25519::decl(), - ed25519::op_verify_ed25519::decl(), - ed25519::op_export_spki_ed25519::decl(), - ed25519::op_export_pkcs8_ed25519::decl(), - ed25519::op_jwk_x_ed25519::decl(), - x25519::op_export_spki_x25519::decl(), - x25519::op_export_pkcs8_x25519::decl(), - ]) - .state(move |state| { - if let Some(seed) = maybe_seed { - state.put(StdRng::seed_from_u64(seed)); - } - }) - .build() +} + +fn ops( + ext: &mut ExtensionBuilder, + maybe_seed: Option, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_crypto_get_random_values::decl(), + op_crypto_generate_key::decl(), + op_crypto_sign_key::decl(), + op_crypto_verify_key::decl(), + op_crypto_derive_bits::decl(), + op_crypto_import_key::decl(), + op_crypto_export_key::decl(), + op_crypto_encrypt::decl(), + op_crypto_decrypt::decl(), + op_crypto_subtle_digest::decl(), + op_crypto_random_uuid::decl(), + op_crypto_wrap_key::decl(), + op_crypto_unwrap_key::decl(), + op_crypto_base64url_decode::decl(), + op_crypto_base64url_encode::decl(), + x25519::op_generate_x25519_keypair::decl(), + x25519::op_derive_bits_x25519::decl(), + x25519::op_import_spki_x25519::decl(), + x25519::op_import_pkcs8_x25519::decl(), + ed25519::op_generate_ed25519_keypair::decl(), + ed25519::op_import_spki_ed25519::decl(), + ed25519::op_import_pkcs8_ed25519::decl(), + ed25519::op_sign_ed25519::decl(), + ed25519::op_verify_ed25519::decl(), + ed25519::op_export_spki_ed25519::decl(), + ed25519::op_export_pkcs8_ed25519::decl(), + ed25519::op_jwk_x_ed25519::decl(), + x25519::op_export_spki_x25519::decl(), + x25519::op_export_pkcs8_x25519::decl(), + ]) + .state(move |state| { + if let Some(seed) = maybe_seed { + state.put(StdRng::seed_from_u64(seed)); + } + }) +} + +pub fn init_ops_and_esm(maybe_seed: Option) -> Extension { + ops(&mut ext(), maybe_seed) + .esm(include_js_files!("00_crypto.js", "01_webidl.js",)) + .build() +} + +pub fn init_ops(maybe_seed: Option) -> Extension { + ops(&mut ext(), maybe_seed).build() } #[op] diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 8576b3c530..647e0ec7fe 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -13,6 +13,7 @@ use deno_core::futures::StreamExt; use deno_core::include_js_files; use deno_core::op; use deno_core::BufView; +use deno_core::ExtensionBuilder; use deno_core::WriteOutcome; use deno_core::url::Url; @@ -91,43 +92,74 @@ impl Default for Options { } } -pub fn init(options: Options) -> Extension -where - FP: FetchPermissions + 'static, -{ +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_web", "deno_url", "deno_console"], ) - .esm(include_js_files!( - "20_headers.js", - "21_formdata.js", - "22_body.js", - "22_http_client.js", - "23_request.js", - "23_response.js", - "26_fetch.js", - )) - .ops(vec![ - op_fetch::decl::(), - op_fetch_send::decl(), - op_fetch_custom_client::decl::(), - ]) - .state(move |state| { - state.put::(options.clone()); - state.put::({ - create_http_client( - options.user_agent.clone(), - options.root_cert_store.clone(), - vec![], - options.proxy.clone(), - options.unsafely_ignore_certificate_errors.clone(), - options.client_cert_chain_and_key.clone(), - ) - .unwrap() - }); - }) - .build() +} + +fn ops( + ext: &mut ExtensionBuilder, + options: Options, +) -> &mut ExtensionBuilder +where + FP: FetchPermissions + 'static, +{ + ext + .ops(vec![ + op_fetch::decl::(), + op_fetch_send::decl(), + op_fetch_custom_client::decl::(), + ]) + .state(move |state| { + state.put::(options.clone()); + state.put::({ + create_http_client( + options.user_agent.clone(), + options.root_cert_store.clone(), + vec![], + options.proxy.clone(), + options.unsafely_ignore_certificate_errors.clone(), + options.client_cert_chain_and_key.clone(), + ) + .unwrap() + }); + }) +} + +pub fn init_ops_and_esm(options: Options) -> Extension +where + FP: FetchPermissions + 'static, +{ + ops::(&mut ext(), options) + .esm(include_js_files!( + "20_headers.js", + "21_formdata.js", + "22_body.js", + "22_http_client.js", + "23_request.js", + "23_response.js", + "26_fetch.js", + )) + .build() +} + +pub fn init_ops(options: Options) -> Extension +where + FP: FetchPermissions + 'static, +{ + ops::(&mut ext(), options) + .esm(include_js_files!( + "20_headers.js", + "21_formdata.js", + "22_body.js", + "22_http_client.js", + "23_request.js", + "23_response.js", + "26_fetch.js", + )) + .build() } pub type CancelableResponseFuture = diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index 8a1227ed99..f93e2e1213 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -5,6 +5,7 @@ use deno_core::futures::channel::mpsc; use deno_core::include_js_files; use deno_core::v8; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use std::cell::RefCell; @@ -81,9 +82,15 @@ pub(crate) struct FfiState { pub(crate) async_work_receiver: mpsc::UnboundedReceiver, } -pub fn init(unstable: bool) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_web"]) - .esm(include_js_files!("00_ffi.js",)) +} + +fn ops( + ext: &mut ExtensionBuilder, + unstable: bool, +) -> &mut ExtensionBuilder { + ext .ops(vec![ op_ffi_load::decl::

(), op_ffi_get_static::decl(), @@ -151,5 +158,18 @@ pub fn init(unstable: bool) -> Extension { async_work_sender, }); }) +} + +pub fn init_ops_and_esm( + unstable: bool, +) -> Extension { + ops::

(&mut ext(), unstable) + .esm(include_js_files!("00_ffi.js",)) + .build() +} + +pub fn init_ops(unstable: bool) -> Extension { + ops::

(&mut ext(), unstable) + .esm(include_js_files!("00_ffi.js",)) .build() } diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index 605dc3e433..41287d003d 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -17,6 +17,7 @@ use deno_core::ByteString; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::StringOrBuffer; use deno_core::ZeroCopyBuf; @@ -1526,7 +1527,7 @@ pub trait FlashPermissions { ) -> Result<(), AnyError>; } -pub fn init(unstable: bool) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &[ @@ -1537,38 +1538,55 @@ pub fn init(unstable: bool) -> Extension { "deno_http", ], ) - .esm(deno_core::include_js_files!("01_http.js",)) - .ops(vec![ - op_flash_serve::decl::

(), - op_node_unstable_flash_serve::decl::

(), - op_flash_respond::decl(), - op_flash_respond_async::decl(), - op_flash_respond_chunked::decl(), - op_flash_method::decl(), - op_flash_path::decl(), - op_flash_headers::decl(), - op_flash_addr::decl(), - op_flash_next::decl(), - op_flash_next_server::decl(), - op_flash_next_async::decl(), - op_flash_read_body::decl(), - op_flash_upgrade_websocket::decl(), - op_flash_drive_server::decl(), - op_flash_wait_for_listening::decl(), - op_flash_first_packet::decl(), - op_flash_has_body_stream::decl(), - op_flash_close_server::decl(), - op_flash_make_request::decl(), - op_flash_write_resource::decl(), - op_try_flash_respond_chunked::decl(), - ]) - .state(move |op_state| { - op_state.put(Unstable(unstable)); - op_state.put(FlashContext { - next_server_id: 0, - join_handles: HashMap::default(), - servers: HashMap::default(), - }); - }) - .build() +} + +fn ops( + ext: &mut ExtensionBuilder, + unstable: bool, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_flash_serve::decl::

(), + op_node_unstable_flash_serve::decl::

(), + op_flash_respond::decl(), + op_flash_respond_async::decl(), + op_flash_respond_chunked::decl(), + op_flash_method::decl(), + op_flash_path::decl(), + op_flash_headers::decl(), + op_flash_addr::decl(), + op_flash_next::decl(), + op_flash_next_server::decl(), + op_flash_next_async::decl(), + op_flash_read_body::decl(), + op_flash_upgrade_websocket::decl(), + op_flash_drive_server::decl(), + op_flash_wait_for_listening::decl(), + op_flash_first_packet::decl(), + op_flash_has_body_stream::decl(), + op_flash_close_server::decl(), + op_flash_make_request::decl(), + op_flash_write_resource::decl(), + op_try_flash_respond_chunked::decl(), + ]) + .state(move |op_state| { + op_state.put(Unstable(unstable)); + op_state.put(FlashContext { + next_server_id: 0, + join_handles: HashMap::default(), + servers: HashMap::default(), + }); + }) +} + +pub fn init_ops_and_esm( + unstable: bool, +) -> Extension { + ops::

(&mut ext(), unstable) + .esm(deno_core::include_js_files!("01_http.js",)) + .build() +} + +pub fn init_ops(unstable: bool) -> Extension { + ops::

(&mut ext(), unstable).build() } diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 4b177a2bfb..31782d38d2 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -9,6 +9,7 @@ use deno_core::op; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::ResourceId; use deno_core::ZeroCopyBuf; @@ -117,9 +118,15 @@ use deno_core::error::generic_error; #[cfg(not(unix))] use deno_core::error::not_supported; -pub fn init(unstable: bool) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder("deno_fs") - .esm(include_js_files!("30_fs.js",)) +} + +fn ops( + ext: &mut ExtensionBuilder, + unstable: bool, +) -> &mut ExtensionBuilder { + ext .state(move |state| { state.put(UnstableChecker { unstable }); }) @@ -184,6 +191,19 @@ pub fn init(unstable: bool) -> Extension { op_readfile_async::decl::

(), op_readfile_text_async::decl::

(), ]) +} + +pub fn init_ops_and_esm( + unstable: bool, +) -> Extension { + ops::

(&mut ext(), unstable) + .esm(include_js_files!("30_fs.js",)) + .build() +} + +pub fn init_ops(unstable: bool) -> Extension { + ops::

(&mut ext(), unstable) + .esm(include_js_files!("30_fs.js",)) .build() } diff --git a/ext/http/lib.rs b/ext/http/lib.rs index 14a93ede92..8fd7015aae 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -30,6 +30,7 @@ use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -77,13 +78,15 @@ use crate::reader_stream::ShutdownHandle; pub mod compressible; mod reader_stream; -pub fn init() -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_web", "deno_net", "deno_fetch", "deno_websocket"], ) - .esm(include_js_files!("01_http.js",)) - .ops(vec![ +} + +fn ops(ext: &mut ExtensionBuilder) -> &mut ExtensionBuilder { + ext.ops(vec![ op_http_accept::decl(), op_http_write_headers::decl(), op_http_headers::decl(), @@ -93,7 +96,16 @@ pub fn init() -> Extension { op_http_websocket_accept_header::decl(), op_http_upgrade_websocket::decl(), ]) - .build() +} + +pub fn init_ops_and_esm() -> Extension { + ops(&mut ext()) + .esm(include_js_files!("01_http.js",)) + .build() +} + +pub fn init_ops() -> Extension { + ops(&mut ext()).build() } pub enum HttpSocketAddr { diff --git a/ext/io/lib.rs b/ext/io/lib.rs index 8eebc93aef..92f681f1ec 100644 --- a/ext/io/lib.rs +++ b/ext/io/lib.rs @@ -13,6 +13,7 @@ use deno_core::BufView; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -78,13 +79,16 @@ pub static STDERR_HANDLE: Lazy = Lazy::new(|| { unsafe { StdFile::from_raw_handle(GetStdHandle(winbase::STD_ERROR_HANDLE)) } }); -pub fn init(stdio: Stdio) -> Extension { - // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics - let stdio = Rc::new(RefCell::new(Some(stdio))); - +fn ext() -> ExtensionBuilder { Extension::builder_with_deps("deno_io", &["deno_web"]) +} + +fn ops( + ext: &mut ExtensionBuilder, + stdio: Rc>>, +) -> &mut ExtensionBuilder { + ext .ops(vec![op_read_sync::decl(), op_write_sync::decl()]) - .esm(include_js_files!("12_io.js",)) .middleware(|op| match op.name { "op_print" => op_print::decl(), _ => op, @@ -132,9 +136,24 @@ pub fn init(stdio: Stdio) -> Extension { )); assert_eq!(rid, 2, "stderr must have ResourceId 2"); }) +} + +pub fn init_ops_and_esm(stdio: Stdio) -> Extension { + // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics + let stdio = Rc::new(RefCell::new(Some(stdio))); + + ops(&mut ext(), stdio) + .esm(include_js_files!("12_io.js",)) .build() } +pub fn init_ops(stdio: Stdio) -> Extension { + // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics + let stdio = Rc::new(RefCell::new(Some(stdio))); + + ops(&mut ext(), stdio).build() +} + pub enum StdioPipe { Inherit, File(StdFile), diff --git a/ext/net/lib.rs b/ext/net/lib.rs index 780cbceb5a..4703b05cc1 100644 --- a/ext/net/lib.rs +++ b/ext/net/lib.rs @@ -10,6 +10,7 @@ pub mod resolve_addr; use deno_core::error::AnyError; use deno_core::include_js_files; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_tls::rustls::RootCertStore; use std::cell::RefCell; @@ -77,24 +78,55 @@ pub struct DefaultTlsOptions { /// would override previously used alias. pub struct UnsafelyIgnoreCertificateErrors(pub Option>); -pub fn init( +fn ext() -> ExtensionBuilder { + Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_web"]) +} + +fn ops( + ext: &mut ExtensionBuilder, + root_cert_store: Option, + unstable: bool, + unsafely_ignore_certificate_errors: Option>, +) -> &mut ExtensionBuilder { + let mut ops = ops::init::

(); + ops.extend(ops_tls::init::

()); + + ext.ops(ops).state(move |state| { + state.put(DefaultTlsOptions { + root_cert_store: root_cert_store.clone(), + }); + state.put(UnstableChecker { unstable }); + state.put(UnsafelyIgnoreCertificateErrors( + unsafely_ignore_certificate_errors.clone(), + )); + }) +} + +pub fn init_ops_and_esm( root_cert_store: Option, unstable: bool, unsafely_ignore_certificate_errors: Option>, ) -> Extension { - let mut ops = ops::init::

(); - ops.extend(ops_tls::init::

()); - Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_web"]) - .esm(include_js_files!("01_net.js", "02_tls.js",)) - .ops(ops) - .state(move |state| { - state.put(DefaultTlsOptions { - root_cert_store: root_cert_store.clone(), - }); - state.put(UnstableChecker { unstable }); - state.put(UnsafelyIgnoreCertificateErrors( - unsafely_ignore_certificate_errors.clone(), - )); - }) - .build() + ops::

( + &mut ext(), + root_cert_store, + unstable, + unsafely_ignore_certificate_errors, + ) + .esm(include_js_files!("01_net.js", "02_tls.js",)) + .build() +} + +pub fn init_ops( + root_cert_store: Option, + unstable: bool, + unsafely_ignore_certificate_errors: Option>, +) -> Extension { + ops::

( + &mut ext(), + root_cert_store, + unstable, + unsafely_ignore_certificate_errors, + ) + .build() } diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 899a1d30c2..1c9d9e0aac 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -5,6 +5,7 @@ use deno_core::include_js_files; use deno_core::located_script_name; use deno_core::op; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::JsRuntime; use once_cell::sync::Lazy; use std::collections::HashSet; @@ -95,16 +96,34 @@ fn op_node_build_os() -> String { .to_string() } +fn ext_polyfill() -> ExtensionBuilder { + Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_io", "deno_fs"]) +} + +fn ops_polyfill(ext: &mut ExtensionBuilder) -> &mut ExtensionBuilder { + ext.ops(vec![ + crypto::op_node_create_hash::decl(), + crypto::op_node_hash_update::decl(), + crypto::op_node_hash_update_str::decl(), + crypto::op_node_hash_digest::decl(), + crypto::op_node_hash_digest_hex::decl(), + crypto::op_node_hash_clone::decl(), + crypto::op_node_private_encrypt::decl(), + crypto::op_node_private_decrypt::decl(), + crypto::op_node_public_encrypt::decl(), + winerror::op_node_sys_to_uv_error::decl(), + v8::op_v8_cached_data_version_tag::decl(), + v8::op_v8_get_heap_statistics::decl(), + idna::op_node_idna_domain_to_ascii::decl(), + idna::op_node_idna_domain_to_unicode::decl(), + idna::op_node_idna_punycode_decode::decl(), + idna::op_node_idna_punycode_encode::decl(), + op_node_build_os::decl(), + ]) +} + pub fn init_polyfill_ops() -> Extension { - Extension::builder(env!("CARGO_PKG_NAME")) - .ops(vec![ - crypto::op_node_create_hash::decl(), - crypto::op_node_hash_update::decl(), - crypto::op_node_hash_digest::decl(), - crypto::op_node_hash_clone::decl(), - op_node_build_os::decl(), - ]) - .build() + ops_polyfill(&mut ext_polyfill()).build() } pub fn init_polyfill_ops_and_esm() -> Extension { @@ -332,40 +351,21 @@ pub fn init_polyfill_ops_and_esm() -> Extension { "zlib.ts", ); - Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_io", "deno_fs"]) + ops_polyfill(&mut ext_polyfill()) .esm(esm_files) .esm_entry_point("ext:deno_node/module_all.ts") - .ops(vec![ - crypto::op_node_create_hash::decl(), - crypto::op_node_hash_update::decl(), - crypto::op_node_hash_update_str::decl(), - crypto::op_node_hash_digest::decl(), - crypto::op_node_hash_digest_hex::decl(), - crypto::op_node_hash_clone::decl(), - crypto::op_node_private_encrypt::decl(), - crypto::op_node_private_decrypt::decl(), - crypto::op_node_public_encrypt::decl(), - winerror::op_node_sys_to_uv_error::decl(), - v8::op_v8_cached_data_version_tag::decl(), - v8::op_v8_get_heap_statistics::decl(), - idna::op_node_idna_domain_to_ascii::decl(), - idna::op_node_idna_domain_to_unicode::decl(), - idna::op_node_idna_punycode_decode::decl(), - idna::op_node_idna_punycode_encode::decl(), - op_node_build_os::decl(), - ]) .build() } -pub fn init( - maybe_npm_resolver: Option>, -) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder("deno_node_loading") - .esm(include_js_files!( - "01_node.js", - "02_require.js", - "module_es_shim.js", - )) +} + +fn ops( + ext: &mut ExtensionBuilder, + maybe_npm_resolver: Option>, +) -> &mut ExtensionBuilder { + ext .ops(vec![ ops::op_require_init_paths::decl(), ops::op_require_node_module_paths::decl::

(), @@ -395,9 +395,26 @@ pub fn init( state.put(npm_resolver); } }) +} + +pub fn init_ops_and_esm( + maybe_npm_resolver: Option>, +) -> Extension { + ops::

(&mut ext(), maybe_npm_resolver) + .esm(include_js_files!( + "01_node.js", + "02_require.js", + "module_es_shim.js", + )) .build() } +pub fn init_ops( + maybe_npm_resolver: Option>, +) -> Extension { + ops::

(&mut ext(), maybe_npm_resolver).build() +} + pub async fn initialize_runtime( js_runtime: &mut JsRuntime, uses_local_node_modules_dir: bool, diff --git a/ext/url/benches/url_ops.rs b/ext/url/benches/url_ops.rs index 1be1fd593d..7dc2651b2e 100644 --- a/ext/url/benches/url_ops.rs +++ b/ext/url/benches/url_ops.rs @@ -11,8 +11,8 @@ use deno_core::ExtensionFileSourceCode; fn setup() -> Vec { vec![ - deno_webidl::init(), - deno_url::init(), + deno_webidl::init_esm(), + deno_url::init_ops_and_esm(), Extension::builder("bench_setup") .esm(vec![ExtensionFileSource { specifier: "ext:setup".to_string(), diff --git a/ext/url/lib.rs b/ext/url/lib.rs index f1295a13fb..8a20c9bc6a 100644 --- a/ext/url/lib.rs +++ b/ext/url/lib.rs @@ -10,6 +10,7 @@ use deno_core::url::form_urlencoded; use deno_core::url::quirks; use deno_core::url::Url; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::ZeroCopyBuf; use std::path::PathBuf; @@ -17,22 +18,33 @@ use std::path::PathBuf; use crate::urlpattern::op_urlpattern_parse; use crate::urlpattern::op_urlpattern_process_match_input; -pub fn init() -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_webidl"]) +} + +fn ops(ext: &mut ExtensionBuilder) -> &mut ExtensionBuilder { + ext.ops(vec![ + op_url_reparse::decl(), + op_url_parse::decl(), + op_url_get_serialization::decl(), + op_url_parse_with_base::decl(), + op_url_parse_search_params::decl(), + op_url_stringify_search_params::decl(), + op_urlpattern_parse::decl(), + op_urlpattern_process_match_input::decl(), + ]) +} + +pub fn init_ops_and_esm() -> Extension { + ops(&mut ext()) .esm(include_js_files!("00_url.js", "01_urlpattern.js",)) - .ops(vec![ - op_url_reparse::decl(), - op_url_parse::decl(), - op_url_get_serialization::decl(), - op_url_parse_with_base::decl(), - op_url_parse_search_params::decl(), - op_url_stringify_search_params::decl(), - op_urlpattern_parse::decl(), - op_urlpattern_process_match_input::decl(), - ]) .build() } +pub fn init_ops() -> Extension { + ops(&mut ext()).build() +} + /// Parse `href` with a `base_href`. Fills the out `buf` with URL components. #[op] pub fn op_url_parse_with_base( diff --git a/ext/web/benches/encoding.rs b/ext/web/benches/encoding.rs index 8885572633..feb3097850 100644 --- a/ext/web/benches/encoding.rs +++ b/ext/web/benches/encoding.rs @@ -23,10 +23,10 @@ impl deno_web::TimersPermission for Permissions { fn setup() -> Vec { vec![ - deno_webidl::init(), - deno_url::init(), - deno_console::init(), - deno_web::init::(BlobStore::default(), None), + deno_webidl::init_esm(), + deno_url::init_ops_and_esm(), + deno_console::init_esm(), + deno_web::init_ops_and_esm::(BlobStore::default(), None), Extension::builder("bench_setup") .esm(vec![ExtensionFileSource { specifier: "ext:setup".to_string(), diff --git a/ext/web/benches/timers_ops.rs b/ext/web/benches/timers_ops.rs index d8ea29021e..e1e97df650 100644 --- a/ext/web/benches/timers_ops.rs +++ b/ext/web/benches/timers_ops.rs @@ -21,10 +21,10 @@ impl deno_web::TimersPermission for Permissions { fn setup() -> Vec { vec![ - deno_webidl::init(), - deno_url::init(), - deno_console::init(), - deno_web::init::(BlobStore::default(), None), + deno_webidl::init_esm(), + deno_url::init_ops_and_esm(), + deno_console::init_esm(), + deno_web::init_ops_and_esm::(BlobStore::default(), None), Extension::builder("bench_setup") .esm(vec![ ExtensionFileSource { diff --git a/ext/web/lib.rs b/ext/web/lib.rs index 6a21a7470d..dfc5ece725 100644 --- a/ext/web/lib.rs +++ b/ext/web/lib.rs @@ -16,6 +16,7 @@ use deno_core::v8; use deno_core::ByteString; use deno_core::CancelHandle; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::Resource; use deno_core::ResourceId; @@ -57,74 +58,92 @@ use crate::timers::op_timer_handle; use crate::timers::StartTime; pub use crate::timers::TimersPermission; -/// Load and execute the javascript code. -pub fn init( - blob_store: BlobStore, - maybe_location: Option, -) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_console", "deno_url"], ) - .esm(include_js_files!( - "00_infra.js", - "01_dom_exception.js", - "01_mimesniff.js", - "02_event.js", - "02_structured_clone.js", - "02_timers.js", - "03_abort_signal.js", - "04_global_interfaces.js", - "05_base64.js", - "06_streams.js", - "08_text_encoding.js", - "09_file.js", - "10_filereader.js", - "11_blob_url.js", - "12_location.js", - "13_message_port.js", - "14_compression.js", - "15_performance.js", - )) - .ops(vec![ - op_base64_decode::decl(), - op_base64_encode::decl(), - op_base64_atob::decl(), - op_base64_btoa::decl(), - op_encoding_normalize_label::decl(), - op_encoding_decode_single::decl(), - op_encoding_decode_utf8::decl(), - op_encoding_new_decoder::decl(), - op_encoding_decode::decl(), - op_encoding_encode_into::decl(), - op_encode_binary_string::decl(), - op_blob_create_part::decl(), - op_blob_slice_part::decl(), - op_blob_read_part::decl(), - op_blob_remove_part::decl(), - op_blob_create_object_url::decl(), - op_blob_revoke_object_url::decl(), - op_blob_from_object_url::decl(), - op_message_port_create_entangled::decl(), - op_message_port_post_message::decl(), - op_message_port_recv_message::decl(), - compression::op_compression_new::decl(), - compression::op_compression_write::decl(), - compression::op_compression_finish::decl(), - op_now::decl::

(), - op_timer_handle::decl(), - op_cancel_handle::decl(), - op_sleep::decl(), - op_transfer_arraybuffer::decl(), - ]) - .state(move |state| { - state.put(blob_store.clone()); - if let Some(location) = maybe_location.clone() { - state.put(Location(location)); - } - state.put(StartTime::now()); - }) - .build() +} + +fn ops( + ext: &mut ExtensionBuilder, + blob_store: BlobStore, + maybe_location: Option, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_base64_decode::decl(), + op_base64_encode::decl(), + op_base64_atob::decl(), + op_base64_btoa::decl(), + op_encoding_normalize_label::decl(), + op_encoding_decode_single::decl(), + op_encoding_decode_utf8::decl(), + op_encoding_new_decoder::decl(), + op_encoding_decode::decl(), + op_encoding_encode_into::decl(), + op_encode_binary_string::decl(), + op_blob_create_part::decl(), + op_blob_slice_part::decl(), + op_blob_read_part::decl(), + op_blob_remove_part::decl(), + op_blob_create_object_url::decl(), + op_blob_revoke_object_url::decl(), + op_blob_from_object_url::decl(), + op_message_port_create_entangled::decl(), + op_message_port_post_message::decl(), + op_message_port_recv_message::decl(), + compression::op_compression_new::decl(), + compression::op_compression_write::decl(), + compression::op_compression_finish::decl(), + op_now::decl::

(), + op_timer_handle::decl(), + op_cancel_handle::decl(), + op_sleep::decl(), + op_transfer_arraybuffer::decl(), + ]) + .state(move |state| { + state.put(blob_store.clone()); + if let Some(location) = maybe_location.clone() { + state.put(Location(location)); + } + state.put(StartTime::now()); + }) +} + +pub fn init_ops_and_esm( + blob_store: BlobStore, + maybe_location: Option, +) -> Extension { + ops::

(&mut ext(), blob_store, maybe_location) + .esm(include_js_files!( + "00_infra.js", + "01_dom_exception.js", + "01_mimesniff.js", + "02_event.js", + "02_structured_clone.js", + "02_timers.js", + "03_abort_signal.js", + "04_global_interfaces.js", + "05_base64.js", + "06_streams.js", + "08_text_encoding.js", + "09_file.js", + "10_filereader.js", + "11_blob_url.js", + "12_location.js", + "13_message_port.js", + "14_compression.js", + "15_performance.js", + )) + .build() +} + +pub fn init_ops( + blob_store: BlobStore, + maybe_location: Option, +) -> Extension { + ops::

(&mut ext(), blob_store, maybe_location).build() } #[op] diff --git a/ext/webgpu/lib.rs b/ext/webgpu/lib.rs index aa6207d349..d399125c7d 100644 --- a/ext/webgpu/lib.rs +++ b/ext/webgpu/lib.rs @@ -6,6 +6,7 @@ use deno_core::error::AnyError; use deno_core::include_js_files; use deno_core::op; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::Resource; use deno_core::ResourceId; @@ -116,21 +117,31 @@ impl Resource for WebGpuQuerySet { } } -pub fn init(unstable: bool) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_web"], ) - .esm(include_js_files!("01_webgpu.js", "02_idl_types.js",)) - .ops(declare_webgpu_ops()) - .state(move |state| { +} + +fn ops(ext: &mut ExtensionBuilder, unstable: bool) -> &mut ExtensionBuilder { + ext.ops(declare_webgpu_ops()).state(move |state| { // TODO: check & possibly streamline this // Unstable might be able to be OpMiddleware // let unstable_checker = state.borrow::(); // let unstable = unstable_checker.unstable; state.put(Unstable(unstable)); }) - .build() +} + +pub fn init_ops_and_esm(unstable: bool) -> Extension { + ops(&mut ext(), unstable) + .esm(include_js_files!("01_webgpu.js", "02_idl_types.js",)) + .build() +} + +pub fn init_ops(unstable: bool) -> Extension { + ops(&mut ext(), unstable).build() } fn deserialize_features(features: &wgpu_types::Features) -> Vec<&'static str> { diff --git a/ext/webidl/benches/dict.rs b/ext/webidl/benches/dict.rs index d07261bca6..2ed9adf780 100644 --- a/ext/webidl/benches/dict.rs +++ b/ext/webidl/benches/dict.rs @@ -11,7 +11,7 @@ use deno_core::ExtensionFileSourceCode; fn setup() -> Vec { vec![ - deno_webidl::init(), + deno_webidl::init_esm(), Extension::builder("deno_webidl_bench") .esm(vec![ExtensionFileSource { specifier: "ext:setup".to_string(), diff --git a/ext/webidl/lib.rs b/ext/webidl/lib.rs index 1c3d760d37..42559e501d 100644 --- a/ext/webidl/lib.rs +++ b/ext/webidl/lib.rs @@ -3,8 +3,11 @@ use deno_core::include_js_files; use deno_core::Extension; -/// Load and execute the javascript code. pub fn init() -> Extension { + Extension::builder(env!("CARGO_PKG_NAME")).build() +} + +pub fn init_esm() -> Extension { Extension::builder(env!("CARGO_PKG_NAME")) .esm(include_js_files!("00_webidl.js",)) .build() diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index bf6d519141..dbbeae21f8 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -9,6 +9,7 @@ use deno_core::futures::SinkExt; use deno_core::futures::StreamExt; use deno_core::include_js_files; use deno_core::op; +use deno_core::ExtensionBuilder; use deno_core::url; use deno_core::AsyncRefCell; @@ -497,33 +498,65 @@ pub async fn op_ws_next_event( Ok(res) } -pub fn init( +fn ext() -> ExtensionBuilder { + Extension::builder_with_deps( + env!("CARGO_PKG_NAME"), + &["deno_url", "deno_webidl"], + ) +} + +fn ops( + ext: &mut ExtensionBuilder, + user_agent: String, + root_cert_store: Option, + unsafely_ignore_certificate_errors: Option>, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_ws_check_permission_and_cancel_handle::decl::

(), + op_ws_create::decl::

(), + op_ws_send::decl(), + op_ws_close::decl(), + op_ws_next_event::decl(), + ]) + .state(move |state| { + state.put::(WsUserAgent(user_agent.clone())); + state.put(UnsafelyIgnoreCertificateErrors( + unsafely_ignore_certificate_errors.clone(), + )); + state.put::(WsRootStore(root_cert_store.clone())); + }) +} + +pub fn init_ops_and_esm( user_agent: String, root_cert_store: Option, unsafely_ignore_certificate_errors: Option>, ) -> Extension { - Extension::builder_with_deps( - env!("CARGO_PKG_NAME"), - &["deno_url", "deno_webidl"], + ops::

( + &mut ext(), + user_agent, + root_cert_store, + unsafely_ignore_certificate_errors, ) .esm(include_js_files!( "01_websocket.js", "02_websocketstream.js", )) - .ops(vec![ - op_ws_check_permission_and_cancel_handle::decl::

(), - op_ws_create::decl::

(), - op_ws_send::decl(), - op_ws_close::decl(), - op_ws_next_event::decl(), - ]) - .state(move |state| { - state.put::(WsUserAgent(user_agent.clone())); - state.put(UnsafelyIgnoreCertificateErrors( - unsafely_ignore_certificate_errors.clone(), - )); - state.put::(WsRootStore(root_cert_store.clone())); - }) + .build() +} + +pub fn init_ops( + user_agent: String, + root_cert_store: Option, + unsafely_ignore_certificate_errors: Option>, +) -> Extension { + ops::

( + &mut ext(), + user_agent, + root_cert_store, + unsafely_ignore_certificate_errors, + ) .build() } diff --git a/ext/webstorage/lib.rs b/ext/webstorage/lib.rs index ca96b01bc1..f3caddbaf0 100644 --- a/ext/webstorage/lib.rs +++ b/ext/webstorage/lib.rs @@ -9,6 +9,7 @@ use deno_core::error::AnyError; use deno_core::include_js_files; use deno_core::op; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use rusqlite::params; use rusqlite::Connection; @@ -21,9 +22,15 @@ struct OriginStorageDir(PathBuf); const MAX_STORAGE_BYTES: usize = 10 * 1024 * 1024; -pub fn init(origin_storage_dir: Option) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_webidl"]) - .esm(include_js_files!("01_webstorage.js",)) +} + +fn ops( + ext: &mut ExtensionBuilder, + origin_storage_dir: Option, +) -> &mut ExtensionBuilder { + ext .ops(vec![ op_webstorage_length::decl(), op_webstorage_key::decl(), @@ -38,9 +45,18 @@ pub fn init(origin_storage_dir: Option) -> Extension { state.put(OriginStorageDir(origin_storage_dir.clone())); } }) +} + +pub fn init_ops_and_esm(origin_storage_dir: Option) -> Extension { + ops(&mut ext(), origin_storage_dir) + .esm(include_js_files!("01_webstorage.js",)) .build() } +pub fn init_ops(origin_storage_dir: Option) -> Extension { + ops(&mut ext(), origin_storage_dir).build() +} + pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_webstorage.d.ts") } diff --git a/runtime/build.rs b/runtime/build.rs index d9f428e9b7..788174d1ba 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -251,38 +251,42 @@ mod startup_snapshot { .build(); let mut extensions_with_js: Vec = vec![ - deno_webidl::init(), - deno_console::init(), - deno_url::init(), + deno_webidl::init_esm(), + deno_console::init_esm(), + deno_url::init_ops_and_esm(), deno_tls::init(), - deno_web::init::( + deno_web::init_ops_and_esm::( deno_web::BlobStore::default(), Default::default(), ), - deno_fetch::init::(Default::default()), - deno_cache::init::(None), - deno_websocket::init::("".to_owned(), None, None), - deno_webstorage::init(None), - deno_crypto::init(None), - deno_webgpu::init(false), - deno_broadcast_channel::init( + deno_fetch::init_ops_and_esm::(Default::default()), + deno_cache::init_ops_and_esm::(None), + deno_websocket::init_ops_and_esm::( + "".to_owned(), + None, + None, + ), + deno_webstorage::init_ops_and_esm(None), + deno_crypto::init_ops_and_esm(None), + deno_webgpu::init_ops_and_esm(false), + deno_broadcast_channel::init_ops_and_esm( deno_broadcast_channel::InMemoryBroadcastChannel::default(), false, // No --unstable. ), - deno_ffi::init::(false), - deno_net::init::( + deno_ffi::init_ops_and_esm::(false), + deno_net::init_ops_and_esm::( None, false, // No --unstable. None, ), deno_napi::init::(), - deno_http::init(), - deno_io::init(Default::default()), - deno_fs::init::(false), - deno_flash::init::(false), // No --unstable + deno_http::init_ops_and_esm(), + deno_io::init_ops_and_esm(Default::default()), + deno_fs::init_ops_and_esm::(false), + deno_flash::init_ops_and_esm::(false), // No --unstable runtime_extension, // FIXME(bartlomieju): these extensions are specified last, because they // depend on `runtime`, even though it should be other way around - deno_node::init::(None), + deno_node::init_ops_and_esm::(None), deno_node::init_polyfill_ops_and_esm(), ]; diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 7948dca0a0..f16ffd5a0c 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 { + 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::( + options.blob_store.clone(), + Some(main_module.clone()), + ), + deno_fetch::init_ops::(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::(create_cache), + deno_websocket::init_ops::( + 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::(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::(unstable), + deno_io::init_ops(std::mem::take(&mut options.stdio)), + deno_tls::init(), + deno_net::init_ops::( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_napi::init::(), + // 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::(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::(unstable), + ops::http::init(), + ] +} + +#[cfg(not(feature = "dont_create_runtime_snapshot"))] +fn get_extensions( + options: &mut WebWorkerOptions, + unstable: bool, + main_module: ModuleSpecifier, +) -> Vec { + 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::( + options.blob_store.clone(), + Some(main_module.clone()), + ), + deno_fetch::init_ops_and_esm::(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::(create_cache), + deno_websocket::init_ops_and_esm::( + 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::(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::(unstable), + deno_io::init_ops_and_esm(std::mem::take(&mut options.stdio)), + deno_tls::init(), + deno_net::init_ops_and_esm::( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_napi::init::(), + // 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::( + 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::(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 = vec![ - // Web APIs - deno_webidl::init(), - deno_console::init(), - deno_url::init(), - deno_web::init::( - options.blob_store.clone(), - Some(main_module.clone()), - ), - deno_fetch::init::(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::(create_cache), - deno_websocket::init::( - 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::(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::(unstable), - deno_io::init(options.stdio), - deno_tls::init(), - deno_net::init::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init::(), - // 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::(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::(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)); diff --git a/runtime/worker.rs b/runtime/worker.rs index 42ff6b0f9a..3d30b95a36 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -183,6 +183,160 @@ impl Default for WorkerOptions { } } +#[cfg(not(feature = "dont_create_runtime_snapshot"))] +fn get_extensions( + options: &mut WorkerOptions, + unstable: bool, + exit_code: ExitCode, + main_module: ModuleSpecifier, +) -> Vec { + 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::( + options.blob_store.clone(), + options.bootstrap.location.clone(), + ), + deno_fetch::init_ops::(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::(create_cache), + deno_websocket::init_ops::( + options.bootstrap.user_agent.clone(), + options.root_cert_store.clone(), + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_webstorage::init_ops(options.origin_storage_dir.clone()), + 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::(unstable), + // Runtime ops + ops::runtime::init(main_module), + ops::worker_host::init( + options.create_web_worker_cb.clone(), + options.web_worker_preload_module_cb.clone(), + options.web_worker_pre_execute_module_cb.clone(), + options.format_js_error_fn.clone(), + ), + ops::fs_events::init(), + deno_fs::init_ops::(unstable), + deno_io::init_ops(std::mem::take(&mut options.stdio)), + deno_tls::init(), + deno_net::init_ops::( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_napi::init::(), + deno_node::init_ops::(options.npm_resolver.take()), + ops::os::init(exit_code), + ops::permissions::init(), + ops::process::init_ops(), + ops::signal::init(), + ops::tty::init(), + deno_http::init_ops(), + deno_flash::init_ops::(unstable), + ops::http::init(), + deno_node::init_polyfill_ops(), + ] +} + +#[cfg(feature = "dont_create_runtime_snapshot")] +fn get_extensions( + options: &mut WorkerOptions, + unstable: bool, + exit_code: ExitCode, + main_module: ModuleSpecifier, +) -> Vec { + 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::( + options.blob_store.clone(), + options.bootstrap.location.clone(), + ), + deno_fetch::init_ops_and_esm::(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::(create_cache), + deno_websocket::init_ops_and_esm::( + options.bootstrap.user_agent.clone(), + options.root_cert_store.clone(), + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_webstorage::init_ops_and_esm(options.origin_storage_dir.clone()), + 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::(unstable), + // Runtime ops + ops::runtime::init(main_module), + ops::worker_host::init( + options.create_web_worker_cb.clone(), + options.web_worker_preload_module_cb.clone(), + options.web_worker_pre_execute_module_cb.clone(), + options.format_js_error_fn.clone(), + ), + ops::fs_events::init(), + deno_fs::init_ops_and_esm::(unstable), + deno_io::init_ops_and_esm(std::mem::take(&mut options.stdio)), + deno_tls::init(), + deno_net::init_ops_and_esm::( + options.root_cert_store.clone(), + unstable, + options.unsafely_ignore_certificate_errors.clone(), + ), + deno_napi::init::(), + deno_node::init_ops_and_esm::( + options.npm_resolver.take(), + ), + ops::os::init(exit_code), + 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::(unstable), + ops::http::init(), + deno_node::init_polyfill_ops_and_esm(), + ] +} + impl MainWorker { pub fn bootstrap_from_options( main_module: ModuleSpecifier, @@ -211,77 +365,13 @@ impl MainWorker { }) .build(); let exit_code = ExitCode(Arc::new(AtomicI32::new(0))); - 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![ - // Web APIs - deno_webidl::init(), - deno_console::init(), - deno_url::init(), - deno_web::init::( - options.blob_store.clone(), - options.bootstrap.location.clone(), - ), - deno_fetch::init::(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::(create_cache), - deno_websocket::init::( - options.bootstrap.user_agent.clone(), - options.root_cert_store.clone(), - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_webstorage::init(options.origin_storage_dir.clone()), - deno_broadcast_channel::init(options.broadcast_channel.clone(), unstable), - deno_crypto::init(options.seed), - deno_webgpu::init(unstable), - // ffi - deno_ffi::init::(unstable), - // Runtime ops - ops::runtime::init(main_module.clone()), - ops::worker_host::init( - options.create_web_worker_cb.clone(), - options.web_worker_preload_module_cb.clone(), - options.web_worker_pre_execute_module_cb.clone(), - options.format_js_error_fn.clone(), - ), - ops::fs_events::init(), - deno_fs::init::(unstable), - deno_io::init(options.stdio), - deno_tls::init(), - deno_net::init::( - options.root_cert_store.clone(), - unstable, - options.unsafely_ignore_certificate_errors.clone(), - ), - deno_napi::init::(), - deno_node::init::(options.npm_resolver), - ops::os::init(exit_code.clone()), - ops::permissions::init(), - ops::process::init_ops(), - ops::signal::init(), - ops::tty::init(), - deno_http::init(), - deno_flash::init::(unstable), - ops::http::init(), - ]; - - // TODO(bartlomieju): finish this work, currently only `deno_node` is different - // as it has the most files - #[cfg(feature = "dont_create_runtime_snapshot")] - extensions.push(deno_node::init_polyfill_ops_and_esm()); - - #[cfg(not(feature = "dont_create_runtime_snapshot"))] - extensions.push(deno_node::init_polyfill_ops()); + let mut extensions = get_extensions( + &mut options, + unstable, + exit_code.clone(), + main_module.clone(), + ); extensions.push(perm_ext);