<commit_before>onceReady = (bongo, callback) ->
  return callback null  if bongo.dbClientReady
  bongo.once 'dbClientReady', callback

module.exports = ->
  { argv }    = require 'optimist'
  KONFIG      = require('koding-config-manager').load("main.#{argv.c}")
  { dash }    = require 'bongo'
  RateLimit   = require('ratelimit.js').RateLimit

  redisClient = require('redis').createClient(
    KONFIG.monitoringRedis.split(':')[1]
    KONFIG.monitoringRedis.split(':')[0]
    {}
  )

  userRules   = KONFIG.nodejsRateLimiter.userRules
  rateLimiter = new RateLimit redisClient, userRules

  bongo = this

  sendMetrics = (start, message) ->

    duration   = new Date() - start
    { method } = message
    bongo.metrics.sendMethodMetrics { method, duration }


  rateLimit = (id, callback) ->

    return callback no  unless KONFIG.nodejsRateLimiter.enabled

    if /^guest-/.test id
      guestRules        = KONFIG.nodejsRateLimiter.guestRules
      rateLimiter.rules = rateLimiter.convertRules guestRules

    rateLimiter.incr id, (err, isRateLimited) ->
      console.log "Rate limit error occurred: #{err}"  if err
      callback isRateLimited


  process = (req, client, callback) ->

    [client, callback] = [null, client]  unless callback

    { channelName, queue } = req.body

    responses = new Array queue.length

    workQueue = queue.map (message, i) -> ->

      start = new Date()
      bongo.handleRequest channelName, { message, client }, (secretName, callbackId, args) ->

        sendMetrics start, message  if bongo.metrics

        bongo.scrubResponse callbackId, args, (message) ->
          responses[i] = message
          workQueue.fin()

    dash workQueue, -> callback responses

  (req, res, next) ->

    { sessionToken, userArea, queue } = req.body

    unless queue?.length
      return res.status(400).end()

    sendResponse = (payload) -> res.send payload

    if sessionToken and userArea
      onceReady bongo, ->
        bongo.fetchClient sessionToken, userArea, (client) ->
          unless client
            console.log "bongo.fetchClient: #{sessionToken} not found in #{userArea}"
            return res.status(500).send 'An error occcurred'

          rateLimit client.username, (isRateLimited) ->
            return res.status(403).send 'Rate limit exceeded'  if isRateLimited
            process req, client, sendResponse

    else
      rateLimit sessionToken, (isRateLimited) ->
        return res.status(403).send 'Rate limit exceeded'  if isRateLimited
        process req, sendResponse


<commit_msg>Node modules koding: add comments<commit_after>onceReady = (bongo, callback) ->
  return callback null  if bongo.dbClientReady
  bongo.once 'dbClientReady', callback

module.exports = ->
  { argv }    = require 'optimist'
  KONFIG      = require('koding-config-manager').load("main.#{argv.c}")
  { dash }    = require 'bongo'
  RateLimit   = require('ratelimit.js').RateLimit

  redisClient = require('redis').createClient(
    KONFIG.monitoringRedis.split(':')[1]
    KONFIG.monitoringRedis.split(':')[0]
    {}
  )

  userRules   = KONFIG.nodejsRateLimiter.userRules
  rateLimiter = new RateLimit redisClient, userRules

  bongo = this

  sendMetrics = (start, message) ->

    duration   = new Date() - start
    { method } = message
    bongo.metrics.sendMethodMetrics { method, duration }


  rateLimit = (id, callback) ->

    return callback no  unless KONFIG.nodejsRateLimiter.enabled

    # if id start with guest- use guest rules for the user
    if /^guest-/.test id
      guestRules        = KONFIG.nodejsRateLimiter.guestRules
      rateLimiter.rules = rateLimiter.convertRules guestRules

    rateLimiter.incr id, (err, isRateLimited) ->
      console.log "Rate limit error occurred: #{err}"  if err
      callback isRateLimited


  process = (req, client, callback) ->

    [client, callback] = [null, client]  unless callback

    { channelName, queue } = req.body

    responses = new Array queue.length

    workQueue = queue.map (message, i) -> ->

      start = new Date()
      bongo.handleRequest channelName, { message, client }, (secretName, callbackId, args) ->

        sendMetrics start, message  if bongo.metrics

        bongo.scrubResponse callbackId, args, (message) ->
          responses[i] = message
          workQueue.fin()

    dash workQueue, -> callback responses

  (req, res, next) ->

    { sessionToken, userArea, queue } = req.body

    unless queue?.length
      return res.status(400).end()

    sendResponse = (payload) -> res.send payload

    if sessionToken and userArea
      onceReady bongo, ->
        bongo.fetchClient sessionToken, userArea, (client) ->
          unless client
            console.log "bongo.fetchClient: #{sessionToken} not found in #{userArea}"
            return res.status(500).send 'An error occcurred'

          rateLimit client.username, (isRateLimited) ->
            return res.status(403).send 'Rate limit exceeded'  if isRateLimited
            process req, client, sendResponse

    else
      # use sessionToken as id for rate limiting
      rateLimit sessionToken, (isRateLimited) ->
        return res.status(403).send 'Rate limit exceeded'  if isRateLimited
        process req, sendResponse



{'fraction_non_alphanumeric': 0.10729285987017946, 'fraction_numerical': 0.005345551737304314, 'mean_word_length': 2.8074127906976742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '5555728', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1531, 'n_words': 404}