MCPcopy Index your code
hub / github.com/nodejs/node / WritableState

Function WritableState

lib/internal/streams/writable.js:301–359  ·  view source on GitHub ↗
(options, stream, isDuplex)

Source from the content-addressed store, hash-verified

299});
300
301function WritableState(options, stream, isDuplex) {
302 // Bit map field to store WritableState more efficiently with 1 bit per field
303 // instead of a V8 slot per field.
304 this[kState] = kSync | kConstructed | kEmitClose | kAutoDestroy;
305
306 if (options?.objectMode)
307 this[kState] |= kObjectMode;
308
309 if (isDuplex && options?.writableObjectMode)
310 this[kState] |= kObjectMode;
311
312 // The point at which write() starts returning false
313 // Note: 0 is a valid value, means that we always return false if
314 // the entire buffer is not flushed immediately on write().
315 this.highWaterMark = options ?
316 getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex) :
317 getDefaultHighWaterMark(false);
318
319 if (!options || options.decodeStrings !== false) this[kState] |= kDecodeStrings;
320
321 // Should close be emitted on destroy. Defaults to true.
322 if (options && options.emitClose === false) this[kState] &= ~kEmitClose;
323
324 // Should .destroy() be called after 'end' (and potentially 'finish').
325 if (options && options.autoDestroy === false) this[kState] &= ~kAutoDestroy;
326
327 // Crypto is kind of old and crusty. Historically, its default string
328 // encoding is 'binary' so we have to make this configurable.
329 // Everything else in the universe uses 'utf8', though.
330 const defaultEncoding = options ? options.defaultEncoding : null;
331 if (defaultEncoding == null || defaultEncoding === 'utf8' || defaultEncoding === 'utf-8') {
332 this[kState] |= kDefaultUTF8Encoding;
333 } else if (Buffer.isEncoding(defaultEncoding)) {
334 this[kState] &= ~kDefaultUTF8Encoding;
335 this[kDefaultEncodingValue] = defaultEncoding;
336 } else {
337 throw new ERR_UNKNOWN_ENCODING(defaultEncoding);
338 }
339
340 // Not an actual buffer we keep track of, but a measurement
341 // of how much we're waiting to get pushed to some underlying
342 // socket or file.
343 this.length = 0;
344
345 // When true all writes will be buffered until .uncork() call.
346 this.corked = 0;
347
348 // The callback that's passed to _write(chunk, cb).
349 this.onwrite = (er) => onwrite(stream, er);
350
351 // The amount that is being written when _write is called.
352 this.writelen = 0;
353
354 resetBuffer(this);
355
356 // Number of pending user-supplied write callbacks
357 // this must be 0 before 'finish' can be emitted.
358 this.pendingcb = 0;

Callers

nothing calls this directly

Calls 4

getHighWaterMarkFunction · 0.85
getDefaultHighWaterMarkFunction · 0.85
onwriteFunction · 0.70
resetBufferFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…