-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When developing new features, it will be useful to be able to compare server and client protocol versions. For example, if a server is ahead of an old client, the server could gracefully degrade performance for clients that don't support the new functionality. This change stores the client's protocol on the `Agent` so we can perform these checks. We also add a `checkAtLeast()` protocol helper function, and store the current protocol in a constant to avoid magic numbers.
- Loading branch information
1 parent
945dc50
commit ad9aa7e
Showing
5 changed files
with
92 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module.exports = { | ||
major: 1, | ||
minor: 1, | ||
checkAtLeast: checkAtLeast | ||
}; | ||
|
||
function checkAtLeast(toCheck, checkAgainst) { | ||
toCheck = normalizedProtocol(toCheck); | ||
checkAgainst = normalizedProtocol(checkAgainst); | ||
if (toCheck.major > checkAgainst.major) return true; | ||
return toCheck.major === checkAgainst.major && | ||
toCheck.minor >= checkAgainst.minor; | ||
} | ||
|
||
function normalizedProtocol(protocol) { | ||
if (typeof protocol === 'string') { | ||
var segments = protocol.split('.'); | ||
protocol = { | ||
major: segments[0], | ||
minor: segments[1] | ||
}; | ||
} | ||
|
||
return { | ||
major: +(protocol.protocol || protocol.major || 0), | ||
minor: +(protocol.protocolMinor || protocol.minor || 0) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var protocol = require('../lib/protocol'); | ||
var expect = require('chai').expect; | ||
|
||
describe('protocol', function() { | ||
describe('checkAtLeast', function() { | ||
var FIXTURES = [ | ||
['1.0', '1.0', true], | ||
['1.1', '1.0', true], | ||
['1.0', '1.1', false], | ||
['1.0', '1', true], | ||
['1.10', '1.3', true], | ||
['2.0', '1.3', true], | ||
[{major: 1, minor: 0}, {major: 1, minor: 0}, true], | ||
[{major: 1, minor: 1}, {major: 1, minor: 0}, true], | ||
[{major: 1, minor: 0}, {major: 1, minor: 1}, false], | ||
[{protocol: 1, protocolMinor: 0}, {protocol: 1, protocolMinor: 0}, true], | ||
[{protocol: 1, protocolMinor: 1}, {protocol: 1, protocolMinor: 0}, true], | ||
[{protocol: 1, protocolMinor: 0}, {protocol: 1, protocolMinor: 1}, false], | ||
[{}, '1.0', false], | ||
['', '1.0', false] | ||
]; | ||
|
||
FIXTURES.forEach(function(fixture) { | ||
var is = fixture[2] ? ' is ' : ' is not '; | ||
var name = 'checks ' + JSON.stringify(fixture[0]) + is + 'at least ' + JSON.stringify(fixture[1]); | ||
it(name, function() { | ||
expect(protocol.checkAtLeast(fixture[0], fixture[1])).to.equal(fixture[2]); | ||
}); | ||
}); | ||
}); | ||
}); |