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();
|
this.tcpSocketPromise = this.tcpConnect();
|
||||||
}
|
}
|
||||||
const socket = await this.tcpSocketPromise;
|
const socket = await this.tcpSocketPromise;
|
||||||
this.tcpSocketPromise = Promise.resolve(socket);
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const removeListeners = () => {
|
const removeListeners = () => {
|
||||||
socket.off('error', onceError);
|
socket.off('error', onceError);
|
||||||
@ -441,7 +440,6 @@ export class Syslog extends EventEmitter {
|
|||||||
this.tlsSocketPromise = this.tlsConnect();
|
this.tlsSocketPromise = this.tlsConnect();
|
||||||
}
|
}
|
||||||
const socket = await this.tlsSocketPromise;
|
const socket = await this.tlsSocketPromise;
|
||||||
this.tlsSocketPromise = Promise.resolve(socket);
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const removeListeners = () => {
|
const removeListeners = () => {
|
||||||
socket.off('error', onceError);
|
socket.off('error', onceError);
|
||||||
@ -773,6 +771,7 @@ type RFC3164Options = {
|
|||||||
extendedColor?: boolean;
|
extendedColor?: boolean;
|
||||||
facility?: number;
|
facility?: number;
|
||||||
hostname?: string;
|
hostname?: string;
|
||||||
|
octetCounting?: boolean;
|
||||||
server?: Syslog | SyslogOptions;
|
server?: Syslog | SyslogOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -797,6 +796,7 @@ export class RFC3164 extends RFC {
|
|||||||
color: boolean;
|
color: boolean;
|
||||||
facility: number;
|
facility: number;
|
||||||
hostname: string;
|
hostname: string;
|
||||||
|
octetCounting: boolean;
|
||||||
/**
|
/**
|
||||||
* Construct a new RFC3164 formatted Syslog object with user options
|
* Construct a new RFC3164 formatted Syslog object with user options
|
||||||
* @public
|
* @public
|
||||||
@ -864,6 +864,11 @@ export class RFC3164 extends RFC {
|
|||||||
} else {
|
} else {
|
||||||
this.server = new Syslog(options.server);
|
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) {
|
if (this.extendedColor) {
|
||||||
/** @private @type {number} */
|
/** @private @type {number} */
|
||||||
this.emergencyColor = 1; // Red foreground color
|
this.emergencyColor = 1; // Red foreground color
|
||||||
@ -957,8 +962,9 @@ export class RFC3164 extends RFC {
|
|||||||
fmtMsg += ' ' + hostname;
|
fmtMsg += ' ' + hostname;
|
||||||
fmtMsg += ' ' + applicationName;
|
fmtMsg += ' ' + applicationName;
|
||||||
fmtMsg += ' ' + msg;
|
fmtMsg += ' ' + msg;
|
||||||
fmtMsg += newLine;
|
return this.octetCounting
|
||||||
return fmtMsg;
|
? `${Buffer.byteLength(fmtMsg)} ${fmtMsg}`
|
||||||
|
: fmtMsg + newLine;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* send a RFC5424 formatted message. Returns a promise with the formatted
|
* send a RFC5424 formatted message. Returns a promise with the formatted
|
||||||
@ -1171,6 +1177,7 @@ type RFC5424Options = {
|
|||||||
};
|
};
|
||||||
extendedColor?: boolean;
|
extendedColor?: boolean;
|
||||||
hostname?: string;
|
hostname?: string;
|
||||||
|
octetCounting?: boolean;
|
||||||
server?: Syslog | SyslogOptions;
|
server?: Syslog | SyslogOptions;
|
||||||
timestamp?: boolean;
|
timestamp?: boolean;
|
||||||
timestampMS?: boolean;
|
timestampMS?: boolean;
|
||||||
@ -1199,6 +1206,7 @@ export class RFC5424 extends RFC {
|
|||||||
applicationName: string;
|
applicationName: string;
|
||||||
color: boolean;
|
color: boolean;
|
||||||
hostname: string;
|
hostname: string;
|
||||||
|
octetCounting: boolean;
|
||||||
timestamp: boolean;
|
timestamp: boolean;
|
||||||
timestampMS: boolean;
|
timestampMS: boolean;
|
||||||
timestampTZ: boolean;
|
timestampTZ: boolean;
|
||||||
@ -1285,6 +1293,11 @@ export class RFC5424 extends RFC {
|
|||||||
} else {
|
} else {
|
||||||
this.server = new Syslog(options.server);
|
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) {
|
if (this.extendedColor) {
|
||||||
/** @private @type {number} */
|
/** @private @type {number} */
|
||||||
this.emergencyColor = 1; // Red foreground color
|
this.emergencyColor = 1; // Red foreground color
|
||||||
@ -1437,8 +1450,9 @@ export class RFC5424 extends RFC {
|
|||||||
} else {
|
} else {
|
||||||
fmtMsg += ' ' + msg;
|
fmtMsg += ' ' + msg;
|
||||||
}
|
}
|
||||||
fmtMsg += newLine;
|
return this.octetCounting
|
||||||
return fmtMsg;
|
? `${Buffer.byteLength(fmtMsg)} ${fmtMsg}`
|
||||||
|
: fmtMsg + newLine;
|
||||||
}
|
}
|
||||||
static buildStructuredData(data) {
|
static buildStructuredData(data) {
|
||||||
// Build Structured Data string
|
// 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$/);
|
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', () => {
|
test('RFC5424 SetColors', () => {
|
||||||
let rfc5424 = new SyslogPro.RFC5424();
|
let rfc5424 = new SyslogPro.RFC5424();
|
||||||
const result = rfc5424.setColor({
|
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$/);
|
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
|
// Base Syslog Class Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user