From ecde942255f93b7dabc6027c5ff8f62b318f9710 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sun, 27 Sep 2015 23:49:14 +0100 Subject: [PATCH] Wrap msg.req/res objects and add deprecated warnings to functions --- nodes/core/io/21-httpin.html | 2 +- nodes/core/io/21-httpin.js | 109 +++++++++++++++++++++++-- nodes/core/locales/en-US/messages.json | 3 +- 3 files changed, 104 insertions(+), 10 deletions(-) diff --git a/nodes/core/io/21-httpin.html b/nodes/core/io/21-httpin.html index b308ac81e..7c58e6ad9 100644 --- a/nodes/core/io/21-httpin.html +++ b/nodes/core/io/21-httpin.html @@ -33,7 +33,7 @@
- +
.
diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index a83fb55eb..731bbc969 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -45,6 +45,99 @@ module.exports = function(RED) { var corsSetup = false; + + function createRequestWrapper(node,req) { + var wrapper = { + _req: req + }; + var toWrap = [ + "param", + "get", + "is", + "acceptsCharset", + "acceptsLanguage", + "app", + "baseUrl", + "body", + "cookies", + "fresh", + "hostname", + "ip", + "ips", + "originalUrl", + "params", + "path", + "protocol", + "query", + "route", + "secure", + "signedCookies", + "stale", + "subdomains", + "xhr", + "socket" // TODO: tidy this up + ]; + toWrap.forEach(function(f) { + if (typeof req[f] === "function") { + wrapper[f] = function() { + node.warn(RED._("httpin.errors.deprecated-call",{method:"msg.req."+f})); + var result = req[f].apply(req,arguments); + if (result === res) { + return wrapper; + } else { + return result; + } + } + } else { + wrapper[f] = req[f]; + } + }); + + + return wrapper; + } + function createResponseWrapper(node,res) { + var wrapper = { + _res: res + }; + var toWrap = [ + "append", + "attachment", + "cookie", + "clearCookie", + "download", + "end", + "format", + "get", + "json", + "jsonp", + "links", + "location", + "redirect", + "render", + "send", + "sendfile", + "sendFile", + "sendStatus", + "set", + "status", + "type", + "vary" + ]; + toWrap.forEach(function(f) { + wrapper[f] = function() { + node.warn(RED._("httpin.errors.deprecated-call",{method:"msg.res."+f})); + var result = res[f].apply(res,arguments); + if (result === res) { + return wrapper; + } else { + return result; + } + } + }); + return wrapper; + } + function HTTPIn(n) { RED.nodes.createNode(this,n); if (RED.settings.httpNodeRoot !== false) { @@ -68,11 +161,11 @@ module.exports = function(RED) { var msgid = RED.util.generateId(); res._msgid = msgid; if (node.method.match(/(^post$|^delete$|^put$|^options$)/)) { - node.send({_msgid:msgid,req:req,res:res,payload:req.body}); + node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.body}); } else if (node.method == "get") { - node.send({_msgid:msgid,req:req,res:res,payload:req.query}); + node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.query}); } else { - node.send({_msgid:msgid,req:req,res:res}); + node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res)}); } }; @@ -143,13 +236,13 @@ module.exports = function(RED) { this.on("input",function(msg) { if (msg.res) { if (msg.headers) { - msg.res.set(msg.headers); + msg.res._res.set(msg.headers); } var statusCode = msg.statusCode || 200; if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) { - msg.res.status(statusCode).jsonp(msg.payload); + msg.res._res.status(statusCode).jsonp(msg.payload); } else { - if (msg.res.get('content-length') == null) { + if (msg.res._res.get('content-length') == null) { var len; if (msg.payload == null) { len = 0; @@ -160,10 +253,10 @@ module.exports = function(RED) { } else { len = Buffer.byteLength(msg.payload); } - msg.res.set('content-length', len); + msg.res._res.set('content-length', len); } - msg.res.send(statusCode,msg.payload); + msg.res._res.status(statusCode).send(msg.payload); } } else { node.warn(RED._("httpin.errors.no-response")); diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index bf9162faf..529bae5e8 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -272,7 +272,8 @@ "missing-path": "missing path", "no-response": "No response object", "json-error": "JSON parse error", - "no-url": "No url specified" + "no-url": "No url specified", + "deprecated-call":"Deprecated call to __method__" }, "status": { "requesting": "requesting"