2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-08-09 15:06:01 -04:00
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::include_js_files;
|
2023-02-10 10:26:39 -05:00
|
|
|
use deno_core::located_script_name;
|
2022-08-09 15:06:01 -04:00
|
|
|
use deno_core::Extension;
|
2023-02-10 10:26:39 -05:00
|
|
|
use deno_core::JsRuntime;
|
2022-09-08 16:01:48 -04:00
|
|
|
use once_cell::sync::Lazy;
|
2022-09-14 11:59:20 -04:00
|
|
|
use std::collections::HashSet;
|
2022-08-20 11:31:33 -04:00
|
|
|
use std::path::Path;
|
2022-08-09 15:06:01 -04:00
|
|
|
use std::path::PathBuf;
|
2022-08-20 11:31:33 -04:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-09-05 06:36:35 -04:00
|
|
|
pub mod errors;
|
2023-02-10 10:26:39 -05:00
|
|
|
mod ops;
|
2022-09-05 06:36:35 -04:00
|
|
|
mod package_json;
|
2022-09-22 11:17:02 -04:00
|
|
|
mod path;
|
2023-02-10 10:26:39 -05:00
|
|
|
mod polyfill;
|
2022-09-05 06:36:35 -04:00
|
|
|
mod resolution;
|
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
pub use package_json::PackageJson;
|
2022-09-22 11:17:02 -04:00
|
|
|
pub use path::PathClean;
|
2023-02-10 10:26:39 -05:00
|
|
|
pub use polyfill::find_builtin_node_module;
|
|
|
|
pub use polyfill::is_builtin_node_module;
|
|
|
|
pub use polyfill::NodeModulePolyfill;
|
|
|
|
pub use polyfill::NodeModulePolyfillSpecifier;
|
|
|
|
pub use polyfill::SUPPORTED_BUILTIN_NODE_MODULES;
|
2022-08-30 14:09:22 -04:00
|
|
|
pub use resolution::get_closest_package_json;
|
2022-08-20 11:31:33 -04:00
|
|
|
pub use resolution::get_package_scope_config;
|
|
|
|
pub use resolution::legacy_main_resolve;
|
|
|
|
pub use resolution::package_exports_resolve;
|
|
|
|
pub use resolution::package_imports_resolve;
|
|
|
|
pub use resolution::package_resolve;
|
2022-11-16 14:41:27 -05:00
|
|
|
pub use resolution::path_to_declaration_path;
|
2022-08-30 14:09:22 -04:00
|
|
|
pub use resolution::NodeModuleKind;
|
2022-11-30 18:07:32 -05:00
|
|
|
pub use resolution::NodeResolutionMode;
|
2022-08-20 11:31:33 -04:00
|
|
|
pub use resolution::DEFAULT_CONDITIONS;
|
|
|
|
|
2022-08-24 12:07:49 -04:00
|
|
|
pub trait NodePermissions {
|
|
|
|
fn check_read(&mut self, path: &Path) -> Result<(), AnyError>;
|
|
|
|
}
|
|
|
|
|
2022-09-13 11:59:01 -04:00
|
|
|
pub trait RequireNpmResolver {
|
2022-08-20 11:31:33 -04:00
|
|
|
fn resolve_package_folder_from_package(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &Path,
|
2022-11-30 18:07:32 -05:00
|
|
|
mode: NodeResolutionMode,
|
2022-08-20 11:31:33 -04:00
|
|
|
) -> Result<PathBuf, AnyError>;
|
|
|
|
|
|
|
|
fn resolve_package_folder_from_path(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
) -> Result<PathBuf, AnyError>;
|
|
|
|
|
|
|
|
fn in_npm_package(&self, path: &Path) -> bool;
|
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
fn ensure_read_permission(
|
|
|
|
&self,
|
|
|
|
permissions: &mut dyn NodePermissions,
|
|
|
|
path: &Path,
|
|
|
|
) -> Result<(), AnyError>;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2022-08-24 08:54:20 -04:00
|
|
|
pub const MODULE_ES_SHIM: &str = include_str!("./module_es_shim.js");
|
|
|
|
|
2022-09-08 16:01:48 -04:00
|
|
|
pub static NODE_GLOBAL_THIS_NAME: Lazy<String> = Lazy::new(|| {
|
|
|
|
let now = std::time::SystemTime::now();
|
|
|
|
let seconds = now
|
|
|
|
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap()
|
|
|
|
.as_secs();
|
|
|
|
// use a changing variable name to make it hard to depend on this
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("__DENO_NODE_GLOBAL_THIS_{seconds}__")
|
2022-09-08 16:01:48 -04:00
|
|
|
});
|
|
|
|
|
2022-09-14 11:59:20 -04:00
|
|
|
pub static NODE_ENV_VAR_ALLOWLIST: Lazy<HashSet<String>> = Lazy::new(|| {
|
|
|
|
// The full list of environment variables supported by Node.js is available
|
|
|
|
// at https://nodejs.org/api/cli.html#environment-variables
|
|
|
|
let mut set = HashSet::new();
|
|
|
|
set.insert("NODE_DEBUG".to_string());
|
|
|
|
set.insert("NODE_OPTIONS".to_string());
|
|
|
|
set
|
|
|
|
});
|
|
|
|
|
2022-08-24 12:07:49 -04:00
|
|
|
pub fn init<P: NodePermissions + 'static>(
|
2022-09-13 11:59:01 -04:00
|
|
|
maybe_npm_resolver: Option<Rc<dyn RequireNpmResolver>>,
|
2022-08-20 11:31:33 -04:00
|
|
|
) -> Extension {
|
2023-01-08 17:48:46 -05:00
|
|
|
Extension::builder(env!("CARGO_PKG_NAME"))
|
2023-02-10 06:40:45 -05:00
|
|
|
.esm(include_js_files!(
|
|
|
|
"01_node.js",
|
|
|
|
"02_require.js",
|
|
|
|
"module_es_shim.js",
|
|
|
|
))
|
2022-08-09 15:06:01 -04:00
|
|
|
.ops(vec![
|
2023-02-10 10:26:39 -05:00
|
|
|
ops::op_require_init_paths::decl(),
|
|
|
|
ops::op_require_node_module_paths::decl::<P>(),
|
|
|
|
ops::op_require_proxy_path::decl(),
|
|
|
|
ops::op_require_is_deno_dir_package::decl(),
|
|
|
|
ops::op_require_resolve_deno_dir::decl(),
|
|
|
|
ops::op_require_is_request_relative::decl(),
|
|
|
|
ops::op_require_resolve_lookup_paths::decl(),
|
|
|
|
ops::op_require_try_self_parent_path::decl::<P>(),
|
|
|
|
ops::op_require_try_self::decl::<P>(),
|
|
|
|
ops::op_require_real_path::decl::<P>(),
|
|
|
|
ops::op_require_path_is_absolute::decl(),
|
|
|
|
ops::op_require_path_dirname::decl(),
|
|
|
|
ops::op_require_stat::decl::<P>(),
|
|
|
|
ops::op_require_path_resolve::decl(),
|
|
|
|
ops::op_require_path_basename::decl(),
|
|
|
|
ops::op_require_read_file::decl::<P>(),
|
|
|
|
ops::op_require_as_file_path::decl(),
|
|
|
|
ops::op_require_resolve_exports::decl::<P>(),
|
|
|
|
ops::op_require_read_closest_package_json::decl::<P>(),
|
|
|
|
ops::op_require_read_package_scope::decl::<P>(),
|
|
|
|
ops::op_require_package_imports_resolve::decl::<P>(),
|
|
|
|
ops::op_require_break_on_next_statement::decl(),
|
2022-08-09 15:06:01 -04:00
|
|
|
])
|
2022-08-09 22:09:51 -04:00
|
|
|
.state(move |state| {
|
2022-08-20 11:31:33 -04:00
|
|
|
if let Some(npm_resolver) = maybe_npm_resolver.clone() {
|
|
|
|
state.put(npm_resolver);
|
|
|
|
}
|
2022-08-09 22:09:51 -04:00
|
|
|
Ok(())
|
|
|
|
})
|
2022-08-09 15:06:01 -04:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2023-02-10 10:26:39 -05:00
|
|
|
pub async fn initialize_runtime(
|
|
|
|
js_runtime: &mut JsRuntime,
|
|
|
|
module_all_url: &str,
|
2022-12-19 11:09:54 -05:00
|
|
|
uses_local_node_modules_dir: bool,
|
2023-02-10 10:26:39 -05:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let source_code = &format!(
|
|
|
|
r#"(async function loadBuiltinNodeModules(moduleAllUrl, nodeGlobalThisName, usesLocalNodeModulesDir) {{
|
|
|
|
const moduleAll = await import(moduleAllUrl);
|
|
|
|
Deno[Deno.internal].node.initialize(moduleAll.default, nodeGlobalThisName);
|
|
|
|
if (usesLocalNodeModulesDir) {{
|
|
|
|
Deno[Deno.internal].require.setUsesLocalNodeModulesDir();
|
|
|
|
}}
|
|
|
|
}})('{}', '{}', {});"#,
|
|
|
|
module_all_url,
|
|
|
|
NODE_GLOBAL_THIS_NAME.as_str(),
|
|
|
|
uses_local_node_modules_dir,
|
|
|
|
);
|
|
|
|
|
|
|
|
let value =
|
|
|
|
js_runtime.execute_script(&located_script_name!(), source_code)?;
|
|
|
|
js_runtime.resolve_value(value).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_cjs_module(
|
|
|
|
js_runtime: &mut JsRuntime,
|
|
|
|
module: &str,
|
|
|
|
main: bool,
|
|
|
|
inspect_brk: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
fn escape_for_single_quote_string(text: &str) -> String {
|
|
|
|
text.replace('\\', r"\\").replace('\'', r"\'")
|
|
|
|
}
|
|
|
|
|
|
|
|
let source_code = &format!(
|
|
|
|
r#"(function loadCjsModule(module, inspectBrk) {{
|
|
|
|
if (inspectBrk) {{
|
|
|
|
Deno[Deno.internal].require.setInspectBrk();
|
|
|
|
}}
|
|
|
|
Deno[Deno.internal].require.Module._load(module, null, {main});
|
|
|
|
}})('{module}', {inspect_brk});"#,
|
|
|
|
main = main,
|
|
|
|
module = escape_for_single_quote_string(module),
|
|
|
|
inspect_brk = inspect_brk,
|
|
|
|
);
|
|
|
|
|
|
|
|
js_runtime.execute_script(&located_script_name!(), source_code)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn initialize_binary_command(
|
|
|
|
js_runtime: &mut JsRuntime,
|
|
|
|
binary_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// overwrite what's done in deno_std in order to set the binary arg name
|
|
|
|
let source_code = &format!(
|
|
|
|
r#"(async function initializeBinaryCommand(binaryName) {{
|
|
|
|
const process = Deno[Deno.internal].node.globalThis.process;
|
|
|
|
Object.defineProperty(process.argv, "0", {{
|
|
|
|
get: () => binaryName,
|
|
|
|
}});
|
|
|
|
}})('{binary_name}');"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
let value =
|
|
|
|
js_runtime.execute_script(&located_script_name!(), source_code)?;
|
|
|
|
js_runtime.resolve_value(value).await?;
|
|
|
|
Ok(())
|
2022-08-30 14:09:22 -04:00
|
|
|
}
|