Skip to content

Commit 5133e78

Browse files
ronagTrott
authored andcommitted
doc: add note about forwarding stream options
It is a common and unfortunate pattern to simply just forward any and all options into the base constructor without taking into account whether these options might conflict with basic stream assumptions. PR-URL: #29857 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent b798f64 commit 5133e78

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

doc/api/stream.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,13 +1645,24 @@ parent class constructor:
16451645
const { Writable } = require('stream');
16461646

16471647
class MyWritable extends Writable {
1648-
constructor(options) {
1649-
super(options);
1648+
constructor({ highWaterMark, ...options }) {
1649+
super({
1650+
highWaterMark,
1651+
autoDestroy: true,
1652+
emitClose: true
1653+
});
16501654
// ...
16511655
}
16521656
}
16531657
```
16541658

1659+
When extending streams, it is important to keep in mind what options the user
1660+
can and should provide before forwarding these to the base constructor. For
1661+
example, if the implementation makes assumptions in regard to e.g. the
1662+
`autoDestroy` and `emitClose` options, it becomes important to not allow the
1663+
user to override these. It is therefore recommended to be explicit about what
1664+
options are forwarded instead of implicitly forwarding all options.
1665+
16551666
The new stream class must then implement one or more specific methods, depending
16561667
on the type of stream being created, as detailed in the chart below:
16571668

0 commit comments

Comments
 (0)