Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 0562cb0

Browse files
author
Face Kapow
committed
Finish linting! 🎉
1 parent f94b97b commit 0562cb0

38 files changed

Lines changed: 962 additions & 1279 deletions

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ rules:
2727
import/no-extraneous-dependencies: 0
2828
no-param-reassign: 1
2929
no-return-assign: 1
30+
new-cap: 1
31+
no-console: 0
3032

3133
parserOptions:
3234
ecmaVersion: 6

docs/code-style-exceptions.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You can still use `for-in`, just be sure you know [the catch](https://developer.
1515
## Using 'dangling' underscores
1616

1717
You *can* use 'dangling' underscores to denote a private member on an object or class.
18-
Some APIs were written before this style was adopted which use underscores for private members and probably won't be changed for compatability.
18+
Some APIs were written before this style was adopted which use underscores for private members and probably won't be changed for compatibility.
1919
However, for any new APIs, it'd be preferred to use a [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) instead, like:
2020

2121
```js
@@ -33,4 +33,31 @@ class Demo {
3333

3434
## Using `get` and `set`
3535

36-
You can (and should) use ES6 `get` and `set`, it's already used in various runtime.js APIs.
36+
You can (and should) use ES6 `get` and `set` in new APIs, it's already used in various runtime.js APIs.
37+
38+
## Assigning to functions parameters
39+
40+
You cannot *reassign* functions parameters, but you can assign to *properties of* function parameters.
41+
```js
42+
// yes
43+
const demo = (myObject) => {
44+
myObject.someProperty = 'my value';
45+
// ...
46+
}
47+
48+
// no
49+
const demo = (myUint8Array) => {
50+
myUint8Array = {
51+
someProperty: 'my value'
52+
};
53+
// ...
54+
}
55+
```
56+
57+
If you get this warning, it's ok:
58+
59+
> Assignment to **property of** function parameter 'parameter-name'
60+
61+
But this is not:
62+
63+
> Assignment to function parameter 'parameter-name'

js/core/net/arp-header.js

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,43 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
var u8view = require('u8-view');
17-
var IP4Address = require('./ip4-address');
18-
var MACAddress = require('./mac-address');
16+
const u8view = require('u8-view');
17+
const IP4Address = require('./ip4-address');
18+
const MACAddress = require('./mac-address');
1919

20-
var HARDWARE_TYPE_ETHERNET = 1;
21-
var PROTOCOL_IP4 = 0x0800;
20+
const HARDWARE_TYPE_ETHERNET = 1;
21+
const PROTOCOL_IP4 = 0x0800;
2222

2323
exports.OPERATION_REQEUST = 1;
2424
exports.OPERATION_REPLY = 2;
2525

26-
exports.getSrcMAC = function(u8, headerOffset) {
27-
return new MACAddress(u8[headerOffset + 8],
28-
u8[headerOffset + 9],
29-
u8[headerOffset + 10],
30-
u8[headerOffset + 11],
31-
u8[headerOffset + 12],
32-
u8[headerOffset + 13]);
33-
};
26+
exports.getSrcMAC = (u8, headerOffset) => (new MACAddress(u8[headerOffset + 8],
27+
u8[headerOffset + 9],
28+
u8[headerOffset + 10],
29+
u8[headerOffset + 11],
30+
u8[headerOffset + 12],
31+
u8[headerOffset + 13]));
3432

35-
exports.getSrcIP = function(u8, headerOffset) {
36-
return new IP4Address(u8[headerOffset + 14],
37-
u8[headerOffset + 15],
38-
u8[headerOffset + 16],
39-
u8[headerOffset + 17]);
40-
};
33+
exports.getSrcIP = (u8, headerOffset) => (new IP4Address(u8[headerOffset + 14],
34+
u8[headerOffset + 15],
35+
u8[headerOffset + 16],
36+
u8[headerOffset + 17]));
4137

42-
exports.getTargetMAC = function(u8, headerOffset) {
43-
return new MACAddress(u8[headerOffset + 18],
44-
u8[headerOffset + 19],
45-
u8[headerOffset + 20],
46-
u8[headerOffset + 21],
47-
u8[headerOffset + 22],
48-
u8[headerOffset + 23]);
49-
};
38+
exports.getTargetMAC = (u8, headerOffset) => (new MACAddress(u8[headerOffset + 18],
39+
u8[headerOffset + 19],
40+
u8[headerOffset + 20],
41+
u8[headerOffset + 21],
42+
u8[headerOffset + 22],
43+
u8[headerOffset + 23]));
5044

51-
exports.getTargetIP = function(u8, headerOffset) {
52-
return new IP4Address(u8[headerOffset + 24],
53-
u8[headerOffset + 25],
54-
u8[headerOffset + 26],
55-
u8[headerOffset + 27]);
56-
};
45+
exports.getTargetIP = (u8, headerOffset) => (new IP4Address(u8[headerOffset + 24],
46+
u8[headerOffset + 25],
47+
u8[headerOffset + 26],
48+
u8[headerOffset + 27]));
5749

58-
exports.getOperation = function(u8, headerOffset) {
59-
return u8view.getUint16BE(u8, headerOffset + 6);
60-
};
50+
exports.getOperation = (u8, headerOffset) => u8view.getUint16BE(u8, headerOffset + 6);
6151

62-
exports.write = function(u8, headerOffset, operation, srcMAC, srcIP, targetMAC, targetIP) {
52+
exports.write = (u8, headerOffset, operation, srcMAC, srcIP, targetMAC, targetIP) => {
6353
u8view.setUint16BE(u8, headerOffset, HARDWARE_TYPE_ETHERNET);
6454
u8view.setUint16BE(u8, headerOffset + 2, PROTOCOL_IP4);
6555
u8[headerOffset + 4] = 6;

js/core/net/arp-resolver.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,52 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
var assert = require('assert');
17-
var MACAddress = require('./mac-address');
18-
var IP4Address = require('./ip4-address');
19-
var arpTransmit = require('./arp-transmit');
20-
var arpHeader = require('./arp-header');
16+
// const assert = require('assert');
17+
const MACAddress = require('./mac-address');
18+
const IP4Address = require('./ip4-address');
19+
const arpTransmit = require('./arp-transmit');
20+
const arpHeader = require('./arp-header');
2121

22-
function ARPResolver(intf) {
23-
this.intf = intf;
24-
this.table = new Map();
25-
}
26-
27-
ARPResolver.prototype.receive = function(u8, headerOffset) {
28-
var operation = arpHeader.getOperation(u8, headerOffset);
29-
var srcMAC = arpHeader.getSrcMAC(u8, headerOffset);
30-
var srcIP = arpHeader.getSrcIP(u8, headerOffset);
31-
var targetMAC = arpHeader.getTargetMAC(u8, headerOffset);
32-
var targetIP = arpHeader.getTargetIP(u8, headerOffset);
33-
var selfIP = this.intf.ipAddr;
22+
class ARPResolver {
23+
constructor(intf) {
24+
this.intf = intf;
25+
this.table = new Map();
26+
}
27+
receive(u8, headerOffset) {
28+
const operation = arpHeader.getOperation(u8, headerOffset);
29+
const srcMAC = arpHeader.getSrcMAC(u8, headerOffset);
30+
const srcIP = arpHeader.getSrcIP(u8, headerOffset);
31+
const targetMAC = arpHeader.getTargetMAC(u8, headerOffset);
32+
const targetIP = arpHeader.getTargetIP(u8, headerOffset);
33+
const selfIP = this.intf.ipAddr;
3434

35-
debug('recv ARP', operation, srcMAC, srcIP, targetMAC, targetIP, selfIP);
35+
debug('recv ARP', operation, srcMAC, srcIP, targetMAC, targetIP, selfIP);
3636

37-
switch (operation) {
38-
case arpHeader.OPERATION_REQEUST:
39-
// Somebody requested this machine IP
40-
if (!selfIP.equals(IP4Address.ANY) && selfIP.equals(targetIP)) {
41-
this.reply(srcMAC, targetIP);
37+
switch (operation) {
38+
case arpHeader.OPERATION_REQEUST:
39+
// Somebody requested this machine IP
40+
if (!selfIP.equals(IP4Address.ANY) && selfIP.equals(targetIP)) this.reply(srcMAC, targetIP);
41+
break;
42+
case arpHeader.OPERATION_REPLY:
43+
this.table.set(srcIP.hash(), srcMAC);
44+
break;
45+
default:
46+
break;
4247
}
43-
break;
44-
case arpHeader.OPERATION_REPLY:
45-
var key = srcIP.hash();
46-
this.table.set(key, srcMAC);
47-
break;
4848
}
49-
};
50-
51-
ARPResolver.prototype.request = function(targetIP) {
52-
arpTransmit(this.intf, arpHeader.OPERATION_REQEUST,
53-
this.intf.macAddr, this.intf.ipAddr,
54-
MACAddress.ZERO, targetIP);
55-
};
56-
57-
ARPResolver.prototype.reply = function(targetMAC, targetIP) {
58-
arpTransmit(this.intf, arpHeader.OPERATION_REPLY,
59-
this.intf.macAddr, this.intf.ipAddr,
60-
targetMAC, targetIP);
61-
};
62-
63-
ARPResolver.prototype.get = function(ip) {
64-
return this.table.get(ip.hash()) || null;
65-
};
49+
request(targetIP) {
50+
arpTransmit(this.intf, arpHeader.OPERATION_REQEUST,
51+
this.intf.macAddr, this.intf.ipAddr,
52+
MACAddress.ZERO, targetIP);
53+
}
54+
reply(targetMAC, targetIP) {
55+
arpTransmit(this.intf, arpHeader.OPERATION_REPLY,
56+
this.intf.macAddr, this.intf.ipAddr,
57+
targetMAC, targetIP);
58+
}
59+
get(ip) {
60+
return this.table.get(ip.hash()) || null;
61+
}
62+
}
6663

6764
module.exports = ARPResolver;

js/core/net/arp-transmit.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
var ethernet = require('./ethernet');
17-
var arpHeader = require('./arp-header');
18-
var MACAddress = require('./mac-address');
16+
const ethernet = require('./ethernet');
17+
const arpHeader = require('./arp-header');
18+
const MACAddress = require('./mac-address');
1919

20-
module.exports = function(intf, operation, srcMAC, srcIP, targetMAC, targetIP) {
21-
var ethOffset = intf.bufferDataOffset;
22-
var arpOffset = ethOffset + ethernet.headerLength;
23-
var len = arpOffset + arpHeader.headerLength;
24-
var u8 = new Uint8Array(len);
20+
module.exports = (intf, operation, srcMAC, srcIP, targetMAC, targetIP) => {
21+
const ethOffset = intf.bufferDataOffset;
22+
const arpOffset = ethOffset + ethernet.headerLength;
23+
const len = arpOffset + arpHeader.headerLength;
24+
const u8 = new Uint8Array(len);
2525
ethernet.write(u8, ethOffset, MACAddress.BROADCAST,
2626
intf.macAddr, ethernet.ETHERTYPE_ARP);
2727
arpHeader.write(u8, arpOffset, operation, srcMAC, srcIP, targetMAC, targetIP);

js/core/net/checksum.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,32 @@
1414

1515
'use strict';
1616

17-
function checksum(u8, offset, len, extraSum) {
18-
var count = len >>> 1;
19-
var acc = (extraSum >>> 0);
20-
for (var i = 0; i < count; ++i) {
21-
acc += (u8[offset + i * 2] << 8) + u8[offset + i * 2 + 1];
22-
}
17+
const checksum = (u8, offset, len, extraSum) => {
18+
const count = len >>> 1;
19+
let acc = (extraSum >>> 0);
20+
for (let i = 0; i < count; ++i) acc += (u8[offset + (i * 2)] << 8) + u8[(offset + (i * 2)) + 1];
2321

24-
if (count * 2 !== len) {
25-
acc += u8[offset + count * 2] << 8;
26-
}
22+
if (count * 2 !== len) acc += u8[offset + (count * 2)] << 8;
2723

2824
acc = (acc & 0xffff) + (acc >>> 16);
2925
acc += (acc >>> 16);
3026
return ((~acc) & 0xffff) >>> 0;
31-
}
27+
};
3228

3329
module.exports = checksum;
3430

35-
module.exports.buffer = function(u8, offset, len) {
36-
var count = len >>> 1;
37-
var acc = 0;
38-
for (var i = 0; i < count; ++i) {
39-
acc += (u8[offset + i * 2] << 8) + u8[offset + i * 2 + 1];
40-
}
31+
module.exports.buffer = (u8, offset, len) => {
32+
const count = len >>> 1;
33+
let acc = 0;
34+
for (let i = 0; i < count; ++i) acc += (u8[offset + (i * 2)] << 8) + u8[(offset + (i * 2)) + 1];
4135

42-
if (count * 2 !== len) {
43-
acc += u8[offset + count * 2] << 8;
44-
}
36+
if (count * 2 !== len) acc += u8[offset + (count * 2)] << 8;
4537

4638
return acc;
4739
};
4840

49-
module.exports.result = function(acc) {
50-
acc = (acc & 0xffff) + (acc >>> 16);
41+
module.exports.result = (accOpt) => {
42+
let acc = (accOpt & 0xffff) + (accOpt >>> 16);
5143
acc += (acc >>> 16);
5244
return ((~acc) & 0xffff) >>> 0;
5345
};

js/core/net/ethernet.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,30 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
var assert = require('assert');
17-
var MACAddress = require('./mac-address');
18-
var u8view = require('u8-view');
16+
// const assert = require('assert');
17+
const MACAddress = require('./mac-address');
18+
const u8view = require('u8-view');
1919

2020
exports.ETHERTYPE_IP4 = 0x0800;
2121
exports.ETHERTYPE_ARP = 0x0806;
2222

23-
exports.getDestMAC = function(u8, headerOffset) {
24-
return new MACAddress(u8[headerOffset + 0],
25-
u8[headerOffset + 1],
26-
u8[headerOffset + 2],
27-
u8[headerOffset + 3],
28-
u8[headerOffset + 4],
29-
u8[headerOffset + 5]);
30-
};
23+
exports.getDestMAC = (u8, headerOffset) => (new MACAddress(u8[headerOffset + 0],
24+
u8[headerOffset + 1],
25+
u8[headerOffset + 2],
26+
u8[headerOffset + 3],
27+
u8[headerOffset + 4],
28+
u8[headerOffset + 5]));
3129

32-
exports.getSrcMAC = function(u8, headerOffset) {
33-
return new MACAddress(u8[headerOffset + 6],
34-
u8[headerOffset + 7],
35-
u8[headerOffset + 8],
36-
u8[headerOffset + 9],
37-
u8[headerOffset + 10],
38-
u8[headerOffset + 11]);
39-
};
30+
exports.getSrcMAC = (u8, headerOffset) => (new MACAddress(u8[headerOffset + 6],
31+
u8[headerOffset + 7],
32+
u8[headerOffset + 8],
33+
u8[headerOffset + 9],
34+
u8[headerOffset + 10],
35+
u8[headerOffset + 11]));
4036

41-
exports.getEtherType = function(u8, headerOffset) {
42-
return ((u8[headerOffset + 12] << 8) + u8[headerOffset + 13]) >>> 0;
43-
};
37+
exports.getEtherType = (u8, headerOffset) => ((u8[headerOffset + 12] << 8) + u8[headerOffset + 13]) >>> 0;
4438

45-
exports.write = function(u8, headerOffset, destMAC, srcMAC, etherType) {
39+
exports.write = (u8, headerOffset, destMAC, srcMAC, etherType) => {
4640
u8[headerOffset + 0] = destMAC.a;
4741
u8[headerOffset + 1] = destMAC.b;
4842
u8[headerOffset + 2] = destMAC.c;

0 commit comments

Comments
 (0)