Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79f1999

Browse files
committed
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 <[email protected]> Reviewed-By: Jordan Harband <[email protected]>
1 parent bb04959 commit 79f1999

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

doc/api/console.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ const { Console } = console;
102102

103103
<!-- YAML
104104
changes:
105+
- version: REPLACEME
106+
pr-url: https://github.com/nodejs/node/pull/60082
107+
description: The `inspectOptions` option can be a `Map` from stream to options.
105108
- version:
106109
- v14.2.0
107110
- v12.17.0
@@ -131,8 +134,9 @@ changes:
131134
and the value returned by `getColorDepth()` on the respective stream. This
132135
option can not be used, if `inspectOptions.colors` is set as well.
133136
**Default:** `'auto'`.
134-
* `inspectOptions` {Object} Specifies options that are passed along to
135-
[`util.inspect()`][].
137+
* `inspectOptions` {Object|Map} Specifies options that are passed along to
138+
[`util.inspect()`][]. Can be an options object or, if different options
139+
for stdout and stderr are desired, a `Map` from stream objects to options.
136140
* `groupIndentation` {number} Set group indentation.
137141
**Default:** `2`.
138142

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
require('../common');
3+
const { Console } = require('console');
4+
const { PassThrough } = require('stream');
5+
const { strict: assert } = require('assert');
6+
7+
const stdout = new PassThrough().setEncoding('utf8');
8+
const stderr = new PassThrough().setEncoding('utf8');
9+
10+
const console = new Console({
11+
stdout,
12+
stderr,
13+
inspectOptions: new Map([
14+
[stdout, { colors: true }],
15+
[stderr, { colors: false }],
16+
]),
17+
});
18+
19+
console.log('Hello', 42);
20+
console.warn('Hello', 42);
21+
22+
assert.strictEqual(stdout.read(), 'Hello \x1B[33m42\x1B[39m\n');
23+
assert.strictEqual(stderr.read(), 'Hello 42\n');

0 commit comments

Comments
 (0)