<commit_before>var concat = require('concat-stream')
var mock = require('mock-data')
var mkdirp = require('mkdirp')
var hyperquest = require('hyperquest')
var debug = require('debug')('mapeo-mock-device')
var path = require('path')
var crypto = require('crypto')
var Mapeo = require('mapeo-server')
var blobstore = require('safe-fs-blob-store')
var osmdb = require('osm-p2p')
var http = require('http')

const MOCK_DATA = 500

module.exports = createMockDevice

function createMockDevice (dir, opts) {
  if (!opts) opts = {}
  mkdirp.sync(path.join(dir, 'osm'))
  mkdirp.sync(path.join(dir, 'media'))
  var osm = osmdb(path.join(dir, 'osm'))
  var media = blobstore(path.join(dir, 'media'))
  var mapeo = Mapeo(osm, media, opts)
  mapeo.api.core.sync.setName(crypto.randomBytes(8).toString('hex'))
  var server = http.createServer(function (req, res) {
    if (!mapeo.handle(req, res)) {
      res.statusCode = 404
      res.end('404')
    }
  })
  server.on('error', function (err) {
    console.trace(err)
  })

  server.on('close', function () {
    console.log('closing mapeo')
    server.mapeo.api.close()
  })

  server.mapeo = mapeo
  server.shutdown = shutdown
  server.shutdown.bind(server)
  server.turnOn = turnOn
  server.turnOn.bind(server)
  server.openSyncScreen = openSyncScreen
  server.openSyncScreen.bind(server)
  server.closeSyncScreen = closeSyncScreen
  server.closeSyncScreen.bind(server)
  server.createMockData = createMockData
  server.createMockData.bind(server)
  return server
}

var DEFAULT_PORT = 5006

function turnOn (opts, cb) {
  var server = this
  if (typeof opts === 'number' || typeof opts === 'string') opts = { port: opts }
  if (typeof opts === 'function') {
    cb = opts
    opts = {}
  }
  var port = opts.port || DEFAULT_PORT

  server.listen(port, function () {
    server.mapeo.api.core.sync.listen(cb)
  })
}

function call (url, cb) {
  var hq2 = hyperquest.get(url, {})
  hq2.pipe(concat({ encoding: 'string' }, function (body) {
    debug('announced', body)
    if (cb) return cb(body)
  }))
  hq2.end()
}

function openSyncScreen (cb) {
  var server = this
  var port = server.address().port
  debug('announcing')
  call(`http://localhost:${port}/sync/join`, cb)
}

function closeSyncScreen (cb) {
  var server = this
  debug('unannouncing')
  var port = server.address().port
  call(`http://localhost:${port}/sync/leave`, cb)
}

function createMockData (count, cb) {
  if (!cb) {
    cb = count
    count = 1
  }
  var bounds = [-78.3155, -3.3493, -74.9871, 0.6275]
  bounds = bounds.map((b) => b * 100)

  var server = this
  var port = server.address().port
  var base = `http://localhost:${port}`
  var fpath = encodeURIComponent(path.join(__dirname, '..', 'hi-res.jpg'))

  mock.generate({
    type: 'integer',
    count: count,
    params: { start: bounds[0], end: bounds[2] }
  }, function (err, lons) {
    if (err) throw err
    mock.generate({
      type: 'integer',
      count: count,
      params: { start: bounds[1], end: bounds[3] }
    }, function (err, lats) {
      if (err) throw err
      lons.forEach((lon, i) => {
        var obs = {
          type: 'observation',
          lat: lats[i] / 100,
          lon: lon / 100,<commit_msg>fix hi-res reference in mock data<commit_after>var concat = require('concat-stream')
var mock = require('mock-data')
var mkdirp = require('mkdirp')
var hyperquest = require('hyperquest')
var debug = require('debug')('mapeo-mock-device')
var path = require('path')
var crypto = require('crypto')
var Mapeo = require('mapeo-server')
var blobstore = require('safe-fs-blob-store')
var osmdb = require('osm-p2p')
var http = require('http')

const MOCK_DATA = 500

module.exports = createMockDevice

function createMockDevice (dir, opts) {
  if (!opts) opts = {}
  mkdirp.sync(path.join(dir, 'osm'))
  mkdirp.sync(path.join(dir, 'media'))
  var osm = osmdb(path.join(dir, 'osm'))
  var media = blobstore(path.join(dir, 'media'))
  var mapeo = Mapeo(osm, media, opts)
  mapeo.api.core.sync.setName(crypto.randomBytes(8).toString('hex'))
  var server = http.createServer(function (req, res) {
    if (!mapeo.handle(req, res)) {
      res.statusCode = 404
      res.end('404')
    }
  })
  server.on('error', function (err) {
    console.trace(err)
  })

  server.on('close', function () {
    console.log('closing mapeo')
    server.mapeo.api.close()
  })

  server.mapeo = mapeo
  server.shutdown = shutdown
  server.shutdown.bind(server)
  server.turnOn = turnOn
  server.turnOn.bind(server)
  server.openSyncScreen = openSyncScreen
  server.openSyncScreen.bind(server)
  server.closeSyncScreen = closeSyncScreen
  server.closeSyncScreen.bind(server)
  server.createMockData = createMockData
  server.createMockData.bind(server)
  return server
}

var DEFAULT_PORT = 5006

function turnOn (opts, cb) {
  var server = this
  if (typeof opts === 'number' || typeof opts === 'string') opts = { port: opts }
  if (typeof opts === 'function') {
    cb = opts
    opts = {}
  }
  var port = opts.port || DEFAULT_PORT

  server.listen(port, function () {
    server.mapeo.api.core.sync.listen(cb)
  })
}

function call (url, cb) {
  var hq2 = hyperquest.get(url, {})
  hq2.pipe(concat({ encoding: 'string' }, function (body) {
    debug('announced', body)
    if (cb) return cb(body)
  }))
  hq2.end()
}

function openSyncScreen (cb) {
  var server = this
  var port = server.address().port
  debug('announcing')
  call(`http://localhost:${port}/sync/join`, cb)
}

function closeSyncScreen (cb) {
  var server = this
  debug('unannouncing')
  var port = server.address().port
  call(`http://localhost:${port}/sync/leave`, cb)
}

function createMockData (count, cb) {
  if (!cb) {
    cb = count
    count = 1
  }
  var bounds = [-78.3155, -3.3493, -74.9871, 0.6275]
  bounds = bounds.map((b) => b * 100)

  var server = this
  var port = server.address().port
  var base = `http://localhost:${port}`
  var fpath = encodeURIComponent(path.join(__dirname, '..', 'test', 'hi-res.jpg'))

  mock.generate({
    type: 'integer',
    count: count,
    params: { start: bounds[0], end: bounds[2] }
  }, function (err, lons) {
    if (err) throw err
    mock.generate({
      type: 'integer',
      count: count,
      params: { start: bounds[1], end: bounds[3] }
    }, function (err, lats) {
      if (err) throw err
      lons.forEach((lon, i) => {
        var obs = {
          type: 'observation',
          lat: lats[i] / 100,
          lon: lon / 100,
{'fraction_non_alphanumeric': 0.16294227188081936, 'fraction_numerical': 0.01675977653631285, 'mean_word_length': 3.4204389574759944, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6455150', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2252, 'n_words': 521}