1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-02 17:01:14 -05:00

refactor: cleanup Node compatibility code (#15766)

- move errors related to Node compat from cli/node/errors.rs to "ext/node" crate
- remove dependency on "node_resolver" crate
- make some of structures private to the "cli/node" module
This commit is contained in:
Bartek Iwańczuk 2022-09-05 12:36:35 +02:00 committed by Yoshiya Hinosawa
parent d908032dbb
commit 6c80cacb58
No known key found for this signature in database
GPG key ID: 0E8BFAA8A5B4E92B
6 changed files with 32 additions and 75 deletions

13
Cargo.lock generated
View file

@ -831,7 +831,6 @@ dependencies = [
"mitata",
"monch",
"nix",
"node_resolver",
"notify",
"once_cell",
"os_pipe",
@ -2790,18 +2789,6 @@ dependencies = [
"memoffset",
]
[[package]]
name = "node_resolver"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1012fcb4f5cc14c272731f4baf6ba8a2d40c650908f4d2531f7ed17f214e5ae"
dependencies = [
"anyhow",
"path-clean",
"serde",
"serde_json",
]
[[package]]
name = "notify"
version = "5.0.0-pre.15"

View file

@ -80,7 +80,6 @@ libc = "=0.2.126"
log = { version = "=0.4.17", features = ["serde"] }
mitata = "=0.0.7"
monch = "=0.2.0"
node_resolver = "=0.1.1"
notify = "=5.0.0-pre.15"
once_cell = "=1.12.0"
os_pipe = "=1.0.1"

View file

@ -1,50 +0,0 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use deno_core::error::generic_error;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::url::Url;
pub fn err_invalid_module_specifier(
request: &str,
reason: &str,
maybe_base: Option<String>,
) -> AnyError {
let mut msg = format!(
"[ERR_INVALID_MODULE_SPECIFIER] Invalid module \"{}\" {}",
request, reason
);
if let Some(base) = maybe_base {
msg = format!("{} imported from {}", msg, base);
}
type_error(msg)
}
pub fn err_module_not_found(path: &str, base: &str, typ: &str) -> AnyError {
generic_error(format!(
"[ERR_MODULE_NOT_FOUND] Cannot find {} \"{}\" imported from \"{}\"",
typ, path, base
))
}
pub fn err_unsupported_dir_import(path: &str, base: &str) -> AnyError {
generic_error(format!("[ERR_UNSUPPORTED_DIR_IMPORT] Directory import '{}' is not supported resolving ES modules imported from {}", path, base))
}
pub fn err_unsupported_esm_url_scheme(url: &Url) -> AnyError {
let mut msg =
"[ERR_UNSUPPORTED_ESM_URL_SCHEME] Only file and data URLS are supported by the default ESM loader"
.to_string();
if cfg!(window) && url.scheme().len() == 2 {
msg = format!(
"{}. On Windows, absolute path must be valid file:// URLs",
msg
);
}
msg = format!("{}. Received protocol '{}'", msg, url.scheme());
generic_error(msg)
}

View file

@ -17,6 +17,7 @@ use deno_core::serde_json::Value;
use deno_core::url::Url;
use deno_core::JsRuntime;
use deno_graph::source::ResolveResponse;
use deno_runtime::deno_node::errors;
use deno_runtime::deno_node::get_closest_package_json;
use deno_runtime::deno_node::legacy_main_resolve;
use deno_runtime::deno_node::package_exports_resolve;
@ -37,19 +38,18 @@ use crate::npm::NpmPackageReq;
use crate::npm::NpmPackageResolver;
mod analyze;
pub mod errors;
pub use analyze::esm_code_with_node_globals;
pub struct NodeModulePolyfill {
struct NodeModulePolyfill {
/// Name of the module like "assert" or "timers/promises"
pub name: &'static str,
name: &'static str,
/// Specifier relative to the root of `deno_std` repo, like "node/asser.ts"
pub specifier: &'static str,
specifier: &'static str,
}
pub(crate) static SUPPORTED_MODULES: &[NodeModulePolyfill] = &[
static SUPPORTED_MODULES: &[NodeModulePolyfill] = &[
NodeModulePolyfill {
name: "assert",
specifier: "node/assert.ts",
@ -224,7 +224,7 @@ pub(crate) static SUPPORTED_MODULES: &[NodeModulePolyfill] = &[
},
];
pub(crate) static NODE_COMPAT_URL: Lazy<Url> = Lazy::new(|| {
static NODE_COMPAT_URL: Lazy<Url> = Lazy::new(|| {
if let Ok(url_str) = std::env::var("DENO_NODE_COMPAT_URL") {
let url = Url::parse(&url_str).expect(
"Malformed DENO_NODE_COMPAT_URL value, make sure it's a file URL ending with a slash"
@ -238,7 +238,7 @@ pub(crate) static NODE_COMPAT_URL: Lazy<Url> = Lazy::new(|| {
pub static MODULE_ALL_URL: Lazy<Url> =
Lazy::new(|| NODE_COMPAT_URL.join("node/module_all.ts").unwrap());
pub fn try_resolve_builtin_module(specifier: &str) -> Option<Url> {
fn try_resolve_builtin_module(specifier: &str) -> Option<Url> {
for module in SUPPORTED_MODULES {
if module.name == specifier {
let module_url = NODE_COMPAT_URL.join(module.specifier).unwrap();

View file

@ -3,6 +3,7 @@
use deno_core::error::generic_error;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::url::Url;
pub fn err_invalid_module_specifier(
request: &str,
@ -120,3 +121,23 @@ pub fn err_package_import_not_defined(
type_error(msg)
}
pub fn err_unsupported_dir_import(path: &str, base: &str) -> AnyError {
generic_error(format!("[ERR_UNSUPPORTED_DIR_IMPORT] Directory import '{}' is not supported resolving ES modules imported from {}", path, base))
}
pub fn err_unsupported_esm_url_scheme(url: &Url) -> AnyError {
let mut msg =
"[ERR_UNSUPPORTED_ESM_URL_SCHEME] Only file and data URLS are supported by the default ESM loader"
.to_string();
if cfg!(window) && url.scheme().len() == 2 {
msg = format!(
"{}. On Windows, absolute path must be valid file:// URLs",
msg
);
}
msg = format!("{}. Received protocol '{}'", msg, url.scheme());
generic_error(msg)
}

View file

@ -11,6 +11,10 @@ use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
pub mod errors;
mod package_json;
mod resolution;
pub use package_json::PackageJson;
pub use resolution::get_closest_package_json;
pub use resolution::get_package_scope_config;
@ -42,10 +46,6 @@ pub trait DenoDirNpmResolver {
fn ensure_read_permission(&self, path: &Path) -> Result<(), AnyError>;
}
mod errors;
mod package_json;
mod resolution;
pub const MODULE_ES_SHIM: &str = include_str!("./module_es_shim.js");
struct Unstable(pub bool);