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: nodejs#20415
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
danbev authored and BridgeAR committed May 18, 2018
1 parent 7d81f5d commit 9b24be1
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 9b24be1

Please sign in to comment.