diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2022-11-19 20:32:39 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-19 20:32:39 +0900 |
commit | 7ab08130a06850f7d30bca8088799926f03e2a84 (patch) | |
tree | 40f35c4aac0d5d0ad1333889306d371784e30c2d /ext/node/02_require.js | |
parent | 763d492ed69c3a22310dd5c758995fbbbf3e06b8 (diff) |
fix(ext/node): handle URL in createRequire (#16682)
Diffstat (limited to 'ext/node/02_require.js')
-rw-r--r-- | ext/node/02_require.js | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/ext/node/02_require.js b/ext/node/02_require.js index ac34a5fca..372cc8471 100644 --- a/ext/node/02_require.js +++ b/ext/node/02_require.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); } |