All files / src/Program/services ogmios.ts

100% Statements 19/19
100% Branches 2/2
100% Functions 5/5
100% Lines 15/15

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        38x 38x 38x 38x                 38x         3x   1x         38x         16x 3x 13x 11x       2x     38x         8x  
/* eslint-disable promise/no-nesting */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { DnsResolver } from '../utils';
import { Logger } from 'ts-log';
import { MissingCardanoNodeOption } from '../errors';
import { OgmiosCardanoNode, OgmiosObservableCardanoNode, urlToConnectionConfig } from '@cardano-sdk/ogmios';
import { OgmiosOptionDescriptions, OgmiosProgramOptions } from '../options/ogmios';
import { defer, from, of } from 'rxjs';
 
/**
 * Creates an ObservableOgmiosCardanoNode instance :
 * - use passed srv service name in order to resolve the port
 * - all other operations are bind to pool object without modifications
 *
 * @returns ObservableOgmiosCardanoNode instance
 */
export const ogmiosObservableCardanoNodeWithDiscovery = (
  dnsResolver: DnsResolver,
  logger: Logger,
  serviceName: string
): OgmiosObservableCardanoNode =>
  new OgmiosObservableCardanoNode(
    {
      connectionConfig$: defer(() => from(dnsResolver(serviceName).then(({ name, port }) => ({ host: name, port }))))
    },
    { logger }
  );
 
export const getOgmiosObservableCardanoNode = (
  dnsResolver: DnsResolver,
  logger: Logger,
  options?: OgmiosProgramOptions
): OgmiosObservableCardanoNode => {
  if (options?.ogmiosSrvServiceName)
    return ogmiosObservableCardanoNodeWithDiscovery(dnsResolver, logger, options.ogmiosSrvServiceName);
  if (options?.ogmiosUrl)
    return new OgmiosObservableCardanoNode(
      { connectionConfig$: of(urlToConnectionConfig(options.ogmiosUrl)) },
      { logger }
    );
  throw new MissingCardanoNodeOption([OgmiosOptionDescriptions.Url, OgmiosOptionDescriptions.SrvServiceName]);
};
 
export const getOgmiosCardanoNode = async (
  dnsResolver: DnsResolver,
  logger: Logger,
  options?: OgmiosProgramOptions
): Promise<OgmiosCardanoNode> =>
  new OgmiosCardanoNode(getOgmiosObservableCardanoNode(dnsResolver, logger, options), logger);