| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5bed327 commit 824dc57
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,8 +97,7 @@ function ReadableState(options, stream, isDuplex) { | |||
| 97 | 97 | // array.shift() | |
| 98 | 98 | this.buffer = new BufferList(); | |
| 99 | 99 | this.length = 0; | |
| 100 | - this.pipes = null; | ||
| 101 | - this.pipesCount = 0; | ||
| 100 | + this.pipes = []; | ||
| 102 | 101 | this.flowing = null; | |
| 103 | 102 | this.ended = false; | |
| 104 | 103 | this.endEmitted = false; | |
@@ -148,6 +147,13 @@ function ReadableState(options, stream, isDuplex) { | |||
| 148 | 147 | } | |
| 149 | 148 | } | |
| 150 | 149 | ||
| 150 | + // Legacy getter for `pipesCount` | ||
| 151 | + Object.defineProperty(ReadableState.prototype, 'pipesCount', { | ||
| 152 | + get() { | ||
| 153 | + return this.pipes.length; | ||
| 154 | + } | ||
| 155 | + }); | ||
| 156 | + | ||
| 151 | 157 | function Readable(options) { | |
| 152 | 158 | if (!(this instanceof Readable)) | |
| 153 | 159 | return new Readable(options); | |
@@ -635,19 +641,8 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |||
| 635 | 641 | const src = this; | |
| 636 | 642 | const state = this._readableState; | |
| 637 | 643 | ||
| 638 | - switch (state.pipesCount) { | ||
| 639 | - case 0: | ||
| 640 | - state.pipes = dest; | ||
| 641 | - break; | ||
| 642 | - case 1: | ||
| 643 | - state.pipes = [state.pipes, dest]; | ||
| 644 | - break; | ||
| 645 | - default: | ||
| 646 | - state.pipes.push(dest); | ||
| 647 | - break; | ||
| 648 | - } | ||
| 649 | - state.pipesCount += 1; | ||
| 650 | - debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); | ||
| 644 | + state.pipes.push(dest); | ||
| 645 | + debug('pipe count=%d opts=%j', state.pipes.length, pipeOpts); | ||
| 651 | 646 | ||
| 652 | 647 | const doEnd = (!pipeOpts || pipeOpts.end !== false) && | |
| 653 | 648 | dest !== process.stdout && | |
@@ -717,9 +712,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |||
| 717 | 712 | // to get stuck in a permanently paused state if that write | |
| 718 | 713 | // also returned false. | |
| 719 | 714 | // => Check whether `dest` is still a piping destination. | |
| 720 | - if (((state.pipesCount === 1 && state.pipes === dest) || | ||
| 721 | - (state.pipesCount > 1 && state.pipes.includes(dest))) && | ||
| 722 | - !cleanedUp) { | ||
| 715 | + if (state.pipes.length > 0 && state.pipes.includes(dest) && !cleanedUp) { | ||
| 723 | 716 | debug('false write response, pause', state.awaitDrain); | |
| 724 | 717 | state.awaitDrain++; | |
| 725 | 718 | } | |
@@ -789,38 +782,16 @@ Readable.prototype.unpipe = function(dest) { | |||
| 789 | 782 | const unpipeInfo = { hasUnpiped: false }; | |
| 790 | 783 | ||
| 791 | 784 | // If we're not piping anywhere, then do nothing. | |
| 792 | - if (state.pipesCount === 0) | ||
| 785 | + if (state.pipes.length === 0) | ||
| 793 | 786 | return this; | |
| 794 | 787 | ||
| 795 | - // Just one destination. most common case. | ||
| 796 | - if (state.pipesCount === 1) { | ||
| 797 | - // Passed in one, but it's not the right one. | ||
| 798 | - if (dest && dest !== state.pipes) | ||
| 799 | - return this; | ||
| 800 | - | ||
| 801 | - if (!dest) | ||
| 802 | - dest = state.pipes; | ||
| 803 | - | ||
| 804 | - // got a match. | ||
| 805 | - state.pipes = null; | ||
| 806 | - state.pipesCount = 0; | ||
| 807 | - state.flowing = false; | ||
| 808 | - if (dest) | ||
| 809 | - dest.emit('unpipe', this, unpipeInfo); | ||
| 810 | - return this; | ||
| 811 | - } | ||
| 812 | - | ||
| 813 | - // Slow case with multiple pipe destinations. | ||
| 814 | - | ||
| 815 | 788 | if (!dest) { | |
| 816 | 789 | // remove all. | |
| 817 | 790 | var dests = state.pipes; | |
| 818 | - var len = state.pipesCount; | ||
| 819 | - state.pipes = null; | ||
| 820 | - state.pipesCount = 0; | ||
| 791 | + state.pipes = []; | ||
| 821 | 792 | state.flowing = false; | |
| 822 | 793 | ||
| 823 | - for (var i = 0; i < len; i++) | ||
| 794 | + for (var i = 0; i < dests.length; i++) | ||
| 824 | 795 | dests[i].emit('unpipe', this, { hasUnpiped: false }); | |
| 825 | 796 | return this; | |
| 826 | 797 | } | |
@@ -831,9 +802,8 @@ Readable.prototype.unpipe = function(dest) { | |||
| 831 | 802 | return this; | |
| 832 | 803 | ||
| 833 | 804 | state.pipes.splice(index, 1); | |
| 834 | - state.pipesCount -= 1; | ||
| 835 | - if (state.pipesCount === 1) | ||
| 836 | - state.pipes = state.pipes[0]; | ||
| 805 | + if (state.pipes.length === 0) | ||
| 806 | + state.flowing = false; | ||
| 837 | 807 | ||
| 838 | 808 | dest.emit('unpipe', this, unpipeInfo); | |
| 839 | 809 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,15 +20,15 @@ const { PassThrough, Writable } = require('stream'); | |||
| 20 | 20 | passThrough.pipe(dest); | |
| 21 | 21 | ||
| 22 | 22 | assert.strictEqual(passThrough._events.data.length, 2); | |
| 23 | - assert.strictEqual(passThrough._readableState.pipesCount, 2); | ||
| 23 | + assert.strictEqual(passThrough._readableState.pipes.length, 2); | ||
| 24 | 24 | assert.strictEqual(passThrough._readableState.pipes[0], dest); | |
| 25 | 25 | assert.strictEqual(passThrough._readableState.pipes[1], dest); | |
| 26 | 26 | ||
| 27 | 27 | passThrough.unpipe(dest); | |
| 28 | 28 | ||
| 29 | 29 | assert.strictEqual(passThrough._events.data.length, 1); | |
| 30 | - assert.strictEqual(passThrough._readableState.pipesCount, 1); | ||
| 31 | - assert.strictEqual(passThrough._readableState.pipes, dest); | ||
| 30 | + assert.strictEqual(passThrough._readableState.pipes.length, 1); | ||
| 31 | + assert.deepStrictEqual(passThrough._readableState.pipes, [dest]); | ||
| 32 | 32 | ||
| 33 | 33 | passThrough.write('foobar'); | |
| 34 | 34 | passThrough.pipe(dest); | |
@@ -47,7 +47,7 @@ const { PassThrough, Writable } = require('stream'); | |||
| 47 | 47 | passThrough.pipe(dest); | |
| 48 | 48 | ||
| 49 | 49 | assert.strictEqual(passThrough._events.data.length, 2); | |
| 50 | - assert.strictEqual(passThrough._readableState.pipesCount, 2); | ||
| 50 | + assert.strictEqual(passThrough._readableState.pipes.length, 2); | ||
| 51 | 51 | assert.strictEqual(passThrough._readableState.pipes[0], dest); | |
| 52 | 52 | assert.strictEqual(passThrough._readableState.pipes[1], dest); | |
| 53 | 53 | ||
@@ -64,15 +64,15 @@ const { PassThrough, Writable } = require('stream'); | |||
| 64 | 64 | passThrough.pipe(dest); | |
| 65 | 65 | ||
| 66 | 66 | assert.strictEqual(passThrough._events.data.length, 2); | |
| 67 | - assert.strictEqual(passThrough._readableState.pipesCount, 2); | ||
| 67 | + assert.strictEqual(passThrough._readableState.pipes.length, 2); | ||
| 68 | 68 | assert.strictEqual(passThrough._readableState.pipes[0], dest); | |
| 69 | 69 | assert.strictEqual(passThrough._readableState.pipes[1], dest); | |
| 70 | 70 | ||
| 71 | 71 | passThrough.unpipe(dest); | |
| 72 | 72 | passThrough.unpipe(dest); | |
| 73 | 73 | ||
| 74 | 74 | assert.strictEqual(passThrough._events.data, undefined); | |
| 75 | - assert.strictEqual(passThrough._readableState.pipesCount, 0); | ||
| 75 | + assert.strictEqual(passThrough._readableState.pipes.length, 0); | ||
| 76 | 76 | ||
| 77 | 77 | passThrough.write('foobar'); | |
| 78 | 78 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,15 +22,15 @@ assert.strictEqual(source._readableState.pipes.length, 2); | |||
| 22 | 22 | ||
| 23 | 23 | source.unpipe(dest2); | |
| 24 | 24 | ||
| 25 | - assert.strictEqual(source._readableState.pipes, dest1); | ||
| 25 | + assert.deepStrictEqual(source._readableState.pipes, [dest1]); | ||
| 26 | 26 | assert.notStrictEqual(source._readableState.pipes, dest2); | |
| 27 | 27 | ||
| 28 | 28 | dest2.on('unpipe', common.mustNotCall()); | |
| 29 | 29 | source.unpipe(dest2); | |
| 30 | 30 | ||
| 31 | 31 | source.unpipe(dest1); | |
| 32 | 32 | ||
| 33 | - assert.strictEqual(source._readableState.pipes, null); | ||
| 33 | + assert.strictEqual(source._readableState.pipes.length, 0); | ||
| 34 | 34 | ||
| 35 | 35 | { | |
| 36 | 36 | // Test `cleanup()` if we unpipe all streams. | |
@@ -43,8 +43,7 @@ assert.strictEqual(source._readableState.pipes, null); | |||
| 43 | 43 | const destCheckEventNames = ['close', 'finish', 'drain', 'error', 'unpipe']; | |
| 44 | 44 | ||
| 45 | 45 | const checkSrcCleanup = common.mustCall(() => { | |
| 46 | - assert.strictEqual(source._readableState.pipes, null); | ||
| 47 | - assert.strictEqual(source._readableState.pipesCount, 0); | ||
| 46 | + assert.strictEqual(source._readableState.pipes.length, 0); | ||
| 48 | 47 | assert.strictEqual(source._readableState.flowing, false); | |
| 49 | 48 | ||
| 50 | 49 | srcCheckEventNames.forEach((eventName) => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,7 +23,7 @@ class NeverEndReadable extends Readable { | |||
| 23 | 23 | dest.on('unpipe', common.mustCall()); | |
| 24 | 24 | src.pipe(dest); | |
| 25 | 25 | setImmediate(() => { | |
| 26 | - assert.strictEqual(src._readableState.pipesCount, 0); | ||
| 26 | + assert.strictEqual(src._readableState.pipes.length, 0); | ||
| 27 | 27 | }); | |
| 28 | 28 | } | |
| 29 | 29 | ||
@@ -34,7 +34,7 @@ class NeverEndReadable extends Readable { | |||
| 34 | 34 | dest.on('unpipe', common.mustNotCall('unpipe should not have been emitted')); | |
| 35 | 35 | src.pipe(dest); | |
| 36 | 36 | setImmediate(() => { | |
| 37 | - assert.strictEqual(src._readableState.pipesCount, 1); | ||
| 37 | + assert.strictEqual(src._readableState.pipes.length, 1); | ||
| 38 | 38 | }); | |
| 39 | 39 | } | |
| 40 | 40 | ||
@@ -46,7 +46,7 @@ class NeverEndReadable extends Readable { | |||
| 46 | 46 | src.pipe(dest); | |
| 47 | 47 | src.unpipe(dest); | |
| 48 | 48 | setImmediate(() => { | |
| 49 | - assert.strictEqual(src._readableState.pipesCount, 0); | ||
| 49 | + assert.strictEqual(src._readableState.pipes.length, 0); | ||
| 50 | 50 | }); | |
| 51 | 51 | } | |
| 52 | 52 | ||
@@ -57,7 +57,7 @@ class NeverEndReadable extends Readable { | |||
| 57 | 57 | dest.on('unpipe', common.mustCall()); | |
| 58 | 58 | src.pipe(dest, { end: false }); | |
| 59 | 59 | setImmediate(() => { | |
| 60 | - assert.strictEqual(src._readableState.pipesCount, 0); | ||
| 60 | + assert.strictEqual(src._readableState.pipes.length, 0); | ||
| 61 | 61 | }); | |
| 62 | 62 | } | |
| 63 | 63 | ||
@@ -68,7 +68,7 @@ class NeverEndReadable extends Readable { | |||
| 68 | 68 | dest.on('unpipe', common.mustNotCall('unpipe should not have been emitted')); | |
| 69 | 69 | src.pipe(dest, { end: false }); | |
| 70 | 70 | setImmediate(() => { | |
| 71 | - assert.strictEqual(src._readableState.pipesCount, 1); | ||
| 71 | + assert.strictEqual(src._readableState.pipes.length, 1); | ||
| 72 | 72 | }); | |
| 73 | 73 | } | |
| 74 | 74 | ||
@@ -80,6 +80,6 @@ class NeverEndReadable extends Readable { | |||
| 80 | 80 | src.pipe(dest, { end: false }); | |
| 81 | 81 | src.unpipe(dest); | |
| 82 | 82 | setImmediate(() => { | |
| 83 | - assert.strictEqual(src._readableState.pipesCount, 0); | ||
| 83 | + assert.strictEqual(src._readableState.pipes.length, 0); | ||
| 84 | 84 | }); | |
| 85 | 85 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,10 +171,10 @@ class TestWriter extends EE { | |||
| 171 | 171 | w[0].on('write', function() { | |
| 172 | 172 | if (--writes === 0) { | |
| 173 | 173 | r.unpipe(); | |
| 174 | - assert.strictEqual(r._readableState.pipes, null); | ||
| 174 | + assert.deepStrictEqual(r._readableState.pipes, []); | ||
| 175 | 175 | w[0].end(); | |
| 176 | 176 | r.pipe(w[1]); | |
| 177 | - assert.strictEqual(r._readableState.pipes, w[1]); | ||
| 177 | + assert.deepStrictEqual(r._readableState.pipes, [w[1]]); | ||
| 178 | 178 | } | |
| 179 | 179 | }); | |
| 180 | 180 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments