2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-10-15 17:19:03 -04:00
|
|
|
|
2023-03-28 04:27:17 -04:00
|
|
|
use deno_core::v8;
|
2021-10-05 16:41:14 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-08-15 15:36:36 -04:00
|
|
|
use serde::Serialize;
|
|
|
|
use std::cell::RefCell;
|
2022-10-15 17:19:03 -04:00
|
|
|
use std::thread;
|
|
|
|
|
2024-02-07 11:25:14 -05:00
|
|
|
use deno_terminal::colors;
|
2021-10-05 16:41:14 -04:00
|
|
|
|
2024-04-24 15:45:49 -04:00
|
|
|
/// The execution mode for this worker. Some modes may have implicit behaviour.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum WorkerExecutionMode {
|
|
|
|
/// No special behaviour.
|
|
|
|
None,
|
|
|
|
|
|
|
|
/// Running in a worker.
|
|
|
|
Worker,
|
|
|
|
/// `deno run`
|
|
|
|
Run,
|
|
|
|
/// `deno repl`
|
|
|
|
Repl,
|
|
|
|
/// `deno eval`
|
|
|
|
Eval,
|
|
|
|
/// `deno test`
|
|
|
|
Test,
|
|
|
|
/// `deno bench`
|
|
|
|
Bench,
|
|
|
|
/// `deno serve`
|
2024-08-14 18:26:21 -04:00
|
|
|
Serve {
|
|
|
|
is_main: bool,
|
|
|
|
worker_count: Option<usize>,
|
|
|
|
},
|
2024-04-24 15:45:49 -04:00
|
|
|
/// `deno jupyter`
|
|
|
|
Jupyter,
|
|
|
|
}
|
|
|
|
|
2024-08-14 18:26:21 -04:00
|
|
|
impl WorkerExecutionMode {
|
|
|
|
pub fn discriminant(&self) -> u8 {
|
|
|
|
match self {
|
|
|
|
WorkerExecutionMode::None => 0,
|
|
|
|
WorkerExecutionMode::Worker => 1,
|
|
|
|
WorkerExecutionMode::Run => 2,
|
|
|
|
WorkerExecutionMode::Repl => 3,
|
|
|
|
WorkerExecutionMode::Eval => 4,
|
|
|
|
WorkerExecutionMode::Test => 5,
|
|
|
|
WorkerExecutionMode::Bench => 6,
|
|
|
|
WorkerExecutionMode::Serve { .. } => 7,
|
|
|
|
WorkerExecutionMode::Jupyter => 8,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn serve_info(&self) -> (Option<bool>, Option<usize>) {
|
|
|
|
match *self {
|
|
|
|
WorkerExecutionMode::Serve {
|
|
|
|
is_main,
|
|
|
|
worker_count,
|
|
|
|
} => (Some(is_main), worker_count),
|
|
|
|
_ => (None, None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 11:34:50 -04:00
|
|
|
/// The log level to use when printing diagnostic log messages, warnings,
|
|
|
|
/// or errors in the worker.
|
|
|
|
///
|
|
|
|
/// Note: This is disconnected with the log crate's log level and the Rust code
|
|
|
|
/// in this crate will respect that value instead. To specify that, use
|
|
|
|
/// `log::set_max_level`.
|
|
|
|
#[derive(Debug, Default, Clone, Copy)]
|
|
|
|
pub enum WorkerLogLevel {
|
|
|
|
// WARNING: Ensure this is kept in sync with
|
|
|
|
// the JS values (search for LogLevel).
|
|
|
|
Error = 1,
|
|
|
|
Warn = 2,
|
|
|
|
#[default]
|
|
|
|
Info = 3,
|
|
|
|
Debug = 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<log::Level> for WorkerLogLevel {
|
|
|
|
fn from(value: log::Level) -> Self {
|
|
|
|
match value {
|
|
|
|
log::Level::Error => WorkerLogLevel::Error,
|
|
|
|
log::Level::Warn => WorkerLogLevel::Warn,
|
|
|
|
log::Level::Info => WorkerLogLevel::Info,
|
|
|
|
log::Level::Debug => WorkerLogLevel::Debug,
|
|
|
|
log::Level::Trace => WorkerLogLevel::Debug,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:41:14 -04:00
|
|
|
/// Common bootstrap options for MainWorker & WebWorker
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BootstrapOptions {
|
2024-08-15 17:47:16 -04:00
|
|
|
pub deno_version: String,
|
2021-10-05 16:41:14 -04:00
|
|
|
/// Sets `Deno.args` in JS runtime.
|
|
|
|
pub args: Vec<String>,
|
|
|
|
pub cpu_count: usize,
|
2023-05-30 11:34:50 -04:00
|
|
|
pub log_level: WorkerLogLevel,
|
2023-11-05 16:27:36 -05:00
|
|
|
pub enable_op_summary_metrics: bool,
|
2021-10-05 16:41:14 -04:00
|
|
|
pub enable_testing_features: bool,
|
2022-10-18 09:33:35 -04:00
|
|
|
pub locale: String,
|
2021-10-05 16:41:14 -04:00
|
|
|
pub location: Option<ModuleSpecifier>,
|
|
|
|
/// Sets `Deno.noColor` in JS runtime.
|
|
|
|
pub no_color: bool,
|
2024-05-14 17:32:09 -04:00
|
|
|
pub is_stdout_tty: bool,
|
|
|
|
pub is_stderr_tty: bool,
|
2024-07-19 06:39:05 -04:00
|
|
|
pub color_level: deno_terminal::colors::ColorLevel,
|
2023-11-01 18:15:08 -04:00
|
|
|
// --unstable-* flags
|
|
|
|
pub unstable_features: Vec<i32>,
|
2022-05-14 06:00:02 -04:00
|
|
|
pub user_agent: String,
|
2022-09-17 06:04:43 -04:00
|
|
|
pub inspect: bool,
|
2023-08-15 15:36:36 -04:00
|
|
|
pub has_node_modules_dir: bool,
|
2024-02-23 11:30:29 -05:00
|
|
|
pub argv0: Option<String>,
|
2024-05-05 10:16:02 -04:00
|
|
|
pub node_debug: Option<String>,
|
2023-12-19 07:37:22 -05:00
|
|
|
pub node_ipc_fd: Option<i64>,
|
2024-04-24 15:45:49 -04:00
|
|
|
pub mode: WorkerExecutionMode,
|
|
|
|
// Used by `deno serve`
|
2024-05-17 01:38:50 -04:00
|
|
|
pub serve_port: Option<u16>,
|
2024-04-24 15:45:49 -04:00
|
|
|
pub serve_host: Option<String>,
|
2021-10-05 16:41:14 -04:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:19:03 -04:00
|
|
|
impl Default for BootstrapOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
let cpu_count = thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1);
|
|
|
|
|
2023-11-15 07:25:55 -05:00
|
|
|
let runtime_version = env!("CARGO_PKG_VERSION");
|
2023-01-27 10:43:16 -05:00
|
|
|
let user_agent = format!("Deno/{runtime_version}");
|
2022-10-15 17:19:03 -04:00
|
|
|
|
|
|
|
Self {
|
2024-08-15 17:47:16 -04:00
|
|
|
deno_version: runtime_version.to_string(),
|
2022-10-15 17:19:03 -04:00
|
|
|
user_agent,
|
|
|
|
cpu_count,
|
|
|
|
no_color: !colors::use_color(),
|
2024-05-14 17:32:09 -04:00
|
|
|
is_stdout_tty: deno_terminal::is_stdout_tty(),
|
|
|
|
is_stderr_tty: deno_terminal::is_stderr_tty(),
|
2024-07-19 06:39:05 -04:00
|
|
|
color_level: colors::get_color_level(),
|
2023-11-05 16:27:36 -05:00
|
|
|
enable_op_summary_metrics: Default::default(),
|
2022-10-15 17:19:03 -04:00
|
|
|
enable_testing_features: Default::default(),
|
2023-05-30 11:34:50 -04:00
|
|
|
log_level: Default::default(),
|
2022-10-28 11:23:05 -04:00
|
|
|
locale: "en".to_string(),
|
2022-10-15 17:19:03 -04:00
|
|
|
location: Default::default(),
|
2023-11-01 18:15:08 -04:00
|
|
|
unstable_features: Default::default(),
|
2022-10-15 17:19:03 -04:00
|
|
|
inspect: Default::default(),
|
|
|
|
args: Default::default(),
|
2023-08-15 15:36:36 -04:00
|
|
|
has_node_modules_dir: Default::default(),
|
2024-02-23 11:30:29 -05:00
|
|
|
argv0: None,
|
2024-05-05 10:16:02 -04:00
|
|
|
node_debug: None,
|
2023-12-13 05:14:16 -05:00
|
|
|
node_ipc_fd: None,
|
2024-04-24 15:45:49 -04:00
|
|
|
mode: WorkerExecutionMode::None,
|
|
|
|
serve_port: Default::default(),
|
|
|
|
serve_host: Default::default(),
|
2022-10-15 17:19:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
/// This is a struct that we use to serialize the contents of the `BootstrapOptions`
|
|
|
|
/// struct above to a V8 form. While `serde_v8` is not as fast as hand-coding this,
|
|
|
|
/// it's "fast enough" while serializing a large tuple like this that it doesn't appear
|
|
|
|
/// on flamegraphs.
|
|
|
|
///
|
|
|
|
/// Note that a few fields in here are derived from the process and environment and
|
|
|
|
/// are not sourced from the underlying `BootstrapOptions`.
|
|
|
|
///
|
|
|
|
/// Keep this in sync with `99_main.js`.
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct BootstrapV8<'a>(
|
2024-08-15 17:47:16 -04:00
|
|
|
// deno version
|
|
|
|
&'a str,
|
2023-08-15 15:36:36 -04:00
|
|
|
// location
|
|
|
|
Option<&'a str>,
|
2023-11-01 18:15:08 -04:00
|
|
|
// granular unstable flags
|
|
|
|
&'a [i32],
|
2023-08-15 15:36:36 -04:00
|
|
|
// inspect
|
|
|
|
bool,
|
|
|
|
// enable_testing_features
|
|
|
|
bool,
|
|
|
|
// has_node_modules_dir
|
|
|
|
bool,
|
2024-02-23 11:30:29 -05:00
|
|
|
// argv0
|
2023-08-15 15:36:36 -04:00
|
|
|
Option<&'a str>,
|
2024-05-05 10:16:02 -04:00
|
|
|
// node_debug
|
|
|
|
Option<&'a str>,
|
2024-04-24 15:45:49 -04:00
|
|
|
// mode
|
|
|
|
i32,
|
|
|
|
// serve port
|
|
|
|
u16,
|
|
|
|
// serve host
|
|
|
|
Option<&'a str>,
|
2024-08-14 18:26:21 -04:00
|
|
|
// serve is main
|
|
|
|
Option<bool>,
|
|
|
|
// serve worker count
|
|
|
|
Option<usize>,
|
2023-08-15 15:36:36 -04:00
|
|
|
);
|
|
|
|
|
2021-10-05 16:41:14 -04:00
|
|
|
impl BootstrapOptions {
|
2023-08-15 15:36:36 -04:00
|
|
|
/// Return the v8 equivalent of this structure.
|
2023-03-28 04:27:17 -04:00
|
|
|
pub fn as_v8<'s>(
|
|
|
|
&self,
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
2023-08-15 15:36:36 -04:00
|
|
|
) -> v8::Local<'s, v8::Value> {
|
|
|
|
let scope = RefCell::new(scope);
|
|
|
|
let ser = deno_core::serde_v8::Serializer::new(&scope);
|
|
|
|
|
2024-08-14 18:26:21 -04:00
|
|
|
let (serve_is_main, serve_worker_count) = self.mode.serve_info();
|
2023-08-15 15:36:36 -04:00
|
|
|
let bootstrap = BootstrapV8(
|
2024-08-15 17:47:16 -04:00
|
|
|
&self.deno_version,
|
2023-08-15 15:36:36 -04:00
|
|
|
self.location.as_ref().map(|l| l.as_str()),
|
2023-11-01 18:15:08 -04:00
|
|
|
self.unstable_features.as_ref(),
|
2023-08-15 15:36:36 -04:00
|
|
|
self.inspect,
|
|
|
|
self.enable_testing_features,
|
|
|
|
self.has_node_modules_dir,
|
2024-02-23 11:30:29 -05:00
|
|
|
self.argv0.as_deref(),
|
2024-05-05 10:16:02 -04:00
|
|
|
self.node_debug.as_deref(),
|
2024-08-14 18:26:21 -04:00
|
|
|
self.mode.discriminant() as _,
|
2024-05-17 01:38:50 -04:00
|
|
|
self.serve_port.unwrap_or_default(),
|
2024-04-24 15:45:49 -04:00
|
|
|
self.serve_host.as_deref(),
|
2024-08-14 18:26:21 -04:00
|
|
|
serve_is_main,
|
|
|
|
serve_worker_count,
|
2023-08-15 15:36:36 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
bootstrap.serialize(ser).unwrap()
|
2021-10-05 16:41:14 -04:00
|
|
|
}
|
|
|
|
}
|