Skip to content

Commit 1b1c1f3

Browse files
authored
Merge pull request #379 from f3rno/fix-skip-auth-seq-error-n
(fix) skip auth seq # for error notifications w/ test
2 parents ba888a1 + 96c114a commit 1b1c1f3

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

lib/transports/ws2.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,11 @@ class WSv2 extends EventEmitter {
313313

314314
// All other packets provide a public sequence # as the last value. For chan
315315
// 0 packets, these are included as the 2nd to last value
316-
const seq = msg[0] === 0 && msg[1] !== 'hb'
316+
const seq = (
317+
(msg[0] === 0) &&
318+
(msg[1] !== 'hb') &&
319+
!(msg[1] === 'n' && msg[2][6] === 'ERROR') // error notifications lack seq
320+
)
317321
? msg[msg.length - 2]
318322
: msg[msg.length - 1]
319323

@@ -332,6 +336,7 @@ class WSv2 extends EventEmitter {
332336

333337
if (!isFinite(authSeq)) return null
334338
if (authSeq === 0) return null // still syncing
339+
if (msg[1] === 'n' && msg[2][6] === 'ERROR') return null // err notifications lack seq
335340
if (authSeq === this._lastAuthSeq) return null // seq didn't advance
336341

337342
// check

test/lib/transports/ws2-unit.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,3 +1479,63 @@ describe('WSv2 message sending', () => {
14791479
ws.send({ a: 42 })
14801480
})
14811481
})
1482+
1483+
describe('WSv2 seq audit: _validateMessageSeq', () => {
1484+
it('returns an error on invalid pub seq', () => {
1485+
const ws = new WSv2()
1486+
1487+
ws._seqAudit = true
1488+
ws._lastPubSeq = 0
1489+
1490+
assert.equal(ws._validateMessageSeq([243, [252.12, 2, -1], 1]), null)
1491+
assert.equal(ws._validateMessageSeq([243, [252.12, 2, -1], 2]), null)
1492+
1493+
const err = ws._validateMessageSeq([243, [252.12, 2, -1], 5])
1494+
assert(err instanceof Error)
1495+
})
1496+
1497+
it('returns an error on invalid auth seq', () => {
1498+
const ws = new WSv2()
1499+
1500+
ws._seqAudit = true
1501+
ws._lastPubSeq = 0
1502+
ws._lastAuthSeq = 0
1503+
1504+
assert.equal(ws._validateMessageSeq([0, [252.12, 2, -1], 1, 1]), null)
1505+
assert.equal(ws._validateMessageSeq([0, [252.12, 2, -1], 2, 2]), null)
1506+
1507+
const err = ws._validateMessageSeq([0, [252.12, 2, -1], 3, 5])
1508+
assert(err instanceof Error)
1509+
})
1510+
1511+
it('ignores heartbeats', () => {
1512+
const ws = new WSv2()
1513+
1514+
ws._seqAudit = true
1515+
ws._lastPubSeq = 0
1516+
1517+
assert.equal(ws._validateMessageSeq([243, [252.12, 2, -1], 1]), null)
1518+
assert.equal(ws._validateMessageSeq([243, [252.12, 2, -1], 2]), null)
1519+
assert.equal(ws._validateMessageSeq([243, 'hb']), null)
1520+
assert.equal(ws._validateMessageSeq([243, 'hb']), null)
1521+
assert.equal(ws._validateMessageSeq([243, [252.12, 2, -1], 3]), null)
1522+
assert.equal(ws._validateMessageSeq([243, [252.12, 2, -1], 4]), null)
1523+
})
1524+
1525+
it('skips auth seq for error notifications', () => {
1526+
const ws = new WSv2()
1527+
1528+
ws._seqAudit = true
1529+
ws._lastPubSeq = 0
1530+
ws._lastAuthSeq = 0
1531+
1532+
const nSuccess = [null, null, null, null, null, null, 'SUCCESS']
1533+
const nError = [null, null, null, null, null, null, 'ERROR']
1534+
1535+
assert.equal(ws._validateMessageSeq([0, 'n', nSuccess, 1, 1]), null)
1536+
assert.equal(ws._validateMessageSeq([0, 'n', nSuccess, 2, 2]), null)
1537+
assert.equal(ws._validateMessageSeq([0, 'n', nError, 3]), null)
1538+
assert.equal(ws._validateMessageSeq([0, 'n', nSuccess, 4, 3]), null)
1539+
assert.equal(ws._validateMessageSeq([0, 'n', nSuccess, 5, 4]), null)
1540+
})
1541+
})

0 commit comments

Comments
 (0)