Skip to content

Commit 4bec6d1

Browse files
ronagBridgeAR
authored andcommitted
stream: enable autoDestroy by default
PR-URL: nodejs#30623 Refs: nodejs#30621 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent c52ebc0 commit 4bec6d1

15 files changed

+48
-37
lines changed

doc/api/stream.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ added: v0.9.4
277277
The `'error'` event is emitted if an error occurred while writing or piping
278278
data. The listener callback is passed a single `Error` argument when called.
279279

280-
The stream is not closed when the `'error'` event is emitted unless the
281-
[`autoDestroy`][writable-new] option was set to `true` when creating the
280+
The stream is closed when the `'error'` event is emitted unless the
281+
[`autoDestroy`][writable-new] option was set to `false` when creating the
282282
stream.
283283

284284
After `'error'`, no further events other than `'close'` *should* be emitted
@@ -1667,11 +1667,7 @@ const { Writable } = require('stream');
16671667

16681668
class MyWritable extends Writable {
16691669
constructor({ highWaterMark, ...options }) {
1670-
super({
1671-
highWaterMark,
1672-
autoDestroy: true,
1673-
emitClose: true
1674-
});
1670+
super({ highWaterMark });
16751671
// ...
16761672
}
16771673
}
@@ -1745,6 +1741,9 @@ changes:
17451741
pr-url: https://github.com/nodejs/node/pull/22795
17461742
description: Add `autoDestroy` option to automatically `destroy()` the
17471743
stream when it emits `'finish'` or errors.
1744+
- version: REPLACEME
1745+
pr-url: https://github.com/nodejs/node/pull/30623
1746+
description: Change `autoDestroy` option default to `true`.
17481747
-->
17491748

17501749
* `options` {Object}
@@ -1776,7 +1775,7 @@ changes:
17761775
* `final` {Function} Implementation for the
17771776
[`stream._final()`][stream-_final] method.
17781777
* `autoDestroy` {boolean} Whether this stream should automatically call
1779-
`.destroy()` on itself after ending. **Default:** `false`.
1778+
`.destroy()` on itself after ending. **Default:** `true`.
17801779

17811780
<!-- eslint-disable no-useless-constructor -->
17821781
```js
@@ -2021,6 +2020,9 @@ changes:
20212020
pr-url: https://github.com/nodejs/node/pull/22795
20222021
description: Add `autoDestroy` option to automatically `destroy()` the
20232022
stream when it emits `'end'` or errors.
2023+
- version: REPLACEME
2024+
pr-url: https://github.com/nodejs/node/pull/30623
2025+
description: Change `autoDestroy` option default to `true`.
20242026
-->
20252027

20262028
* `options` {Object}
@@ -2039,7 +2041,7 @@ changes:
20392041
* `destroy` {Function} Implementation for the
20402042
[`stream._destroy()`][readable-_destroy] method.
20412043
* `autoDestroy` {boolean} Whether this stream should automatically call
2042-
`.destroy()` on itself after ending. **Default:** `false`.
2044+
`.destroy()` on itself after ending. **Default:** `true`.
20432045

20442046
<!-- eslint-disable no-useless-constructor -->
20452047
```js

lib/_http_incoming.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function IncomingMessage(socket) {
4848
};
4949
}
5050

51-
Stream.Readable.call(this, streamOptions);
51+
Stream.Readable.call(this, { autoDestroy: false, ...streamOptions });
5252

5353
this._readableState.readingMore = true;
5454

lib/_stream_readable.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function ReadableState(options, stream, isDuplex) {
138138
this.emitClose = !options || options.emitClose !== false;
139139

140140
// Should .destroy() be called after 'end' (and potentially 'finish')
141-
this.autoDestroy = !!(options && options.autoDestroy);
141+
this.autoDestroy = !options || options.autoDestroy !== false;
142142

143143
// Has it been destroyed
144144
this.destroyed = false;
@@ -201,11 +201,7 @@ Readable.prototype._destroy = function(err, cb) {
201201
};
202202

203203
Readable.prototype[EE.captureRejectionSymbol] = function(err) {
204-
// TODO(mcollina): remove the destroyed if once errorEmitted lands in
205-
// Readable.
206-
if (!this.destroyed) {
207-
this.destroy(err);
208-
}
204+
this.destroy(err);
209205
};
210206

211207
// Manually shove something into the read() buffer.

lib/_stream_writable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function WritableState(options, stream, isDuplex) {
167167
this.emitClose = !options || options.emitClose !== false;
168168

169169
// Should .destroy() be called after 'finish' (and potentially 'end')
170-
this.autoDestroy = !!(options && options.autoDestroy);
170+
this.autoDestroy = !options || options.autoDestroy !== false;
171171

172172
// Indicates whether the stream has errored. When true all write() calls
173173
// should return false. This is needed since when autoDestroy

lib/internal/fs/streams.js

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ function ReadStream(path, options) {
7777
if (options.emitClose === undefined) {
7878
options.emitClose = false;
7979
}
80+
if (options.autoDestroy === undefined) {
81+
options.autoDestroy = false;
82+
}
8083

8184
this[kFs] = options.fs || fs;
8285

@@ -298,6 +301,9 @@ function WriteStream(path, options) {
298301
if (options.emitClose === undefined) {
299302
options.emitClose = false;
300303
}
304+
if (options.autoDestroy === undefined) {
305+
options.autoDestroy = false;
306+
}
301307

302308
this[kFs] = options.fs || fs;
303309
if (typeof this[kFs].open !== 'function') {

lib/internal/http2/compat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function onStreamTimeout(kind) {
282282

283283
class Http2ServerRequest extends Readable {
284284
constructor(stream, headers, options, rawHeaders) {
285-
super(options);
285+
super({ autoDestroy: false, ...options });
286286
this[kState] = {
287287
closed: false,
288288
didRead: false,

lib/internal/http2/core.js

+1
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,7 @@ class Http2Stream extends Duplex {
17821782
constructor(session, options) {
17831783
options.allowHalfOpen = true;
17841784
options.decodeStrings = false;
1785+
options.autoDestroy = false;
17851786
super(options);
17861787
this[async_id_symbol] = -1;
17871788

lib/net.js

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ function Socket(options) {
289289
options.allowHalfOpen = true;
290290
// For backwards compat do not emit close on destroy.
291291
options.emitClose = false;
292+
options.autoDestroy = false;
292293
// Handle strings directly.
293294
options.decodeStrings = false;
294295
stream.Duplex.call(this, options);

lib/zlib.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
259259
}
260260
}
261261

262-
Transform.call(this, opts);
262+
Transform.call(this, { autoDestroy: false, ...opts });
263263
this._hadError = false;
264264
this.bytesWritten = 0;
265265
this._handle = handle;

test/parallel/test-stream-pipe-error-handling.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ const Stream = require('stream').Stream;
6262
const R = Stream.Readable;
6363
const W = Stream.Writable;
6464

65-
const r = new R();
66-
const w = new W();
65+
const r = new R({ autoDestroy: false });
66+
const w = new W({ autoDestroy: false });
6767
let removed = false;
6868

6969
r._read = common.mustCall(function() {

test/parallel/test-stream-unshift-read-race.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const assert = require('assert');
3232

3333
const stream = require('stream');
3434
const hwm = 10;
35-
const r = stream.Readable({ highWaterMark: hwm });
35+
const r = stream.Readable({ highWaterMark: hwm, autoDestroy: false });
3636
const chunks = 10;
3737

3838
const data = Buffer.allocUnsafe(chunks * hwm + Math.ceil(hwm / 2));

test/parallel/test-stream-writable-null.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const assert = require('assert');
55
const stream = require('stream');
66

77
class MyWritable extends stream.Writable {
8+
constructor(options) {
9+
super({ autoDestroy: false, ...options });
10+
}
811
_write(chunk, encoding, callback) {
912
assert.notStrictEqual(chunk, null);
1013
callback();
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use strict';
22
const common = require('../common');
33
const { Writable } = require('stream');
4-
const assert = require('assert');
54

65
{
76
// Sync + Sync
87
const writable = new Writable({
98
write: common.mustCall((buf, enc, cb) => {
109
cb();
11-
assert.throws(cb, {
12-
code: 'ERR_MULTIPLE_CALLBACK',
13-
name: 'Error'
14-
});
10+
cb();
1511
})
1612
});
1713
writable.write('hi');
14+
writable.on('error', common.expectsError({
15+
code: 'ERR_MULTIPLE_CALLBACK',
16+
name: 'Error'
17+
}));
1818
}
1919

2020
{
@@ -23,14 +23,15 @@ const assert = require('assert');
2323
write: common.mustCall((buf, enc, cb) => {
2424
cb();
2525
process.nextTick(() => {
26-
assert.throws(cb, {
27-
code: 'ERR_MULTIPLE_CALLBACK',
28-
name: 'Error'
29-
});
26+
cb();
3027
});
3128
})
3229
});
3330
writable.write('hi');
31+
writable.on('error', common.expectsError({
32+
code: 'ERR_MULTIPLE_CALLBACK',
33+
name: 'Error'
34+
}));
3435
}
3536

3637
{
@@ -39,12 +40,13 @@ const assert = require('assert');
3940
write: common.mustCall((buf, enc, cb) => {
4041
process.nextTick(cb);
4142
process.nextTick(() => {
42-
assert.throws(cb, {
43-
code: 'ERR_MULTIPLE_CALLBACK',
44-
name: 'Error'
45-
});
43+
cb();
4644
});
4745
})
4846
});
4947
writable.write('hi');
48+
writable.on('error', common.expectsError({
49+
code: 'ERR_MULTIPLE_CALLBACK',
50+
name: 'Error'
51+
}));
5052
}

test/parallel/test-stream2-pipe-error-handling.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const stream = require('stream');
8080
stream.Readable.prototype.unpipe.call(this, dest);
8181
};
8282

83-
const dest = new stream.Writable();
83+
const dest = new stream.Writable({ autoDestroy: false });
8484
dest._write = function(chunk, encoding, cb) {
8585
cb();
8686
};

test/parallel/test-stream2-writable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ const helloWorldBuffer = Buffer.from('hello world');
275275

276276
{
277277
// Verify writables cannot be piped
278-
const w = new W();
278+
const w = new W({ autoDestroy: false });
279279
w._write = common.mustNotCall();
280280
let gotError = false;
281281
w.on('error', function() {

0 commit comments

Comments
 (0)