2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-14 07:15:49 -05:00
|
|
|
|
|
|
|
use crate::ops;
|
2023-12-02 18:40:27 -05:00
|
|
|
use crate::ops::bootstrap::SnapshotOptions;
|
2023-11-14 07:15:49 -05:00
|
|
|
use crate::shared::maybe_transpile_source;
|
|
|
|
use crate::shared::runtime;
|
|
|
|
use deno_cache::SqliteBackedCache;
|
2024-02-17 17:22:46 -05:00
|
|
|
use deno_core::snapshot::*;
|
2023-12-11 02:08:45 -05:00
|
|
|
use deno_core::v8;
|
2023-11-14 07:15:49 -05:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_http::DefaultHttpPropertyExtractor;
|
2024-04-19 20:12:03 -04:00
|
|
|
use deno_io::fs::FsError;
|
2024-11-04 12:17:21 -05:00
|
|
|
use deno_permissions::PermissionCheckError;
|
2024-09-16 16:39:37 -04:00
|
|
|
use std::borrow::Cow;
|
2024-02-27 10:05:57 -05:00
|
|
|
use std::io::Write;
|
2023-11-14 07:15:49 -05:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2024-03-04 20:17:39 -05:00
|
|
|
use std::rc::Rc;
|
2023-11-14 07:15:49 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Permissions;
|
|
|
|
|
2024-03-12 13:42:26 -04:00
|
|
|
impl deno_websocket::WebSocketPermissions for Permissions {
|
2023-11-14 07:15:49 -05:00
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2024-03-12 13:42:26 -04:00
|
|
|
}
|
2023-11-14 07:15:49 -05:00
|
|
|
|
2024-03-12 13:42:26 -04:00
|
|
|
impl deno_web::TimersPermission for Permissions {
|
|
|
|
fn allow_hrtime(&mut self) -> bool {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 13:42:26 -04:00
|
|
|
impl deno_fetch::FetchPermissions for Permissions {
|
2023-11-14 07:15:49 -05:00
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
fn check_read<'a>(
|
2024-03-12 13:42:26 -04:00
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_p: &'a Path,
|
2024-03-12 13:42:26 -04:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_ffi::FfiPermissions for Permissions {
|
2024-11-04 12:17:21 -05:00
|
|
|
fn check_partial_no_path(&mut self) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
fn check_partial_with_path(
|
|
|
|
&mut self,
|
|
|
|
_path: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2023-11-14 07:15:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_napi::NapiPermissions for Permissions {
|
2024-11-04 12:17:21 -05:00
|
|
|
fn check(&mut self, _path: &str) -> Result<PathBuf, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_node::NodePermissions for Permissions {
|
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &deno_core::url::Url,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2024-11-06 09:08:26 -05:00
|
|
|
fn check_net(
|
|
|
|
&mut self,
|
|
|
|
_host: (&str, Option<u16>),
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
fn check_read_path<'a>(
|
|
|
|
&mut self,
|
|
|
|
_path: &'a Path,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2023-12-04 16:05:40 -05:00
|
|
|
fn check_read_with_api_name(
|
2024-06-06 23:37:53 -04:00
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_p: &str,
|
2023-12-04 16:05:40 -05:00
|
|
|
_api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2024-10-04 15:55:41 -04:00
|
|
|
fn query_read_all(&mut self) -> bool {
|
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2024-01-05 07:58:33 -05:00
|
|
|
fn check_write_with_api_name(
|
2024-06-06 23:37:53 -04:00
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_p: &str,
|
2024-01-05 07:58:33 -05:00
|
|
|
_api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-01-05 07:58:33 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2023-11-14 07:15:49 -05:00
|
|
|
fn check_sys(
|
2024-06-06 23:37:53 -04:00
|
|
|
&mut self,
|
2023-11-14 07:15:49 -05:00
|
|
|
_kind: &str,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_net::NetPermissions for Permissions {
|
|
|
|
fn check_net<T: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
_host: &(T, Option<u16>),
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_p: &str,
|
2023-11-14 07:15:49 -05:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_p: &str,
|
2023-11-14 07:15:49 -05:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write_path<'a>(
|
|
|
|
&mut self,
|
|
|
|
_p: &'a Path,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_fs::FsPermissions for Permissions {
|
2024-04-19 20:12:03 -04:00
|
|
|
fn check_open<'a>(
|
|
|
|
&mut self,
|
|
|
|
_resolved: bool,
|
|
|
|
_read: bool,
|
|
|
|
_write: bool,
|
|
|
|
_path: &'a Path,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<Cow<'a, Path>, FsError> {
|
2024-04-19 20:12:03 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2023-11-14 07:15:49 -05:00
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_path: &str,
|
2023-11-14 07:15:49 -05:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
fn check_read_all(
|
|
|
|
&mut self,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read_blind(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_display: &str,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_path: &str,
|
2023-11-14 07:15:49 -05:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write_partial(
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_path: &str,
|
2023-11-14 07:15:49 -05:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
fn check_write_all(
|
|
|
|
&mut self,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write_blind(
|
|
|
|
&mut self,
|
|
|
|
_path: &Path,
|
|
|
|
_display: &str,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<(), PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
fn check_read_path<'a>(
|
|
|
|
&mut self,
|
|
|
|
_path: &'a Path,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_write_path<'a>(
|
|
|
|
&mut self,
|
|
|
|
_path: &'a Path,
|
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
2023-11-14 07:15:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_kv::sqlite::SqliteDbHandlerPermissions for Permissions {
|
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_path: &str,
|
2023-11-14 07:15:49 -05:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
fn check_write<'a>(
|
2023-11-14 07:15:49 -05:00
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
_path: &'a Path,
|
2023-11-14 07:15:49 -05:00
|
|
|
_api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2023-11-14 07:15:49 -05:00
|
|
|
unreachable!("snapshotting!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 18:40:27 -05:00
|
|
|
pub fn create_runtime_snapshot(
|
|
|
|
snapshot_path: PathBuf,
|
|
|
|
snapshot_options: SnapshotOptions,
|
2024-05-01 18:00:32 -04:00
|
|
|
// NOTE: For embedders that wish to add additional extensions to the snapshot
|
|
|
|
custom_extensions: Vec<Extension>,
|
2023-12-02 18:40:27 -05:00
|
|
|
) {
|
2023-11-14 07:15:49 -05:00
|
|
|
// NOTE(bartlomieju): ordering is important here, keep it in sync with
|
2023-12-02 18:40:27 -05:00
|
|
|
// `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/snapshot.rs`!
|
2023-11-14 07:15:49 -05:00
|
|
|
let fs = std::sync::Arc::new(deno_fs::RealFs);
|
2024-05-01 18:00:32 -04:00
|
|
|
let mut extensions: Vec<Extension> = vec![
|
2023-11-14 07:15:49 -05: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>(
|
|
|
|
Default::default(),
|
|
|
|
Default::default(),
|
|
|
|
),
|
2023-12-08 19:19:16 -05:00
|
|
|
deno_webgpu::deno_webgpu::init_ops_and_esm(),
|
2024-01-22 06:08:01 -05:00
|
|
|
deno_canvas::deno_canvas::init_ops_and_esm(),
|
2023-11-14 07:15:49 -05: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>(
|
|
|
|
"".to_owned(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
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(
|
|
|
|
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
|
|
|
|
),
|
|
|
|
deno_ffi::deno_ffi::init_ops_and_esm::<Permissions>(),
|
|
|
|
deno_net::deno_net::init_ops_and_esm::<Permissions>(None, None),
|
|
|
|
deno_tls::deno_tls::init_ops_and_esm(),
|
2024-08-27 03:30:19 -04:00
|
|
|
deno_kv::deno_kv::init_ops_and_esm(
|
|
|
|
deno_kv::sqlite::SqliteDbHandler::<Permissions>::new(None, None),
|
|
|
|
deno_kv::KvConfig::builder().build(),
|
|
|
|
),
|
2023-11-14 07:15:49 -05:00
|
|
|
deno_cron::deno_cron::init_ops_and_esm(
|
|
|
|
deno_cron::local::LocalCronHandler::new(),
|
|
|
|
),
|
|
|
|
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()),
|
|
|
|
deno_fs::deno_fs::init_ops_and_esm::<Permissions>(fs.clone()),
|
2024-09-16 16:39:37 -04:00
|
|
|
deno_node::deno_node::init_ops_and_esm::<Permissions>(None, fs.clone()),
|
2023-11-14 07:15:49 -05:00
|
|
|
runtime::init_ops_and_esm(),
|
|
|
|
ops::runtime::deno_runtime::init_ops("deno:runtime".parse().unwrap()),
|
|
|
|
ops::worker_host::deno_worker_host::init_ops(
|
|
|
|
Arc::new(|_| unreachable!("not used in snapshot.")),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
ops::fs_events::deno_fs_events::init_ops(),
|
|
|
|
ops::os::deno_os::init_ops(Default::default()),
|
2024-09-30 09:19:24 -04:00
|
|
|
ops::permissions::deno_permissions::init_ops(),
|
2024-09-27 15:35:37 -04:00
|
|
|
ops::process::deno_process::init_ops(None),
|
2023-11-14 07:15:49 -05:00
|
|
|
ops::signal::deno_signal::init_ops(),
|
|
|
|
ops::tty::deno_tty::init_ops(),
|
|
|
|
ops::http::deno_http_runtime::init_ops(),
|
2023-12-02 18:40:27 -05:00
|
|
|
ops::bootstrap::deno_bootstrap::init_ops(Some(snapshot_options)),
|
2024-02-05 18:58:06 -05:00
|
|
|
ops::web_worker::deno_web_worker::init_ops(),
|
2023-11-14 07:15:49 -05:00
|
|
|
];
|
2024-05-01 18:00:32 -04:00
|
|
|
extensions.extend(custom_extensions);
|
2023-11-14 07:15:49 -05:00
|
|
|
|
2024-02-13 21:44:37 -05:00
|
|
|
let output = create_snapshot(
|
|
|
|
CreateSnapshotOptions {
|
|
|
|
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
|
|
|
|
startup_snapshot: None,
|
|
|
|
extensions,
|
2024-03-04 20:17:39 -05:00
|
|
|
extension_transpiler: Some(Rc::new(|specifier, source| {
|
|
|
|
maybe_transpile_source(specifier, source)
|
|
|
|
})),
|
2024-02-13 21:44:37 -05:00
|
|
|
with_runtime_cb: Some(Box::new(|rt| {
|
|
|
|
let isolate = rt.v8_isolate();
|
|
|
|
let scope = &mut v8::HandleScope::new(isolate);
|
2023-12-11 02:08:45 -05:00
|
|
|
|
2024-06-12 13:02:54 -04:00
|
|
|
let tmpl = deno_node::init_global_template(
|
|
|
|
scope,
|
|
|
|
deno_node::ContextInitMode::ForSnapshot,
|
|
|
|
);
|
|
|
|
let ctx = deno_node::create_v8_context(
|
|
|
|
scope,
|
|
|
|
tmpl,
|
|
|
|
deno_node::ContextInitMode::ForSnapshot,
|
2024-08-06 08:52:53 -04:00
|
|
|
std::ptr::null_mut(),
|
2024-06-12 13:02:54 -04:00
|
|
|
);
|
2024-02-13 21:44:37 -05:00
|
|
|
assert_eq!(scope.add_context(ctx), deno_node::VM_CONTEXT_INDEX);
|
|
|
|
})),
|
|
|
|
skip_op_registration: false,
|
|
|
|
},
|
|
|
|
None,
|
2024-02-17 17:22:46 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2024-02-27 10:05:57 -05:00
|
|
|
let mut snapshot = std::fs::File::create(snapshot_path).unwrap();
|
|
|
|
snapshot.write_all(&output.output).unwrap();
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
2023-11-14 07:15:49 -05:00
|
|
|
for path in output.files_loaded_during_snapshot {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
|
|
|
}
|