How to use the @hapi/boom.tooManyRequests function in @hapi/boom

To help you get started, we’ve selected a few @hapi/boom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github brave-intl / bat-ledger / bat-utils / lib / hapi-rate-limiter.js View on Github external
const rateLimiter = chooseRateLimiter(request)
        let scope = null
        try {
          scope = rateLimiter._keyPrefix
          await rateLimiter.consume(address)
          scope = globalRateLimiter._keyPrefix
          await globalRateLimiter.consume('all')
          return h.continue
        } catch (err) {
          let error
          if (err instanceof Error) {
            // If some Redis error and `insuranceLimiter` is not set
            error = boom.internal('Try later')
          } else {
            // Not enough points to consume
            error = boom.tooManyRequests('Rate limit exceeded: ' + scope)
            error.output.headers['Retry-After'] = Math.round(err.msBeforeNext / 1000) || 1
          }

          return error
        }
      })
    }
github bakjs / bak / packages / ratelimit / lib / index.js View on Github external
options.namespace + ':' + realIP(request) + ':' + (request.route.id || request.route.path),
      routeLimit.limit,
      routeLimit.duration
    )

    request.plugins.ratelimit = {
      limit: rateLimit.limit,
      remaining: rateLimit.remaining - 1,
      reset: rateLimit.reset
    }

    if (rateLimit.remaining > 0) {
      return h.continue
    }

    const error = Boom.tooManyRequests('RATE_LIMIT_EXCEEDED')
    setHeaders(error.output.headers, request.plugins.ratelimit, options.XHeaders)
    error.reformat()
    return error
  }