Class: stream.Writable
The Writable stream interface is an abstraction for a destination that you are writing data to.
Examples of writable streams include:
- HTTP requests, on the client
- HTTP responses, on the server
- fs write streams
- zlib streams
- crypto streams
- TCP sockets
- child process stdin
-
process.stdout
,process.stderr
Event: 'drain'
If a stream.write(chunk)
call returns false
, then the 'drain'
event will indicate when it is appropriate to begin writing more data to the stream.
// Write the data to the supplied writable stream one million times. // Be attentive to back-pressure. function writeOneMillionTimes(writer, data, encoding, callback) { var i = 1000000; write(); function write() { var ok = true; do { i -= 1; if (i === 0) { // last time! writer.write(data, encoding, callback); } else { // see if we should continue, or wait // don't pass the callback, because we're not done yet. ok = writer.write(data, encoding); } } while (i > 0 && ok); if (i > 0) { // had to stop early! // write some more once it drains writer.once('drain', write); } } }
Event: 'error'
Emitted if there was an error when writing or piping data.
Event: 'finish'
When the stream.end()
method has been called, and all data has been flushed to the underlying system, this event is emitted.
var writer = getWritableStreamSomehow(); for (var i = 0; i < 100; i ++) { writer.write('hello, #${i}!\n'); } writer.end('this is the end\n'); writer.on('finish', () => { console.error('all writes are now complete.'); });
Event: 'pipe'
-
src
<stream.Readable> source stream that is piping to this writable
This is emitted whenever the stream.pipe()
method is called on a readable stream, adding this writable to its set of destinations.
var writer = getWritableStreamSomehow(); var reader = getReadableStreamSomehow(); writer.on('pipe', (src) => { console.error('something is piping into the writer'); assert.equal(src, reader); }); reader.pipe(writer);
Event: 'unpipe'
-
src
<Readable Stream> The source stream that unpiped this writable
This is emitted whenever the stream.unpipe()
method is called on a readable stream, removing this writable from its set of destinations.
var writer = getWritableStreamSomehow(); var reader = getReadableStreamSomehow(); writer.on('unpipe', (src) => { console.error('something has stopped piping into the writer'); assert.equal(src, reader); }); reader.pipe(writer); reader.unpipe(writer);
writable.cork()
Forces buffering of all writes.
Buffered data will be flushed either at stream.uncork()
or at stream.end()
call.
writable.end([chunk][, encoding][, callback])
-
chunk
<String> | <Buffer> Optional data to write -
encoding
<String> The encoding, ifchunk
is a String -
callback
<Function> Optional callback for when the stream is finished
Call this method when no more data will be written to the stream. If supplied, the callback is attached as a listener on the 'finish'
event.
Calling stream.write()
after calling stream.end()
will raise an error.
// write 'hello, ' and then end with 'world!' var file = fs.createWriteStream('example.txt'); file.write('hello, '); file.end('world!'); // writing more now is not allowed!
writable.setDefaultEncoding(encoding)
-
encoding
<String> The new default encoding
Sets the default encoding for a writable stream.
writable.uncork()
Flush all data, buffered since stream.cork()
call.
writable.write(chunk[, encoding][, callback])
-
chunk
<String> | <Buffer> The data to write -
encoding
<String> The encoding, ifchunk
is a String -
callback
<Function> Callback for when this chunk of data is flushed - Returns: <Boolean>
true
if the data was handled completely.
This method writes some data to the underlying system, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback may or may not be called with the error as its first argument. To detect write errors, listen for the 'error'
event.
The return value indicates if you should continue writing right now. If the data had to be buffered internally, then it will return false
. Otherwise, it will return true
.
This return value is strictly advisory. You MAY continue to write, even if it returns false
. However, writes will be buffered in memory, so it is best not to do this excessively. Instead, wait for the 'drain'
event before writing more data.
Please login to continue.