Skip to content

Commit 863c211

Browse files
authored
fix: only original value destroy if the new value is not a stream (#1914)
close #1913
1 parent 1c642f4 commit 863c211

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

__tests__/response/body.test.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,16 @@ describe('res.body=', () => {
127127
assert.strictEqual(body.listenerCount('error'), 0)
128128
})
129129

130-
it('should cleanup original stream when replaced by new stream', () => {
130+
it('should NOT cleanup original stream when replaced by new stream (to support wrapping middleware)', () => {
131131
const res = response()
132132
const stream1 = new Stream.PassThrough()
133133
const stream2 = new Stream.PassThrough()
134134

135135
res.body = stream1
136136
res.body = stream2
137137

138-
assert.strictEqual(stream1.destroyed, true)
138+
assert.strictEqual(stream1.destroyed, false)
139+
assert.strictEqual(stream2.destroyed, false)
139140
})
140141

141142
it('should cleanup original stream when replaced by null', () => {
@@ -179,8 +180,8 @@ describe('res.body=', () => {
179180
res.body = stream2
180181
res.body = stream3
181182

182-
assert.strictEqual(stream1.destroyed, true)
183-
assert.strictEqual(stream2.destroyed, true)
183+
assert.strictEqual(stream1.destroyed, false)
184+
assert.strictEqual(stream2.destroyed, false)
184185
assert.strictEqual(stream3.destroyed, false)
185186
})
186187

@@ -196,9 +197,9 @@ describe('res.body=', () => {
196197
res.body = stream3
197198
res.body = stream4
198199

199-
assert.strictEqual(stream1.destroyed, true)
200-
assert.strictEqual(stream2.destroyed, true)
201-
assert.strictEqual(stream3.destroyed, true)
200+
assert.strictEqual(stream1.destroyed, false)
201+
assert.strictEqual(stream2.destroyed, false)
202+
assert.strictEqual(stream3.destroyed, false)
202203
assert.strictEqual(stream4.destroyed, false)
203204
})
204205

@@ -231,6 +232,32 @@ describe('res.body=', () => {
231232

232233
assert.strictEqual(stream.destroyed, true)
233234
})
235+
236+
it('should support wrapping stream middleware (like koa-compress)', async () => {
237+
const res = response()
238+
const sourceStream = new Stream.Readable({
239+
read () {
240+
this.push('hello world')
241+
this.push(null)
242+
}
243+
})
244+
245+
res.body = sourceStream
246+
247+
const wrappedStream = new Stream.PassThrough()
248+
sourceStream.pipe(wrappedStream)
249+
res.body = wrappedStream
250+
251+
assert.strictEqual(sourceStream.destroyed, false)
252+
assert.strictEqual(wrappedStream.destroyed, false)
253+
254+
const chunks = []
255+
for await (const chunk of wrappedStream) {
256+
chunks.push(chunk)
257+
}
258+
const result = Buffer.concat(chunks).toString()
259+
assert.strictEqual(result, 'hello world')
260+
})
234261
})
235262

236263
describe('when a buffer is given', () => {

lib/response.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ module.exports = {
139139
const cleanupPreviousStream = () => {
140140
if (original && isStream(original)) {
141141
original.once('error', () => {})
142-
destroy(original)
142+
// Only destroy if the new value is not a stream
143+
if (!isStream(val)) {
144+
destroy(original)
145+
}
143146
}
144147
}
145148

0 commit comments

Comments
 (0)