Skip to content
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

Engines enforcement #6

Merged
merged 5 commits into from
Nov 8, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/check-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
node-version: [20.x, 21.x]

steps:
- uses: actions/checkout@v3
Expand All @@ -17,7 +17,7 @@ jobs:
cache: 'npm'
- run: npm install
- run: npm run lint
- run: npm run test
- run: npm run unit-test
env:
BASE_URL: ${{ secrets.BASE_URL }}
API_USERNAME: ${{ secrets.API_USERNAME }}
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
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 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 @@ -43,14 +44,14 @@
export class AccountStreamer {
private websocket: WebSocket | null = null
private startResolve: ((result: boolean) => void) | null = null
private startReject: ((reason?: any) => void) | null = null

Check warning on line 47 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 47 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
private requestCounter: number = 0

Check warning on line 48 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Type number trivially inferred from a number literal, remove type annotation

Check warning on line 48 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Type number trivially inferred from a number literal, remove type annotation
private queued: string[] = []

private heartbeatTimerId: number | NodeJS.Timeout | null = null

lastCloseEvent: any = null

Check warning on line 53 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 53 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
lastErrorEvent: any = null

Check warning on line 54 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 54 in lib/account-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
private _streamerState: STREAMER_STATE = STREAMER_STATE.Closed

private readonly streamerStateObservers: StreamerStateObserver[] = []
Expand Down Expand Up @@ -123,7 +124,10 @@
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 @@
this.queued = []
}

private readonly handleOpen = (event: Event) => {
private readonly handleOpen = (event: WebSocket.Event) => {
if (this.startResolve === null) {
return
}
Expand All @@ -315,7 +319,7 @@
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 @@
this.teardown()
}

private readonly handleError = (event: Event) => {
private readonly handleError = (event: WebSocket.ErrorEvent) => {
if (this.websocket === null) {
return
}
Expand All @@ -344,7 +348,7 @@
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 All @@ -26,12 +27,12 @@
Price = 'p'
}

export type MarketDataListener = (data: any) => void

Check warning on line 30 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 30 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
export type ErrorListener = (error: any) => void

Check warning on line 31 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 31 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
export type AuthStateListener = (isAuthorized: boolean) => void

type QueuedSubscription = { symbol: string, subscriptionTypes: MarketDataSubscriptionType[], subscriptionArgs?: any }

Check warning on line 34 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 34 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
type SubscriptionOptions = { subscriptionTypes: MarketDataSubscriptionType[], channelId: number, subscriptionArgs?: any }

Check warning on line 35 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 35 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
export type CandleSubscriptionOptions = { period: number, type: CandleType, channelId: number }
type Remover = () => void

Expand All @@ -45,7 +46,7 @@
export default class MarketDataStreamer {
private webSocket: WebSocket | null = null
private token = ''
private keepaliveIntervalId: any | null = null

Check warning on line 49 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 49 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
private dataListeners = new Map()
private openChannels = new Set()
private subscriptionsQueue: Map<number, QueuedSubscription[]> = new Map()
Expand Down Expand Up @@ -89,7 +90,9 @@
}

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 @@ -243,7 +246,7 @@
return
}

_.remove(queue, (queueItem: any) => queueItem.symbol === symbol)

Check warning on line 249 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 249 in lib/market-data-streamer.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Unexpected any. Specify a different type
}

private sendQueuedSubscriptions(channelId: number) {
Expand Down Expand Up @@ -343,9 +346,9 @@
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'
83 changes: 72 additions & 11 deletions package-lock.json

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

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"name": "@tastytrade/api",
"version": "2.1.1",
"version": "3.0.0",
"main": "dist/tastytrade-api.js",
"typings": "dist/tastytrade-api.d.ts",
"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"
},
"scripts": {
"build": "tsc -p tsconfig.json",
"test": "jest -i --restoreMocks",
Expand All @@ -26,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