<commit_before>var express = require('express')
var FsDb = require('fs-db')
var pull = require('pull-stream')
var sendJson = require('send-data/json')
var debug = require('debug')('holodex:api')
var yaml = require('js-yaml')

var types = require('types')

module.exports = createApi

function createApi (config) {
  debug('config', config)

  var api = express()

  var db = FsDb({
    location: config.data.location,
    idAttribute: '@id',
    codec: {
      type: 'yml',
      encode: function (obj) {
        return yaml.safeDump(obj)
      },
      decode: function (str) {
        return yaml.safeLoad(str)
      }
    }
  })

  var graph = undefined
  var queue = []

  pull(
    db.createReadStream(),
    pull.map(function (item) {
      item['@id'] = item.id
      delete item.id
      return item
    }),
    pull.collect(function (err, arr) {
      if (err) { throw err }

      graph = arr.map(function (attrs) {
        var Model = types[attrs['@type']].Model
        return new Model(attrs)
      })

      queue.forEach(function (cb) { cb() })
      queue = []
    })
  )

  api.get('/*', function (req, res) {
    debug('get', req.path)
    if (graph == null) {
      queue.push(sendGraph.bind(null, req, res))
    } else {
      sendGraph(req, res)
    }<commit_msg>clone data coming out of fs-db before changing it<commit_after>var express = require('express')
var FsDb = require('fs-db')
var pull = require('pull-stream')
var sendJson = require('send-data/json')
var debug = require('debug')('holodex:api')
var yaml = require('js-yaml')
var clone = require('lodash').clone

var types = require('types')

module.exports = createApi

function createApi (config) {
  debug('config', config)

  var api = express()

  var db = FsDb({
    location: config.data.location,
    idAttribute: '@id',
    codec: {
      type: 'yml',
      encode: function (obj) {
        return yaml.safeDump(obj)
      },
      decode: function (str) {
        return yaml.safeLoad(str)
      }
    }
  })

  var graph = undefined
  var queue = []

  pull(
    db.createReadStream(),
    pull.map(function (item) {
      attrs = clone(item)
      attrs['@id'] = attrs.id
      delete attrs.id
      return attrs
    }),
    pull.collect(function (err, arr) {
      if (err) { throw err }

      graph = arr.map(function (attrs) {
        var Model = types[attrs['@type']].Model
        return new Model(attrs)
      })

      queue.forEach(function (cb) { cb() })
      queue = []
    })
  )

  api.get('/*', function (req, res) {
    debug('get', req.path)
    if (graph == null) {
      queue.push(sendGraph.bind(null, req, res))
    } else {
      sendGraph(req, res)
    }
{'fraction_non_alphanumeric': 0.15655430711610488, 'fraction_numerical': 0.0, 'mean_word_length': 2.514473684210526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5370105', 'n_tokens_mistral': 892, 'n_tokens_neox': 868, 'n_words': 215}