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

convert to buffer before write

This commit is contained in:
Hiroyasu Nishiyama 2019-02-07 22:46:21 +09:00
parent 1db1ec7b5e
commit a713c92530

View File

@ -75,15 +75,16 @@ 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; }
var buf = Buffer.from(data);
if (node.overwriteFile === "true") { if (node.overwriteFile === "true") {
var wstream = fs.createWriteStream(filename, { encoding:'utf8', flags:'w', autoClose:true }); var wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true });
node.wstream = wstream; node.wstream = wstream;
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(); done();
}); });
wstream.on("open", function() { wstream.on("open", function() {
wstream.end(data, function() { wstream.end(buf, function() {
node.send(msg); node.send(msg);
done(); done();
}); });
@ -117,7 +118,7 @@ module.exports = function(RED) {
} }
} }
if (recreateStream) { if (recreateStream) {
node.wstream = fs.createWriteStream(filename, { encoding:'utf8', flags:'a', autoClose:true }); node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'a', autoClose:true });
node.wstream.on("open", function(fd) { node.wstream.on("open", function(fd) {
try { try {
var stat = fs.statSync(filename); var stat = fs.statSync(filename);
@ -132,13 +133,13 @@ module.exports = function(RED) {
} }
if (node.filename) { if (node.filename) {
// Static filename - write and reuse the stream next time // Static filename - write and reuse the stream next time
node.wstream.write(data, function() { node.wstream.write(buf, function() {
node.send(msg); node.send(msg);
done(); done();
}); });
} else { } else {
// Dynamic filename - write and close the stream // Dynamic filename - write and close the stream
node.wstream.end(data, function() { node.wstream.end(buf, function() {
node.send(msg); node.send(msg);
delete node.wstream; delete node.wstream;
delete node.wstreamIno; delete node.wstreamIno;