Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. function asyncEvaluateNodeProperty (RED, value, type, node, msg) {
  2. return new Promise(function (resolve, reject) {
  3. RED.util.evaluateNodeProperty(value, type, node, msg, function (e, r) {
  4. if (e) {
  5. reject(e)
  6. } else {
  7. resolve(r)
  8. }
  9. })
  10. })
  11. }
  12. async function appendTopic (RED, config, wNode, msg) {
  13. // populate topic if the node specifies one
  14. if (config.topic || config.topicType) {
  15. try {
  16. msg.topic = await asyncEvaluateNodeProperty(RED, config.topic, config.topicType || 'str', wNode, msg) || ''
  17. } catch (_err) {
  18. // do nothing
  19. console.error(_err)
  20. }
  21. }
  22. // ensure we have a topic property in the msg, even if it's an empty string
  23. if (!('topic' in msg)) {
  24. msg.topic = ''
  25. }
  26. return msg
  27. }
  28. /**
  29. * Adds socket/client data to a msg payload, if enabled
  30. *
  31. */
  32. function addConnectionCredentials (RED, msg, conn, config) {
  33. if (config.includeClientData) {
  34. if (!msg._client) {
  35. msg._client = {}
  36. }
  37. RED.plugins.getByType('node-red-dashboard-2').forEach(plugin => {
  38. if (plugin.hooks?.onAddConnectionCredentials && msg) {
  39. msg = plugin.hooks.onAddConnectionCredentials(conn, msg)
  40. }
  41. })
  42. msg._client = {
  43. ...msg._client,
  44. ...{
  45. socketId: conn.id,
  46. socketIp: conn.handshake?.address
  47. }
  48. }
  49. }
  50. return msg
  51. }
  52. module.exports = {
  53. asyncEvaluateNodeProperty,
  54. appendTopic,
  55. addConnectionCredentials
  56. }