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

97.36% Statements 37/38
100% Branches 0/0
92.85% Functions 13/14
97.29% Lines 36/37

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 754x 4x     4x 4x 93x 93x 93x 93x 93x 1x       4x 4x 93x 93x 93x 93x 93x     4x 4x 93x 93x 93x                     93x 93x 1x                     4x 4x 93x 93x 93x 93x 93x             4x 4x 93x   93x          
import {objToModule} from '@jsonjoy.com/json-type/lib/typescript/converter';
import {toText} from '@jsonjoy.com/json-type/lib/typescript/toText';
import type {RouteDeps, Router, RouterBase} from '../types';
 
export const ping =
  ({t}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    const Request = t.undef;
    const Response = t.Const(<const>'pong');
    const Func = t.Function(Request, Response);
    return r.add('util.ping', Func, async () => {
      return 'pong' as const;
    });
  };
 
export const echo =
  ({t}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    const Request = t.any;
    const Response = t.any;
    const Func = t.Function(Request, Response);
    return r.add('util.echo', Func, async (msg: any) => msg);
  };
 
export const info =
  ({t, services}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    const Request = t.any;
    const Response = t.Object(
      t.Key('now', t.num),
      t.Key(
        'stats',
        t.Object(
          t.Key('pubsub', t.Object(t.Key('channels', t.num), t.Key('observers', t.num))),
          t.Key('presence', t.Object(t.Key('rooms', t.num), t.Key('entries', t.num), t.Key('observers', t.num))),
          t.Key('blocks', t.Object(t.Key('blocks', t.num), t.Key('batches', t.num))),
        ),
      ),
    );
    const Func = t.Function(Request, Response);
    return r.add('util.info', Func, async () => {
      return {
        now: Date.now(),
        stats: {
          pubsub: services.pubsub.stats(),
          presence: services.presence.stats(),
          blocks: services.blocks.stats(),
        },
      };
    });
  };
 
export const schema =
  ({t, router}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    const Request = t.any;
    const Response = t.Object(t.Key('typescript', t.str));
    const Func = t.Function(Request, Response);
    return r.add('util.schema', Func, async () => {
      return {
        typescript: toText(objToModule(router.type!)),
      };
    });
  };
 
export const util =
  (d: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) =>
    // biome-ignore format: each on its own line
    ( ping(d)
  ( echo(d)
  ( info(d)
  ( schema(d)
  ( r )))));