2012-10-03 00:44:50 +02:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
// A bit simpler than readable streams.
|
|
|
|
// Implement an async ._write(chunk, cb), and it'll handle all
|
|
|
|
// the drain event emission and buffering.
|
|
|
|
|
2012-11-13 08:30:10 +01:00
|
|
|
module.exports = Writable;
|
|
|
|
Writable.WritableState = WritableState;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
var util = require('util');
|
2012-11-13 08:33:06 +01:00
|
|
|
var assert = require('assert');
|
2012-10-03 00:44:50 +02:00
|
|
|
var Stream = require('stream');
|
|
|
|
|
|
|
|
util.inherits(Writable, Stream);
|
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
function WritableState(options, stream) {
|
2012-10-03 00:44:50 +02:00
|
|
|
options = options || {};
|
2012-11-13 08:33:06 +01:00
|
|
|
|
|
|
|
// the point at which write() starts returning false
|
2012-12-19 03:49:42 +01:00
|
|
|
// Note: 0 is a valid value, means that we always return false if
|
|
|
|
// the entire buffer is not flushed immediately on write()
|
|
|
|
var hwm = options.highWaterMark;
|
|
|
|
this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024;
|
2012-11-13 08:33:06 +01:00
|
|
|
|
2013-01-12 05:59:57 +01:00
|
|
|
// object stream flag to indicate whether or not this stream
|
|
|
|
// contains buffers or objects.
|
|
|
|
this.objectMode = !!options.objectMode;
|
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
// cast to ints.
|
|
|
|
this.highWaterMark = ~~this.highWaterMark;
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
this.needDrain = false;
|
2012-11-13 08:33:06 +01:00
|
|
|
// at the start of calling end()
|
2012-10-03 00:44:50 +02:00
|
|
|
this.ending = false;
|
2012-11-13 08:33:06 +01:00
|
|
|
// when end() has been called, and returned
|
|
|
|
this.ended = false;
|
|
|
|
// when 'finish' has emitted
|
|
|
|
this.finished = false;
|
|
|
|
// when 'finish' is being emitted
|
|
|
|
this.finishing = false;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-10-09 20:01:53 +02:00
|
|
|
// should we decode strings into buffers before passing to _write?
|
|
|
|
// this is here so that some node-core streams can optimize string
|
|
|
|
// handling at a lower level.
|
2012-12-19 03:49:42 +01:00
|
|
|
var noDecode = options.decodeStrings === false;
|
|
|
|
this.decodeStrings = !noDecode;
|
2012-10-09 20:01:53 +02:00
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
// not an actual buffer we keep track of, but a measurement
|
|
|
|
// of how much we're waiting to get pushed to some underlying
|
|
|
|
// socket or file.
|
|
|
|
this.length = 0;
|
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
// a flag to see when we're in the middle of a write.
|
2012-10-03 00:44:50 +02:00
|
|
|
this.writing = false;
|
2012-11-13 08:33:06 +01:00
|
|
|
|
|
|
|
// a flag to be able to tell if the onwrite cb is called immediately,
|
2013-02-23 01:45:22 +01:00
|
|
|
// or on a later tick. We set this to true at first, becuase any
|
|
|
|
// actions that shouldn't happen until "later" should generally also
|
|
|
|
// not happen before the first write call.
|
|
|
|
this.sync = true;
|
2012-11-13 08:33:06 +01:00
|
|
|
|
2012-12-19 17:57:05 +01:00
|
|
|
// a flag to know if we're processing previously buffered items, which
|
|
|
|
// may call the _write() callback in the same tick, so that we don't
|
|
|
|
// end up in an overlapped onwrite situation.
|
|
|
|
this.bufferProcessing = false;
|
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
// the callback that's passed to _write(chunk,cb)
|
2012-11-17 05:24:14 +01:00
|
|
|
this.onwrite = function(er) {
|
|
|
|
onwrite(stream, er);
|
|
|
|
};
|
2012-11-13 08:33:06 +01:00
|
|
|
|
|
|
|
// the callback that the user supplies to write(chunk,encoding,cb)
|
|
|
|
this.writecb = null;
|
|
|
|
|
|
|
|
// the amount that is being written when _write is called.
|
|
|
|
this.writelen = 0;
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
this.buffer = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function Writable(options) {
|
2012-10-08 23:43:17 +02:00
|
|
|
// Writable ctor is applied to Duplexes, though they're not
|
|
|
|
// instanceof Writable, they're instanceof Readable.
|
2012-10-10 02:42:47 +02:00
|
|
|
if (!(this instanceof Writable) && !(this instanceof Stream.Duplex))
|
2012-10-08 23:43:17 +02:00
|
|
|
return new Writable(options);
|
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
this._writableState = new WritableState(options, this);
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
// legacy.
|
|
|
|
this.writable = true;
|
|
|
|
|
|
|
|
Stream.call(this);
|
|
|
|
}
|
|
|
|
|
2013-02-25 07:14:30 +01:00
|
|
|
// Otherwise people can pipe Writable streams, which is just wrong.
|
|
|
|
Writable.prototype.pipe = function() {
|
|
|
|
this.emit('error', new Error('Cannot pipe. Not readable.'));
|
|
|
|
};
|
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
// Override this method or _write(chunk, cb)
|
2012-10-10 02:31:29 +02:00
|
|
|
Writable.prototype.write = function(chunk, encoding, cb) {
|
2012-10-03 00:44:50 +02:00
|
|
|
var state = this._writableState;
|
|
|
|
|
2012-10-10 02:31:29 +02:00
|
|
|
if (typeof encoding === 'function') {
|
|
|
|
cb = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
if (state.ended) {
|
2013-03-02 19:20:33 +01:00
|
|
|
var self = this;
|
2012-11-13 08:33:06 +01:00
|
|
|
var er = new Error('write after end');
|
2013-03-02 19:20:33 +01:00
|
|
|
// TODO: defer error events consistently everywhere, not just the cb
|
|
|
|
self.emit('error', er);
|
|
|
|
if (typeof cb === 'function') {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(er);
|
|
|
|
});
|
|
|
|
}
|
2012-11-13 08:33:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-23 01:47:27 +01:00
|
|
|
// If we get something that is not a buffer, string, null, or undefined,
|
|
|
|
// and we're not in objectMode, then that's an error.
|
|
|
|
// Otherwise stream chunks are all considered to be of length=1, and the
|
|
|
|
// watermarks determine how many objects to keep in the buffer, rather than
|
|
|
|
// how many bytes or characters.
|
|
|
|
if (!Buffer.isBuffer(chunk) &&
|
|
|
|
'string' !== typeof chunk &&
|
2013-01-12 05:59:57 +01:00
|
|
|
chunk !== null &&
|
|
|
|
chunk !== undefined &&
|
2013-02-23 01:47:27 +01:00
|
|
|
!state.objectMode) {
|
|
|
|
var er = new TypeError('Invalid non-string/buffer chunk');
|
|
|
|
if (typeof cb === 'function')
|
|
|
|
cb(er);
|
|
|
|
this.emit('error', er);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-12 05:59:57 +01:00
|
|
|
|
|
|
|
var len;
|
|
|
|
if (state.objectMode)
|
|
|
|
len = 1;
|
|
|
|
else {
|
|
|
|
len = chunk.length;
|
|
|
|
if (false === state.decodeStrings)
|
|
|
|
chunk = [chunk, encoding || 'utf8'];
|
2013-02-07 17:50:18 +01:00
|
|
|
else if (typeof chunk === 'string') {
|
|
|
|
chunk = new Buffer(chunk, encoding);
|
2013-01-12 05:59:57 +01:00
|
|
|
len = chunk.length;
|
|
|
|
}
|
2012-10-09 20:01:53 +02:00
|
|
|
}
|
|
|
|
|
2013-01-12 05:59:57 +01:00
|
|
|
state.length += len;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-10-05 16:45:03 +02:00
|
|
|
var ret = state.length < state.highWaterMark;
|
|
|
|
if (ret === false)
|
|
|
|
state.needDrain = true;
|
|
|
|
|
2012-10-10 02:31:29 +02:00
|
|
|
// if we're already writing something, then just put this
|
|
|
|
// in the queue, and wait our turn.
|
2012-10-03 00:44:50 +02:00
|
|
|
if (state.writing) {
|
2012-10-10 02:31:29 +02:00
|
|
|
state.buffer.push([chunk, cb]);
|
2012-10-03 00:44:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.writing = true;
|
2012-11-13 08:33:06 +01:00
|
|
|
state.sync = true;
|
2013-01-12 05:59:57 +01:00
|
|
|
state.writelen = len;
|
2012-11-13 08:33:06 +01:00
|
|
|
state.writecb = cb;
|
|
|
|
this._write(chunk, state.onwrite);
|
|
|
|
state.sync = false;
|
2012-10-09 20:01:53 +02:00
|
|
|
|
|
|
|
return ret;
|
2012-11-13 08:33:06 +01:00
|
|
|
};
|
2012-10-09 20:01:53 +02:00
|
|
|
|
2012-11-17 05:24:14 +01:00
|
|
|
function onwrite(stream, er) {
|
|
|
|
var state = stream._writableState;
|
2012-11-13 08:33:06 +01:00
|
|
|
var sync = state.sync;
|
|
|
|
var cb = state.writecb;
|
2013-01-12 05:59:57 +01:00
|
|
|
var len = state.writelen;
|
2012-11-13 08:33:06 +01:00
|
|
|
|
|
|
|
state.writing = false;
|
|
|
|
state.writelen = null;
|
|
|
|
state.writecb = null;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
if (er) {
|
2012-10-10 02:31:29 +02:00
|
|
|
if (cb) {
|
2012-12-16 21:47:01 +01:00
|
|
|
// If _write(chunk,cb) calls cb() in this tick, we still defer
|
|
|
|
// the *user's* write callback to the next tick.
|
|
|
|
// Never present an external API that is *sometimes* async!
|
2012-11-13 08:33:06 +01:00
|
|
|
if (sync)
|
2012-11-17 05:24:14 +01:00
|
|
|
process.nextTick(function() {
|
|
|
|
cb(er);
|
|
|
|
});
|
2012-10-10 02:31:29 +02:00
|
|
|
else
|
2012-11-13 08:33:06 +01:00
|
|
|
cb(er);
|
2012-12-14 19:49:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// backwards compatibility. still emit if there was a cb.
|
|
|
|
stream.emit('error', er);
|
2012-11-13 08:33:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-01-12 05:59:57 +01:00
|
|
|
state.length -= len;
|
2012-11-13 08:33:06 +01:00
|
|
|
|
|
|
|
if (cb) {
|
2012-12-16 21:47:01 +01:00
|
|
|
// Don't call the cb until the next tick if we're in sync mode.
|
2012-12-19 17:57:05 +01:00
|
|
|
if (sync)
|
2012-11-13 08:33:06 +01:00
|
|
|
process.nextTick(cb);
|
|
|
|
else
|
|
|
|
cb();
|
2012-10-09 20:01:53 +02:00
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-12-12 09:59:54 +01:00
|
|
|
if (state.length === 0 && (state.ended || state.ending) &&
|
|
|
|
!state.finished && !state.finishing) {
|
2012-11-13 08:33:06 +01:00
|
|
|
// emit 'finish' at the very end.
|
|
|
|
state.finishing = true;
|
2012-11-17 05:24:14 +01:00
|
|
|
stream.emit('finish');
|
2012-11-13 08:33:06 +01:00
|
|
|
state.finished = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-21 19:51:15 +01:00
|
|
|
if (state.length === 0 && state.needDrain) {
|
2012-11-13 08:33:06 +01:00
|
|
|
// Must force callback to be called on nextTick, so that we don't
|
|
|
|
// emit 'drain' before the write() consumer gets the 'false' return
|
|
|
|
// value, and has a chance to attach a 'drain' listener.
|
|
|
|
process.nextTick(function() {
|
|
|
|
if (!state.needDrain)
|
|
|
|
return;
|
|
|
|
state.needDrain = false;
|
2012-11-17 05:24:14 +01:00
|
|
|
stream.emit('drain');
|
|
|
|
});
|
2012-11-13 08:33:06 +01:00
|
|
|
}
|
2012-12-19 18:04:08 +01:00
|
|
|
|
|
|
|
// if there's something in the buffer waiting, then process it
|
|
|
|
// It would be nice if there were TCO in JS, and we could just
|
|
|
|
// shift the top off the buffer and _write that, but that approach
|
|
|
|
// causes RangeErrors when you have a very large number of very
|
|
|
|
// small writes, and is not very efficient otherwise.
|
|
|
|
if (!state.bufferProcessing && state.buffer.length) {
|
|
|
|
state.bufferProcessing = true;
|
|
|
|
|
|
|
|
for (var c = 0; c < state.buffer.length; c++) {
|
|
|
|
var chunkCb = state.buffer[c];
|
|
|
|
var chunk = chunkCb[0];
|
|
|
|
cb = chunkCb[1];
|
|
|
|
|
2013-01-12 05:59:57 +01:00
|
|
|
if (state.objectMode)
|
|
|
|
len = 1;
|
|
|
|
else if (false === state.decodeStrings)
|
|
|
|
len = chunk[0].length;
|
2012-12-19 18:04:08 +01:00
|
|
|
else
|
2013-01-12 05:59:57 +01:00
|
|
|
len = chunk.length;
|
2012-12-19 18:04:08 +01:00
|
|
|
|
2013-01-12 05:59:57 +01:00
|
|
|
state.writelen = len;
|
2012-12-19 18:04:08 +01:00
|
|
|
state.writecb = cb;
|
|
|
|
state.writechunk = chunk;
|
|
|
|
state.writing = true;
|
|
|
|
state.sync = true;
|
|
|
|
stream._write(chunk, state.onwrite);
|
|
|
|
state.sync = false;
|
|
|
|
|
|
|
|
// if we didn't call the onwrite immediately, then
|
|
|
|
// it means that we need to wait until it does.
|
|
|
|
// also, that means that the chunk and cb are currently
|
|
|
|
// being processed, so move the buffer counter past them.
|
|
|
|
if (state.writing) {
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.bufferProcessing = false;
|
|
|
|
if (c < state.buffer.length)
|
|
|
|
state.buffer = state.buffer.slice(c);
|
|
|
|
else
|
|
|
|
state.buffer.length = 0;
|
|
|
|
}
|
2012-11-13 08:33:06 +01:00
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
Writable.prototype._write = function(chunk, cb) {
|
2012-11-17 05:24:14 +01:00
|
|
|
process.nextTick(function() {
|
|
|
|
cb(new Error('not implemented'));
|
|
|
|
});
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
2013-02-05 23:58:16 +01:00
|
|
|
Writable.prototype.end = function(chunk, encoding, cb) {
|
2012-10-03 00:44:50 +02:00
|
|
|
var state = this._writableState;
|
2012-11-13 08:33:06 +01:00
|
|
|
|
2013-03-03 02:21:28 +01:00
|
|
|
// ignore unnecessary end() calls.
|
|
|
|
if (state.ending || state.ended || state.finished)
|
|
|
|
return;
|
|
|
|
|
2013-02-05 23:58:16 +01:00
|
|
|
if (typeof chunk === 'function') {
|
|
|
|
cb = chunk;
|
|
|
|
chunk = null;
|
|
|
|
encoding = null;
|
|
|
|
} else if (typeof encoding === 'function') {
|
|
|
|
cb = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2013-03-02 21:25:16 +01:00
|
|
|
state.ending = true;
|
2013-03-03 02:21:28 +01:00
|
|
|
if (chunk)
|
|
|
|
this.write(chunk, encoding, cb);
|
|
|
|
else if (state.length === 0 && !state.finishing && !state.finished) {
|
2012-11-13 08:33:06 +01:00
|
|
|
state.finishing = true;
|
2013-03-03 02:21:28 +01:00
|
|
|
this.emit('finish');
|
2012-11-13 08:33:06 +01:00
|
|
|
state.finished = true;
|
2013-03-03 02:21:28 +01:00
|
|
|
if (cb) process.nextTick(cb);
|
|
|
|
} else if (cb) {
|
|
|
|
this.once('finish', cb);
|
2012-11-13 08:33:06 +01:00
|
|
|
}
|
2013-03-03 02:21:28 +01:00
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
state.ended = true;
|
|
|
|
};
|