2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-10-15 17:19:03 -04:00
|
|
|
|
2021-10-05 16:41:14 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2022-10-15 17:19:03 -04:00
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
use crate::colors;
|
|
|
|
use crate::ops::runtime::ppid;
|
2021-10-05 16:41:14 -04:00
|
|
|
|
|
|
|
/// Common bootstrap options for MainWorker & WebWorker
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BootstrapOptions {
|
|
|
|
/// Sets `Deno.args` in JS runtime.
|
|
|
|
pub args: Vec<String>,
|
|
|
|
pub cpu_count: usize,
|
|
|
|
pub debug_flag: bool,
|
|
|
|
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,
|
2022-02-28 22:37:50 -05:00
|
|
|
pub is_tty: bool,
|
2021-10-05 16:41:14 -04:00
|
|
|
/// Sets `Deno.version.deno` in JS runtime.
|
|
|
|
pub runtime_version: String,
|
|
|
|
/// Sets `Deno.version.typescript` in JS runtime.
|
|
|
|
pub ts_version: String,
|
|
|
|
pub unstable: bool,
|
2022-05-14 06:00:02 -04:00
|
|
|
pub user_agent: String,
|
2022-09-17 06:04:43 -04:00
|
|
|
pub inspect: bool,
|
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);
|
|
|
|
|
|
|
|
let runtime_version = env!("CARGO_PKG_VERSION").into();
|
2023-01-27 10:43:16 -05:00
|
|
|
let user_agent = format!("Deno/{runtime_version}");
|
2022-10-15 17:19:03 -04:00
|
|
|
|
|
|
|
Self {
|
|
|
|
runtime_version,
|
|
|
|
user_agent,
|
|
|
|
cpu_count,
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
is_tty: colors::is_tty(),
|
|
|
|
enable_testing_features: Default::default(),
|
|
|
|
debug_flag: Default::default(),
|
|
|
|
ts_version: 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(),
|
|
|
|
unstable: Default::default(),
|
|
|
|
inspect: Default::default(),
|
|
|
|
args: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:41:14 -04:00
|
|
|
impl BootstrapOptions {
|
|
|
|
pub fn as_json(&self) -> String {
|
|
|
|
let payload = json!({
|
|
|
|
// Shared bootstrap args
|
|
|
|
"args": self.args,
|
|
|
|
"cpuCount": self.cpu_count,
|
|
|
|
"debugFlag": self.debug_flag,
|
|
|
|
"denoVersion": self.runtime_version,
|
2022-10-18 09:33:35 -04:00
|
|
|
"locale": self.locale,
|
2021-10-05 16:41:14 -04:00
|
|
|
"location": self.location,
|
|
|
|
"noColor": self.no_color,
|
2022-02-28 22:37:50 -05:00
|
|
|
"isTty": self.is_tty,
|
2021-10-05 16:41:14 -04:00
|
|
|
"tsVersion": self.ts_version,
|
|
|
|
"unstableFlag": self.unstable,
|
|
|
|
// Web worker only
|
|
|
|
"enableTestingFeaturesFlag": self.enable_testing_features,
|
|
|
|
// Env values
|
|
|
|
"pid": std::process::id(),
|
|
|
|
"ppid": ppid(),
|
|
|
|
"target": env!("TARGET"),
|
|
|
|
"v8Version": deno_core::v8_version(),
|
2022-05-14 06:00:02 -04:00
|
|
|
"userAgent": self.user_agent,
|
2022-09-17 06:04:43 -04:00
|
|
|
"inspectFlag": self.inspect,
|
2021-10-05 16:41:14 -04:00
|
|
|
});
|
|
|
|
serde_json::to_string_pretty(&payload).unwrap()
|
|
|
|
}
|
|
|
|
}
|