From 26e16bb865df22d2875d870f5aa38c24eb0c5814 Mon Sep 17 00:00:00 2001 From: Jeremie Pardou-Piquemal <571533+jrmi@users.noreply.github.com> Date: Sat, 6 Mar 2021 14:39:44 +0100 Subject: [PATCH] Add repl to access store manually from shell --- src/cli.js | 60 ++++++++++++++++++++++++++++++++++++++------ src/middleware.js | 11 ++------ src/store.js | 2 +- src/storeBackends.js | 9 +++++++ 4 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/cli.js b/src/cli.js index 0a8a041..bd6b524 100644 --- a/src/cli.js +++ b/src/cli.js @@ -2,18 +2,62 @@ import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import startServer from './server.js'; import { generateKey } from './crypt.js'; +import repl from 'repl'; +import { getStoreBackend, wrapBackend } from './storeBackends.js'; -const argv = yargs(hideBin(process.argv)) +import { + STORE_BACKEND, + STORE_PREFIX, + NEDB_BACKEND_DIRNAME, +} from './settings.js'; + +yargs(hideBin(process.argv)) .usage('Usage: $0 [options]') + .command( + '$0', + 'Start the ricochetjs server', + () => {}, + (argv) => { + if (argv.generateKey) { + const key = generateKey(); + console.log(`Key: ${key}`); + return; + } + console.log('Should start the server'); + startServer(); + } + ) + .command( + ['generatekey', 'generateKey'], + 'Generate random encryption key', + () => {}, + () => { + const key = generateKey(); + console.log(`Key: ${key}`); + } + ) + .command( + 'shell ', + 'Open a shell for specified siteId', + () => {}, + (argv) => { + const siteId = argv.siteId; + + const storeConfig = { + prefix: STORE_PREFIX, + dirname: NEDB_BACKEND_DIRNAME, + }; + + // Create JSON wrapped store backend + const storeBackend = getStoreBackend(STORE_BACKEND, storeConfig); + const store = wrapBackend(storeBackend, siteId); + + const r = repl.start('> '); + r.context.store = store; + } + ) .boolean(['generate-key']) .describe('generate-key', 'Generate random encryption key') .help('h') .version() .alias('h', 'help').argv; - -if (argv.generateKey) { - const key = generateKey(); - console.log(`Key: ${key}`); -} else { - startServer(); -} diff --git a/src/middleware.js b/src/middleware.js index 6a73538..cabd83f 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -8,7 +8,7 @@ import log from './log.js'; import fileStore from './fileStore.js'; import store from './store.js'; -import { NeDBBackend, memoryBackend, wrapBackend } from './storeBackends.js'; +import { getStoreBackend, wrapBackend } from './storeBackends.js'; import remote from './remote.js'; import execute from './execute.js'; @@ -41,14 +41,7 @@ export const middleware = ({ ); // JSON store backend - let storeBackend; - switch (storeConfig.type) { - case 'nedb': - storeBackend = NeDBBackend({ dirname: storeConfig.dirname }); - break; - default: - storeBackend = memoryBackend(); - } + const storeBackend = getStoreBackend(storeConfig.type, storeConfig); const site = {}; diff --git a/src/store.js b/src/store.js index 3d02bff..1fc6bb6 100644 --- a/src/store.js +++ b/src/store.js @@ -1,5 +1,5 @@ import express from 'express'; -import { memoryBackend, wrapBackend } from './storeBackends.js'; +import { memoryBackend, NeDBBackend, wrapBackend } from './storeBackends.js'; // Utility functions diff --git a/src/storeBackends.js b/src/storeBackends.js index d10ff4c..5640981 100644 --- a/src/storeBackends.js +++ b/src/storeBackends.js @@ -92,6 +92,15 @@ export const wrapBackend = (backend, siteId, userId) => { }; }; +export const getStoreBackend = (type, options = {}) => { + switch (type) { + case 'nedb': + return NeDBBackend(options); + default: + return memoryBackend(); + } +}; + // Memory backend for proof of concept export const memoryBackend = () => { const dataMemoryStore = {};