Repository: nodejs/node
File: lib/internal/crypto/hash.js (JavaScript)

Code before:
  },
} = require('internal/errors');

const {
  validateEncoding,
  validateString,
  validateUint32,
} = require('internal/validators');

const {
  isArrayBufferView,
} = require('internal/util/types');

const LazyTransform = require('internal/streams/lazy_transform');

const kState = Symbol('kState');
const kFinalized = Symbol('kFinalized');

/**
 * @param {string} name
 */
function normalizeAlgorithm(name) {
  return StringPrototypeReplace(StringPrototypeToLowerCase(name), '-', '');
}

const emitDeprecationWarning = getDeprecationWarningEmitter(
  'DEP0198',
  'Creating SHAKE128/256 digests without an explicit options.outputLength is deprecated.',
  undefined,
  false,
  (algorithm) => {
    if (!isPendingDeprecation()) return false;
    const normalized = normalizeAlgorithm(algorithm);
    return normalized === 'shake128' || normalized === 'shake256';
  },
);

function Hash(algorithm, options) {
  if (!new.target)
    return new Hash(algorithm, options);
  const isCopy = algorithm instanceof _Hash;
  if (!isCopy)
    validateString(algorithm, 'algorithm');
  const xofLen = typeof options === 'object' && options !== null ?
    options.outputLength : undefined;
  if (xofLen !== undefined)
    validateUint32(xofLen, 'options.outputLength');
  // Lookup the cached ID from JS land because it's faster than decoding
  // the string in C++ land.
  const algorithmId = isCopy ? -1 : getCachedHashId(algorithm);
  this[kHandle] = new _Hash(algorithm, xofLen, algorithmId, getHashCache());

Review comment (suggestion):
nit
```suggestion
const emitDeprecationWarningIfAlgoIsShake = getDeprecationWarningEmitter(
```
or
```suggestion
const maybeEmitDeprecationWarning = getDeprecationWarningEmitter(
```

Code after:
  },
} = require('internal/errors');

const {
  validateEncoding,
  validateString,
  validateUint32,
} = require('internal/validators');

const {
  isArrayBufferView,
} = require('internal/util/types');

const LazyTransform = require('internal/streams/lazy_transform');

const kState = Symbol('kState');
const kFinalized = Symbol('kFinalized');

/**
 * @param {string} name
 */
function normalizeAlgorithm(name) {
  return StringPrototypeReplace(StringPrototypeToLowerCase(name), '-', '');
}

const maybeEmitDeprecationWarning = getDeprecationWarningEmitter(
  'DEP0198',
  'Creating SHAKE128/256 digests without an explicit options.outputLength is deprecated.',
  undefined,
  false,
  (algorithm) => {
    if (!isPendingDeprecation()) return false;
    const normalized = normalizeAlgorithm(algorithm);
    return normalized === 'shake128' || normalized === 'shake256';
  },
);

function Hash(algorithm, options) {
  if (!new.target)
    return new Hash(algorithm, options);
  const isCopy = algorithm instanceof _Hash;
  if (!isCopy)
    validateString(algorithm, 'algorithm');
  const xofLen = typeof options === 'object' && options !== null ?
    options.outputLength : undefined;
  if (xofLen !== undefined)
    validateUint32(xofLen, 'options.outputLength');
  // Lookup the cached ID from JS land because it's faster than decoding
  // the string in C++ land.
  const algorithmId = isCopy ? -1 : getCachedHashId(algorithm);
  this[kHandle] = new _Hash(algorithm, xofLen, algorithmId, getHashCache());