2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2022-07-04 18:12:41 -04:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-02-20 15:45:34 -05:00
|
|
|
#[cfg(all(
|
|
|
|
not(feature = "docsrs"),
|
|
|
|
not(feature = "dont_create_runtime_snapshot")
|
|
|
|
))]
|
|
|
|
mod startup_snapshot {
|
2023-01-27 10:43:16 -05:00
|
|
|
use std::path::Path;
|
|
|
|
|
2022-07-04 18:12:41 -04:00
|
|
|
use super::*;
|
2023-02-08 16:40:18 -05:00
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ParseParams;
|
|
|
|
use deno_ast::SourceTextInfo;
|
2023-02-20 15:45:34 -05:00
|
|
|
use deno_cache::SqliteBackedCache;
|
2023-02-08 16:40:18 -05:00
|
|
|
use deno_core::error::AnyError;
|
2023-02-20 15:45:34 -05:00
|
|
|
use deno_core::include_js_files;
|
|
|
|
use deno_core::snapshot_util::*;
|
|
|
|
use deno_core::Extension;
|
2023-02-08 16:40:18 -05:00
|
|
|
use deno_core::ExtensionFileSource;
|
|
|
|
|
|
|
|
fn transpile_ts_for_snapshotting(
|
|
|
|
file_source: &ExtensionFileSource,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let media_type = MediaType::from(Path::new(&file_source.specifier));
|
|
|
|
|
|
|
|
let should_transpile = match media_type {
|
|
|
|
MediaType::JavaScript => false,
|
2023-02-14 11:38:45 -05:00
|
|
|
MediaType::Mjs => false,
|
2023-02-08 16:40:18 -05:00
|
|
|
MediaType::TypeScript => true,
|
2023-02-14 11:38:45 -05:00
|
|
|
_ => panic!(
|
|
|
|
"Unsupported media type for snapshotting {media_type:?} for file {}",
|
|
|
|
file_source.specifier
|
|
|
|
),
|
2023-02-08 16:40:18 -05:00
|
|
|
};
|
2023-02-20 15:45:34 -05:00
|
|
|
let code = file_source.code.load()?;
|
2023-02-19 19:11:56 -05:00
|
|
|
|
2023-02-08 16:40:18 -05:00
|
|
|
if !should_transpile {
|
2023-02-20 15:45:34 -05:00
|
|
|
return Ok(code);
|
2023-02-08 16:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let parsed = deno_ast::parse_module(ParseParams {
|
|
|
|
specifier: file_source.specifier.to_string(),
|
2023-02-20 15:45:34 -05:00
|
|
|
text_info: SourceTextInfo::from_string(code),
|
2023-02-08 16:40:18 -05:00
|
|
|
media_type,
|
|
|
|
capture_tokens: false,
|
|
|
|
scope_analysis: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})?;
|
2023-02-14 11:38:45 -05:00
|
|
|
let transpiled_source = parsed.transpile(&deno_ast::EmitOptions {
|
|
|
|
imports_not_used_as_values: deno_ast::ImportsNotUsedAsValues::Remove,
|
|
|
|
inline_source_map: false,
|
|
|
|
..Default::default()
|
|
|
|
})?;
|
|
|
|
|
2023-02-08 16:40:18 -05:00
|
|
|
Ok(transpiled_source.text)
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:12:41 -04:00
|
|
|
struct Permissions;
|
|
|
|
|
|
|
|
impl deno_fetch::FetchPermissions for Permissions {
|
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-07-04 18:12:41 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-07-04 18:12:41 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_websocket::WebSocketPermissions for Permissions {
|
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-07-04 18:12:41 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_web::TimersPermission for Permissions {
|
|
|
|
fn allow_hrtime(&mut self) -> bool {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_unstable(
|
|
|
|
&self,
|
|
|
|
_state: &deno_core::OpState,
|
|
|
|
_api_name: &'static str,
|
|
|
|
) {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_ffi::FfiPermissions for Permissions {
|
|
|
|
fn check(
|
|
|
|
&mut self,
|
|
|
|
_path: Option<&Path>,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
impl deno_napi::NapiPermissions for Permissions {
|
|
|
|
fn check(
|
|
|
|
&mut self,
|
|
|
|
_path: Option<&Path>,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 08:05:02 -04:00
|
|
|
impl deno_flash::FlashPermissions for Permissions {
|
|
|
|
fn check_net<T: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
_host: &(T, Option<u16>),
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-08-18 08:05:02 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:07:49 -04:00
|
|
|
impl deno_node::NodePermissions for Permissions {
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:12:41 -04:00
|
|
|
impl deno_net::NetPermissions for Permissions {
|
|
|
|
fn check_net<T: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
_host: &(T, Option<u16>),
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-07-04 18:12:41 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-07-04 18:12:41 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-07-04 18:12:41 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 04:13:44 -05:00
|
|
|
impl deno_fs::FsPermissions for Permissions {
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read_blind(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_display: &str,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read_all(&mut self, _api_name: &str) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write_all(&mut self, _api_name: &str) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 16:40:18 -05:00
|
|
|
fn create_runtime_snapshot(
|
|
|
|
snapshot_path: PathBuf,
|
2023-02-13 09:34:32 -05:00
|
|
|
maybe_additional_extension: Option<Extension>,
|
2023-02-08 16:40:18 -05:00
|
|
|
) {
|
2023-03-09 07:10:54 -05:00
|
|
|
let runtime_extension = Extension::builder_with_deps(
|
|
|
|
"runtime",
|
|
|
|
&[
|
2023-02-13 09:34:32 -05:00
|
|
|
"deno_webidl",
|
|
|
|
"deno_console",
|
|
|
|
"deno_url",
|
|
|
|
"deno_tls",
|
|
|
|
"deno_web",
|
|
|
|
"deno_fetch",
|
|
|
|
"deno_cache",
|
|
|
|
"deno_websocket",
|
|
|
|
"deno_webstorage",
|
|
|
|
"deno_crypto",
|
|
|
|
"deno_webgpu",
|
|
|
|
"deno_broadcast_channel",
|
2023-02-14 11:38:45 -05:00
|
|
|
// FIXME(bartlomieju): this should be reenabled
|
|
|
|
// "deno_node",
|
2023-02-13 09:34:32 -05:00
|
|
|
"deno_ffi",
|
|
|
|
"deno_net",
|
|
|
|
"deno_napi",
|
|
|
|
"deno_http",
|
|
|
|
"deno_flash",
|
2023-03-04 19:39:48 -05:00
|
|
|
"deno_io",
|
2023-03-07 04:13:44 -05:00
|
|
|
"deno_fs",
|
2023-03-09 07:10:54 -05:00
|
|
|
],
|
|
|
|
)
|
|
|
|
.esm(include_js_files!(
|
|
|
|
dir "js",
|
|
|
|
"01_errors.js",
|
|
|
|
"01_version.ts",
|
|
|
|
"06_util.js",
|
|
|
|
"10_permissions.js",
|
|
|
|
"11_workers.js",
|
|
|
|
"13_buffer.js",
|
|
|
|
"30_os.js",
|
|
|
|
"40_fs_events.js",
|
|
|
|
"40_http.js",
|
|
|
|
"40_process.js",
|
|
|
|
"40_signals.js",
|
|
|
|
"40_tty.js",
|
|
|
|
"41_prompt.js",
|
|
|
|
"90_deno_ns.js",
|
|
|
|
"98_global_scope.js",
|
|
|
|
))
|
|
|
|
.build();
|
2023-02-13 09:34:32 -05:00
|
|
|
|
2023-03-16 13:36:53 -04:00
|
|
|
// NOTE(bartlomieju): ordering is important here, keep it in sync with
|
|
|
|
// `runtime/worker.rs`, `runtime/web_worker.rs` and `cli/build.rs`!
|
2023-03-09 19:22:27 -05:00
|
|
|
let mut extensions: Vec<Extension> = vec![
|
2023-03-09 09:56:19 -05:00
|
|
|
deno_webidl::init_esm(),
|
|
|
|
deno_console::init_esm(),
|
|
|
|
deno_url::init_ops_and_esm(),
|
|
|
|
deno_web::init_ops_and_esm::<Permissions>(
|
2022-07-04 18:12:41 -04:00
|
|
|
deno_web::BlobStore::default(),
|
|
|
|
Default::default(),
|
|
|
|
),
|
2023-03-09 09:56:19 -05:00
|
|
|
deno_fetch::init_ops_and_esm::<Permissions>(Default::default()),
|
|
|
|
deno_cache::init_ops_and_esm::<SqliteBackedCache>(None),
|
|
|
|
deno_websocket::init_ops_and_esm::<Permissions>(
|
|
|
|
"".to_owned(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
deno_webstorage::init_ops_and_esm(None),
|
|
|
|
deno_crypto::init_ops_and_esm(None),
|
|
|
|
deno_webgpu::init_ops_and_esm(false),
|
|
|
|
deno_broadcast_channel::init_ops_and_esm(
|
2022-07-04 18:12:41 -04:00
|
|
|
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
|
|
|
|
false, // No --unstable.
|
|
|
|
),
|
2023-03-09 09:56:19 -05:00
|
|
|
deno_ffi::init_ops_and_esm::<Permissions>(false),
|
|
|
|
deno_net::init_ops_and_esm::<Permissions>(
|
2022-07-04 18:12:41 -04:00
|
|
|
None, false, // No --unstable.
|
|
|
|
None,
|
|
|
|
),
|
2023-03-16 13:36:53 -04:00
|
|
|
deno_tls::init_ops(),
|
2023-03-09 15:09:45 -05:00
|
|
|
deno_napi::init_ops::<Permissions>(),
|
2023-03-09 09:56:19 -05:00
|
|
|
deno_http::init_ops_and_esm(),
|
|
|
|
deno_io::init_ops_and_esm(Default::default()),
|
|
|
|
deno_fs::init_ops_and_esm::<Permissions>(false),
|
|
|
|
deno_flash::init_ops_and_esm::<Permissions>(false), // No --unstable
|
2023-02-13 09:34:32 -05:00
|
|
|
runtime_extension,
|
2023-02-14 11:38:45 -05:00
|
|
|
// FIXME(bartlomieju): these extensions are specified last, because they
|
|
|
|
// depend on `runtime`, even though it should be other way around
|
2023-03-09 09:56:19 -05:00
|
|
|
deno_node::init_ops_and_esm::<Permissions>(None),
|
2023-03-08 06:43:26 -05:00
|
|
|
deno_node::init_polyfill_ops_and_esm(),
|
2022-07-04 18:12:41 -04:00
|
|
|
];
|
|
|
|
|
2023-02-13 09:34:32 -05:00
|
|
|
if let Some(additional_extension) = maybe_additional_extension {
|
2023-03-09 19:22:27 -05:00
|
|
|
extensions.push(additional_extension);
|
2023-02-13 09:34:32 -05:00
|
|
|
}
|
|
|
|
|
2022-11-21 08:36:26 -05:00
|
|
|
create_snapshot(CreateSnapshotOptions {
|
|
|
|
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
|
|
|
|
snapshot_path,
|
|
|
|
startup_snapshot: None,
|
2023-03-09 19:22:27 -05:00
|
|
|
extensions,
|
2023-03-08 10:44:04 -05:00
|
|
|
compression_cb: Some(Box::new(|vec, snapshot_slice| {
|
|
|
|
lzzzz::lz4_hc::compress_to_vec(
|
|
|
|
snapshot_slice,
|
|
|
|
vec,
|
|
|
|
lzzzz::lz4_hc::CLEVEL_MAX,
|
|
|
|
)
|
|
|
|
.expect("snapshot compression failed");
|
|
|
|
})),
|
2023-02-08 16:40:18 -05:00
|
|
|
snapshot_module_load_cb: Some(Box::new(transpile_ts_for_snapshotting)),
|
2022-07-04 18:12:41 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_snapshot(runtime_snapshot_path: PathBuf) {
|
2023-02-08 16:40:18 -05:00
|
|
|
#[allow(unused_mut, unused_assignments)]
|
2023-02-13 09:34:32 -05:00
|
|
|
let mut maybe_additional_extension = None;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2022-11-28 06:47:25 -05:00
|
|
|
#[cfg(not(feature = "snapshot_from_snapshot"))]
|
|
|
|
{
|
2023-02-20 15:45:34 -05:00
|
|
|
use deno_core::ExtensionFileSourceCode;
|
2023-02-13 09:34:32 -05:00
|
|
|
maybe_additional_extension = Some(
|
2023-03-10 03:39:29 -05:00
|
|
|
Extension::builder_with_deps("runtime_main", &["runtime"])
|
2023-02-13 09:34:32 -05:00
|
|
|
.esm(vec![ExtensionFileSource {
|
|
|
|
specifier: "js/99_main.js".to_string(),
|
2023-02-19 19:11:56 -05:00
|
|
|
code: ExtensionFileSourceCode::IncludedInBinary(include_str!(
|
|
|
|
"js/99_main.js"
|
|
|
|
)),
|
2023-02-13 09:34:32 -05:00
|
|
|
}])
|
|
|
|
.build(),
|
|
|
|
);
|
2022-11-28 06:47:25 -05:00
|
|
|
}
|
2023-02-08 16:40:18 -05:00
|
|
|
|
2023-02-13 09:34:32 -05:00
|
|
|
create_runtime_snapshot(runtime_snapshot_path, maybe_additional_extension);
|
2022-07-04 18:12:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 12:01:30 -04:00
|
|
|
fn main() {
|
2022-07-04 18:12:41 -04:00
|
|
|
// To debug snapshot issues uncomment:
|
|
|
|
// op_fetch_asset::trace_serializer();
|
|
|
|
|
|
|
|
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
|
|
|
|
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
|
|
|
|
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
// Main snapshot
|
2022-11-21 08:36:26 -05:00
|
|
|
let runtime_snapshot_path = o.join("RUNTIME_SNAPSHOT.bin");
|
2022-07-04 18:12:41 -04:00
|
|
|
|
|
|
|
// If we're building on docs.rs we just create
|
|
|
|
// and empty snapshot file and return, because `rusty_v8`
|
|
|
|
// doesn't actually compile on docs.rs
|
|
|
|
if env::var_os("DOCS_RS").is_some() {
|
|
|
|
let snapshot_slice = &[];
|
2023-02-20 15:45:34 -05:00
|
|
|
#[allow(clippy::needless_borrow)]
|
2022-07-04 18:12:41 -04:00
|
|
|
std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap();
|
2021-08-11 12:01:30 -04:00
|
|
|
}
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2023-02-20 15:45:34 -05:00
|
|
|
#[cfg(all(
|
|
|
|
not(feature = "docsrs"),
|
|
|
|
not(feature = "dont_create_runtime_snapshot")
|
|
|
|
))]
|
|
|
|
startup_snapshot::build_snapshot(runtime_snapshot_path)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|