1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Always request buffer

This commit is contained in:
bartbutenaers 2017-03-05 15:07:16 +01:00 committed by Nick O'Leary
parent 9bd4598c6a
commit d7f5b0c9d7
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -161,30 +161,19 @@ module.exports = function(RED) {
tlsNode.addTLSOptions(opts);
}
var req = ((/^https/.test(urltotest))?https:http).request(opts,function(res) {
//(node.ret === "bin") ? res.setEncoding('binary') : res.setEncoding('utf8');
if (node.ret === "bin") {
// Force NodeJs to return a Buffer instead of a string
// Force NodeJs to return a Buffer (instead of a string)
// See https://github.com/nodejs/node/issues/6038
res.setEncoding(null);
delete res._readableState.decoder;
}
msg.statusCode = res.statusCode;
msg.headers = res.headers;
msg.responseUrl = res.responseUrl;
if (node.ret === "bin") {
msg.payload = [];
}
else {
msg.payload = "";
}
// msg.url = url; // revert when warning above finally removed
res.on('data',function(chunk) {
if (node.ret === "bin") {
msg.payload.push(chunk);
}
else {
msg.payload += chunk;
}
});
res.on('end',function() {
if (node.metric()) {
@ -197,13 +186,18 @@ module.exports = function(RED) {
node.metric("size.bytes", msg, res.client.bytesRead);
}
}
if (node.ret === "bin") {
msg.payload = Buffer.concat(msg.payload);
}
else if (node.ret === "obj") {
try { msg.payload = JSON.parse(msg.payload); }
// Convert the payload to the required return type
msg.payload = Buffer.concat(msg.payload); // bin
if (node.ret !== "bin") {
msg.payload = msg.payload.toString('utf8'); // txt
if (node.ret === "obj") {
try { msg.payload = JSON.parse(msg.payload); } // obj
catch(e) { node.warn(RED._("httpin.errors.json-error")); }
}
}
node.send(msg);
node.status({});
});