GeezerVison is showing github.com — stream: reject unbounded at SyncShare construction · nodejs/node@7e1ae17
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/share.js +15-10Lines changed: 15 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,7 @@ const { | 54 | 54 | const { | 55 | 55 | codes: { | 56 | 56 | ERR_INVALID_ARG_TYPE, | | 57 | + ERR_INVALID_ARG_VALUE, | 57 | 58 | ERR_INVALID_RETURN_VALUE, | 58 | 59 | ERR_OUT_OF_RANGE, | 59 | 60 | }, | @@ -581,11 +582,6 @@ class SyncShareImpl { | 581 | 582 | throw new ERR_OUT_OF_RANGE( | 582 | 583 | 'buffered bytes', `< ${self.#options.budget}`, | 583 | 584 | self.#bufferedBytes); | 584 | | - case 'unbounded': | 585 | | - throw new ERR_OUT_OF_RANGE( | 586 | | - 'buffered bytes', `< ${self.#options.budget} ` + | 587 | | - '(unbounded not available in sync context)', | 588 | | - self.#bufferedBytes); | 589 | 585 | case 'drop-oldest': | 590 | 586 | while (self.#bufferedBytes >= self.#options.budget && | 591 | 587 | self.#buffer.length > 0) { | @@ -602,9 +598,13 @@ class SyncShareImpl { | 602 | 598 | self.#recomputeMinCursor(); | 603 | 599 | break; | 604 | 600 | case 'drop-newest': | 605 | | - state.detached = true; | 606 | | - self.#deleteConsumer(state); | 607 | | - return { __proto__: null, done: true, value: undefined }; | | 601 | + while (self.#bufferedBytes >= self.#options.budget && | | 602 | + !self.#sourceExhausted && | | 603 | + !self.#cancelled && | | 604 | + self.#sourceError === kNoShareError) { | | 605 | + self.#pullFromSource(true); | | 606 | + } | | 607 | + break; | 608 | 608 | } | 609 | 609 | } | 610 | 610 |
| @@ -679,7 +679,7 @@ class SyncShareImpl { | 679 | 679 | this.cancel(); | 680 | 680 | } | 681 | 681 |
| 682 | | - #pullFromSource() { | | 682 | + #pullFromSource(discard = false) { | 683 | 683 | if (this.#sourceExhausted || this.#cancelled) return; | 684 | 684 |
| 685 | 685 | try { | @@ -689,7 +689,7 @@ class SyncShareImpl { | 689 | 689 |
| 690 | 690 | if (result.done) { | 691 | 691 | this.#sourceExhausted = true; | 692 | | - } else { | | 692 | + } else if (!discard) { | 693 | 693 | const entry = createBatchEntry(result.value); | 694 | 694 | this.#buffer.push(entry); | 695 | 695 | this.#bufferedBytes += entry.byteLength; | @@ -803,6 +803,11 @@ function shareSync(source, options = { __proto__: null }) { | 803 | 803 | } = options; | 804 | 804 | validateInteger(budget, 'options.budget', 16384); | 805 | 805 | validateBackpressure(backpressure); | | 806 | + if (backpressure === 'unbounded') { | | 807 | + throw new ERR_INVALID_ARG_VALUE( | | 808 | + 'options.backpressure', backpressure, | | 809 | + 'unbounded is not supported by shareSync()'); | | 810 | + } | 806 | 811 |
| 807 | 812 | const opts = { | 808 | 813 | __proto__: null, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-share-sync.js +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -139,6 +139,39 @@ function testShareSyncSourceError() { | 139 | 139 | }, { message: 'sync share boom' }); | 140 | 140 | } | 141 | 141 |
| | 142 | +function testShareSyncRejectsUnbounded() { | | 143 | + assert.throws( | | 144 | + () => shareSync(fromSync('data'), { backpressure: 'unbounded' }), | | 145 | + { code: 'ERR_INVALID_ARG_VALUE' }, | | 146 | + ); | | 147 | +} | | 148 | + | | 149 | +function testShareSyncDropNewest() { | | 150 | + let pulls = 0; | | 151 | + function* source() { | | 152 | + for (let i = 0; i < 3; i++) { | | 153 | + pulls++; | | 154 | + const chunk = new Uint8Array(16384); | | 155 | + chunk[0] = i; | | 156 | + yield [chunk]; | | 157 | + } | | 158 | + } | | 159 | + | | 160 | + const shared = shareSync(source(), { | | 161 | + budget: 16384, | | 162 | + backpressure: 'drop-newest', | | 163 | + }); | | 164 | + const fast = shared.pull()[Symbol.iterator](); | | 165 | + const slow = shared.pull()[Symbol.iterator](); | | 166 | + | | 167 | + assert.strictEqual(fast.next().value[0][0], 0); | | 168 | + assert.strictEqual(fast.next().done, true); | | 169 | + assert.strictEqual(pulls, 3); | | 170 | + | | 171 | + assert.strictEqual(slow.next().value[0][0], 0); | | 172 | + assert.strictEqual(slow.next().done, true); | | 173 | +} | | 174 | + | 142 | 175 | // shareSync() accepts string source directly (normalized via fromSync()) | 143 | 176 | function testShareSyncRetainsBufferWhenAllConsumersDetach() { | 144 | 177 | // Data that a consumer had not read yet must stay available to consumers | @@ -174,6 +207,8 @@ Promise.all([ | 174 | 207 | testShareSyncCancelWithReason(), | 175 | 208 | testShareSyncCancelWithFalsyReason(), | 176 | 209 | testShareSyncSourceError(), | | 210 | + testShareSyncRejectsUnbounded(), | | 211 | + testShareSyncDropNewest(), | 177 | 212 | testShareSyncStringSource(), | 178 | 213 | testShareSyncRetainsBufferWhenAllConsumersDetach(), | 179 | 214 | ]).then(common.mustCall()); |
|