nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
53 lines (43 loc) • 1.54 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/streams/state.js
import { validateInteger } from "nstdlib/lib/internal/validators";
import { codes as __codes__ } from "nstdlib/lib/internal/errors";
const { ERR_INVALID_ARG_VALUE } = __codes__;
// TODO (fix): For some reason Windows CI fails with bigger hwm.
let defaultHighWaterMarkBytes =
process.platform === "win32" ? 16 * 1024 : 64 * 1024;
let defaultHighWaterMarkObjectMode = 16;
function highWaterMarkFrom(options, isDuplex, duplexKey) {
return options.highWaterMark != null
? options.highWaterMark
: isDuplex
? options[duplexKey]
: null;
}
function getDefaultHighWaterMark(objectMode) {
return objectMode
? defaultHighWaterMarkObjectMode
: defaultHighWaterMarkBytes;
}
function setDefaultHighWaterMark(objectMode, value) {
validateInteger(value, "value", 0);
if (objectMode) {
defaultHighWaterMarkObjectMode = value;
} else {
defaultHighWaterMarkBytes = value;
}
}
function getHighWaterMark(state, options, duplexKey, isDuplex) {
const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
if (hwm != null) {
if (!Number.isInteger(hwm) || hwm < 0) {
const name = isDuplex ? `options.${duplexKey}` : "options.highWaterMark";
throw new ERR_INVALID_ARG_VALUE(name, hwm);
}
return Math.floor(hwm);
}
// Default value
return getDefaultHighWaterMark(state.objectMode);
}
export { getHighWaterMark };
export { getDefaultHighWaterMark };
export { setDefaultHighWaterMark };