2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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;
|
2020-12-11 18:36:18 -05:00
|
|
|
use crate::version;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2021-01-04 18:15:52 -05:00
|
|
|
use deno_core::error::Context;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::futures::FutureExt;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url;
|
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;
|
|
|
|
use deno_core::OpState;
|
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-01-07 21:08:51 -05:00
|
|
|
use log::Level;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::env::current_exe;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::io::Seek;
|
|
|
|
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;
|
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>,
|
|
|
|
pub ca_data: Option<Vec<u8>>,
|
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)`.
|
2021-01-07 21:08:51 -05:00
|
|
|
pub fn extract_standalone(
|
|
|
|
args: Vec<String>,
|
|
|
|
) -> Result<Option<(Metadata, String)>, AnyError> {
|
2020-11-30 14:35:12 -05:00
|
|
|
let current_exe_path = current_exe()?;
|
|
|
|
|
|
|
|
let mut current_exe = File::open(current_exe_path)?;
|
2021-01-04 18:15:52 -05:00
|
|
|
let trailer_pos = current_exe.seek(SeekFrom::End(-24))?;
|
|
|
|
let mut trailer = [0; 24];
|
2020-11-30 14:35:12 -05:00
|
|
|
current_exe.read_exact(&mut trailer)?;
|
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
|
|
|
|
|
|
|
let (bundle_pos, rest) = rest.split_at(8);
|
|
|
|
let metadata_pos = rest;
|
|
|
|
let bundle_pos = u64_from_bytes(bundle_pos)?;
|
|
|
|
let metadata_pos = u64_from_bytes(metadata_pos)?;
|
|
|
|
let bundle_len = metadata_pos - bundle_pos;
|
|
|
|
let metadata_len = trailer_pos - metadata_pos;
|
|
|
|
current_exe.seek(SeekFrom::Start(bundle_pos))?;
|
|
|
|
|
|
|
|
let bundle = read_string_slice(&mut current_exe, bundle_pos, bundle_len)
|
|
|
|
.context("Failed to read source bundle from the current executable")?;
|
|
|
|
let metadata =
|
|
|
|
read_string_slice(&mut current_exe, metadata_pos, metadata_len)
|
|
|
|
.context("Failed to read metadata from the current executable")?;
|
|
|
|
|
|
|
|
let mut metadata: Metadata = serde_json::from_str(&metadata).unwrap();
|
|
|
|
metadata.argv.append(&mut args[1..].to_vec());
|
|
|
|
Ok(Some((metadata, bundle)))
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_string_slice(
|
|
|
|
file: &mut File,
|
|
|
|
pos: u64,
|
|
|
|
len: u64,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let mut string = String::new();
|
|
|
|
file.seek(SeekFrom::Start(pos))?;
|
|
|
|
file.take(len).read_to_string(&mut string)?;
|
|
|
|
// TODO: check amount of bytes read
|
|
|
|
Ok(string)
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
const SPECIFIER: &str = "file://$deno$/bundle.js";
|
|
|
|
|
|
|
|
struct EmbeddedModuleLoader(String);
|
|
|
|
|
|
|
|
impl ModuleLoader for EmbeddedModuleLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
_op_state: Rc<RefCell<OpState>>,
|
|
|
|
specifier: &str,
|
|
|
|
_referrer: &str,
|
|
|
|
_is_main: bool,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
if specifier != SPECIFIER {
|
|
|
|
return Err(type_error(
|
|
|
|
"Self-contained binaries don't support module loading",
|
|
|
|
));
|
|
|
|
}
|
2021-02-17 13:47:18 -05:00
|
|
|
Ok(resolve_url(specifier)?)
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
_op_state: Rc<RefCell<OpState>>,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
|
|
|
_is_dynamic: bool,
|
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
|
|
|
let module_specifier = module_specifier.clone();
|
|
|
|
let code = self.0.to_string();
|
|
|
|
async move {
|
|
|
|
if module_specifier.to_string() != SPECIFIER {
|
|
|
|
return Err(type_error(
|
|
|
|
"Self-contained binaries don't support module loading",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(deno_core::ModuleSource {
|
|
|
|
code,
|
|
|
|
module_url_specified: module_specifier.to_string(),
|
|
|
|
module_url_found: module_specifier.to_string(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:08:51 -05:00
|
|
|
pub async fn run(
|
|
|
|
source_code: String,
|
|
|
|
metadata: Metadata,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = resolve_url(SPECIFIER)?;
|
2021-01-07 21:08:51 -05:00
|
|
|
let permissions = Permissions::from_options(&metadata.permissions);
|
2020-11-30 14:35:12 -05:00
|
|
|
let module_loader = Rc::new(EmbeddedModuleLoader(source_code));
|
2020-12-11 12:49:26 -05:00
|
|
|
let create_web_worker_cb = Arc::new(|_| {
|
|
|
|
todo!("Worker are currently not supported in standalone binaries");
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
let options = WorkerOptions {
|
|
|
|
apply_source_maps: false,
|
2021-01-07 21:08:51 -05:00
|
|
|
args: metadata.argv,
|
|
|
|
debug_flag: metadata.log_level.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
user_agent: version::get_user_agent(),
|
|
|
|
unstable: metadata.unstable,
|
|
|
|
ca_data: metadata.ca_data,
|
|
|
|
seed: metadata.seed,
|
2020-12-11 12:49:26 -05:00
|
|
|
js_error_create_fn: None,
|
|
|
|
create_web_worker_cb,
|
|
|
|
attach_inspector: false,
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
2020-11-30 14:35:12 -05:00
|
|
|
module_loader,
|
2020-12-11 18:36:18 -05:00
|
|
|
runtime_version: version::deno(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
no_color: !colors::use_color(),
|
2021-01-07 21:08:51 -05:00
|
|
|
get_error_class_fn: Some(&get_error_class_name),
|
|
|
|
location: metadata.location,
|
2020-12-11 12:49:26 -05:00
|
|
|
};
|
|
|
|
let mut worker =
|
|
|
|
MainWorker::from_options(main_module.clone(), permissions, &options);
|
|
|
|
worker.bootstrap(&options);
|
2020-11-30 14:35:12 -05:00
|
|
|
worker.execute_module(&main_module).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
|
|
|
worker.run_event_loop().await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
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
|
|
|
})
|
|
|
|
}
|