stream: make sure 'readable' is emitted before ending the stream · nodejs/node@0b2f900 (original) (raw)
``
1
`+
'use strict';
`
``
2
+
``
3
`+
const common = require('../common');
`
``
4
`+
const stream = require('stream');
`
``
5
`+
const assert = require('assert');
`
``
6
+
``
7
`+
{
`
``
8
`+
const r = new stream.Readable({
`
``
9
`+
read: common.mustCall(function() {
`
``
10
`+
this.push('content');
`
``
11
`+
this.push(null);
`
``
12
`+
})
`
``
13
`+
});
`
``
14
+
``
15
`+
const t = new stream.Transform({
`
``
16
`+
transform: common.mustCall(function(chunk, encoding, callback) {
`
``
17
`+
this.push(chunk);
`
``
18
`+
return callback();
`
``
19
`+
}),
`
``
20
`+
flush: common.mustCall(function(callback) {
`
``
21
`+
return callback();
`
``
22
`+
})
`
``
23
`+
});
`
``
24
+
``
25
`+
r.pipe(t);
`
``
26
`+
t.on('readable', common.mustCall(function() {
`
``
27
`+
while (true) {
`
``
28
`+
const chunk = t.read();
`
``
29
`+
if (!chunk)
`
``
30
`+
break;
`
``
31
+
``
32
`+
assert.strictEqual(chunk.toString(), 'content');
`
``
33
`+
}
`
``
34
`+
}, 2));
`
``
35
`+
}
`
``
36
+
``
37
`+
{
`
``
38
`+
const t = new stream.Transform({
`
``
39
`+
transform: common.mustCall(function(chunk, encoding, callback) {
`
``
40
`+
this.push(chunk);
`
``
41
`+
return callback();
`
``
42
`+
}),
`
``
43
`+
flush: common.mustCall(function(callback) {
`
``
44
`+
return callback();
`
``
45
`+
})
`
``
46
`+
});
`
``
47
+
``
48
`+
t.end('content');
`
``
49
+
``
50
`+
t.on('readable', common.mustCall(function() {
`
``
51
`+
while (true) {
`
``
52
`+
const chunk = t.read();
`
``
53
`+
if (!chunk)
`
``
54
`+
break;
`
``
55
`+
assert.strictEqual(chunk.toString(), 'content');
`
``
56
`+
}
`
``
57
`+
}, 2));
`
``
58
`+
}
`
``
59
+
``
60
`+
{
`
``
61
`+
const t = new stream.Transform({
`
``
62
`+
transform: common.mustCall(function(chunk, encoding, callback) {
`
``
63
`+
this.push(chunk);
`
``
64
`+
return callback();
`
``
65
`+
}),
`
``
66
`+
flush: common.mustCall(function(callback) {
`
``
67
`+
return callback();
`
``
68
`+
})
`
``
69
`+
});
`
``
70
+
``
71
`+
t.write('content');
`
``
72
`+
t.end();
`
``
73
+
``
74
`+
t.on('readable', common.mustCall(function() {
`
``
75
`+
while (true) {
`
``
76
`+
const chunk = t.read();
`
``
77
`+
if (!chunk)
`
``
78
`+
break;
`
``
79
`+
assert.strictEqual(chunk.toString(), 'content');
`
``
80
`+
}
`
``
81
`+
}, 2));
`
``
82
`+
}
`
``
83
+
``
84
`+
{
`
``
85
`+
const t = new stream.Readable({
`
``
86
`+
read() {
`
``
87
`+
}
`
``
88
`+
});
`
``
89
+
``
90
`+
t.on('readable', common.mustCall(function() {
`
``
91
`+
while (true) {
`
``
92
`+
const chunk = t.read();
`
``
93
`+
if (!chunk)
`
``
94
`+
break;
`
``
95
`+
assert.strictEqual(chunk.toString(), 'content');
`
``
96
`+
}
`
``
97
`+
}, 2));
`
``
98
+
``
99
`+
t.push('content');
`
``
100
`+
t.push(null);
`
``
101
`+
}
`
``
102
+
``
103
`+
{
`
``
104
`+
const t = new stream.Readable({
`
``
105
`+
read() {
`
``
106
`+
}
`
``
107
`+
});
`
``
108
+
``
109
`+
t.on('readable', common.mustCall(function() {
`
``
110
`+
while (true) {
`
``
111
`+
const chunk = t.read();
`
``
112
`+
if (!chunk)
`
``
113
`+
break;
`
``
114
`+
assert.strictEqual(chunk.toString(), 'content');
`
``
115
`+
}
`
``
116
`+
}, 2));
`
``
117
+
``
118
`+
process.nextTick(() => {
`
``
119
`+
t.push('content');
`
``
120
`+
t.push(null);
`
``
121
`+
});
`
``
122
`+
}
`
``
123
+
``
124
`+
{
`
``
125
`+
const t = new stream.Transform({
`
``
126
`+
transform: common.mustCall(function(chunk, encoding, callback) {
`
``
127
`+
this.push(chunk);
`
``
128
`+
return callback();
`
``
129
`+
}),
`
``
130
`+
flush: common.mustCall(function(callback) {
`
``
131
`+
return callback();
`
``
132
`+
})
`
``
133
`+
});
`
``
134
+
``
135
`+
t.on('readable', common.mustCall(function() {
`
``
136
`+
while (true) {
`
``
137
`+
const chunk = t.read();
`
``
138
`+
if (!chunk)
`
``
139
`+
break;
`
``
140
`+
assert.strictEqual(chunk.toString(), 'content');
`
``
141
`+
}
`
``
142
`+
}, 2));
`
``
143
+
``
144
`+
t.write('content');
`
``
145
`+
t.end();
`
``
146
`+
}
`