mirror of
https://github.com/misskey-dev/SyslogPro.git
synced 2025-04-29 02:37:18 +09:00
support octet-counting method (RFC6587)
This commit is contained in:
parent
ffe6a5f0c4
commit
93efe03209
26
index.ts
26
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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user