Skip to content

Commit 757de53

Browse files
committed
stream: _write optional when _writev
When implementing _writev, _write should be optional.
1 parent ba3be57 commit 757de53

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

doc/api/stream.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,8 +1667,8 @@ const myWritable = new Writable({
16671667
The `stream.Writable` class is extended to implement a [`Writable`][] stream.
16681668

16691669
Custom `Writable` streams *must* call the `new stream.Writable([options])`
1670-
constructor and implement the `writable._write()` method. The
1671-
`writable._writev()` method *may* also be implemented.
1670+
constructor and implement the `writable._write()` and/or `writable._writev()`
1671+
method.
16721672

16731673
#### Constructor: new stream.Writable([options])
16741674
<!-- YAML
@@ -1770,7 +1770,8 @@ const myWritable = new Writable({
17701770
argument) when processing is complete for the supplied chunk.
17711771

17721772
All `Writable` stream implementations must provide a
1773-
[`writable._write()`][stream-_write] method to send data to the underlying
1773+
[`writable._write()`][stream-_write] and/or
1774+
[`writable._writev()`][stream-_writev] method to send data to the underlying
17741775
resource.
17751776

17761777
[`Transform`][] streams provide their own implementation of the
@@ -1813,8 +1814,8 @@ This function MUST NOT be called by application code directly. It should be
18131814
implemented by child classes, and called by the internal `Writable` class
18141815
methods only.
18151816

1816-
The `writable._writev()` method may be implemented in addition to
1817-
`writable._write()` in stream implementations that are capable of processing
1817+
The `writable._writev()` method may be implemented in addition or alternatively
1818+
to `writable._write()` in stream implementations that are capable of processing
18181819
multiple chunks of data at once. If implemented, the method will be called with
18191820
all chunks of data currently buffered in the write queue.
18201821

lib/_stream_writable.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,11 @@ function clearBuffer(stream, state) {
558558
}
559559

560560
Writable.prototype._write = function(chunk, encoding, cb) {
561-
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
561+
if (this._writev) {
562+
this._writev([{ chunk, encoding }], cb);
563+
} else {
564+
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
565+
}
562566
};
563567

564568
Writable.prototype._writev = null;

test/parallel/test-stream-writev.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,12 @@ function test(decode, uncork, multi, next) {
119119
next();
120120
});
121121
}
122+
123+
{
124+
const w = new stream.Writable({
125+
writev: function(chunks, cb) {
126+
cb();
127+
}
128+
});
129+
w.write('asd', common.mustCall());
130+
}

0 commit comments

Comments
 (0)