Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
617bfe4
lib: remove internalBinding('config').pendingDeprecation
joyeecheung Dec 11, 2018
c168672
inspector: move process.binding to internalBinding
BeniCheni Dec 10, 2018
dc4e5c5
util: inspect ArrayBuffers contents as well
BridgeAR Dec 13, 2018
0c67a51
lib: remove internal `util._extends()` usage
BridgeAR Dec 18, 2018
e97acf7
lib: expose all type checks from the internal types module
BridgeAR Dec 20, 2018
95fa3c7
buffer: inspect extra properties
BridgeAR Dec 20, 2018
7ff4402
lib: move lib/console.js to lib/internal/console/constructor.js
joyeecheung Nov 28, 2018
2f1f009
console: split console into global.js and constructor.js
joyeecheung Nov 28, 2018
6280cda
console: move the inspector console wrapping in a separate file
joyeecheung Nov 28, 2018
6bf7511
console: add `inspectOptions` option
BridgeAR Dec 12, 2018
049b24d
console: improve inspectOptions validation
cjihrig Dec 17, 2018
8d166c9
process: provide dummy stdio for non-console Windows apps
addaleax May 9, 2018
c219b8f
Revert "lib: expose all type checks from the internal types module"
joyeecheung Jan 11, 2019
f4dbe57
Revert "inspector: move process.binding to internalBinding"
joyeecheung Jan 11, 2019
7aeec97
Revert "lib: remove internalBinding('config').pendingDeprecation"
joyeecheung Jan 11, 2019
48dfda7
Revert "os: move process.binding('os') to internalBinding"
joyeecheung Jan 11, 2019
abfde34
Revert "lib: remove unused NativeModule/NativeModule wraps"
joyeecheung Jan 11, 2019
c5b2c31
Revert "src,lib: make process.binding('config') internal"
joyeecheung Jan 11, 2019
fb6f2f2
Revert "lib: remove duplicated noop function"
joyeecheung Jan 11, 2019
106b299
fs: make process.binding('fs') internal
Aug 23, 2018
9a5984d
src: use NativeModuleLoader to compile all the bootstrappers
joyeecheung Nov 26, 2018
373bbe7
lib: remove duplicated noop function
ZYSzys Dec 1, 2018
b6afa46
src,lib: make process.binding('config') internal
Oct 8, 2018
5bc4592
lib: remove unused NativeModule/NativeModule wraps
joyeecheung Dec 8, 2018
e05e5b3
inspector: move process.binding to internalBinding
BeniCheni Dec 10, 2018
181d03a
os: move process.binding('os') to internalBinding
briete Dec 17, 2018
4cb9036
lib: remove internalBinding('config').pendingDeprecation
joyeecheung Dec 11, 2018
7d90f03
lib: expose all type checks from the internal types module
BridgeAR Dec 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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>
  • Loading branch information
BridgeAR committed Jan 10, 2019
commit 6bf75119792551b3ccdfbc0c5adefa9e2c461afa
8 changes: 7 additions & 1 deletion doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ changes:
pr-url: https://github.com/nodejs/node/pull/19372
description: The `Console` constructor now supports an `options` argument,
and the `colorMode` option was introduced.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/24978
description: The `inspectOptions` option is introduced.
-->

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

Creates a new `Console` with one or two writable stream instances. `stdout` is a
writable stream to print log or info output. `stderr` is used for warning or
Expand Down
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,12 @@ is set for the `Http2Stream`.
`http2.connect()` was passed a URL that uses any protocol other than `http:` or
`https:`.

<a id="ERR_INCOMPATIBLE_OPTION_PAIR"></a>
### ERR_INCOMPATIBLE_OPTION_PAIR

An option pair is incompatible with each other and can not be used at the same
time.

<a id="ERR_INSPECTOR_ALREADY_CONNECTED"></a>
### ERR_INSPECTOR_ALREADY_CONNECTED

Expand Down
23 changes: 22 additions & 1 deletion lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
ERR_CONSOLE_WRITABLE_STREAM,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INCOMPATIBLE_OPTION_PAIR,
},
} = require('internal/errors');
const { previewEntries } = internalBinding('util');
Expand Down Expand Up @@ -54,6 +55,8 @@ const kBindStreamsLazy = Symbol('kBindStreamsLazy');
const kUseStdout = Symbol('kUseStdout');
const kUseStderr = Symbol('kUseStderr');

const optionsMap = new WeakMap();

function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
// We have to test new.target here to see if this function is called
// with new, because we need to define a custom instanceof to accommodate
Expand All @@ -74,7 +77,8 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
stdout,
stderr = stdout,
ignoreErrors = true,
colorMode = 'auto'
colorMode = 'auto',
inspectOptions
} = options;

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

if (inspectOptions) {
if (inspectOptions.colors !== undefined &&
options.colorMode !== undefined) {
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
'inspectOptions.color', 'colorMode');
}
optionsMap.set(this, inspectOptions);
}

// Bind the prototype functions to this Console instance
var keys = Object.keys(Console.prototype);
for (var v = 0; v < keys.length; v++) {
Expand Down Expand Up @@ -243,6 +256,14 @@ Console.prototype[kGetInspectOptions] = function(stream) {
stream.getColorDepth() > 2 : true);
}

const options = optionsMap.get(this);
if (options) {
if (options.colors === undefined) {
options.colors = color;
}
return options;
}

return color ? kColorInspectOptions : kNoColorInspectOptions;
};

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,8 @@ E('ERR_HTTP_INVALID_HEADER_VALUE',
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
E('ERR_HTTP_TRAILER_INVALID',
'Trailers are invalid with this transfer encoding', Error);
E('ERR_INCOMPATIBLE_OPTION_PAIR',
'Option "%s" can not be used in combination with option "%s"', TypeError);
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
Expand Down
33 changes: 29 additions & 4 deletions test/parallel/test-console-tty-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const util = require('util');
const { Writable } = require('stream');
const { Console } = require('console');

function check(isTTY, colorMode, expectedColorMode) {
function check(isTTY, colorMode, expectedColorMode, inspectOptions) {
const items = [
1,
{ a: 2 },
Expand All @@ -18,7 +18,8 @@ function check(isTTY, colorMode, expectedColorMode) {
write: common.mustCall((chunk, enc, cb) => {
assert.strictEqual(chunk.trim(),
util.inspect(items[i++], {
colors: expectedColorMode
colors: expectedColorMode,
...inspectOptions
}));
cb();
}, items.length),
Expand All @@ -31,7 +32,8 @@ function check(isTTY, colorMode, expectedColorMode) {
const testConsole = new Console({
stdout: stream,
ignoreErrors: false,
colorMode
colorMode,
inspectOptions
});
for (const item of items) {
testConsole.log(item);
Expand All @@ -40,12 +42,15 @@ function check(isTTY, colorMode, expectedColorMode) {

check(true, 'auto', true);
check(false, 'auto', false);
check(false, undefined, true, { colors: true, compact: false });
check(true, 'auto', true, { compact: false });
check(true, undefined, false, { colors: false });
check(true, true, true);
check(false, true, true);
check(true, false, false);
check(false, false, false);

// check invalid colorMode type
// Check invalid options.
{
const stream = new Writable({
write: common.mustNotCall()
Expand All @@ -67,4 +72,24 @@ check(false, false, false);
}
);
});

[true, false, 'auto'].forEach((colorMode) => {
assert.throws(
() => {
new Console({
stdout: stream,
ignoreErrors: false,
colorMode: colorMode,
inspectOptions: {
colors: false
}
});
},
{
message: 'Option "inspectOptions.color" can not be used in ' +
'combination with option "colorMode"',
code: 'ERR_INCOMPATIBLE_OPTION_PAIR'
}
);
});
}