Merge pull request #13 from zeit/octet-counting

Support octet-counting method (RFC6587)
This commit is contained in:
Naoyuki Kanezawa 2019-10-11 15:03:38 +09:00 committed by GitHub
commit 27ce1bc715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 6 deletions

View File

@ -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

View File

@ -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