2020-09-16 19:05:06 +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, encoding, cb), and it'll handle all
|
|
|
|
// the drain event emission and buffering.
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
2020-11-16 12:52:30 +01:00
|
|
|
ArrayPrototypeSlice,
|
2021-01-06 19:45:41 +01:00
|
|
|
Error,
|
2020-11-16 12:52:30 +01:00
|
|
|
FunctionPrototypeSymbolHasInstance,
|
2020-09-16 19:05:06 +02:00
|
|
|
ObjectDefineProperties,
|
2024-04-21 18:53:08 +02:00
|
|
|
ObjectDefineProperty,
|
2020-09-16 19:05:06 +02:00
|
|
|
ObjectSetPrototypeOf,
|
2020-11-16 12:52:30 +01:00
|
|
|
StringPrototypeToLowerCase,
|
2020-09-16 19:05:06 +02:00
|
|
|
Symbol,
|
|
|
|
SymbolHasInstance,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
module.exports = Writable;
|
|
|
|
Writable.WritableState = WritableState;
|
|
|
|
|
|
|
|
const EE = require('events');
|
2020-09-18 15:38:57 +02:00
|
|
|
const Stream = require('internal/streams/legacy').Stream;
|
2020-09-16 19:05:06 +02:00
|
|
|
const { Buffer } = require('buffer');
|
|
|
|
const destroyImpl = require('internal/streams/destroy');
|
2020-12-07 18:42:46 +02:00
|
|
|
|
|
|
|
const {
|
2021-07-10 21:28:47 +02:00
|
|
|
addAbortSignal,
|
2020-12-07 18:42:46 +02:00
|
|
|
} = require('internal/streams/add-abort-signal');
|
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
const {
|
|
|
|
getHighWaterMark,
|
2023-02-18 18:53:57 +01:00
|
|
|
getDefaultHighWaterMark,
|
2020-09-16 19:05:06 +02:00
|
|
|
} = require('internal/streams/state');
|
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_METHOD_NOT_IMPLEMENTED,
|
|
|
|
ERR_MULTIPLE_CALLBACK,
|
|
|
|
ERR_STREAM_CANNOT_PIPE,
|
|
|
|
ERR_STREAM_DESTROYED,
|
|
|
|
ERR_STREAM_ALREADY_FINISHED,
|
|
|
|
ERR_STREAM_NULL_VALUES,
|
|
|
|
ERR_STREAM_WRITE_AFTER_END,
|
2023-02-18 18:53:57 +01:00
|
|
|
ERR_UNKNOWN_ENCODING,
|
2020-09-16 19:05:06 +02:00
|
|
|
} = require('internal/errors').codes;
|
2023-10-28 15:55:34 +02:00
|
|
|
const {
|
|
|
|
kState,
|
|
|
|
// bitfields
|
|
|
|
kObjectMode,
|
|
|
|
kErrorEmitted,
|
|
|
|
kAutoDestroy,
|
|
|
|
kEmitClose,
|
|
|
|
kDestroyed,
|
|
|
|
kClosed,
|
|
|
|
kCloseEmitted,
|
|
|
|
kErrored,
|
|
|
|
kConstructed,
|
|
|
|
kOnConstructed,
|
|
|
|
} = require('internal/streams/utils');
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
const { errorOrDestroy } = destroyImpl;
|
|
|
|
|
|
|
|
ObjectSetPrototypeOf(Writable.prototype, Stream.prototype);
|
|
|
|
ObjectSetPrototypeOf(Writable, Stream);
|
|
|
|
|
|
|
|
function nop() {}
|
|
|
|
|
2023-09-29 20:13:44 +02:00
|
|
|
const kOnFinishedValue = Symbol('kOnFinishedValue');
|
|
|
|
const kErroredValue = Symbol('kErroredValue');
|
|
|
|
const kDefaultEncodingValue = Symbol('kDefaultEncodingValue');
|
|
|
|
const kWriteCbValue = Symbol('kWriteCbValue');
|
|
|
|
const kAfterWriteTickInfoValue = Symbol('kAfterWriteTickInfoValue');
|
2023-10-05 22:27:45 +02:00
|
|
|
const kBufferedValue = Symbol('kBufferedValue');
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-10-28 15:55:34 +02:00
|
|
|
const kSync = 1 << 9;
|
2023-09-26 22:19:08 +03:00
|
|
|
const kFinalCalled = 1 << 10;
|
|
|
|
const kNeedDrain = 1 << 11;
|
|
|
|
const kEnding = 1 << 12;
|
|
|
|
const kFinished = 1 << 13;
|
|
|
|
const kDecodeStrings = 1 << 14;
|
|
|
|
const kWriting = 1 << 15;
|
|
|
|
const kBufferProcessing = 1 << 16;
|
|
|
|
const kPrefinished = 1 << 17;
|
|
|
|
const kAllBuffers = 1 << 18;
|
|
|
|
const kAllNoop = 1 << 19;
|
2023-09-29 20:13:44 +02:00
|
|
|
const kOnFinished = 1 << 20;
|
2023-10-28 15:55:34 +02:00
|
|
|
const kHasWritable = 1 << 21;
|
|
|
|
const kWritable = 1 << 22;
|
|
|
|
const kCorked = 1 << 23;
|
|
|
|
const kDefaultUTF8Encoding = 1 << 24;
|
|
|
|
const kWriteCb = 1 << 25;
|
|
|
|
const kExpectWriteCb = 1 << 26;
|
|
|
|
const kAfterWriteTickInfo = 1 << 27;
|
|
|
|
const kAfterWritePending = 1 << 28;
|
|
|
|
const kBuffered = 1 << 29;
|
|
|
|
const kEnded = 1 << 30;
|
2023-09-26 22:19:08 +03:00
|
|
|
|
|
|
|
// TODO(benjamingr) it is likely slower to do it this way than with free functions
|
|
|
|
function makeBitMapDescriptor(bit) {
|
|
|
|
return {
|
|
|
|
enumerable: false,
|
2023-10-14 23:50:30 +02:00
|
|
|
get() { return (this[kState] & bit) !== 0; },
|
2023-09-26 22:19:08 +03:00
|
|
|
set(value) {
|
2023-10-14 23:50:30 +02:00
|
|
|
if (value) this[kState] |= bit;
|
|
|
|
else this[kState] &= ~bit;
|
2023-09-26 22:19:08 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
ObjectDefineProperties(WritableState.prototype, {
|
|
|
|
// Object stream flag to indicate whether or not this stream
|
|
|
|
// contains buffers or objects.
|
|
|
|
objectMode: makeBitMapDescriptor(kObjectMode),
|
|
|
|
|
|
|
|
// if _final has been called.
|
|
|
|
finalCalled: makeBitMapDescriptor(kFinalCalled),
|
|
|
|
|
|
|
|
// drain event flag.
|
|
|
|
needDrain: makeBitMapDescriptor(kNeedDrain),
|
|
|
|
|
|
|
|
// At the start of calling end()
|
|
|
|
ending: makeBitMapDescriptor(kEnding),
|
|
|
|
|
|
|
|
// When end() has been called, and returned.
|
|
|
|
ended: makeBitMapDescriptor(kEnded),
|
|
|
|
|
|
|
|
// When 'finish' is emitted.
|
|
|
|
finished: makeBitMapDescriptor(kFinished),
|
|
|
|
|
|
|
|
// Has it been destroyed.
|
|
|
|
destroyed: makeBitMapDescriptor(kDestroyed),
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
decodeStrings: makeBitMapDescriptor(kDecodeStrings),
|
|
|
|
|
|
|
|
// A flag to see when we're in the middle of a write.
|
|
|
|
writing: makeBitMapDescriptor(kWriting),
|
|
|
|
|
|
|
|
// A flag to be able to tell if the onwrite cb is called immediately,
|
|
|
|
// or on a later tick. We set this to true at first, because any
|
|
|
|
// actions that shouldn't happen until "later" should generally also
|
|
|
|
// not happen before the first write call.
|
|
|
|
sync: makeBitMapDescriptor(kSync),
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
bufferProcessing: makeBitMapDescriptor(kBufferProcessing),
|
|
|
|
|
|
|
|
// Stream is still being constructed and cannot be
|
|
|
|
// destroyed until construction finished or failed.
|
|
|
|
// Async construction is opt in, therefore we start as
|
|
|
|
// constructed.
|
|
|
|
constructed: makeBitMapDescriptor(kConstructed),
|
|
|
|
|
|
|
|
// Emit prefinish if the only thing we're waiting for is _write cbs
|
|
|
|
// This is relevant for synchronous Transform streams.
|
|
|
|
prefinished: makeBitMapDescriptor(kPrefinished),
|
|
|
|
|
|
|
|
// True if the error was already emitted and should not be thrown again.
|
|
|
|
errorEmitted: makeBitMapDescriptor(kErrorEmitted),
|
|
|
|
|
|
|
|
// Should close be emitted on destroy. Defaults to true.
|
|
|
|
emitClose: makeBitMapDescriptor(kEmitClose),
|
|
|
|
|
|
|
|
// Should .destroy() be called after 'finish' (and potentially 'end').
|
|
|
|
autoDestroy: makeBitMapDescriptor(kAutoDestroy),
|
|
|
|
|
|
|
|
// Indicates whether the stream has finished destroying.
|
|
|
|
closed: makeBitMapDescriptor(kClosed),
|
|
|
|
|
|
|
|
// True if close has been emitted or would have been emitted
|
|
|
|
// depending on emitClose.
|
|
|
|
closeEmitted: makeBitMapDescriptor(kCloseEmitted),
|
|
|
|
|
|
|
|
allBuffers: makeBitMapDescriptor(kAllBuffers),
|
|
|
|
allNoop: makeBitMapDescriptor(kAllNoop),
|
2023-09-29 20:13:44 +02:00
|
|
|
|
|
|
|
// Indicates whether the stream has errored. When true all write() calls
|
|
|
|
// should return false. This is needed since when autoDestroy
|
|
|
|
// is disabled we need a way to tell whether the stream has failed.
|
|
|
|
// This is/should be a cold path.
|
|
|
|
errored: {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: false,
|
2023-10-14 23:50:30 +02:00
|
|
|
get() { return (this[kState] & kErrored) !== 0 ? this[kErroredValue] : null; },
|
2023-09-29 20:13:44 +02:00
|
|
|
set(value) {
|
|
|
|
if (value) {
|
|
|
|
this[kErroredValue] = value;
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= kErrored;
|
2023-09-29 20:13:44 +02:00
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] &= ~kErrored;
|
2023-09-29 20:13:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
writable: {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: false,
|
2023-10-14 23:50:30 +02:00
|
|
|
get() { return (this[kState] & kHasWritable) !== 0 ? (this[kState] & kWritable) !== 0 : undefined; },
|
2023-09-29 20:13:44 +02:00
|
|
|
set(value) {
|
|
|
|
if (value == null) {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] &= ~(kHasWritable | kWritable);
|
2023-09-29 20:13:44 +02:00
|
|
|
} else if (value) {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= (kHasWritable | kWritable);
|
2023-09-29 20:13:44 +02:00
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= kHasWritable;
|
|
|
|
this[kState] &= ~kWritable;
|
2023-09-29 20:13:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
defaultEncoding: {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: false,
|
2023-10-14 23:50:30 +02:00
|
|
|
get() { return (this[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : this[kDefaultEncodingValue]; },
|
2023-09-29 20:13:44 +02:00
|
|
|
set(value) {
|
|
|
|
if (value === 'utf8' || value === 'utf-8') {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= kDefaultUTF8Encoding;
|
2023-09-29 20:13:44 +02:00
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] &= ~kDefaultUTF8Encoding;
|
2023-09-29 20:13:44 +02:00
|
|
|
this[kDefaultEncodingValue] = value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// The callback that the user supplies to write(chunk, encoding, cb).
|
|
|
|
writecb: {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: false,
|
2023-10-14 23:50:30 +02:00
|
|
|
get() { return (this[kState] & kWriteCb) !== 0 ? this[kWriteCbValue] : nop; },
|
2023-09-29 20:13:44 +02:00
|
|
|
set(value) {
|
2024-06-02 03:35:36 +03:00
|
|
|
this[kWriteCbValue] = value;
|
2023-09-29 20:13:44 +02:00
|
|
|
if (value) {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= kWriteCb;
|
2023-09-29 20:13:44 +02:00
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] &= ~kWriteCb;
|
2023-09-29 20:13:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Storage for data passed to the afterWrite() callback in case of
|
|
|
|
// synchronous _write() completion.
|
|
|
|
afterWriteTickInfo: {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: false,
|
2023-10-14 23:50:30 +02:00
|
|
|
get() { return (this[kState] & kAfterWriteTickInfo) !== 0 ? this[kAfterWriteTickInfoValue] : null; },
|
2023-09-29 20:13:44 +02:00
|
|
|
set(value) {
|
2024-06-02 03:35:36 +03:00
|
|
|
this[kAfterWriteTickInfoValue] = value;
|
2023-09-29 20:13:44 +02:00
|
|
|
if (value) {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= kAfterWriteTickInfo;
|
2023-09-29 20:13:44 +02:00
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] &= ~kAfterWriteTickInfo;
|
2023-09-29 20:13:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2023-10-05 22:27:45 +02:00
|
|
|
|
|
|
|
buffered: {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: false,
|
2023-10-14 23:50:30 +02:00
|
|
|
get() { return (this[kState] & kBuffered) !== 0 ? this[kBufferedValue] : []; },
|
2023-10-05 22:27:45 +02:00
|
|
|
set(value) {
|
|
|
|
this[kBufferedValue] = value;
|
|
|
|
if (value) {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= kBuffered;
|
2023-10-05 22:27:45 +02:00
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] &= ~kBuffered;
|
2023-10-05 22:27:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2023-09-26 22:19:08 +03:00
|
|
|
});
|
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
function WritableState(options, stream, isDuplex) {
|
2023-09-26 22:19:08 +03:00
|
|
|
// Bit map field to store WritableState more effciently with 1 bit per field
|
|
|
|
// instead of a V8 slot per field.
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] = kSync | kConstructed | kEmitClose | kAutoDestroy;
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-10-25 13:26:27 +02:00
|
|
|
if (options && options.objectMode)
|
|
|
|
this[kState] |= kObjectMode;
|
|
|
|
|
|
|
|
if (isDuplex && options && options.writableObjectMode)
|
|
|
|
this[kState] |= kObjectMode;
|
2020-09-16 19:05:06 +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().
|
|
|
|
this.highWaterMark = options ?
|
|
|
|
getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex) :
|
|
|
|
getDefaultHighWaterMark(false);
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if (!options || options.decodeStrings !== false) this[kState] |= kDecodeStrings;
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-09-26 22:19:08 +03:00
|
|
|
// Should close be emitted on destroy. Defaults to true.
|
2023-10-14 23:50:30 +02:00
|
|
|
if (options && options.emitClose === false) this[kState] &= ~kEmitClose;
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-09-26 22:19:08 +03:00
|
|
|
// Should .destroy() be called after 'end' (and potentially 'finish').
|
2023-10-14 23:50:30 +02:00
|
|
|
if (options && options.autoDestroy === false) this[kState] &= ~kAutoDestroy;
|
2020-09-16 19:05:06 +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.
|
2023-10-25 13:26:27 +02:00
|
|
|
const defaultEncoding = options ? options.defaultEncoding : null;
|
2023-09-29 20:13:44 +02:00
|
|
|
if (defaultEncoding == null || defaultEncoding === 'utf8' || defaultEncoding === 'utf-8') {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] |= kDefaultUTF8Encoding;
|
2023-01-30 18:54:29 +01:00
|
|
|
} else if (Buffer.isEncoding(defaultEncoding)) {
|
2023-10-14 23:50:30 +02:00
|
|
|
this[kState] &= ~kDefaultUTF8Encoding;
|
2023-09-29 20:13:44 +02:00
|
|
|
this[kDefaultEncodingValue] = defaultEncoding;
|
2023-01-30 18:54:29 +01:00
|
|
|
} else {
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(defaultEncoding);
|
|
|
|
}
|
2020-09-16 19:05:06 +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;
|
|
|
|
|
|
|
|
// When true all writes will be buffered until .uncork() call.
|
|
|
|
this.corked = 0;
|
|
|
|
|
|
|
|
// The callback that's passed to _write(chunk, cb).
|
|
|
|
this.onwrite = onwrite.bind(undefined, stream);
|
|
|
|
|
|
|
|
// The amount that is being written when _write is called.
|
|
|
|
this.writelen = 0;
|
|
|
|
|
|
|
|
resetBuffer(this);
|
|
|
|
|
|
|
|
// Number of pending user-supplied write callbacks
|
|
|
|
// this must be 0 before 'finish' can be emitted.
|
|
|
|
this.pendingcb = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetBuffer(state) {
|
2023-10-05 22:27:45 +02:00
|
|
|
state[kBufferedValue] = null;
|
2020-09-16 19:05:06 +02:00
|
|
|
state.bufferedIndex = 0;
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kAllBuffers | kAllNoop;
|
|
|
|
state[kState] &= ~kBuffered;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WritableState.prototype.getBuffer = function getBuffer() {
|
2023-10-14 23:50:30 +02:00
|
|
|
return (this[kState] & kBuffered) === 0 ? [] : ArrayPrototypeSlice(this.buffered, this.bufferedIndex);
|
2020-09-16 19:05:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ObjectDefineProperty(WritableState.prototype, 'bufferedRequestCount', {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-10-14 23:50:30 +02:00
|
|
|
return (this[kState] & kBuffered) === 0 ? 0 : this[kBufferedValue].length - this.bufferedIndex;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
});
|
|
|
|
|
2023-10-25 13:26:27 +02:00
|
|
|
WritableState.prototype[kOnConstructed] = function onConstructed(stream) {
|
|
|
|
if ((this[kState] & kWriting) === 0) {
|
|
|
|
clearBuffer(stream, this);
|
|
|
|
}
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-10-25 13:26:27 +02:00
|
|
|
if ((this[kState] & kEnding) !== 0) {
|
|
|
|
finishMaybe(stream, this);
|
|
|
|
}
|
|
|
|
};
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-10-25 13:26:27 +02:00
|
|
|
function Writable(options) {
|
|
|
|
if (!(this instanceof Writable))
|
2020-09-16 19:05:06 +02:00
|
|
|
return new Writable(options);
|
|
|
|
|
2023-10-29 17:29:59 +01:00
|
|
|
this._events ??= {
|
|
|
|
close: undefined,
|
|
|
|
error: undefined,
|
|
|
|
prefinish: undefined,
|
|
|
|
finish: undefined,
|
|
|
|
drain: undefined,
|
|
|
|
// Skip uncommon events...
|
|
|
|
// [destroyImpl.kConstruct]: undefined,
|
|
|
|
// [destroyImpl.kDestroy]: undefined,
|
|
|
|
};
|
|
|
|
|
2023-10-25 13:26:27 +02:00
|
|
|
this._writableState = new WritableState(options, this, false);
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
if (options) {
|
|
|
|
if (typeof options.write === 'function')
|
|
|
|
this._write = options.write;
|
|
|
|
|
|
|
|
if (typeof options.writev === 'function')
|
|
|
|
this._writev = options.writev;
|
|
|
|
|
|
|
|
if (typeof options.destroy === 'function')
|
|
|
|
this._destroy = options.destroy;
|
|
|
|
|
|
|
|
if (typeof options.final === 'function')
|
|
|
|
this._final = options.final;
|
|
|
|
|
|
|
|
if (typeof options.construct === 'function')
|
|
|
|
this._construct = options.construct;
|
2021-07-10 21:28:47 +02:00
|
|
|
|
2020-12-07 18:42:46 +02:00
|
|
|
if (options.signal)
|
2021-07-10 21:28:47 +02:00
|
|
|
addAbortSignal(options.signal, this);
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 15:05:42 +02:00
|
|
|
Stream.call(this, options);
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-10-25 13:26:27 +02:00
|
|
|
if (this._construct != null) {
|
|
|
|
destroyImpl.construct(this, () => {
|
|
|
|
this._writableState[kOnConstructed](this);
|
|
|
|
});
|
|
|
|
}
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:52:30 +01:00
|
|
|
ObjectDefineProperty(Writable, SymbolHasInstance, {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-11-16 12:52:30 +01:00
|
|
|
value: function(object) {
|
|
|
|
if (FunctionPrototypeSymbolHasInstance(this, object)) return true;
|
|
|
|
if (this !== Writable) return false;
|
|
|
|
|
|
|
|
return object && object._writableState instanceof WritableState;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
// Otherwise people can pipe Writable streams, which is just wrong.
|
|
|
|
Writable.prototype.pipe = function() {
|
|
|
|
errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE());
|
|
|
|
};
|
|
|
|
|
2021-01-06 19:45:41 +01:00
|
|
|
function _write(stream, chunk, encoding, cb) {
|
|
|
|
const state = stream._writableState;
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-10-24 15:45:23 +02:00
|
|
|
if (cb == null || typeof cb !== 'function') {
|
2023-10-23 14:08:26 +02:00
|
|
|
cb = nop;
|
2023-10-24 15:45:23 +02:00
|
|
|
}
|
2023-10-23 14:08:26 +02:00
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
if (chunk === null) {
|
|
|
|
throw new ERR_STREAM_NULL_VALUES();
|
2023-10-24 15:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((state[kState] & kObjectMode) === 0) {
|
|
|
|
if (!encoding) {
|
|
|
|
encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state.defaultEncoding;
|
|
|
|
} else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding)) {
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
if (typeof chunk === 'string') {
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kDecodeStrings) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
chunk = Buffer.from(chunk, encoding);
|
|
|
|
encoding = 'buffer';
|
|
|
|
}
|
|
|
|
} else if (chunk instanceof Buffer) {
|
|
|
|
encoding = 'buffer';
|
2024-03-20 18:27:29 +01:00
|
|
|
} else if (Stream._isArrayBufferView(chunk)) {
|
2020-09-16 19:05:06 +02:00
|
|
|
chunk = Stream._uint8ArrayToBuffer(chunk);
|
|
|
|
encoding = 'buffer';
|
|
|
|
} else {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2024-03-20 18:27:29 +01:00
|
|
|
'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk);
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let err;
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kEnding) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
err = new ERR_STREAM_WRITE_AFTER_END();
|
2023-10-14 23:50:30 +02:00
|
|
|
} else if ((state[kState] & kDestroyed) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
err = new ERR_STREAM_DESTROYED('write');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
process.nextTick(cb, err);
|
2021-01-06 19:45:41 +01:00
|
|
|
errorOrDestroy(stream, err, true);
|
|
|
|
return err;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
2023-10-24 15:45:23 +02:00
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
state.pendingcb++;
|
2021-01-06 19:45:41 +01:00
|
|
|
return writeOrBuffer(stream, state, chunk, encoding, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
Writable.prototype.write = function(chunk, encoding, cb) {
|
2023-10-24 15:45:23 +02:00
|
|
|
if (encoding != null && typeof encoding === 'function') {
|
|
|
|
cb = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2021-01-06 19:45:41 +01:00
|
|
|
return _write(this, chunk, encoding, cb) === true;
|
2020-09-16 19:05:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Writable.prototype.cork = function() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kCorked;
|
2023-09-29 20:13:44 +02:00
|
|
|
state.corked++;
|
2020-09-16 19:05:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Writable.prototype.uncork = function() {
|
|
|
|
const state = this._writableState;
|
|
|
|
|
|
|
|
if (state.corked) {
|
|
|
|
state.corked--;
|
|
|
|
|
2023-09-29 20:13:44 +02:00
|
|
|
if (!state.corked) {
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kCorked;
|
2023-09-29 20:13:44 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kWriting) === 0)
|
2020-09-16 19:05:06 +02:00
|
|
|
clearBuffer(this, state);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
|
|
|
|
// node::ParseEncoding() requires lower case.
|
|
|
|
if (typeof encoding === 'string')
|
2020-11-16 12:52:30 +01:00
|
|
|
encoding = StringPrototypeToLowerCase(encoding);
|
2020-09-16 19:05:06 +02:00
|
|
|
if (!Buffer.isEncoding(encoding))
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
|
|
this._writableState.defaultEncoding = encoding;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
function writeOrBuffer(stream, state, chunk, encoding, callback) {
|
2023-10-14 23:50:30 +02:00
|
|
|
const len = (state[kState] & kObjectMode) !== 0 ? 1 : chunk.length;
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
state.length += len;
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & (kWriting | kErrored | kCorked | kConstructed)) !== kConstructed) {
|
|
|
|
if ((state[kState] & kBuffered) === 0) {
|
|
|
|
state[kState] |= kBuffered;
|
2023-10-05 22:27:45 +02:00
|
|
|
state[kBufferedValue] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
state[kBufferedValue].push({ chunk, encoding, callback });
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kAllBuffers) !== 0 && encoding !== 'buffer') {
|
|
|
|
state[kState] &= ~kAllBuffers;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kAllNoop) !== 0 && callback !== nop) {
|
|
|
|
state[kState] &= ~kAllNoop;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state.writelen = len;
|
2023-09-29 20:13:44 +02:00
|
|
|
if (callback !== nop) {
|
|
|
|
state.writecb = callback;
|
|
|
|
}
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kWriting | kSync | kExpectWriteCb;
|
2020-09-16 19:05:06 +02:00
|
|
|
stream._write(chunk, encoding, state.onwrite);
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kSync;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
2023-10-04 14:45:40 +02:00
|
|
|
const ret = state.length < state.highWaterMark;
|
|
|
|
|
|
|
|
if (!ret) {
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kNeedDrain;
|
2023-10-04 14:45:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
// Return false if errored or destroyed in order to break
|
|
|
|
// any synchronous while(stream.write(data)) loops.
|
2023-10-14 23:50:30 +02:00
|
|
|
return ret && (state[kState] & (kDestroyed | kErrored)) === 0;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function doWrite(stream, state, writev, len, chunk, encoding, cb) {
|
|
|
|
state.writelen = len;
|
2023-09-29 20:13:44 +02:00
|
|
|
if (cb !== nop) {
|
|
|
|
state.writecb = cb;
|
|
|
|
}
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kWriting | kSync | kExpectWriteCb;
|
|
|
|
if ((state[kState] & kDestroyed) !== 0)
|
2020-09-16 19:05:06 +02:00
|
|
|
state.onwrite(new ERR_STREAM_DESTROYED('write'));
|
|
|
|
else if (writev)
|
|
|
|
stream._writev(chunk, state.onwrite);
|
|
|
|
else
|
|
|
|
stream._write(chunk, encoding, state.onwrite);
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kSync;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onwriteError(stream, state, er, cb) {
|
|
|
|
--state.pendingcb;
|
|
|
|
|
|
|
|
cb(er);
|
|
|
|
// Ensure callbacks are invoked even when autoDestroy is
|
|
|
|
// not enabled. Passing `er` here doesn't make sense since
|
|
|
|
// it's related to one specific write, not to the buffered
|
|
|
|
// writes.
|
|
|
|
errorBuffer(state);
|
|
|
|
// This can emit error, but error must always follow cb.
|
|
|
|
errorOrDestroy(stream, er);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onwrite(stream, er) {
|
|
|
|
const state = stream._writableState;
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kExpectWriteCb) === 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
errorOrDestroy(stream, new ERR_MULTIPLE_CALLBACK());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
const sync = (state[kState] & kSync) !== 0;
|
|
|
|
const cb = (state[kState] & kWriteCb) !== 0 ? state[kWriteCbValue] : nop;
|
2023-09-29 20:13:44 +02:00
|
|
|
|
2024-06-02 03:35:36 +03:00
|
|
|
state.writecb = null;
|
|
|
|
state[kState] &= ~(kWriting | kExpectWriteCb);
|
2020-09-16 19:05:06 +02:00
|
|
|
state.length -= state.writelen;
|
|
|
|
state.writelen = 0;
|
|
|
|
|
|
|
|
if (er) {
|
|
|
|
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
|
2020-11-24 14:11:20 +01:00
|
|
|
er.stack; // eslint-disable-line no-unused-expressions
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kErrored) === 0) {
|
2023-10-04 12:13:06 +02:00
|
|
|
state[kErroredValue] = er;
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kErrored;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case of duplex streams we need to notify the readable side of the
|
|
|
|
// error.
|
|
|
|
if (stream._readableState && !stream._readableState.errored) {
|
|
|
|
stream._readableState.errored = er;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sync) {
|
|
|
|
process.nextTick(onwriteError, stream, state, er, cb);
|
|
|
|
} else {
|
|
|
|
onwriteError(stream, state, er, cb);
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kBuffered) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
clearBuffer(stream, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sync) {
|
2023-10-14 23:50:30 +02:00
|
|
|
const needDrain = (state[kState] & kNeedDrain) !== 0 && state.length === 0;
|
|
|
|
const needTick = needDrain || (state[kState] & kDestroyed !== 0) || cb !== nop;
|
2023-10-01 22:07:12 +02:00
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
// It is a common case that the callback passed to .write() is always
|
|
|
|
// the same. In that case, we do not schedule a new nextTick(), but
|
|
|
|
// rather just increase a counter, to improve performance and avoid
|
|
|
|
// memory allocations.
|
2023-09-29 20:13:44 +02:00
|
|
|
if (cb === nop) {
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kAfterWritePending) === 0 && needTick) {
|
2023-09-29 20:13:44 +02:00
|
|
|
process.nextTick(afterWrite, stream, state, 1, cb);
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kAfterWritePending;
|
2023-09-29 20:13:44 +02:00
|
|
|
} else {
|
2023-10-01 22:07:12 +02:00
|
|
|
state.pendingcb--;
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kEnding) !== 0) {
|
2023-10-04 12:13:06 +02:00
|
|
|
finishMaybe(stream, state, true);
|
|
|
|
}
|
2023-09-29 20:13:44 +02:00
|
|
|
}
|
2023-10-14 23:50:30 +02:00
|
|
|
} else if ((state[kState] & kAfterWriteTickInfo) !== 0 &&
|
2023-10-01 22:07:12 +02:00
|
|
|
state[kAfterWriteTickInfoValue].cb === cb) {
|
|
|
|
state[kAfterWriteTickInfoValue].count++;
|
|
|
|
} else if (needTick) {
|
|
|
|
state[kAfterWriteTickInfoValue] = { count: 1, cb, stream, state };
|
|
|
|
process.nextTick(afterWriteTick, state[kAfterWriteTickInfoValue]);
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= (kAfterWritePending | kAfterWriteTickInfo);
|
2020-09-16 19:05:06 +02:00
|
|
|
} else {
|
2023-10-01 22:07:12 +02:00
|
|
|
state.pendingcb--;
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kEnding) !== 0) {
|
2023-10-04 12:13:06 +02:00
|
|
|
finishMaybe(stream, state, true);
|
|
|
|
}
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
afterWrite(stream, state, 1, cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function afterWriteTick({ stream, state, count, cb }) {
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kAfterWriteTickInfo;
|
2023-10-01 22:07:12 +02:00
|
|
|
state[kAfterWriteTickInfoValue] = null;
|
2020-09-16 19:05:06 +02:00
|
|
|
return afterWrite(stream, state, count, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
function afterWrite(stream, state, count, cb) {
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kAfterWritePending;
|
2023-09-29 20:13:44 +02:00
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
const needDrain = (state[kState] & (kEnding | kNeedDrain | kDestroyed)) === kNeedDrain && state.length === 0;
|
2020-09-16 19:05:06 +02:00
|
|
|
if (needDrain) {
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kNeedDrain;
|
2020-09-16 19:05:06 +02:00
|
|
|
stream.emit('drain');
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count-- > 0) {
|
|
|
|
state.pendingcb--;
|
2022-08-23 09:51:53 +02:00
|
|
|
cb(null);
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kDestroyed) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
errorBuffer(state);
|
|
|
|
}
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kEnding) !== 0) {
|
2023-10-04 12:13:06 +02:00
|
|
|
finishMaybe(stream, state, true);
|
|
|
|
}
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there's something in the buffer waiting, then invoke callbacks.
|
|
|
|
function errorBuffer(state) {
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kWriting) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kBuffered) !== 0) {
|
2023-10-05 22:27:45 +02:00
|
|
|
for (let n = state.bufferedIndex; n < state.buffered.length; ++n) {
|
|
|
|
const { chunk, callback } = state[kBufferedValue][n];
|
2023-10-14 23:50:30 +02:00
|
|
|
const len = (state[kState] & kObjectMode) !== 0 ? 1 : chunk.length;
|
2023-10-05 22:27:45 +02:00
|
|
|
state.length -= len;
|
|
|
|
callback(state.errored ?? new ERR_STREAM_DESTROYED('write'));
|
|
|
|
}
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
2023-09-29 20:13:44 +02:00
|
|
|
|
|
|
|
callFinishedCallbacks(state, state.errored ?? new ERR_STREAM_DESTROYED('end'));
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
resetBuffer(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's something in the buffer waiting, then process it.
|
|
|
|
function clearBuffer(stream, state) {
|
2023-12-08 02:21:19 +08:00
|
|
|
if ((state[kState] & (kDestroyed | kBufferProcessing | kCorked | kBuffered | kConstructed)) !==
|
|
|
|
(kBuffered | kConstructed)) {
|
2020-09-16 19:05:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
const objectMode = (state[kState] & kObjectMode) !== 0;
|
2023-10-05 22:27:45 +02:00
|
|
|
const { [kBufferedValue]: buffered, bufferedIndex } = state;
|
2020-09-16 19:05:06 +02:00
|
|
|
const bufferedLength = buffered.length - bufferedIndex;
|
|
|
|
|
|
|
|
if (!bufferedLength) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let i = bufferedIndex;
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kBufferProcessing;
|
2020-09-16 19:05:06 +02:00
|
|
|
if (bufferedLength > 1 && stream._writev) {
|
|
|
|
state.pendingcb -= bufferedLength - 1;
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
const callback = (state[kState] & kAllNoop) !== 0 ? nop : (err) => {
|
2020-09-16 19:05:06 +02:00
|
|
|
for (let n = i; n < buffered.length; ++n) {
|
|
|
|
buffered[n].callback(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Make a copy of `buffered` if it's going to be used by `callback` above,
|
|
|
|
// since `doWrite` will mutate the array.
|
2023-10-14 23:50:30 +02:00
|
|
|
const chunks = (state[kState] & kAllNoop) !== 0 && i === 0 ?
|
2020-11-16 12:52:30 +01:00
|
|
|
buffered : ArrayPrototypeSlice(buffered, i);
|
2023-10-14 23:50:30 +02:00
|
|
|
chunks.allBuffers = (state[kState] & kAllBuffers) !== 0;
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
doWrite(stream, state, true, state.length, chunks, '', callback);
|
|
|
|
|
|
|
|
resetBuffer(state);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
const { chunk, encoding, callback } = buffered[i];
|
|
|
|
buffered[i++] = null;
|
|
|
|
const len = objectMode ? 1 : chunk.length;
|
|
|
|
doWrite(stream, state, false, len, chunk, encoding, callback);
|
2023-10-14 23:50:30 +02:00
|
|
|
} while (i < buffered.length && (state[kState] & kWriting) === 0);
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
if (i === buffered.length) {
|
|
|
|
resetBuffer(state);
|
|
|
|
} else if (i > 256) {
|
2021-04-15 15:05:42 +02:00
|
|
|
buffered.splice(0, i);
|
2020-09-16 19:05:06 +02:00
|
|
|
state.bufferedIndex = 0;
|
|
|
|
} else {
|
|
|
|
state.bufferedIndex = i;
|
|
|
|
}
|
|
|
|
}
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kBufferProcessing;
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Writable.prototype._write = function(chunk, encoding, cb) {
|
|
|
|
if (this._writev) {
|
|
|
|
this._writev([{ chunk, encoding }], cb);
|
|
|
|
} else {
|
|
|
|
throw new ERR_METHOD_NOT_IMPLEMENTED('_write()');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Writable.prototype._writev = null;
|
|
|
|
|
|
|
|
Writable.prototype.end = function(chunk, encoding, cb) {
|
|
|
|
const state = this._writableState;
|
|
|
|
|
|
|
|
if (typeof chunk === 'function') {
|
|
|
|
cb = chunk;
|
|
|
|
chunk = null;
|
|
|
|
encoding = null;
|
|
|
|
} else if (typeof encoding === 'function') {
|
|
|
|
cb = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2021-01-06 19:45:41 +01:00
|
|
|
let err;
|
|
|
|
|
2023-09-29 20:13:44 +02:00
|
|
|
if (chunk != null) {
|
2021-01-06 19:45:41 +01:00
|
|
|
const ret = _write(this, chunk, encoding);
|
|
|
|
if (ret instanceof Error) {
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
// .end() fully uncorks.
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kCorked) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
state.corked = 1;
|
|
|
|
this.uncork();
|
|
|
|
}
|
|
|
|
|
2021-01-06 19:45:41 +01:00
|
|
|
if (err) {
|
|
|
|
// Do nothing...
|
2023-10-14 23:50:30 +02:00
|
|
|
} else if ((state[kState] & (kEnding | kErrored)) === 0) {
|
2021-01-06 19:45:41 +01:00
|
|
|
// This is forgiving in terms of unnecessary calls to end() and can hide
|
|
|
|
// logic errors. However, usually such errors are harmless and causing a
|
|
|
|
// hard error can be disproportionately destructive. It is not always
|
|
|
|
// trivial for the user to determine whether end() needs to be called
|
|
|
|
// or not.
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kEnding;
|
2020-09-16 19:05:06 +02:00
|
|
|
finishMaybe(this, state, true);
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kEnded;
|
|
|
|
} else if ((state[kState] & kFinished) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
err = new ERR_STREAM_ALREADY_FINISHED('end');
|
2023-10-14 23:50:30 +02:00
|
|
|
} else if ((state[kState] & kDestroyed) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
err = new ERR_STREAM_DESTROYED('end');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof cb === 'function') {
|
2022-08-23 09:51:53 +02:00
|
|
|
if (err) {
|
2020-09-16 19:05:06 +02:00
|
|
|
process.nextTick(cb, err);
|
2023-10-14 23:50:30 +02:00
|
|
|
} else if ((state[kState] & kErrored) !== 0) {
|
2023-10-01 22:07:12 +02:00
|
|
|
process.nextTick(cb, state[kErroredValue]);
|
2023-10-14 23:50:30 +02:00
|
|
|
} else if ((state[kState] & kFinished) !== 0) {
|
2022-08-23 09:51:53 +02:00
|
|
|
process.nextTick(cb, null);
|
2020-09-16 19:05:06 +02:00
|
|
|
} else {
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kOnFinished;
|
2023-09-29 20:13:44 +02:00
|
|
|
state[kOnFinishedValue] ??= [];
|
|
|
|
state[kOnFinishedValue].push(cb);
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
function needFinish(state) {
|
2023-09-26 22:19:08 +03:00
|
|
|
return (
|
|
|
|
// State is ended && constructed but not destroyed, finished, writing, errorEmitted or closedEmitted
|
2023-10-14 23:50:30 +02:00
|
|
|
(state[kState] & (
|
2023-09-26 22:19:08 +03:00
|
|
|
kEnding |
|
|
|
|
kDestroyed |
|
|
|
|
kConstructed |
|
|
|
|
kFinished |
|
|
|
|
kWriting |
|
|
|
|
kErrorEmitted |
|
2023-09-29 20:13:44 +02:00
|
|
|
kCloseEmitted |
|
2023-10-05 22:27:45 +02:00
|
|
|
kErrored |
|
|
|
|
kBuffered
|
|
|
|
)) === (kEnding | kConstructed) && state.length === 0);
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 18:43:18 +02:00
|
|
|
function onFinish(stream, state, err) {
|
|
|
|
if ((state[kState] & kPrefinished) !== 0) {
|
2024-03-18 05:33:04 -04:00
|
|
|
errorOrDestroy(stream, err ?? new ERR_MULTIPLE_CALLBACK());
|
2023-10-22 18:43:18 +02:00
|
|
|
return;
|
2021-07-09 20:18:06 +02:00
|
|
|
}
|
2023-10-22 18:43:18 +02:00
|
|
|
state.pendingcb--;
|
|
|
|
if (err) {
|
|
|
|
callFinishedCallbacks(state, err);
|
|
|
|
errorOrDestroy(stream, err, (state[kState] & kSync) !== 0);
|
|
|
|
} else if (needFinish(state)) {
|
|
|
|
state[kState] |= kPrefinished;
|
|
|
|
stream.emit('prefinish');
|
|
|
|
// Backwards compat. Don't check state.sync here.
|
|
|
|
// Some streams assume 'finish' will be emitted
|
|
|
|
// asynchronously relative to _final callback.
|
|
|
|
state.pendingcb++;
|
|
|
|
process.nextTick(finish, stream, state);
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prefinish(stream, state) {
|
2023-10-22 18:43:18 +02:00
|
|
|
if ((state[kState] & (kPrefinished | kFinalCalled)) !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof stream._final === 'function' && (state[kState] & kDestroyed) === 0) {
|
|
|
|
state[kState] |= kFinalCalled | kSync;
|
|
|
|
state.pendingcb++;
|
|
|
|
|
|
|
|
try {
|
|
|
|
stream._final((err) => onFinish(stream, state, err));
|
|
|
|
} catch (err) {
|
|
|
|
onFinish(stream, state, err);
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
2023-10-22 18:43:18 +02:00
|
|
|
|
|
|
|
state[kState] &= ~kSync;
|
|
|
|
} else {
|
|
|
|
state[kState] |= kFinalCalled | kPrefinished;
|
|
|
|
stream.emit('prefinish');
|
2020-09-16 19:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function finishMaybe(stream, state, sync) {
|
|
|
|
if (needFinish(state)) {
|
|
|
|
prefinish(stream, state);
|
2021-11-18 07:50:29 +01:00
|
|
|
if (state.pendingcb === 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
if (sync) {
|
2021-11-18 07:50:29 +01:00
|
|
|
state.pendingcb++;
|
|
|
|
process.nextTick((stream, state) => {
|
|
|
|
if (needFinish(state)) {
|
|
|
|
finish(stream, state);
|
|
|
|
} else {
|
|
|
|
state.pendingcb--;
|
|
|
|
}
|
|
|
|
}, stream, state);
|
|
|
|
} else if (needFinish(state)) {
|
|
|
|
state.pendingcb++;
|
2020-09-16 19:05:06 +02:00
|
|
|
finish(stream, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function finish(stream, state) {
|
|
|
|
state.pendingcb--;
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] |= kFinished;
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-09-29 20:13:44 +02:00
|
|
|
callFinishedCallbacks(state, null);
|
2020-09-16 19:05:06 +02:00
|
|
|
|
|
|
|
stream.emit('finish');
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kAutoDestroy) !== 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
// In case of duplex streams we need a way to detect
|
|
|
|
// if the readable side is ready for autoDestroy as well.
|
|
|
|
const rState = stream._readableState;
|
|
|
|
const autoDestroy = !rState || (
|
|
|
|
rState.autoDestroy &&
|
|
|
|
// We don't expect the readable to ever 'end'
|
|
|
|
// if readable is explicitly set to false.
|
|
|
|
(rState.endEmitted || rState.readable === false)
|
|
|
|
);
|
|
|
|
if (autoDestroy) {
|
|
|
|
stream.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 20:13:44 +02:00
|
|
|
function callFinishedCallbacks(state, err) {
|
2023-10-14 23:50:30 +02:00
|
|
|
if ((state[kState] & kOnFinished) === 0) {
|
2023-09-29 20:13:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onfinishCallbacks = state[kOnFinishedValue];
|
|
|
|
state[kOnFinishedValue] = null;
|
2023-10-14 23:50:30 +02:00
|
|
|
state[kState] &= ~kOnFinished;
|
2023-09-29 20:13:44 +02:00
|
|
|
for (let i = 0; i < onfinishCallbacks.length; i++) {
|
|
|
|
onfinishCallbacks[i](err);
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 19:05:06 +02:00
|
|
|
|
2023-09-29 20:13:44 +02:00
|
|
|
ObjectDefineProperties(Writable.prototype, {
|
2021-11-02 12:01:48 +02:00
|
|
|
closed: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2021-11-02 12:01:48 +02:00
|
|
|
get() {
|
2023-10-14 23:50:30 +02:00
|
|
|
return this._writableState ? (this._writableState[kState] & kClosed) !== 0 : false;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2021-11-02 12:01:48 +02:00
|
|
|
},
|
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
destroyed: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-10-14 23:50:30 +02:00
|
|
|
return this._writableState ? (this._writableState[kState] & kDestroyed) !== 0 : false;
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
// Backward compatibility, the user is explicitly managing destroyed.
|
2023-09-26 22:19:08 +03:00
|
|
|
if (!this._writableState) return;
|
|
|
|
|
2023-10-14 23:50:30 +02:00
|
|
|
if (value) this._writableState[kState] |= kDestroyed;
|
|
|
|
else this._writableState[kState] &= ~kDestroyed;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
writable: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
|
|
|
const w = this._writableState;
|
|
|
|
// w.writable === false means that this is part of a Duplex stream
|
|
|
|
// where the writable side was disabled upon construction.
|
|
|
|
// Compat. The user might manually disable writable side through
|
|
|
|
// deprecated setter.
|
2024-06-04 12:47:35 +03:00
|
|
|
return !!w && w.writable !== false &&
|
|
|
|
(w[kState] & (kEnding | kEnded | kDestroyed | kErrored)) === 0;
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
// Backwards compatible.
|
|
|
|
if (this._writableState) {
|
|
|
|
this._writableState.writable = !!val;
|
|
|
|
}
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
writableFinished: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
2023-10-14 23:50:30 +02:00
|
|
|
return state ? (state[kState] & kFinished) !== 0 : false;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
writableObjectMode: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
2023-10-14 23:50:30 +02:00
|
|
|
return state ? (state[kState] & kObjectMode) !== 0 : false;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
writableBuffer: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
|
|
|
return state && state.getBuffer();
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
writableEnded: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
2023-10-14 23:50:30 +02:00
|
|
|
return state ? (state[kState] & kEnding) !== 0 : false;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
2020-09-25 18:40:01 +02:00
|
|
|
writableNeedDrain: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-25 18:40:01 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
2023-10-14 23:50:30 +02:00
|
|
|
return state ? (state[kState] & (kDestroyed | kEnding | kNeedDrain)) === kNeedDrain : false;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-25 18:40:01 +02:00
|
|
|
},
|
|
|
|
|
2020-09-16 19:05:06 +02:00
|
|
|
writableHighWaterMark: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
|
|
|
return state && state.highWaterMark;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
writableCorked: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
|
|
|
return state ? state.corked : 0;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
writableLength: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-09-16 19:05:06 +02:00
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
|
|
|
return state && state.length;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2021-11-02 12:01:48 +02:00
|
|
|
},
|
|
|
|
|
2021-11-13 14:42:30 +02:00
|
|
|
errored: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2021-11-02 12:01:48 +02:00
|
|
|
enumerable: false,
|
|
|
|
get() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
|
|
|
return state ? state.errored : null;
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2021-11-02 12:01:48 +02:00
|
|
|
},
|
2021-11-13 15:08:14 +02:00
|
|
|
|
|
|
|
writableAborted: {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2021-11-13 15:08:14 +02:00
|
|
|
get: function() {
|
2023-09-29 20:13:44 +02:00
|
|
|
const state = this._writableState;
|
|
|
|
return (
|
2023-10-14 23:50:30 +02:00
|
|
|
(state[kState] & (kHasWritable | kWritable)) !== kHasWritable &&
|
|
|
|
(state[kState] & (kDestroyed | kErrored)) !== 0 &&
|
|
|
|
(state[kState] & kFinished) === 0
|
2021-11-13 15:08:14 +02:00
|
|
|
);
|
2023-02-18 18:53:57 +01:00
|
|
|
},
|
2021-11-13 15:08:14 +02:00
|
|
|
},
|
2020-09-16 19:05:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const destroy = destroyImpl.destroy;
|
|
|
|
Writable.prototype.destroy = function(err, cb) {
|
|
|
|
const state = this._writableState;
|
|
|
|
|
|
|
|
// Invoke pending callbacks.
|
2023-10-26 15:52:53 +02:00
|
|
|
if ((state[kState] & (kBuffered | kOnFinished)) !== 0 && (state[kState] & kDestroyed) === 0) {
|
2020-09-16 19:05:06 +02:00
|
|
|
process.nextTick(errorBuffer, state);
|
|
|
|
}
|
|
|
|
|
2021-04-15 15:05:42 +02:00
|
|
|
destroy.call(this, err, cb);
|
2020-09-16 19:05:06 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Writable.prototype._undestroy = destroyImpl.undestroy;
|
|
|
|
Writable.prototype._destroy = function(err, cb) {
|
|
|
|
cb(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
Writable.prototype[EE.captureRejectionSymbol] = function(err) {
|
|
|
|
this.destroy(err);
|
|
|
|
};
|
2021-06-23 22:24:19 -07:00
|
|
|
|
|
|
|
let webStreamsAdapters;
|
|
|
|
|
|
|
|
// Lazy to avoid circular references
|
|
|
|
function lazyWebStreams() {
|
|
|
|
if (webStreamsAdapters === undefined)
|
|
|
|
webStreamsAdapters = require('internal/webstreams/adapters');
|
|
|
|
return webStreamsAdapters;
|
|
|
|
}
|
|
|
|
|
|
|
|
Writable.fromWeb = function(writableStream, options) {
|
|
|
|
return lazyWebStreams().newStreamWritableFromWritableStream(
|
|
|
|
writableStream,
|
|
|
|
options);
|
|
|
|
};
|
|
|
|
|
|
|
|
Writable.toWeb = function(streamWritable) {
|
|
|
|
return lazyWebStreams().newWritableStreamFromStreamWritable(streamWritable);
|
|
|
|
};
|