New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

winston-syslog

Package Overview
Dependencies
Maintainers
5
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

winston-syslog - npm Package Compare versions

Comparing version 2.3.0 to 2.4.0

test/message-test.js

8

CHANGELOG.md
# CHANGELOG
## v2.4.0 / 2020-01-01
- (@DABH) Node v12 support, fix node-unix-dgram issues, update dependencies
- #[115], (@pepakriz) TLS connection support
- #[123], (@JeffTomlinson, @elliotttf) Handle oversize messages sent over UDP transports
- #[116], (@pepakriz) Make socket options configurable
- #[122], (@cjbarth) Correct improper opening and closing of sockets
## v2.2.0 / 2019-08-14

@@ -4,0 +12,0 @@

@@ -23,2 +23,9 @@ 'use strict';

case 'tls':
case 'tls4':
return { type: 'tls', family: 4 };
case 'tls6':
return { type: 'tls', family: 6 };
default:

@@ -25,0 +32,0 @@ throw new Error('Invalid syslog protocol: ' + protocol);

202

lib/winston-syslog.js

@@ -11,2 +11,3 @@ /*

const net = require('net');
const secNet = require('tls');
const utils = require('./utils');

@@ -18,2 +19,4 @@ const glossy = require('glossy');

const _noop = () => {};
// Ensure we have the correct winston here.

@@ -73,3 +76,3 @@ if (Number(winston.version.split('.')[0]) < 3) {

//
this.socket = null;
this.socket = null;
var Producer = options.customProducer || glossy.Produce;

@@ -89,2 +92,3 @@ this.producer = new Producer({

this.protocol = options.protocol || 'udp4';
this.protocolOptions = options.protocolOptions || {};
this.endOfLine = options.eol;

@@ -97,3 +101,6 @@

//
this.localhost = typeof options.localhost !== 'undefined' ? options.localhost : 'localhost';
this.localhost =
typeof options.localhost !== 'undefined'
? options.localhost
: 'localhost';
this.type = options.type || 'BSD';

@@ -108,5 +115,5 @@ this.facility = options.facility || 'local0';

this.protocolType = parsedProtocol.type;
this.protocolType = parsedProtocol.type;
this.protocolFamily = parsedProtocol.family;
this.isDgram = parsedProtocol.isDgram;
this.isDgram = parsedProtocol.isDgram;

@@ -119,2 +126,53 @@ if (this.protocolType === 'unix' && !this.path) {

//
// ### function chunkMessage (buffer, callback)
// #### @buffer {Buffer} Syslog message buffer.
// #### @callback {function} Continuation to respond to when complete.
// Syslog messages sent over the UDP transport must be 64KB bytes or less. In
// order to avoid silent failures messages should be chunked when the buffer
// to write is larger than the maximum message size.
//
chunkMessage(buffer, callback = _noop) {
if (!this.connected) {
this.queue.push(buffer);
} else {
// Get maximum syslog message size for the UDP transport in bytes.
// https://tools.ietf.org/html/rfc5426#section-3.2
const maxUdpLength = this.socket.getSendBufferSize();
let offset = 0;
while (offset < buffer.length) {
this.inFlight++;
const length =
offset + maxUdpLength > buffer.length
? buffer.length - offset
: maxUdpLength;
this._sendChunk(
buffer,
{ offset: offset, length: length, port: this.port, host: this.host },
callback
);
offset += length;
}
}
}
//
// ### function _sendChunk (buffer, options, callback)
// #### @buffer {Buffer} Syslog message buffer.
// #### @options {object} Options for the message send method.
// #### @callback {function} Continuation to respond to when complete.
// Sends a single chunk from an oversize UDP buffer.
//
_sendChunk(buffer, options, callback) {
this.socket.send(
buffer,
options.offset,
options.length,
options.port,
options.host,
callback
);
}
//
// ### function log (info, callback)

@@ -129,3 +187,5 @@ // #### @info {object} All relevant log information

if (!~levels.indexOf(level)) {
return callback(new Error('Cannot log unknown syslog level: ' + info[LEVEL]));
return callback(
new Error('Cannot log unknown syslog level: ' + info[LEVEL])
);
}

@@ -145,3 +205,3 @@ level = level === 'warn' ? 'warning' : level;

//
this.connect((err) => {
this.connect(err => {
if (err) {

@@ -159,3 +219,3 @@ //

//
const onError = (logErr) => {
const onError = logErr => {
if (logErr) {

@@ -174,7 +234,6 @@ this.queue.push(syslogMsg);

const sendDgram = () => {
const buffer = new Buffer(syslogMsg);
const buffer = Buffer.from(syslogMsg);
if (this.protocolType === 'udp') {
this.inFlight++;
this.socket.send(buffer, 0, buffer.length, this.port, this.host, onError);
this.chunkMessage(buffer, onError);
} else if (this.protocol === 'unix') {

@@ -188,3 +247,3 @@ this.inFlight++;

this.inFlight++;
this.socket.send(buffer, (e) => {
this.socket.send(buffer, e => {
this.socket.removeListener('congestion', onCongestion);

@@ -244,3 +303,3 @@ onError(e);

this.socket = require('unix-dgram').createSocket('unix_dgram');
} else {
} else if (!this.socket) {
// UDP protocol

@@ -251,3 +310,12 @@ const proto = this.protocol === 'udp' ? 'udp4' : this.protocol;

type: proto
});
})
.on('listening', () => {
this.connected = true;
let msg = this.queue.shift();
while (msg) {
this.chunkMessage(msg);
msg = this.queue.shift();
}
});
this.socket.bind();
}

@@ -268,3 +336,5 @@

if (this.socket) {
return ((!this.socket.readyState) || (this.socket.readyState === 'open')) || this.socket.connected
return !this.socket.readyState ||
this.socket.readyState === 'open' ||
this.socket.connected
? callback(null)

@@ -281,3 +351,3 @@ : callback(true);

this.socket = new net.Socket();
this.socket = this.protocol === 'tls' ? new secNet.Socket() : new net.Socket();
this.socket.setKeepAlive(true);

@@ -288,6 +358,6 @@ this.socket.setNoDelay();

const connectConfig = {
const connectConfig = Object.assign({}, this.protocolOptions, {
host: this.host,
port: this.port
};
});

@@ -313,4 +383,6 @@ if (this.protocolFamily) {

//
const onError = (logErr) => {
if (logErr) { this.emit('error', logErr); }
const onError = logErr => {
if (logErr) {
this.emit('error', logErr);
}
this.emit('logged');

@@ -324,39 +396,43 @@ this.inFlight--;

//
this.socket.on(readyEvent, () => {
//
// When the socket is ready, write the current queue
// to it.
//
this.socket.write(this.queue.join(''), 'utf8', onError);
this.socket
.on(readyEvent, () => {
//
// When the socket is ready, write the current queue
// to it.
//
this.socket.write(this.queue.join(''), 'utf8', onError);
this.emit('logged');
this.queue = [];
this.retries = 0;
this.connected = true;
}).on('error', function () {
//
// TODO: Pass this error back up
//
}).on('close', () => {
//
// Attempt to reconnect on lost connection(s), progressively
// increasing the amount of time between each try.
//
const interval = Math.pow(2, this.retries);
this.connected = false;
this.emit('logged');
this.queue = [];
this.retries = 0;
this.connected = true;
})
.on('error', function () {
//
// TODO: Pass this error back up
//
})
.on('close', () => {
//
// Attempt to reconnect on lost connection(s), progressively
// increasing the amount of time between each try.
//
const interval = Math.pow(2, this.retries);
this.connected = false;
setTimeout(() => {
this.retries++;
this.socket.connect(this.port, this.host);
}, interval * 1000);
}).on('timeout', () => {
if (this.socket.destroy) {
// https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback
this.socket.destroy();
} else if (this.socket.close) {
// https://nodejs.org/api/dgram.html#dgram_socket_close_callback
// https://www.npmjs.com/package/unix-dgram#socketclose
this.socket.close();
}
});
setTimeout(() => {
this.retries++;
this.socket.connect(this.port, this.host);
}, interval * 1000);
})
.on('timeout', () => {
if (this.socket.destroy) {
// https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback
this.socket.destroy();
} else if (this.socket.close) {
// https://nodejs.org/api/dgram.html#dgram_socket_close_callback
// https://www.npmjs.com/package/unix-dgram#socketclose
this.socket.close();
}
});
}

@@ -369,9 +445,15 @@

let sentMsgs = 0;
this.queue.forEach((msg) => {
const buffer = new Buffer(msg);
this.queue.forEach(msg => {
const buffer = Buffer.from(msg);
if (!this.congested) {
this.socket.send(buffer, function () {
++sentMsgs;
});
if (this.protocolType === 'udp') {
this.chunkMessage(buffer, () => {
++sentMsgs;
});
} else {
this.socket.send(buffer, function () {
++sentMsgs;
});
}
}

@@ -384,3 +466,3 @@ });

this.socket = require('unix-dgram').createSocket('unix_dgram');
this.socket.on('error', (err) => {
this.socket.on('error', err => {
this.emit('error', err);

@@ -387,0 +469,0 @@

{
"name": "winston-syslog",
"description": "A syslog transport for winston",
"version": "2.3.0",
"version": "2.4.0",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"contributors": [
{
"name": "DABH",
"email": "DABH@users.noreply.github.com"
},
{
"name": "Squeeks",

@@ -29,6 +33,9 @@ "email": "privacymyass@gmail.com"

"cycle": "^1.0.3",
"glossy": "^0.1.7"
"eslint-config-populist": "^4.2.0",
"glossy": "^0.1.7",
"sinon": "^8.0.2",
"vows": "^0.8.3"
},
"optionalDependencies": {
"unix-dgram": "2.0.2"
"unix-dgram": "2.0.3"
},

@@ -39,4 +46,2 @@ "peerDependencies": {

"devDependencies": {
"eslint-config-populist": "4.2.0",
"vows": "^0.8.2",
"winston": "^3.0.0"

@@ -51,5 +56,5 @@ },

"engines": {
"node": ">= 4.2.2"
"node": ">= 8"
},
"license": "MIT"
}

@@ -54,3 +54,4 @@ # winston-syslog

* __port:__ The port on the host that syslog is running on, defaults to syslogd's default port.
* __protocol:__ The network protocol to log over (e.g. `tcp4`, `udp4`, `unix`, `unix-connect`, etc).
* __protocol:__ The network protocol to log over (e.g. `tcp4`, `udp4`, `tls4`, `unix`, `unix-connect`, etc).
* __protocolOptions:__ Socket connect options. See [`net.socket.connect`](https://nodejs.org/api/net.html#net_socket_connect_options_connectlistener) for available options.
* __path:__ The path to the syslog dgram socket (i.e. `/dev/log` or `/var/run/syslog` for OS X).

@@ -57,0 +58,0 @@ * __pid:__ PID of the process that log messages are coming from (Default `process.pid`).

@@ -15,34 +15,16 @@ 'use strict';

vows.describe('syslog messages').addBatch({
'opening fake syslog server': {
'topic': function () {
const self = this;
server = dgram.createSocket('udp4');
server.on('listening', function () {
self.callback();
});
server.bind(PORT);
},
'default format': {
vows
.describe('syslog messages')
.addBatch({
'opening fake syslog server': {
'topic': function () {
const self = this;
server.once('message', function (msg) {
parser.parse(msg, function (d) {
self.callback(null, d);
});
server = dgram.createSocket('udp4');
server.on('listening', function () {
self.callback();
});
transport = new Syslog({
port: PORT
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping' }, function (err) {
assert.ifError(err);
});
server.bind(PORT);
},
'should have host field set to localhost': function (msg) {
assert.equal(msg.host, 'localhost');
transport.close();
},
'setting locahost option to a different falsy value (null)': {
'default format': {
'topic': function () {

@@ -57,15 +39,13 @@ const self = this;

transport = new Syslog({
port: PORT,
localhost: null
port: PORT
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping2' }, function (err) {
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping' }, function (err) {
assert.ifError(err);
});
},
'should have host different from localhost': function (msg) {
assert.notEqual(msg.host, 'localhost');
'should have host field set to localhost': function (msg) {
assert.equal(msg.host, 'localhost');
transport.close();
},
'setting appName option to hello': {
'setting locahost option to a different falsy value (null)': {
'topic': function () {

@@ -81,15 +61,16 @@ const self = this;

port: PORT,
type: '5424',
appName: 'hello'
localhost: null
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'app name test' }, function (err) {
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping2' }, function (
err
) {
assert.ifError(err);
});
},
'should have appName field set to hello': function (msg) {
assert.equal(msg.appName, 'hello');
'should have host different from localhost': function (msg) {
assert.notEqual(msg.host, 'localhost');
transport.close();
},
'setting app_name option to hello': {
'setting appName option to hello': {
'topic': function () {

@@ -106,8 +87,11 @@ const self = this;

type: '5424',
app_name: 'hello'
appName: 'hello'
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'app name test' }, function (err) {
assert.ifError(err);
});
transport.log(
{ [LEVEL]: 'debug', [MESSAGE]: 'app name test' },
function (err) {
assert.ifError(err);
}
);
},

@@ -117,52 +101,80 @@ 'should have appName field set to hello': function (msg) {

transport.close();
},
'setting app_name option to hello': {
'topic': function () {
const self = this;
server.once('message', function (msg) {
parser.parse(msg, function (d) {
self.callback(null, d);
});
});
transport = new Syslog({
port: PORT,
type: '5424',
app_name: 'hello'
});
transport.log(
{ [LEVEL]: 'debug', [MESSAGE]: 'app name test' },
function (err) {
assert.ifError(err);
}
);
},
'should have appName field set to hello': function (msg) {
assert.equal(msg.appName, 'hello');
transport.close();
}
}
}
}
},
'teardown': function () {
server.close();
}
},
'teardown': function () {
server.close();
}
}
}).addBatch({
'opening fake syslog server': {
'topic': function () {
var self = this;
server = dgram.createSocket('udp4');
server.on('listening', function () {
self.callback();
});
server.bind(PORT);
},
'Custom producer': {
})
.addBatch({
'opening fake syslog server': {
'topic': function () {
var self = this;
server.once('message', function (msg) {
self.callback(null, msg.toString());
server = dgram.createSocket('udp4');
server.on('listening', function () {
self.callback();
});
function CustomProducer() {}
CustomProducer.prototype.produce = function (opts) {
return 'test ' + opts.severity + ': ' + opts.message;
};
server.bind(PORT);
},
'Custom producer': {
'topic': function () {
var self = this;
server.once('message', function (msg) {
self.callback(null, msg.toString());
});
transport = new Syslog({
port: PORT,
customProducer: CustomProducer
});
function CustomProducer() {}
CustomProducer.prototype.produce = function (opts) {
return 'test ' + opts.severity + ': ' + opts.message;
};
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping' }, function (err) {
assert.ifError(err);
});
transport = new Syslog({
port: PORT,
customProducer: CustomProducer
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping' }, function (err) {
assert.ifError(err);
});
},
'should apply custom syslog format': function (msg) {
assert.equal(msg, 'test debug: ping');
transport.close();
}
},
'should apply custom syslog format': function (msg) {
assert.equal(msg, 'test debug: ping');
transport.close();
'teardown': function () {
server.close();
}
},
'teardown': function () {
server.close();
}
}
}).export(module);
})
.export(module);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc