GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: forward compose tail error after finish · nodejs/node@f5264fa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/streams
    • compose.js
  • test/parallel
    • test-stream-compose-tail-error-after-finish.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/streams/compose.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/compose.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ module.exports = function compose(...streams) {
196196
tail.on('end', function() {
197197
d.push(null);
198198
});
199+
200+
tail.on('error', function(err) {
201+
if (!d.destroyed) {
202+
d.destroy(err);
203+
}
204+
});
199205
} else if (isWebStream(tail)) {
200206
const readable = isTransformStream(tail) ? tail.readable : tail;
201207
const reader = readable.getReader();
Collapse file

‎test/parallel/test-stream-compose-tail-error-after-finish.js‎

Copy file name to clipboard
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const {
6+
compose,
7+
Duplex,
8+
Readable,
9+
} = require('stream');
10+
11+
// Returns a Duplex whose writable side finishes (emitting 'finish') but whose
12+
// readable side then errors instead of ending. This mimics a zlib decompressor
13+
// that only detects truncated input during its final flush, which runs after the
14+
// writable side has already finished, so 'end' is never emitted.
15+
function createTail() {
16+
return new Duplex({
17+
write(chunk, encoding, callback) {
18+
callback();
19+
},
20+
final(callback) {
21+
callback();
22+
setImmediate(() => this.destroy(new Error('readable-side failure after finish')));
23+
},
24+
read() {},
25+
});
26+
}
27+
28+
// Regression test: an error emitted by the last stream of compose() after its
29+
// writable side has finished must be forwarded to the composed Duplex, rather
30+
// than becoming an unhandled 'error' event on the tail.
31+
{
32+
const composed = compose(Readable.from([Buffer.from('x')]), createTail());
33+
34+
composed.on('error', common.mustCall((err) => {
35+
assert.strictEqual(err.message, 'readable-side failure after finish');
36+
}));
37+
composed.resume();
38+
}
39+
40+
// The same error must reject a `for await` consumer of the composed stream.
41+
{
42+
const composed = compose(Readable.from([Buffer.from('x')]), createTail());
43+
44+
(async () => {
45+
try {
46+
for await (const chunk of composed) {
47+
assert.ok(chunk);
48+
}
49+
assert.fail('should have thrown');
50+
} catch (err) {
51+
assert.strictEqual(err.message, 'readable-side failure after finish');
52+
}
53+
})().then(common.mustCall());
54+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.