summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-12-12 03:26:15 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2018-12-17 16:42:36 +0100
commitbe3ae339360c9833a77ebf5772786d75b7a8dd78 (patch)
treec52c3e2b5843eb12fb3d177c47b760877e57de7a /lib
parenta361b94b783bc92296307602f56d8beccad3fd22 (diff)
downloadandroid-node-v8-be3ae339360c9833a77ebf5772786d75b7a8dd78.tar.gz
android-node-v8-be3ae339360c9833a77ebf5772786d75b7a8dd78.tar.bz2
android-node-v8-be3ae339360c9833a77ebf5772786d75b7a8dd78.zip
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: https://github.com/nodejs/node/pull/24978 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/console/constructor.js23
-rw-r--r--lib/internal/errors.js2
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js
index d3c5ed7436..da54dd11a7 100644
--- a/lib/internal/console/constructor.js
+++ b/lib/internal/console/constructor.js
@@ -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');
@@ -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
@@ -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') {
@@ -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++) {
@@ -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;
};
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index b8f8b4cfa2..174281f8a2 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -686,6 +686,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);