<commit_before>// Deps
const express = require('express')
const bodyParser = require('body-parser')
const fork = require('child_process').fork
const path = require('path')

const runner = path.resolve('build/lib/runner')

// Express setup
const service = express()
service.use(bodyParser.json())
const port = process.env.PORT || 8181

/**
 * Handles response from forked lambda runner procs
 * @param {Object} msg Message object from child proc
 * @param {Object} res Express response object
 */
const procResponse = (msg, res) => {
  switch (msg.type) {
    case 'success':
      res.status(200).send(msg.output)
      break
    case 'error':
      res.status(500).send(msg.output)
      break
    case 'metric':
      console.log(msg.output)
  }
}

/**
 * Builds payload and execs lambda
 * @param {Object} req Express req object
 * @param {Object} res Express res object
 * @param {String} lambdas Path to the lambdas directory
 */
const runLambda = (req, res, lambdas) => {
  const evtBody = req.body
  const lambda = req.params.endpoint
  // Custom stuff...
  evtBody.operation = req.method
  // Execute lambda
  const proc = fork(runner, [ lambda ], {
    env: {
      lambdas,
      event: JSON.stringify(evtBody)
    }
  })
  // Print pid
  console.log(`PID ${proc.pid} running ${lambda}`)
  // Await proc
  proc.on('message', (msg) => procResponse(msg, res))
}

/**
 * Core app method, binds endpoints and starts listener
 * @param {String} lambdas Path to the lambdas directory
 */
export const app = (lambdas) => {
  service.all('/api/:endpoint', (req, res) => runLambda(req, res, lambdas))
  service.listen(port, () => {
    console.log(`Service running on ${port}`)
  })
}<commit_msg>Misc cleanup on runLambda method<commit_after>// Deps
const express = require('express')
const bodyParser = require('body-parser')
const fork = require('child_process').fork
const path = require('path')

const runner = path.resolve('build/lib/runner')

// Express setup
const service = express()
service.use(bodyParser.json())
const port = process.env.PORT || 8181

/**
 * Handles response from forked lambda runner procs
 * @param {Object} msg Message object from child proc
 * @param {Object} res Express response object
 */
const procResponse = (msg, res) => {
  switch (msg.type) {
    case 'success':
      res.status(200).send(msg.output)
      break
    case 'error':
      res.status(500).send(msg.output)
      break
    case 'metric':
      console.log(msg.output)
  }
}

/**
 * Builds payload and execs lambda
 * @param {Object} req Express req object
 * @param {Object} res Express res object
 * @param {String} lambdas Path to the lambdas directory
 */
const runLambda = (req, res, lambdas) => {
  const evt = req.body
  const lambda = req.params.endpoint
  // Custom stuff...
  evt.operation = req.method
  // Set event
  const event = JSON.stringify(evt)
  // Execute lambda
  const proc = fork(runner, [ lambda ], { env: { lambdas, event } })
  // Print pid
  console.log(`PID ${proc.pid} running ${lambda}`)
  // Await proc
  proc.on('message', (msg) => procResponse(msg, res))
}

/**
 * Core app method, binds endpoints and starts listener
 * @param {String} lambdas Path to the lambdas directory
 */
export const app = (lambdas) => {
  service.all('/api/:endpoint', (req, res) => runLambda(req, res, lambdas))
  service.listen(port, () => {
    console.log(`Service running on ${port}`)
  })
}
{'fraction_non_alphanumeric': 0.14549721325902024, 'fraction_numerical': 0.005866823115283074, 'mean_word_length': 3.480946123521682, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6836566', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1050, 'n_words': 324}