Skip to content

Commit

Permalink
stream: make _write() optional when _writev() is implemented
Browse files Browse the repository at this point in the history
When implementing _writev, _write should be optional.

PR-URL: #29639
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
ronag authored and BridgeAR committed Sep 25, 2019
1 parent 2707beb commit e71bdad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
17 changes: 12 additions & 5 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,8 @@ const myWritable = new Writable({
The `stream.Writable` class is extended to implement a [`Writable`][] stream.

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

#### Constructor: new stream.Writable([options])
<!-- YAML
Expand Down Expand Up @@ -1774,6 +1774,12 @@ const myWritable = new Writable({
```

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

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

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

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

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

Expand Down
6 changes: 5 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,11 @@ function clearBuffer(stream, state) {
}

Writable.prototype._write = function(chunk, encoding, cb) {
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
if (this._writev) {
this._writev([{ chunk, encoding }], cb);
} else {
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
}
};

Writable.prototype._writev = null;
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-stream-writev.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,12 @@ function test(decode, uncork, multi, next) {
next();
});
}

{
const w = new stream.Writable({
writev: common.mustCall(function(chunks, cb) {
cb();
})
});
w.write('asd', common.mustCall());
}

0 comments on commit e71bdad

Please sign in to comment.