mirror of
https://github.com/misskey-dev/SyslogPro.git
synced 2025-04-29 02:37:18 +09:00
Merge pull request #11 from zeit/structured-data
Generate structured data from object with escape
This commit is contained in:
commit
0ebf6cb7dc
50
index.ts
50
index.ts
@ -1082,7 +1082,7 @@ export class RFC5424 extends RFC {
|
|||||||
super();
|
super();
|
||||||
options = options || {};
|
options = options || {};
|
||||||
this.hostname = options.hostname || os.hostname();
|
this.hostname = options.hostname || os.hostname();
|
||||||
this.applicationName = options.applicationName || '';
|
this.applicationName = options.applicationName || '-';
|
||||||
this.timestamp = options.timestamp !== false;
|
this.timestamp = options.timestamp !== false;
|
||||||
this.timestampMS = options.timestampMS !== false;
|
this.timestampMS = options.timestampMS !== false;
|
||||||
this.timestampTZ = options.timestampTZ !== false;
|
this.timestampTZ = options.timestampTZ !== false;
|
||||||
@ -1154,8 +1154,8 @@ export class RFC5424 extends RFC {
|
|||||||
* message
|
* message
|
||||||
* @param {string} [options.pid='-'] - The process id of the service sending
|
* @param {string} [options.pid='-'] - The process id of the service sending
|
||||||
* this message
|
* this message
|
||||||
* @param {string[]} [options.structuredData] - An array of structure
|
* @param {object} [options.structuredData] - An object of structure
|
||||||
* data strings conforming to the IETF/IANA defined SD-IDs or IANA
|
* data objects conforming to the IETF/IANA defined SD-IDs or IANA
|
||||||
* registered SMI Network Management Private Enterprise Code SD-ID
|
* registered SMI Network Management Private Enterprise Code SD-ID
|
||||||
* conforming to the format
|
* conforming to the format
|
||||||
* [name@<private enterprise number> parameter=value]
|
* [name@<private enterprise number> parameter=value]
|
||||||
@ -1181,7 +1181,6 @@ export class RFC5424 extends RFC {
|
|||||||
let applicationName = options.applicationName || this.applicationName;
|
let applicationName = options.applicationName || this.applicationName;
|
||||||
let pid = options.pid || '-';
|
let pid = options.pid || '-';
|
||||||
let id = options.id || '-';
|
let id = options.id || '-';
|
||||||
let msgStructuredData = options.msgStructuredData || [];
|
|
||||||
let fmtMsg = ''; // Formated Syslog message string var
|
let fmtMsg = ''; // Formated Syslog message string var
|
||||||
const newLine = '\n';
|
const newLine = '\n';
|
||||||
const newLineRegEx = /(\r|\n|(\r\n))/;
|
const newLineRegEx = /(\r|\n|(\r\n))/;
|
||||||
@ -1251,26 +1250,7 @@ export class RFC5424 extends RFC {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Build Structured Data string
|
// Build Structured Data string
|
||||||
let structuredData = '-';
|
const structuredData = RFC5424.buildStructuredData(options.structuredData);
|
||||||
const sdElementCount = msgStructuredData.length;
|
|
||||||
if (sdElementCount > 0) {
|
|
||||||
let sdElementNames = [];
|
|
||||||
let sdElements = [];
|
|
||||||
const sdElementNameRegEx = /(\[)(\S*)(\s|\])/;
|
|
||||||
// Loop to drop duplicates of the same SD Element name
|
|
||||||
for (let elementIndex = 0;
|
|
||||||
elementIndex < sdElementCount;
|
|
||||||
elementIndex++) {
|
|
||||||
let elementName =
|
|
||||||
msgStructuredData[elementIndex]
|
|
||||||
.match(sdElementNameRegEx)[2];
|
|
||||||
if (!sdElementNames.includes(elementName)) {
|
|
||||||
sdElementNames.push(elementName);
|
|
||||||
sdElements.push(msgStructuredData[elementIndex]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
structuredData = sdElements.join('');
|
|
||||||
}
|
|
||||||
// Build the message
|
// Build the message
|
||||||
fmtMsg = '<' + pri + '>';
|
fmtMsg = '<' + pri + '>';
|
||||||
fmtMsg += '1'; // Version number
|
fmtMsg += '1'; // Version number
|
||||||
@ -1288,6 +1268,28 @@ export class RFC5424 extends RFC {
|
|||||||
fmtMsg += newLine;
|
fmtMsg += newLine;
|
||||||
return fmtMsg;
|
return fmtMsg;
|
||||||
}
|
}
|
||||||
|
static buildStructuredData(data) {
|
||||||
|
// Build Structured Data string
|
||||||
|
let result;
|
||||||
|
if (data) {
|
||||||
|
result = Object.entries(data).map(([sdId, sdParam]) => {
|
||||||
|
return '[' + [
|
||||||
|
sdId,
|
||||||
|
...Object.entries(sdParam)
|
||||||
|
.map(([name, value]) =>
|
||||||
|
`${name}="${RFC5424.escapeParamValue(value)}"`
|
||||||
|
),
|
||||||
|
].join(' ') + ']';
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
return result || '-';
|
||||||
|
}
|
||||||
|
static escapeParamValue(value) {
|
||||||
|
return String(value)
|
||||||
|
.replace(/\\/g, '\\\\')
|
||||||
|
.replace(/"/g, '\\\"')
|
||||||
|
.replace(/]/g, '\\]');
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* send a RFC5424 formatted message. Returns a promise with the formatted
|
* send a RFC5424 formatted message. Returns a promise with the formatted
|
||||||
* message that was sent. If no server connection was defined when the
|
* message that was sent. If no server connection was defined when the
|
||||||
|
@ -336,10 +336,9 @@ describe('RFC5424 Class Tests', () => {
|
|||||||
});
|
});
|
||||||
result = rfc5424.buildMessage('hello', {
|
result = rfc5424.buildMessage('hello', {
|
||||||
msgColor: 30,
|
msgColor: 30,
|
||||||
msgStructuredData: [
|
structuredData: {
|
||||||
'[ourSDID@32473 test=test]',
|
'ourSDID@32473': { test: 'test' }
|
||||||
'[ourSDID@32473 test=test]'
|
}
|
||||||
]
|
|
||||||
});
|
});
|
||||||
resultMsg = /<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,3} /;
|
resultMsg = /<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,3} /;
|
||||||
expect(result).toMatch(resultMsg);
|
expect(result).toMatch(resultMsg);
|
||||||
@ -417,6 +416,23 @@ describe('RFC5424 Class Tests', () => {
|
|||||||
});
|
});
|
||||||
expect(result).toMatch(/^<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,3}[\+\-]\d{2}:\d{2} hostname applicationName - - - BOMhello\n$/);
|
expect(result).toMatch(/^<190>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,3}[\+\-]\d{2}:\d{2} hostname applicationName - - - BOMhello\n$/);
|
||||||
});
|
});
|
||||||
|
test('RFC5424 BuildMessage with structredData option', () => {
|
||||||
|
const rfc5424 = new SyslogPro.RFC5424();
|
||||||
|
const result = rfc5424.buildMessage('hello', {
|
||||||
|
structuredData: {
|
||||||
|
"hi@32473": {
|
||||||
|
foo: 1,
|
||||||
|
bar: 2
|
||||||
|
},
|
||||||
|
escape: {
|
||||||
|
quoteCharacter: '"',
|
||||||
|
backslack: '\\',
|
||||||
|
closingBrace: ']'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(result).toMatch(/^<190>1 \S+ \S+ - - - \[hi@32473 foo="1" bar="2"\]\[escape quoteCharacter="\\"" backslack="\\\\" closingBrace="\\]"\] BOMhello\n$/);
|
||||||
|
});
|
||||||
test('RFC5424 SetColors', () => {
|
test('RFC5424 SetColors', () => {
|
||||||
let rfc5424 = new SyslogPro.RFC5424();
|
let rfc5424 = new SyslogPro.RFC5424();
|
||||||
const result = rfc5424.setColor({
|
const result = rfc5424.setColor({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user