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 31bb476

Browse filesBrowse 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
Copy full SHA for 31bb476

File tree

Expand file treeCollapse file tree

3 files changed

+46
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+46
-8
lines changed
Open diff view settings
Collapse file

‎doc/api/console.md‎

Copy file name to clipboardExpand all lines: doc/api/console.md
+6-2Lines changed: 6 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
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

Collapse file

‎lib/internal/console/constructor.js‎

Copy file name to clipboardExpand all lines: lib/internal/console/constructor.js
+17-6Lines 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;
Collapse file
+23Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.