All files / json-crdt-repo/src/__tests__/e2e demo-client.ts

39.28% Statements 11/28
0% Branches 0/4
0% Functions 0/6
37.5% Lines 9/24

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 441x 1x 1x 1x 1x     1x 1x     1x                                   1x                            
import WebSocket from 'ws';
import {RxPersistentCaller} from '@jsonjoy.com/rpc-calls/lib/caller/RxPersistentCaller';
import {FetchCaller} from '@jsonjoy.com/rpc-calls/lib/caller/FetchCaller';
import {WebSocketChannel} from '@jsonjoy.com/channel/lib/WebSocketChannel';
import {RpcBinBatchCodec} from '@jsonjoy.com/rpc-codec/lib/RpcBinBatchCodec';
import type {RpcCodec} from '@jsonjoy.com/rpc-codec/lib/RpcCodec';
 
const secure = true;
const host = 'pub-1-api.jsonjoy.org';
// const host = '127.0.0.1:8080';
 
export const setupDemoServerPersistentClient = (codec: RpcCodec<any>) => {
  const url = `ws${secure ? 's' : ''}://${host}/rx`;
  const caller = new RxPersistentCaller({
    codec,
    physical: {
      newChannel: () =>
        new WebSocketChannel({
          newSocket: () => new WebSocket(url, [codec.specifier()]) as any,
        }),
    },
  });
  caller.start();
  const call = caller.call.bind(caller);
  const call$ = caller.call$.bind(caller);
  const stop = async () => void caller.stop();
  return {caller, call, call$, stop};
};
 
export const setupDemoServerFetchClient = (codec: RpcCodec<any>) => {
  const url = `http${secure ? 's' : ''}://${host}/rx`;
  const caller = new FetchCaller({
    url,
    codec: new RpcBinBatchCodec(codec),
    headers: {
      'Content-Type': `application/x.${codec.specifier()}`,
    },
  });
  const call = caller.call.bind(caller);
  const call$ = caller.call$.bind(caller);
  const stop = async () => void caller.stop();
  return {caller, call, call$, stop};
};