1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 04:48:52 -05:00

feat(compat): inject Node globals (#12342)

This commit adds automatic injection of Node globals when "--compat" flag
is present.

This is done by executing "https://deno.land/std/node/global.ts" as a "side module",
before main module is executed.

This commit makes "--compat" required to be used with "--unstable" flag, as some
of Node globals require unstable Deno APIs.
This commit is contained in:
Bartek Iwańczuk 2021-10-06 19:07:04 +02:00 committed by GitHub
parent b033a7a6d4
commit b686907a45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 15 deletions

View file

@ -1,7 +1,10 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::url::Url;
use std::collections::HashMap;
static STD_NODE: &str = "https://deno.land/std/node/";
static SUPPORTED_MODULES: &[&str] = &[
"assert",
"assert/strict",
@ -47,12 +50,20 @@ static SUPPORTED_MODULES: &[&str] = &[
"zlib",
];
pub fn get_node_globals_url() -> Url {
Url::parse(&format!("{}global.ts", STD_NODE)).unwrap()
}
/// Create a map that can be used to update import map.
///
/// Keys are built-in Node modules (and built-ins prefixed with "node:"), while
/// values are URLs pointing to relevant files in deno.land/std/node/ directory.
pub fn get_mapped_node_builtins() -> HashMap<String, String> {
let mut mappings = HashMap::new();
for module in SUPPORTED_MODULES {
// TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable?
let module_url = format!("https://deno.land/std/node/{}.ts", module);
let module_url = format!("{}{}.ts", STD_NODE, module);
mappings.insert(module.to_string(), module_url.clone());
// Support for `node:<module_name>`

View file

@ -1627,7 +1627,8 @@ fn seed_arg<'a, 'b>() -> Arg<'a, 'b> {
fn compat_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("compat")
.long("compat")
.help("Node compatibility mode. Currently only enables built-in node modules like 'fs'.")
.requires("unstable")
.help("Node compatibility mode. Currently only enables built-in node modules like 'fs' and globals like 'process'.")
}
fn watch_arg<'a, 'b>() -> Arg<'a, 'b> {
@ -4453,7 +4454,8 @@ mod tests {
#[test]
fn compat() {
let r = flags_from_vec(svec!["deno", "run", "--compat", "foo.js"]);
let r =
flags_from_vec(svec!["deno", "run", "--compat", "--unstable", "foo.js"]);
assert_eq!(
r.unwrap(),
Flags {
@ -4461,6 +4463,7 @@ mod tests {
script: "foo.js".to_string(),
}),
compat: true,
unstable: true,
..Flags::default()
}
);

View file

@ -568,7 +568,7 @@ async fn eval_command(
// Force TypeScript compile.
let main_module = resolve_url_or_path("./$deno$eval.ts").unwrap();
let permissions = Permissions::from_options(&flags.clone().into());
let ps = ProcState::build(flags).await?;
let ps = ProcState::build(flags.clone()).await?;
let mut worker =
create_main_worker(&ps, main_module.clone(), permissions, None);
// Create a dummy source file.
@ -600,6 +600,11 @@ async fn eval_command(
// to allow module access by TS compiler.
ps.file_fetcher.insert_cached(file);
debug!("main_module {}", &main_module);
if flags.compat {
worker
.execute_side_module(&compat::get_node_globals_url())
.await?;
}
worker.execute_main_module(&main_module).await?;
worker.execute_script(
&located_script_name!(),
@ -845,6 +850,11 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
ps.file_fetcher.insert_cached(source_file);
debug!("main_module {}", main_module);
if flags.compat {
worker
.execute_side_module(&compat::get_node_globals_url())
.await?;
}
worker.execute_main_module(&main_module).await?;
worker.execute_script(
&located_script_name!(),
@ -912,13 +922,15 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
struct FileWatcherModuleExecutor {
worker: MainWorker,
pending_unload: bool,
compat: bool,
}
impl FileWatcherModuleExecutor {
pub fn new(worker: MainWorker) -> FileWatcherModuleExecutor {
pub fn new(worker: MainWorker, compat: bool) -> FileWatcherModuleExecutor {
FileWatcherModuleExecutor {
worker,
pending_unload: false,
compat,
}
}
@ -928,6 +940,12 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
&mut self,
main_module: &ModuleSpecifier,
) -> Result<(), AnyError> {
if self.compat {
self
.worker
.execute_side_module(&compat::get_node_globals_url())
.await?;
}
self.worker.execute_main_module(main_module).await?;
self.worker.execute_script(
&located_script_name!(),
@ -967,16 +985,14 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
let operation = |(ps, main_module): (ProcState, ModuleSpecifier)| {
let flags = flags.clone();
let permissions = Permissions::from_options(&flags.into());
let permissions = Permissions::from_options(&flags.clone().into());
async move {
// We make use an module executor guard to ensure that unload is always fired when an
// operation is called.
let mut executor = FileWatcherModuleExecutor::new(create_main_worker(
&ps,
main_module.clone(),
permissions,
None,
));
let mut executor = FileWatcherModuleExecutor::new(
create_main_worker(&ps, main_module.clone(), permissions, None),
flags.compat,
);
executor.execute(&main_module).await?;
@ -1022,6 +1038,11 @@ async fn run_command(
};
debug!("main_module {}", main_module);
if flags.compat {
worker
.execute_side_module(&compat::get_node_globals_url())
.await?;
}
worker.execute_main_module(&main_module).await?;
worker.execute_script(
&located_script_name!(),

View file

@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use crate::colors;
use crate::compat;
use crate::config_file::ConfigFile;
use crate::deno_dir;
use crate::file_fetcher::CacheSetting;
@ -217,7 +218,7 @@ impl ProcState {
.unwrap()
}
};
let node_builtins = crate::compat::get_mapped_node_builtins();
let node_builtins = compat::get_mapped_node_builtins();
let diagnostics = import_map.update_imports(node_builtins)?;
if !diagnostics.is_empty() {
@ -353,6 +354,9 @@ impl ProcState {
)?));
let mut builder =
GraphBuilder::new(handler, maybe_import_map, self.lockfile.clone());
if self.flags.compat {
builder.add(&compat::get_node_globals_url(), false).await?;
}
builder.add(&specifier, is_dynamic).await?;
builder.analyze_config_file(&self.maybe_config_file).await?;
let mut graph = builder.get_graph();

View file

@ -2,6 +2,11 @@
use crate::itest;
itest!(globals {
args: "run --compat --unstable --allow-read --allow-env compat/globals.ts",
output: "compat/globals.out",
});
itest!(fs_promises {
args: "run --compat --unstable -A compat/fs_promises.js",
output: "compat/fs_promises.out",
@ -13,7 +18,7 @@ itest!(node_prefix_fs_promises {
});
itest!(existing_import_map {
args: "run --compat --import-map compat/existing_import_map.json compat/fs_promises.js",
args: "run --compat --unstable --import-map compat/existing_import_map.json compat/fs_promises.js",
output: "compat/existing_import_map.out",
exit_code: 1,
});

View file

@ -2,4 +2,5 @@
Some Node built-ins were not added to the import map:
- "fs/promises" already exists and is mapped to "[WILDCARD]non_existent_file.js"
If you want to use Node built-ins provided by Deno remove listed specifiers from "imports" mapping in the import map file.
[WILDCARD]
error: Cannot resolve module [WILDCARD]

7
cli/tests/testdata/compat/globals.out vendored Normal file
View file

@ -0,0 +1,7 @@
[WILDCARD]
process {
[WILDCARD]
}
[Function: Buffer]
[Function: setImmediate]
[Function: clearTimeout]

8
cli/tests/testdata/compat/globals.ts vendored Normal file
View file

@ -0,0 +1,8 @@
if (global != window) {
throw new Error("global is not equal to window");
}
console.log(process);
console.log(Buffer);
console.log(setImmediate);
console.log(clearImmediate);