Skip to content

Commit

Permalink
Specify min tls in market data and account streamers
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoss18 committed Nov 8, 2023
1 parent f9e4095 commit f14d2e5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 23 deletions.
14 changes: 9 additions & 5 deletions lib/account-streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from 'lodash'
import type { JsonMap, JsonValue } from './utils/json-util'
import { JsonBuilder } from './utils/json-util'
import TastytradeSession from './models/tastytrade-session'
import { MinTlsVersion } from './utils/constants'

export enum STREAMER_STATE {
Open = 0,
Expand Down Expand Up @@ -123,7 +124,10 @@ export class AccountStreamer {
return this.startPromise
}

const websocket = (this.websocket = new WebSocket(this.url))
this.websocket = new WebSocket(this.url, [], {
minVersion: MinTlsVersion // TLS Config
})
const websocket = this.websocket
this.lastCloseEvent = null
this.lastErrorEvent = null
websocket.addEventListener('open', this.handleOpen)
Expand Down Expand Up @@ -300,7 +304,7 @@ export class AccountStreamer {
this.queued = []
}

private readonly handleOpen = (event: Event) => {
private readonly handleOpen = (event: WebSocket.Event) => {
if (this.startResolve === null) {
return
}
Expand All @@ -315,7 +319,7 @@ export class AccountStreamer {
this.scheduleHeartbeatTimer()
}

private readonly handleClose = (event: CloseEvent) => {
private readonly handleClose = (event: WebSocket.CloseEvent) => {
this.logger.info('AccountStreamer closed', event)
if (this.websocket === null) {
return
Expand All @@ -326,7 +330,7 @@ export class AccountStreamer {
this.teardown()
}

private readonly handleError = (event: Event) => {
private readonly handleError = (event: WebSocket.ErrorEvent) => {
if (this.websocket === null) {
return
}
Expand All @@ -344,7 +348,7 @@ export class AccountStreamer {
this.teardown()
}

private readonly handleMessage = (event: MessageEvent) => {
private readonly handleMessage = (event: WebSocket.MessageEvent) => {
const json = JSON.parse(event.data as string) as JsonMap

if (json.results !== undefined) {
Expand Down
11 changes: 7 additions & 4 deletions lib/market-data-streamer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import WebSocket from 'isomorphic-ws'
import _ from 'lodash'
import { v4 as uuidv4 } from 'uuid'
import { MinTlsVersion } from './utils/constants'

export enum MarketDataSubscriptionType {
Candle = 'Candle',
Expand Down Expand Up @@ -89,7 +90,9 @@ export default class MarketDataStreamer {
}

this.token = token
this.webSocket = new WebSocket(url)
this.webSocket = new WebSocket(url, [], {
minVersion: MinTlsVersion // TLS Config
})
this.webSocket.onopen = this.onOpen.bind(this)
this.webSocket.onerror = this.onError.bind(this)
this.webSocket.onmessage = this.handleMessageReceived.bind(this)
Expand Down Expand Up @@ -343,9 +346,9 @@ export default class MarketDataStreamer {
this.errorListeners.forEach(listener => listener(error))
}

private handleMessageReceived(data: string) {
const messageData = _.get(data, 'data', data)
const jsonData = JSON.parse(messageData)
private handleMessageReceived(data: WebSocket.MessageEvent) {
const messageData = _.get(data, 'data', '{}')
const jsonData = JSON.parse(messageData as string)
switch (jsonData.type) {
case 'AUTH_STATE':
this.handleAuthStateMessage(jsonData)
Expand Down
7 changes: 6 additions & 1 deletion lib/services/tastytrade-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import axios from "axios"
import qs from 'qs'
import { recursiveDasherizeKeys } from "../utils/json-util"
import _ from 'lodash'
import https from 'https'
import { MinTlsVersion } from "../utils/constants"

const ParamsSerializer = {
serialize: function (queryParams: object) {
Expand All @@ -12,9 +14,11 @@ const ParamsSerializer = {

export default class TastytradeHttpClient{
public readonly session: TastytradeSession
private readonly httpsAgent: https.Agent

constructor(private readonly baseUrl: string) {
this.session = new TastytradeSession()
this.httpsAgent = new https.Agent({ minVersion: MinTlsVersion })
}

private getDefaultHeaders(): any {
Expand All @@ -37,7 +41,8 @@ export default class TastytradeHttpClient{
data: dasherizedData,
headers: mergedHeaders,
params: dasherizedParams,
paramsSerializer: ParamsSerializer
paramsSerializer: ParamsSerializer,
httpsAgent: this.httpsAgent
}, _.isEmpty)

return axios.request(config)
Expand Down
1 change: 1 addition & 0 deletions lib/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MinTlsVersion = 'TLSv1.2'
75 changes: 66 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"repository": "https://github.com/tastytrade/tastytrade-api-js",
"license": "MIT",
"description": "Typescript impelementation of tastytrade api",
"engines" : {
"npm" : ">=9.0.0",
"node" : ">=20.0.0"
"engines": {
"npm": ">=9.0.0",
"node": ">=20.0.0"
},
"scripts": {
"build": "tsc -p tsconfig.json",
Expand All @@ -30,9 +30,11 @@
"ws": "^8.13.0"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/jest": "^29.5.0",
"@types/node": "17.0.27",
"@types/node": "20.9.0",
"@types/uuid": "^9.0.2",
"@types/ws": "^8.5.9",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"dotenv": "^16.0.3",
Expand Down

0 comments on commit f14d2e5

Please sign in to comment.