2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-01-07 21:08:51 -05:00
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
use crate::colors;
|
2021-09-13 00:19:23 -04:00
|
|
|
use crate::file_fetcher::get_source_from_data_url;
|
2021-06-07 08:02:53 -04:00
|
|
|
use crate::flags::Flags;
|
|
|
|
use crate::ops;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2020-12-11 18:36:18 -05:00
|
|
|
use crate::version;
|
2022-02-24 08:28:00 -05:00
|
|
|
use crate::ImportMapResolver;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::FutureExt;
|
2021-06-21 19:45:41 -04:00
|
|
|
use deno_core::located_script_name;
|
2021-01-04 18:15:52 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::serde_json;
|
2021-01-07 21:08:51 -05:00
|
|
|
use deno_core::url::Url;
|
2021-01-04 18:15:52 -05:00
|
|
|
use deno_core::v8_set_flags;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::ModuleLoader;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2022-02-24 08:28:00 -05:00
|
|
|
use deno_graph::source::Resolver;
|
2021-05-22 12:08:24 -04:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
2021-12-01 11:13:11 -05:00
|
|
|
use deno_runtime::deno_tls::create_default_root_cert_store;
|
2021-12-06 18:48:11 -05:00
|
|
|
use deno_runtime::deno_tls::rustls_pemfile;
|
2021-07-05 09:34:37 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-01-07 21:08:51 -05:00
|
|
|
use deno_runtime::permissions::PermissionsOptions;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
2021-10-05 16:41:14 -04:00
|
|
|
use deno_runtime::BootstrapOptions;
|
2022-02-24 08:28:00 -05:00
|
|
|
use import_map::parse_from_json;
|
2021-01-07 21:08:51 -05:00
|
|
|
use log::Level;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::env::current_exe;
|
2021-08-07 08:49:38 -04:00
|
|
|
use std::io::BufReader;
|
|
|
|
use std::io::Cursor;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::io::SeekFrom;
|
2021-01-04 18:15:52 -05:00
|
|
|
use std::iter::once;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
2020-12-11 12:49:26 -05:00
|
|
|
use std::sync::Arc;
|
2022-02-15 07:33:46 -05:00
|
|
|
use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2021-01-04 18:15:52 -05:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2021-01-07 21:08:51 -05:00
|
|
|
pub struct Metadata {
|
|
|
|
pub argv: Vec<String>,
|
|
|
|
pub unstable: bool,
|
|
|
|
pub seed: Option<u64>,
|
|
|
|
pub permissions: PermissionsOptions,
|
|
|
|
pub location: Option<Url>,
|
|
|
|
pub v8_flags: Vec<String>,
|
|
|
|
pub log_level: Option<Level>,
|
2021-08-07 08:49:38 -04:00
|
|
|
pub ca_stores: Option<Vec<String>>,
|
2021-01-07 21:08:51 -05:00
|
|
|
pub ca_data: Option<Vec<u8>>,
|
2021-08-10 07:19:45 -04:00
|
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
2022-02-24 08:28:00 -05:00
|
|
|
pub maybe_import_map: Option<(Url, String)>,
|
2022-02-15 07:33:46 -05:00
|
|
|
pub entrypoint: ModuleSpecifier,
|
2021-01-04 18:15:52 -05:00
|
|
|
}
|
|
|
|
|
2021-01-07 21:08:51 -05:00
|
|
|
pub const MAGIC_TRAILER: &[u8; 8] = b"d3n0l4nd";
|
2020-11-30 14:35:12 -05:00
|
|
|
|
|
|
|
/// This function will try to run this binary as a standalone binary
|
2021-01-24 08:25:18 -05:00
|
|
|
/// produced by `deno compile`. It determines if this is a standalone
|
2020-11-30 14:35:12 -05:00
|
|
|
/// binary by checking for the magic trailer string `D3N0` at EOF-12.
|
2021-01-04 18:15:52 -05:00
|
|
|
/// The magic trailer is followed by:
|
|
|
|
/// - a u64 pointer to the JS bundle embedded in the binary
|
|
|
|
/// - a u64 pointer to JSON metadata (serialized flags) embedded in the binary
|
|
|
|
/// These are dereferenced, and the bundle is executed under the configuration
|
|
|
|
/// specified by the metadata. If no magic trailer is present, this function
|
2021-01-25 01:14:25 -05:00
|
|
|
/// exits with `Ok(None)`.
|
2022-02-15 07:33:46 -05:00
|
|
|
pub async fn extract_standalone(
|
2021-01-07 21:08:51 -05:00
|
|
|
args: Vec<String>,
|
2022-02-15 07:33:46 -05:00
|
|
|
) -> Result<Option<(Metadata, eszip::EszipV2)>, AnyError> {
|
2020-11-30 14:35:12 -05:00
|
|
|
let current_exe_path = current_exe()?;
|
|
|
|
|
2022-02-15 07:33:46 -05:00
|
|
|
let file = tokio::fs::File::open(current_exe_path).await?;
|
|
|
|
|
|
|
|
let mut bufreader = tokio::io::BufReader::new(file);
|
|
|
|
|
|
|
|
let trailer_pos = bufreader.seek(SeekFrom::End(-24)).await?;
|
2021-01-04 18:15:52 -05:00
|
|
|
let mut trailer = [0; 24];
|
2022-02-15 07:33:46 -05:00
|
|
|
bufreader.read_exact(&mut trailer).await?;
|
2021-01-04 18:15:52 -05:00
|
|
|
let (magic_trailer, rest) = trailer.split_at(8);
|
2021-01-07 21:08:51 -05:00
|
|
|
if magic_trailer != MAGIC_TRAILER {
|
|
|
|
return Ok(None);
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
2021-01-07 21:08:51 -05:00
|
|
|
|
2022-02-15 07:33:46 -05:00
|
|
|
let (eszip_archive_pos, rest) = rest.split_at(8);
|
2021-01-07 21:08:51 -05:00
|
|
|
let metadata_pos = rest;
|
2022-02-15 07:33:46 -05:00
|
|
|
let eszip_archive_pos = u64_from_bytes(eszip_archive_pos)?;
|
2021-01-07 21:08:51 -05:00
|
|
|
let metadata_pos = u64_from_bytes(metadata_pos)?;
|
|
|
|
let metadata_len = trailer_pos - metadata_pos;
|
|
|
|
|
2022-02-15 07:33:46 -05:00
|
|
|
bufreader.seek(SeekFrom::Start(eszip_archive_pos)).await?;
|
|
|
|
|
|
|
|
let (eszip, loader) = eszip::EszipV2::parse(bufreader)
|
|
|
|
.await
|
|
|
|
.context("Failed to parse eszip header")?;
|
|
|
|
|
|
|
|
let mut bufreader = loader.await.context("Failed to parse eszip archive")?;
|
|
|
|
|
|
|
|
bufreader.seek(SeekFrom::Start(metadata_pos)).await?;
|
|
|
|
|
|
|
|
let mut metadata = String::new();
|
|
|
|
|
|
|
|
bufreader
|
|
|
|
.take(metadata_len)
|
|
|
|
.read_to_string(&mut metadata)
|
|
|
|
.await
|
|
|
|
.context("Failed to read metadata from the current executable")?;
|
2021-01-07 21:08:51 -05:00
|
|
|
|
|
|
|
let mut metadata: Metadata = serde_json::from_str(&metadata).unwrap();
|
|
|
|
metadata.argv.append(&mut args[1..].to_vec());
|
2022-02-15 07:33:46 -05:00
|
|
|
|
|
|
|
Ok(Some((metadata, eszip)))
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 18:15:52 -05:00
|
|
|
fn u64_from_bytes(arr: &[u8]) -> Result<u64, AnyError> {
|
|
|
|
let fixed_arr: &[u8; 8] = arr
|
|
|
|
.try_into()
|
|
|
|
.context("Failed to convert the buffer into a fixed-size array")?;
|
|
|
|
Ok(u64::from_be_bytes(*fixed_arr))
|
|
|
|
}
|
|
|
|
|
2022-02-24 08:28:00 -05:00
|
|
|
struct EmbeddedModuleLoader {
|
|
|
|
eszip: eszip::EszipV2,
|
|
|
|
maybe_import_map_resolver: Option<ImportMapResolver>,
|
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
|
|
|
|
impl ModuleLoader for EmbeddedModuleLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
2022-02-24 08:28:00 -05:00
|
|
|
referrer: &str,
|
2020-11-30 14:35:12 -05:00
|
|
|
_is_main: bool,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2022-03-31 04:41:30 -04:00
|
|
|
// Try to follow redirects when resolving.
|
|
|
|
let referrer = match self.eszip.get_module(referrer) {
|
|
|
|
Some(eszip::Module { ref specifier, .. }) => {
|
|
|
|
deno_core::resolve_url_or_path(specifier)?
|
|
|
|
}
|
|
|
|
None => deno_core::resolve_url_or_path(referrer)?,
|
|
|
|
};
|
|
|
|
|
2022-02-24 08:28:00 -05:00
|
|
|
self.maybe_import_map_resolver.as_ref().map_or_else(
|
|
|
|
|| {
|
|
|
|
deno_core::resolve_import(specifier, referrer.as_str())
|
|
|
|
.map_err(|err| err.into())
|
|
|
|
},
|
|
|
|
|r| r.resolve(specifier, &referrer).to_result(),
|
|
|
|
)
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
|
|
|
_is_dynamic: bool,
|
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
|
|
|
let module_specifier = module_specifier.clone();
|
2022-02-15 07:33:46 -05:00
|
|
|
|
2021-06-05 10:24:17 -04:00
|
|
|
let is_data_uri = get_source_from_data_url(&module_specifier).ok();
|
2022-02-15 07:33:46 -05:00
|
|
|
let module = self
|
2022-02-24 08:28:00 -05:00
|
|
|
.eszip
|
2022-02-15 07:33:46 -05:00
|
|
|
.get_module(module_specifier.as_str())
|
|
|
|
.ok_or_else(|| type_error("Module not found"));
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
async move {
|
2022-02-15 07:33:46 -05:00
|
|
|
if let Some((ref source, _)) = is_data_uri {
|
|
|
|
return Ok(deno_core::ModuleSource {
|
|
|
|
code: source.to_owned(),
|
|
|
|
module_type: deno_core::ModuleType::JavaScript,
|
|
|
|
module_url_specified: module_specifier.to_string(),
|
|
|
|
module_url_found: module_specifier.to_string(),
|
|
|
|
});
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
2021-06-05 10:24:17 -04:00
|
|
|
|
2022-02-15 07:33:46 -05:00
|
|
|
let module = module?;
|
|
|
|
let code = module.source().await;
|
|
|
|
let code = std::str::from_utf8(&code)
|
|
|
|
.map_err(|_| type_error("Module source is not utf-8"))?
|
|
|
|
.to_owned();
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
Ok(deno_core::ModuleSource {
|
|
|
|
code,
|
2022-02-15 07:33:46 -05:00
|
|
|
module_type: match module.kind {
|
|
|
|
eszip::ModuleKind::JavaScript => deno_core::ModuleType::JavaScript,
|
|
|
|
eszip::ModuleKind::Json => deno_core::ModuleType::Json,
|
|
|
|
},
|
2020-11-30 14:35:12 -05:00
|
|
|
module_url_specified: module_specifier.to_string(),
|
|
|
|
module_url_found: module_specifier.to_string(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 08:02:53 -04:00
|
|
|
fn metadata_to_flags(metadata: &Metadata) -> Flags {
|
|
|
|
let permissions = metadata.permissions.clone();
|
|
|
|
Flags {
|
|
|
|
argv: metadata.argv.clone(),
|
|
|
|
unstable: metadata.unstable,
|
|
|
|
seed: metadata.seed,
|
|
|
|
location: metadata.location.clone(),
|
|
|
|
allow_env: permissions.allow_env,
|
|
|
|
allow_hrtime: permissions.allow_hrtime,
|
|
|
|
allow_net: permissions.allow_net,
|
2021-08-06 17:28:10 -04:00
|
|
|
allow_ffi: permissions.allow_ffi,
|
2021-06-07 08:02:53 -04:00
|
|
|
allow_read: permissions.allow_read,
|
|
|
|
allow_run: permissions.allow_run,
|
|
|
|
allow_write: permissions.allow_write,
|
|
|
|
v8_flags: metadata.v8_flags.clone(),
|
|
|
|
log_level: metadata.log_level,
|
2021-08-07 08:49:38 -04:00
|
|
|
ca_stores: metadata.ca_stores.clone(),
|
2021-06-07 08:02:53 -04:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:08:51 -05:00
|
|
|
pub async fn run(
|
2022-02-15 07:33:46 -05:00
|
|
|
eszip: eszip::EszipV2,
|
2021-01-07 21:08:51 -05:00
|
|
|
metadata: Metadata,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-06-07 08:02:53 -04:00
|
|
|
let flags = metadata_to_flags(&metadata);
|
2022-02-15 07:33:46 -05:00
|
|
|
let main_module = &metadata.entrypoint;
|
2022-02-11 14:04:31 -05:00
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
2021-01-07 21:08:51 -05:00
|
|
|
let permissions = Permissions::from_options(&metadata.permissions);
|
2021-07-05 09:34:37 -04:00
|
|
|
let blob_store = BlobStore::default();
|
2021-05-22 12:08:24 -04:00
|
|
|
let broadcast_channel = InMemoryBroadcastChannel::default();
|
2022-02-24 08:28:00 -05:00
|
|
|
let module_loader = Rc::new(EmbeddedModuleLoader {
|
|
|
|
eszip,
|
|
|
|
maybe_import_map_resolver: metadata.maybe_import_map.map(
|
|
|
|
|(base, source)| {
|
|
|
|
ImportMapResolver::new(Arc::new(
|
|
|
|
parse_from_json(&base, &source).unwrap().import_map,
|
|
|
|
))
|
|
|
|
},
|
|
|
|
),
|
|
|
|
});
|
2020-12-11 12:49:26 -05:00
|
|
|
let create_web_worker_cb = Arc::new(|_| {
|
|
|
|
todo!("Worker are currently not supported in standalone binaries");
|
|
|
|
});
|
2022-02-11 07:41:56 -05:00
|
|
|
let web_worker_preload_module_cb = Arc::new(|_| {
|
|
|
|
todo!("Worker are currently not supported in standalone binaries");
|
|
|
|
});
|
2020-12-11 12:49:26 -05:00
|
|
|
|
2021-01-04 18:15:52 -05:00
|
|
|
// Keep in sync with `main.rs`.
|
|
|
|
v8_set_flags(
|
|
|
|
once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
|
2021-01-07 21:08:51 -05:00
|
|
|
.chain(metadata.v8_flags.iter().cloned())
|
2021-01-04 18:15:52 -05:00
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
);
|
2021-01-07 21:08:51 -05:00
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
let mut root_cert_store = ps
|
2021-08-07 08:49:38 -04:00
|
|
|
.root_cert_store
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(create_default_root_cert_store);
|
|
|
|
|
|
|
|
if let Some(cert) = metadata.ca_data {
|
|
|
|
let reader = &mut BufReader::new(Cursor::new(cert));
|
2021-12-06 18:48:11 -05:00
|
|
|
match rustls_pemfile::certs(reader) {
|
|
|
|
Ok(certs) => {
|
|
|
|
root_cert_store.add_parsable_certificates(&certs);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(anyhow!(
|
|
|
|
"Unable to add pem file to certificate store: {}",
|
|
|
|
e
|
|
|
|
));
|
|
|
|
}
|
2021-08-07 08:49:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
let options = WorkerOptions {
|
2021-10-05 16:41:14 -04:00
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: metadata.argv,
|
2022-02-24 20:03:12 -05:00
|
|
|
cpu_count: std::thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1),
|
2021-10-05 16:41:14 -04:00
|
|
|
debug_flag: metadata.log_level.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
enable_testing_features: false,
|
|
|
|
location: metadata.location,
|
|
|
|
no_color: !colors::use_color(),
|
2022-02-28 22:37:50 -05:00
|
|
|
is_tty: colors::is_tty(),
|
2021-10-05 16:41:14 -04:00
|
|
|
runtime_version: version::deno(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
unstable: metadata.unstable,
|
|
|
|
},
|
2021-12-29 08:30:08 -05:00
|
|
|
extensions: ops::cli_exts(ps.clone(), true),
|
2021-01-07 21:08:51 -05:00
|
|
|
user_agent: version::get_user_agent(),
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors: metadata
|
|
|
|
.unsafely_ignore_certificate_errors,
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store: Some(root_cert_store),
|
2021-01-07 21:08:51 -05:00
|
|
|
seed: metadata.seed,
|
2022-04-15 10:08:09 -04:00
|
|
|
source_map_getter: None,
|
2020-12-11 12:49:26 -05:00
|
|
|
js_error_create_fn: None,
|
|
|
|
create_web_worker_cb,
|
2022-02-11 07:41:56 -05:00
|
|
|
web_worker_preload_module_cb,
|
2020-12-11 12:49:26 -05:00
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
2020-11-30 14:35:12 -05:00
|
|
|
module_loader,
|
2021-01-07 21:08:51 -05:00
|
|
|
get_error_class_fn: Some(&get_error_class_name),
|
2021-05-27 01:23:12 -04:00
|
|
|
origin_storage_dir: None,
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store,
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel,
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store: None,
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store: None,
|
2020-12-11 12:49:26 -05:00
|
|
|
};
|
2021-10-05 16:41:14 -04:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
options,
|
|
|
|
);
|
2022-02-15 07:33:46 -05:00
|
|
|
worker.execute_main_module(main_module).await?;
|
2021-12-21 09:49:27 -05:00
|
|
|
worker.dispatch_load_event(&located_script_name!())?;
|
2021-05-26 15:07:12 -04:00
|
|
|
worker.run_event_loop(true).await?;
|
2021-12-21 09:49:27 -05:00
|
|
|
worker.dispatch_unload_event(&located_script_name!())?;
|
2021-01-07 21:08:51 -05:00
|
|
|
std::process::exit(0);
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
2021-01-04 18:15:52 -05:00
|
|
|
|
2021-01-07 21:08:51 -05:00
|
|
|
fn get_error_class_name(e: &AnyError) -> &'static str {
|
|
|
|
deno_runtime::errors::get_error_class_name(e).unwrap_or_else(|| {
|
2021-01-08 19:57:37 -05:00
|
|
|
panic!(
|
|
|
|
"Error '{}' contains boxed error of unsupported type:{}",
|
|
|
|
e,
|
|
|
|
e.chain()
|
|
|
|
.map(|e| format!("\n {:?}", e))
|
|
|
|
.collect::<String>()
|
|
|
|
);
|
2021-01-04 18:15:52 -05:00
|
|
|
})
|
|
|
|
}
|