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

update close & input handling of File node

This commit is contained in:
Hiroyasu Nishiyama 2018-10-03 21:29:28 +09:00
parent 58c8311d56
commit 1441042458

View File

@ -28,10 +28,9 @@ module.exports = function(RED) {
this.createDir = n.createDir || false; this.createDir = n.createDir || false;
var node = this; var node = this;
node.wstream = null; node.wstream = null;
node.data = [];
node.msgQueue = []; node.msgQueue = [];
node.closing = false; node.closing = false;
node.closeCallbacks = []; node.closeCallback = null;
function processMsg(msg, done) { function processMsg(msg, done) {
var filename = node.filename || msg.filename || ""; var filename = node.filename || msg.filename || "";
@ -76,22 +75,20 @@ module.exports = function(RED) {
if (typeof data === "boolean") { data = data.toString(); } if (typeof data === "boolean") { data = data.toString(); }
if (typeof data === "number") { data = data.toString(); } if (typeof data === "number") { data = data.toString(); }
if ((node.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; } if ((node.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; }
node.data.push({msg:msg,data:Buffer.from(data)});
while (node.data.length > 0) {
if (node.overwriteFile === "true") { if (node.overwriteFile === "true") {
(function(packet) { var wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true });
node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true }); node.wstream = wstream;
node.wstream.on("error", function(err) { wstream.on("error", function(err) {
node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); node.error(RED._("file.errors.writefail",{error:err.toString()}),msg);
done();
}); });
node.wstream.on("open", function() { wstream.on("open", function() {
node.wstream.end(packet.data, function() { wstream.end(data, function() {
node.send(packet.msg); node.send(msg);
done(); done();
}); });
}) })
})(node.data.shift()); return;
} }
else { else {
// Append mode // Append mode
@ -130,21 +127,19 @@ module.exports = function(RED) {
}); });
node.wstream.on("error", function(err) { node.wstream.on("error", function(err) {
node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg);
done();
}); });
} }
if (node.filename) { if (node.filename) {
// Static filename - write and reuse the stream next time // Static filename - write and reuse the stream next time
var packet = node.data.shift() node.wstream.write(data, function() {
node.wstream.write(packet.data, function() { node.send(msg);
node.send(packet.msg);
done(); done();
}); });
} else { } else {
// Dynamic filename - write and close the stream // Dynamic filename - write and close the stream
var packet = node.data.shift() node.wstream.end(data, function() {
node.wstream.end(packet.data, function() { node.send(msg);
node.send(packet.msg);
delete node.wstream; delete node.wstream;
delete node.wstreamIno; delete node.wstreamIno;
done(); done();
@ -152,6 +147,8 @@ module.exports = function(RED) {
} }
} }
} }
else {
done();
} }
} }
@ -162,6 +159,9 @@ module.exports = function(RED) {
if (queue.length > 0) { if (queue.length > 0) {
processQ(queue); processQ(queue);
} }
else if (node.closing) {
closeNode();
}
}); });
} }
@ -171,33 +171,39 @@ module.exports = function(RED) {
// pending write exists // pending write exists
return; return;
} }
try {
processQ(msgQueue); processQ(msgQueue);
}
catch (e) {
node.msgQueue = [];
if (node.closing) { if (node.closing) {
closeNode(); closeNode();
} }
throw e;
}
}); });
function closeNode() { function closeNode() {
if (node.wstream) { node.wstream.end(); } if (node.wstream) { node.wstream.end(); }
if (node.tout) { clearTimeout(node.tout); } if (node.tout) { clearTimeout(node.tout); }
node.status({}); node.status({});
var callbacks = node.closeCallbacks; var cb = node.closeCallback;
node.closeCallbacks = []; node.closeCallback = null;
node.closing = false; node.closing = false;
for (cb in callbacks) { if (cb) {
cb(); cb();
} }
} }
this.on('close', function(cb) { this.on('close', function(done) {
if (cb) {
node.closeCallbacks.push(done);
}
if (node.closing) { if (node.closing) {
// already closing // already closing
return; return;
} }
node.closing = true; node.closing = true;
if (done) {
node.closeCallback = done;
}
if (node.msgQueue.length > 0) { if (node.msgQueue.length > 0) {
// close after queue processed // close after queue processed
return; return;