summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/validators.mjs
diff options
context:
space:
mode:
authorNathan Whitaker <17734409+nathanwhit@users.noreply.github.com>2024-04-02 16:20:48 -0700
committerGitHub <noreply@github.com>2024-04-02 16:20:48 -0700
commit219a27dde51bf4c04af2d905f71d1cec021ee98e (patch)
tree80a889c2f97dc7f5aa991a169310bfa5ff0f5d22 /ext/node/polyfills/internal/validators.mjs
parent8eb2b6c61f9fdac12f8bab23ad3e9ef71c7c59b1 (diff)
fix(ext/node): Support returning tokens and option defaults in `node:util.parseArgs` (#23192)
Fixes #23179. Fixes #22454. Enables passing `{tokens: true}` to `parseArgs` and setting default values for options. With this PR, the observable framework works with deno out of the box (no unstable flags needed). The existing code was basically copied straight from node, so this PR mostly just updates that (out of date) vendored code. Also fixes some issues with error exports (before this PR, in certain error cases we were attempting to construct error classes that weren't actually in scope). The last change (in the second commit) adds a small hack so that we actually exercise the `test-parse-args.js` node_compat test, previously it was reported as passing though it should have failed. That test now passes. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/node/polyfills/internal/validators.mjs')
-rw-r--r--ext/node/polyfills/internal/validators.mjs48
1 files changed, 47 insertions, 1 deletions
diff --git a/ext/node/polyfills/internal/validators.mjs b/ext/node/polyfills/internal/validators.mjs
index 89527a84c..d4cd95546 100644
--- a/ext/node/polyfills/internal/validators.mjs
+++ b/ext/node/polyfills/internal/validators.mjs
@@ -288,9 +288,51 @@ const validateArray = hideStackFrames(
},
);
+/**
+ * @callback validateStringArray
+ * @param {*} value
+ * @param {string} name
+ * @returns {asserts value is string[]}
+ */
+
+/** @type {validateStringArray} */
+const validateStringArray = hideStackFrames((value, name) => {
+ validateArray(value, name);
+ for (let i = 0; i < value.length; ++i) {
+ // Don't use validateString here for performance reasons, as
+ // we would generate intermediate strings for the name.
+ if (typeof value[i] !== "string") {
+ throw new codes.ERR_INVALID_ARG_TYPE(`${name}[${i}]`, "string", value[i]);
+ }
+ }
+});
+
+/**
+ * @callback validateBooleanArray
+ * @param {*} value
+ * @param {string} name
+ * @returns {asserts value is boolean[]}
+ */
+
+/** @type {validateBooleanArray} */
+const validateBooleanArray = hideStackFrames((value, name) => {
+ validateArray(value, name);
+ for (let i = 0; i < value.length; ++i) {
+ // Don't use validateBoolean here for performance reasons, as
+ // we would generate intermediate strings for the name.
+ if (value[i] !== true && value[i] !== false) {
+ throw new codes.ERR_INVALID_ARG_TYPE(
+ `${name}[${i}]`,
+ "boolean",
+ value[i],
+ );
+ }
+ }
+});
+
function validateUnion(value, name, union) {
if (!ArrayPrototypeIncludes(union, value)) {
- throw new ERR_INVALID_ARG_TYPE(
+ throw new codes.ERR_INVALID_ARG_TYPE(
name,
`('${ArrayPrototypeJoin(union, "|")}')`,
value,
@@ -305,6 +347,7 @@ export default {
validateAbortSignal,
validateArray,
validateBoolean,
+ validateBooleanArray,
validateBuffer,
validateFunction,
validateInt32,
@@ -314,6 +357,7 @@ export default {
validateOneOf,
validatePort,
validateString,
+ validateStringArray,
validateUint32,
validateUnion,
};
@@ -324,6 +368,7 @@ export {
validateAbortSignal,
validateArray,
validateBoolean,
+ validateBooleanArray,
validateBuffer,
validateFunction,
validateInt32,
@@ -333,6 +378,7 @@ export {
validateOneOf,
validatePort,
validateString,
+ validateStringArray,
validateUint32,
validateUnion,
};