Skip to content

Commit

Permalink
Add repl to access store manually from shell
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Mar 6, 2021
1 parent 93445f9 commit 26e16bb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
60 changes: 52 additions & 8 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <siteId>',
'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();
}
11 changes: 2 additions & 9 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {};

Expand Down
2 changes: 1 addition & 1 deletion src/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { memoryBackend, wrapBackend } from './storeBackends.js';
import { memoryBackend, NeDBBackend, wrapBackend } from './storeBackends.js';

// Utility functions

Expand Down
9 changes: 9 additions & 0 deletions src/storeBackends.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down

0 comments on commit 26e16bb

Please sign in to comment.