Skip to content

Commit cbd1251

Browse files
ronagtargos
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 fc53345 commit cbd1251

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
@@ -1641,13 +1641,24 @@ parent class constructor:
16411641
const { Writable } = require('stream');
16421642

16431643
class MyWritable extends Writable {
1644-
constructor(options) {
1645-
super(options);
1644+
constructor({ highWaterMark, ...options }) {
1645+
super({
1646+
highWaterMark,
1647+
autoDestroy: true,
1648+
emitClose: true
1649+
});
16461650
// ...
16471651
}
16481652
}
16491653
```
16501654

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

0 commit comments

Comments
 (0)