summaryrefslogtreecommitdiff
path: root/ext/node
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node')
-rw-r--r--ext/node/02_require.js13
-rw-r--r--ext/node/lib.rs5
2 files changed, 16 insertions, 2 deletions
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('/')
};