-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ws proxy server and client (#75)
* feat: Add ws proxy server and client * minor clenup * fix token refresh flow * refactor server and connection handling * break down server proxy * add disposable interface * fix tests * bump deps
- Loading branch information
Showing
13 changed files
with
2,045 additions
and
1,434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDATCCAekCFANi/vgvNCE/QMwmMvTqYzJ8KUdSMA0GCSqGSIb3DQEBCwUAMD0x | ||
CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJXQTEQMA4GA1UEBwwHU2VhdHRsZTEPMA0G | ||
A1UECgwGSHVza2x5MB4XDTIzMTIwNzAxMzQyMVoXDTI0MDEwNjAxMzQyMVowPTEL | ||
MAkGA1UEBhMCVVMxCzAJBgNVBAgMAldBMRAwDgYDVQQHDAdTZWF0dGxlMQ8wDQYD | ||
VQQKDAZIdXNrbHkwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaRoG5 | ||
iMYVNQBLQtyc/98xisbWT7XEbRhWkxSgGep/G/1by/Y0YiI79fFK9Bw7mlabSmmj | ||
VnW5fI/iSGXp7WO1CWSwT5QNXpUnJ3igal38YhR1H3OCgi9VlJOiKLUi0omsKy48 | ||
fPp7mGUOxR4dSGdXMEobrOPH0PxpZpOUXmK6KKe+/iOxhw2uqN7B88vcSd9dwOEP | ||
JgSrXU5iTrcNqqHEr+/Dj8tKXKETNly5Gi6233owqDUggcZ0wjXeysR2uM0GV25c | ||
l7Hpar1csx6sYWfccmHoXdhMmAdyCS4dAGwN+J/jxqtb97jWqUSe8R4o/sIzu/fq | ||
bpTB8T/Dx1fJqxXrAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAALXmKSzQNzuS9mj | ||
FyUf9duuRPGcnRG95BBup0WQyyh8oQY8yEGPd1GnKmvBYIrLbdcuGA84IEUUC1ih | ||
MuWrfT1KZ+7KKC6cRD7AsXHZl1hh4xlssiz6muBrhJzSvs6jhBaHnQM72FVizMjC | ||
ET/B8GukZh2ba9nbg0JPn+IxgrDbMfBBtOPXIfZDpHsGHzoBPjrZjkHAXV8PaWWA | ||
k57FDqOjnl/h26vujBQgOZScTvLfvcIG+h/GQwd5VRAH9wJdo6WFQlBZ9SUvWOH1 | ||
nWLbx0ohVFAOE7RV1d50bQjoASBz6zHIhR3Odd2oovtqb+cHAlhbv+I9Mkr6U6QW | ||
VdXNs7M= | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDaRoG5iMYVNQBL | ||
Qtyc/98xisbWT7XEbRhWkxSgGep/G/1by/Y0YiI79fFK9Bw7mlabSmmjVnW5fI/i | ||
SGXp7WO1CWSwT5QNXpUnJ3igal38YhR1H3OCgi9VlJOiKLUi0omsKy48fPp7mGUO | ||
xR4dSGdXMEobrOPH0PxpZpOUXmK6KKe+/iOxhw2uqN7B88vcSd9dwOEPJgSrXU5i | ||
TrcNqqHEr+/Dj8tKXKETNly5Gi6233owqDUggcZ0wjXeysR2uM0GV25cl7Hpar1c | ||
sx6sYWfccmHoXdhMmAdyCS4dAGwN+J/jxqtb97jWqUSe8R4o/sIzu/fqbpTB8T/D | ||
x1fJqxXrAgMBAAECggEAI6btjHD3LcKU9DYNE8XFXnG07Y9ieJ17IrTuYwIop69a | ||
MDK92auHvPR8f4okzGV2rPG4FHpMS0o5tDOwFcf1B75riFLP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yGLVJbIWpNTSvzHj389mImRt | ||
i2URj3r/EIrdgSLKXzxMyJc+ToM705v4OgZYAuSLQSTaSBH6C1Kuw+l8UCuWK6/p | ||
i4X9qUqQUd9bT/Zh9nGs9G0= | ||
-----END PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
import { WsJsonClient } from "./wsJsonClient"; | ||
import { PositionsResponse } from "./services/positionsMessageHandler"; | ||
import { RawLoginResponseBody } from "./services/loginMessageHandler"; | ||
import { | ||
CancelAlertResponse, | ||
CreateAlertResponse, | ||
LookupAlertsResponse, | ||
} from "./types/alertTypes"; | ||
import { CancelOrderResponse } from "./services/cancelOrderMessageHandler"; | ||
import { | ||
ChartRequestParams, | ||
ChartResponse, | ||
} from "./services/chartMessageHandler"; | ||
import { CreateAlertRequestParams } from "./services/createAlertMessageHandler"; | ||
import { MarketDepthResponse } from "./services/marketDepthMessageHandler"; | ||
import { OptionChainResponse } from "./services/optionSeriesMessageHandler"; | ||
import { | ||
OptionChainDetailsRequest, | ||
OptionChainDetailsResponse, | ||
} from "./services/optionChainDetailsMessageHandler"; | ||
import { OptionSeriesQuotesResponse } from "./services/optionSeriesQuotesMessageHandler"; | ||
import { | ||
OptionQuotesRequestParams, | ||
OptionQuotesResponse, | ||
} from "./services/optionQuotesMessageHandler"; | ||
import { | ||
PlaceLimitOrderRequestParams, | ||
PlaceOrderSnapshotResponse, | ||
} from "./services/placeOrderMessageHandler"; | ||
import { QuotesResponse } from "./services/quotesMessageHandler"; | ||
import { OrderEventsResponse } from "./services/orderEventsMessageHandler"; | ||
import { InstrumentSearchResponse } from "./services/instrumentSearchMessageHandler"; | ||
import { UserPropertiesResponse } from "./services/userPropertiesMessageHandler"; | ||
import { GetWatchlistResponse } from "./services/getWatchlistMessageHandler"; | ||
import WebSocket from "isomorphic-ws"; | ||
import MulticastIterator from "obgen/multicastIterator"; | ||
import BufferedIterator from "obgen/bufferedIterator"; | ||
import { deferredWrap } from "obgen"; | ||
import { throwError } from "./util"; | ||
import debug from "debug"; | ||
import { ChannelState } from "./realWsJsonClient"; | ||
import { isString } from "lodash"; | ||
|
||
const logger = debug("wsClientProxy"); | ||
|
||
export const ALL_REQUESTS = [ | ||
"authenticate", | ||
"optionChainQuotes", | ||
"disconnect", | ||
] as const; | ||
type RequestType = typeof ALL_REQUESTS; | ||
type Request = RequestType[number]; | ||
|
||
export type ProxiedRequest = { | ||
request: Request; | ||
args?: any[]; | ||
}; | ||
|
||
export type ProxiedResponse = ProxiedRequest & { response: unknown }; | ||
|
||
// A WsJsonClient proxy implementation that proxies requests to a WebSocket server using the provided `proxyUrl`. | ||
export default class WsJsonClientProxy implements WsJsonClient { | ||
private state = ChannelState.DISCONNECTED; | ||
private buffer = new BufferedIterator<ProxiedResponse>(); | ||
private iterator = new MulticastIterator(this.buffer); | ||
private socket?: WebSocket; | ||
|
||
constructor( | ||
private readonly proxyUrl: string, | ||
private readonly options?: any | ||
) {} | ||
|
||
accountPositions(_: string): AsyncIterable<PositionsResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
async authenticate( | ||
accessToken: string | ||
): Promise<RawLoginResponseBody | null> { | ||
this.socket = new WebSocket(this.proxyUrl, this.options); | ||
this.state = ChannelState.CONNECTING; | ||
this.buffer = new BufferedIterator<ProxiedResponse>(); | ||
this.iterator = new MulticastIterator(this.buffer); | ||
const { buffer, socket } = this; | ||
return new Promise((resolve, reject) => { | ||
socket.onmessage = ({ data }) => { | ||
buffer.emit(JSON.parse(data as string) as ProxiedResponse); | ||
}; | ||
socket.onopen = () => { | ||
logger("proxy ws connection opened"); | ||
this.state = ChannelState.CONNECTED; | ||
this.doAuthenticate(accessToken).then((res) => { | ||
logger("proxy ws authentication response: %O", res); | ||
if (isString(res) && res.includes("NOT_AUTHORIZED")) { | ||
reject(res); | ||
} else { | ||
resolve(res); | ||
} | ||
}); | ||
}; | ||
socket.onclose = (event) => { | ||
this.state = ChannelState.DISCONNECTED; | ||
logger("proxy ws connection closed: ", event?.reason); | ||
reject(event?.reason); | ||
}; | ||
socket.onerror = (err) => { | ||
this.state = ChannelState.ERROR; | ||
logger("proxy ws socket error: %O", err); | ||
reject(err); | ||
}; | ||
}); | ||
} | ||
|
||
private doAuthenticate( | ||
accessToken: string | ||
): Promise<RawLoginResponseBody | null> { | ||
this.sendMessage({ request: "authenticate", args: [accessToken] }); | ||
return deferredWrap(() => this.iterator) | ||
.filter(({ request }) => request === "authenticate") | ||
.map(({ response }) => response) | ||
.promise() as Promise<RawLoginResponseBody | null>; | ||
} | ||
|
||
cancelAlert(_: number): Promise<CancelAlertResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
cancelOrder(_: number): Promise<CancelOrderResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
chart(_: ChartRequestParams): AsyncIterable<ChartResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
createAlert(_: CreateAlertRequestParams): Promise<CreateAlertResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
disconnect(): void { | ||
this.sendMessage({ request: "disconnect" }); | ||
} | ||
|
||
ensureConnected(): void { | ||
if (this.state !== ChannelState.CONNECTED) { | ||
throw new Error("Not connected"); | ||
} | ||
} | ||
|
||
isConnected(): boolean { | ||
return this.state === ChannelState.CONNECTED; | ||
} | ||
|
||
isConnecting(): boolean { | ||
return this.state === ChannelState.CONNECTING; | ||
} | ||
|
||
lookupAlerts(): AsyncIterable<LookupAlertsResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
marketDepth(_: string): AsyncIterable<MarketDepthResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
optionChain(_: string): Promise<OptionChainResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
optionChainDetails( | ||
_: OptionChainDetailsRequest | ||
): Promise<OptionChainDetailsResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
optionChainQuotes(symbol: string): AsyncIterable<OptionSeriesQuotesResponse> { | ||
this.sendMessage({ request: "optionChainQuotes", args: [symbol] }); | ||
return deferredWrap(() => this.iterator) | ||
.filter(({ request }) => request === "optionChainQuotes") | ||
.map(({ response }) => response) | ||
.iterable() as AsyncIterable<OptionSeriesQuotesResponse>; | ||
} | ||
|
||
optionQuotes( | ||
_: OptionQuotesRequestParams | ||
): AsyncIterable<OptionQuotesResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
placeOrder( | ||
_: PlaceLimitOrderRequestParams | ||
): Promise<PlaceOrderSnapshotResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
quotes(_: string[]): AsyncIterable<QuotesResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
replaceOrder( | ||
_: Required<PlaceLimitOrderRequestParams> | ||
): Promise<OrderEventsResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
searchInstruments(_: string): Promise<InstrumentSearchResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
userProperties(): Promise<UserPropertiesResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
watchlist(_: number): Promise<GetWatchlistResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
workingOrders(_: string): AsyncIterable<OrderEventsResponse> { | ||
throwError("not implemented"); | ||
} | ||
|
||
private sendMessage(request: ProxiedRequest) { | ||
this.ensureConnected(); | ||
this.socket!.send(JSON.stringify(request)); | ||
} | ||
} |
Oops, something went wrong.