GeezerVison is showing github.com — stream: ensure that stateful transforms preserve this · nodejs/node@649a4ab
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/pull.js +15-9Lines changed: 15 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -505,8 +505,9 @@ function* withFlushSync(source) { | 505 | 505 | yield null; | 506 | 506 | } | 507 | 507 |
| 508 | | -function* applyStatefulSyncTransform(source, transform) { | 509 | | - const output = transform(withFlushSync(source)); | | 508 | +function* applyStatefulSyncTransform(source, transform, receiver) { | | 509 | + const output = FunctionPrototypeCall( | | 510 | + transform, receiver, withFlushSync(source)); | 510 | 511 | for (const item of output) { | 511 | 512 | if (item === null) continue; | 512 | 513 | const batch = []; | @@ -537,7 +538,8 @@ function* createSyncPipeline(source, transforms) { | 537 | 538 | current = applyFusedStatelessSyncTransforms(current, statelessRun); | 538 | 539 | statelessRun = []; | 539 | 540 | } | 540 | | - current = applyStatefulSyncTransform(current, transform.transform); | | 541 | + current = applyStatefulSyncTransform( | | 542 | + current, transform.transform, transform); | 541 | 543 | } else { | 542 | 544 | ArrayPrototypePush(statelessRun, transform); | 543 | 545 | } | @@ -648,8 +650,10 @@ async function* withFlushAsync(source) { | 648 | 650 | yield null; | 649 | 651 | } | 650 | 652 |
| 651 | | -async function* applyStatefulAsyncTransform(source, transform, options) { | 652 | | - const output = transform(withFlushAsync(source), options); | | 653 | +async function* applyStatefulAsyncTransform( | | 654 | + source, transform, receiver, options) { | | 655 | + const output = FunctionPrototypeCall( | | 656 | + transform, receiver, withFlushAsync(source), options); | 653 | 657 | for await (const item of output) { | 654 | 658 | if (item === null) continue; | 655 | 659 | // Fast path: item is already a Uint8Array[] batch (e.g. compression transforms) | @@ -681,8 +685,10 @@ async function* applyStatefulAsyncTransform(source, transform, options) { | 681 | 685 | * skips isUint8ArrayBatch validation (transform guarantees valid output). | 682 | 686 | * @yields {Uint8Array[]} | 683 | 687 | */ | 684 | | -async function* applyValidatedStatefulAsyncTransform(source, transform, options) { | 685 | | - const output = transform(source, options); | | 688 | +async function* applyValidatedStatefulAsyncTransform( | | 689 | + source, transform, receiver, options) { | | 690 | + const output = FunctionPrototypeCall( | | 691 | + transform, receiver, source, options); | 686 | 692 | for await (const batch of output) { | 687 | 693 | if (batch.length > 0) { | 688 | 694 | yield batch; | @@ -750,10 +756,10 @@ async function* createAsyncPipeline(source, transforms, signal) { | 750 | 756 | const opts = { __proto__: null, signal: transformSignal }; | 751 | 757 | if (transform[kValidatedTransform]) { | 752 | 758 | current = applyValidatedStatefulAsyncTransform( | 753 | | - current, transform.transform, opts); | | 759 | + current, transform.transform, transform, opts); | 754 | 760 | } else { | 755 | 761 | current = applyStatefulAsyncTransform( | 756 | | - current, transform.transform, opts); | | 762 | + current, transform.transform, transform, opts); | 757 | 763 | } | 758 | 764 | } else { | 759 | 765 | ArrayPrototypePush(statelessRun, transform); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pull-async.js +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,6 +49,19 @@ async function testPullStatefulTransform() { | 49 | 49 | assert.strictEqual(data, 'data-ASYNC-END'); | 50 | 50 | } | 51 | 51 |
| | 52 | +async function testPullStatefulTransformReceiver() { | | 53 | + const descriptor = {}; | | 54 | + descriptor.transform = common.mustCall( | | 55 | + async function*(source) { | | 56 | + assert.strictEqual(this, descriptor); | | 57 | + for await (const chunks of source) { | | 58 | + yield chunks; | | 59 | + } | | 60 | + }); | | 61 | + | | 62 | + assert.strictEqual(await text(pull(from('receiver'), descriptor)), 'receiver'); | | 63 | +} | | 64 | + | 52 | 65 | async function testPullWithAbortSignal() { | 53 | 66 | async function* gen() { | 54 | 67 | yield [new Uint8Array([1])]; | @@ -511,6 +524,7 @@ async function testTransformOptionsNotShared() { | 511 | 524 | testPullIdentity(), | 512 | 525 | testPullStatelessTransform(), | 513 | 526 | testPullStatefulTransform(), | | 527 | + testPullStatefulTransformReceiver(), | 514 | 528 | testPullWithAbortSignal(), | 515 | 529 | testPullNormalizesSourceAtCallTime(), | 516 | 530 | testPullPreAbortOrdering(), | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pull-sync.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,6 +74,20 @@ function testPullSyncStatefulTransform() { | 74 | 74 | assert.strictEqual(data, 'data-END'); | 75 | 75 | } | 76 | 76 |
| | 77 | +function testPullSyncStatefulTransformReceiver() { | | 78 | + const descriptor = {}; | | 79 | + descriptor.transform = common.mustCall( | | 80 | + function*(source) { | | 81 | + assert.strictEqual(this, descriptor); | | 82 | + yield* source; | | 83 | + }); | | 84 | + | | 85 | + assert.strictEqual( | | 86 | + new TextDecoder().decode(bytesSync(pullSync(fromSync('receiver'), descriptor))), | | 87 | + 'receiver', | | 88 | + ); | | 89 | +} | | 90 | + | 77 | 91 | function testPullSyncChainedTransforms() { | 78 | 92 | const addExcl = (chunks) => { | 79 | 93 | if (chunks === null) return null; | @@ -210,6 +224,7 @@ Promise.all([ | 210 | 224 | testPullSyncNormalizesSourceAtCallTime(), | 211 | 225 | testPullSyncStatelessTransform(), | 212 | 226 | testPullSyncStatefulTransform(), | | 227 | + testPullSyncStatefulTransformReceiver(), | 213 | 228 | testPullSyncChainedTransforms(), | 214 | 229 | testPullSyncSourceError(), | 215 | 230 | testPullSyncEmptySource(), | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-transform-roundtrip.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,18 @@ async function testGzipRoundTrip() { | 42 | 42 | assert.strictEqual(result, input); | 43 | 43 | } | 44 | 44 |
| | 45 | +async function testValidatedTransformReceiver() { | | 46 | + const descriptor = compressGzip(); | | 47 | + const transform = descriptor.transform; | | 48 | + descriptor.transform = common.mustCall(function(source, options) { | | 49 | + assert.strictEqual(this, descriptor); | | 50 | + return Reflect.apply(transform, this, [source, options]); | | 51 | + }); | | 52 | + | | 53 | + const result = await bytes(pull(from('receiver'), descriptor)); | | 54 | + assert.ok(result.byteLength > 0); | | 55 | +} | | 56 | + | 45 | 57 | async function testGzipLargeData() { | 46 | 58 | // 100KB of repeated text - exercises multi-chunk path | 47 | 59 | const input = 'gzip large data test. '.repeat(5000); | @@ -250,6 +262,7 @@ async function testGzipWithLevel() { | 250 | 262 | (async () => { | 251 | 263 | // Gzip | 252 | 264 | await testGzipRoundTrip(); | | 265 | + await testValidatedTransformReceiver(); | 253 | 266 | await testGzipLargeData(); | 254 | 267 | await testGzipActuallyCompresses(); | 255 | 268 |
| |
|