All files / json-crdt-server/src/routes index.ts

61.11% Statements 22/36
0% Branches 0/9
50% Functions 2/4
61.29% Lines 19/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 514x 4x 4x 4x 4x 4x 4x 4x 4x 4x       4x 93x 93x           93x     4x 93x 93x                   93x     4x                      
import {routes} from './routes';
import {RpcError} from '@jsonjoy.com/rpc-error';
import {Value, ObjValue} from '@jsonjoy.com/json-type';
import {TypedCallee} from '@jsonjoy.com/rpc-calls';
import {TypedRpcError} from '@jsonjoy.com/rpc-calls/lib/callee/error/typed';
import {system} from './system';
import {Services} from '../services/Services';
import {MemoryStore} from '../services/blocks/store/MemoryStore';
import {LevelStore} from '../services/blocks/store/level/LevelStore';
import {ClassicLevel} from 'classic-level';
import type {Store} from '../services/blocks/store/types';
import type {RouteDeps} from './types';
 
export const createRouter = (services: Services) => {
  const router = ObjValue.new(system);
  const deps: RouteDeps = {
    services,
    router,
    system,
    t: system.t,
  };
  return routes(deps)(router);
};
 
export const createCaller = (services: Services = new Services()) => {
  const router = createRouter(services);
  const caller = new TypedCallee<any, typeof router>({
    router,
    wrapInternalError: (error: unknown) => {
      if (error instanceof Value) return error;
      if (error instanceof RpcError) return TypedRpcError.value(error);
      // tslint:disable-next-line:no-console
      console.error(error);
      return TypedRpcError.valueFrom(error);
    },
  });
  return {router, caller, services};
};
 
export const createServices = async () => {
  let store: Store = new MemoryStore();
  if (process.env.JSON_CRDT_STORE === 'level') {
    const path = process.env.JSON_CRDT_STORE_PATH || './db';
    const kv = new ClassicLevel<string, Uint8Array>(path, {valueEncoding: 'view'});
    await kv.open();
    store = new LevelStore(<any>kv);
  }
  const services = new Services({store});
  return services;
};