Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(ext/npm): handle URL in createRequire
  • Loading branch information
kt3k committed Nov 17, 2022
commit 968b7f3b18f519455da845b62530cf9dbba10993
8 changes: 8 additions & 0 deletions cli/tests/integration/npm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
6 changes: 6 additions & 0 deletions cli/tests/testdata/npm/create_require/main.out
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions cli/tests/testdata/npm/create_require/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "npm:@denotest/[email protected]";
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@denotest/create-require",
"version": "1.0.0",
"type": "module",
"main": "index.js"
}
24 changes: 22 additions & 2 deletions ext/node/02_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,28 @@
}

function createRequire(filenameOrUrl) {
// FIXME: handle URLs and validation
const filename = core.ops.op_require_as_file_path(filenameOrUrl);
let fileUrlStr;
// TODO(kt3k): Support node.js Url object
kt3k marked this conversation as resolved.
Show resolved Hide resolved
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);
}

Expand Down