<commit_before>﻿#!/usr/bin/env node

"use strict";

const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');

const libdir = path.join(path.dirname(fs.realpathSync(__filename)), '..');
const tuvero = require(libdir + path.sep + 'state.js');

const app = express();

const PORT = 8080;

app.use(bodyParser.json({ type: 'application/json' }));

function formatError(error) {
  return JSON.stringify({
    'error': 'Error while processing the request',
    'message': error ? error.message || error.msg || error : ''
  }, null, '  ');
}

let router = express.Router();

const allCommands = Object.keys(tuvero.commands);

const allRoutes = (function (commands) {
  var routes = {};
  commands.forEach(function (cmd) {
    routes[cmd] = `/${cmd}`
  });
  return routes;
})(allCommands);

router.post('/:command', function (request, response, next) {
  let command = request.params.command || undefined;
  let id = `${request.ip}->{${request.path}`;

  (new Promise((resolve, reject) => {
    if (!command || !tuvero.commands[command]) {
      return reject("Command not recognized. Available commands: " + allCommands.join(', '));
    }
    if (!request.is("application/json")) {
      return reject("Content-Type must be application/json");
    }
    if (!request.body) {
      return reject("No JSON data received");
    }

    tuvero.parse(request.body)
      .then(state => {
        resolve(JSON.stringify(tuvero.commands[command](state), null, ' '));
      }).catch(reject);
  }))
    .then(jsonstring => {
      response.send(jsonstring);
    }).catch(error => {
      response.status(400).send(formatError(error));
    });
});

router.all('/', function (request, response, next) {
  response.send(JSON.stringify(allRoutes, null, ' '));
});

app.use('/', router);

app.listen(PORT);

console.log("listening on port " + PORT);<commit_msg>autoformat tuvero-server.js<commit_after>﻿#!/usr/bin/env node

'use strict'

const express = require('express')
const bodyParser = require('body-parser')
const fs = require('fs')
const path = require('path')

const libdir = path.join(path.dirname(fs.realpathSync(__filename)), '..')
const tuvero = require(libdir + path.sep + 'state.js')

const app = express()

const PORT = 8080

app.use(bodyParser.json({ type: 'application/json' }))

function formatError (error) {
  return JSON.stringify({
    'error': 'Error while processing the request',
    'message': error ? error.message || error.msg || error : ''
  }, null, '  ')
}

let router = express.Router()

const allCommands = Object.keys(tuvero.commands)

const allRoutes = (function (commands) {
  var routes = {}
  commands.forEach(function (cmd) {
    routes[cmd] = `/${cmd}`
  })
  return routes
})(allCommands)

router.post('/:command', function (request, response, next) {
  let command = request.params.command || undefined
  let id = `${request.ip}->{${request.path}`;

  (new Promise((resolve, reject) => {
    if (!command || !tuvero.commands[command]) {
      return reject('Command not recognized. Available commands: ' + allCommands.join(', '))
    }
    if (!request.is('application/json')) {
      return reject('Content-Type must be application/json')
    }
    if (!request.body) {
      return reject('No JSON data received')
    }

    tuvero.parse(request.body)
      .then(state => {
        resolve(JSON.stringify(tuvero.commands[command](state), null, ' '))
      }).catch(reject)
  }))
    .then(jsonstring => {
      response.send(jsonstring)
    }).catch(error => {
      response.status(400).send(formatError(error))
    })
})

router.all('/', function (request, response, next) {
  response.send(JSON.stringify(allRoutes, null, ' '))
})

app.use('/', router)

app.listen(PORT)

console.log('listening on port ' + PORT)
{'fraction_non_alphanumeric': 0.18129421011265393, 'fraction_numerical': 0.0036678019386953103, 'mean_word_length': 3.8513341804320205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4405764', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1211, 'n_words': 261}