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
|
|
|
|
2023-08-28 17:30:46 -04:00
|
|
|
mod shared;
|
|
|
|
|
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 {
|
2022-07-04 18:12:41 -04:00
|
|
|
use super::*;
|
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-11-17 03:57:25 -05:00
|
|
|
use deno_core::op2;
|
2023-02-20 15:45:34 -05:00
|
|
|
use deno_core::snapshot_util::*;
|
|
|
|
use deno_core::Extension;
|
2023-11-17 03:57:25 -05:00
|
|
|
use deno_core::OpState;
|
2023-05-10 10:23:26 -04:00
|
|
|
use deno_http::DefaultHttpPropertyExtractor;
|
2023-08-28 17:30:46 -04:00
|
|
|
use shared::maybe_transpile_source;
|
|
|
|
use shared::runtime;
|
2023-03-17 14:22:15 -04:00
|
|
|
use std::path::Path;
|
2023-02-08 16:40:18 -05:00
|
|
|
|
2023-11-17 03:57:25 -05:00
|
|
|
// Keep in sync with `runtime/ops/bootstrap.rs`
|
|
|
|
#[derive(serde::Serialize, Default)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SnapshotOptions {
|
|
|
|
pub deno_version: String,
|
|
|
|
pub ts_version: String,
|
|
|
|
pub v8_version: &'static str,
|
|
|
|
pub target: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@littledivy): Remove this once we get rid of deno_runtime snapshots.
|
|
|
|
#[op2]
|
|
|
|
#[serde]
|
|
|
|
pub fn op_snapshot_options(_: &mut OpState) -> SnapshotOptions {
|
|
|
|
SnapshotOptions::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
deno_core::extension!(snapshot, ops = [op_snapshot_options],);
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
#[derive(Clone)]
|
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!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_ffi::FfiPermissions for Permissions {
|
2023-08-03 07:19:19 -04:00
|
|
|
fn check_partial(
|
2022-07-04 18:12:41 -04:00
|
|
|
&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-24 12:07:49 -04:00
|
|
|
impl deno_node::NodePermissions for Permissions {
|
2023-05-27 09:42:20 -04:00
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2023-04-24 21:07:48 -04:00
|
|
|
fn check_read(&self, _p: &Path) -> Result<(), deno_core::error::AnyError> {
|
2022-08-24 12:07:49 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2023-07-31 16:29:09 -04:00
|
|
|
fn check_sys(
|
|
|
|
&self,
|
|
|
|
_kind: &str,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2022-08-24 12:07:49 -04:00
|
|
|
}
|
|
|
|
|
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!")
|
|
|
|
}
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
fn check_read_all(&mut self, _api_name: &str) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2023-03-07 04:13:44 -05:00
|
|
|
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!")
|
|
|
|
}
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
fn check_write_partial(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
fn check_write_all(&mut self, _api_name: &str) -> Result<(), AnyError> {
|
2023-03-07 04:13:44 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
fn check_write_blind(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_display: &str,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-03-07 04:13:44 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
impl deno_kv::sqlite::SqliteDbHandlerPermissions for Permissions {
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
pub fn create_runtime_snapshot(snapshot_path: PathBuf) {
|
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-05-05 12:44:24 -04:00
|
|
|
let fs = std::sync::Arc::new(deno_fs::RealFs);
|
2023-08-05 19:00:38 -04:00
|
|
|
let mut extensions: Vec<Extension> = vec![
|
2023-06-26 07:54:10 -04:00
|
|
|
deno_webidl::deno_webidl::init_ops_and_esm(),
|
|
|
|
deno_console::deno_console::init_ops_and_esm(),
|
|
|
|
deno_url::deno_url::init_ops_and_esm(),
|
|
|
|
deno_web::deno_web::init_ops_and_esm::<Permissions>(
|
2023-07-01 18:52:30 -04:00
|
|
|
Default::default(),
|
2022-07-04 18:12:41 -04:00
|
|
|
Default::default(),
|
|
|
|
),
|
2023-06-26 07:54:10 -04:00
|
|
|
deno_fetch::deno_fetch::init_ops_and_esm::<Permissions>(
|
|
|
|
Default::default(),
|
|
|
|
),
|
|
|
|
deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(None),
|
|
|
|
deno_websocket::deno_websocket::init_ops_and_esm::<Permissions>(
|
2023-03-09 09:56:19 -05:00
|
|
|
"".to_owned(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
),
|
2023-06-26 07:54:10 -04:00
|
|
|
deno_webstorage::deno_webstorage::init_ops_and_esm(None),
|
|
|
|
deno_crypto::deno_crypto::init_ops_and_esm(None),
|
|
|
|
deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm(
|
2022-07-04 18:12:41 -04:00
|
|
|
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
|
|
|
|
),
|
2023-10-04 15:42:17 -04:00
|
|
|
deno_ffi::deno_ffi::init_ops_and_esm::<Permissions>(),
|
|
|
|
deno_net::deno_net::init_ops_and_esm::<Permissions>(None, None),
|
2023-06-26 07:54:10 -04:00
|
|
|
deno_tls::deno_tls::init_ops_and_esm(),
|
2023-10-04 15:42:17 -04:00
|
|
|
deno_kv::deno_kv::init_ops_and_esm(deno_kv::sqlite::SqliteDbHandler::<
|
|
|
|
Permissions,
|
2023-10-31 07:13:57 -04:00
|
|
|
>::new(None, None)),
|
2023-11-01 14:57:55 -04:00
|
|
|
deno_cron::deno_cron::init_ops_and_esm(
|
|
|
|
deno_cron::local::LocalCronHandler::new(),
|
|
|
|
),
|
2023-06-26 07:54:10 -04:00
|
|
|
deno_napi::deno_napi::init_ops_and_esm::<Permissions>(),
|
|
|
|
deno_http::deno_http::init_ops_and_esm::<DefaultHttpPropertyExtractor>(),
|
|
|
|
deno_io::deno_io::init_ops_and_esm(Default::default()),
|
2023-10-04 15:42:17 -04:00
|
|
|
deno_fs::deno_fs::init_ops_and_esm::<Permissions>(fs.clone()),
|
2023-06-26 07:54:10 -04:00
|
|
|
deno_node::deno_node::init_ops_and_esm::<Permissions>(None, fs),
|
2023-08-28 17:30:46 -04:00
|
|
|
runtime::init_ops_and_esm(),
|
2023-11-17 03:57:25 -05:00
|
|
|
snapshot::init_ops_and_esm(),
|
2022-07-04 18:12:41 -04:00
|
|
|
];
|
|
|
|
|
2023-08-05 19:00:38 -04:00
|
|
|
for extension in &mut extensions {
|
|
|
|
for source in extension.esm_files.to_mut() {
|
|
|
|
maybe_transpile_source(source).unwrap();
|
|
|
|
}
|
|
|
|
for source in extension.js_files.to_mut() {
|
|
|
|
maybe_transpile_source(source).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 18:36:16 -04:00
|
|
|
let output = create_snapshot(CreateSnapshotOptions {
|
2022-11-21 08:36:26 -05:00
|
|
|
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
|
|
|
|
snapshot_path,
|
|
|
|
startup_snapshot: None,
|
2023-03-09 19:22:27 -05:00
|
|
|
extensions,
|
2023-03-16 23:19:46 -04:00
|
|
|
compression_cb: None,
|
2023-07-23 09:42:41 -04:00
|
|
|
with_runtime_cb: None,
|
2023-11-06 20:03:48 -05:00
|
|
|
skip_op_registration: false,
|
2022-07-04 18:12:41 -04:00
|
|
|
});
|
2023-06-13 18:36:16 -04:00
|
|
|
for path in output.files_loaded_during_snapshot {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
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)]
|
2023-05-10 20:06:59 -04:00
|
|
|
#[allow(clippy::disallowed_methods)]
|
2023-11-17 10:06:28 -05:00
|
|
|
#[allow(clippy::needless_borrows_for_generic_args)]
|
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")
|
|
|
|
))]
|
2023-03-17 14:22:15 -04:00
|
|
|
startup_snapshot::create_runtime_snapshot(runtime_snapshot_path)
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|