|
27 | 27 |
|
28 | 28 | const {
|
29 | 29 | FunctionPrototype,
|
| 30 | + Error, |
30 | 31 | ObjectDefineProperty,
|
31 | 32 | ObjectDefineProperties,
|
32 | 33 | ObjectSetPrototypeOf,
|
@@ -290,8 +291,8 @@ Writable.prototype.pipe = function() {
|
290 | 291 | errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE());
|
291 | 292 | };
|
292 | 293 |
|
293 |
| -Writable.prototype.write = function(chunk, encoding, cb) { |
294 |
| - const state = this._writableState; |
| 294 | +function _write(stream, chunk, encoding, cb) { |
| 295 | + const state = stream._writableState; |
295 | 296 |
|
296 | 297 | if (typeof encoding === 'function') {
|
297 | 298 | cb = encoding;
|
@@ -333,11 +334,15 @@ Writable.prototype.write = function(chunk, encoding, cb) {
|
333 | 334 |
|
334 | 335 | if (err) {
|
335 | 336 | process.nextTick(cb, err);
|
336 |
| - errorOrDestroy(this, err, true); |
337 |
| - return false; |
| 337 | + errorOrDestroy(stream, err, true); |
| 338 | + return err; |
338 | 339 | }
|
339 | 340 | state.pendingcb++;
|
340 |
| - return writeOrBuffer(this, state, chunk, encoding, cb); |
| 341 | + return writeOrBuffer(stream, state, chunk, encoding, cb); |
| 342 | +} |
| 343 | + |
| 344 | +Writable.prototype.write = function(chunk, encoding, cb) { |
| 345 | + return _write(this, chunk, encoding, cb) === true; |
341 | 346 | };
|
342 | 347 |
|
343 | 348 | Writable.prototype.cork = function() {
|
@@ -607,21 +612,30 @@ Writable.prototype.end = function(chunk, encoding, cb) {
|
607 | 612 | encoding = null;
|
608 | 613 | }
|
609 | 614 |
|
610 |
| - if (chunk !== null && chunk !== undefined) |
611 |
| - this.write(chunk, encoding); |
| 615 | + let err; |
| 616 | + |
| 617 | + if (chunk !== null && chunk !== undefined) { |
| 618 | + const ret = _write(this, chunk, encoding); |
| 619 | + if (ret instanceof Error) { |
| 620 | + err = ret; |
| 621 | + } |
| 622 | + } |
612 | 623 |
|
613 | 624 | // .end() fully uncorks.
|
614 | 625 | if (state.corked) {
|
615 | 626 | state.corked = 1;
|
616 | 627 | this.uncork();
|
617 | 628 | }
|
618 | 629 |
|
619 |
| - // This is forgiving in terms of unnecessary calls to end() and can hide |
620 |
| - // logic errors. However, usually such errors are harmless and causing a |
621 |
| - // hard error can be disproportionately destructive. It is not always |
622 |
| - // trivial for the user to determine whether end() needs to be called or not. |
623 |
| - let err; |
624 |
| - if (!state.errored && !state.ending) { |
| 630 | + if (err) { |
| 631 | + // Do nothing... |
| 632 | + } else if (!state.errored && !state.ending) { |
| 633 | + // This is forgiving in terms of unnecessary calls to end() and can hide |
| 634 | + // logic errors. However, usually such errors are harmless and causing a |
| 635 | + // hard error can be disproportionately destructive. It is not always |
| 636 | + // trivial for the user to determine whether end() needs to be called |
| 637 | + // or not. |
| 638 | + |
625 | 639 | state.ending = true;
|
626 | 640 | finishMaybe(this, state, true);
|
627 | 641 | state.ended = true;
|
|
0 commit comments