2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-17 10:19:55 -04:00
|
|
|
use std::borrow::Cow;
|
2024-01-14 20:28:46 -05:00
|
|
|
use std::collections::HashMap;
|
2023-01-05 14:29:50 -05:00
|
|
|
use std::rc::Rc;
|
2023-12-24 08:44:40 -05:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2023-01-05 14:29:50 -05:00
|
|
|
use std::sync::atomic::AtomicI32;
|
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
|
|
|
use std::sync::Arc;
|
2023-12-05 11:26:06 -05:00
|
|
|
use std::time::Duration;
|
2023-11-10 12:41:24 -05:00
|
|
|
use std::time::Instant;
|
2023-01-05 14:29:50 -05:00
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
use deno_broadcast_channel::InMemoryBroadcastChannel;
|
2022-09-28 08:11:12 -04:00
|
|
|
use deno_cache::CreateCache;
|
|
|
|
use deno_cache::SqliteBackedCache;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-04-26 19:06:10 -04:00
|
|
|
use deno_core::error::JsError;
|
2023-11-10 12:41:24 -05:00
|
|
|
use deno_core::merge_op_metrics;
|
2022-12-18 15:34:33 -05:00
|
|
|
use deno_core::v8;
|
2021-09-29 04:47:24 -04:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2021-04-28 12:41:50 -04:00
|
|
|
use deno_core::Extension;
|
2023-10-12 11:55:50 -04:00
|
|
|
use deno_core::FeatureChecker;
|
2022-10-15 17:19:03 -04:00
|
|
|
use deno_core::FsModuleLoader;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_core::GetErrorClassFn;
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2021-05-26 15:07:12 -04:00
|
|
|
use deno_core::LocalInspectorSession;
|
2024-01-09 23:18:40 -05:00
|
|
|
use deno_core::ModuleCodeString;
|
2020-02-18 10:08:18 -05:00
|
|
|
use deno_core::ModuleId;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-11-10 12:41:24 -05:00
|
|
|
use deno_core::OpMetricsFactoryFn;
|
2023-11-05 16:27:36 -05:00
|
|
|
use deno_core::OpMetricsSummaryTracker;
|
2023-12-05 11:26:06 -05:00
|
|
|
use deno_core::PollEventLoopOptions;
|
2020-09-11 09:18:49 -04:00
|
|
|
use deno_core::RuntimeOptions;
|
2021-07-06 13:42:52 -04:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2022-04-15 10:08:09 -04:00
|
|
|
use deno_core::SourceMapGetter;
|
2023-11-01 14:57:55 -04:00
|
|
|
use deno_cron::local::LocalCronHandler;
|
2023-05-04 14:28:42 -04:00
|
|
|
use deno_fs::FileSystem;
|
2023-05-10 10:23:26 -04:00
|
|
|
use deno_http::DefaultHttpPropertyExtractor;
|
2023-03-04 19:39:48 -05:00
|
|
|
use deno_io::Stdio;
|
2023-08-22 01:56:00 -04:00
|
|
|
use deno_kv::dynamic::MultiBackendDbHandler;
|
2023-05-01 16:42:05 -04:00
|
|
|
use deno_tls::RootCertStoreProvider;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
use deno_tls::TlsKeys;
|
2021-07-05 09:34:37 -04:00
|
|
|
use deno_web::BlobStore;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2023-01-05 14:29:50 -05:00
|
|
|
|
2024-04-17 10:19:55 -04:00
|
|
|
use crate::code_cache::CodeCache;
|
|
|
|
use crate::code_cache::CodeCacheType;
|
|
|
|
use crate::fs_util::code_timestamp;
|
2023-01-05 14:29:50 -05:00
|
|
|
use crate::inspector_server::InspectorServer;
|
|
|
|
use crate::ops;
|
2023-01-07 11:25:34 -05:00
|
|
|
use crate::permissions::PermissionsContainer;
|
2024-03-04 20:17:39 -05:00
|
|
|
use crate::shared::maybe_transpile_source;
|
2023-08-28 17:30:46 -04:00
|
|
|
use crate::shared::runtime;
|
2023-01-05 14:29:50 -05:00
|
|
|
use crate::BootstrapOptions;
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2022-04-26 19:06:10 -04:00
|
|
|
pub type FormatJsErrorFn = dyn Fn(&JsError) -> String + Sync + Send;
|
|
|
|
|
2023-12-28 14:37:10 -05:00
|
|
|
pub fn import_meta_resolve_callback(
|
|
|
|
loader: &dyn deno_core::ModuleLoader,
|
|
|
|
specifier: String,
|
|
|
|
referrer: String,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
loader.resolve(
|
|
|
|
&specifier,
|
|
|
|
&referrer,
|
|
|
|
deno_core::ResolutionKind::DynamicImport,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-14 20:28:46 -05:00
|
|
|
// TODO(bartlomieju): temporary measurement until we start supporting more
|
|
|
|
// module types
|
|
|
|
pub fn validate_import_attributes_callback(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
attributes: &HashMap<String, String>,
|
|
|
|
) {
|
|
|
|
for (key, value) in attributes {
|
|
|
|
let msg = if key != "type" {
|
|
|
|
Some(format!("\"{key}\" attribute is not supported."))
|
|
|
|
} else if value != "json" {
|
|
|
|
Some(format!("\"{value}\" is not a valid module type."))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(msg) = msg else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
let message = v8::String::new(scope, &msg).unwrap();
|
|
|
|
let exception = v8::Exception::type_error(scope, message);
|
|
|
|
scope.throw_exception(exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 20:26:58 -04:00
|
|
|
#[derive(Clone, Default)]
|
2022-06-08 11:45:38 -04:00
|
|
|
pub struct ExitCode(Arc<AtomicI32>);
|
|
|
|
|
|
|
|
impl ExitCode {
|
|
|
|
pub fn get(&self) -> i32 {
|
|
|
|
self.0.load(Relaxed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, code: i32) {
|
|
|
|
self.0.store(code, Relaxed);
|
|
|
|
}
|
|
|
|
}
|
2023-05-28 14:44:41 -04:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
/// This worker is created and used by almost all
|
|
|
|
/// subcommands in Deno executable.
|
2020-01-21 11:50:06 -05:00
|
|
|
///
|
2020-11-26 09:17:45 -05:00
|
|
|
/// It provides ops available in the `Deno` namespace.
|
2020-01-21 11:50:06 -05:00
|
|
|
///
|
2020-11-26 09:17:45 -05:00
|
|
|
/// All `WebWorker`s created during program execution
|
|
|
|
/// are descendants of this worker.
|
|
|
|
pub struct MainWorker {
|
2020-12-11 12:49:26 -05:00
|
|
|
pub js_runtime: JsRuntime,
|
2020-09-19 19:17:35 -04:00
|
|
|
should_break_on_first_statement: bool,
|
2022-12-12 09:33:30 -05:00
|
|
|
should_wait_for_inspector_session: bool,
|
2022-06-08 11:45:38 -04:00
|
|
|
exit_code: ExitCode,
|
2023-02-21 19:55:31 -05:00
|
|
|
bootstrap_fn_global: Option<v8::Global<v8::Function>>,
|
2024-04-15 14:08:33 -04:00
|
|
|
dispatch_load_event_fn_global: v8::Global<v8::Function>,
|
|
|
|
dispatch_beforeunload_event_fn_global: v8::Global<v8::Function>,
|
|
|
|
dispatch_unload_event_fn_global: v8::Global<v8::Function>,
|
2024-04-16 09:45:41 -04:00
|
|
|
dispatch_process_beforeexit_event_fn_global: v8::Global<v8::Function>,
|
|
|
|
dispatch_process_exit_event_fn_global: v8::Global<v8::Function>,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
pub struct WorkerOptions {
|
2021-10-05 16:41:14 -04:00
|
|
|
pub bootstrap: BootstrapOptions,
|
2022-12-18 21:55:50 -05:00
|
|
|
|
|
|
|
/// JsRuntime extensions, not to be confused with ES modules.
|
|
|
|
///
|
2023-03-09 19:22:27 -05:00
|
|
|
/// Extensions register "ops" and JavaScript sources provided in `js` or `esm`
|
|
|
|
/// configuration. If you are using a snapshot, then extensions shouldn't
|
|
|
|
/// provide JavaScript sources that were already snapshotted.
|
|
|
|
pub extensions: Vec<Extension>,
|
2022-12-18 21:55:50 -05:00
|
|
|
|
|
|
|
/// V8 snapshot that should be loaded on startup.
|
2024-02-27 10:05:57 -05:00
|
|
|
pub startup_snapshot: Option<&'static [u8]>,
|
2023-06-05 05:22:32 -04:00
|
|
|
|
2023-11-11 12:01:48 -05:00
|
|
|
/// Should op registration be skipped?
|
|
|
|
pub skip_op_registration: bool,
|
|
|
|
|
2023-06-05 05:22:32 -04:00
|
|
|
/// Optional isolate creation parameters, such as heap limits.
|
|
|
|
pub create_params: Option<v8::CreateParams>,
|
|
|
|
|
2021-08-10 07:19:45 -04:00
|
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
2023-05-01 16:42:05 -04:00
|
|
|
pub root_cert_store_provider: Option<Arc<dyn RootCertStoreProvider>>,
|
2020-12-11 12:49:26 -05:00
|
|
|
pub seed: Option<u64>,
|
2022-12-18 21:55:50 -05:00
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
pub fs: Arc<dyn FileSystem>,
|
2022-12-18 21:55:50 -05:00
|
|
|
/// Implementation of `ModuleLoader` which will be
|
|
|
|
/// called when V8 requests to load ES modules.
|
|
|
|
///
|
|
|
|
/// If not provided runtime will error if code being
|
|
|
|
/// executed tries to load modules.
|
2020-12-11 12:49:26 -05:00
|
|
|
pub module_loader: Rc<dyn ModuleLoader>,
|
2023-04-24 19:44:35 -04:00
|
|
|
pub npm_resolver: Option<Arc<dyn deno_node::NpmResolver>>,
|
2022-02-11 07:41:56 -05:00
|
|
|
// Callbacks invoked when creating new instance of WebWorker
|
2020-12-11 12:49:26 -05:00
|
|
|
pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>,
|
2022-04-26 19:06:10 -04:00
|
|
|
pub format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
|
2022-12-18 21:55:50 -05:00
|
|
|
|
|
|
|
/// Source map reference for errors.
|
2024-02-10 21:08:02 -05:00
|
|
|
pub source_map_getter: Option<Rc<dyn SourceMapGetter>>,
|
2020-12-11 12:49:26 -05:00
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2022-12-12 09:33:30 -05:00
|
|
|
// If true, the worker will wait for inspector session and break on first
|
|
|
|
// statement of user code. Takes higher precedence than
|
|
|
|
// `should_wait_for_inspector_session`.
|
2020-12-11 12:49:26 -05:00
|
|
|
pub should_break_on_first_statement: bool,
|
2022-12-12 09:33:30 -05:00
|
|
|
// If true, the worker will wait for inspector session before executing
|
|
|
|
// user code.
|
|
|
|
pub should_wait_for_inspector_session: bool,
|
2023-11-10 12:41:24 -05:00
|
|
|
/// If Some, print a low-level trace output for ops matching the given patterns.
|
|
|
|
pub strace_ops: Option<Vec<String>>,
|
2022-12-18 21:55:50 -05:00
|
|
|
|
|
|
|
/// Allows to map error type to a string "class" used to represent
|
|
|
|
/// error in JavaScript.
|
2020-12-13 13:45:53 -05:00
|
|
|
pub get_error_class_fn: Option<GetErrorClassFn>,
|
2022-09-28 08:11:12 -04:00
|
|
|
pub cache_storage_dir: Option<std::path::PathBuf>,
|
2021-05-27 01:23:12 -04:00
|
|
|
pub origin_storage_dir: Option<std::path::PathBuf>,
|
2023-07-01 18:52:30 -04:00
|
|
|
pub blob_store: Arc<BlobStore>,
|
2021-05-22 12:08:24 -04:00
|
|
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
2022-12-18 21:55:50 -05:00
|
|
|
|
|
|
|
/// The store to use for transferring SharedArrayBuffers between isolates.
|
|
|
|
/// If multiple isolates should have the possibility of sharing
|
|
|
|
/// SharedArrayBuffers, they should use the same [SharedArrayBufferStore]. If
|
|
|
|
/// no [SharedArrayBufferStore] is specified, SharedArrayBuffer can not be
|
|
|
|
/// serialized.
|
2021-07-06 13:42:52 -04:00
|
|
|
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
2022-12-18 21:55:50 -05:00
|
|
|
|
|
|
|
/// The store to use for transferring `WebAssembly.Module` objects between
|
|
|
|
/// isolates.
|
|
|
|
/// If multiple isolates should have the possibility of sharing
|
|
|
|
/// `WebAssembly.Module` objects, they should use the same
|
|
|
|
/// [CompiledWasmModuleStore]. If no [CompiledWasmModuleStore] is specified,
|
|
|
|
/// `WebAssembly.Module` objects cannot be serialized.
|
2021-09-29 04:47:24 -04:00
|
|
|
pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
|
2022-04-26 19:00:04 -04:00
|
|
|
pub stdio: Stdio,
|
2023-10-12 11:55:50 -04:00
|
|
|
pub feature_checker: Arc<FeatureChecker>,
|
2024-04-17 10:19:55 -04:00
|
|
|
|
|
|
|
/// V8 code cache for module and script source code.
|
|
|
|
pub v8_code_cache: Option<Arc<dyn CodeCache>>,
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2022-10-15 17:19:03 -04:00
|
|
|
impl Default for WorkerOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
create_web_worker_cb: Arc::new(|_| {
|
|
|
|
unimplemented!("web workers are not supported")
|
|
|
|
}),
|
2023-05-04 14:28:42 -04:00
|
|
|
fs: Arc::new(deno_fs::RealFs),
|
2022-10-15 17:19:03 -04:00
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
2023-11-11 12:01:48 -05:00
|
|
|
skip_op_registration: false,
|
2022-10-15 17:19:03 -04:00
|
|
|
seed: None,
|
|
|
|
unsafely_ignore_certificate_errors: Default::default(),
|
|
|
|
should_break_on_first_statement: Default::default(),
|
2022-12-12 09:33:30 -05:00
|
|
|
should_wait_for_inspector_session: Default::default(),
|
2023-11-10 12:41:24 -05:00
|
|
|
strace_ops: Default::default(),
|
2022-10-15 17:19:03 -04:00
|
|
|
compiled_wasm_module_store: Default::default(),
|
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
maybe_inspector_server: Default::default(),
|
|
|
|
format_js_error_fn: Default::default(),
|
|
|
|
get_error_class_fn: Default::default(),
|
|
|
|
origin_storage_dir: Default::default(),
|
|
|
|
cache_storage_dir: Default::default(),
|
|
|
|
broadcast_channel: Default::default(),
|
|
|
|
source_map_getter: Default::default(),
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider: Default::default(),
|
2022-10-15 17:19:03 -04:00
|
|
|
npm_resolver: Default::default(),
|
|
|
|
blob_store: Default::default(),
|
|
|
|
extensions: Default::default(),
|
2022-11-10 06:46:26 -05:00
|
|
|
startup_snapshot: Default::default(),
|
2023-06-05 05:22:32 -04:00
|
|
|
create_params: Default::default(),
|
2022-10-15 17:19:03 -04:00
|
|
|
bootstrap: Default::default(),
|
|
|
|
stdio: Default::default(),
|
2023-10-12 11:55:50 -04:00
|
|
|
feature_checker: Default::default(),
|
2024-04-17 10:19:55 -04:00
|
|
|
v8_code_cache: Default::default(),
|
2022-10-15 17:19:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 13:22:25 -04:00
|
|
|
pub fn create_op_metrics(
|
2023-11-10 12:41:24 -05:00
|
|
|
enable_op_summary_metrics: bool,
|
|
|
|
strace_ops: Option<Vec<String>>,
|
|
|
|
) -> (
|
|
|
|
Option<Rc<OpMetricsSummaryTracker>>,
|
|
|
|
Option<OpMetricsFactoryFn>,
|
|
|
|
) {
|
|
|
|
let mut op_summary_metrics = None;
|
|
|
|
let mut op_metrics_factory_fn: Option<OpMetricsFactoryFn> = None;
|
|
|
|
let now = Instant::now();
|
|
|
|
let max_len: Rc<std::cell::Cell<usize>> = Default::default();
|
|
|
|
if let Some(patterns) = strace_ops {
|
|
|
|
/// Match an op name against a list of patterns
|
|
|
|
fn matches_pattern(patterns: &[String], name: &str) -> bool {
|
|
|
|
let mut found_match = false;
|
|
|
|
let mut found_nomatch = false;
|
|
|
|
for pattern in patterns.iter() {
|
|
|
|
if let Some(pattern) = pattern.strip_prefix('-') {
|
|
|
|
if name.contains(pattern) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if name.contains(pattern.as_str()) {
|
|
|
|
found_match = true;
|
|
|
|
} else {
|
|
|
|
found_nomatch = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
found_match || !found_nomatch
|
|
|
|
}
|
|
|
|
|
|
|
|
op_metrics_factory_fn = Some(Box::new(move |_, _, decl| {
|
|
|
|
// If we don't match a requested pattern, or we match a negative pattern, bail
|
|
|
|
if !matches_pattern(&patterns, decl.name) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_len.set(max_len.get().max(decl.name.len()));
|
|
|
|
let max_len = max_len.clone();
|
|
|
|
Some(Rc::new(
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stderr)]
|
2023-11-10 12:41:24 -05:00
|
|
|
move |op: &deno_core::_ops::OpCtx, event, source| {
|
|
|
|
eprintln!(
|
|
|
|
"[{: >10.3}] {name:max_len$}: {event:?} {source:?}",
|
|
|
|
now.elapsed().as_secs_f64(),
|
|
|
|
name = op.decl().name,
|
|
|
|
max_len = max_len.get()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if enable_op_summary_metrics {
|
|
|
|
let summary = Rc::new(OpMetricsSummaryTracker::default());
|
|
|
|
let summary_metrics = summary.clone().op_metrics_factory_fn(|_| true);
|
|
|
|
op_metrics_factory_fn = Some(match op_metrics_factory_fn {
|
|
|
|
Some(f) => merge_op_metrics(f, summary_metrics),
|
|
|
|
None => summary_metrics,
|
|
|
|
});
|
|
|
|
op_summary_metrics = Some(summary);
|
|
|
|
}
|
|
|
|
|
|
|
|
(op_summary_metrics, op_metrics_factory_fn)
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:19:55 -04:00
|
|
|
fn get_code_cache(
|
|
|
|
code_cache: Arc<dyn CodeCache>,
|
|
|
|
specifier: &str,
|
|
|
|
) -> Option<Vec<u8>> {
|
|
|
|
// Code hashes are not maintained for op_eval_context scripts. Instead we use
|
|
|
|
// the modified timestamp from the local file system.
|
|
|
|
if let Ok(code_timestamp) = code_timestamp(specifier) {
|
|
|
|
code_cache
|
|
|
|
.get_sync(
|
|
|
|
specifier,
|
|
|
|
CodeCacheType::Script,
|
|
|
|
code_timestamp.to_string().as_str(),
|
|
|
|
)
|
|
|
|
.inspect(|_| {
|
|
|
|
// This log line is also used by tests.
|
|
|
|
log::debug!(
|
|
|
|
"V8 code cache hit for script: {specifier}, [{code_timestamp}]"
|
|
|
|
);
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_code_cache(
|
|
|
|
code_cache: Arc<dyn CodeCache>,
|
|
|
|
specifier: &str,
|
|
|
|
data: &[u8],
|
|
|
|
) {
|
|
|
|
// Code hashes are not maintained for op_eval_context scripts. Instead we use
|
|
|
|
// the modified timestamp from the local file system.
|
|
|
|
if let Ok(code_timestamp) = code_timestamp(specifier) {
|
|
|
|
// This log line is also used by tests.
|
|
|
|
log::debug!(
|
|
|
|
"Updating V8 code cache for script: {specifier}, [{code_timestamp}]",
|
|
|
|
);
|
|
|
|
code_cache.set_sync(
|
|
|
|
specifier,
|
|
|
|
CodeCacheType::Script,
|
|
|
|
code_timestamp.to_string().as_str(),
|
|
|
|
data,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
impl MainWorker {
|
2021-10-05 16:41:14 -04:00
|
|
|
pub fn bootstrap_from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2021-10-05 16:41:14 -04:00
|
|
|
options: WorkerOptions,
|
|
|
|
) -> Self {
|
|
|
|
let bootstrap_options = options.bootstrap.clone();
|
|
|
|
let mut worker = Self::from_options(main_module, permissions, options);
|
2023-11-12 23:52:59 -05:00
|
|
|
worker.bootstrap(bootstrap_options);
|
2021-10-05 16:41:14 -04:00
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
pub fn from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2021-10-08 11:03:49 -04:00
|
|
|
mut options: WorkerOptions,
|
2020-11-30 14:35:12 -05:00
|
|
|
) -> Self {
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(deno_permissions_worker,
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
2023-03-17 14:22:15 -04:00
|
|
|
permissions: PermissionsContainer,
|
|
|
|
enable_testing_features: bool,
|
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
2024-03-12 13:42:26 -04:00
|
|
|
// Save the permissions container and the wrapper.
|
2024-03-13 00:34:48 -04:00
|
|
|
state.put::<deno_permissions::PermissionsContainer>(options.permissions.0.clone());
|
2024-03-12 13:42:26 -04:00
|
|
|
// This is temporary until we migrate all exts/ to the deno_permissions crate.
|
2023-03-17 18:15:27 -04:00
|
|
|
state.put::<PermissionsContainer>(options.permissions);
|
|
|
|
state.put(ops::TestingFeaturesEnabled(options.enable_testing_features));
|
2023-03-17 14:22:15 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-11-10 12:41:24 -05:00
|
|
|
// Get our op metrics
|
|
|
|
let (op_summary_metrics, op_metrics_factory_fn) = create_op_metrics(
|
|
|
|
options.bootstrap.enable_op_summary_metrics,
|
|
|
|
options.strace_ops,
|
|
|
|
);
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
// Permissions: many ops depend on this
|
2021-10-05 16:41:14 -04:00
|
|
|
let enable_testing_features = options.bootstrap.enable_testing_features;
|
2022-06-08 11:45:38 -04:00
|
|
|
let exit_code = ExitCode(Arc::new(AtomicI32::new(0)));
|
2023-03-09 15:09:45 -05:00
|
|
|
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))
|
|
|
|
});
|
2023-03-08 06:43:26 -05:00
|
|
|
|
2023-03-16 13:36:53 -04:00
|
|
|
// NOTE(bartlomieju): ordering is important here, keep it in sync with
|
2024-01-10 10:30:50 -05:00
|
|
|
// `runtime/web_worker.rs` and `runtime/snapshot.rs`!
|
2023-03-09 15:09:45 -05:00
|
|
|
let mut extensions = vec![
|
|
|
|
// Web APIs
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_webidl::deno_webidl::init_ops_and_esm(),
|
|
|
|
deno_console::deno_console::init_ops_and_esm(),
|
|
|
|
deno_url::deno_url::init_ops_and_esm(),
|
|
|
|
deno_web::deno_web::init_ops_and_esm::<PermissionsContainer>(
|
2023-03-09 15:09:45 -05:00
|
|
|
options.blob_store.clone(),
|
|
|
|
options.bootstrap.location.clone(),
|
|
|
|
),
|
2023-12-08 19:19:16 -05:00
|
|
|
deno_webgpu::deno_webgpu::init_ops_and_esm(),
|
2024-01-22 06:08:01 -05:00
|
|
|
deno_canvas::deno_canvas::init_ops_and_esm(),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_fetch::deno_fetch::init_ops_and_esm::<PermissionsContainer>(
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_fetch::Options {
|
|
|
|
user_agent: options.bootstrap.user_agent.clone(),
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider: options.root_cert_store_provider.clone(),
|
2023-03-17 14:22:15 -04:00
|
|
|
unsafely_ignore_certificate_errors: options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
|
|
|
file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(
|
|
|
|
create_cache,
|
|
|
|
),
|
|
|
|
deno_websocket::deno_websocket::init_ops_and_esm::<PermissionsContainer>(
|
2023-03-09 15:09:45 -05:00
|
|
|
options.bootstrap.user_agent.clone(),
|
2023-05-01 16:42:05 -04:00
|
|
|
options.root_cert_store_provider.clone(),
|
2023-03-09 15:09:45 -05:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_webstorage::deno_webstorage::init_ops_and_esm(
|
2023-03-17 14:22:15 -04:00
|
|
|
options.origin_storage_dir.clone(),
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_crypto::deno_crypto::init_ops_and_esm(options.seed),
|
|
|
|
deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm(
|
2023-03-09 15:09:45 -05:00
|
|
|
options.broadcast_channel.clone(),
|
|
|
|
),
|
2023-10-04 15:42:17 -04:00
|
|
|
deno_ffi::deno_ffi::init_ops_and_esm::<PermissionsContainer>(),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_net::deno_net::init_ops_and_esm::<PermissionsContainer>(
|
2023-05-01 16:42:05 -04:00
|
|
|
options.root_cert_store_provider.clone(),
|
2023-03-09 15:09:45 -05:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_tls::deno_tls::init_ops_and_esm(),
|
|
|
|
deno_kv::deno_kv::init_ops_and_esm(
|
2023-08-22 01:56:00 -04:00
|
|
|
MultiBackendDbHandler::remote_or_sqlite::<PermissionsContainer>(
|
2023-03-22 00:13:24 -04:00
|
|
|
options.origin_storage_dir.clone(),
|
2023-10-31 07:13:57 -04:00
|
|
|
options.seed,
|
|
|
|
deno_kv::remote::HttpOptions {
|
|
|
|
user_agent: options.bootstrap.user_agent.clone(),
|
|
|
|
root_cert_store_provider: options.root_cert_store_provider.clone(),
|
|
|
|
unsafely_ignore_certificate_errors: options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
client_cert_chain_and_key: TlsKeys::Null,
|
2023-10-31 07:13:57 -04:00
|
|
|
proxy: None,
|
|
|
|
},
|
2023-03-22 00:13:24 -04:00
|
|
|
),
|
|
|
|
),
|
2023-11-01 14:57:55 -04:00
|
|
|
deno_cron::deno_cron::init_ops_and_esm(LocalCronHandler::new()),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_napi::deno_napi::init_ops_and_esm::<PermissionsContainer>(),
|
|
|
|
deno_http::deno_http::init_ops_and_esm::<DefaultHttpPropertyExtractor>(),
|
|
|
|
deno_io::deno_io::init_ops_and_esm(Some(options.stdio)),
|
|
|
|
deno_fs::deno_fs::init_ops_and_esm::<PermissionsContainer>(
|
2023-05-05 12:44:24 -04:00
|
|
|
options.fs.clone(),
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_node::deno_node::init_ops_and_esm::<PermissionsContainer>(
|
2023-03-17 14:22:15 -04:00
|
|
|
options.npm_resolver,
|
2023-05-05 12:44:24 -04:00
|
|
|
options.fs,
|
2023-03-17 14:22:15 -04:00
|
|
|
),
|
2023-03-16 13:36:53 -04:00
|
|
|
// Ops from this crate
|
2023-08-05 19:47:15 -04:00
|
|
|
ops::runtime::deno_runtime::init_ops_and_esm(main_module.clone()),
|
|
|
|
ops::worker_host::deno_worker_host::init_ops_and_esm(
|
2023-03-16 13:36:53 -04:00
|
|
|
options.create_web_worker_cb.clone(),
|
|
|
|
options.format_js_error_fn.clone(),
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
ops::fs_events::deno_fs_events::init_ops_and_esm(),
|
|
|
|
ops::os::deno_os::init_ops_and_esm(exit_code.clone()),
|
|
|
|
ops::permissions::deno_permissions::init_ops_and_esm(),
|
|
|
|
ops::process::deno_process::init_ops_and_esm(),
|
|
|
|
ops::signal::deno_signal::init_ops_and_esm(),
|
|
|
|
ops::tty::deno_tty::init_ops_and_esm(),
|
|
|
|
ops::http::deno_http_runtime::init_ops_and_esm(),
|
2024-01-22 07:37:25 -05:00
|
|
|
ops::bootstrap::deno_bootstrap::init_ops_and_esm(
|
|
|
|
if options.startup_snapshot.is_some() {
|
2023-11-20 08:00:05 -05:00
|
|
|
None
|
2024-01-22 07:37:25 -05:00
|
|
|
} else {
|
|
|
|
Some(Default::default())
|
|
|
|
},
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
deno_permissions_worker::init_ops_and_esm(
|
2023-03-17 14:22:15 -04:00
|
|
|
permissions,
|
|
|
|
enable_testing_features,
|
|
|
|
),
|
2023-08-05 19:47:15 -04:00
|
|
|
runtime::init_ops_and_esm(),
|
2024-02-05 18:58:06 -05:00
|
|
|
// NOTE(bartlomieju): this is done, just so that ops from this extension
|
|
|
|
// are available and importing them in `99_main.js` doesn't cause an
|
|
|
|
// error because they're not defined. Trying to use these ops in non-worker
|
|
|
|
// context will cause a panic.
|
|
|
|
ops::web_worker::deno_web_worker::init_ops_and_esm().disable(),
|
2023-03-09 15:09:45 -05:00
|
|
|
];
|
2023-03-08 06:43:26 -05:00
|
|
|
|
2024-01-22 07:37:25 -05:00
|
|
|
#[cfg(__runtime_js_sources)]
|
|
|
|
assert!(cfg!(not(feature = "only_snapshotted_js_sources")), "'__runtime_js_sources' is incompatible with 'only_snapshotted_js_sources'.");
|
|
|
|
|
2023-08-05 19:47:15 -04:00
|
|
|
for extension in &mut extensions {
|
2024-01-22 07:37:25 -05:00
|
|
|
if options.startup_snapshot.is_some() {
|
2023-08-05 19:47:15 -04:00
|
|
|
extension.js_files = std::borrow::Cow::Borrowed(&[]);
|
|
|
|
extension.esm_files = std::borrow::Cow::Borrowed(&[]);
|
|
|
|
extension.esm_entry_point = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 11:03:49 -04:00
|
|
|
extensions.extend(std::mem::take(&mut options.extensions));
|
2021-04-28 12:41:50 -04:00
|
|
|
|
2024-01-22 07:37:25 -05:00
|
|
|
#[cfg(feature = "only_snapshotted_js_sources")]
|
|
|
|
options.startup_snapshot.as_ref().expect("A user snapshot was not provided, even though 'only_snapshotted_js_sources' is used.");
|
2023-02-20 15:45:34 -05:00
|
|
|
|
2023-12-24 08:44:40 -05:00
|
|
|
let has_notified_of_inspector_disconnect = AtomicBool::new(false);
|
|
|
|
let wait_for_inspector_disconnect_callback = Box::new(move || {
|
|
|
|
if !has_notified_of_inspector_disconnect
|
|
|
|
.swap(true, std::sync::atomic::Ordering::SeqCst)
|
|
|
|
{
|
2024-05-08 22:45:06 -04:00
|
|
|
log::info!("Program finished. Waiting for inspector to disconnect to exit the process...");
|
2023-12-24 08:44:40 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-07 11:20:20 -04:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2020-12-11 12:49:26 -05:00
|
|
|
module_loader: Some(options.module_loader.clone()),
|
2024-01-10 10:30:50 -05:00
|
|
|
startup_snapshot: options.startup_snapshot,
|
2023-06-05 05:22:32 -04:00
|
|
|
create_params: options.create_params,
|
2022-04-15 10:08:09 -04:00
|
|
|
source_map_getter: options.source_map_getter,
|
2023-11-11 12:01:48 -05:00
|
|
|
skip_op_registration: options.skip_op_registration,
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: options.get_error_class_fn,
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store: options.shared_array_buffer_store.clone(),
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store: options.compiled_wasm_module_store.clone(),
|
2021-04-28 12:41:50 -04:00
|
|
|
extensions,
|
2024-03-04 20:17:39 -05:00
|
|
|
extension_transpiler: Some(Rc::new(|specifier, source| {
|
|
|
|
maybe_transpile_source(specifier, source)
|
|
|
|
})),
|
2022-10-26 07:37:50 -04:00
|
|
|
inspector: options.maybe_inspector_server.is_some(),
|
2022-11-26 17:09:48 -05:00
|
|
|
is_main: true,
|
2023-10-12 11:55:50 -04:00
|
|
|
feature_checker: Some(options.feature_checker.clone()),
|
2023-11-05 16:27:36 -05:00
|
|
|
op_metrics_factory_fn,
|
2023-12-24 08:44:40 -05:00
|
|
|
wait_for_inspector_disconnect_callback: Some(
|
|
|
|
wait_for_inspector_disconnect_callback,
|
|
|
|
),
|
2023-12-28 14:37:10 -05:00
|
|
|
import_meta_resolve_callback: Some(Box::new(
|
|
|
|
import_meta_resolve_callback,
|
|
|
|
)),
|
2024-01-14 20:28:46 -05:00
|
|
|
validate_import_attributes_cb: Some(Box::new(
|
|
|
|
validate_import_attributes_callback,
|
|
|
|
)),
|
2024-04-17 10:19:55 -04:00
|
|
|
enable_code_cache: options.v8_code_cache.is_some(),
|
|
|
|
eval_context_code_cache_cbs: options.v8_code_cache.map(|cache| {
|
|
|
|
let cache_clone = cache.clone();
|
|
|
|
(
|
|
|
|
Box::new(move |specifier: &str| {
|
|
|
|
Ok(get_code_cache(cache.clone(), specifier).map(Cow::Owned))
|
|
|
|
}) as Box<dyn Fn(&_) -> _>,
|
|
|
|
Box::new(move |specifier: &str, data: &[u8]| {
|
|
|
|
set_code_cache(cache_clone.clone(), specifier, data);
|
|
|
|
}) as Box<dyn Fn(&_, &_)>,
|
|
|
|
)
|
|
|
|
}),
|
2020-09-11 09:18:49 -04:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2023-11-05 16:27:36 -05:00
|
|
|
if let Some(op_summary_metrics) = op_summary_metrics {
|
|
|
|
js_runtime.op_state().borrow_mut().put(op_summary_metrics);
|
|
|
|
}
|
2024-03-13 11:40:38 -04:00
|
|
|
extern "C" fn message_handler(
|
|
|
|
_msg: v8::Local<v8::Message>,
|
|
|
|
_exception: v8::Local<v8::Value>,
|
|
|
|
) {
|
|
|
|
// TODO(@littledivy): Propogate message to users.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register message listener
|
|
|
|
js_runtime
|
|
|
|
.v8_isolate()
|
|
|
|
.add_message_listener(message_handler);
|
2023-11-05 16:27:36 -05:00
|
|
|
|
2021-06-21 13:37:51 -04:00
|
|
|
if let Some(server) = options.maybe_inspector_server.clone() {
|
2021-12-17 12:43:25 -05:00
|
|
|
server.register_inspector(
|
|
|
|
main_module.to_string(),
|
|
|
|
&mut js_runtime,
|
2022-12-12 09:33:30 -05:00
|
|
|
options.should_break_on_first_statement
|
|
|
|
|| options.should_wait_for_inspector_session,
|
2021-12-17 12:43:25 -05:00
|
|
|
);
|
2022-11-28 15:59:36 -05:00
|
|
|
|
|
|
|
// Put inspector handle into the op state so we can put a breakpoint when
|
|
|
|
// executing a CJS entrypoint.
|
|
|
|
let op_state = js_runtime.op_state();
|
|
|
|
let inspector = js_runtime.inspector();
|
|
|
|
op_state.borrow_mut().put(inspector);
|
2021-05-26 15:07:12 -04:00
|
|
|
}
|
2024-04-15 14:08:33 -04:00
|
|
|
let (
|
|
|
|
bootstrap_fn_global,
|
|
|
|
dispatch_load_event_fn_global,
|
|
|
|
dispatch_beforeunload_event_fn_global,
|
|
|
|
dispatch_unload_event_fn_global,
|
2024-04-16 09:45:41 -04:00
|
|
|
dispatch_process_beforeexit_event_fn_global,
|
|
|
|
dispatch_process_exit_event_fn_global,
|
2024-04-15 14:08:33 -04:00
|
|
|
) = {
|
2023-07-23 09:42:41 -04:00
|
|
|
let context = js_runtime.main_context();
|
2023-02-21 19:55:31 -05:00
|
|
|
let scope = &mut js_runtime.handle_scope();
|
|
|
|
let context_local = v8::Local::new(scope, context);
|
|
|
|
let global_obj = context_local.global(scope);
|
2023-03-06 11:37:46 -05:00
|
|
|
let bootstrap_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"bootstrap").unwrap();
|
2023-02-21 19:55:31 -05:00
|
|
|
let bootstrap_ns: v8::Local<v8::Object> = global_obj
|
|
|
|
.get(scope, bootstrap_str.into())
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2023-03-06 11:37:46 -05:00
|
|
|
let main_runtime_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"mainRuntime").unwrap();
|
2023-02-21 19:55:31 -05:00
|
|
|
let bootstrap_fn =
|
|
|
|
bootstrap_ns.get(scope, main_runtime_str.into()).unwrap();
|
|
|
|
let bootstrap_fn =
|
|
|
|
v8::Local::<v8::Function>::try_from(bootstrap_fn).unwrap();
|
2024-04-15 14:08:33 -04:00
|
|
|
let dispatch_load_event_fn_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"dispatchLoadEvent")
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_load_event_fn = bootstrap_ns
|
|
|
|
.get(scope, dispatch_load_event_fn_str.into())
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_load_event_fn =
|
|
|
|
v8::Local::<v8::Function>::try_from(dispatch_load_event_fn).unwrap();
|
|
|
|
let dispatch_beforeunload_event_fn_str =
|
|
|
|
v8::String::new_external_onebyte_static(
|
|
|
|
scope,
|
|
|
|
b"dispatchBeforeUnloadEvent",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_beforeunload_event_fn = bootstrap_ns
|
|
|
|
.get(scope, dispatch_beforeunload_event_fn_str.into())
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_beforeunload_event_fn =
|
|
|
|
v8::Local::<v8::Function>::try_from(dispatch_beforeunload_event_fn)
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_unload_event_fn_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"dispatchUnloadEvent")
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_unload_event_fn = bootstrap_ns
|
|
|
|
.get(scope, dispatch_unload_event_fn_str.into())
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_unload_event_fn =
|
|
|
|
v8::Local::<v8::Function>::try_from(dispatch_unload_event_fn).unwrap();
|
2024-04-16 09:45:41 -04:00
|
|
|
let dispatch_process_beforeexit_event =
|
|
|
|
v8::String::new_external_onebyte_static(
|
|
|
|
scope,
|
|
|
|
b"dispatchProcessBeforeExitEvent",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_process_beforeexit_event_fn = bootstrap_ns
|
|
|
|
.get(scope, dispatch_process_beforeexit_event.into())
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_process_beforeexit_event_fn =
|
|
|
|
v8::Local::<v8::Function>::try_from(
|
|
|
|
dispatch_process_beforeexit_event_fn,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_process_exit_event =
|
|
|
|
v8::String::new_external_onebyte_static(
|
|
|
|
scope,
|
|
|
|
b"dispatchProcessExitEvent",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_process_exit_event_fn = bootstrap_ns
|
|
|
|
.get(scope, dispatch_process_exit_event.into())
|
|
|
|
.unwrap();
|
|
|
|
let dispatch_process_exit_event_fn =
|
|
|
|
v8::Local::<v8::Function>::try_from(dispatch_process_exit_event_fn)
|
|
|
|
.unwrap();
|
2024-04-15 14:08:33 -04:00
|
|
|
(
|
|
|
|
v8::Global::new(scope, bootstrap_fn),
|
|
|
|
v8::Global::new(scope, dispatch_load_event_fn),
|
|
|
|
v8::Global::new(scope, dispatch_beforeunload_event_fn),
|
|
|
|
v8::Global::new(scope, dispatch_unload_event_fn),
|
2024-04-16 09:45:41 -04:00
|
|
|
v8::Global::new(scope, dispatch_process_beforeexit_event_fn),
|
|
|
|
v8::Global::new(scope, dispatch_process_exit_event_fn),
|
2024-04-15 14:08:33 -04:00
|
|
|
)
|
2023-02-21 19:55:31 -05:00
|
|
|
};
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
Self {
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime,
|
2021-06-21 13:37:51 -04:00
|
|
|
should_break_on_first_statement: options.should_break_on_first_statement,
|
2022-12-12 09:33:30 -05:00
|
|
|
should_wait_for_inspector_session: options
|
|
|
|
.should_wait_for_inspector_session,
|
2022-06-08 11:45:38 -04:00
|
|
|
exit_code,
|
2023-02-21 19:55:31 -05:00
|
|
|
bootstrap_fn_global: Some(bootstrap_fn_global),
|
2024-04-15 14:08:33 -04:00
|
|
|
dispatch_load_event_fn_global,
|
|
|
|
dispatch_beforeunload_event_fn_global,
|
|
|
|
dispatch_unload_event_fn_global,
|
2024-04-16 09:45:41 -04:00
|
|
|
dispatch_process_beforeexit_event_fn_global,
|
|
|
|
dispatch_process_exit_event_fn_global,
|
2020-06-01 22:44:17 -04:00
|
|
|
}
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
|
|
|
|
2023-11-12 23:52:59 -05:00
|
|
|
pub fn bootstrap(&mut self, options: BootstrapOptions) {
|
2023-12-15 05:50:05 -05:00
|
|
|
// Setup bootstrap options for ops.
|
|
|
|
{
|
|
|
|
let op_state = self.js_runtime.op_state();
|
|
|
|
let mut state = op_state.borrow_mut();
|
|
|
|
state.put(options.clone());
|
|
|
|
if let Some(node_ipc_fd) = options.node_ipc_fd {
|
|
|
|
state.put(deno_node::ChildPipeFd(node_ipc_fd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 19:55:31 -05:00
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
2024-03-13 23:23:37 -04:00
|
|
|
let scope = &mut v8::TryCatch::new(scope);
|
2023-03-28 04:27:17 -04:00
|
|
|
let args = options.as_v8(scope);
|
2023-02-21 19:55:31 -05:00
|
|
|
let bootstrap_fn = self.bootstrap_fn_global.take().unwrap();
|
|
|
|
let bootstrap_fn = v8::Local::new(scope, bootstrap_fn);
|
|
|
|
let undefined = v8::undefined(scope);
|
2024-03-13 23:23:37 -04:00
|
|
|
bootstrap_fn.call(scope, undefined.into(), &[args]);
|
|
|
|
if let Some(exception) = scope.exception() {
|
|
|
|
let error = JsError::from_v8_exception(scope, exception);
|
|
|
|
panic!("Bootstrap exception: {error}");
|
|
|
|
}
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-09-28 06:14:11 -04:00
|
|
|
|
2021-06-21 19:45:41 -04:00
|
|
|
/// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script)
|
2023-04-04 08:46:31 -04:00
|
|
|
pub fn execute_script(
|
2021-06-21 19:45:41 -04:00
|
|
|
&mut self,
|
2023-03-21 18:33:12 -04:00
|
|
|
script_name: &'static str,
|
2024-01-09 23:18:40 -05:00
|
|
|
source_code: ModuleCodeString,
|
2022-12-18 15:34:33 -05:00
|
|
|
) -> Result<v8::Global<v8::Value>, AnyError> {
|
|
|
|
self.js_runtime.execute_script(script_name, source_code)
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2022-08-10 18:10:51 -04:00
|
|
|
/// Loads and instantiates specified JavaScript module as "main" module.
|
|
|
|
pub async fn preload_main_module(
|
2020-11-26 09:17:45 -05:00
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleId, AnyError> {
|
2024-03-04 20:17:39 -05:00
|
|
|
self.js_runtime.load_main_es_module(module_specifier).await
|
2022-08-10 18:10:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads and instantiates specified JavaScript module as "side" module.
|
|
|
|
pub async fn preload_side_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleId, AnyError> {
|
2024-03-04 20:17:39 -05:00
|
|
|
self.js_runtime.load_side_es_module(module_specifier).await
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2022-06-19 17:29:48 -04:00
|
|
|
/// Executes specified JavaScript module.
|
|
|
|
pub async fn evaluate_module(
|
|
|
|
&mut self,
|
|
|
|
id: ModuleId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 18:10:51 -04:00
|
|
|
self.wait_for_inspector_session();
|
2021-03-04 07:19:47 -05:00
|
|
|
let mut receiver = self.js_runtime.mod_evaluate(id);
|
|
|
|
tokio::select! {
|
2022-04-13 05:50:57 -04:00
|
|
|
// Not using biased mode leads to non-determinism for relatively simple
|
|
|
|
// programs.
|
|
|
|
biased;
|
|
|
|
|
2021-07-30 07:36:43 -04:00
|
|
|
maybe_result = &mut receiver => {
|
2021-03-04 07:19:47 -05:00
|
|
|
debug!("received module evaluate {:#?}", maybe_result);
|
2023-11-27 18:01:27 -05:00
|
|
|
maybe_result
|
2021-03-04 07:19:47 -05:00
|
|
|
}
|
|
|
|
|
2021-05-26 15:07:12 -04:00
|
|
|
event_loop_result = self.run_event_loop(false) => {
|
2021-03-04 07:19:47 -05:00
|
|
|
event_loop_result?;
|
2023-11-27 18:01:27 -05:00
|
|
|
receiver.await
|
2021-03-04 07:19:47 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2023-12-05 11:26:06 -05:00
|
|
|
/// Run the event loop up to a given duration. If the runtime resolves early, returns
|
|
|
|
/// early. Will always poll the runtime at least once.
|
|
|
|
pub async fn run_up_to_duration(
|
|
|
|
&mut self,
|
|
|
|
duration: Duration,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
match tokio::time::timeout(
|
|
|
|
duration,
|
2023-12-13 10:07:26 -05:00
|
|
|
self
|
|
|
|
.js_runtime
|
|
|
|
.run_event_loop(PollEventLoopOptions::default()),
|
2023-12-05 11:26:06 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(Ok(_)) => Ok(()),
|
|
|
|
Err(_) => Ok(()),
|
|
|
|
Ok(Err(e)) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:44:53 -04:00
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
pub async fn execute_side_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 18:10:51 -04:00
|
|
|
let id = self.preload_side_module(module_specifier).await?;
|
2021-09-17 21:44:53 -04:00
|
|
|
self.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
///
|
|
|
|
/// This module will have "import.meta.main" equal to true.
|
|
|
|
pub async fn execute_main_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 18:10:51 -04:00
|
|
|
let id = self.preload_main_module(module_specifier).await?;
|
2021-09-17 21:44:53 -04:00
|
|
|
self.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
fn wait_for_inspector_session(&mut self) {
|
|
|
|
if self.should_break_on_first_statement {
|
|
|
|
self
|
2021-05-26 15:07:12 -04:00
|
|
|
.js_runtime
|
|
|
|
.inspector()
|
2022-09-02 06:43:39 -04:00
|
|
|
.borrow_mut()
|
2022-12-12 09:33:30 -05:00
|
|
|
.wait_for_session_and_break_on_next_statement();
|
|
|
|
} else if self.should_wait_for_inspector_session {
|
|
|
|
self.js_runtime.inspector().borrow_mut().wait_for_session();
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
/// Create new inspector session. This function panics if Worker
|
|
|
|
/// was not configured to create inspector.
|
2024-03-11 23:48:00 -04:00
|
|
|
pub fn create_inspector_session(&mut self) -> LocalInspectorSession {
|
2022-10-26 07:37:50 -04:00
|
|
|
self.js_runtime.maybe_init_inspector();
|
2022-09-02 06:43:39 -04:00
|
|
|
self.js_runtime.inspector().borrow().create_local_session()
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2021-05-26 15:07:12 -04:00
|
|
|
pub async fn run_event_loop(
|
|
|
|
&mut self,
|
|
|
|
wait_for_inspector: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-11-26 18:09:04 -05:00
|
|
|
self
|
|
|
|
.js_runtime
|
2023-12-13 10:07:26 -05:00
|
|
|
.run_event_loop(deno_core::PollEventLoopOptions {
|
2023-11-26 18:09:04 -05:00
|
|
|
wait_for_inspector,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
2020-10-11 07:20:40 -04:00
|
|
|
}
|
2021-05-26 11:47:33 -04:00
|
|
|
|
2021-12-11 09:56:45 -05:00
|
|
|
/// Return exit code set by the executed code (either in main worker
|
|
|
|
/// or one of child web workers).
|
2023-01-05 14:29:50 -05:00
|
|
|
pub fn exit_code(&self) -> i32 {
|
2022-06-08 11:45:38 -04:00
|
|
|
self.exit_code.get()
|
2021-12-11 09:56:45 -05:00
|
|
|
}
|
2021-12-21 09:49:27 -05:00
|
|
|
|
|
|
|
/// Dispatches "load" event to the JavaScript runtime.
|
|
|
|
///
|
|
|
|
/// Does not poll event loop, and thus not await any of the "load" event handlers.
|
2024-04-15 14:08:33 -04:00
|
|
|
pub fn dispatch_load_event(&mut self) -> Result<(), AnyError> {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
|
|
let dispatch_load_event_fn =
|
|
|
|
v8::Local::new(tc_scope, &self.dispatch_load_event_fn_global);
|
|
|
|
let undefined = v8::undefined(tc_scope);
|
|
|
|
dispatch_load_event_fn.call(tc_scope, undefined.into(), &[]);
|
|
|
|
if let Some(exception) = tc_scope.exception() {
|
|
|
|
let error = JsError::from_v8_exception(tc_scope, exception);
|
|
|
|
return Err(error.into());
|
|
|
|
}
|
2022-12-18 15:34:33 -05:00
|
|
|
Ok(())
|
2021-12-21 09:49:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dispatches "unload" event to the JavaScript runtime.
|
|
|
|
///
|
|
|
|
/// Does not poll event loop, and thus not await any of the "unload" event handlers.
|
2024-04-15 14:08:33 -04:00
|
|
|
pub fn dispatch_unload_event(&mut self) -> Result<(), AnyError> {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
|
|
let dispatch_unload_event_fn =
|
|
|
|
v8::Local::new(tc_scope, &self.dispatch_unload_event_fn_global);
|
|
|
|
let undefined = v8::undefined(tc_scope);
|
|
|
|
dispatch_unload_event_fn.call(tc_scope, undefined.into(), &[]);
|
|
|
|
if let Some(exception) = tc_scope.exception() {
|
|
|
|
let error = JsError::from_v8_exception(tc_scope, exception);
|
|
|
|
return Err(error.into());
|
|
|
|
}
|
2022-12-18 15:34:33 -05:00
|
|
|
Ok(())
|
2021-12-21 09:49:27 -05:00
|
|
|
}
|
2022-06-28 10:49:30 -04:00
|
|
|
|
2024-04-16 09:45:41 -04:00
|
|
|
/// Dispatches process.emit("exit") event for node compat.
|
|
|
|
pub fn dispatch_process_exit_event(&mut self) -> Result<(), AnyError> {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
|
|
let dispatch_process_exit_event_fn =
|
|
|
|
v8::Local::new(tc_scope, &self.dispatch_process_exit_event_fn_global);
|
|
|
|
let undefined = v8::undefined(tc_scope);
|
|
|
|
dispatch_process_exit_event_fn.call(tc_scope, undefined.into(), &[]);
|
|
|
|
if let Some(exception) = tc_scope.exception() {
|
|
|
|
let error = JsError::from_v8_exception(tc_scope, exception);
|
|
|
|
return Err(error.into());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:49:30 -04:00
|
|
|
/// Dispatches "beforeunload" event to the JavaScript runtime. Returns a boolean
|
|
|
|
/// indicating if the event was prevented and thus event loop should continue
|
|
|
|
/// running.
|
2024-04-15 14:08:33 -04:00
|
|
|
pub fn dispatch_beforeunload_event(&mut self) -> Result<bool, AnyError> {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
|
|
let dispatch_beforeunload_event_fn =
|
|
|
|
v8::Local::new(tc_scope, &self.dispatch_beforeunload_event_fn_global);
|
|
|
|
let undefined = v8::undefined(tc_scope);
|
|
|
|
let ret_val =
|
|
|
|
dispatch_beforeunload_event_fn.call(tc_scope, undefined.into(), &[]);
|
|
|
|
if let Some(exception) = tc_scope.exception() {
|
|
|
|
let error = JsError::from_v8_exception(tc_scope, exception);
|
|
|
|
return Err(error.into());
|
|
|
|
}
|
|
|
|
let ret_val = ret_val.unwrap();
|
|
|
|
Ok(ret_val.is_false())
|
2022-06-28 10:49:30 -04:00
|
|
|
}
|
2024-04-16 09:45:41 -04:00
|
|
|
|
|
|
|
/// Dispatches process.emit("beforeExit") event for node compat.
|
|
|
|
pub fn dispatch_process_beforeexit_event(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<bool, AnyError> {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
|
|
let dispatch_process_beforeexit_event_fn = v8::Local::new(
|
|
|
|
tc_scope,
|
|
|
|
&self.dispatch_process_beforeexit_event_fn_global,
|
|
|
|
);
|
|
|
|
let undefined = v8::undefined(tc_scope);
|
|
|
|
let ret_val = dispatch_process_beforeexit_event_fn.call(
|
|
|
|
tc_scope,
|
|
|
|
undefined.into(),
|
|
|
|
&[],
|
|
|
|
);
|
|
|
|
if let Some(exception) = tc_scope.exception() {
|
|
|
|
let error = JsError::from_v8_exception(tc_scope, exception);
|
|
|
|
return Err(error.into());
|
|
|
|
}
|
|
|
|
let ret_val = ret_val.unwrap();
|
|
|
|
Ok(ret_val.is_true())
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
}
|