Skip to content

Commit 5e9cac4

Browse files
Trottevanlucas
authored andcommitted
console: check that stderr is writable
`Console` constructor checks that `stdout.write()` is a function but does not do an equivalent check for `stderr.write()`. If `stderr` is not specified in the constructor, then `stderr` is set to be `stdout`. However, if `stderr` is specified, but `stderr.write()` is not a function, then an exception is not thrown until `console.error()` is called. This change adds the same check for 'stderr' in the constructor that is there for `stdout`. If `stderr` fails the check, then a `TypeError` is thrown. Took the opportunity to copyedit the `console` doc a little too. PR-URL: #5635 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
1 parent 4ecd996 commit 5e9cac4

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

doc/api/console.markdown

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ myConsole.warn(`Danger ${name}! Danger!`);
4848
```
4949

5050
While the API for the `Console` class is designed fundamentally around the
51-
Web browser `console` object, the `Console` in Node.js is *not* intended to
52-
duplicate the browsers functionality exactly.
51+
browser `console` object, the `Console` in Node.js is *not* intended to
52+
duplicate the browser's functionality exactly.
5353

5454
## Asynchronous vs Synchronous Consoles
5555

@@ -75,8 +75,8 @@ const Console = console.Console;
7575

7676
Creates a new `Console` by passing one or two writable stream instances.
7777
`stdout` is a writable stream to print log or info output. `stderr`
78-
is used for warning or error output. If `stderr` isn't passed, the warning
79-
and error output will be sent to the `stdout`.
78+
is used for warning or error output. If `stderr` isn't passed, warning and error
79+
output will be sent to `stdout`.
8080

8181
```js
8282
const output = fs.createWriteStream('./stdout.log');

lib/console.js

+3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ function Console(stdout, stderr) {
1111
}
1212
if (!stderr) {
1313
stderr = stdout;
14+
} else if (typeof stderr.write !== 'function') {
15+
throw new TypeError('Console expects writable stream instances');
1416
}
17+
1518
var prop = {
1619
writable: true,
1720
enumerable: false,

test/parallel/test-console-instance.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
22
require('../common');
3-
var assert = require('assert');
4-
var Stream = require('stream');
5-
var Console = require('console').Console;
3+
const assert = require('assert');
4+
const Stream = require('stream');
5+
const Console = require('console').Console;
66
var called = false;
77

8+
const out = new Stream();
9+
const err = new Stream();
10+
811
// ensure the Console instance doesn't write to the
912
// process' "stdout" or "stderr" streams
1013
process.stdout.write = process.stderr.write = function() {
@@ -20,9 +23,13 @@ assert.throws(function() {
2023
new Console();
2124
}, /Console expects a writable stream/);
2225

23-
var out = new Stream();
24-
var err = new Stream();
25-
out.writable = err.writable = true;
26+
// Console constructor should throw if stderr exists but is not writable
27+
assert.throws(function() {
28+
out.write = function() {};
29+
err.write = undefined;
30+
new Console(out, err);
31+
}, /Console expects writable stream instances/);
32+
2633
out.write = err.write = function(d) {};
2734

2835
var c = new Console(out, err);

0 commit comments

Comments
 (0)