Skip to content

Commit 7598044

Browse files
authored
Modernize code and drop old Node versions (#281)
* Modernize code and drop old Node versions * update scripts and deps * update ci
1 parent fa1977e commit 7598044

File tree

20 files changed

+6478
-5360
lines changed

20 files changed

+6478
-5360
lines changed

.eslintignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

.eslintrc.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
contents: read
3939
strategy:
4040
matrix:
41-
node-version: [14, 16, 18, 20, 22]
41+
node-version: [20, 22, 24]
4242
os: [macos-latest, ubuntu-latest]
4343
exclude:
4444
- os: macos-latest

eslint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('neostandard')({})

index.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#! /usr/bin/env node
22

3-
'use strict';
3+
'use strict'
44

5-
const program = require('commander');
6-
const version = require('./package.json').version;
7-
const pinoGelf = require('./lib/pino-gelf');
5+
const program = require('commander')
6+
const version = require('./package.json').version
7+
const pinoGelf = require('./lib/pino-gelf')
88

99
program
10-
.version(version);
10+
.version(version)
1111

1212
program
1313
.command('log')
@@ -22,7 +22,7 @@ program
2222
.addOption(new program.Option('-v, --verbose', 'Output GELF to console'))
2323
.addOption(new program.Option('-t, --passthrough', 'Output original input to stdout to allow command chaining'))
2424
.action(function () {
25-
const options = this.opts();
25+
const options = this.opts()
2626

2727
const opts = {
2828
customKeys: options.specifyCustomFields || [],
@@ -35,21 +35,21 @@ program
3535
port: options.port || 12201,
3636
verbose: (options.verbose && !options.passthrough) || false,
3737
passthrough: options.passthrough || false
38-
};
39-
40-
switch(opts.protocol) {
41-
case 'udp':
42-
case 'http':
43-
case 'https':
44-
case 'tcp':
45-
case 'tls':
46-
break;
47-
default:
48-
throw new Error('Unsupported protocol ' + opts.protocol);
49-
}
50-
51-
pinoGelf(opts);
52-
});
38+
}
39+
40+
switch (opts.protocol) {
41+
case 'udp':
42+
case 'http':
43+
case 'https':
44+
case 'tcp':
45+
case 'tls':
46+
break
47+
default:
48+
throw new Error('Unsupported protocol ' + opts.protocol)
49+
}
50+
51+
pinoGelf(opts)
52+
})
5353

5454
program
55-
.parse(process.argv);
55+
.parse(process.argv)

lib/pino-gelf.js

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

3-
const fastJsonParse = require('fast-json-parse');
4-
const pipeline = require('./pipeline');
5-
const pump = require('pump');
6-
const split = require('split2');
3+
const fastJsonParse = require('fast-json-parse')
4+
const pipeline = require('./pipeline')
5+
const pump = require('pump')
6+
const split = require('split2')
77

88
module.exports = function (opts) {
9-
const pipe = pipeline(opts);
10-
pump(process.stdin, split(fastJsonParse), pipe);
11-
process.on('SIGINT', function () { process.exit(0); });
12-
};
9+
const pipe = pipeline(opts)
10+
pump(process.stdin, split(fastJsonParse), pipe)
11+
process.on('SIGINT', function () { process.exit(0) })
12+
}

lib/pipeline.js

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

3-
const through2 = require('through2');
4-
const transformer = require('./transformer');
5-
const utils = require('./utils');
6-
const Udp = require('./transports/udp');
7-
const Http = require('./transports/http');
8-
const Tcp = require('./transports/tcp');
3+
const through2 = require('through2')
4+
const transformer = require('./transformer')
5+
const utils = require('./utils')
6+
const Udp = require('./transports/udp')
7+
const Http = require('./transports/http')
8+
const Tcp = require('./transports/tcp')
99

1010
module.exports = function (opts) {
11-
let transport;
12-
switch(opts.protocol) {
13-
case 'udp':
14-
transport = new Udp(opts);
15-
break;
16-
case 'http':
17-
case 'https':
18-
transport = new Http(opts);
19-
break;
20-
case 'tcp':
21-
case 'tls':
22-
transport = new Tcp(opts);
23-
break;
11+
let transport
12+
switch (opts.protocol) {
13+
case 'udp':
14+
transport = new Udp(opts)
15+
break
16+
case 'http':
17+
case 'https':
18+
transport = new Http(opts)
19+
break
20+
case 'tcp':
21+
case 'tls':
22+
transport = new Tcp(opts)
23+
break
2424
}
2525

2626
return through2.obj(function (data, enc, cb) {
2727
if (data.value) {
28-
const transform = transformer(opts);
29-
const message = transform(data.value);
28+
const transform = transformer(opts)
29+
const message = transform(data.value)
3030

3131
if (opts.passthrough) {
3232
// Pass original input back to stdout to allow chaining of multiple commands
33-
setImmediate(function () { process.stdout.write(`${JSON.stringify(data.value)}\n`); });
33+
setImmediate(function () { process.stdout.write(`${JSON.stringify(data.value)}\n`) })
3434
} else if (opts.verbose) {
35-
const stringify = utils.stringify(opts);
36-
const messageString = stringify(message);
37-
setImmediate(function () { process.stdout.write(`${messageString}\n`); });
35+
const stringify = utils.stringify(opts)
36+
const messageString = stringify(message)
37+
setImmediate(function () { process.stdout.write(`${messageString}\n`) })
3838
}
3939

40-
transport.emit('log', message);
40+
transport.emit('log', message)
4141
}
4242

43-
cb();
44-
});
45-
};
43+
cb()
44+
})
45+
}

lib/transformer/express-gelf.js

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

3-
const _ = require('lodash');
3+
const _ = require('lodash')
44

5-
const standardKeys = ['pid', 'hostname', 'name', 'level', 'time', 'msg', 'v'];
5+
const standardKeys = ['pid', 'hostname', 'name', 'level', 'time', 'msg', 'v']
66

77
function filterStandardKeys (data) {
88
return Object
99
.keys(data)
10-
.filter(function (key) { return !standardKeys.includes(key); });
10+
.filter(function (key) { return !standardKeys.includes(key) })
1111
}
1212

1313
module.exports = function (data) {
14-
const expressKeys = filterStandardKeys(data);
15-
const expressGelf = _.pick(data, expressKeys);
14+
const expressKeys = filterStandardKeys(data)
15+
const expressGelf = _.pick(data, expressKeys)
1616

17-
if(_.has(expressGelf, 'req') && _.has(expressGelf.req, 'headers')) {
18-
expressGelf.req.headers = JSON.stringify(expressGelf.req.headers);
17+
if (_.has(expressGelf, 'req') && _.has(expressGelf.req, 'headers')) {
18+
expressGelf.req.headers = JSON.stringify(expressGelf.req.headers)
1919
}
2020

21-
if(_.has(expressGelf, 'res') && _.has(expressGelf.res, 'header')) {
22-
expressGelf.res.header = JSON.stringify(expressGelf.res.header);
21+
if (_.has(expressGelf, 'res') && _.has(expressGelf.res, 'header')) {
22+
expressGelf.res.header = JSON.stringify(expressGelf.res.header)
2323
}
2424

25-
return expressGelf;
26-
};
25+
return expressGelf
26+
}

lib/transformer/index.js

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

3-
const buildStandardGelf = require('./standard-gelf');
3+
const buildStandardGelf = require('./standard-gelf')
44

55
module.exports = function (/* opts */) {
66
return function (data) {
7-
const standardGelf = buildStandardGelf(data);
7+
const standardGelf = buildStandardGelf(data)
88

9-
return Object.assign({}, standardGelf /*, expressGelf */);
10-
};
11-
};
9+
return Object.assign({}, standardGelf /*, expressGelf */)
10+
}
11+
}

lib/transformer/standard-gelf.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
1-
const utils = require('./../utils');
1+
const utils = require('./../utils')
22

33
module.exports = function (data) {
4-
5-
let mandatoryPayload = {
4+
const mandatoryPayload = {
65
version: '1.1',
76
host: '0.0.0.0',
87
short_message: '[empty message]',
9-
};
8+
}
109

11-
let payload = {};
10+
const payload = {}
1211

1312
if (typeof data.hostname === 'string') {
14-
payload.host = data.hostname;
13+
payload.host = data.hostname
1514
}
1615

1716
if (typeof data.msg === 'string') {
1817
// msg is a string
19-
payload.short_message = data.msg.substring(0, 65);
18+
payload.short_message = data.msg.substring(0, 65)
2019

2120
// if (payload.short_message !== data.msg) {
22-
payload.full_message = data.msg;
21+
payload.full_message = data.msg
2322
// }
2423
} else if (typeof data.msg !== 'undefined') {
2524
// msg is something else
26-
payload.full_message = JSON.stringify(data.msg);
25+
payload.full_message = JSON.stringify(data.msg)
2726
}
2827

2928
if (typeof data.time === 'number') {
30-
payload.timestamp = data.time / 1000;
29+
payload.timestamp = data.time / 1000
3130
}
3231

3332
if (typeof data.level === 'number') {
34-
payload.level = utils.pinoLevelToSyslogLevel(data.level);
33+
payload.level = utils.pinoLevelToSyslogLevel(data.level)
3534
}
3635

3736
// Add all additional fields as underscore prefixed string values
38-
for (let [key, value] of Object.entries(data)) {
37+
for (const [key, value] of Object.entries(data)) {
3938
// console.log(key, value)
4039
if (!['hostname', 'msg', 'time', 'level', 'pid', 'v'].includes(key)) {
41-
payload[`_${key}`] = typeof value === 'string' ? value : JSON.stringify(value);
40+
payload[`_${key}`] = typeof value === 'string' ? value : JSON.stringify(value)
4241
}
4342
}
4443

45-
return Object.assign({}, mandatoryPayload, payload);
46-
};
44+
return Object.assign({}, mandatoryPayload, payload)
45+
}

0 commit comments

Comments
 (0)