Skip to content

Commit

Permalink
doc: notes about forwarding stream options
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ronag committed Oct 6, 2019
1 parent ddcd235 commit 58c813f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1645,13 +1645,24 @@ parent class constructor:
const { Writable } = require('stream');

class MyWritable extends Writable {
constructor(options) {
super(options);
constructor({ highWaterMark, ...options }) {
super({
highWaterMark,
autoDestroy: false,
emitClose: true
});
// ...
}
}
```

When extending streams it is important to keep in mind what options the user
can and should provide before forwarding these to the base constructor. For
example if the implementation makes assumptions in regard to e.g. the
`autoDestroy` and `emitClose` options, it becomes important to not allow the
user to override these. It is therefore recommend to be explicit about what
options are forwarded instead of implicitly forwarding all options.

The new stream class must then implement one or more specific methods, depending
on the type of stream being created, as detailed in the chart below:

Expand Down

0 comments on commit 58c813f

Please sign in to comment.