-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: emit .write() end error in next tick.
This changes the behaviour of error event emitted when writing to a stream after it has ended, from synchronously to asynchronously. PR-URL: #4749
- Loading branch information
Showing
3 changed files
with
45 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const assert = require('assert'); | ||
const Writable = require('stream').Writable; | ||
|
||
const stream = new Writable({ write }); | ||
|
||
let errorsRecorded = 0; | ||
|
||
function write() { | ||
throw new Error('write() should not have been called!'); | ||
} | ||
|
||
function errorRecorder(err) { | ||
if (err.message === 'write after end') { | ||
errorsRecorded++; | ||
} | ||
} | ||
|
||
// should trigger ended errors when writing later | ||
stream.end(); | ||
|
||
stream.on('error', errorRecorder); | ||
stream.write('this should explode', errorRecorder); | ||
|
||
assert.equal(errorsRecorded, 0, | ||
'Waits until next tick before emitting error'); | ||
|
||
process.nextTick(() => assert.equal(errorsRecorded, 2)); |