diff --git a/Cargo.lock b/Cargo.lock index c2392c4e..eb490540 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -939,6 +939,12 @@ dependencies = [ "libc", ] +[[package]] +name = "nodejs-built-in-modules" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef70c89437ecec6d0052d1e39efee1830f37fe7d466d9aa44f08b04297f31d49" + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -984,6 +990,7 @@ dependencies = [ "fast-glob", "indexmap", "json-strip-comments", + "nodejs-built-in-modules", "once_cell", "papaya", "parking_lot", diff --git a/Cargo.toml b/Cargo.toml index 884edf80..a3d07f24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,7 @@ cfg-if = "1" fast-glob = "1" indexmap = { version = "2", features = ["serde"] } json-strip-comments = "3.1" +nodejs-built-in-modules = "0.0.1" once_cell = "1" # Use `std::sync::OnceLock::get_or_try_init` when it is stable. papaya = "0.2" parking_lot = "0.12" diff --git a/src/builtins.rs b/src/builtins.rs deleted file mode 100644 index 8ceffe6e..00000000 --- a/src/builtins.rs +++ /dev/null @@ -1,79 +0,0 @@ -/// Node.js built-in modules -/// -/// `node -p "[...require('module').builtinModules].map(b => JSON.stringify(b)).join(',\n')"` -/// -pub const NODEJS_BUILTINS: &[&str] = &[ - "_http_agent", - "_http_client", - "_http_common", - "_http_incoming", - "_http_outgoing", - "_http_server", - "_stream_duplex", - "_stream_passthrough", - "_stream_readable", - "_stream_transform", - "_stream_wrap", - "_stream_writable", - "_tls_common", - "_tls_wrap", - "assert", - "assert/strict", - "async_hooks", - "buffer", - "child_process", - "cluster", - "console", - "constants", - "crypto", - "dgram", - "diagnostics_channel", - "dns", - "dns/promises", - "domain", - "events", - "fs", - "fs/promises", - "http", - "http2", - "https", - "inspector", - "module", - "net", - "os", - "path", - "path/posix", - "path/win32", - "perf_hooks", - "process", - "punycode", - "querystring", - "readline", - "repl", - "stream", - "stream/consumers", - "stream/promises", - "stream/web", - "string_decoder", - "sys", - "timers", - "timers/promises", - "tls", - "trace_events", - "tty", - "url", - "util", - "util/types", - "v8", - "vm", - "worker_threads", - "zlib", -]; - -#[test] -fn test_alphabetization() { - let mut sorted_builtins = NODEJS_BUILTINS.to_vec(); - sorted_builtins.sort_unstable(); - - assert_eq!(sorted_builtins, NODEJS_BUILTINS); -} diff --git a/src/lib.rs b/src/lib.rs index c5adb541..c68420f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,6 @@ #![doc = include_str!("../examples/dir.rs")] //! ``` -mod builtins; mod cache; mod context; mod error; @@ -65,7 +64,6 @@ mod windows; mod tests; pub use crate::{ - builtins::NODEJS_BUILTINS, cache::{Cache, CachedPath}, error::{JSONError, ResolveError, SpecifierError}, file_system::{FileMetadata, FileSystem, FileSystemOs}, @@ -447,7 +445,9 @@ impl ResolverGeneric { fn require_core(&self, specifier: &str) -> Result<(), ResolveError> { if self.options.builtin_modules { let is_runtime_module = specifier.starts_with("node:"); - if is_runtime_module || NODEJS_BUILTINS.binary_search(&specifier).is_ok() { + if is_runtime_module + || nodejs_built_in_modules::BUILTINS.binary_search(&specifier).is_ok() + { let resolved = if is_runtime_module { specifier.to_string() } else { diff --git a/src/tests/builtins.rs b/src/tests/builtins.rs index 63d378f3..9941e8bb 100644 --- a/src/tests/builtins.rs +++ b/src/tests/builtins.rs @@ -16,79 +16,11 @@ fn builtins() { let resolver = Resolver::new(ResolveOptions::default().with_builtin_modules(true)); - let pass = [ - "_http_agent", - "_http_client", - "_http_common", - "_http_incoming", - "_http_outgoing", - "_http_server", - "_stream_duplex", - "_stream_passthrough", - "_stream_readable", - "_stream_transform", - "_stream_wrap", - "_stream_writable", - "_tls_common", - "_tls_wrap", - "assert", - "assert/strict", - "async_hooks", - "buffer", - "child_process", - "cluster", - "console", - "constants", - "crypto", - "dgram", - "diagnostics_channel", - "dns", - "dns/promises", - "domain", - "events", - "fs", - "fs/promises", - "http", - "http2", - "https", - "inspector", - "module", - "net", - "os", - "path", - "path/posix", - "path/win32", - "perf_hooks", - "process", - "punycode", - "querystring", - "readline", - "repl", - "stream", - "stream/consumers", - "stream/promises", - "stream/web", - "string_decoder", - "sys", - "timers", - "timers/promises", - "tls", - "trace_events", - "tty", - "url", - "util", - "util/types", - "v8", - "vm", - "worker_threads", - "zlib", - ]; - - for request in pass { + for request in nodejs_built_in_modules::BUILTINS { let prefixed_request = format!("node:{request}"); for request in [prefixed_request.clone(), request.to_string()] { let starts_with_node = request.starts_with("node:"); - let resolved_path = resolver.resolve(f, &request).map(|r| r.full_path()); + let resolved_path = resolver.resolve(f, &request); let err = ResolveError::Builtin { resolved: prefixed_request.clone(), is_runtime_module: starts_with_node, @@ -96,6 +28,16 @@ fn builtins() { assert_eq!(resolved_path, Err(err), "{request}"); } } + + for request in nodejs_built_in_modules::BUILTINS_WITH_MANDATORY_NODE_PREFIX { + let resolved_path = resolver.resolve(f, request); + assert_eq!(resolved_path, Err(ResolveError::NotFound(request.to_string())), "{request}"); + + let prefixed_request = format!("node:{request}"); + let resolved_path = resolver.resolve(f, &prefixed_request); + let err = ResolveError::Builtin { resolved: prefixed_request, is_runtime_module: true }; + assert_eq!(resolved_path, Err(err), "{request}"); + } } #[test]