• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  MathFloor,
5  NumberIsInteger,
6} = primordials;
7const { validateInteger } = require('internal/validators');
8
9const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes;
10
11let defaultHighWaterMarkBytes = 16 * 1024;
12let defaultHighWaterMarkObjectMode = 16;
13
14function highWaterMarkFrom(options, isDuplex, duplexKey) {
15  return options.highWaterMark != null ? options.highWaterMark :
16    isDuplex ? options[duplexKey] : null;
17}
18
19function getDefaultHighWaterMark(objectMode) {
20  return objectMode ? defaultHighWaterMarkObjectMode : defaultHighWaterMarkBytes;
21}
22
23function setDefaultHighWaterMark(objectMode, value) {
24  validateInteger(value, 'value', 0);
25  if (objectMode) {
26    defaultHighWaterMarkObjectMode = value;
27  } else {
28    defaultHighWaterMarkBytes = value;
29  }
30}
31
32function getHighWaterMark(state, options, duplexKey, isDuplex) {
33  const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
34  if (hwm != null) {
35    if (!NumberIsInteger(hwm) || hwm < 0) {
36      const name = isDuplex ? `options.${duplexKey}` : 'options.highWaterMark';
37      throw new ERR_INVALID_ARG_VALUE(name, hwm);
38    }
39    return MathFloor(hwm);
40  }
41
42  // Default value
43  return getDefaultHighWaterMark(state.objectMode);
44}
45
46module.exports = {
47  getHighWaterMark,
48  getDefaultHighWaterMark,
49  setDefaultHighWaterMark,
50};
51