Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 272ddb1

Browse filesBrowse files
cjihrigaddaleax
authored andcommitted
console: improve inspectOptions validation
This commit adds stricter type checking to the inspectOptions option to the Console constructor. PR-URL: #25090 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 65d485b commit 272ddb1
Copy full SHA for 272ddb1

File tree

Expand file treeCollapse file tree

2 files changed

+21
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/console/constructor.js‎

Copy file name to clipboardExpand all lines: lib/internal/console/constructor.js
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
9191
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
9292
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);
9393

94-
if (inspectOptions) {
94+
if (typeof inspectOptions === 'object' && inspectOptions !== null) {
9595
if (inspectOptions.colors !== undefined &&
9696
options.colorMode !== undefined) {
9797
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
9898
'inspectOptions.color', 'colorMode');
9999
}
100100
optionsMap.set(this, inspectOptions);
101+
} else if (inspectOptions !== undefined) {
102+
throw new ERR_INVALID_ARG_TYPE('inspectOptions', 'object', inspectOptions);
101103
}
102104

103105
// Bind the prototype functions to this Console instance
Collapse file

‎test/parallel/test-console-instance.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-console-instance.js
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,21 @@ out.write = err.write = (d) => {};
128128
assert.throws(() => c2.warn('foo'), /^Error: err$/);
129129
assert.throws(() => c2.dir('foo'), /^Error: out$/);
130130
}
131+
132+
// Console constructor throws if inspectOptions is not an object.
133+
[null, true, false, 'foo', 5, Symbol()].forEach((inspectOptions) => {
134+
assert.throws(
135+
() => {
136+
new Console({
137+
stdout: out,
138+
stderr: err,
139+
inspectOptions
140+
});
141+
},
142+
{
143+
message: 'The "inspectOptions" argument must be of type object.' +
144+
` Received type ${typeof inspectOptions}`,
145+
code: 'ERR_INVALID_ARG_TYPE'
146+
}
147+
);
148+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.