Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console: check that stderr is writable #5635

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions doc/api/console.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ myConsole.warn(`Danger ${name}! Danger!`);
```

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

## Asynchronous vs Synchronous Consoles

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

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

```js
const output = fs.createWriteStream('./stdout.log');
Expand Down
3 changes: 3 additions & 0 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ function Console(stdout, stderr) {
}
if (!stderr) {
stderr = stdout;
} else if (typeof stderr.write !== 'function') {
throw new TypeError('Console expects writable stream instances');
}

var prop = {
writable: true,
enumerable: false,
Expand Down
19 changes: 13 additions & 6 deletions test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';
require('../common');
var assert = require('assert');
var Stream = require('stream');
var Console = require('console').Console;
const assert = require('assert');
const Stream = require('stream');
const Console = require('console').Console;
var called = false;

const out = new Stream();
const err = new Stream();

// ensure the Console instance doesn't write to the
// process' "stdout" or "stderr" streams
process.stdout.write = process.stderr.write = function() {
Expand All @@ -20,9 +23,13 @@ assert.throws(function() {
new Console();
}, /Console expects a writable stream/);

var out = new Stream();
var err = new Stream();
out.writable = err.writable = true;
// Console constructor should throw if stderr exists but is not writable
assert.throws(function() {
out.write = function() {};
err.write = undefined;
new Console(out, err);
}, /Console expects writable stream instances/);

out.write = err.write = function(d) {};

var c = new Console(out, err);
Expand Down