node-red-contrib-motechat
Version:
ultranet topic and payload communication
30 lines (26 loc) • 952 B
JavaScript
const path = require('path')
const debug = require('../lib/mcClient.js').debug
module.exports = (RED) => {
function js(config) {
RED.nodes.createNode(this, config)
const node = this
const { path: jsPath } = config
const correctPath = path.isAbsolute(jsPath) ?
jsPath : path.resolve(jsPath)
node.on('input', (msg, send, done) => {
send = send || function() { node.send.apply(node, arguments) }
try {
const exec = require(correctPath)
if (typeof exec !== 'function') {
debug('NOT_FUNCTION')
node.error('NOT_FUNCTION')
return
}
send(exec(msg))
} catch (err) { node.error('REQUIRE_FAIL', err) } finally {
if (done) done()
}
})
}
RED.nodes.registerType("js", js)
}