console: allow per-stream `inspectOptions` option · nodejs/node@31bb476 · GitHub
Skip to content

Commit 31bb476

Browse files
addaleaxtargos
authored andcommitted
console: allow per-stream inspectOptions option
We (correctly) allow different streams to be specified for `stdout` and `stderr`, so we should also allow different inspect options for these streams. PR-URL: #60082 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com>
1 parent 08b9eb8 commit 31bb476

3 files changed

Lines changed: 46 additions & 8 deletions

File tree

doc/api/console.md

Lines changed: 6 additions & 2 deletions

lib/internal/console/constructor.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const {
1212
Boolean,
1313
ErrorCaptureStackTrace,
1414
FunctionPrototypeBind,
15+
MapPrototypeGet,
16+
MapPrototypeValues,
1517
ObjectDefineProperties,
1618
ObjectDefineProperty,
1719
ObjectKeys,
@@ -139,12 +141,20 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
139141
if (inspectOptions !== undefined) {
140142
validateObject(inspectOptions, 'options.inspectOptions');
141143

142-
if (inspectOptions.colors !== undefined &&
143-
options.colorMode !== undefined) {
144-
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
145-
'options.inspectOptions.color', 'colorMode');
144+
const inspectOptionsMap = isMap(inspectOptions) ?
145+
inspectOptions : new SafeMap([
146+
[stdout, inspectOptions],
147+
[stderr, inspectOptions],
148+
]);
149+
150+
for (const inspectOptions of MapPrototypeValues(inspectOptionsMap)) {
151+
if (inspectOptions.colors !== undefined &&
152+
options.colorMode !== undefined) {
153+
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
154+
'options.inspectOptions.color', 'colorMode');
155+
}
146156
}
147-
optionsMap.set(this, inspectOptions);
157+
optionsMap.set(this, inspectOptionsMap);
148158
}
149159

150160
// Bind the prototype functions to this Console instance
@@ -316,7 +326,8 @@ ObjectDefineProperties(Console.prototype, {
316326
color = lazyUtilColors().shouldColorize(stream);
317327
}
318328

319-
const options = optionsMap.get(this);
329+
const inspectOptionsMap = optionsMap.get(this);
330+
const options = inspectOptionsMap ? MapPrototypeGet(inspectOptionsMap, stream) : undefined;
320331
if (options) {
321332
if (options.colors === undefined) {
322333
options.colors = color;
Lines changed: 23 additions & 0 deletions

0 commit comments

Comments
 (0)