diff --git a/lib/internal/streams/state.js b/lib/internal/streams/state.js index 4a1b49d25cadba..c54c8230c72227 100644 --- a/lib/internal/streams/state.js +++ b/lib/internal/streams/state.js @@ -2,19 +2,19 @@ const { ERR_INVALID_OPT_VALUE } = require('internal/errors').codes; +function highWaterMarkFrom(options, isDuplex, duplexKey) { + return options.highWaterMark != null ? options.highWaterMark : + isDuplex ? options[duplexKey] : null; +} + function getHighWaterMark(state, options, duplexKey, isDuplex) { - let hwm = options.highWaterMark; + const hwm = highWaterMarkFrom(options, isDuplex, duplexKey); if (hwm != null) { - if (typeof hwm !== 'number' || !(hwm >= 0)) - throw new ERR_INVALID_OPT_VALUE('highWaterMark', hwm); - return Math.floor(hwm); - } else if (isDuplex) { - hwm = options[duplexKey]; - if (hwm != null) { - if (typeof hwm !== 'number' || !(hwm >= 0)) - throw new ERR_INVALID_OPT_VALUE(duplexKey, hwm); - return Math.floor(hwm); + if (!Number.isInteger(hwm) || hwm < 0) { + const name = isDuplex ? duplexKey : 'highWaterMark'; + throw new ERR_INVALID_OPT_VALUE(name, hwm); } + return Math.floor(hwm); } // Default value