Skip to content

Commit 5fc6a41

Browse files
f3rnoJacobPlaster
authored andcommitted
(feature) auth arg management
1 parent d961254 commit 5fc6a41

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
4.0.5
2+
- WSv2: add auth arg management (pre-set dms and calc)
3+
- WSv2: add updateAthArgs()
4+
- WSv2: add getAuthArgs()
5+
- WS2Manager: fix auth args handling with new methods
6+
17
4.0.4
28
- Orderbook: generate and update book using lossless string format
39
in order to prevent floating point precision checksum errors:

lib/transports/ws2.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const _isString = require('lodash/isString')
1212
const _includes = require('lodash/includes')
1313
const _pick = require('lodash/pick')
1414
const _isEqual = require('lodash/isEqual')
15+
const _isFinite = require('lodash/isFinite')
1516
const { genAuthSig, nonce } = require('bfx-api-node-util')
1617
const getMessagePayload = require('../util/ws2')
1718
const LosslessJSON = require('lossless-json')
@@ -107,6 +108,7 @@ class WSv2 extends EventEmitter {
107108
this._orderBooks = {}
108109
this._losslessOrderBooks = {}
109110
this._candles = {}
111+
this._authArgs = {}
110112

111113
/**
112114
* {
@@ -144,6 +146,17 @@ class WSv2 extends EventEmitter {
144146
this._sendCalc = _Throttle(this._sendCalc.bind(this), 1000 / MAX_CALC_OPS)
145147
}
146148

149+
updateAuthArgs (args = {}) {
150+
this._authArgs = {
151+
...this._authArgs,
152+
...args
153+
}
154+
}
155+
156+
getAuthArgs () {
157+
return this._authArgs
158+
}
159+
147160
setAPICredentials (apiKey, apiSecret) {
148161
this._apiKey = apiKey
149162
this._apiSecret = apiSecret
@@ -259,7 +272,7 @@ class WSv2 extends EventEmitter {
259272
* @param {number?} dms - optional dead man switch flag, active 4
260273
* @return {Promise} p
261274
*/
262-
auth (calc = 0, dms = 0) {
275+
auth (calc, dms) {
263276
if (!this._isOpen) return Promise.reject(new Error('not open'))
264277
if (this._isAuthenticated) {
265278
return Promise.reject(new Error('already authenticated'))
@@ -268,6 +281,10 @@ class WSv2 extends EventEmitter {
268281
const authNonce = nonce()
269282
const authPayload = `AUTH${authNonce}${authNonce}`
270283
const { sig } = genAuthSig(this._apiSecret, authPayload)
284+
const authArgs = { ...this._authArgs }
285+
286+
if (_isFinite(calc)) authArgs.calc = calc
287+
if (_isFinite(dms)) authArgs.dms = dms
271288

272289
return new Promise((resolve, reject) => {
273290
this.once('auth', () => {
@@ -281,8 +298,7 @@ class WSv2 extends EventEmitter {
281298
authSig: sig,
282299
authPayload,
283300
authNonce,
284-
dms,
285-
calc
301+
...authArgs
286302
})
287303
})
288304
}

lib/ws2_manager.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class WS2Manager extends EventEmitter {
5151
...this._authArgs,
5252
...args
5353
}
54+
55+
this._sockets.forEach(socket => socket.ws.updateAuthArgs(this._authArgs))
5456
}
5557

5658
/**
@@ -127,7 +129,8 @@ class WS2Manager extends EventEmitter {
127129
this._sockets.forEach(s => {
128130
if (!s.ws.isAuthenticated()) {
129131
s.ws.setAuthCredentials(apiKey, apiSecret)
130-
s.ws.auth({ calc, dms })
132+
s.ws.updateAuthArgs(this._authArgs)
133+
s.ws.auth()
131134
}
132135
})
133136
}
@@ -141,14 +144,14 @@ class WS2Manager extends EventEmitter {
141144
*/
142145
openSocket () {
143146
const { apiKey, apiSecret } = this._socketArgs
144-
const { calc, dms } = this._authArgs
145147
const ws = new WSv2(this._socketArgs)
146148
const wsState = {
147149
pendingSubscriptions: [],
148150
pendingUnsubscriptions: [],
149151
ws
150152
}
151153

154+
ws.updateAuthArgs(this._authArgs)
152155
ws.on('open', () => this.emit('open', ws))
153156
ws.on('message', (msg = {}) => this.emit('message', msg, ws))
154157
ws.on('error', (error) => this.emit('error', error, ws))
@@ -194,7 +197,7 @@ class WS2Manager extends EventEmitter {
194197
ws.once('open', () => {
195198
debug('authenticating socket...')
196199

197-
ws.auth(calc, dms).then(() => {
200+
ws.auth().then(() => {
198201
debug('socket authenticated')
199202
}).catch((err) => {
200203
debug('error authenticating socket: %s', err.message)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitfinex-api-node",
3-
"version": "4.0.4",
3+
"version": "4.0.5",
44
"description": "Node reference library for Bitfinex API",
55
"engines": {
66
"node": ">=7"

test/lib/transports/ws2-unit.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const assert = require('assert')
55
const WSv2 = require('../../../lib/transports/ws2')
66
const { MockWSv2Server } = require('bfx-api-mock-srv')
77
const _isObject = require('lodash/isObject')
8+
const _isEmpty = require('lodash/isEmpty')
89

910
const API_KEY = 'dummy'
1011
const API_SECRET = 'dummy'
@@ -805,7 +806,7 @@ describe('WSv2 channel msg handling', () => {
805806
if (++errorsSeen === 2) done()
806807
})
807808

808-
let obMsg = [42, [100, 0, 1]]
809+
const obMsg = [42, [100, 0, 1]]
809810
wsTransform._handleOBMessage(obMsg, wsTransform._channelMap[42], JSON.stringify(obMsg))
810811
wsNoTransform._handleOBMessage(obMsg, wsNoTransform._channelMap[42], JSON.stringify(obMsg))
811812
})
@@ -830,7 +831,7 @@ describe('WSv2 channel msg handling', () => {
830831
if (++seen === 2) done()
831832
})
832833

833-
let obMsg = [42, [[100, 2, 3]]]
834+
const obMsg = [42, [[100, 2, 3]]]
834835
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
835836
})
836837

@@ -869,7 +870,7 @@ describe('WSv2 channel msg handling', () => {
869870
if (++seen === 2) done()
870871
})
871872

872-
let obMsg = [42, [[100, 2, 3]]]
873+
const obMsg = [42, [[100, 2, 3]]]
873874
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
874875
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
875876
})
@@ -889,7 +890,7 @@ describe('WSv2 channel msg handling', () => {
889890
done()
890891
})
891892

892-
let obMsg = [42, [[100, 2, 3]]]
893+
const obMsg = [42, [[100, 2, 3]]]
893894
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
894895
})
895896

@@ -911,7 +912,7 @@ describe('WSv2 channel msg handling', () => {
911912
done()
912913
})
913914

914-
let obMsg = [42, [[100, 2, 3]]]
915+
const obMsg = [42, [[100, 2, 3]]]
915916
ws._handleOBMessage(obMsg, ws._channelMap[42], obMsg)
916917
})
917918

@@ -1716,3 +1717,40 @@ describe('resubscribePreviousChannels', () => {
17161717
assert(subBook)
17171718
})
17181719
})
1720+
1721+
describe('WSv2 auth args', () => {
1722+
it('provides getAuthArgs to read args', () => {
1723+
const ws = new WSv2()
1724+
ws.updateAuthArgs({ dms: 4 })
1725+
assert.strictEqual(ws.getAuthArgs().dms, 4)
1726+
})
1727+
1728+
it('initializes to empty args set', () => {
1729+
const ws = new WSv2()
1730+
const initAuthArgs = ws.getAuthArgs()
1731+
1732+
assert(_isObject(initAuthArgs) && _isEmpty(initAuthArgs))
1733+
})
1734+
1735+
it('updates auth args with setAuthArgs', async () => {
1736+
const ws = new WSv2()
1737+
const wss = new MockWSv2Server()
1738+
let sendCalled = false
1739+
1740+
await ws.open()
1741+
1742+
ws.updateAuthArgs({ dms: 4 })
1743+
assert.strictEqual(ws.getAuthArgs().dms, 4)
1744+
1745+
ws.send = (payload) => {
1746+
assert.strictEqual(payload.dms, 4)
1747+
sendCalled = true
1748+
ws.emit('auth')
1749+
}
1750+
1751+
await ws.auth()
1752+
assert(sendCalled)
1753+
1754+
wss.close()
1755+
})
1756+
})

0 commit comments

Comments
 (0)