-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: make null an invalid chunk to write in object mode
this harmonizes behavior between readable, writable, and transform streams so that they all handle nulls in object mode the same way by considering them invalid chunks. PR-URL: #6170 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
816df10
commit c0abecd
Showing
2 changed files
with
66 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const stream = require('stream'); | ||
const util = require('util'); | ||
|
||
function MyWritable(options) { | ||
stream.Writable.call(this, options); | ||
} | ||
|
||
util.inherits(MyWritable, stream.Writable); | ||
|
||
MyWritable.prototype._write = function(chunk, encoding, callback) { | ||
assert.notStrictEqual(chunk, null); | ||
callback(); | ||
}; | ||
|
||
assert.throws(() => { | ||
var m = new MyWritable({objectMode: true}); | ||
m.write(null, (err) => assert.ok(err)); | ||
}, TypeError, 'May not write null values to stream'); | ||
assert.doesNotThrow(() => { | ||
var m = new MyWritable({objectMode: true}).on('error', (e) => { | ||
assert.ok(e); | ||
}); | ||
m.write(null, (err) => { | ||
assert.ok(err); | ||
}); | ||
}); | ||
|
||
assert.throws(() => { | ||
var m = new MyWritable(); | ||
m.write(false, (err) => assert.ok(err)); | ||
}, TypeError, 'Invalid non-string/buffer chunk'); | ||
assert.doesNotThrow(() => { | ||
var m = new MyWritable().on('error', (e) => { | ||
assert.ok(e); | ||
}); | ||
m.write(false, (err) => { | ||
assert.ok(err); | ||
}); | ||
}); | ||
|
||
assert.doesNotThrow(() => { | ||
var m = new MyWritable({objectMode: true}); | ||
m.write(false, (err) => assert.ifError(err)); | ||
}); | ||
assert.doesNotThrow(() => { | ||
var m = new MyWritable({objectMode: true}).on('error', (e) => { | ||
assert.ifError(e || new Error('should not get here')); | ||
}); | ||
m.write(false, (err) => { | ||
assert.ifError(err); | ||
}); | ||
}); |