Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: _write optional when _writev #29639

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1667,8 +1667,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 @@ -1757,6 +1757,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 @@ -1770,7 +1776,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
ronag marked this conversation as resolved.
Show resolved Hide resolved
resource.

[`Transform`][] streams provide their own implementation of the
Expand Down Expand Up @@ -1813,8 +1820,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 @@ -558,7 +558,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this introduces a small performance penalty but the user can implement _write() to avoid it.

} 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());
}