Skip to content

Commit ac12683

Browse files
committed
chore: linters, tap
1 parent 47dbb04 commit ac12683

File tree

11 files changed

+248
-242
lines changed

11 files changed

+248
-242
lines changed

.eslintrc

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
{
2-
"env": {
3-
"es6": true,
4-
"node": true
5-
},
6-
"extends": "airbnb-base",
7-
"rules": {
8-
"strict": [0, "global"],
9-
"prefer-const": 1,
10-
"indent": [1, 4],
11-
"class-methods-use-this": [0],
12-
"import/no-extraneous-dependencies": [0],
13-
"arrow-body-style": [0, "always"],
14-
"no-multiple-empty-lines": [0],
15-
"no-underscore-dangle": [0],
16-
"comma-dangle": [0],
17-
"no-plusplus": [0],
18-
"no-console": [0],
19-
"new-cap": [0]
20-
}
2+
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
3+
"plugins": ["prettier"],
4+
"env": { "es6": true, "node": true },
5+
"parserOptions": {
6+
"requireConfigFile": false,
7+
"ecmaVersion": 2020,
8+
"sourceType": "module"
9+
}
2110
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ tmp/**/*
88
*.log
99
package-lock.json
1010
.nyc_output
11+
.tap

.prettierrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"singleQuote": false,
3+
"trailingComma": "all",
4+
"useTabs": true,
5+
"printWidth": 120,
6+
"overrides": [
7+
{
8+
"files": ["*.yml"],
9+
"options": {
10+
"tabWidth": 2,
11+
"useTabs": false
12+
}
13+
}
14+
]
15+
}

bin/bin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
// const path = require('path');
4-
const Daemon = require('../');
4+
const Daemon = require("../");
55

66
// const config = require(path.resolve(process.argv[2]));
77

example/cluster.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
'use strict';
1+
"use strict";
22

3-
const Emitter = require('@metrics/emitter');
4-
const cluster = require('cluster');
5-
const Client = require('@metrics/client');
6-
const stream = require('readable-stream');
7-
const os = require('os');
8-
const Daemon = require('../');
3+
const Emitter = require("@metrics/emitter");
4+
const cluster = require("cluster");
5+
const Client = require("@metrics/client");
6+
const stream = require("readable-stream");
7+
const os = require("os");
8+
const Daemon = require("../");
99

1010
const port = 60600;
1111

1212
const master = () => {
13-
const dest = new stream.Writable({
14-
objectMode: true,
15-
write(chunk, encoding, callback) {
16-
console.log(chunk);
17-
callback();
18-
},
19-
});
20-
21-
const daemon = new Daemon('udp', { port });
22-
daemon.pipe(dest);
23-
daemon.listen();
13+
const dest = new stream.Writable({
14+
objectMode: true,
15+
write(chunk, encoding, callback) {
16+
console.log(chunk);
17+
callback();
18+
},
19+
});
20+
21+
const daemon = new Daemon("udp", { port });
22+
daemon.pipe(dest);
23+
daemon.listen();
2424
};
2525

2626
const worker = () => {
27-
const emitter = new Emitter('udp', { port });
28-
const client = new Client();
29-
30-
client.pipe(emitter);
31-
32-
const interval = Math.floor((Math.random() * 1000) + 1000);
33-
let counter = 0;
34-
setInterval(() => {
35-
client.metric({
36-
name: `worker_${cluster.worker.id}`,
37-
description: `Worker number: ${cluster.worker.id}`,
38-
value: counter,
39-
});
40-
41-
counter += 1;
42-
}, interval);
27+
const emitter = new Emitter("udp", { port });
28+
const client = new Client();
29+
30+
client.pipe(emitter);
31+
32+
const interval = Math.floor(Math.random() * 1000 + 1000);
33+
let counter = 0;
34+
setInterval(() => {
35+
client.metric({
36+
name: `worker_${cluster.worker.id}`,
37+
description: `Worker number: ${cluster.worker.id}`,
38+
value: counter,
39+
});
40+
41+
counter += 1;
42+
}, interval);
4343
};
4444

4545
const workers = [];
4646

4747
if (cluster.isMaster) {
48-
for (let i = 0; i < (os.cpus().length - 1); i++) {
49-
workers.push(cluster.fork());
50-
}
51-
master();
48+
for (let i = 0; i < os.cpus().length - 1; i++) {
49+
workers.push(cluster.fork());
50+
}
51+
master();
5252
}
5353

5454
if (cluster.isWorker) {
55-
worker();
55+
worker();
5656
}

example/server.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
'use strict';
1+
"use strict";
22

3-
const stream = require('readable-stream');
4-
const Daemon = require('../');
3+
const stream = require("readable-stream");
4+
const Daemon = require("../");
55

66
const dest = new stream.Writable({
7-
objectMode: true,
8-
write(chunk, encoding, callback) {
9-
console.log(chunk);
10-
callback();
11-
},
7+
objectMode: true,
8+
write(chunk, encoding, callback) {
9+
console.log(chunk);
10+
callback();
11+
},
1212
});
1313

14-
const daemon = new Daemon('udp');
14+
const daemon = new Daemon("udp");
1515
daemon.pipe(dest);
1616
daemon.listen();

lib/daemon.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
'use strict';
1+
"use strict";
22

3-
const stream = require('readable-stream');
4-
const Udpd = require('./udpd');
3+
const stream = require("readable-stream");
4+
const Udpd = require("./udpd");
55

66
const MetricsDaemon = class MetricsDaemon extends stream.Readable {
7-
constructor(type = 'udp', options = {}) {
8-
super({ objectMode: true, });
9-
10-
let transport;
11-
if (type === 'udp') {
12-
transport = new Udpd(options);
13-
} else {
14-
throw new Error(`Transport type "${type || 'undefined'}" is not supported.`);
15-
}
16-
17-
Object.defineProperty(this, 'server', {
18-
value: transport,
19-
});
20-
21-
this.server.on('message', (metric) => {
22-
if (this.readableFlowing) {
23-
this.push(metric);
24-
}
25-
});
26-
}
27-
28-
get [Symbol.toStringTag]() {
29-
return 'MetricsDaemon';
30-
}
31-
32-
_read() {
33-
// nothiong to do, push happens in on message
34-
}
35-
36-
listen() {
37-
this.server.listen();
38-
}
7+
constructor(type = "udp", options = {}) {
8+
super({ objectMode: true });
9+
10+
let transport;
11+
if (type === "udp") {
12+
transport = new Udpd(options);
13+
} else {
14+
throw new Error(`Transport type "${type || "undefined"}" is not supported.`);
15+
}
16+
17+
Object.defineProperty(this, "server", {
18+
value: transport,
19+
});
20+
21+
this.server.on("message", (metric) => {
22+
if (this.readableFlowing) {
23+
this.push(metric);
24+
}
25+
});
26+
}
27+
28+
get [Symbol.toStringTag]() {
29+
return "MetricsDaemon";
30+
}
31+
32+
_read() {
33+
// nothiong to do, push happens in on message
34+
}
35+
36+
listen() {
37+
this.server.listen();
38+
}
3939
};
4040

4141
module.exports = MetricsDaemon;

lib/udpd.js

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
'use strict';
1+
"use strict";
22

3-
const EventEmitter = require('events');
4-
const Metric = require('@metrics/metric');
5-
const abslog = require('abslog');
6-
const dgram = require('dgram');
3+
const EventEmitter = require("events");
4+
const Metric = require("@metrics/metric");
5+
const abslog = require("abslog");
6+
const dgram = require("dgram");
77

88
const MetricsDaemonUdpd = class MetricsDaemonUdpd extends EventEmitter {
9-
constructor({ port = 40400, address, logger } = {}) {
10-
super();
11-
12-
Object.defineProperty(this, 'socket', {
13-
value: dgram.createSocket('udp4'),
14-
});
15-
16-
Object.defineProperty(this, 'address', {
17-
value: address,
18-
});
19-
20-
Object.defineProperty(this, 'port', {
21-
value: port,
22-
});
23-
24-
Object.defineProperty(this, 'log', {
25-
value: abslog(logger),
26-
});
27-
28-
this.socket.on('error', (error) => {
29-
this.log.error(error);
30-
this.socket.close();
31-
});
32-
33-
this.socket.on('message', (data, info) => {
34-
try {
35-
const obj = JSON.parse(data);
36-
const metric = new Metric(obj);
37-
this.emit('message', metric);
38-
} catch (err) {
39-
this.log.error(`Recieved non metric object from ${info.address}:${info.port}`);
40-
}
41-
});
42-
43-
this.socket.on('listening', () => {
44-
const info = this.socket.address();
45-
this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`);
46-
});
47-
}
48-
49-
get [Symbol.toStringTag]() {
50-
return 'MetricsDaemonUdpd';
51-
}
52-
53-
listen() {
54-
this.socket.bind(this.port, this.address);
55-
}
56-
57-
destroy() {
58-
this.socket.close();
59-
}
9+
constructor({ port = 40400, address, logger } = {}) {
10+
super();
11+
12+
Object.defineProperty(this, "socket", {
13+
value: dgram.createSocket("udp4"),
14+
});
15+
16+
Object.defineProperty(this, "address", {
17+
value: address,
18+
});
19+
20+
Object.defineProperty(this, "port", {
21+
value: port,
22+
});
23+
24+
Object.defineProperty(this, "log", {
25+
value: abslog(logger),
26+
});
27+
28+
this.socket.on("error", (error) => {
29+
this.log.error(error);
30+
this.socket.close();
31+
});
32+
33+
this.socket.on("message", (data, info) => {
34+
try {
35+
const obj = JSON.parse(data);
36+
const metric = new Metric(obj);
37+
this.emit("message", metric);
38+
} catch (err) {
39+
this.log.error(`Recieved non metric object from ${info.address}:${info.port}`);
40+
}
41+
});
42+
43+
this.socket.on("listening", () => {
44+
const info = this.socket.address();
45+
this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`);
46+
});
47+
}
48+
49+
get [Symbol.toStringTag]() {
50+
return "MetricsDaemonUdpd";
51+
}
52+
53+
listen() {
54+
this.socket.bind(this.port, this.address);
55+
}
56+
57+
destroy() {
58+
this.socket.close();
59+
}
6060
};
6161

6262
module.exports = MetricsDaemonUdpd;

0 commit comments

Comments
 (0)