-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-ini.js
41 lines (41 loc) · 1.23 KB
/
parser-ini.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module.exports = function (RED) {
'use strict';
const ini = require('ini');
function ParserIniNode(n) {
RED.nodes.createNode(this, n);
this.property = n.property || 'payload';
const node = this;
this.on('input', function (msg) {
let value = RED.util.getMessageProperty(msg, node.property);
if (value !== undefined) {
if (typeof value === 'string') {
try {
// Object.assign() to avoid bugs due to Object.create(null) used by the ini library
value = Object.assign({}, ini.parse(value));
RED.util.setMessageProperty(msg, node.property, value);
node.send(msg);
} catch (e) {
node.error(e.message, msg);
}
} else if (typeof value === 'object') {
if (!Buffer.isBuffer(value)) {
try {
value = ini.stringify(value);
RED.util.setMessageProperty(msg, node.property, value);
node.send(msg);
} catch (e) {
node.error(RED._('parser-ini.errors.dropped-error'));
}
} else {
node.warn(RED._('parser-ini.errors.dropped-object'));
}
} else {
node.warn(RED._('parser-ini.errors.dropped'));
}
} else {
node.send(msg); // If no payload - just pass it on.
}
});
}
RED.nodes.registerType('parser-ini', ParserIniNode);
};