1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-30 16:40:57 -05:00

fix(ext/node): handle URL in createRequire (#16682)

This commit is contained in:
Yoshiya Hinosawa 2022-11-19 20:32:39 +09:00 committed by Bartek Iwańczuk
parent a44675cc2a
commit a20fd23949
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
6 changed files with 61 additions and 2 deletions

View file

@ -1470,6 +1470,14 @@ itest!(info_peer_deps_json {
http_server: true, http_server: true,
}); });
itest!(create_require {
args: "run --reload npm/create_require/main.ts",
output: "npm/create_require/main.out",
exit_code: 0,
envs: env_vars(),
http_server: true,
});
fn env_vars_no_sync_download() -> Vec<(String, String)> { fn env_vars_no_sync_download() -> Vec<(String, String)> {
vec![ vec![
("DENO_NODE_COMPAT_URL".to_string(), util::std_file_url()), ("DENO_NODE_COMPAT_URL".to_string(), util::std_file_url()),

View file

@ -0,0 +1,6 @@
[WILDCARD]
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

View file

@ -0,0 +1 @@
import "npm:@denotest/create-require@1.0.0";

View file

@ -0,0 +1,19 @@
import { createRequire } from "module";
console.log(typeof createRequire(import.meta.url));
console.log(typeof createRequire(new URL(import.meta.url)));
try {
createRequire("https://example.com/");
} catch (e) {
console.log(e.message);
}
try {
createRequire(new URL("https://example.com/"));
} catch (e) {
console.log(e.message);
}
try {
createRequire(1);
} catch (e) {
console.log(e.message);
}

View file

@ -0,0 +1,6 @@
{
"name": "@denotest/create-require",
"version": "1.0.0",
"type": "module",
"main": "index.js"
}

View file

@ -819,8 +819,27 @@
} }
function createRequire(filenameOrUrl) { function createRequire(filenameOrUrl) {
// FIXME: handle URLs and validation let fileUrlStr;
const filename = core.ops.op_require_as_file_path(filenameOrUrl); if (filenameOrUrl instanceof URL) {
if (filenameOrUrl.protocol !== "file:") {
throw new Error(
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
);
}
fileUrlStr = filenameOrUrl.toString();
} else if (typeof filenameOrUrl === "string") {
if (!filenameOrUrl.startsWith("file:")) {
throw new Error(
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
);
}
fileUrlStr = filenameOrUrl;
} else {
throw new Error(
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
);
}
const filename = core.ops.op_require_as_file_path(fileUrlStr);
return createRequireFromPath(filename); return createRequireFromPath(filename);
} }