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