| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -716,35 +716,39 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |||
| 716 | 716 | ondrain(); | |
| 717 | 717 | } | |
| 718 | 718 | ||
| 719 | + function pause() { | ||
| 720 | + // If the user unpiped during `dest.write()`, it is possible | ||
| 721 | + // to get stuck in a permanently paused state if that write | ||
| 722 | + // also returned false. | ||
| 723 | + // => Check whether `dest` is still a piping destination. | ||
| 724 | + if (!cleanedUp) { | ||
| 725 | + if (state.pipes.length === 1 && state.pipes[0] === dest) { | ||
| 726 | + debug('false write response, pause', 0); | ||
| 727 | + state.awaitDrainWriters = dest; | ||
| 728 | + state.multiAwaitDrain = false; | ||
| 729 | + } else if (state.pipes.length > 1 && state.pipes.includes(dest)) { | ||
| 730 | + debug('false write response, pause', state.awaitDrainWriters.size); | ||
| 731 | + state.awaitDrainWriters.add(dest); | ||
| 732 | + } | ||
| 733 | + src.pause(); | ||
| 734 | + } | ||
| 735 | + if (!ondrain) { | ||
| 736 | + // When the dest drains, it reduces the awaitDrain counter | ||
| 737 | + // on the source. This would be more elegant with a .once() | ||
| 738 | + // handler in flow(), but adding and removing repeatedly is | ||
| 739 | + // too slow. | ||
| 740 | + ondrain = pipeOnDrain(src, dest); | ||
| 741 | + dest.on('drain', ondrain); | ||
| 742 | + } | ||
| 743 | + } | ||
| 744 | + | ||
| 719 | 745 | src.on('data', ondata); | |
| 720 | 746 | function ondata(chunk) { | |
| 721 | 747 | debug('ondata'); | |
| 722 | 748 | const ret = dest.write(chunk); | |
| 723 | 749 | debug('dest.write', ret); | |
| 724 | 750 | if (ret === false) { | |
| 725 | - // If the user unpiped during `dest.write()`, it is possible | ||
| 726 | - // to get stuck in a permanently paused state if that write | ||
| 727 | - // also returned false. | ||
| 728 | - // => Check whether `dest` is still a piping destination. | ||
| 729 | - if (!cleanedUp) { | ||
| 730 | - if (state.pipes.length === 1 && state.pipes[0] === dest) { | ||
| 731 | - debug('false write response, pause', 0); | ||
| 732 | - state.awaitDrainWriters = dest; | ||
| 733 | - state.multiAwaitDrain = false; | ||
| 734 | - } else if (state.pipes.length > 1 && state.pipes.includes(dest)) { | ||
| 735 | - debug('false write response, pause', state.awaitDrainWriters.size); | ||
| 736 | - state.awaitDrainWriters.add(dest); | ||
| 737 | - } | ||
| 738 | - src.pause(); | ||
| 739 | - } | ||
| 740 | - if (!ondrain) { | ||
| 741 | - // When the dest drains, it reduces the awaitDrain counter | ||
| 742 | - // on the source. This would be more elegant with a .once() | ||
| 743 | - // handler in flow(), but adding and removing repeatedly is | ||
| 744 | - // too slow. | ||
| 745 | - ondrain = pipeOnDrain(src, dest); | ||
| 746 | - dest.on('drain', ondrain); | ||
| 747 | - } | ||
| 751 | + pause(); | ||
| 748 | 752 | } | |
| 749 | 753 | } | |
| 750 | 754 | ||
@@ -793,7 +797,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |||
| 793 | 797 | ||
| 794 | 798 | if (dest.writableNeedDrain === true) { | |
| 795 | 799 | if (state.flowing) { | |
| 796 | - src.pause(); | ||
| 800 | + pause(); | ||
| 797 | 801 | } | |
| 798 | 802 | } else if (!state.flowing) { | |
| 799 | 803 | debug('pipe resume'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,12 +5,13 @@ const assert = require('assert'); | |||
| 5 | 5 | const Readable = require('_stream_readable'); | |
| 6 | 6 | const Writable = require('_stream_writable'); | |
| 7 | 7 | ||
| 8 | - // Pipe should not continue writing if writable needs drain. | ||
| 8 | + // Pipe should pause temporarily if writable needs drain. | ||
| 9 | 9 | { | |
| 10 | 10 | const w = new Writable({ | |
| 11 | 11 | write(buf, encoding, callback) { | |
| 12 | - | ||
| 13 | - } | ||
| 12 | + process.nextTick(callback); | ||
| 13 | + }, | ||
| 14 | + highWaterMark: 1 | ||
| 14 | 15 | }); | |
| 15 | 16 | ||
| 16 | 17 | while (w.write('asd')); | |
@@ -20,10 +21,12 @@ const Writable = require('_stream_writable'); | |||
| 20 | 21 | const r = new Readable({ | |
| 21 | 22 | read() { | |
| 22 | 23 | this.push('asd'); | |
| 24 | + this.push(null); | ||
| 23 | 25 | } | |
| 24 | 26 | }); | |
| 25 | 27 | ||
| 26 | - w.write = common.mustNotCall(); | ||
| 28 | + r.on('pause', common.mustCall(2)); | ||
| 29 | + r.on('end', common.mustCall()); | ||
| 27 | 30 | ||
| 28 | 31 | r.pipe(w); | |
| 29 | 32 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments