Skip to content

Commit 9c6914d

Browse files
author
vitaliystoliarovcc
authored
Fix reconnect (#571)
* fix authArgs for reconnect * update tests * jsdoc * test reconnect with updateAuthArgs
1 parent 63deabc commit 9c6914d

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

lib/transports/ws2.js

+12-22
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ class WSv2 extends EventEmitter {
9696
super()
9797

9898
this.setMaxListeners(1000)
99-
this._apiKey = opts.apiKey || ''
100-
this._apiSecret = opts.apiSecret || ''
10199
this._affCode = opts.affCode
102100
this._agent = opts.agent
103101
this._url = opts.url || WSv2.url
@@ -117,7 +115,10 @@ class WSv2 extends EventEmitter {
117115
this._orderBooks = {}
118116
this._losslessOrderBooks = {}
119117
this._candles = {}
120-
this._authArgs = {}
118+
this._authArgs = {
119+
apiKey: opts.apiKey,
120+
apiSecret: opts.apiSecret
121+
}
121122

122123
/**
123124
* {
@@ -140,7 +141,7 @@ class WSv2 extends EventEmitter {
140141
this._enabledFlags = this._seqAudit ? WSv2.flags.SEQ_ALL : 0
141142
this._eventCallbacks = new CbQ()
142143
this._isAuthenticated = false
143-
this._wasEverAuthenticated = false // used for auto-auth on reconnect
144+
this._authOnReconnect = false // used for auto-auth on reconnect
144145
this._lastPubSeq = -1
145146
this._lastAuthSeq = -1
146147
this._isOpen = false
@@ -176,6 +177,8 @@ class WSv2 extends EventEmitter {
176177
* @param {object} args - arguments
177178
* @param {number} [args.calc] - calc value
178179
* @param {number} [args.dms] - dms value, active 4
180+
* @param {number} [args.apiKey] API key
181+
* @param {number} [args.apiSecret] API secret
179182
* @see WSv2#auth
180183
*/
181184
updateAuthArgs (args = {}) {
@@ -196,19 +199,6 @@ class WSv2 extends EventEmitter {
196199
return this._authArgs
197200
}
198201

199-
/**
200-
* Update the internal API credentials, used on subsequent {@link WSv2#auth}
201-
* calls
202-
*
203-
* @param {string} apiKey - API key
204-
* @param {string} apiSecret - API secret
205-
* @see WSv2#auth
206-
*/
207-
setAPICredentials (apiKey, apiSecret) {
208-
this._apiKey = apiKey
209-
this._apiSecret = apiSecret
210-
}
211-
212202
/**
213203
* Get the total number of data channels this instance is currently
214204
* subscribed too.
@@ -362,6 +352,7 @@ class WSv2 extends EventEmitter {
362352
* @returns {Promise} p
363353
*/
364354
async auth (calc, dms) {
355+
this._authOnReconnect = true
365356
if (!this._isOpen) {
366357
throw new Error('not open')
367358
}
@@ -372,7 +363,7 @@ class WSv2 extends EventEmitter {
372363

373364
const authNonce = nonce()
374365
const authPayload = `AUTH${authNonce}${authNonce}`
375-
const { sig } = genAuthSig(this._apiSecret, authPayload)
366+
const { sig } = genAuthSig(this._authArgs.apiSecret, authPayload)
376367
const authArgs = { ...this._authArgs }
377368

378369
if (_isFinite(calc)) authArgs.calc = calc
@@ -386,7 +377,7 @@ class WSv2 extends EventEmitter {
386377

387378
this.send({
388379
event: 'auth',
389-
apiKey: this._apiKey,
380+
apiKey: this._authArgs.apiKey,
390381
authSig: sig,
391382
authPayload,
392383
authNonce,
@@ -408,7 +399,7 @@ class WSv2 extends EventEmitter {
408399
await this.close()
409400

410401
return new Promise((resolve) => {
411-
this.once(this._wasEverAuthenticated ? 'auth' : 'open', resolve)
402+
this.once(this._authOnReconnect ? 'auth' : 'open', resolve)
412403
})
413404
}
414405

@@ -425,7 +416,7 @@ class WSv2 extends EventEmitter {
425416

426417
await this.open()
427418

428-
if (this._wasEverAuthenticated) {
419+
if (this._authOnReconnect) {
429420
await this.auth()
430421
}
431422
}
@@ -1358,7 +1349,6 @@ class WSv2 extends EventEmitter {
13581349

13591350
this._channelMap[chanId] = { channel: 'auth' }
13601351
this._isAuthenticated = true
1361-
this._wasEverAuthenticated = true
13621352

13631353
this.emit('auth', data)
13641354
debug('authenticated!')

lib/ws2_manager.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ class WS2Manager extends EventEmitter {
201201

202202
if (_isFinite(calc)) this._authArgs.calc = calc
203203
if (_isFinite(dms)) this._authArgs.dms = dms
204+
if (apiKey) this._authArgs.apiKey = apiKey
205+
if (apiSecret) this._authArgs.apiSecret = apiSecret
204206

205207
this._sockets.forEach(s => {
206208
if (!s.ws.isAuthenticated()) {
207-
s.ws.setAPICredentials(apiKey, apiSecret)
208209
s.ws.updateAuthArgs(this._authArgs)
209210
s.ws.auth()
210211
}

test/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ describe('BFX', () => {
105105
const ws2 = bfx.ws(2)
106106

107107
assert.strictEqual(ws1._apiKey, 'k')
108-
assert.strictEqual(ws2._apiKey, 'k')
108+
assert.strictEqual(ws2._authArgs.apiKey, 'k')
109109
assert.strictEqual(ws1._apiSecret, 's')
110-
assert.strictEqual(ws2._apiSecret, 's')
110+
assert.strictEqual(ws2._authArgs.apiSecret, 's')
111111
assert.strictEqual(ws1._url, 'wss://')
112112
assert.strictEqual(ws2._url, 'wss://')
113113
assert.strictEqual(ws2._transform, true)

test/lib/transports/ws2-unit.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,25 @@ describe('WSv2 unit', () => {
565565
})
566566
})
567567

568+
it('reconnect with new credentials', async () => {
569+
wss = new MockWSv2Server({ authMiddleware: ({ apiKey, apiSecret }) => apiKey === API_KEY && apiSecret === API_SECRET })
570+
ws = createTestWSv2Instance({ reconnectDelay: 10 })
571+
572+
await ws.open()
573+
await ws.auth()
574+
assert(ws.isAuthenticated())
575+
576+
ws.updateAuthArgs({ apiKey: 'wrong', apiSecret: 'wrong' })
577+
ws.reconnect()
578+
await Promise.delay(50)
579+
assert(!ws.isAuthenticated())
580+
581+
ws.updateAuthArgs({ apiKey: API_KEY, apiSecret: API_SECRET })
582+
ws.reconnect()
583+
await Promise.delay(50)
584+
assert(ws.isAuthenticated())
585+
})
586+
568587
describe('seq audit', () => {
569588
it('automatically enables sequencing if seqAudit is true in constructor', () => {
570589
ws = createTestWSv2Instance({ seqAudit: true })
@@ -2024,11 +2043,15 @@ describe('WSv2 unit', () => {
20242043
assert.strictEqual(ws.getAuthArgs().dms, 4)
20252044
})
20262045

2027-
it('initializes to empty args set', () => {
2046+
it('initializes auth args', () => {
20282047
ws = createTestWSv2Instance()
20292048
const initAuthArgs = ws.getAuthArgs()
20302049

2031-
assert(_isObject(initAuthArgs) && _isEmpty(initAuthArgs))
2050+
assert(_isObject(initAuthArgs))
2051+
assert.deepStrictEqual(initAuthArgs, {
2052+
apiKey: API_KEY,
2053+
apiSecret: API_SECRET
2054+
})
20322055
})
20332056

20342057
it('updates auth args with setAuthArgs', async () => {

test/lib/ws2_manager.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ describe('WS2Manager', () => {
189189
m._sockets = [{
190190
ws: {
191191
isAuthenticated: () => false,
192-
setAPICredentials: (key, secret) => { cred = `${key}:${secret}` },
193-
updateAuthArgs: () => {},
192+
updateAuthArgs: ({ apiKey: key, apiSecret: secret }) => { cred = `${key}:${secret}` },
194193
auth: () => {
195194
assert.strictEqual(cred, '41:42')
196195
done()
@@ -285,8 +284,8 @@ describe('WS2Manager', () => {
285284
const { ws } = s
286285

287286
ws.auth = async () => {
288-
assert.strictEqual(ws._apiKey, 'key', 'api key not set')
289-
assert.strictEqual(ws._apiSecret, 'secret', 'api secret not set')
287+
assert.strictEqual(ws._authArgs.apiKey, 'key', 'api key not set')
288+
assert.strictEqual(ws._authArgs.apiSecret, 'secret', 'api secret not set')
290289

291290
await ws.close()
292291
done()

0 commit comments

Comments
 (0)