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

Function _write

lib/internal/streams/writable.js:450–502  ·  view source on GitHub ↗
(stream, chunk, encoding, cb)

Source from the content-addressed store, hash-verified

448};
449
450function _write(stream, chunk, encoding, cb) {
451 const state = stream._writableState;
452
453 if (cb == null || typeof cb !== 'function') {
454 cb = nop;
455 }
456
457 if (chunk === null) {
458 throw new ERR_STREAM_NULL_VALUES();
459 }
460
461 if ((state[kState] & kObjectMode) === 0) {
462 if (!encoding) {
463 encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state.defaultEncoding;
464 } else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding)) {
465 throw new ERR_UNKNOWN_ENCODING(encoding);
466 }
467
468 if (typeof chunk === 'string') {
469 if (encoding === 'buffer') {
470 throw new ERR_UNKNOWN_ENCODING(encoding);
471 }
472 if ((state[kState] & kDecodeStrings) !== 0) {
473 chunk = Buffer.from(chunk, encoding);
474 encoding = 'buffer';
475 }
476 } else if (chunk instanceof Buffer) {
477 encoding = 'buffer';
478 } else if (Stream._isArrayBufferView(chunk)) {
479 chunk = Stream._uint8ArrayToBuffer(chunk);
480 encoding = 'buffer';
481 } else {
482 throw new ERR_INVALID_ARG_TYPE(
483 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk);
484 }
485 }
486
487 let err;
488 if ((state[kState] & kEnding) !== 0) {
489 err = new ERR_STREAM_WRITE_AFTER_END();
490 } else if ((state[kState] & kDestroyed) !== 0) {
491 err = new ERR_STREAM_DESTROYED('write');
492 }
493
494 if (err) {
495 process.nextTick(cb, err);
496 errorOrDestroy(stream, err, true);
497 return err;
498 }
499
500 state.pendingcb++;
501 return writeOrBuffer(stream, state, chunk, encoding, cb);
502}
503
504Writable.prototype.write = function(chunk, encoding, cb) {
505 if (encoding != null && typeof encoding === 'function') {

Callers 1

writable.jsFile · 0.70

Calls 3

errorOrDestroyFunction · 0.85
writeOrBufferFunction · 0.85
fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…