2017-01-03 22:16:48 +01: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.
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
// A bit simpler than readable streams.
|
2015-10-08 00:27:06 +02:00
|
|
|
// Implement an async ._write(chunk, encoding, cb), and it'll handle all
|
2012-10-03 00:44:50 +02:00
|
|
|
// the drain event emission and buffering.
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2012-11-13 08:30:10 +01:00
|
|
|
module.exports = Writable;
|
|
|
|
Writable.WritableState = WritableState;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const util = require('util');
|
2015-06-13 18:44:39 +02:00
|
|
|
const internalUtil = require('internal/util');
|
2015-01-21 17:36:59 +01:00
|
|
|
const Stream = require('stream');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { Buffer } = require('buffer');
|
2017-05-06 14:20:52 +02:00
|
|
|
const destroyImpl = require('internal/streams/destroy');
|
2018-01-09 20:39:29 +01:00
|
|
|
const { getHighWaterMark } = require('internal/streams/state');
|
2018-03-04 22:16:24 +01:00
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_METHOD_NOT_IMPLEMENTED,
|
2018-03-21 13:01:41 +01:00
|
|
|
ERR_MULTIPLE_CALLBACK,
|
2018-03-04 22:16:24 +01:00
|
|
|
ERR_STREAM_CANNOT_PIPE,
|
|
|
|
ERR_STREAM_DESTROYED,
|
|
|
|
ERR_STREAM_NULL_VALUES,
|
|
|
|
ERR_STREAM_WRITE_AFTER_END,
|
|
|
|
ERR_UNKNOWN_ENCODING
|
|
|
|
} = require('internal/errors').codes;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
util.inherits(Writable, Stream);
|
|
|
|
|
2015-01-23 02:46:43 +01:00
|
|
|
function nop() {}
|
|
|
|
|
2018-03-17 01:35:32 +01:00
|
|
|
function WritableState(options, stream, isDuplex) {
|
2012-10-03 00:44:50 +02:00
|
|
|
options = options || {};
|
2012-11-13 08:33:06 +01:00
|
|
|
|
2017-08-05 01:12:24 +02:00
|
|
|
// Duplex streams are both readable and writable, but share
|
|
|
|
// the same options object.
|
|
|
|
// However, some cases require setting options to different
|
|
|
|
// values for the readable and the writable sides of the duplex stream.
|
|
|
|
// These options can be provided separately as readableXXX and writableXXX.
|
2018-03-17 01:35:32 +01:00
|
|
|
if (typeof isDuplex !== 'boolean')
|
|
|
|
isDuplex = stream instanceof Stream.Duplex;
|
2017-08-05 01:12:24 +02: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;
|
|
|
|
|
2017-08-05 01:12:24 +02:00
|
|
|
if (isDuplex)
|
2014-04-15 18:44:46 +02:00
|
|
|
this.objectMode = this.objectMode || !!options.writableObjectMode;
|
|
|
|
|
2014-07-09 00:06:40 +02:00
|
|
|
// the point at which write() starts returning false
|
|
|
|
// Note: 0 is a valid value, means that we always return false if
|
|
|
|
// the entire buffer is not flushed immediately on write()
|
2018-01-09 20:39:29 +01:00
|
|
|
this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark',
|
|
|
|
isDuplex);
|
2012-11-13 08:33:06 +01:00
|
|
|
|
2017-05-04 15:33:14 +02:00
|
|
|
// if _final has been called
|
|
|
|
this.finalCalled = false;
|
|
|
|
|
2016-09-22 03:43:16 +02:00
|
|
|
// drain event flag.
|
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;
|
2013-03-04 01:12:02 +01:00
|
|
|
// when 'finish' is emitted
|
2012-11-13 08:33:06 +01:00
|
|
|
this.finished = false;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2017-05-06 14:20:52 +02:00
|
|
|
// has it been destroyed
|
|
|
|
this.destroyed = false;
|
|
|
|
|
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
|
|
|
|
2013-05-02 21:25:50 +02:00
|
|
|
// Crypto is kind of old and crusty. Historically, its default string
|
|
|
|
// encoding is 'binary' so we have to make this configurable.
|
|
|
|
// Everything else in the universe uses 'utf8', though.
|
|
|
|
this.defaultEncoding = options.defaultEncoding || 'utf8';
|
|
|
|
|
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
|
|
|
|
2013-04-14 14:15:15 +02:00
|
|
|
// when true all writes will be buffered until .uncork() call
|
2013-05-08 22:29:54 +02:00
|
|
|
this.corked = 0;
|
2013-04-14 14:15:15 +02:00
|
|
|
|
2012-11-13 08:33:06 +01:00
|
|
|
// a flag to be able to tell if the onwrite cb is called immediately,
|
2013-08-13 00:01:05 +02:00
|
|
|
// or on a later tick. We set this to true at first, because any
|
2013-02-23 01:45:22 +01:00
|
|
|
// 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)
|
2016-12-19 03:25:31 +01:00
|
|
|
this.onwrite = onwrite.bind(undefined, stream);
|
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;
|
|
|
|
|
2014-12-04 21:00:23 +01:00
|
|
|
this.bufferedRequest = null;
|
|
|
|
this.lastBufferedRequest = null;
|
2013-05-08 21:54:29 +02:00
|
|
|
|
|
|
|
// number of pending user-supplied write callbacks
|
|
|
|
// this must be 0 before 'finish' can be emitted
|
|
|
|
this.pendingcb = 0;
|
|
|
|
|
|
|
|
// emit prefinish if the only thing we're waiting for is _write cbs
|
|
|
|
// This is relevant for synchronous Transform streams
|
|
|
|
this.prefinished = false;
|
2014-01-22 02:07:11 +01:00
|
|
|
|
2014-02-09 11:59:31 +01:00
|
|
|
// True if the error was already emitted and should not be thrown again
|
2014-02-09 12:09:34 +01:00
|
|
|
this.errorEmitted = false;
|
2015-12-16 09:39:11 +01:00
|
|
|
|
2018-01-29 19:32:34 +01:00
|
|
|
// Should close be emitted on destroy. Defaults to true.
|
|
|
|
this.emitClose = options.emitClose !== false;
|
|
|
|
|
2015-12-16 09:39:11 +01:00
|
|
|
// count buffered requests
|
|
|
|
this.bufferedRequestCount = 0;
|
|
|
|
|
2016-04-12 12:30:03 +02:00
|
|
|
// allocate the first CorkedRequest, there is always
|
|
|
|
// one allocated and free to use, and we maintain at most two
|
2016-12-31 22:32:20 +01:00
|
|
|
var corkReq = { next: null, entry: null, finish: undefined };
|
|
|
|
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, this);
|
|
|
|
this.corkedRequestsFree = corkReq;
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
|
|
|
|
2016-10-16 00:02:43 +02:00
|
|
|
WritableState.prototype.getBuffer = function getBuffer() {
|
2014-12-04 21:00:23 +01:00
|
|
|
var current = this.bufferedRequest;
|
|
|
|
var out = [];
|
|
|
|
while (current) {
|
|
|
|
out.push(current);
|
|
|
|
current = current.next;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperty(WritableState.prototype, 'buffer', {
|
2015-06-13 18:44:39 +02:00
|
|
|
get: internalUtil.deprecate(function() {
|
2014-12-04 21:00:23 +01:00
|
|
|
return this.getBuffer();
|
2015-06-13 18:44:39 +02:00
|
|
|
}, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' +
|
2016-12-04 21:47:01 +01:00
|
|
|
'instead.', 'DEP0003')
|
2014-12-04 21:00:23 +01:00
|
|
|
});
|
|
|
|
|
2016-09-28 19:47:20 +02:00
|
|
|
// Test _writableState for inheritance to account for Duplex streams,
|
|
|
|
// whose prototype chain only points to Readable.
|
|
|
|
var realHasInstance;
|
|
|
|
if (typeof Symbol === 'function' && Symbol.hasInstance) {
|
|
|
|
realHasInstance = Function.prototype[Symbol.hasInstance];
|
|
|
|
Object.defineProperty(Writable, Symbol.hasInstance, {
|
|
|
|
value: function(object) {
|
2016-10-14 00:09:30 +02:00
|
|
|
if (realHasInstance.call(this, object))
|
|
|
|
return true;
|
2017-08-19 18:29:25 +02:00
|
|
|
if (this !== Writable)
|
|
|
|
return false;
|
2016-10-14 00:09:30 +02:00
|
|
|
|
|
|
|
return object && object._writableState instanceof WritableState;
|
2016-09-28 19:47:20 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
realHasInstance = function(object) {
|
|
|
|
return object instanceof this;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
function Writable(options) {
|
2016-09-28 19:47:20 +02:00
|
|
|
// Writable ctor is applied to Duplexes, too.
|
|
|
|
// `realHasInstance` is necessary because using plain `instanceof`
|
|
|
|
// would return false, as no `_writableState` property is attached.
|
2016-10-14 00:09:30 +02:00
|
|
|
|
|
|
|
// Trying to use the custom `instanceof` for Writable here will also break the
|
|
|
|
// Node.js LazyTransform implementation, which has a non-trivial getter for
|
|
|
|
// `_writableState` that would lead to infinite recursion.
|
2018-03-17 01:35:32 +01:00
|
|
|
|
|
|
|
// Checking for a Stream.Duplex instance is faster here instead of inside
|
|
|
|
// the WritableState constructor, at least with V8 6.5
|
|
|
|
const isDuplex = (this instanceof Stream.Duplex);
|
|
|
|
|
|
|
|
if (!isDuplex && !realHasInstance.call(Writable, this))
|
2012-10-08 23:43:17 +02:00
|
|
|
return new Writable(options);
|
|
|
|
|
2018-03-17 01:35:32 +01:00
|
|
|
this._writableState = new WritableState(options, this, isDuplex);
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
// legacy.
|
|
|
|
this.writable = true;
|
|
|
|
|
2015-02-03 02:12:41 +01:00
|
|
|
if (options) {
|
|
|
|
if (typeof options.write === 'function')
|
|
|
|
this._write = options.write;
|
|
|
|
|
|
|
|
if (typeof options.writev === 'function')
|
|
|
|
this._writev = options.writev;
|
2017-05-06 14:20:52 +02:00
|
|
|
|
|
|
|
if (typeof options.destroy === 'function')
|
|
|
|
this._destroy = options.destroy;
|
2017-05-04 15:33:14 +02:00
|
|
|
|
|
|
|
if (typeof options.final === 'function')
|
|
|
|
this._final = options.final;
|
2015-02-03 02:12:41 +01:00
|
|
|
}
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
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() {
|
2018-03-04 22:16:24 +01:00
|
|
|
this.emit('error', new ERR_STREAM_CANNOT_PIPE());
|
2013-02-25 07:14:30 +01:00
|
|
|
};
|
|
|
|
|
2012-10-10 02:31:29 +02:00
|
|
|
|
2015-03-28 04:04:49 +01:00
|
|
|
function writeAfterEnd(stream, cb) {
|
2018-03-04 22:16:24 +01:00
|
|
|
var er = new ERR_STREAM_WRITE_AFTER_END();
|
2013-03-04 01:12:02 +01:00
|
|
|
// TODO: defer error events consistently everywhere, not just the cb
|
|
|
|
stream.emit('error', er);
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(cb, er);
|
2013-03-04 01:12:02 +01:00
|
|
|
}
|
2012-11-13 08:33:06 +01:00
|
|
|
|
2017-01-03 00:37:26 +01:00
|
|
|
// Checks that a user-supplied chunk is valid, especially for the particular
|
|
|
|
// mode the stream is in. Currently this means that `null` is never accepted
|
|
|
|
// and undefined/non-string values are only allowed in object mode.
|
2013-03-04 01:12:02 +01:00
|
|
|
function validChunk(stream, state, chunk, cb) {
|
2018-05-08 19:24:44 +02:00
|
|
|
var er;
|
2017-01-03 00:37:26 +01:00
|
|
|
|
2016-04-12 21:42:23 +02:00
|
|
|
if (chunk === null) {
|
2018-03-04 22:16:24 +01:00
|
|
|
er = new ERR_STREAM_NULL_VALUES();
|
2017-12-13 07:34:51 +01:00
|
|
|
} else if (typeof chunk !== 'string' && !state.objectMode) {
|
2018-03-19 13:33:46 +01:00
|
|
|
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
|
2016-04-12 21:42:23 +02:00
|
|
|
}
|
|
|
|
if (er) {
|
2013-03-04 01:12:02 +01:00
|
|
|
stream.emit('error', er);
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(cb, er);
|
2018-05-13 05:07:52 +02:00
|
|
|
return false;
|
2013-02-23 01:47:27 +01:00
|
|
|
}
|
2018-05-13 05:07:52 +02:00
|
|
|
return true;
|
2013-03-04 01:12:02 +01:00
|
|
|
}
|
2013-01-12 05:59:57 +01:00
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
Writable.prototype.write = function(chunk, encoding, cb) {
|
|
|
|
var state = this._writableState;
|
|
|
|
var ret = false;
|
2017-06-27 19:03:00 +02:00
|
|
|
var isBuf = !state.objectMode && Stream._isUint8Array(chunk);
|
2017-01-09 19:05:06 +01:00
|
|
|
|
|
|
|
if (isBuf && Object.getPrototypeOf(chunk) !== Buffer.prototype) {
|
|
|
|
chunk = Stream._uint8ArrayToBuffer(chunk);
|
|
|
|
}
|
2013-03-04 01:12:02 +01:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof encoding === 'function') {
|
2013-03-04 01:12:02 +01:00
|
|
|
cb = encoding;
|
|
|
|
encoding = null;
|
2012-10-09 20:01:53 +02:00
|
|
|
}
|
2013-05-02 21:25:50 +02:00
|
|
|
|
2017-01-03 00:37:26 +01:00
|
|
|
if (isBuf)
|
2013-05-02 21:25:50 +02:00
|
|
|
encoding = 'buffer';
|
|
|
|
else if (!encoding)
|
|
|
|
encoding = state.defaultEncoding;
|
2013-03-04 01:12:02 +01:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof cb !== 'function')
|
2015-01-23 02:46:43 +01:00
|
|
|
cb = nop;
|
2013-03-04 01:12:02 +01:00
|
|
|
|
2018-01-16 05:17:58 +01:00
|
|
|
if (state.ending)
|
2015-03-28 04:04:49 +01:00
|
|
|
writeAfterEnd(this, cb);
|
2017-01-03 00:37:26 +01:00
|
|
|
else if (isBuf || validChunk(this, state, chunk, cb)) {
|
2013-05-08 21:54:29 +02:00
|
|
|
state.pendingcb++;
|
2017-01-03 00:37:26 +01:00
|
|
|
ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);
|
2013-05-08 21:54:29 +02:00
|
|
|
}
|
2013-03-04 01:12:02 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2013-04-14 14:15:15 +02:00
|
|
|
Writable.prototype.cork = function() {
|
2018-05-13 05:25:21 +02:00
|
|
|
this._writableState.corked++;
|
2013-04-14 14:15:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Writable.prototype.uncork = function() {
|
|
|
|
var state = this._writableState;
|
|
|
|
|
|
|
|
if (state.corked) {
|
2013-05-08 22:29:54 +02:00
|
|
|
state.corked--;
|
2013-04-14 14:15:15 +02:00
|
|
|
|
|
|
|
if (!state.writing &&
|
2013-05-08 22:29:54 +02:00
|
|
|
!state.corked &&
|
2013-04-14 14:15:15 +02:00
|
|
|
!state.bufferProcessing &&
|
2014-12-04 21:00:23 +01:00
|
|
|
state.bufferedRequest)
|
2013-04-14 14:15:15 +02:00
|
|
|
clearBuffer(this, state);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-08 01:32:05 +02:00
|
|
|
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
|
|
|
|
// node::ParseEncoding() requires lower case.
|
2014-10-09 22:07:50 +02:00
|
|
|
if (typeof encoding === 'string')
|
|
|
|
encoding = encoding.toLowerCase();
|
2014-10-08 01:32:05 +02:00
|
|
|
if (!Buffer.isEncoding(encoding))
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
2014-10-08 01:32:05 +02:00
|
|
|
this._writableState.defaultEncoding = encoding;
|
2016-04-30 18:55:46 +02:00
|
|
|
return this;
|
2014-10-08 01:32:05 +02:00
|
|
|
};
|
|
|
|
|
2017-05-16 17:08:49 +02:00
|
|
|
Object.defineProperty(Writable.prototype, 'writableBuffer', {
|
|
|
|
// making it explicit this property is not enumerable
|
|
|
|
// because otherwise some prototype manipulation in
|
|
|
|
// userland will fail
|
|
|
|
enumerable: false,
|
|
|
|
get: function() {
|
|
|
|
return this._writableState && this._writableState.getBuffer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-03-04 04:14:06 +01:00
|
|
|
function decodeChunk(state, chunk, encoding) {
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!state.objectMode &&
|
|
|
|
state.decodeStrings !== false &&
|
2015-01-29 02:05:53 +01:00
|
|
|
typeof chunk === 'string') {
|
2016-01-26 00:00:06 +01:00
|
|
|
chunk = Buffer.from(chunk, encoding);
|
2013-03-04 04:14:06 +01:00
|
|
|
}
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
2017-05-05 15:57:57 +02:00
|
|
|
Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
|
|
|
|
// making it explicit this property is not enumerable
|
|
|
|
// because otherwise some prototype manipulation in
|
|
|
|
// userland will fail
|
|
|
|
enumerable: false,
|
|
|
|
get: function() {
|
|
|
|
return this._writableState.highWaterMark;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
// if we're already writing something, then just put this
|
|
|
|
// in the queue, and wait our turn. Otherwise, call _write
|
|
|
|
// If we return false, then we need a drain event, so set that flag.
|
2017-01-03 00:37:26 +01:00
|
|
|
function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
|
|
|
|
if (!isBuf) {
|
2016-12-31 22:33:36 +01:00
|
|
|
var newChunk = decodeChunk(state, chunk, encoding);
|
|
|
|
if (chunk !== newChunk) {
|
2017-05-24 05:42:41 +02:00
|
|
|
isBuf = true;
|
2017-01-03 00:37:26 +01:00
|
|
|
encoding = 'buffer';
|
2016-12-31 22:33:36 +01:00
|
|
|
chunk = newChunk;
|
|
|
|
}
|
2017-01-03 00:37:26 +01:00
|
|
|
}
|
2013-03-04 01:12:02 +01:00
|
|
|
var len = state.objectMode ? 1 : chunk.length;
|
|
|
|
|
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;
|
2014-01-03 12:37:16 +01:00
|
|
|
// we must ensure that previous needDrain will not be reset to false.
|
|
|
|
if (!ret)
|
|
|
|
state.needDrain = true;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2014-12-04 21:00:23 +01:00
|
|
|
if (state.writing || state.corked) {
|
|
|
|
var last = state.lastBufferedRequest;
|
2017-05-24 05:42:41 +02:00
|
|
|
state.lastBufferedRequest = {
|
|
|
|
chunk,
|
|
|
|
encoding,
|
|
|
|
isBuf,
|
|
|
|
callback: cb,
|
|
|
|
next: null
|
|
|
|
};
|
2014-12-04 21:00:23 +01:00
|
|
|
if (last) {
|
|
|
|
last.next = state.lastBufferedRequest;
|
|
|
|
} else {
|
|
|
|
state.bufferedRequest = state.lastBufferedRequest;
|
|
|
|
}
|
2015-12-16 09:39:11 +01:00
|
|
|
state.bufferedRequestCount += 1;
|
2015-04-28 19:46:14 +02:00
|
|
|
} else {
|
2013-04-14 14:15:15 +02:00
|
|
|
doWrite(stream, state, false, len, chunk, encoding, cb);
|
2015-04-28 19:46:14 +02:00
|
|
|
}
|
2013-03-04 01:12:02 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-14 14:15:15 +02:00
|
|
|
function doWrite(stream, state, writev, len, chunk, encoding, cb) {
|
2013-01-12 05:59:57 +01:00
|
|
|
state.writelen = len;
|
2012-11-13 08:33:06 +01:00
|
|
|
state.writecb = cb;
|
2013-03-04 01:12:02 +01:00
|
|
|
state.writing = true;
|
|
|
|
state.sync = true;
|
2018-01-29 19:32:34 +01:00
|
|
|
if (state.destroyed)
|
2018-03-04 22:16:24 +01:00
|
|
|
state.onwrite(new ERR_STREAM_DESTROYED('write'));
|
2018-01-29 19:32:34 +01:00
|
|
|
else if (writev)
|
2013-04-14 14:15:15 +02:00
|
|
|
stream._writev(chunk, state.onwrite);
|
|
|
|
else
|
|
|
|
stream._write(chunk, encoding, state.onwrite);
|
2012-11-13 08:33:06 +01:00
|
|
|
state.sync = false;
|
2013-03-04 01:12:02 +01:00
|
|
|
}
|
2012-10-09 20:01:53 +02:00
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
function onwriteError(stream, state, sync, er, cb) {
|
2015-05-04 20:40:25 +02:00
|
|
|
--state.pendingcb;
|
2013-03-04 01:12:02 +01:00
|
|
|
|
2017-06-21 20:04:12 +02:00
|
|
|
if (sync) {
|
|
|
|
// defer the callback if we are being called synchronously
|
|
|
|
// to avoid piling up things on the stack
|
|
|
|
process.nextTick(cb, er);
|
|
|
|
// this can emit finish, and it will always happen
|
|
|
|
// after error
|
|
|
|
process.nextTick(finishMaybe, stream, state);
|
|
|
|
stream._writableState.errorEmitted = true;
|
|
|
|
stream.emit('error', er);
|
|
|
|
} else {
|
|
|
|
// the caller expect this to happen before if
|
|
|
|
// it is async
|
|
|
|
cb(er);
|
|
|
|
stream._writableState.errorEmitted = true;
|
|
|
|
stream.emit('error', er);
|
|
|
|
// this can emit finish, but finish must
|
|
|
|
// always follow error
|
|
|
|
finishMaybe(stream, state);
|
|
|
|
}
|
2017-05-24 13:24:19 +02:00
|
|
|
}
|
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
function onwriteStateUpdate(state) {
|
|
|
|
state.writing = false;
|
|
|
|
state.writecb = null;
|
|
|
|
state.length -= state.writelen;
|
|
|
|
state.writelen = 0;
|
|
|
|
}
|
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;
|
|
|
|
|
2018-03-21 13:01:41 +01:00
|
|
|
if (typeof cb !== 'function')
|
|
|
|
throw new ERR_MULTIPLE_CALLBACK();
|
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
onwriteStateUpdate(state);
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
if (er)
|
|
|
|
onwriteError(stream, state, sync, er, cb);
|
|
|
|
else {
|
2013-04-08 20:00:26 +02:00
|
|
|
// Check if we're actually ready to finish, but don't emit yet
|
2015-03-28 04:04:49 +01:00
|
|
|
var finished = needFinish(state);
|
2012-12-14 19:49:16 +01:00
|
|
|
|
2013-04-14 14:15:15 +02:00
|
|
|
if (!finished &&
|
|
|
|
!state.corked &&
|
|
|
|
!state.bufferProcessing &&
|
2014-12-04 21:00:23 +01:00
|
|
|
state.bufferedRequest) {
|
2013-03-06 02:53:37 +01:00
|
|
|
clearBuffer(stream, state);
|
2013-04-14 14:15:15 +02:00
|
|
|
}
|
2012-11-13 08:33:06 +01:00
|
|
|
|
2013-03-06 02:53:37 +01:00
|
|
|
if (sync) {
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(afterWrite, stream, state, finished, cb);
|
2013-03-06 02:53:37 +01:00
|
|
|
} else {
|
|
|
|
afterWrite(stream, state, finished, cb);
|
|
|
|
}
|
2012-10-09 20:01:53 +02:00
|
|
|
}
|
2013-03-04 01:12:02 +01:00
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2013-03-06 02:53:37 +01:00
|
|
|
function afterWrite(stream, state, finished, cb) {
|
|
|
|
if (!finished)
|
|
|
|
onwriteDrain(stream, state);
|
2013-05-08 21:54:29 +02:00
|
|
|
state.pendingcb--;
|
2013-03-06 02:53:37 +01:00
|
|
|
cb();
|
2013-05-08 21:54:29 +02:00
|
|
|
finishMaybe(stream, state);
|
2013-03-06 02:53:37 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 01:12:02 +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.
|
|
|
|
function onwriteDrain(stream, state) {
|
2013-03-06 02:53:37 +01:00
|
|
|
if (state.length === 0 && state.needDrain) {
|
|
|
|
state.needDrain = false;
|
|
|
|
stream.emit('drain');
|
|
|
|
}
|
2013-03-04 01:12:02 +01:00
|
|
|
}
|
2012-12-19 18:04:08 +01:00
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
// if there's something in the buffer waiting, then process it
|
|
|
|
function clearBuffer(stream, state) {
|
|
|
|
state.bufferProcessing = true;
|
2014-12-04 21:00:23 +01:00
|
|
|
var entry = state.bufferedRequest;
|
2013-03-04 01:12:02 +01:00
|
|
|
|
2014-12-04 21:00:23 +01:00
|
|
|
if (stream._writev && entry && entry.next) {
|
2013-04-14 14:15:15 +02:00
|
|
|
// Fast case, write everything using _writev()
|
2015-12-16 09:39:11 +01:00
|
|
|
var l = state.bufferedRequestCount;
|
|
|
|
var buffer = new Array(l);
|
|
|
|
var holder = state.corkedRequestsFree;
|
|
|
|
holder.entry = entry;
|
|
|
|
|
|
|
|
var count = 0;
|
2017-05-24 05:42:41 +02:00
|
|
|
var allBuffers = true;
|
2014-12-04 21:00:23 +01:00
|
|
|
while (entry) {
|
2015-12-16 09:39:11 +01:00
|
|
|
buffer[count] = entry;
|
2017-05-24 05:42:41 +02:00
|
|
|
if (!entry.isBuf)
|
|
|
|
allBuffers = false;
|
2014-12-04 21:00:23 +01:00
|
|
|
entry = entry.next;
|
2015-12-16 09:39:11 +01:00
|
|
|
count += 1;
|
2014-12-04 21:00:23 +01:00
|
|
|
}
|
2017-05-24 05:42:41 +02:00
|
|
|
buffer.allBuffers = allBuffers;
|
2013-04-14 14:15:15 +02:00
|
|
|
|
2015-12-16 09:39:11 +01:00
|
|
|
doWrite(stream, state, true, state.length, buffer, '', holder.finish);
|
|
|
|
|
2016-04-12 12:30:03 +02:00
|
|
|
// doWrite is almost always async, defer these to save a bit of time
|
2015-12-16 09:39:11 +01:00
|
|
|
// as the hot path ends with doWrite
|
2013-05-08 21:54:29 +02:00
|
|
|
state.pendingcb++;
|
2014-12-04 21:00:23 +01:00
|
|
|
state.lastBufferedRequest = null;
|
2016-04-12 12:30:03 +02:00
|
|
|
if (holder.next) {
|
|
|
|
state.corkedRequestsFree = holder.next;
|
|
|
|
holder.next = null;
|
|
|
|
} else {
|
2016-12-31 22:32:20 +01:00
|
|
|
var corkReq = { next: null, entry: null, finish: undefined };
|
|
|
|
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, state);
|
|
|
|
state.corkedRequestsFree = corkReq;
|
2016-04-12 12:30:03 +02:00
|
|
|
}
|
2017-09-28 20:21:58 +02:00
|
|
|
state.bufferedRequestCount = 0;
|
2013-04-14 14:15:15 +02:00
|
|
|
} else {
|
|
|
|
// Slow case, write chunks one-by-one
|
2014-12-04 21:00:23 +01:00
|
|
|
while (entry) {
|
2013-04-14 14:15:15 +02:00
|
|
|
var chunk = entry.chunk;
|
|
|
|
var encoding = entry.encoding;
|
|
|
|
var cb = entry.callback;
|
|
|
|
var len = state.objectMode ? 1 : chunk.length;
|
|
|
|
|
|
|
|
doWrite(stream, state, false, len, chunk, encoding, cb);
|
2014-12-04 21:00:23 +01:00
|
|
|
entry = entry.next;
|
2017-09-28 20:21:58 +02:00
|
|
|
state.bufferedRequestCount--;
|
2013-04-14 14:15:15 +02:00
|
|
|
// 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) {
|
|
|
|
break;
|
|
|
|
}
|
2013-03-04 01:12:02 +01:00
|
|
|
}
|
2013-04-14 14:15:15 +02:00
|
|
|
|
2014-12-04 21:00:23 +01:00
|
|
|
if (entry === null)
|
|
|
|
state.lastBufferedRequest = null;
|
2012-12-19 18:04:08 +01:00
|
|
|
}
|
2015-12-16 09:39:11 +01:00
|
|
|
|
2014-12-04 21:00:23 +01:00
|
|
|
state.bufferedRequest = entry;
|
2013-03-04 01:12:02 +01:00
|
|
|
state.bufferProcessing = false;
|
2012-11-13 08:33:06 +01:00
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2013-03-04 04:14:06 +01:00
|
|
|
Writable.prototype._write = function(chunk, encoding, cb) {
|
2018-02-16 12:53:34 +01:00
|
|
|
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
2013-04-14 14:15:15 +02:00
|
|
|
Writable.prototype._writev = null;
|
|
|
|
|
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
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof chunk === 'function') {
|
2013-02-05 23:58:16 +01:00
|
|
|
cb = chunk;
|
|
|
|
chunk = null;
|
|
|
|
encoding = null;
|
2015-01-29 02:05:53 +01:00
|
|
|
} else if (typeof encoding === 'function') {
|
2013-02-05 23:58:16 +01:00
|
|
|
cb = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (chunk !== null && chunk !== undefined)
|
2013-03-02 21:25:16 +01:00
|
|
|
this.write(chunk, encoding);
|
|
|
|
|
2013-05-08 22:29:54 +02:00
|
|
|
// .end() fully uncorks
|
|
|
|
if (state.corked) {
|
|
|
|
state.corked = 1;
|
|
|
|
this.uncork();
|
|
|
|
}
|
2013-04-14 14:15:15 +02:00
|
|
|
|
2013-03-02 21:25:16 +01:00
|
|
|
// ignore unnecessary end() calls.
|
2018-01-14 16:34:18 +01:00
|
|
|
if (!state.ending)
|
2013-03-02 21:25:16 +01:00
|
|
|
endWritable(this, state, cb);
|
2018-02-14 13:29:17 +01:00
|
|
|
|
|
|
|
return this;
|
2013-03-02 21:25:16 +01:00
|
|
|
};
|
|
|
|
|
2017-05-05 14:42:21 +02:00
|
|
|
Object.defineProperty(Writable.prototype, 'writableLength', {
|
|
|
|
// making it explicit this property is not enumerable
|
|
|
|
// because otherwise some prototype manipulation in
|
|
|
|
// userland will fail
|
|
|
|
enumerable: false,
|
|
|
|
get() {
|
|
|
|
return this._writableState.length;
|
|
|
|
}
|
|
|
|
});
|
2013-04-08 20:00:26 +02:00
|
|
|
|
2015-03-28 04:04:49 +01:00
|
|
|
function needFinish(state) {
|
2013-04-08 20:00:26 +02:00
|
|
|
return (state.ending &&
|
|
|
|
state.length === 0 &&
|
2014-12-04 21:00:23 +01:00
|
|
|
state.bufferedRequest === null &&
|
2013-04-08 20:00:26 +02:00
|
|
|
!state.finished &&
|
|
|
|
!state.writing);
|
|
|
|
}
|
2017-05-04 15:33:14 +02:00
|
|
|
function callFinal(stream, state) {
|
|
|
|
stream._final((err) => {
|
|
|
|
state.pendingcb--;
|
|
|
|
if (err) {
|
|
|
|
stream.emit('error', err);
|
|
|
|
}
|
2013-05-08 21:54:29 +02:00
|
|
|
state.prefinished = true;
|
|
|
|
stream.emit('prefinish');
|
2017-05-04 15:33:14 +02:00
|
|
|
finishMaybe(stream, state);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function prefinish(stream, state) {
|
|
|
|
if (!state.prefinished && !state.finalCalled) {
|
2018-01-29 19:32:34 +01:00
|
|
|
if (typeof stream._final === 'function' && !state.destroyed) {
|
2017-05-04 15:33:14 +02:00
|
|
|
state.pendingcb++;
|
|
|
|
state.finalCalled = true;
|
|
|
|
process.nextTick(callFinal, stream, state);
|
|
|
|
} else {
|
|
|
|
state.prefinished = true;
|
|
|
|
stream.emit('prefinish');
|
|
|
|
}
|
2013-05-08 21:54:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 01:12:02 +01:00
|
|
|
function finishMaybe(stream, state) {
|
2015-03-28 04:04:49 +01:00
|
|
|
var need = needFinish(state);
|
2013-04-08 20:00:26 +02:00
|
|
|
if (need) {
|
2017-05-04 15:33:14 +02:00
|
|
|
prefinish(stream, state);
|
2013-05-08 21:54:29 +02:00
|
|
|
if (state.pendingcb === 0) {
|
|
|
|
state.finished = true;
|
|
|
|
stream.emit('finish');
|
2015-04-28 19:46:14 +02:00
|
|
|
}
|
2012-11-13 08:33:06 +01:00
|
|
|
}
|
2013-04-08 20:00:26 +02:00
|
|
|
return need;
|
2013-03-04 01:12:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function endWritable(stream, state, cb) {
|
|
|
|
state.ending = true;
|
|
|
|
finishMaybe(stream, state);
|
2013-03-02 21:25:16 +01:00
|
|
|
if (cb) {
|
2013-03-04 01:12:02 +01:00
|
|
|
if (state.finished)
|
2013-03-02 21:25:16 +01:00
|
|
|
process.nextTick(cb);
|
|
|
|
else
|
|
|
|
stream.once('finish', cb);
|
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
state.ended = true;
|
2015-03-20 03:04:07 +01:00
|
|
|
stream.writable = false;
|
2013-03-02 21:25:16 +01:00
|
|
|
}
|
2015-12-16 09:39:11 +01:00
|
|
|
|
2016-12-19 03:25:31 +01:00
|
|
|
function onCorkedFinish(corkReq, state, err) {
|
|
|
|
var entry = corkReq.entry;
|
|
|
|
corkReq.entry = null;
|
|
|
|
while (entry) {
|
|
|
|
var cb = entry.callback;
|
|
|
|
state.pendingcb--;
|
|
|
|
cb(err);
|
|
|
|
entry = entry.next;
|
|
|
|
}
|
2018-01-15 07:45:11 +01:00
|
|
|
|
|
|
|
// reuse the free corkReq.
|
|
|
|
state.corkedRequestsFree.next = corkReq;
|
2015-12-16 09:39:11 +01:00
|
|
|
}
|
2017-05-06 14:20:52 +02:00
|
|
|
|
|
|
|
Object.defineProperty(Writable.prototype, 'destroyed', {
|
2017-05-05 14:42:21 +02:00
|
|
|
// making it explicit this property is not enumerable
|
|
|
|
// because otherwise some prototype manipulation in
|
|
|
|
// userland will fail
|
|
|
|
enumerable: false,
|
2017-05-06 14:20:52 +02:00
|
|
|
get() {
|
|
|
|
if (this._writableState === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this._writableState.destroyed;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
// we ignore the value if the stream
|
|
|
|
// has not been initialized yet
|
|
|
|
if (!this._writableState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// backward compatibility, the user is explicitly
|
|
|
|
// managing destroyed
|
|
|
|
this._writableState.destroyed = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Writable.prototype.destroy = destroyImpl.destroy;
|
|
|
|
Writable.prototype._undestroy = destroyImpl.undestroy;
|
|
|
|
Writable.prototype._destroy = function(err, cb) {
|
|
|
|
cb(err);
|
|
|
|
};
|