Skip to content

Commit

Permalink
add eslint, travis and coveralls
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Tiertant committed Apr 27, 2018
1 parent c6aaf1b commit b603314
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 102 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/
29 changes: 29 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"extends": "eslint:recommended",
"env": {
"node": true,
"es6": true
},
"rules": {
"indent": [1, 2, { "SwitchCase": 1 }],
"no-undef": 1,
"no-console": 1,
"keyword-spacing": 1,
"no-trailing-spaces": 1,

"no-unused-vars": 1,
"no-redeclare": 1,
"no-unused-labels": 1,
"no-unreachable": 1,
"semi": [1, "always"],
"no-extra-semi": 1,
"no-fallthrough": 1,
"no-constant-condition": 1,
"no-mixed-spaces-and-tabs": 1,
"no-case-declarations": 1
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
package-lock.json
package-lock.json
.nyc_output/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ package-lock.json
node_modules
test
karma.conf.js
.eslintignore
.eslintrc
.nyc_output/
.travis.yml
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- '6'
- '8'
- '9'
sudo: false
after_success: npm run coveralls
2 changes: 1 addition & 1 deletion lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ address.prototype.UintAddress = function() {

address.prototype.toString = function() {
var str = '';
switch(this.family) {
switch (this.family) {
case CONSTANTS.TRANSPORT.FAMILY.IPV4:
str += 'IPV4://';
break;
Expand Down
1 change: 0 additions & 1 deletion lib/allocation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const CONSTANTS = require('./constants');
const Transport = require('./transport');
const Address = require('./address');
const Message = require('./message');
const ChannelMsg = require('./channelMessage');
Expand Down
87 changes: 42 additions & 45 deletions lib/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const CONSTANTS = require('./constants');
const Data = require('./data');
const Address = require('./address');

/*
* STUN Attributes https://tools.ietf.org/html/rfc5389#section-15
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Value (variable) ....
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/*
* STUN Attributes https://tools.ietf.org/html/rfc5389#section-15
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Value (variable) ....
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

var attribute = function(msg, name, value) {
if (!msg) {
Expand All @@ -34,40 +34,40 @@ var attribute = function(msg, name, value) {
if (!this.type) {
throw new Error('invalid attribute name: ' + name);
}

this.value = value;

switch(this.type) {
switch (this.type) {
case CONSTANTS.ATTR.MAPPED_ADDRESS:
this.length = 8;
if (this.value.family === CONSTANTS.TRANSPORT.FAMILY.IPV6) {
this.length = 20;
}
break;

case CONSTANTS.ATTR.USERNAME:
this.length = this.value.length;
break;

case CONSTANTS.ATTR.MESSAGE_INTEGRITY:
this.length = 20;
break;

case CONSTANTS.ATTR.ERROR_CODE:
this.length = 4 + this.value.reason.length;
break;

case CONSTANTS.ATTR.UNKNOWN_ATTRIBUTES:
break;

case CONSTANTS.ATTR.REALM:
this.length = this.value.length;
break;

case CONSTANTS.ATTR.NONCE:
this.length = this.value.length;
break;

case CONSTANTS.ATTR.XOR_MAPPED_ADDRESS:
case CONSTANTS.ATTR.XOR_PEER_ADDRESS:
case CONSTANTS.ATTR.XOR_RELAYED_ADDRESS:
Expand All @@ -76,50 +76,49 @@ var attribute = function(msg, name, value) {
this.length = 20;
}
break;

// Comprehension-optional range (0x8000-0xFFFF)
case CONSTANTS.ATTR.SOFTWARE:
this.length = this.value.length;
break;

case CONSTANTS.ATTR.ALTERNATE_SERVER:
break;

case CONSTANTS.ATTR.FINGERPRINT:
break;

// https://tools.ietf.org/html/rfc5766#section-6.2
case CONSTANTS.ATTR.CHANNEL_NUMBER:
this.length = 4;
break;

case CONSTANTS.ATTR.LIFETIME:
this.length = 4;
break;

case CONSTANTS.ATTR.DATA:
this.length = this.value.length;
break;

case CONSTANTS.ATTR.EVEN_PORT:
this.length = 1;
break;

case CONSTANTS.ATTR.REQUESTED_TRANSPORT:
this.length = 4;
break;

case CONSTANTS.ATTR.DONT_FRAGMENT:
this.length = 0;
break;

case CONSTANTS.ATTR.RESERVATION_TOKEN:
this.length = 8;
break;

default:
throw new Error('invalid type ' + name);
break;
}
}

Expand All @@ -131,7 +130,7 @@ attribute.prototype.read = function(data) {
this.type = data.readUIntBE(0, 2);
this.length = data.readUIntBE(2, 2);
this.padding = this.length % 4 ? 4 - (this.length % 4) : 0;
switch(this.type) {
switch (this.type) {
case CONSTANTS.ATTR.ALTERNATE_SERVER:
this.value = data.readUIntBE(4, this.length);
break;
Expand Down Expand Up @@ -208,7 +207,6 @@ attribute.prototype.read = function(data) {
break;
default:
throw new Error('Invalid Attribute type ' + this.type.toString(16));
break;
}

Object.keys(CONSTANTS.ATTR).forEach(function(name) {
Expand All @@ -220,7 +218,7 @@ attribute.prototype.read = function(data) {

attribute.prototype.toBuffer = function() {
var attrValue = Data.alloc(this.length);
switch(this.type) {
switch (this.type) {
case CONSTANTS.ATTR.MAPPED_ADDRESS:
/*
* MAPPED-ADDRESS https://tools.ietf.org/html/rfc5389#section-15.1
Expand Down Expand Up @@ -255,7 +253,7 @@ attribute.prototype.toBuffer = function() {

case CONSTANTS.ATTR.USERNAME:
// value: string (username)
attrValue.write(value);
attrValue.write(this.value);
break;

case CONSTANTS.ATTR.MESSAGE_INTEGRITY:
Expand All @@ -279,7 +277,7 @@ attribute.prototype.toBuffer = function() {
case CONSTANTS.ATTR.ERROR_CODE:
/*
* ERROR-CODE https://tools.ietf.org/html/rfc5389#section-15.6
*
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Expand Down Expand Up @@ -317,15 +315,15 @@ attribute.prototype.toBuffer = function() {
case CONSTANTS.ATTR.REALM:
attrValue.write(this.value);
break;

case CONSTANTS.ATTR.NONCE:
attrValue.write(this.value);
break;

case CONSTANTS.ATTR.XOR_MAPPED_ADDRESS:
case CONSTANTS.ATTR.XOR_PEER_ADDRESS:
case CONSTANTS.ATTR.XOR_RELAYED_ADDRESS:
/*
/*
* XOR-MAPPED-ADDRESS https://tools.ietf.org/html/rfc5389#section-15.2
*
* 0 1 2 3
Expand All @@ -345,11 +343,11 @@ attribute.prototype.toBuffer = function() {
attrValue.writeUIntBE((this.value.port ^ (this.msg.magicCookie >> 16)), 2, 2);
// write X-Address
if (this.value.family === CONSTANTS.TRANSPORT.FAMILY.IPV4) {
var ip = this.value.address.split('.');
var address = parseInt(ip[0]) * Math.pow(2, 24);
address += parseInt(ip[1]) * Math.pow(2, 16);
address += parseInt(ip[2]) * Math.pow(2, 8);
address += parseInt(ip[3]);
var ipaddr = this.value.address.split('.');
var address = parseInt(ipaddr[0]) * Math.pow(2, 24);
address += parseInt(ipaddr[1]) * Math.pow(2, 16);
address += parseInt(ipaddr[2]) * Math.pow(2, 8);
address += parseInt(ipaddr[3]);
var xorAddress = address ^ this.msg.magicCookie;
attrValue.writeUIntBE(xorAddress, 4, 4);
} else {
Expand Down Expand Up @@ -402,10 +400,9 @@ attribute.prototype.toBuffer = function() {
// 0 - String: token value
attrValue.write(this.value);
break;

default:
throw new Error('invalid type ' + this.type);
break;
}

var attrHeader = Buffer.alloc(4);
Expand Down
6 changes: 3 additions & 3 deletions lib/authentification.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ authentification.prototype.shortTerm = function(msg, cb) {
// reject the request with an error response. This response MUST use an error code of 400 (Bad Request).
reply.reject(400, 'Bad Request');
return cb(new Error('Bad Request'));

} else if (msg.class === CONSTANTS.CLASS.INDICATION) {
// silently discard the indication
reply.discard();
Expand All @@ -50,7 +50,7 @@ authentification.prototype.shortTerm = function(msg, cb) {
// reject the request with an error response. This response MUST use an error code of 401 (Unauthorized).
reply.reject(401, 'Unauthorized');
return cb(new Error('Unauthorized'));

} else if (msg.class === CONSTANTS.CLASS.INDICATION) {
// silently discard the indication
reply.discard();
Expand Down Expand Up @@ -103,7 +103,7 @@ authentification.prototype.longTerm = function(msg, cb) {
// This response MUST include NONCE and REALM attributes
reply.addAttribute('realm', this.server.realm);
reply.addAttribute('nonce', this.generateNonce());
// and SHOULD NOT include the USERNAME or MESSAGE-INTEGRITY attribute.
// and SHOULD NOT include the USERNAME or MESSAGE-INTEGRITY attribute.
// Servers can invalidate nonces in order to provide additional security.
reply.reject(438, 'Stale Nonce');
return cb(new Error('Stale Nonce'));
Expand Down
11 changes: 0 additions & 11 deletions lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,6 @@ data.prototype.writeUncontiguous = function(value, indexArray) {
}
};

data.prototype.writeWord = function(offset, length) {
var value = 0;
for (var i = 0; i < length; i++) {
var pos = offset + length - i - 1;
let weight = Math.pow(2, indexArray.length -1 - i);
if (self.readBit(pos)) {
value += weight;
}
}
};

data.prototype.writeWord = function(value, offset, length) {
var bits = Array.from(value.toString(2));
if (bits.length > length) {
Expand Down
Loading

0 comments on commit b603314

Please sign in to comment.