Wrap msg.req/res objects and add deprecated warnings to functions

This commit is contained in:
Nick O'Leary 2015-09-27 23:49:14 +01:00
parent d668d43a0a
commit ecde942255
3 changed files with 104 additions and 10 deletions

View File

@ -33,7 +33,7 @@
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
<div class="form-row row-swagger-doc">
<label for="node-input-swaggerDoc"><i class="fa fa-tag"></i> <span data-i18n="httpin.label.doc"></span></label>
<label for="node-input-swaggerDoc"><i class="fa fa-file-text-o"></i> <span data-i18n="httpin.label.doc"></span></label>
<input type="text" id="node-input-swaggerDoc">
</div>
<div id="node-input-tip" class="form-tips"><span data-i18n="httpin.tip.in"></span><code><span id="node-input-path"></span></code>.</div>

View File

@ -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"));

View File

@ -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"