Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
fix: resolve transport sort order in browsers (#333)
Browse files Browse the repository at this point in the history
* fix: resolve transport sort order in browsers

* fix: update sort logic
  • Loading branch information
jacobheun authored Apr 16, 2019
1 parent bf4d926 commit 951e0c9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ class Switch extends EventEmitter {

// Only listen on transports we actually have addresses for
return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0)
// push Circuit to be the last proto to be dialed
.sort((a) => {
return a === Circuit.tag ? 1 : 0
// push Circuit to be the last proto to be dialed, and alphabetize the others
.sort((a, b) => {
if (a === Circuit.tag) return 1
if (b === Circuit.tag) return -1
return a < b ? -1 : 1
})
}

Expand Down
37 changes: 37 additions & 0 deletions test/switch.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const Switch = require('../src')

describe('Switch', () => {
describe('.availableTransports', () => {
it('should always sort circuit last', () => {
const switchA = new Switch({}, {})
const transport = {
filter: (addrs) => addrs
}
const mockPeerInfo = {
multiaddrs: {
toArray: () => ['a', 'b', 'c']
}
}

switchA.transports = {
Circuit: transport,
TCP: transport,
WebSocketStar: transport
}

expect(switchA.availableTransports(mockPeerInfo)).to.eql([
'TCP',
'WebSocketStar',
'Circuit'
])
})
})
})

0 comments on commit 951e0c9

Please sign in to comment.