<commit_before>const https = require('https')
const http = require('http')
const url = require('url')

const VERSION = 0
const MIN_VERSION = 0

const FORBIDDEN = /^\[\s*\[\s*"forbidden"/
const APPROVED = /^\[\s*\[\s*"approved"/

function isValid (data) {
  if (typeof data !== 'object') return false
  if (typeof data.version !== 'number') return false
  if (data.version > MIN_VERSION) return false
  if (typeof data.password !== 'string') return false
  if (!Array.isArray(data.commands)) return false
  for (const command of data.commands) {
    if (!Array.isArray(command)) return false
    if (command[0] !== 'action') return false
    if (typeof command[1] !== 'object') return false
    if (typeof command[2] !== 'object') return false
  }
  return true
}

function waitForEnd (res) {
  return new Promise(resolve => {
    res.on('end', resolve)
  })
}

function createBackendProxy (server, options) {
  if (!options.password) {
    throw new Error(
      'For security reasons you must set strong password ' +
      'in `backend.password` option'
    )
  }
  if (!options.url) {
    throw new Error('You must set `backend.url` option with address to backend')
  }

  const backend = url.parse(options.url)

  const processing = []

  function send (ctx, action, meta) {
    const body = JSON.stringify({
      version: VERSION,
      password: <PASSWORD>.password,
      commands: [['action', action, meta]]
    })
    const protocol = backend.protocol === 'https:' ? https : http
    return new Promise((resolve, reject) => {
      const req = protocol.request({
        method: 'POST',
        host: backend.hostname,
        port: backend.port,
        path: backend.path,
        headers: {
          'Content-Type': 'application/json',
          'Content-Length': Buffer.byteLength(body)
        }
      }, res => {
        let received = ''
        let approved = false
        if (res.statusCode < 200 || res.statusCode > 299) {
          reject(
            new Error('Backend responsed with ' + res.statusCode + ' code'))
        } else {
          processing[meta.id] = waitForEnd(res)
          res.on('data', part => {
            if (!approved) {
              received += part
              if (APPROVED.test(received)) {
                approved = true
                resolve(true)
              } else if (FORBIDDEN.test(received)) {
                approved = true
                delete processing[meta.id]
                resolve(false)
              }
            }
          })
        }
      })
      req.on('error', reject)
      req.end(body)
    })
  }

  function process (ctx, action, meta) {
    return processing[meta.id].then(() => {
      delete processing[meta.id]
    })
  }

  server.otherType({
    access: send,
    process
  })

  server.otherChannel({
    access: send,<commit_msg>Clear vairable name<commit_after>const https = require('https')
const http = require('http')
const url = require('url')

const VERSION = 0
const MIN_VERSION = 0

const FORBIDDEN = /^\[\s*\[\s*"forbidden"/
const APPROVED = /^\[\s*\[\s*"approved"/

function isValid (data) {
  if (typeof data !== 'object') return false
  if (typeof data.version !== 'number') return false
  if (data.version > MIN_VERSION) return false
  if (typeof data.password !== 'string') return false
  if (!Array.isArray(data.commands)) return false
  for (const command of data.commands) {
    if (!Array.isArray(command)) return false
    if (command[0] !== 'action') return false
    if (typeof command[1] !== 'object') return false
    if (typeof command[2] !== 'object') return false
  }
  return true
}

function waitForEnd (res) {
  return new Promise(resolve => {
    res.on('end', resolve)
  })
}

function createBackendProxy (server, options) {
  if (!options.password) {
    throw new Error(
      'For security reasons you must set strong password ' +
      'in `backend.password` option'
    )
  }
  if (!options.url) {
    throw new Error('You must set `backend.url` option with address to backend')
  }

  const backend = url.parse(options.url)

  const processing = []

  function send (ctx, action, meta) {
    const body = JSON.stringify({
      version: VERSION,
      password: <PASSWORD>,
      commands: [['action', action, meta]]
    })
    const protocol = backend.protocol === 'https:' ? https : http
    return new Promise((resolve, reject) => {
      const req = protocol.request({
        method: 'POST',
        host: backend.hostname,
        port: backend.port,
        path: backend.path,
        headers: {
          'Content-Type': 'application/json',
          'Content-Length': Buffer.byteLength(body)
        }
      }, res => {
        let received = ''
        let answer = false
        if (res.statusCode < 200 || res.statusCode > 299) {
          reject(
            new Error('Backend responsed with ' + res.statusCode + ' code'))
        } else {
          processing[meta.id] = waitForEnd(res)
          res.on('data', part => {
            if (!answer) {
              received += part
              if (APPROVED.test(received)) {
                answer = true
                resolve(true)
              } else if (FORBIDDEN.test(received)) {
                answer = true
                delete processing[meta.id]
                resolve(false)
              }
            }
          })
        }
      })
      req.on('error', reject)
      req.end(body)
    })
  }

  function process (ctx, action, meta) {
    return processing[meta.id].then(() => {
      delete processing[meta.id]
    })
  }

  server.otherType({
    access: send,
    process
  })

  server.otherChannel({
    access: send,
{'fraction_non_alphanumeric': 0.13617850185939437, 'fraction_numerical': 0.003895873915353285, 'mean_word_length': 2.216400911161731, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '5966955', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1640, 'n_words': 453}