node-red / node-red/node-red.github.io

msg transport - by reference issue

未关闭
#50 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
118
派生
161
平均合并
8 小时 18 分钟
30 天内合并 PR
4

描述

Following discussions elsewhere I made a node called "node-red-contrib-diode" after discovering something I should have known already - that messages are passed by referrence, not by value. This can cause issues as someone may use a message twice in a function - sending it out with node-send and then re-using it. Should the receiving function or node alter the message in any way, because it was sent by reference - this can actually damage the original message. "diode" stops this - but I think this should be clearly pointed out in the documentation.

I found this...

ttp://nodered.org/docs/creating-nodes/node-js
Section "receiving messages"

Suggest you add: "Please note that a reference to the original message is passed here. Altering that message could, in edge cases, affect the calling function."

http://nodered.org/docs/writing-functions
Section "Writing functions"
Subsection "Writing a function"...

You say:

"The returned message object does not need to be same object as was passed in; the function can construct a completely new object before returning it. For example:
var newMsg = { payload: msg.payload.length };
return newMsg;
Note: constructing a new message object will lose any message properties of the received message. This will break some flows, for example the HTTP In/Response flow requires the msg.req and msg.res properties to be preserved end-to-end. In general, function nodes should return the message object they were passed having made any changes to its properties."

So firstly - why would construction a new message lose properties of the old. Using the code DaveCJ provided:

var newMsg = RED.util.cloneMessage(msg);

does not lose any of the original message - I tested it... so you seem to be saying NOT to make a new object - but you don't warn of the consequences. Why not change that example to use the code above which will ensure everything is copied across - scrap the warning that data could be lost and instead tell readers that this might slightly increase overhead - but will eliminate any chance of interaction with the original message.

In my diode node - here you see the unprocessed output - and the output through the node - they are identical down to the msgID - except for the payload which I deliberately altered to show the feedback effect.

msg : Object
{ _msgid: "e5a704f0.1a58f8", topic: "", payload: "goodbye", blah: "fred" }
6/26/2017, 2:31:23 PMnode: d9f3747f.7ef978
msg : Object
{ _msgid: "e5a704f0.1a58f8", topic: "", payload: "hello", blah: "fred" }

Hope this is helpful.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

检查 creating-nodes/node-js 文档中的“receiving messages”部分,以及 writing-functions 中的“Writing a function”小节。在决定哪些指导是准确的之前,核实 issue 关于消息引用、RED.util.cloneMessage(msg) 和属性保留的说法。完成的标准是两个部分都清楚地说明行为及其后果,且彼此不矛盾。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
半天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。