<commit_before>import endsWith from 'lodash/endsWith'
import levelup from 'level-party'
import startsWith from 'lodash/startsWith'
import sublevel from 'level-sublevel'
import { ensureDir } from 'fs-extra'

import { forEach, isFunction, promisify } from '../utils'

// ===================================================================

const _levelHas = function has(key, cb) {
  if (cb) {
    return this.get(key, (error, value) =>
      error ? (error.notFound ? cb(null, false) : cb(error)) : cb(null, true)
    )
  }

  try {
    this.get(key)
    return true
  } catch (error) {
    if (!error.notFound) {
      throw error
    }
  }
  return false
}
const levelHas = db => {
  db.has = _levelHas

  return db
}

const levelPromise = db => {
  const dbP = {}
  forEach(db, (value, name) => {
    if (!isFunction(value)) {
      return
    }

    if (endsWith(name, 'Stream') || startsWith(name, 'is')) {
      dbP[name] = db::value
    } else {
      dbP[name] = promisify(value, db)
    }
  })

  return dbP
}

// ===================================================================

export default class {
  constructor(xo) {
    const dir = `${xo._config.datadir}/leveldb`
    this._db = ensureDir(dir).then(() => {
      return sublevel(
        levelup(dir, {
          valueEncoding: 'json',
        })
      )
    })
  }

  getStore(namespace) {
    return this._db.then(db => levelPromise(levelHas(db.sublevel(namespace))))
  }
}<commit_msg>chore(xo-server/store): dont access xo._config<commit_after>import endsWith from 'lodash/endsWith'
import levelup from 'level-party'
import startsWith from 'lodash/startsWith'
import sublevel from 'level-sublevel'
import { ensureDir } from 'fs-extra'

import { forEach, isFunction, promisify } from '../utils'

// ===================================================================

const _levelHas = function has(key, cb) {
  if (cb) {
    return this.get(key, (error, value) =>
      error ? (error.notFound ? cb(null, false) : cb(error)) : cb(null, true)
    )
  }

  try {
    this.get(key)
    return true
  } catch (error) {
    if (!error.notFound) {
      throw error
    }
  }
  return false
}
const levelHas = db => {
  db.has = _levelHas

  return db
}

const levelPromise = db => {
  const dbP = {}
  forEach(db, (value, name) => {
    if (!isFunction(value)) {
      return
    }

    if (endsWith(name, 'Stream') || startsWith(name, 'is')) {
      dbP[name] = db::value
    } else {
      dbP[name] = promisify(value, db)
    }
  })

  return dbP
}

// ===================================================================

export default class {
  constructor(xo, config) {
    const dir = `${config.datadir}/leveldb`
    this._db = ensureDir(dir).then(() => {
      return sublevel(
        levelup(dir, {
          valueEncoding: 'json',
        })
      )
    })
  }

  getStore(namespace) {
    return this._db.then(db => levelPromise(levelHas(db.sublevel(namespace))))
  }
}
{'fraction_non_alphanumeric': 0.22802850356294538, 'fraction_numerical': 0.0, 'mean_word_length': 3.0218281036834926, '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': '661317', 'n_tokens_mistral': 979, 'n_tokens_neox': 949, 'n_words': 214}