Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions packages/cardano-services-client/src/WebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/* eslint-disable unicorn/prefer-add-event-listener */

import {
AsyncReturnType,
Cardano,
EpochInfo,
EraSummary,
NetworkInfoMethods,
HealthCheckResponse,
NetworkInfoProvider,
Provider,
ProviderError,
ProviderFailure,
StakeSummary,
SupplySummary,
WSMessage,
WsProvider,
createSlotEpochInfoCalc
} from '@cardano-sdk/core';
import { Logger } from 'ts-log';
Expand All @@ -22,6 +20,19 @@ import WebSocket from 'isomorphic-ws';

const NOT_CONNECTED_ID = 'not-connected';

export type AsyncReturnType<F extends () => unknown> = F extends () => Promise<infer R> ? R : never;

export type NetworkInfoMethods = Exclude<keyof NetworkInfoProvider, 'healthCheck'>;
export type NetworkInfoResponses = { [m in NetworkInfoMethods]: AsyncReturnType<NetworkInfoProvider[m]> };

export interface WSMessage {
/** The client id assigned by the server. */
clientId?: string;

/** Latest value(s) for the `NetworkInfoProvider` methods.*/
networkInfo?: Partial<NetworkInfoResponses>;
}

type WSStatus = 'connecting' | 'connected' | 'idle' | 'stop';

export type WSHandler = (message: WSMessage) => void;
Expand Down Expand Up @@ -54,6 +65,35 @@ const isEventError = (error: unknown): error is { error: Error } =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typeof error === 'object' && !!error && (error as any).error instanceof Error;

export class WsProvider implements Provider {
/** Emits the health state. */
public health$: Observable<HealthCheckResponse>;

private healthSubject$: ReplaySubject<HealthCheckResponse>;
private reason?: string;

constructor() {
this.health$ = this.healthSubject$ = new ReplaySubject<HealthCheckResponse>(1);
this.healthSubject$.next({ ok: false, reason: 'starting' });
}

protected emitHealth(reason?: string, overwrite?: boolean) {
if (!reason) {
this.reason = undefined;

return this.healthSubject$.next({ ok: true });
}

if (overwrite || !this.reason) this.reason = reason;

this.healthSubject$.next({ ok: false, reason: this.reason });
}

public healthCheck() {
return firstValueFrom(this.health$);
}
}

export class CardanoWsClient extends WsProvider {
/** The client id, assigned by the server. */
clientId = NOT_CONNECTED_ID;
Expand Down
11 changes: 2 additions & 9 deletions packages/cardano-services/src/WsServer/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {
Cardano,
CardanoNode,
NetworkInfoResponses,
Seconds,
WSMessage,
WsProvider,
createSlotEpochInfoCalc
} from '@cardano-sdk/core';
import { Cardano, CardanoNode, Seconds, createSlotEpochInfoCalc } from '@cardano-sdk/core';
import { GenesisData } from '..';
import { Logger } from 'ts-log';
import { NetworkInfoResponses, WSMessage, WsProvider } from '@cardano-sdk/cardano-services-client';
import { Notification, Pool } from 'pg';
import { Server, createServer } from 'http';
import { WebSocket, WebSocketServer } from 'ws';
Expand Down
44 changes: 0 additions & 44 deletions packages/core/src/WebSocket.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export * from './Provider';
export * from './util';
export * from './errors';
export * from './CardanoNode';
export * from './WebSocket';
4 changes: 2 additions & 2 deletions packages/e2e/test/ws-server/webSocket.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CardanoWsClient } from '@cardano-sdk/cardano-services-client';
import { CardanoWsClient, WsProvider } from '@cardano-sdk/cardano-services-client';
import {
CardanoWsServer,
GenesisData,
createDnsResolver,
getOgmiosCardanoNode,
util
} from '@cardano-sdk/cardano-services';
import { HealthCheckResponse, WsProvider } from '@cardano-sdk/core';
import { HealthCheckResponse } from '@cardano-sdk/core';
import { OgmiosCardanoNode } from '@cardano-sdk/ogmios';
import { Pool } from 'pg';
import { filter, firstValueFrom } from 'rxjs';
Expand Down
Loading