summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/npm_tests.rs2
-rw-r--r--cli/tests/testdata/npm/create_require/main.out6
-rw-r--r--cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/index.js14
-rw-r--r--ext/node/02_require.js13
-rw-r--r--ext/node/lib.rs5
5 files changed, 37 insertions, 3 deletions
diff --git a/cli/tests/npm_tests.rs b/cli/tests/npm_tests.rs
index 99f11ba28..c845f8f4a 100644
--- a/cli/tests/npm_tests.rs
+++ b/cli/tests/npm_tests.rs
@@ -1516,7 +1516,7 @@ mod npm {
});
itest!(create_require {
- args: "run --reload npm/create_require/main.ts",
+ args: "run --reload --allow-read npm/create_require/main.ts",
output: "npm/create_require/main.out",
exit_code: 0,
envs: env_vars_for_npm_tests(),
diff --git a/cli/tests/testdata/npm/create_require/main.out b/cli/tests/testdata/npm/create_require/main.out
index ebd96c120..70b0415b3 100644
--- a/cli/tests/testdata/npm/create_require/main.out
+++ b/cli/tests/testdata/npm/create_require/main.out
@@ -1,6 +1,12 @@
[WILDCARD]
function
function
+function
+function
+function
+function
The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received https://example.com/
The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received https://example.com/
The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received 1
+The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received foo
+The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ./foo
diff --git a/cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/index.js
index eaa3e5afd..2ab564366 100644
--- a/cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/index.js
+++ b/cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/index.js
@@ -2,6 +2,10 @@ import { createRequire } from "module";
console.log(typeof createRequire(import.meta.url));
console.log(typeof createRequire(new URL(import.meta.url)));
+console.log(typeof createRequire("/"));
+console.log(typeof createRequire("/foo"));
+console.log(typeof createRequire("/foo/"));
+console.log(typeof createRequire("c:\\foo"));
try {
createRequire("https://example.com/");
} catch (e) {
@@ -17,3 +21,13 @@ try {
} catch (e) {
console.log(e.message);
}
+try {
+ createRequire("foo");
+} catch (e) {
+ console.log(e.message);
+}
+try {
+ createRequire("./foo");
+} catch (e) {
+ console.log(e.message);
+}
diff --git a/ext/node/02_require.js b/ext/node/02_require.js
index db4329911..b5117042d 100644
--- a/ext/node/02_require.js
+++ b/ext/node/02_require.js
@@ -826,6 +826,17 @@
return require;
}
+ // Matches to:
+ // - /foo/...
+ // - \foo\...
+ // - C:/foo/...
+ // - C:\foo\...
+ const RE_START_OF_ABS_PATH = /^([/\\]|[a-zA-Z]:[/\\])/;
+
+ function isAbsolute(filenameOrUrl) {
+ return RE_START_OF_ABS_PATH.test(filenameOrUrl);
+ }
+
function createRequire(filenameOrUrl) {
let fileUrlStr;
if (filenameOrUrl instanceof URL) {
@@ -836,7 +847,7 @@
}
fileUrlStr = filenameOrUrl.toString();
} else if (typeof filenameOrUrl === "string") {
- if (!filenameOrUrl.startsWith("file:")) {
+ if (!filenameOrUrl.startsWith("file:") && !isAbsolute(filenameOrUrl)) {
throw new Error(
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
);
diff --git a/ext/node/lib.rs b/ext/node/lib.rs
index 0e84cea7b..c365d5d7b 100644
--- a/ext/node/lib.rs
+++ b/ext/node/lib.rs
@@ -245,7 +245,10 @@ where
fn op_require_proxy_path(filename: String) -> String {
// Allow a directory to be passed as the filename
let trailing_slash = if cfg!(windows) {
- filename.ends_with('\\')
+ // Node also counts a trailing forward slash as a
+ // directory for node on Windows, but not backslashes
+ // on non-Windows platforms
+ filename.ends_with('\\') || filename.ends_with('/')
} else {
filename.ends_with('/')
};