Skip to content

Commit

Permalink
compat: add DENO_NODE_COMPAT_URL env variable (denoland#12508)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Oct 20, 2021
1 parent 46bc117 commit 8a0e206
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/compat/esm_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn finalize_resolution(
} else if !is_file {
return Err(errors::err_module_not_found(
&path.display().to_string(),
&to_file_path_string(base),
base.as_str(),
"module",
));
}
Expand Down
9 changes: 6 additions & 3 deletions cli/compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ static SUPPORTED_MODULES: &[&str] = &[
];

lazy_static::lazy_static! {
static ref GLOBAL_URL_STR: String = format!("{}node/global.ts", STD_URL_STR);
static ref NODE_COMPAT_URL: String = std::env::var("DENO_NODE_COMPAT_URL").map(String::into).ok()
.unwrap_or_else(|| STD_URL_STR.to_string());
static ref GLOBAL_URL_STR: String = format!("{}node/global.ts", NODE_COMPAT_URL.as_str());
pub(crate) static ref GLOBAL_URL: Url = Url::parse(&GLOBAL_URL_STR).unwrap();
static ref MODULE_URL_STR: String = format!("{}node/module.ts", STD_URL_STR);
static ref MODULE_URL_STR: String = format!("{}node/module.ts", NODE_COMPAT_URL.as_str());
pub(crate) static ref MODULE_URL: Url = Url::parse(&MODULE_URL_STR).unwrap();
static ref COMPAT_IMPORT_URL: Url = Url::parse("flags:compat").unwrap();
}
Expand All @@ -76,7 +78,8 @@ pub(crate) fn get_node_imports() -> Vec<(Url, Vec<String>)> {

fn try_resolve_builtin_module(specifier: &str) -> Option<Url> {
if SUPPORTED_MODULES.contains(&specifier) {
let module_url = format!("{}node/{}.ts", STD_URL_STR, specifier);
let module_url =
format!("{}node/{}.ts", NODE_COMPAT_URL.as_str(), specifier);
Some(Url::parse(&module_url).unwrap())
} else {
None
Expand Down
17 changes: 17 additions & 0 deletions cli/tests/integration/compat_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ fn globals_in_repl() {
);
assert!(out.contains("true"));
}

#[test]
fn node_compat_url() {
let (out, err) = util::run_and_collect_output_with_args(
false,
vec!["repl", "--compat", "--unstable", "--quiet"],
None,
Some(vec![(
"DENO_NODE_COMPAT_URL".to_string(),
"file:https:///non_existent/".to_string(),
)]),
false,
);
assert!(out.is_empty());
assert!(!err.is_empty());
assert!(err.contains("file:https:///non_existent/node/global.ts"));
}

0 comments on commit 8a0e206

Please sign in to comment.