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 65d485b

Browse filesBrowse files
BridgeARaddaleax
authored andcommitted
console: add inspectOptions option
Add an `inspectOptions` option to the `console` constructor. That way it's possible to define all inspection defaults for each `console` instance instead of relying on the `inspect()` defaults. PR-URL: #24978 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 57323e8 commit 65d485b
Copy full SHA for 65d485b

File tree

Expand file treeCollapse file tree

5 files changed

+66
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+66
-6
lines changed
Open diff view settings
Collapse file

‎doc/api/console.md‎

Copy file name to clipboardExpand all lines: doc/api/console.md
+7-1Lines changed: 7 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ changes:
8888
pr-url: https://github.com/nodejs/node/pull/19372
8989
description: The `Console` constructor now supports an `options` argument,
9090
and the `colorMode` option was introduced.
91+
- version: REPLACEME
92+
pr-url: https://github.com/nodejs/node/pull/24978
93+
description: The `inspectOptions` option is introduced.
9194
-->
9295

9396
* `options` {Object}
@@ -98,8 +101,11 @@ changes:
98101
* `colorMode` {boolean|string} Set color support for this `Console` instance.
99102
Setting to `true` enables coloring while inspecting values, setting to
100103
`'auto'` will make color support depend on the value of the `isTTY` property
101-
and the value returned by `getColorDepth()` on the respective stream.
104+
and the value returned by `getColorDepth()` on the respective stream. This
105+
option can not be used, if `inspectOptions.colors` is set as well.
102106
**Default:** `'auto'`.
107+
* `inspectOptions` {Object} Specifies options that are passed along to
108+
[`util.inspect()`][].
103109

104110
Creates a new `Console` with one or two writable stream instances. `stdout` is a
105111
writable stream to print log or info output. `stderr` is used for warning or
Collapse file

‎doc/api/errors.md‎

Copy file name to clipboardExpand all lines: doc/api/errors.md
+6Lines changed: 6 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,12 @@ is set for the `Http2Stream`.
11411141
`http2.connect()` was passed a URL that uses any protocol other than `http:` or
11421142
`https:`.
11431143

1144+
<a id="ERR_INCOMPATIBLE_OPTION_PAIR"></a>
1145+
### ERR_INCOMPATIBLE_OPTION_PAIR
1146+
1147+
An option pair is incompatible with each other and can not be used at the same
1148+
time.
1149+
11441150
<a id="ERR_INSPECTOR_ALREADY_CONNECTED"></a>
11451151
### ERR_INSPECTOR_ALREADY_CONNECTED
11461152

Collapse file

‎lib/internal/console/constructor.js‎

Copy file name to clipboardExpand all lines: lib/internal/console/constructor.js
+22-1Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ERR_CONSOLE_WRITABLE_STREAM,
1111
ERR_INVALID_ARG_TYPE,
1212
ERR_INVALID_ARG_VALUE,
13+
ERR_INCOMPATIBLE_OPTION_PAIR,
1314
},
1415
} = require('internal/errors');
1516
const { previewEntries } = internalBinding('util');
@@ -54,6 +55,8 @@ const kBindStreamsLazy = Symbol('kBindStreamsLazy');
5455
const kUseStdout = Symbol('kUseStdout');
5556
const kUseStderr = Symbol('kUseStderr');
5657

58+
const optionsMap = new WeakMap();
59+
5760
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
5861
// We have to test new.target here to see if this function is called
5962
// with new, because we need to define a custom instanceof to accommodate
@@ -74,7 +77,8 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
7477
stdout,
7578
stderr = stdout,
7679
ignoreErrors = true,
77-
colorMode = 'auto'
80+
colorMode = 'auto',
81+
inspectOptions
7882
} = options;
7983

8084
if (!stdout || typeof stdout.write !== 'function') {
@@ -87,6 +91,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
8791
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
8892
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);
8993

94+
if (inspectOptions) {
95+
if (inspectOptions.colors !== undefined &&
96+
options.colorMode !== undefined) {
97+
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
98+
'inspectOptions.color', 'colorMode');
99+
}
100+
optionsMap.set(this, inspectOptions);
101+
}
102+
90103
// Bind the prototype functions to this Console instance
91104
var keys = Object.keys(Console.prototype);
92105
for (var v = 0; v < keys.length; v++) {
@@ -243,6 +256,14 @@ Console.prototype[kGetInspectOptions] = function(stream) {
243256
stream.getColorDepth() > 2 : true);
244257
}
245258

259+
const options = optionsMap.get(this);
260+
if (options) {
261+
if (options.colors === undefined) {
262+
options.colors = color;
263+
}
264+
return options;
265+
}
266+
246267
return color ? kColorInspectOptions : kNoColorInspectOptions;
247268
};
248269

Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,8 @@ E('ERR_HTTP_INVALID_HEADER_VALUE',
702702
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
703703
E('ERR_HTTP_TRAILER_INVALID',
704704
'Trailers are invalid with this transfer encoding', Error);
705+
E('ERR_INCOMPATIBLE_OPTION_PAIR',
706+
'Option "%s" can not be used in combination with option "%s"', TypeError);
705707
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
706708
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
707709
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
Collapse file

‎test/parallel/test-console-tty-colors.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-console-tty-colors.js
+29-4Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const util = require('util');
55
const { Writable } = require('stream');
66
const { Console } = require('console');
77

8-
function check(isTTY, colorMode, expectedColorMode) {
8+
function check(isTTY, colorMode, expectedColorMode, inspectOptions) {
99
const items = [
1010
1,
1111
{ a: 2 },
@@ -18,7 +18,8 @@ function check(isTTY, colorMode, expectedColorMode) {
1818
write: common.mustCall((chunk, enc, cb) => {
1919
assert.strictEqual(chunk.trim(),
2020
util.inspect(items[i++], {
21-
colors: expectedColorMode
21+
colors: expectedColorMode,
22+
...inspectOptions
2223
}));
2324
cb();
2425
}, items.length),
@@ -31,7 +32,8 @@ function check(isTTY, colorMode, expectedColorMode) {
3132
const testConsole = new Console({
3233
stdout: stream,
3334
ignoreErrors: false,
34-
colorMode
35+
colorMode,
36+
inspectOptions
3537
});
3638
for (const item of items) {
3739
testConsole.log(item);
@@ -40,12 +42,15 @@ function check(isTTY, colorMode, expectedColorMode) {
4042

4143
check(true, 'auto', true);
4244
check(false, 'auto', false);
45+
check(false, undefined, true, { colors: true, compact: false });
46+
check(true, 'auto', true, { compact: false });
47+
check(true, undefined, false, { colors: false });
4348
check(true, true, true);
4449
check(false, true, true);
4550
check(true, false, false);
4651
check(false, false, false);
4752

48-
// check invalid colorMode type
53+
// Check invalid options.
4954
{
5055
const stream = new Writable({
5156
write: common.mustNotCall()
@@ -67,4 +72,24 @@ check(false, false, false);
6772
}
6873
);
6974
});
75+
76+
[true, false, 'auto'].forEach((colorMode) => {
77+
assert.throws(
78+
() => {
79+
new Console({
80+
stdout: stream,
81+
ignoreErrors: false,
82+
colorMode: colorMode,
83+
inspectOptions: {
84+
colors: false
85+
}
86+
});
87+
},
88+
{
89+
message: 'Option "inspectOptions.color" can not be used in ' +
90+
'combination with option "colorMode"',
91+
code: 'ERR_INCOMPATIBLE_OPTION_PAIR'
92+
}
93+
);
94+
});
7095
}

0 commit comments

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