Skip to content

Commit e71bdad

Browse files
ronagBridgeAR
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 2707beb commit e71bdad

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

doc/api/stream.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -1684,8 +1684,8 @@ const myWritable = new Writable({
16841684
The `stream.Writable` class is extended to implement a [`Writable`][] stream.
16851685

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

16901690
#### Constructor: new stream.Writable([options])
16911691
<!-- YAML
@@ -1774,6 +1774,12 @@ const myWritable = new Writable({
17741774
```
17751775

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

17781784
* `chunk` {Buffer|string|any} The `Buffer` to be written, converted from the
17791785
`string` passed to [`stream.write()`][stream-write]. If the stream's
@@ -1787,7 +1793,8 @@ const myWritable = new Writable({
17871793
argument) when processing is complete for the supplied chunk.
17881794

17891795
All `Writable` stream implementations must provide a
1790-
[`writable._write()`][stream-_write] method to send data to the underlying
1796+
[`writable._write()`][stream-_write] and/or
1797+
[`writable._writev()`][stream-_writev] method to send data to the underlying
17911798
resource.
17921799

17931800
[`Transform`][] streams provide their own implementation of the
@@ -1830,8 +1837,8 @@ This function MUST NOT be called by application code directly. It should be
18301837
implemented by child classes, and called by the internal `Writable` class
18311838
methods only.
18321839

1833-
The `writable._writev()` method may be implemented in addition to
1834-
`writable._write()` in stream implementations that are capable of processing
1840+
The `writable._writev()` method may be implemented in addition or alternatively
1841+
to `writable._write()` in stream implementations that are capable of processing
18351842
multiple chunks of data at once. If implemented, the method will be called with
18361843
all chunks of data currently buffered in the write queue.
18371844

lib/_stream_writable.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,11 @@ function clearBuffer(stream, state) {
569569
}
570570

571571
Writable.prototype._write = function(chunk, encoding, cb) {
572-
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
572+
if (this._writev) {
573+
this._writev([{ chunk, encoding }], cb);
574+
} else {
575+
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
576+
}
573577
};
574578

575579
Writable.prototype._writev = null;

test/parallel/test-stream-writev.js

+9
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)