From 1db1ec7b5ee414c358cb86138b01fbfa7af9251f Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Wed, 6 Feb 2019 21:53:23 +0900 Subject: [PATCH 1/3] fix encoding of file node from binary to utf8 --- nodes/core/storage/50-file.js | 4 ++-- test/nodes/core/storage/50-file_spec.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/nodes/core/storage/50-file.js b/nodes/core/storage/50-file.js index 936e2f84f..04805af06 100644 --- a/nodes/core/storage/50-file.js +++ b/nodes/core/storage/50-file.js @@ -76,7 +76,7 @@ module.exports = function(RED) { if (typeof data === "number") { data = data.toString(); } if ((node.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; } if (node.overwriteFile === "true") { - var wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true }); + var wstream = fs.createWriteStream(filename, { encoding:'utf8', flags:'w', autoClose:true }); node.wstream = wstream; wstream.on("error", function(err) { node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); @@ -117,7 +117,7 @@ module.exports = function(RED) { } } if (recreateStream) { - node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'a', autoClose:true }); + node.wstream = fs.createWriteStream(filename, { encoding:'utf8', flags:'a', autoClose:true }); node.wstream.on("open", function(fd) { try { var stat = fs.statSync(filename); diff --git a/test/nodes/core/storage/50-file_spec.js b/test/nodes/core/storage/50-file_spec.js index b60933649..cf6491c42 100644 --- a/test/nodes/core/storage/50-file_spec.js +++ b/test/nodes/core/storage/50-file_spec.js @@ -79,6 +79,29 @@ describe('file Nodes', function() { }); }); + it('should write multi-byte string to a file', function(done) { + var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":true, wires: [["helperNode1"]]}, + {id:"helperNode1", type:"helper"}]; + helper.load(fileNode, flow, function() { + var n1 = helper.getNode("fileNode1"); + var n2 = helper.getNode("helperNode1"); + n2.on("input", function(msg) { + try { + var f = fs.readFileSync(fileToTest).toString(); + f.should.have.length(2); + f.should.equal("試験"); + fs.unlinkSync(fileToTest); + msg.should.have.property("payload", "試験"); + done(); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"試験"}); + }); + }); + it('should append to a file and add newline', function(done) { var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false, wires: [["helperNode1"]]}, {id:"helperNode1", type:"helper"}]; From a713c925304cff722bb42f6896b3f122263bc61e Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Thu, 7 Feb 2019 22:46:21 +0900 Subject: [PATCH 2/3] convert to buffer before write --- nodes/core/storage/50-file.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/nodes/core/storage/50-file.js b/nodes/core/storage/50-file.js index 04805af06..b9659b2cf 100644 --- a/nodes/core/storage/50-file.js +++ b/nodes/core/storage/50-file.js @@ -75,15 +75,16 @@ 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; } + var buf = Buffer.from(data); 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; wstream.on("error", function(err) { node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); done(); }); wstream.on("open", function() { - wstream.end(data, function() { + wstream.end(buf, function() { node.send(msg); done(); }); @@ -117,7 +118,7 @@ module.exports = function(RED) { } } 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) { try { var stat = fs.statSync(filename); @@ -132,13 +133,13 @@ module.exports = function(RED) { } if (node.filename) { // Static filename - write and reuse the stream next time - node.wstream.write(data, function() { + node.wstream.write(buf, function() { node.send(msg); done(); }); } else { // Dynamic filename - write and close the stream - node.wstream.end(data, function() { + node.wstream.end(buf, function() { node.send(msg); delete node.wstream; delete node.wstreamIno; From 50d7e163656ad5c451009c382416ea4fed83cd6f Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 25 Feb 2019 14:40:08 +0000 Subject: [PATCH 3/3] Bump for 0.19.6 --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f74ec2bdf..a59004153 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +#### 0.19.6: Maintenance Release + + - Fix encoding of file node from binary to utf8 - #2051 + #### 0.19.5: Maintenance Release - Recognize pip installs of RPi.GPIO (#1934) diff --git a/package.json b/package.json index 86f182d72..44ef285c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red", - "version": "0.19.5", + "version": "0.19.6", "description": "A visual tool for wiring the Internet of Things", "homepage": "http://nodered.org", "license": "Apache-2.0",