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

fix behavior of msg per line mode of FILE IN node with empty line

This commit is contained in:
Hiroyasu Nishiyama 2018-01-28 21:27:15 +09:00
parent 15c3cc60f6
commit 34e46fc6d3
2 changed files with 66 additions and 16 deletions

View File

@ -189,14 +189,9 @@ module.exports = function(RED) {
parts:{index:count, ch:ch, type:type, id:msg._msgid}
}
count += 1;
if ((chunk.length < hwm) && (bits[i+1].length === 0)) {
m.parts.count = count;
}
node.send(m);
}
spare = bits[i];
if (chunk.length !== hwm) { getout = false; }
//console.log("LEFT",bits[i].length,bits[i]);
}
if (node.format === "stream") {
var m = {
@ -233,6 +228,18 @@ module.exports = function(RED) {
else { msg.payload = lines; }
node.send(msg);
}
else if (node.format === "lines") {
var m = { payload: spare,
parts: {
index: count,
count: count+1,
ch: ch,
type: type,
id: msg._msgid
}
};
node.send(m);
}
else if (getout) { // last chunk same size as high water mark - have to send empty extra packet.
var m = { parts:{index:count, count:count, ch:ch, type:type, id:msg._msgid} };
node.send(m);

View File

@ -542,21 +542,64 @@ describe('file Nodes', function() {
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
try {
msg.should.have.property('payload');
msg.payload.should.be.a.String();
msg.payload.should.have.length(19);
if (c === 0) {
msg.payload.should.equal("File message line 1");
c++;
} else {
msg.payload.should.equal("File message line 2");
msg.should.have.property('parts');
msg.parts.should.have.property('index',1);
msg.parts.should.have.property('count',2);
msg.parts.should.have.property('index',c);
msg.parts.should.have.property('type','string');
msg.parts.should.have.property('ch','\n');
if (c === 0) {
msg.payload.should.equal("File message line 1");
}
if (c === 1) {
msg.payload.should.equal("File message line 2");
}
if (c === 2) {
msg.payload.should.equal("");
done();
}
c++;
}
catch(e) {
done(e);
}
});
n1.receive({payload:""});
});
});
it('should read in a file with empty line and output split lines with parts', function(done) {
var data = ["-", "", "-", ""];
var line = data.join("\n");
fs.writeFileSync(fileToTest, line);
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", filename:fileToTest, format:"lines", wires:[["n2"]]},
{id:"n2", type:"helper"}];
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileInNode1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
try {
msg.should.have.property('payload');
msg.payload.should.equal(data[c]);
msg.should.have.property('parts');
var parts = msg.parts;
parts.should.have.property('index',c);
parts.should.have.property('type','string');
parts.should.have.property('ch','\n');
c++;
if (c === data.length) {
parts.should.have.property('count', data.length);
done();
}
else {
parts.should.not.have.property('count');
}
}
catch(e) {
done(e);
}
});
n1.receive({payload:""});
});