| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4ecd996 commit 5e9cac4
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,8 +48,8 @@ myConsole.warn(`Danger ${name}! Danger!`); | |||
| 48 | 48 | ``` | |
| 49 | 49 | ||
| 50 | 50 | 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. | ||
| 53 | 53 | ||
| 54 | 54 | ## Asynchronous vs Synchronous Consoles | |
| 55 | 55 | ||
@@ -75,8 +75,8 @@ const Console = console.Console; | |||
| 75 | 75 | ||
| 76 | 76 | Creates a new `Console` by passing one or two writable stream instances. | |
| 77 | 77 | `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`. | ||
| 80 | 80 | ||
| 81 | 81 | ```js | |
| 82 | 82 | const output = fs.createWriteStream('./stdout.log'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,10 @@ function Console(stdout, stderr) { | |||
| 11 | 11 | } | |
| 12 | 12 | if (!stderr) { | |
| 13 | 13 | stderr = stdout; | |
| 14 | + } else if (typeof stderr.write !== 'function') { | ||
| 15 | + throw new TypeError('Console expects writable stream instances'); | ||
| 14 | 16 | } | |
| 17 | + | ||
| 15 | 18 | var prop = { | |
| 16 | 19 | writable: true, | |
| 17 | 20 | enumerable: false, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,13 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | 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; | ||
| 6 | 6 | var called = false; | |
| 7 | 7 | ||
| 8 | + const out = new Stream(); | ||
| 9 | + const err = new Stream(); | ||
| 10 | + | ||
| 8 | 11 | // ensure the Console instance doesn't write to the | |
| 9 | 12 | // process' "stdout" or "stderr" streams | |
| 10 | 13 | process.stdout.write = process.stderr.write = function() { | |
@@ -20,9 +23,13 @@ assert.throws(function() { | |||
| 20 | 23 | new Console(); | |
| 21 | 24 | }, /Console expects a writable stream/); | |
| 22 | 25 | ||
| 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 | + | ||
| 26 | 33 | out.write = err.write = function(d) {}; | |
| 27 | 34 | ||
| 28 | 35 | var c = new Console(out, err); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments