Skip to content

Commit d8365bc

Browse files
BridgeARcodebytere
authored andcommitted
console: mark special console properties as non-enumerable
This makes sure internal console properties are not visible during default inspection. They are still visible when inspecting the console with `showHidden` set to `true`. These properties are confusing while working with the REPL and easily show up. Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #33524 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 80782cb commit d8365bc

File tree

1 file changed

+147
-129
lines changed

1 file changed

+147
-129
lines changed

lib/internal/console/constructor.js

+147-129
Original file line numberDiff line numberDiff line change
@@ -150,68 +150,156 @@ ObjectDefineProperty(Console, SymbolHasInstance, {
150150
}
151151
});
152152

153-
// Eager version for the Console constructor
154-
Console.prototype[kBindStreamsEager] = function(stdout, stderr) {
155-
ObjectDefineProperties(this, {
156-
'_stdout': { ...consolePropAttributes, value: stdout },
157-
'_stderr': { ...consolePropAttributes, value: stderr }
158-
});
159-
};
153+
const kColorInspectOptions = { colors: true };
154+
const kNoColorInspectOptions = {};
160155

161-
// Lazily load the stdout and stderr from an object so we don't
162-
// create the stdio streams when they are not even accessed
163-
Console.prototype[kBindStreamsLazy] = function(object) {
164-
let stdout;
165-
let stderr;
166-
ObjectDefineProperties(this, {
167-
'_stdout': {
168-
enumerable: false,
169-
configurable: true,
170-
get() {
171-
if (!stdout) stdout = object.stdout;
172-
return stdout;
173-
},
174-
set(value) { stdout = value; }
175-
},
176-
'_stderr': {
177-
enumerable: false,
178-
configurable: true,
179-
get() {
180-
if (!stderr) { stderr = object.stderr; }
181-
return stderr;
182-
},
183-
set(value) { stderr = value; }
156+
ObjectDefineProperties(Console.prototype, {
157+
[kBindStreamsEager]: {
158+
...consolePropAttributes,
159+
// Eager version for the Console constructor
160+
value: function(stdout, stderr) {
161+
ObjectDefineProperties(this, {
162+
'_stdout': { ...consolePropAttributes, value: stdout },
163+
'_stderr': { ...consolePropAttributes, value: stderr }
164+
});
184165
}
185-
});
186-
};
166+
},
167+
[kBindStreamsLazy]: {
168+
...consolePropAttributes,
169+
// Lazily load the stdout and stderr from an object so we don't
170+
// create the stdio streams when they are not even accessed
171+
value: function(object) {
172+
let stdout;
173+
let stderr;
174+
ObjectDefineProperties(this, {
175+
'_stdout': {
176+
enumerable: false,
177+
configurable: true,
178+
get() {
179+
if (!stdout) stdout = object.stdout;
180+
return stdout;
181+
},
182+
set(value) { stdout = value; }
183+
},
184+
'_stderr': {
185+
enumerable: false,
186+
configurable: true,
187+
get() {
188+
if (!stderr) { stderr = object.stderr; }
189+
return stderr;
190+
},
191+
set(value) { stderr = value; }
192+
}
193+
});
194+
}
195+
},
196+
[kBindProperties]: {
197+
...consolePropAttributes,
198+
value: function(ignoreErrors, colorMode, groupIndentation = 2) {
199+
ObjectDefineProperties(this, {
200+
'_stdoutErrorHandler': {
201+
...consolePropAttributes,
202+
value: createWriteErrorHandler(this, kUseStdout)
203+
},
204+
'_stderrErrorHandler': {
205+
...consolePropAttributes,
206+
value: createWriteErrorHandler(this, kUseStderr)
207+
},
208+
'_ignoreErrors': {
209+
...consolePropAttributes,
210+
value: Boolean(ignoreErrors)
211+
},
212+
'_times': { ...consolePropAttributes, value: new Map() },
213+
// Corresponds to https://console.spec.whatwg.org/#count-map
214+
[kCounts]: { ...consolePropAttributes, value: new Map() },
215+
[kColorMode]: { ...consolePropAttributes, value: colorMode },
216+
[kIsConsole]: { ...consolePropAttributes, value: true },
217+
[kGroupIndent]: { ...consolePropAttributes, value: '' },
218+
[kGroupIndentationWidth]: {
219+
...consolePropAttributes,
220+
value: groupIndentation
221+
},
222+
});
223+
}
224+
},
225+
[kWriteToConsole]: {
226+
...consolePropAttributes,
227+
value: function(streamSymbol, string) {
228+
const ignoreErrors = this._ignoreErrors;
229+
const groupIndent = this[kGroupIndent];
230+
231+
const useStdout = streamSymbol === kUseStdout;
232+
const stream = useStdout ? this._stdout : this._stderr;
233+
const errorHandler = useStdout ?
234+
this._stdoutErrorHandler : this._stderrErrorHandler;
235+
236+
if (groupIndent.length !== 0) {
237+
if (string.includes('\n')) {
238+
string = string.replace(/\n/g, `\n${groupIndent}`);
239+
}
240+
string = groupIndent + string;
241+
}
242+
string += '\n';
243+
244+
if (ignoreErrors === false) return stream.write(string);
245+
246+
// There may be an error occurring synchronously (e.g. for files or TTYs
247+
// on POSIX systems) or asynchronously (e.g. pipes on POSIX systems), so
248+
// handle both situations.
249+
try {
250+
// Add and later remove a noop error handler to catch synchronous
251+
// errors.
252+
if (stream.listenerCount('error') === 0)
253+
stream.once('error', noop);
254+
255+
stream.write(string, errorHandler);
256+
} catch (e) {
257+
// Console is a debugging utility, so it swallowing errors is not
258+
// desirable even in edge cases such as low stack space.
259+
if (isStackOverflowError(e))
260+
throw e;
261+
// Sorry, there's no proper way to pass along the error here.
262+
} finally {
263+
stream.removeListener('error', noop);
264+
}
265+
}
266+
},
267+
[kGetInspectOptions]: {
268+
...consolePropAttributes,
269+
value: function(stream) {
270+
let color = this[kColorMode];
271+
if (color === 'auto') {
272+
color = stream.isTTY && (
273+
typeof stream.getColorDepth === 'function' ?
274+
stream.getColorDepth() > 2 : true);
275+
}
187276

188-
Console.prototype[kBindProperties] = function(ignoreErrors, colorMode,
189-
groupIndentation = 2) {
190-
ObjectDefineProperties(this, {
191-
'_stdoutErrorHandler': {
192-
...consolePropAttributes,
193-
value: createWriteErrorHandler(this, kUseStdout)
194-
},
195-
'_stderrErrorHandler': {
196-
...consolePropAttributes,
197-
value: createWriteErrorHandler(this, kUseStderr)
198-
},
199-
'_ignoreErrors': {
200-
...consolePropAttributes,
201-
value: Boolean(ignoreErrors)
202-
},
203-
'_times': { ...consolePropAttributes, value: new Map() },
204-
// Corresponds to https://console.spec.whatwg.org/#count-map
205-
[kCounts]: { ...consolePropAttributes, value: new Map() },
206-
[kColorMode]: { ...consolePropAttributes, value: colorMode },
207-
[kIsConsole]: { ...consolePropAttributes, value: true },
208-
[kGroupIndent]: { ...consolePropAttributes, value: '' },
209-
[kGroupIndentationWidth]: {
210-
...consolePropAttributes,
211-
value: groupIndentation
212-
},
213-
});
214-
};
277+
const options = optionsMap.get(this);
278+
if (options) {
279+
if (options.colors === undefined) {
280+
options.colors = color;
281+
}
282+
return options;
283+
}
284+
285+
return color ? kColorInspectOptions : kNoColorInspectOptions;
286+
}
287+
},
288+
[kFormatForStdout]: {
289+
...consolePropAttributes,
290+
value: function(args) {
291+
const opts = this[kGetInspectOptions](this._stdout);
292+
return formatWithOptions(opts, ...args);
293+
}
294+
},
295+
[kFormatForStderr]: {
296+
...consolePropAttributes,
297+
value: function(args) {
298+
const opts = this[kGetInspectOptions](this._stderr);
299+
return formatWithOptions(opts, ...args);
300+
}
301+
},
302+
});
215303

216304
// Make a function that can serve as the callback passed to `stream.write()`.
217305
function createWriteErrorHandler(instance, streamSymbol) {
@@ -234,76 +322,6 @@ function createWriteErrorHandler(instance, streamSymbol) {
234322
};
235323
}
236324

237-
Console.prototype[kWriteToConsole] = function(streamSymbol, string) {
238-
const ignoreErrors = this._ignoreErrors;
239-
const groupIndent = this[kGroupIndent];
240-
241-
const useStdout = streamSymbol === kUseStdout;
242-
const stream = useStdout ? this._stdout : this._stderr;
243-
const errorHandler = useStdout ?
244-
this._stdoutErrorHandler : this._stderrErrorHandler;
245-
246-
if (groupIndent.length !== 0) {
247-
if (string.includes('\n')) {
248-
string = string.replace(/\n/g, `\n${groupIndent}`);
249-
}
250-
string = groupIndent + string;
251-
}
252-
string += '\n';
253-
254-
if (ignoreErrors === false) return stream.write(string);
255-
256-
// There may be an error occurring synchronously (e.g. for files or TTYs
257-
// on POSIX systems) or asynchronously (e.g. pipes on POSIX systems), so
258-
// handle both situations.
259-
try {
260-
// Add and later remove a noop error handler to catch synchronous errors.
261-
if (stream.listenerCount('error') === 0)
262-
stream.once('error', noop);
263-
264-
stream.write(string, errorHandler);
265-
} catch (e) {
266-
// Console is a debugging utility, so it swallowing errors is not desirable
267-
// even in edge cases such as low stack space.
268-
if (isStackOverflowError(e))
269-
throw e;
270-
// Sorry, there's no proper way to pass along the error here.
271-
} finally {
272-
stream.removeListener('error', noop);
273-
}
274-
};
275-
276-
const kColorInspectOptions = { colors: true };
277-
const kNoColorInspectOptions = {};
278-
Console.prototype[kGetInspectOptions] = function(stream) {
279-
let color = this[kColorMode];
280-
if (color === 'auto') {
281-
color = stream.isTTY && (
282-
typeof stream.getColorDepth === 'function' ?
283-
stream.getColorDepth() > 2 : true);
284-
}
285-
286-
const options = optionsMap.get(this);
287-
if (options) {
288-
if (options.colors === undefined) {
289-
options.colors = color;
290-
}
291-
return options;
292-
}
293-
294-
return color ? kColorInspectOptions : kNoColorInspectOptions;
295-
};
296-
297-
Console.prototype[kFormatForStdout] = function(args) {
298-
const opts = this[kGetInspectOptions](this._stdout);
299-
return formatWithOptions(opts, ...args);
300-
};
301-
302-
Console.prototype[kFormatForStderr] = function(args) {
303-
const opts = this[kGetInspectOptions](this._stderr);
304-
return formatWithOptions(opts, ...args);
305-
};
306-
307325
const consoleMethods = {
308326
log(...args) {
309327
this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args));

0 commit comments

Comments
 (0)