<commit_before>
var express   = require('express')
  , fs        = require('fs')
  , pkg       = require('./package.json')
  , dbs       = {}
  , app       = module.exports = express()
  , Pouch     = module.exports.Pouch = require('pouchdb');

// We'll need this for the _all_dbs route.
Pouch.enableAllDbs = true;

app.configure(function () {
  app.use(function (req, res, next) {
    var opts = {}
      , data = ''
      , prop;

    // Normalize query string parameters for direct passing
    // into Pouch queries.
    for (prop in req.query) {
      try {
        req.query[prop] = JSON.parse(req.query[prop]);
      } catch (e) {}
    }

    // Custom bodyParsing because express.bodyParser() chokes
    // on `malformed` requests.
    req.on('data', function (chunk) { data += chunk; });
    req.on('end', function () {
      if (data) {
        try {
          req.body = JSON.parse(data);
        } catch (e) {
          req.body = data;
        }
      }
      next();
    });
  });
});

// Root route, return welcome message
app.get('/', function (req, res, next) {
  res.send(200, {
    'express-pouchdb': 'Welcome!',
    'version': pkg.version
  });
});

// Generate UUIDs
app.get('/_uuids', function (req, res, next) {
  var count = req.query.count || 1
    , uuids = [];

  while (--count >= 0) { uuids.push(Pouch.uuid()); }
  res.send(200, {
    uuids: uuids
  });
});

// List all databases.
app.get('/_all_dbs', function (req, res, next) {
  Pouch.allDbs(function (err, response) {
    if (err) res.send(500, Pouch.UNKNOWN_ERROR);
    res.send(200, response);
  });
});

// Replicate a database
app.post('/_replicate', function (req, res, next) {

  var source = req.body.source
    , target = req.body.target
    , opts = { continuous: !!req.body.continuous };

  if (req.body.filter) opts.filter = req.body.filter;
  opts.complete = function (err, response) {
    if (err) return res.send(400, err);
    res.send(200, response);
  };

  Pouch.replicate(source, target, opts);

  // if continuous pull replication return 'ok' since we cannot wait for callback
  if (target in dbs && opts.continuous) {
    res.send(200, { ok : true });
  }

});

// Create a database.
app.put('/:db', function (req, res, next) {
  var name = encodeURIComponent(req.params.db);
  if (name in dbs) {
    return res.send(412, {
      'error': 'file_exists',
      'reason': 'The database could not be created.'<commit_msg>Restore @tahmmee's fix from (#6)<commit_after>
var express   = require('express')
  , fs        = require('fs')
  , pkg       = require('./package.json')
  , dbs       = {}
  , app       = module.exports = express()
  , Pouch     = module.exports.Pouch = require('pouchdb');

// We'll need this for the _all_dbs route.
Pouch.enableAllDbs = true;

app.configure(function () {
  app.use(function (req, res, next) {
    var opts = {}
      , data = ''
      , prop;

    // Normalize query string parameters for direct passing
    // into Pouch queries.
    for (prop in req.query) {
      try {
        req.query[prop] = JSON.parse(req.query[prop]);
      } catch (e) {}
    }

    // Custom bodyParsing because express.bodyParser() chokes
    // on `malformed` requests.
    req.on('data', function (chunk) { data += chunk; });
    req.on('end', function () {
      if (data) {
        try {
          req.body = JSON.parse(data);
        } catch (e) {
          req.body = data;
        }
      }
      next();
    });
  });
});

// Root route, return welcome message
app.get('/', function (req, res, next) {
  res.send(200, {
    'express-pouchdb': 'Welcome!',
    'version': pkg.version
  });
});

// Generate UUIDs
app.get('/_uuids', function (req, res, next) {
  var count = req.query.count || 1
    , uuids = [];

  while (--count >= 0) { uuids.push(Pouch.uuid()); }
  res.send(200, {
    uuids: uuids
  });
});

// List all databases.
app.get('/_all_dbs', function (req, res, next) {
  Pouch.allDbs(function (err, response) {
    if (err) res.send(500, Pouch.UNKNOWN_ERROR);
    res.send(200, response);
  });
});

// Replicate a database
app.post('/_replicate', function (req, res, next) {

  var source = req.body.source
    , target = req.body.target
    , opts = { continuous: !!req.body.continuous };

  if (req.body.filter) opts.filter = req.body.filter;
  if (req.body.query_params) opts.query_params = req.body.query_params;
  opts.complete = function (err, response) {
    if (err) return res.send(400, err);
    res.send(200, response);
  };

  Pouch.replicate(source, target, opts);

  // if continuous pull replication return 'ok' since we cannot wait for callback
  if (target in dbs && opts.continuous) {
    res.send(200, { ok : true });
  }

});

// Create a database.
app.put('/:db', function (req, res, next) {
  var name = encodeURIComponent(req.params.db);
  if (name in dbs) {
    return res.send(412, {
      'error': 'file_exists',
      'reason': 'The database could not be created.'
{'fraction_non_alphanumeric': 0.16980749746707194, 'fraction_numerical': 0.010739614994934144, 'mean_word_length': 2.5434314429289304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '689380', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1673, 'n_words': 425}