mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
fix(ext/node): handle URL in createRequire (#16682)
This commit is contained in:
parent
763d492ed6
commit
7ab08130a0
6 changed files with 61 additions and 2 deletions
|
@ -1470,6 +1470,14 @@ itest!(info_peer_deps_json {
|
|||
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)> {
|
||||
vec![
|
||||
("DENO_NODE_COMPAT_URL".to_string(), util::std_file_url()),
|
||||
|
|
6
cli/tests/testdata/npm/create_require/main.out
vendored
Normal file
6
cli/tests/testdata/npm/create_require/main.out
vendored
Normal 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
|
1
cli/tests/testdata/npm/create_require/main.ts
vendored
Normal file
1
cli/tests/testdata/npm/create_require/main.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
import "npm:@denotest/create-require@1.0.0";
|
19
cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/index.js
vendored
Normal file
19
cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/index.js
vendored
Normal 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);
|
||||
}
|
6
cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/package.json
vendored
Normal file
6
cli/tests/testdata/npm/registry/@denotest/create-require/1.0.0/package.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "@denotest/create-require",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "index.js"
|
||||
}
|
|
@ -819,8 +819,27 @@
|
|||
}
|
||||
|
||||
function createRequire(filenameOrUrl) {
|
||||
// FIXME: handle URLs and validation
|
||||
const filename = core.ops.op_require_as_file_path(filenameOrUrl);
|
||||
let fileUrlStr;
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue