From 1e9474347c6d6ac24ff63e7fd7c5d28610152ab8 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Mon, 15 Apr 2019 19:54:18 +0200 Subject: [PATCH 1/2] fix: resolve transport sort order in browsers --- src/index.js | 2 +- test/switch.spec.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/switch.spec.js diff --git a/src/index.js b/src/index.js index b32f8fd..3efa77d 100644 --- a/src/index.js +++ b/src/index.js @@ -129,7 +129,7 @@ class Switch extends EventEmitter { 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 + return a === Circuit.tag ? 1 : -1 }) } diff --git a/test/switch.spec.js b/test/switch.spec.js new file mode 100644 index 0000000..52d9273 --- /dev/null +++ b/test/switch.spec.js @@ -0,0 +1,33 @@ +/* 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)[2]).to.eql('Circuit') + }) + }) +}) From 82eddade3d0959c12c61e99589aeacb9e200c661 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 16 Apr 2019 10:52:41 +0200 Subject: [PATCH 2/2] fix: update sort logic --- src/index.js | 8 +++++--- test/switch.spec.js | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 3efa77d..f463d4d 100644 --- a/src/index.js +++ b/src/index.js @@ -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 : -1 + // 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 }) } diff --git a/test/switch.spec.js b/test/switch.spec.js index 52d9273..d38edca 100644 --- a/test/switch.spec.js +++ b/test/switch.spec.js @@ -27,7 +27,11 @@ describe('Switch', () => { WebSocketStar: transport } - expect(switchA.availableTransports(mockPeerInfo)[2]).to.eql('Circuit') + expect(switchA.availableTransports(mockPeerInfo)).to.eql([ + 'TCP', + 'WebSocketStar', + 'Circuit' + ]) }) }) })