Skip to content

Commit

Permalink
chore: linters, tap
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Mar 7, 2024
1 parent 47dbb04 commit ac12683
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 242 deletions.
27 changes: 8 additions & 19 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
{
"env": {
"es6": true,
"node": true
},
"extends": "airbnb-base",
"rules": {
"strict": [0, "global"],
"prefer-const": 1,
"indent": [1, 4],
"class-methods-use-this": [0],
"import/no-extraneous-dependencies": [0],
"arrow-body-style": [0, "always"],
"no-multiple-empty-lines": [0],
"no-underscore-dangle": [0],
"comma-dangle": [0],
"no-plusplus": [0],
"no-console": [0],
"new-cap": [0]
}
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"plugins": ["prettier"],
"env": { "es6": true, "node": true },
"parserOptions": {
"requireConfigFile": false,
"ecmaVersion": 2020,
"sourceType": "module"
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ tmp/**/*
*.log
package-lock.json
.nyc_output
.tap
15 changes: 15 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"singleQuote": false,
"trailingComma": "all",
"useTabs": true,
"printWidth": 120,
"overrides": [
{
"files": ["*.yml"],
"options": {
"tabWidth": 2,
"useTabs": false
}
}
]
}
2 changes: 1 addition & 1 deletion bin/bin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

// const path = require('path');
const Daemon = require('../');
const Daemon = require("../");

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

Expand Down
78 changes: 39 additions & 39 deletions example/cluster.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
'use strict';
"use strict";

const Emitter = require('@metrics/emitter');
const cluster = require('cluster');
const Client = require('@metrics/client');
const stream = require('readable-stream');
const os = require('os');
const Daemon = require('../');
const Emitter = require("@metrics/emitter");
const cluster = require("cluster");
const Client = require("@metrics/client");
const stream = require("readable-stream");
const os = require("os");
const Daemon = require("../");

const port = 60600;

const master = () => {
const dest = new stream.Writable({
objectMode: true,
write(chunk, encoding, callback) {
console.log(chunk);
callback();
},
});

const daemon = new Daemon('udp', { port });
daemon.pipe(dest);
daemon.listen();
const dest = new stream.Writable({
objectMode: true,
write(chunk, encoding, callback) {
console.log(chunk);
callback();
},
});

const daemon = new Daemon("udp", { port });
daemon.pipe(dest);
daemon.listen();
};

const worker = () => {
const emitter = new Emitter('udp', { port });
const client = new Client();

client.pipe(emitter);

const interval = Math.floor((Math.random() * 1000) + 1000);
let counter = 0;
setInterval(() => {
client.metric({
name: `worker_${cluster.worker.id}`,
description: `Worker number: ${cluster.worker.id}`,
value: counter,
});

counter += 1;
}, interval);
const emitter = new Emitter("udp", { port });
const client = new Client();

client.pipe(emitter);

const interval = Math.floor(Math.random() * 1000 + 1000);
let counter = 0;
setInterval(() => {
client.metric({
name: `worker_${cluster.worker.id}`,
description: `Worker number: ${cluster.worker.id}`,
value: counter,
});

counter += 1;
}, interval);
};

const workers = [];

if (cluster.isMaster) {
for (let i = 0; i < (os.cpus().length - 1); i++) {
workers.push(cluster.fork());
}
master();
for (let i = 0; i < os.cpus().length - 1; i++) {
workers.push(cluster.fork());
}
master();
}

if (cluster.isWorker) {
worker();
worker();
}
18 changes: 9 additions & 9 deletions example/server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';
"use strict";

const stream = require('readable-stream');
const Daemon = require('../');
const stream = require("readable-stream");
const Daemon = require("../");

const dest = new stream.Writable({
objectMode: true,
write(chunk, encoding, callback) {
console.log(chunk);
callback();
},
objectMode: true,
write(chunk, encoding, callback) {
console.log(chunk);
callback();
},
});

const daemon = new Daemon('udp');
const daemon = new Daemon("udp");
daemon.pipe(dest);
daemon.listen();
70 changes: 35 additions & 35 deletions lib/daemon.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
'use strict';
"use strict";

const stream = require('readable-stream');
const Udpd = require('./udpd');
const stream = require("readable-stream");
const Udpd = require("./udpd");

const MetricsDaemon = class MetricsDaemon extends stream.Readable {
constructor(type = 'udp', options = {}) {
super({ objectMode: true, });

let transport;
if (type === 'udp') {
transport = new Udpd(options);
} else {
throw new Error(`Transport type "${type || 'undefined'}" is not supported.`);
}

Object.defineProperty(this, 'server', {
value: transport,
});

this.server.on('message', (metric) => {
if (this.readableFlowing) {
this.push(metric);
}
});
}

get [Symbol.toStringTag]() {
return 'MetricsDaemon';
}

_read() {
// nothiong to do, push happens in on message
}

listen() {
this.server.listen();
}
constructor(type = "udp", options = {}) {
super({ objectMode: true });

let transport;
if (type === "udp") {
transport = new Udpd(options);
} else {
throw new Error(`Transport type "${type || "undefined"}" is not supported.`);
}

Object.defineProperty(this, "server", {
value: transport,
});

this.server.on("message", (metric) => {
if (this.readableFlowing) {
this.push(metric);
}
});
}

get [Symbol.toStringTag]() {
return "MetricsDaemon";
}

_read() {
// nothiong to do, push happens in on message
}

listen() {
this.server.listen();
}
};

module.exports = MetricsDaemon;
112 changes: 56 additions & 56 deletions lib/udpd.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
'use strict';
"use strict";

const EventEmitter = require('events');
const Metric = require('@metrics/metric');
const abslog = require('abslog');
const dgram = require('dgram');
const EventEmitter = require("events");
const Metric = require("@metrics/metric");
const abslog = require("abslog");
const dgram = require("dgram");

const MetricsDaemonUdpd = class MetricsDaemonUdpd extends EventEmitter {
constructor({ port = 40400, address, logger } = {}) {
super();

Object.defineProperty(this, 'socket', {
value: dgram.createSocket('udp4'),
});

Object.defineProperty(this, 'address', {
value: address,
});

Object.defineProperty(this, 'port', {
value: port,
});

Object.defineProperty(this, 'log', {
value: abslog(logger),
});

this.socket.on('error', (error) => {
this.log.error(error);
this.socket.close();
});

this.socket.on('message', (data, info) => {
try {
const obj = JSON.parse(data);
const metric = new Metric(obj);
this.emit('message', metric);
} catch (err) {
this.log.error(`Recieved non metric object from ${info.address}:${info.port}`);
}
});

this.socket.on('listening', () => {
const info = this.socket.address();
this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`);
});
}

get [Symbol.toStringTag]() {
return 'MetricsDaemonUdpd';
}

listen() {
this.socket.bind(this.port, this.address);
}

destroy() {
this.socket.close();
}
constructor({ port = 40400, address, logger } = {}) {
super();

Object.defineProperty(this, "socket", {
value: dgram.createSocket("udp4"),
});

Object.defineProperty(this, "address", {
value: address,
});

Object.defineProperty(this, "port", {
value: port,
});

Object.defineProperty(this, "log", {
value: abslog(logger),
});

this.socket.on("error", (error) => {
this.log.error(error);
this.socket.close();
});

this.socket.on("message", (data, info) => {
try {
const obj = JSON.parse(data);
const metric = new Metric(obj);
this.emit("message", metric);
} catch (err) {
this.log.error(`Recieved non metric object from ${info.address}:${info.port}`);
}
});

this.socket.on("listening", () => {
const info = this.socket.address();
this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`);
});
}

get [Symbol.toStringTag]() {
return "MetricsDaemonUdpd";
}

listen() {
this.socket.bind(this.port, this.address);
}

destroy() {
this.socket.close();
}
};

module.exports = MetricsDaemonUdpd;
Loading

0 comments on commit ac12683

Please sign in to comment.