Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
DataStore.prototype.connect = function ({database, collection}) {
if (this._mockIsDisconnected(collection)) {
this.readyState = STATE_DISCONNECTED
return Promise.reject(new Error('DB_DISCONNECTED'))
}
const connectionKey = `${database}.db`
if (databasePool[connectionKey]) {
this.database = databasePool[connectionKey]
} else {
this.database = new Loki(connectionKey, {
adapter: new Loki.LokiMemoryAdapter()
})
// For debugging
this.database.___id = {
name: database,
uuid: Math.random()
}
this._debug('connect: new db', {
database,
collection
})
databasePool[connectionKey] = this.database
}
useIncrementalIDB: boolean,
): mixed {
if (adapter) {
return adapter
} else if (await isIDBAvailable()) {
if (useIncrementalIDB) {
const IncrementalIDBAdapter = require('lokijs/src/incremental-indexeddb-adapter')
return new IncrementalIDBAdapter()
}
const LokiIndexedAdapter = require('lokijs/src/loki-indexed-adapter')
return new LokiIndexedAdapter(name)
}
// if IDB is unavailable (that happens in private mode), fall back to memory adapter
// we could also fall back to localstorage adapter, but it will fail in all but the smallest dbs
return new LokiMemoryAdapter()
}
// npm packages
const path = require('path');
const Loki = require('lokijs');
// our packages
const {baseFolder} = require('../config');
// TTL for auth requests
const REQ_TTL =
process.env.NODE_ENV !== 'testing'
? 5 * 60 * 1000 // 5 mins for prod
: 0; // 0 for tests
// init in-memory adapter for login requests
const memAdapter = new Loki.LokiMemoryAdapter();
const fsAdapter = new Loki.LokiFsAdapter();
// init in-memory requests db
const db = new Loki('requests.db', {adapter: memAdapter, autoload: true});
// init persistent tokens db
let tokenCollection = {};
const tokenDb = new Loki(path.join(baseFolder, 'auth.db'), {
adapter: process.env.NODE_ENV !== 'testing' ? fsAdapter : memAdapter,
autoload: true,
autoloadCallback() {
// get of create tokens collection
tokenCollection = tokenDb.getCollection('tokens');
if (tokenCollection === null) {
tokenCollection = tokenDb.addCollection('tokens');
}
},
const generateCommandData = require('./test_utils').generateCommandData;
const sinon = require('sinon');
const crypto = require('crypto');
const consts = require('../lib/constants');
const test_modules = [
{
name: "cache_ram",
path: "../lib/cache/cache_ram",
options: {
cachePath: tmp.tmpNameSync({}),
pageSize: 1024 * 1024,
minFreeBlockSize: 1024,
persistenceOptions: {
autosave: false,
adapter: new loki.LokiMemoryAdapter()
},
highReliability: false
}
},
{
name: "cache_fs",
path: "../lib/cache/cache_fs",
options: {
cachePath: tmp.tmpNameSync({}),
highReliability: false,
persistenceOptions: {
autosave: false
}
}
}
];
const test_modules = [
{
tmpDir: tmp.dirSync({unsafeCleanup: true}),
name: "cache_ram",
path: "../lib/cache/cache_ram",
options: {
pageSize: MAX_FILE_SIZE,
minFreeBlockSize: 1024,
highReliability: true,
highReliabilityOptions: {
reliabilityThreshold: 0
},
persistenceOptions: {
autosave: false,
adapter: new loki.LokiMemoryAdapter()
}
}
},
{
tmpDir: tmp.dirSync({unsafeCleanup: true}),
name: "cache_fs",
path: "../lib/cache/cache_fs",
options: {
highReliability: true,
highReliabilityOptions: {
reliabilityThreshold: 0
},
persistenceOptions: {
autosave: false
}
}