mirror of
https://github.com/denoland/deno.git
synced 2025-01-12 09:03:42 -05:00
fix(ext/node): allow absolute path in createRequire (#16853)
Co-authored-by: David Sherret <dsherret@gmail.com>
This commit is contained in:
parent
d3299c2d6c
commit
e4fe5ee72a
5 changed files with 37 additions and 3 deletions
|
@ -1516,7 +1516,7 @@ mod npm {
|
||||||
});
|
});
|
||||||
|
|
||||||
itest!(create_require {
|
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",
|
output: "npm/create_require/main.out",
|
||||||
exit_code: 0,
|
exit_code: 0,
|
||||||
envs: env_vars_for_npm_tests(),
|
envs: env_vars_for_npm_tests(),
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
[WILDCARD]
|
[WILDCARD]
|
||||||
function
|
function
|
||||||
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 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 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
|
||||||
|
|
|
@ -2,6 +2,10 @@ import { createRequire } from "module";
|
||||||
|
|
||||||
console.log(typeof createRequire(import.meta.url));
|
console.log(typeof createRequire(import.meta.url));
|
||||||
console.log(typeof createRequire(new URL(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 {
|
try {
|
||||||
createRequire("https://example.com/");
|
createRequire("https://example.com/");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -17,3 +21,13 @@ try {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e.message);
|
console.log(e.message);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
createRequire("foo");
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e.message);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
createRequire("./foo");
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e.message);
|
||||||
|
}
|
||||||
|
|
|
@ -826,6 +826,17 @@
|
||||||
return require;
|
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) {
|
function createRequire(filenameOrUrl) {
|
||||||
let fileUrlStr;
|
let fileUrlStr;
|
||||||
if (filenameOrUrl instanceof URL) {
|
if (filenameOrUrl instanceof URL) {
|
||||||
|
@ -836,7 +847,7 @@
|
||||||
}
|
}
|
||||||
fileUrlStr = filenameOrUrl.toString();
|
fileUrlStr = filenameOrUrl.toString();
|
||||||
} else if (typeof filenameOrUrl === "string") {
|
} else if (typeof filenameOrUrl === "string") {
|
||||||
if (!filenameOrUrl.startsWith("file:")) {
|
if (!filenameOrUrl.startsWith("file:") && !isAbsolute(filenameOrUrl)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
|
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
|
||||||
);
|
);
|
||||||
|
|
|
@ -245,7 +245,10 @@ where
|
||||||
fn op_require_proxy_path(filename: String) -> String {
|
fn op_require_proxy_path(filename: String) -> String {
|
||||||
// Allow a directory to be passed as the filename
|
// Allow a directory to be passed as the filename
|
||||||
let trailing_slash = if cfg!(windows) {
|
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 {
|
} else {
|
||||||
filename.ends_with('/')
|
filename.ends_with('/')
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue