• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const { MathFloor, NumberIsInteger } = require('../../ours/primordials')
4const { ERR_INVALID_ARG_VALUE } = require('../../ours/errors').codes
5function highWaterMarkFrom(options, isDuplex, duplexKey) {
6  return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null
7}
8function getDefaultHighWaterMark(objectMode) {
9  return objectMode ? 16 : 16 * 1024
10}
11function getHighWaterMark(state, options, duplexKey, isDuplex) {
12  const hwm = highWaterMarkFrom(options, isDuplex, duplexKey)
13  if (hwm != null) {
14    if (!NumberIsInteger(hwm) || hwm < 0) {
15      const name = isDuplex ? `options.${duplexKey}` : 'options.highWaterMark'
16      throw new ERR_INVALID_ARG_VALUE(name, hwm)
17    }
18    return MathFloor(hwm)
19  }
20
21  // Default value
22  return getDefaultHighWaterMark(state.objectMode)
23}
24module.exports = {
25  getHighWaterMark,
26  getDefaultHighWaterMark
27}
28