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

fix: resolve transport sort order in browsers #333

Merged
merged 2 commits into from
Apr 16, 2019
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
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'
])
})
})
})