Skip to content

Commit ed5f253

Browse files
danbevMylesBorins
authored andcommitted
stream: refactor getHighWaterMark in state.js
This commit aims to reduce some code duplication in state.js PR-URL: #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]>
1 parent 4710349 commit ed5f253

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/internal/streams/state.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
const { ERR_INVALID_OPT_VALUE } = require('internal/errors').codes;
44

5+
function highWaterMarkFrom(options, isDuplex, duplexKey) {
6+
return options.highWaterMark != null ? options.highWaterMark :
7+
isDuplex ? options[duplexKey] : null;
8+
}
9+
510
function getHighWaterMark(state, options, duplexKey, isDuplex) {
6-
let hwm = options.highWaterMark;
11+
const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
712
if (hwm != null) {
8-
if (typeof hwm !== 'number' || !(hwm >= 0))
9-
throw new ERR_INVALID_OPT_VALUE('highWaterMark', hwm);
10-
return Math.floor(hwm);
11-
} else if (isDuplex) {
12-
hwm = options[duplexKey];
13-
if (hwm != null) {
14-
if (typeof hwm !== 'number' || !(hwm >= 0))
15-
throw new ERR_INVALID_OPT_VALUE(duplexKey, hwm);
16-
return Math.floor(hwm);
13+
if (!Number.isInteger(hwm) || hwm < 0) {
14+
const name = isDuplex ? duplexKey : 'highWaterMark';
15+
throw new ERR_INVALID_OPT_VALUE(name, hwm);
1716
}
17+
return Math.floor(hwm);
1818
}
1919

2020
// Default value

0 commit comments

Comments
 (0)