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
|
|
|
|
2023-05-08 11:02:02 -04:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-08-09 15:06:01 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-02-10 10:26:39 -05:00
|
|
|
use deno_core::located_script_name;
|
2023-09-14 02:29:44 -04:00
|
|
|
use deno_core::op2;
|
2023-05-27 09:42:20 -04:00
|
|
|
use deno_core::url::Url;
|
2023-07-05 13:15:10 -04:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use deno_core::v8;
|
2023-07-19 04:30:04 -04:00
|
|
|
use deno_core::v8::ExternalReference;
|
2023-02-10 10:26:39 -05:00
|
|
|
use deno_core::JsRuntime;
|
2023-04-21 21:02:46 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-05-08 11:02:02 -04:00
|
|
|
use deno_fs::sync::MaybeSend;
|
|
|
|
use deno_fs::sync::MaybeSync;
|
2022-09-08 16:01:48 -04:00
|
|
|
use once_cell::sync::Lazy;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-21 16:38:10 -04:00
|
|
|
pub mod analyze;
|
2022-09-05 06:36:35 -04:00
|
|
|
pub mod errors;
|
2023-07-19 04:30:04 -04:00
|
|
|
mod global;
|
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::is_builtin_node_module;
|
|
|
|
pub use polyfill::SUPPORTED_BUILTIN_NODE_MODULES;
|
2023-07-02 14:19:30 -04:00
|
|
|
pub use polyfill::SUPPORTED_BUILTIN_NODE_MODULES_WITH_PREFIX;
|
2022-08-30 14:09:22 -04:00
|
|
|
pub use resolution::NodeModuleKind;
|
2023-04-24 19:44:35 -04:00
|
|
|
pub use resolution::NodeResolution;
|
2022-11-30 18:07:32 -05:00
|
|
|
pub use resolution::NodeResolutionMode;
|
2023-04-24 19:44:35 -04:00
|
|
|
pub use resolution::NodeResolver;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
use crate::global::global_object_middleware;
|
|
|
|
use crate::global::global_template_middleware;
|
|
|
|
|
2022-08-24 12:07:49 -04:00
|
|
|
pub trait NodePermissions {
|
2023-05-27 09:42:20 -04:00
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
url: &Url,
|
|
|
|
api_name: &str,
|
|
|
|
) -> Result<(), AnyError>;
|
2023-04-24 21:07:48 -04:00
|
|
|
fn check_read(&self, path: &Path) -> Result<(), AnyError>;
|
2023-07-31 16:29:09 -04:00
|
|
|
fn check_sys(&self, kind: &str, api_name: &str) -> Result<(), AnyError>;
|
2022-08-24 12:07:49 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
pub(crate) struct AllowAllNodePermissions;
|
|
|
|
|
|
|
|
impl NodePermissions for AllowAllNodePermissions {
|
2023-05-27 09:42:20 -04:00
|
|
|
fn check_net_url(
|
|
|
|
&mut self,
|
|
|
|
_url: &Url,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-04-24 21:07:48 -04:00
|
|
|
fn check_read(&self, _path: &Path) -> Result<(), AnyError> {
|
2023-04-21 21:02:46 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-31 16:29:09 -04:00
|
|
|
fn check_sys(&self, _kind: &str, _api_name: &str) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-04-21 21:02:46 -04:00
|
|
|
}
|
|
|
|
|
2023-05-08 11:02:02 -04:00
|
|
|
#[allow(clippy::disallowed_types)]
|
|
|
|
pub type NpmResolverRc = deno_fs::sync::MaybeArc<dyn NpmResolver>;
|
|
|
|
|
|
|
|
pub trait NpmResolver: std::fmt::Debug + MaybeSend + MaybeSync {
|
2023-04-21 21:02:46 -04:00
|
|
|
/// Resolves an npm package folder path from an npm package referrer.
|
2022-08-20 11:31:33 -04:00
|
|
|
fn resolve_package_folder_from_package(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
2023-04-21 21:02:46 -04:00
|
|
|
referrer: &ModuleSpecifier,
|
2022-11-30 18:07:32 -05:00
|
|
|
mode: NodeResolutionMode,
|
2022-08-20 11:31:33 -04:00
|
|
|
) -> Result<PathBuf, AnyError>;
|
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
/// Resolves the npm package folder path from the specified path.
|
2022-08-20 11:31:33 -04:00
|
|
|
fn resolve_package_folder_from_path(
|
|
|
|
&self,
|
2023-09-26 16:42:39 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-07-17 14:00:44 -04:00
|
|
|
) -> Result<Option<PathBuf>, AnyError>;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool;
|
|
|
|
|
|
|
|
fn in_npm_package_at_path(&self, path: &Path) -> bool {
|
|
|
|
let specifier =
|
|
|
|
match ModuleSpecifier::from_file_path(path.to_path_buf().clean()) {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
self.in_npm_package(&specifier)
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
fn ensure_read_permission(
|
|
|
|
&self,
|
2023-04-24 21:07:48 -04:00
|
|
|
permissions: &dyn NodePermissions,
|
2023-01-10 08:35:44 -05:00
|
|
|
path: &Path,
|
|
|
|
) -> Result<(), AnyError>;
|
2022-08-20 11:31:33 -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
|
|
|
|
});
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2023-02-14 11:38:45 -05:00
|
|
|
fn op_node_build_os() -> String {
|
2023-08-05 19:47:15 -04:00
|
|
|
env!("TARGET").split('-').nth(2).unwrap().to_string()
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_is_any_arraybuffer(value: &v8::Value) -> bool {
|
|
|
|
value.is_array_buffer() || value.is_shared_array_buffer()
|
2023-07-05 13:15:10 -04:00
|
|
|
}
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_node_is_promise_rejected(value: v8::Local<v8::Value>) -> bool {
|
|
|
|
let Ok(promise) = v8::Local::<v8::Promise>::try_from(value) else {
|
2023-07-06 07:05:10 -04:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
promise.state() == v8::PromiseState::Rejected
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(deno_node,
|
|
|
|
deps = [ deno_io, deno_fs ],
|
2023-05-04 08:36:38 -04:00
|
|
|
parameters = [P: NodePermissions],
|
2023-03-17 14:22:15 -04:00
|
|
|
ops = [
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::crypto::op_node_create_decipheriv,
|
|
|
|
ops::crypto::op_node_cipheriv_encrypt,
|
|
|
|
ops::crypto::op_node_cipheriv_final,
|
2023-09-06 01:31:50 -04:00
|
|
|
ops::crypto::op_node_cipheriv_set_aad,
|
|
|
|
ops::crypto::op_node_decipheriv_set_aad,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::crypto::op_node_create_cipheriv,
|
|
|
|
ops::crypto::op_node_create_hash,
|
2023-06-26 22:04:49 -04:00
|
|
|
ops::crypto::op_node_get_hashes,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::crypto::op_node_decipheriv_decrypt,
|
|
|
|
ops::crypto::op_node_decipheriv_final,
|
|
|
|
ops::crypto::op_node_hash_update,
|
|
|
|
ops::crypto::op_node_hash_update_str,
|
|
|
|
ops::crypto::op_node_hash_digest,
|
|
|
|
ops::crypto::op_node_hash_digest_hex,
|
|
|
|
ops::crypto::op_node_hash_clone,
|
|
|
|
ops::crypto::op_node_private_encrypt,
|
|
|
|
ops::crypto::op_node_private_decrypt,
|
|
|
|
ops::crypto::op_node_public_encrypt,
|
|
|
|
ops::crypto::op_node_check_prime,
|
|
|
|
ops::crypto::op_node_check_prime_async,
|
|
|
|
ops::crypto::op_node_check_prime_bytes,
|
|
|
|
ops::crypto::op_node_check_prime_bytes_async,
|
2023-04-27 10:10:59 -04:00
|
|
|
ops::crypto::op_node_gen_prime,
|
|
|
|
ops::crypto::op_node_gen_prime_async,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::crypto::op_node_pbkdf2,
|
|
|
|
ops::crypto::op_node_pbkdf2_async,
|
|
|
|
ops::crypto::op_node_hkdf,
|
|
|
|
ops::crypto::op_node_hkdf_async,
|
|
|
|
ops::crypto::op_node_generate_secret,
|
|
|
|
ops::crypto::op_node_generate_secret_async,
|
|
|
|
ops::crypto::op_node_sign,
|
|
|
|
ops::crypto::op_node_generate_rsa,
|
|
|
|
ops::crypto::op_node_generate_rsa_async,
|
|
|
|
ops::crypto::op_node_dsa_generate,
|
|
|
|
ops::crypto::op_node_dsa_generate_async,
|
|
|
|
ops::crypto::op_node_ec_generate,
|
|
|
|
ops::crypto::op_node_ec_generate_async,
|
|
|
|
ops::crypto::op_node_ed25519_generate,
|
|
|
|
ops::crypto::op_node_ed25519_generate_async,
|
|
|
|
ops::crypto::op_node_x25519_generate,
|
|
|
|
ops::crypto::op_node_x25519_generate_async,
|
|
|
|
ops::crypto::op_node_dh_generate_group,
|
|
|
|
ops::crypto::op_node_dh_generate_group_async,
|
|
|
|
ops::crypto::op_node_dh_generate,
|
2023-05-15 13:41:53 -04:00
|
|
|
ops::crypto::op_node_dh_generate2,
|
|
|
|
ops::crypto::op_node_dh_compute_secret,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::crypto::op_node_dh_generate_async,
|
|
|
|
ops::crypto::op_node_verify,
|
|
|
|
ops::crypto::op_node_random_int,
|
|
|
|
ops::crypto::op_node_scrypt_sync,
|
|
|
|
ops::crypto::op_node_scrypt_async,
|
2023-04-27 12:31:35 -04:00
|
|
|
ops::crypto::op_node_ecdh_generate_keys,
|
|
|
|
ops::crypto::op_node_ecdh_compute_secret,
|
|
|
|
ops::crypto::op_node_ecdh_compute_public_key,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::crypto::x509::op_node_x509_parse,
|
|
|
|
ops::crypto::x509::op_node_x509_ca,
|
|
|
|
ops::crypto::x509::op_node_x509_check_email,
|
|
|
|
ops::crypto::x509::op_node_x509_fingerprint,
|
|
|
|
ops::crypto::x509::op_node_x509_fingerprint256,
|
|
|
|
ops::crypto::x509::op_node_x509_fingerprint512,
|
|
|
|
ops::crypto::x509::op_node_x509_get_issuer,
|
|
|
|
ops::crypto::x509::op_node_x509_get_subject,
|
|
|
|
ops::crypto::x509::op_node_x509_get_valid_from,
|
|
|
|
ops::crypto::x509::op_node_x509_get_valid_to,
|
|
|
|
ops::crypto::x509::op_node_x509_get_serial_number,
|
|
|
|
ops::crypto::x509::op_node_x509_key_usage,
|
|
|
|
ops::winerror::op_node_sys_to_uv_error,
|
|
|
|
ops::v8::op_v8_cached_data_version_tag,
|
|
|
|
ops::v8::op_v8_get_heap_statistics,
|
|
|
|
ops::idna::op_node_idna_domain_to_ascii,
|
|
|
|
ops::idna::op_node_idna_domain_to_unicode,
|
|
|
|
ops::idna::op_node_idna_punycode_decode,
|
|
|
|
ops::idna::op_node_idna_punycode_encode,
|
|
|
|
ops::zlib::op_zlib_new,
|
|
|
|
ops::zlib::op_zlib_close,
|
|
|
|
ops::zlib::op_zlib_close_if_pending,
|
|
|
|
ops::zlib::op_zlib_write,
|
|
|
|
ops::zlib::op_zlib_write_async,
|
|
|
|
ops::zlib::op_zlib_init,
|
|
|
|
ops::zlib::op_zlib_reset,
|
2023-06-24 10:12:08 -04:00
|
|
|
ops::zlib::brotli::op_brotli_compress,
|
|
|
|
ops::zlib::brotli::op_brotli_compress_async,
|
|
|
|
ops::zlib::brotli::op_create_brotli_compress,
|
|
|
|
ops::zlib::brotli::op_brotli_compress_stream,
|
|
|
|
ops::zlib::brotli::op_brotli_compress_stream_end,
|
|
|
|
ops::zlib::brotli::op_brotli_decompress,
|
|
|
|
ops::zlib::brotli::op_brotli_decompress_async,
|
|
|
|
ops::zlib::brotli::op_create_brotli_decompress,
|
|
|
|
ops::zlib::brotli::op_brotli_decompress_stream,
|
|
|
|
ops::zlib::brotli::op_brotli_decompress_stream_end,
|
2023-05-27 09:42:20 -04:00
|
|
|
ops::http::op_node_http_request<P>,
|
2023-09-15 15:51:25 -04:00
|
|
|
ops::http2::op_http2_connect,
|
|
|
|
ops::http2::op_http2_poll_client_connection,
|
|
|
|
ops::http2::op_http2_client_request,
|
|
|
|
ops::http2::op_http2_client_get_response,
|
|
|
|
ops::http2::op_http2_client_get_response_body_chunk,
|
|
|
|
ops::http2::op_http2_client_send_data,
|
|
|
|
ops::http2::op_http2_client_end_stream,
|
|
|
|
ops::http2::op_http2_client_reset_stream,
|
|
|
|
ops::http2::op_http2_client_send_trailers,
|
|
|
|
ops::http2::op_http2_client_get_response_trailers,
|
|
|
|
ops::http2::op_http2_accept,
|
|
|
|
ops::http2::op_http2_listen,
|
|
|
|
ops::http2::op_http2_send_response,
|
2023-07-31 16:29:09 -04:00
|
|
|
ops::os::op_node_os_get_priority<P>,
|
|
|
|
ops::os::op_node_os_set_priority<P>,
|
|
|
|
ops::os::op_node_os_username<P>,
|
2023-03-17 14:22:15 -04:00
|
|
|
op_node_build_os,
|
2023-07-05 13:15:10 -04:00
|
|
|
op_is_any_arraybuffer,
|
2023-07-06 07:05:10 -04:00
|
|
|
op_node_is_promise_rejected,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::require::op_require_init_paths,
|
2023-05-04 08:36:38 -04:00
|
|
|
ops::require::op_require_node_module_paths<P>,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::require::op_require_proxy_path,
|
|
|
|
ops::require::op_require_is_deno_dir_package,
|
|
|
|
ops::require::op_require_resolve_deno_dir,
|
|
|
|
ops::require::op_require_is_request_relative,
|
|
|
|
ops::require::op_require_resolve_lookup_paths,
|
2023-05-04 08:36:38 -04:00
|
|
|
ops::require::op_require_try_self_parent_path<P>,
|
|
|
|
ops::require::op_require_try_self<P>,
|
|
|
|
ops::require::op_require_real_path<P>,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::require::op_require_path_is_absolute,
|
|
|
|
ops::require::op_require_path_dirname,
|
2023-05-04 08:36:38 -04:00
|
|
|
ops::require::op_require_stat<P>,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::require::op_require_path_resolve,
|
|
|
|
ops::require::op_require_path_basename,
|
2023-05-04 08:36:38 -04:00
|
|
|
ops::require::op_require_read_file<P>,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::require::op_require_as_file_path,
|
2023-05-04 08:36:38 -04:00
|
|
|
ops::require::op_require_resolve_exports<P>,
|
|
|
|
ops::require::op_require_read_closest_package_json<P>,
|
|
|
|
ops::require::op_require_read_package_scope<P>,
|
|
|
|
ops::require::op_require_package_imports_resolve<P>,
|
2023-04-24 06:22:21 -04:00
|
|
|
ops::require::op_require_break_on_next_statement,
|
2023-03-17 14:22:15 -04:00
|
|
|
],
|
2023-03-20 14:05:13 -04:00
|
|
|
esm_entry_point = "ext:deno_node/02_init.js",
|
2023-03-17 14:22:15 -04:00
|
|
|
esm = [
|
2023-02-14 11:38:45 -05:00
|
|
|
dir "polyfills",
|
2023-03-20 14:05:13 -04:00
|
|
|
"00_globals.js",
|
|
|
|
"02_init.js",
|
2023-06-24 10:12:08 -04:00
|
|
|
"_brotli.js",
|
2023-02-14 11:38:45 -05:00
|
|
|
"_events.mjs",
|
|
|
|
"_fs/_fs_access.ts",
|
|
|
|
"_fs/_fs_appendFile.ts",
|
|
|
|
"_fs/_fs_chmod.ts",
|
|
|
|
"_fs/_fs_chown.ts",
|
|
|
|
"_fs/_fs_close.ts",
|
|
|
|
"_fs/_fs_common.ts",
|
|
|
|
"_fs/_fs_constants.ts",
|
|
|
|
"_fs/_fs_copy.ts",
|
|
|
|
"_fs/_fs_dir.ts",
|
|
|
|
"_fs/_fs_dirent.ts",
|
|
|
|
"_fs/_fs_exists.ts",
|
|
|
|
"_fs/_fs_fdatasync.ts",
|
|
|
|
"_fs/_fs_fstat.ts",
|
|
|
|
"_fs/_fs_fsync.ts",
|
|
|
|
"_fs/_fs_ftruncate.ts",
|
|
|
|
"_fs/_fs_futimes.ts",
|
|
|
|
"_fs/_fs_link.ts",
|
|
|
|
"_fs/_fs_lstat.ts",
|
|
|
|
"_fs/_fs_mkdir.ts",
|
|
|
|
"_fs/_fs_mkdtemp.ts",
|
|
|
|
"_fs/_fs_open.ts",
|
|
|
|
"_fs/_fs_opendir.ts",
|
|
|
|
"_fs/_fs_read.ts",
|
|
|
|
"_fs/_fs_readdir.ts",
|
|
|
|
"_fs/_fs_readFile.ts",
|
|
|
|
"_fs/_fs_readlink.ts",
|
|
|
|
"_fs/_fs_realpath.ts",
|
|
|
|
"_fs/_fs_rename.ts",
|
|
|
|
"_fs/_fs_rm.ts",
|
|
|
|
"_fs/_fs_rmdir.ts",
|
|
|
|
"_fs/_fs_stat.ts",
|
|
|
|
"_fs/_fs_symlink.ts",
|
|
|
|
"_fs/_fs_truncate.ts",
|
|
|
|
"_fs/_fs_unlink.ts",
|
|
|
|
"_fs/_fs_utimes.ts",
|
|
|
|
"_fs/_fs_watch.ts",
|
|
|
|
"_fs/_fs_write.mjs",
|
|
|
|
"_fs/_fs_writeFile.ts",
|
|
|
|
"_fs/_fs_writev.mjs",
|
|
|
|
"_http_agent.mjs",
|
|
|
|
"_http_common.ts",
|
|
|
|
"_http_outgoing.ts",
|
|
|
|
"_next_tick.ts",
|
|
|
|
"_process/exiting.ts",
|
|
|
|
"_process/process.ts",
|
|
|
|
"_process/streams.mjs",
|
|
|
|
"_readline.mjs",
|
|
|
|
"_stream.mjs",
|
|
|
|
"_tls_common.ts",
|
|
|
|
"_tls_wrap.ts",
|
|
|
|
"_util/_util_callbackify.ts",
|
|
|
|
"_util/asserts.ts",
|
|
|
|
"_util/async.ts",
|
|
|
|
"_util/os.ts",
|
|
|
|
"_util/std_asserts.ts",
|
|
|
|
"_util/std_fmt_colors.ts",
|
|
|
|
"_util/std_testing_diff.ts",
|
|
|
|
"_utils.ts",
|
|
|
|
"_zlib_binding.mjs",
|
|
|
|
"_zlib.mjs",
|
|
|
|
"assertion_error.ts",
|
|
|
|
"inspector.ts",
|
|
|
|
"internal_binding/_libuv_winerror.ts",
|
|
|
|
"internal_binding/_listen.ts",
|
|
|
|
"internal_binding/_node.ts",
|
|
|
|
"internal_binding/_timingSafeEqual.ts",
|
|
|
|
"internal_binding/_utils.ts",
|
|
|
|
"internal_binding/ares.ts",
|
|
|
|
"internal_binding/async_wrap.ts",
|
|
|
|
"internal_binding/buffer.ts",
|
|
|
|
"internal_binding/cares_wrap.ts",
|
|
|
|
"internal_binding/connection_wrap.ts",
|
|
|
|
"internal_binding/constants.ts",
|
|
|
|
"internal_binding/crypto.ts",
|
|
|
|
"internal_binding/handle_wrap.ts",
|
|
|
|
"internal_binding/mod.ts",
|
|
|
|
"internal_binding/node_file.ts",
|
|
|
|
"internal_binding/node_options.ts",
|
|
|
|
"internal_binding/pipe_wrap.ts",
|
|
|
|
"internal_binding/stream_wrap.ts",
|
|
|
|
"internal_binding/string_decoder.ts",
|
|
|
|
"internal_binding/symbols.ts",
|
|
|
|
"internal_binding/tcp_wrap.ts",
|
|
|
|
"internal_binding/types.ts",
|
|
|
|
"internal_binding/udp_wrap.ts",
|
|
|
|
"internal_binding/util.ts",
|
|
|
|
"internal_binding/uv.ts",
|
|
|
|
"internal/assert.mjs",
|
|
|
|
"internal/async_hooks.ts",
|
|
|
|
"internal/buffer.mjs",
|
|
|
|
"internal/child_process.ts",
|
|
|
|
"internal/cli_table.ts",
|
|
|
|
"internal/console/constructor.mjs",
|
|
|
|
"internal/constants.ts",
|
|
|
|
"internal/crypto/_keys.ts",
|
|
|
|
"internal/crypto/_randomBytes.ts",
|
2023-09-23 04:04:55 -04:00
|
|
|
"internal/crypto/_randomFill.mjs",
|
2023-02-14 11:38:45 -05:00
|
|
|
"internal/crypto/_randomInt.ts",
|
|
|
|
"internal/crypto/certificate.ts",
|
|
|
|
"internal/crypto/cipher.ts",
|
|
|
|
"internal/crypto/constants.ts",
|
|
|
|
"internal/crypto/diffiehellman.ts",
|
|
|
|
"internal/crypto/hash.ts",
|
|
|
|
"internal/crypto/hkdf.ts",
|
|
|
|
"internal/crypto/keygen.ts",
|
|
|
|
"internal/crypto/keys.ts",
|
|
|
|
"internal/crypto/pbkdf2.ts",
|
|
|
|
"internal/crypto/random.ts",
|
|
|
|
"internal/crypto/scrypt.ts",
|
|
|
|
"internal/crypto/sig.ts",
|
|
|
|
"internal/crypto/util.ts",
|
|
|
|
"internal/crypto/x509.ts",
|
|
|
|
"internal/dgram.ts",
|
|
|
|
"internal/dns/promises.ts",
|
|
|
|
"internal/dns/utils.ts",
|
|
|
|
"internal/dtrace.ts",
|
|
|
|
"internal/error_codes.ts",
|
|
|
|
"internal/errors.ts",
|
|
|
|
"internal/event_target.mjs",
|
|
|
|
"internal/fixed_queue.ts",
|
|
|
|
"internal/fs/streams.mjs",
|
|
|
|
"internal/fs/utils.mjs",
|
feat(node_compat): Added base implementation of FileHandle (#19294)
<!--
Before submitting a PR, please read https://deno.com/manual/contributing
1. Give the PR a descriptive title.
Examples of good title:
- fix(std/http): Fix race condition in server
- docs(console): Update docstrings
- feat(doc): Handle nested reexports
Examples of bad title:
- fix #7123
- update docs
- fix bugs
2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->
## WHY
ref: https://github.com/denoland/deno/issues/19165
Node's fs/promises includes a FileHandle class, but deno does not. The
open function in Node's fs/promises returns a FileHandle, which provides
an IO interface to the file. However, deno's open function returns a
resource id.
### deno
```js
> const fs = await import("node:fs/promises");
undefined
> const file3 = await fs.open("./README.md");
undefined
> file3
3
> file3.read
undefined
Node:
```
### Node
```js
> const fs = await import("fs/promises");
undefined
> const file3 = await fs.open("./tests/e2e_unit/testdata/file.txt");
undefined
> file3
FileHandle {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
close: [Function: close],
[Symbol(kCapture)]: false,
[Symbol(kHandle)]: FileHandle {},
[Symbol(kFd)]: 24,
[Symbol(kRefs)]: 1,
[Symbol(kClosePromise)]: null
}
> file3.read
[Function: read]
```
To be compatible with Node, deno's open function should also return a
FileHandle.
## WHAT
I have implemented the first step in adding a FileHandle.
- Changed the return value of the open function to a FileHandle object
- Implemented the readFile method in FileHandle
- Add test code
## What to do next
This PR is the first step in adding a FileHandle, and there are things
that should be done next.
- Add functionality equivalent to Node's FileHandle to FileHandle
(currently there is only readFile)
---------
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
2023-06-02 10:28:05 -04:00
|
|
|
"internal/fs/handle.ts",
|
2023-02-14 11:38:45 -05:00
|
|
|
"internal/hide_stack_frames.ts",
|
|
|
|
"internal/http.ts",
|
|
|
|
"internal/idna.ts",
|
|
|
|
"internal/net.ts",
|
|
|
|
"internal/normalize_encoding.mjs",
|
|
|
|
"internal/options.ts",
|
|
|
|
"internal/primordials.mjs",
|
|
|
|
"internal/process/per_thread.mjs",
|
|
|
|
"internal/querystring.ts",
|
|
|
|
"internal/readline/callbacks.mjs",
|
|
|
|
"internal/readline/emitKeypressEvents.mjs",
|
|
|
|
"internal/readline/interface.mjs",
|
|
|
|
"internal/readline/promises.mjs",
|
|
|
|
"internal/readline/symbols.mjs",
|
|
|
|
"internal/readline/utils.mjs",
|
|
|
|
"internal/stream_base_commons.ts",
|
|
|
|
"internal/streams/add-abort-signal.mjs",
|
|
|
|
"internal/streams/buffer_list.mjs",
|
|
|
|
"internal/streams/destroy.mjs",
|
|
|
|
"internal/streams/duplex.mjs",
|
|
|
|
"internal/streams/end-of-stream.mjs",
|
|
|
|
"internal/streams/lazy_transform.mjs",
|
|
|
|
"internal/streams/passthrough.mjs",
|
|
|
|
"internal/streams/readable.mjs",
|
|
|
|
"internal/streams/state.mjs",
|
|
|
|
"internal/streams/transform.mjs",
|
|
|
|
"internal/streams/utils.mjs",
|
|
|
|
"internal/streams/writable.mjs",
|
|
|
|
"internal/test/binding.ts",
|
|
|
|
"internal/timers.mjs",
|
|
|
|
"internal/url.ts",
|
|
|
|
"internal/util.mjs",
|
|
|
|
"internal/util/comparisons.ts",
|
|
|
|
"internal/util/debuglog.ts",
|
|
|
|
"internal/util/inspect.mjs",
|
|
|
|
"internal/util/types.ts",
|
|
|
|
"internal/validators.mjs",
|
|
|
|
"path/_constants.ts",
|
|
|
|
"path/_interface.ts",
|
|
|
|
"path/_util.ts",
|
2023-04-18 04:44:25 -04:00
|
|
|
"path/_posix.ts",
|
|
|
|
"path/_win32.ts",
|
2023-02-14 11:38:45 -05:00
|
|
|
"path/common.ts",
|
|
|
|
"path/mod.ts",
|
|
|
|
"path/separator.ts",
|
|
|
|
"readline/promises.ts",
|
2023-07-31 14:19:15 -04:00
|
|
|
"wasi.ts",
|
|
|
|
"assert.ts" with_specifier "node:assert",
|
|
|
|
"assert/strict.ts" with_specifier "node:assert/strict",
|
|
|
|
"async_hooks.ts" with_specifier "node:async_hooks",
|
|
|
|
"buffer.ts" with_specifier "node:buffer",
|
|
|
|
"child_process.ts" with_specifier "node:child_process",
|
|
|
|
"cluster.ts" with_specifier "node:cluster",
|
|
|
|
"console.ts" with_specifier "node:console",
|
|
|
|
"constants.ts" with_specifier "node:constants",
|
|
|
|
"crypto.ts" with_specifier "node:crypto",
|
|
|
|
"dgram.ts" with_specifier "node:dgram",
|
|
|
|
"diagnostics_channel.ts" with_specifier "node:diagnostics_channel",
|
|
|
|
"dns.ts" with_specifier "node:dns",
|
|
|
|
"dns/promises.ts" with_specifier "node:dns/promises",
|
|
|
|
"domain.ts" with_specifier "node:domain",
|
|
|
|
"events.ts" with_specifier "node:events",
|
|
|
|
"fs.ts" with_specifier "node:fs",
|
|
|
|
"fs/promises.ts" with_specifier "node:fs/promises",
|
|
|
|
"http.ts" with_specifier "node:http",
|
|
|
|
"http2.ts" with_specifier "node:http2",
|
|
|
|
"https.ts" with_specifier "node:https",
|
|
|
|
"01_require.js" with_specifier "node:module",
|
|
|
|
"net.ts" with_specifier "node:net",
|
|
|
|
"os.ts" with_specifier "node:os",
|
|
|
|
"path.ts" with_specifier "node:path",
|
|
|
|
"path/posix.ts" with_specifier "node:path/posix",
|
|
|
|
"path/win32.ts" with_specifier "node:path/win32",
|
|
|
|
"perf_hooks.ts" with_specifier "node:perf_hooks",
|
|
|
|
"process.ts" with_specifier "node:process",
|
|
|
|
"punycode.ts" with_specifier "node:punycode",
|
|
|
|
"querystring.ts" with_specifier "node:querystring",
|
|
|
|
"readline.ts" with_specifier "node:readline",
|
2023-08-04 08:30:48 -04:00
|
|
|
"repl.ts" with_specifier "node:repl",
|
2023-07-31 14:19:15 -04:00
|
|
|
"stream.ts" with_specifier "node:stream",
|
|
|
|
"stream/consumers.mjs" with_specifier "node:stream/consumers",
|
|
|
|
"stream/promises.mjs" with_specifier "node:stream/promises",
|
|
|
|
"stream/web.ts" with_specifier "node:stream/web",
|
|
|
|
"string_decoder.ts" with_specifier "node:string_decoder",
|
|
|
|
"sys.ts" with_specifier "node:sys",
|
2023-08-01 19:17:38 -04:00
|
|
|
"testing.ts" with_specifier "node:test",
|
2023-07-31 14:19:15 -04:00
|
|
|
"timers.ts" with_specifier "node:timers",
|
|
|
|
"timers/promises.ts" with_specifier "node:timers/promises",
|
|
|
|
"tls.ts" with_specifier "node:tls",
|
|
|
|
"tty.ts" with_specifier "node:tty",
|
|
|
|
"url.ts" with_specifier "node:url",
|
|
|
|
"util.ts" with_specifier "node:util",
|
|
|
|
"util/types.ts" with_specifier "node:util/types",
|
|
|
|
"v8.ts" with_specifier "node:v8",
|
|
|
|
"vm.ts" with_specifier "node:vm",
|
|
|
|
"worker_threads.ts" with_specifier "node:worker_threads",
|
|
|
|
"zlib.ts" with_specifier "node:zlib",
|
2023-03-17 14:22:15 -04:00
|
|
|
],
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
2023-05-08 11:02:02 -04:00
|
|
|
maybe_npm_resolver: Option<NpmResolverRc>,
|
|
|
|
fs: deno_fs::FileSystemRc,
|
2023-03-17 14:22:15 -04:00
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
2023-05-05 12:44:24 -04:00
|
|
|
let fs = options.fs;
|
2023-04-24 19:44:35 -04:00
|
|
|
state.put(fs.clone());
|
2023-03-17 18:15:27 -04:00
|
|
|
if let Some(npm_resolver) = options.maybe_npm_resolver {
|
2023-04-24 19:44:35 -04:00
|
|
|
state.put(npm_resolver.clone());
|
|
|
|
state.put(Rc::new(NodeResolver::new(
|
|
|
|
fs,
|
|
|
|
npm_resolver,
|
|
|
|
)))
|
2023-03-17 14:22:15 -04:00
|
|
|
}
|
2023-07-19 04:30:04 -04:00
|
|
|
},
|
|
|
|
global_template_middleware = global_template_middleware,
|
|
|
|
global_object_middleware = global_object_middleware,
|
2023-07-31 14:19:15 -04:00
|
|
|
customizer = |ext: &mut deno_core::Extension| {
|
2023-07-19 04:30:04 -04:00
|
|
|
let mut external_references = Vec::with_capacity(7);
|
|
|
|
|
|
|
|
global::GETTER_MAP_FN.with(|getter| {
|
|
|
|
external_references.push(ExternalReference {
|
|
|
|
named_getter: *getter,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
global::SETTER_MAP_FN.with(|setter| {
|
|
|
|
external_references.push(ExternalReference {
|
|
|
|
named_setter: *setter,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
global::QUERY_MAP_FN.with(|query| {
|
|
|
|
external_references.push(ExternalReference {
|
|
|
|
named_getter: *query,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
global::DELETER_MAP_FN.with(|deleter| {
|
|
|
|
external_references.push(ExternalReference {
|
|
|
|
named_getter: *deleter,
|
|
|
|
},);
|
|
|
|
});
|
|
|
|
global::ENUMERATOR_MAP_FN.with(|enumerator| {
|
|
|
|
external_references.push(ExternalReference {
|
|
|
|
enumerator: *enumerator,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
global::DEFINER_MAP_FN.with(|definer| {
|
|
|
|
external_references.push(ExternalReference {
|
|
|
|
named_definer: *definer,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
global::DESCRIPTOR_MAP_FN.with(|descriptor| {
|
|
|
|
external_references.push(ExternalReference {
|
|
|
|
named_getter: *descriptor,
|
|
|
|
});
|
|
|
|
});
|
2023-07-31 14:19:15 -04:00
|
|
|
ext.external_references.to_mut().extend(external_references);
|
2023-07-19 04:30:04 -04:00
|
|
|
},
|
2023-03-17 14:22:15 -04:00
|
|
|
);
|
2023-03-09 09:56:19 -05:00
|
|
|
|
2023-02-10 10:26:39 -05:00
|
|
|
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"\'")
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
let source_code = format!(
|
2023-03-20 14:05:13 -04:00
|
|
|
r#"(function loadCjsModule(moduleName, isMain, inspectBrk) {{
|
|
|
|
Deno[Deno.internal].node.loadCjsModule(moduleName, isMain, inspectBrk);
|
|
|
|
}})('{module}', {main}, {inspect_brk});"#,
|
2023-02-10 10:26:39 -05:00
|
|
|
main = main,
|
|
|
|
module = escape_for_single_quote_string(module),
|
|
|
|
inspect_brk = inspect_brk,
|
2023-04-04 08:46:31 -04:00
|
|
|
)
|
|
|
|
.into();
|
2023-02-10 10:26:39 -05:00
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
js_runtime.execute_script(located_script_name!(), source_code)?;
|
2023-02-10 10:26:39 -05:00
|
|
|
Ok(())
|
|
|
|
}
|