From 93efe032091dec793008a494707cef0cff3c030c Mon Sep 17 00:00:00 2001 From: nkzawa Date: Fri, 11 Oct 2019 15:01:01 +0900 Subject: [PATCH] support octet-counting method (RFC6587) --- index.ts | 26 ++++++++++++++++++++------ tests/syslog.test.js | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/index.ts b/index.ts index 690c02f..c707cdc 100644 --- a/index.ts +++ b/index.ts @@ -362,7 +362,6 @@ export class Syslog extends EventEmitter { this.tcpSocketPromise = this.tcpConnect(); } const socket = await this.tcpSocketPromise; - this.tcpSocketPromise = Promise.resolve(socket); return new Promise((resolve, reject) => { const removeListeners = () => { socket.off('error', onceError); @@ -441,7 +440,6 @@ export class Syslog extends EventEmitter { this.tlsSocketPromise = this.tlsConnect(); } const socket = await this.tlsSocketPromise; - this.tlsSocketPromise = Promise.resolve(socket); return new Promise((resolve, reject) => { const removeListeners = () => { socket.off('error', onceError); @@ -773,6 +771,7 @@ type RFC3164Options = { extendedColor?: boolean; facility?: number; hostname?: string; + octetCounting?: boolean; server?: Syslog | SyslogOptions; }; @@ -797,6 +796,7 @@ export class RFC3164 extends RFC { color: boolean; facility: number; hostname: string; + octetCounting: boolean; /** * Construct a new RFC3164 formatted Syslog object with user options * @public @@ -864,6 +864,11 @@ export class RFC3164 extends RFC { } else { this.server = new Syslog(options.server); } + if (this.server.protocol === 'tcp' || this.server.protocol === 'tls') { + this.octetCounting = options.octetCounting !== false; + } else { + this.octetCounting = false; + } if (this.extendedColor) { /** @private @type {number} */ this.emergencyColor = 1; // Red foreground color @@ -957,8 +962,9 @@ export class RFC3164 extends RFC { fmtMsg += ' ' + hostname; fmtMsg += ' ' + applicationName; fmtMsg += ' ' + msg; - fmtMsg += newLine; - return fmtMsg; + return this.octetCounting + ? `${Buffer.byteLength(fmtMsg)} ${fmtMsg}` + : fmtMsg + newLine; } /** * send a RFC5424 formatted message. Returns a promise with the formatted @@ -1171,6 +1177,7 @@ type RFC5424Options = { }; extendedColor?: boolean; hostname?: string; + octetCounting?: boolean; server?: Syslog | SyslogOptions; timestamp?: boolean; timestampMS?: boolean; @@ -1199,6 +1206,7 @@ export class RFC5424 extends RFC { applicationName: string; color: boolean; hostname: string; + octetCounting: boolean; timestamp: boolean; timestampMS: boolean; timestampTZ: boolean; @@ -1285,6 +1293,11 @@ export class RFC5424 extends RFC { } else { this.server = new Syslog(options.server); } + if (this.server.protocol === 'tcp' || this.server.protocol === 'tls') { + this.octetCounting = options.octetCounting !== false; + } else { + this.octetCounting = false; + } if (this.extendedColor) { /** @private @type {number} */ this.emergencyColor = 1; // Red foreground color @@ -1437,8 +1450,9 @@ export class RFC5424 extends RFC { } else { fmtMsg += ' ' + msg; } - fmtMsg += newLine; - return fmtMsg; + return this.octetCounting + ? `${Buffer.byteLength(fmtMsg)} ${fmtMsg}` + : fmtMsg + newLine; } static buildStructuredData(data) { // Build Structured Data string diff --git a/tests/syslog.test.js b/tests/syslog.test.js index 5e96423..24ec8e0 100644 --- a/tests/syslog.test.js +++ b/tests/syslog.test.js @@ -462,6 +462,15 @@ describe('RFC5424 Class Tests', () => { }); expect(result).toMatch(/^<190>1 \S+ \S+ - - - \[hi@32473 foo="1" bar="2"\]\[escape quoteCharacter="\\"" backslack="\\\\" closingBrace="\\]"\] BOMhello\n$/); }); + test('RFC5424 BuildMessage with octet-counting', () => { + const rfc5424 = new SyslogPro.RFC5424({ + server: { + protocol: 'tcp' + } + }); + const result = rfc5424.buildMessage('hello'); + expect(result).toMatch(/^\d+ <190>1/); + }); test('RFC5424 SetColors', () => { let rfc5424 = new SyslogPro.RFC5424(); const result = rfc5424.setColor({ @@ -792,6 +801,15 @@ describe('RFC3164 Class Tests', () => { }); expect(result).toMatch(/^<190>[A-Z][a-z]{2} [ \d]\d \d{2}:\d{2}:\d{2} hostname applicationName hello\n$/); }); + test('RFC3164 BuildMessage with octet-counting', () => { + const rfc3164 = new SyslogPro.RFC3164({ + server: { + protocol: 'tcp' + } + }); + const result = rfc3164.buildMessage('hello'); + expect(result).toMatch(/^\d+ <190>/); + }); }); // Base Syslog Class Test