stream: refactor getHighWaterMark in state.js
This commit aims to reduce some code duplication in state.js PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
7d81f5d143
commit
9b24be1340
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user