<commit_before>/* global DDPRateLimiter */
/* eslint import/prefer-default-export: 0 */

const isOptionalStringOrFunction =
  val => typeof val === 'string' || typeof val === 'function' || typeof val === 'undefined';

const isOptionalObject = val => typeof val === 'object' || typeof val === 'undefined';

const alwaysTrue = () => true;

const RateLimiterMixin = (methodOptions) => {
  const {
    name,
    rateLimit,
  } = methodOptions;

  if (!rateLimit) {
    throw new Error(`RateLimiterMixin: rateLimit option is missing (${name} method)`);
  }

  const {
    matcher,
    numRequests,
    timeInterval,
  } = rateLimit;

  if (typeof numRequests !== 'number') {
    throw new Error(`RateLimiterMixin: numRequests must be a number (${name} method)`);
  }

  if (typeof timeInterval !== 'number') {
    throw new Error(`RateLimiterMixin: timeInterval must be a number (${name} method)`);
  }

  if (!isOptionalObject(matcher)) {
    throw new Error(`RateLimiterMixin: matcher must be an object if specified (${name} method)`);
  }

  const {
    userId = alwaysTrue,
    connectionId = alwaysTrue,
    clientAddress = alwaysTrue,
  } = matcher || {};

  if (!isOptionalStringOrFunction(userId)) {
    throw new Error(`RateLimiterMixin: matcher.userId must be a string or a function if specified (${name} method)`);
  }

  if (!isOptionalStringOrFunction(connectionId)) {
    throw new Error(`RateLimiterMixin: matcher.connectionId must be a string or a function if specified (${name} method)`);
  }

  if (!isOptionalStringOrFunction(clientAddress)) {
    throw new Error(`RateLimiterMixin: matcher.clientAddress must be a string or a function if specified (${name} method)`);
  }

  DDPRateLimiter.addRule({
    type: 'method',
    name,
    userId,
    connectionId,
    clientAddress,
  }, numRequests, timeInterval);

  return methodOptions;
};

export { RateLimiterMixin };<commit_msg>Add callback to methodOptions in src/rate-limiter.js<commit_after>/* global DDPRateLimiter */
/* eslint import/prefer-default-export: 0 */

const isOptionalStringOrFunction =
  val => typeof val === 'string' || typeof val === 'function' || typeof val === 'undefined';

const isOptionalObject = val => typeof val === 'object' || typeof val === 'undefined';

const isOptionalFunction = val => typeof val === 'function' || typeof val === 'undefined';

const alwaysTrue = () => true;

const RateLimiterMixin = (methodOptions) => {
  const {
    name,
    rateLimit,
  } = methodOptions;

  if (!rateLimit) {
    throw new Error(`RateLimiterMixin: rateLimit option is missing (${name} method)`);
  }

  const {
    matcher,
    numRequests,
    timeInterval,
    callback,
  } = rateLimit;

  if (typeof numRequests !== 'number') {
    throw new Error(`RateLimiterMixin: numRequests must be a number (${name} method)`);
  }

  if (typeof timeInterval !== 'number') {
    throw new Error(`RateLimiterMixin: timeInterval must be a number (${name} method)`);
  }

  if (!isOptionalObject(matcher)) {
    throw new Error(`RateLimiterMixin: matcher must be an object if specified (${name} method)`);
  }

  if (!isOptionalFunction(callback)) {
    throw new Error(`RateLimiterMixin: callback must be a function if specified (${name} method)`);
  }

  const {
    userId = alwaysTrue,
    connectionId = alwaysTrue,
    clientAddress = alwaysTrue,
  } = matcher || {};

  if (!isOptionalStringOrFunction(userId)) {
    throw new Error(`RateLimiterMixin: matcher.userId must be a string or a function if specified (${name} method)`);
  }

  if (!isOptionalStringOrFunction(connectionId)) {
    throw new Error(`RateLimiterMixin: matcher.connectionId must be a string or a function if specified (${name} method)`);
  }

  if (!isOptionalStringOrFunction(clientAddress)) {
    throw new Error(`RateLimiterMixin: matcher.clientAddress must be a string or a function if specified (${name} method)`);
  }

  DDPRateLimiter.addRule({
    type: 'method',
    name,
    userId,
    connectionId,
    clientAddress,
  }, numRequests, timeInterval, callback);

  return methodOptions;
};

export { RateLimiterMixin };
{'fraction_non_alphanumeric': 0.1297971156196529, 'fraction_numerical': 0.0004888780249327792, 'mean_word_length': 4.070631970260223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5141582', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1232, 'n_words': 364}