Skip to content

Commit d398b8f

Browse files
ronagTrott
authored andcommitted
stream: make _write() optional when _writev() is implemented
When implementing _writev, _write should be optional. PR-URL: #29639 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 68d6c4c commit d398b8f

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

doc/api/stream.md

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

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

16931693
#### Constructor: new stream.Writable([options])
16941694
<!-- YAML
@@ -1777,6 +1777,12 @@ const myWritable = new Writable({
17771777
```
17781778

17791779
#### writable.\_write(chunk, encoding, callback)
1780+
<!-- YAML
1781+
changes:
1782+
- version: REPLACEME
1783+
pr-url: https://github.com/nodejs/node/pull/29639
1784+
description: _write() is optional when providing _writev().
1785+
-->
17801786

17811787
* `chunk` {Buffer|string|any} The `Buffer` to be written, converted from the
17821788
`string` passed to [`stream.write()`][stream-write]. If the stream's
@@ -1790,7 +1796,8 @@ const myWritable = new Writable({
17901796
argument) when processing is complete for the supplied chunk.
17911797

17921798
All `Writable` stream implementations must provide a
1793-
[`writable._write()`][stream-_write] method to send data to the underlying
1799+
[`writable._write()`][stream-_write] and/or
1800+
[`writable._writev()`][stream-_writev] method to send data to the underlying
17941801
resource.
17951802

17961803
[`Transform`][] streams provide their own implementation of the
@@ -1833,8 +1840,8 @@ This function MUST NOT be called by application code directly. It should be
18331840
implemented by child classes, and called by the internal `Writable` class
18341841
methods only.
18351842

1836-
The `writable._writev()` method may be implemented in addition to
1837-
`writable._write()` in stream implementations that are capable of processing
1843+
The `writable._writev()` method may be implemented in addition or alternatively
1844+
to `writable._write()` in stream implementations that are capable of processing
18381845
multiple chunks of data at once. If implemented, the method will be called with
18391846
all chunks of data currently buffered in the write queue.
18401847

lib/_stream_writable.js

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

567567
Writable.prototype._write = function(chunk, encoding, cb) {
568-
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
568+
if (this._writev) {
569+
this._writev([{ chunk, encoding }], cb);
570+
} else {
571+
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
572+
}
569573
};
570574

571575
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: common.mustCall(function(chunks, cb) {
126+
cb();
127+
})
128+
});
129+
w.write('asd', common.mustCall());
130+
}

0 commit comments

Comments
 (0)