summaryrefslogtreecommitdiff
path: root/cli/compat
diff options
context:
space:
mode:
Diffstat (limited to 'cli/compat')
-rw-r--r--cli/compat/errors.rs141
-rw-r--r--cli/compat/esm_resolver.rs13
-rw-r--r--cli/compat/mod.rs107
3 files changed, 21 insertions, 240 deletions
diff --git a/cli/compat/errors.rs b/cli/compat/errors.rs
deleted file mode 100644
index d7d1bbd05..000000000
--- a/cli/compat/errors.rs
+++ /dev/null
@@ -1,141 +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_invalid_package_config(
- path: &str,
- maybe_base: Option<String>,
- maybe_message: Option<String>,
-) -> AnyError {
- let mut msg = format!(
- "[ERR_INVALID_PACKAGE_CONFIG] Invalid package config {}",
- path
- );
-
- if let Some(base) = maybe_base {
- msg = format!("{} while importing {}", msg, base);
- }
-
- if let Some(message) = maybe_message {
- msg = format!("{}. {}", msg, message);
- }
-
- generic_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)
-}
-
-pub fn err_invalid_package_target(
- pkg_path: String,
- key: String,
- target: String,
- is_import: bool,
- maybe_base: Option<String>,
-) -> AnyError {
- let rel_error = !is_import && !target.is_empty() && !target.starts_with("./");
- let mut msg = "[ERR_INVALID_PACKAGE_TARGET]".to_string();
-
- if key == "." {
- assert!(!is_import);
- msg = format!("{} Invalid \"exports\" main target {} defined in the package config {}package.json", msg, target, pkg_path)
- } else {
- let ie = if is_import { "imports" } else { "exports" };
- msg = format!("{} Invalid \"{}\" target {} defined for '{}' in the package config {}package.json", msg, ie, target, key, pkg_path)
- };
-
- if let Some(base) = maybe_base {
- msg = format!("{} imported from {}", msg, base);
- };
- if rel_error {
- msg = format!("{}; target must start with \"./\"", msg);
- }
-
- generic_error(msg)
-}
-
-pub fn err_package_path_not_exported(
- pkg_path: String,
- subpath: String,
- maybe_base: Option<String>,
-) -> AnyError {
- let mut msg = "[ERR_PACKAGE_PATH_NOT_EXPORTED]".to_string();
-
- if subpath == "." {
- msg = format!(
- "{} No \"exports\" main defined in {}package.json",
- msg, pkg_path
- );
- } else {
- msg = format!("{} Package subpath \'{}\' is not defined by \"exports\" in {}package.json", msg, subpath, pkg_path);
- };
-
- if let Some(base) = maybe_base {
- msg = format!("{} imported from {}", msg, base);
- }
-
- generic_error(msg)
-}
-
-pub fn err_package_import_not_defined(
- specifier: &str,
- package_path: Option<String>,
- base: &str,
-) -> AnyError {
- let mut msg = format!(
- "[ERR_PACKAGE_IMPORT_NOT_DEFINED] Package import specifier \"{}\" is not defined in",
- specifier
- );
-
- if let Some(package_path) = package_path {
- msg = format!("{} in package {}package.json", msg, package_path);
- }
-
- msg = format!("{} imported from {}", msg, base);
-
- type_error(msg)
-}
diff --git a/cli/compat/esm_resolver.rs b/cli/compat/esm_resolver.rs
index 77d0fe47e..2a5da4fe1 100644
--- a/cli/compat/esm_resolver.rs
+++ b/cli/compat/esm_resolver.rs
@@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
-use super::errors;
+use crate::node::errors;
use crate::resolver::ImportMapResolver;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
@@ -84,7 +84,7 @@ fn node_resolve(
) -> Result<ResolveResponse, AnyError> {
// TODO(bartlomieju): skipped "policy" part as we don't plan to support it
- if let Some(resolved) = crate::compat::try_resolve_builtin_module(specifier) {
+ if let Some(resolved) = crate::node::try_resolve_builtin_module(specifier) {
return Ok(ResolveResponse::Esm(resolved));
}
@@ -99,7 +99,7 @@ fn node_resolve(
let split_specifier = url.as_str().split(':');
let specifier = split_specifier.skip(1).collect::<String>();
if let Some(resolved) =
- crate::compat::try_resolve_builtin_module(&specifier)
+ crate::node::try_resolve_builtin_module(&specifier)
{
return Ok(ResolveResponse::Esm(resolved));
} else {
@@ -1154,7 +1154,7 @@ fn legacy_main_resolve(
#[cfg(test)]
mod tests {
use super::*;
- use crate::compat::STD_URL_STR;
+ use crate::deno_std::CURRENT_STD_URL;
fn testdir(name: &str) -> PathBuf {
let c = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
@@ -1233,10 +1233,7 @@ mod tests {
fn builtin_http() {
let cwd = testdir("basic");
let main = Url::from_file_path(cwd.join("main.js")).unwrap();
- let expected = Url::parse(STD_URL_STR)
- .unwrap()
- .join("node/http.ts")
- .unwrap();
+ let expected = CURRENT_STD_URL.join("node/http.ts").unwrap();
let actual = node_resolve("http", main.as_str(), &cwd).unwrap();
assert!(matches!(actual, ResolveResponse::Esm(_)));
diff --git a/cli/compat/mod.rs b/cli/compat/mod.rs
index fb2f2628c..0581f208f 100644
--- a/cli/compat/mod.rs
+++ b/cli/compat/mod.rs
@@ -1,9 +1,10 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
-pub mod errors;
mod esm_resolver;
use crate::file_fetcher::FileFetcher;
+use crate::node::NODE_COMPAT_URL;
+use crate::node::SUPPORTED_MODULES;
use deno_ast::MediaType;
use deno_core::error::AnyError;
use deno_core::located_script_name;
@@ -15,101 +16,21 @@ use once_cell::sync::Lazy;
pub use esm_resolver::check_if_should_use_esm_loader;
pub use esm_resolver::NodeEsmResolver;
-// WARNING: Ensure this is the only deno_std version reference as this
-// is automatically updated by the version bump workflow.
-pub(crate) static STD_URL_STR: &str = "https://deno.land/std@0.153.0/";
-
-static SUPPORTED_MODULES: &[&str] = &[
- "assert",
- "assert/strict",
- "async_hooks",
- "buffer",
- "child_process",
- "cluster",
- "console",
- "constants",
- "crypto",
- "dgram",
- "dns",
- "domain",
- "events",
- "fs",
- "fs/promises",
- "http",
- "https",
- "module",
- "net",
- "os",
- "path",
- "path/posix",
- "path/win32",
- "perf_hooks",
- "process",
- "querystring",
- "readline",
- "stream",
- "stream/promises",
- "stream/web",
- "string_decoder",
- "sys",
- "timers",
- "timers/promises",
- "tls",
- "tty",
- "url",
- "util",
- "util/types",
- "v8",
- "vm",
- "worker_threads",
- "zlib",
-];
-
-static NODE_COMPAT_URL: Lazy<String> = Lazy::new(|| {
- std::env::var("DENO_NODE_COMPAT_URL")
- .map(String::into)
- .ok()
- .unwrap_or_else(|| STD_URL_STR.to_string())
-});
-
-static GLOBAL_URL_STR: Lazy<String> =
- Lazy::new(|| format!("{}node/global.ts", NODE_COMPAT_URL.as_str()));
-
pub static GLOBAL_URL: Lazy<Url> =
- Lazy::new(|| Url::parse(&GLOBAL_URL_STR).unwrap());
-
-static MODULE_URL_STR: Lazy<String> =
- Lazy::new(|| format!("{}node/module.ts", NODE_COMPAT_URL.as_str()));
-
-pub static MODULE_ALL_URL: Lazy<Url> =
- Lazy::new(|| Url::parse(&MODULE_ALL_URL_STR).unwrap());
-
-static MODULE_ALL_URL_STR: Lazy<String> =
- Lazy::new(|| format!("{}node/module_all.ts", NODE_COMPAT_URL.as_str()));
+ Lazy::new(|| NODE_COMPAT_URL.join("node/global.ts").unwrap());
pub static MODULE_URL: Lazy<Url> =
- Lazy::new(|| Url::parse(&MODULE_URL_STR).unwrap());
+ Lazy::new(|| NODE_COMPAT_URL.join("node/module.ts").unwrap());
static COMPAT_IMPORT_URL: Lazy<Url> =
Lazy::new(|| Url::parse("flags:compat").unwrap());
/// Provide imports into a module graph when the compat flag is true.
pub fn get_node_imports() -> Vec<(Url, Vec<String>)> {
- vec![(COMPAT_IMPORT_URL.clone(), vec![GLOBAL_URL_STR.clone()])]
-}
-
-pub fn try_resolve_builtin_module(specifier: &str) -> Option<Url> {
- if SUPPORTED_MODULES.contains(&specifier) {
- let ext = match specifier {
- "stream/promises" => "mjs",
- _ => "ts",
- };
- let module_url =
- format!("{}node/{}.{}", NODE_COMPAT_URL.as_str(), specifier, ext);
- Some(Url::parse(&module_url).unwrap())
- } else {
- None
- }
+ vec![(
+ COMPAT_IMPORT_URL.clone(),
+ vec![GLOBAL_URL.as_str().to_owned()],
+ )]
}
pub fn load_cjs_module(
@@ -122,7 +43,7 @@ pub fn load_cjs_module(
const Module = await import("{module_loader}");
Module.default._load(module, null, {main});
}})('{module}');"#,
- module_loader = MODULE_URL_STR.as_str(),
+ module_loader = MODULE_URL.as_str(),
main = main,
module = escape_for_single_quote_string(module),
);
@@ -141,7 +62,7 @@ pub fn add_global_require(
const require = Module.createRequire(main);
globalThis.require = require;
}})('{}');"#,
- MODULE_URL_STR.as_str(),
+ MODULE_URL.as_str(),
escape_for_single_quote_string(main_module),
);
@@ -159,8 +80,12 @@ pub fn setup_builtin_modules(
let mut script = String::new();
for module in SUPPORTED_MODULES {
// skipping the modules that contains '/' as they are not available in NodeJS repl as well
- if !module.contains('/') {
- script = format!("{}const {} = require('{}');\n", script, module, module);
+ if !module.name.contains('/') {
+ script = format!(
+ "{}const {MODULE_NAME} = require('{MODULE_NAME}');\n",
+ script,
+ MODULE_NAME = module.name
+ );
}
}