Skip to content

Commit

Permalink
stream: refactor getHighWaterMark in state.js
Browse files Browse the repository at this point in the history
This commit aims to reduce some code duplication in state.js

PR-URL: #20415
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
danbev authored and MylesBorins committed May 22, 2018
1 parent 4710349 commit ed5f253
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/internal/streams/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed5f253

Please sign in to comment.