-
Notifications
You must be signed in to change notification settings - Fork 1
Added websocket support #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c2c8f75
7420d05
c405c8e
c626e2f
46d3619
750203a
93a3b39
5b07eee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export function getConsumerClientFactory() { | ||
| const factories = {}; | ||
|
|
||
| // todo: can we have both clients on node.js ? | ||
| // conditionally require respective clients. | ||
| if (isNodejs()) { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const grpcClient = require("../streamdbConsumerClient/grpcClient"); | ||
| factories["grpc"] = (endpoint) => new grpcClient.StreamDBConsumerGrpcClient(endpoint); | ||
| } else { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const httpClient = require("../streamdbConsumerClient/httpClient"); | ||
| factories["http"] = (endpoint) => new httpClient.StreamDBConsumerHttpClient(endpoint); | ||
| } | ||
|
|
||
| return factories; | ||
| } | ||
|
|
||
| function isNodejs() { | ||
| return typeof process === "object" && | ||
| typeof require === "function"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { StreamEvent, Offset } from "../model"; | ||
| import { PausableStream, StreamController } from "../lib/pausableStream"; | ||
|
|
||
| export interface StreamDBConsumerClient { | ||
| getEvents( | ||
| stream: string, | ||
| offset: Offset, | ||
| count: number, | ||
| direction: "next" | "last" | ||
| ): Promise<StreamEvent[]>; | ||
|
|
||
| getEventsStream( | ||
| stream: string, | ||
| offset: Offset, | ||
| controller?: StreamController | ||
| ): PausableStream<StreamEvent>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import {Offset, StreamEvent} from "../model"; | ||
| import {Axios} from "axios"; | ||
| import { | ||
| GetStateTransitionsResponse, | ||
| StreamStateTransitionsResponse | ||
| } from "../gen/stream_consumer/v1alpha1/stream_consumer"; | ||
| import {stateTransitionProtoToStreamEvent} from "../streamdb/converters"; | ||
| import {PausableStream, StreamController} from "../lib/pausableStream"; | ||
| import {ExponentialBackoff, WebsocketBuilder} from "websocket-ts"; | ||
| import {StreamDBConsumerClient} from "./consumerClient"; | ||
|
|
||
| export class StreamDBConsumerHttpClient implements StreamDBConsumerClient { | ||
| private readonly client: Axios; | ||
|
|
||
| constructor(private readonly uri: string) { | ||
| this.client = new Axios({ | ||
| baseURL: "https://" + this.uri, | ||
| validateStatus: status => | ||
| (status >= 200 && status < 300) || status == 404, | ||
| }); | ||
| } | ||
|
|
||
| public getEvents( | ||
| stream: string, | ||
| offset: Offset, | ||
| count: number, | ||
| direction: "next" | "last" | ||
| ): Promise<StreamEvent[]> { | ||
| return this.client | ||
| .post(`/api/consumer/${stream}/transitions`, | ||
| JSON.stringify({ | ||
| offset: offset, | ||
| count: count, | ||
| direction: direction.toUpperCase(), | ||
| }, (key, value) => (typeof value === "bigint" ? value.toString() : value) | ||
| ) | ||
| ).then(resp => | ||
| (JSON.parse(resp.data) as GetStateTransitionsResponse) | ||
| .stateTransitions | ||
| .map(stateTransitionProtoToStreamEvent) | ||
| ); | ||
| } | ||
|
|
||
| public getEventsStream( | ||
| stream: string, | ||
| offset: Offset, | ||
| controller?: StreamController | ||
| ): PausableStream<StreamEvent> { | ||
| return PausableStream.create<StreamEvent>((observer, _) => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ivanbenko is it necessary to use PauseState here? I haven't dove deeply into its internals
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, |
||
| new WebsocketBuilder(webSocketUriForOffset(this.uri, stream, offset)) | ||
| .onClose(() => observer.complete()) | ||
| .onError((_, ev) => observer.error(ev.type)) | ||
| .onMessage((_, ev) => | ||
| (JSON.parse(ev.data)["result"] as StreamStateTransitionsResponse) | ||
| .stateTransition | ||
| .map(stateTransitionProtoToStreamEvent) | ||
| .forEach(transition => observer.next(transition))) | ||
| .withBackoff(new ExponentialBackoff(100, 7)) | ||
| .build(); | ||
| }, controller); | ||
| } | ||
| } | ||
|
|
||
| function webSocketUriForOffset(endpoint: string, streamId: string, offset: Offset): string { | ||
| const params = new URLSearchParams({ | ||
| "offset.height": offset.height.toString(), | ||
| "offset.timestamp.epochMs": offset.timestamp.epochMs.toString(), | ||
| }); | ||
|
|
||
| // creates "&offset.timestamp.parts=parts[0]&offset.timestamp.parts=parts[1]" etc. string | ||
| const parts = ["", ...offset.timestamp.parts].join("&offset.timestamp.parts="); | ||
|
|
||
| return `wss://${endpoint}/api/consumer/${streamId}/stream?${params.toString()}${parts}`; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we will be implementing this in the future it's worth mentioning that websockets are not working properly inside Node.js out of the box for some reason. So websocket client for Node.js won't be only about client creation