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

Merge pull request #1586 from node-red-hitachi/fix-file-in-parts

fix behavior of "a msg per line" mode of FILE IN node with empty line
This commit is contained in:
Nick O'Leary 2018-01-29 11:14:07 +00:00 committed by GitHub
commit 5e462f0f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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} parts:{index:count, ch:ch, type:type, id:msg._msgid}
} }
count += 1; count += 1;
if ((chunk.length < hwm) && (bits[i+1].length === 0)) {
m.parts.count = count;
}
node.send(m); node.send(m);
} }
spare = bits[i]; spare = bits[i];
if (chunk.length !== hwm) { getout = false; }
//console.log("LEFT",bits[i].length,bits[i]);
} }
if (node.format === "stream") { if (node.format === "stream") {
var m = { var m = {
@ -233,6 +228,18 @@ module.exports = function(RED) {
else { msg.payload = lines; } else { msg.payload = lines; }
node.send(msg); 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. 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} }; var m = { parts:{index:count, count:count, ch:ch, type:type, id:msg._msgid} };
node.send(m); node.send(m);

View File

@ -542,21 +542,64 @@ describe('file Nodes', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
var c = 0; var c = 0;
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('payload'); msg.should.have.property('payload');
msg.payload.should.be.a.String(); 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.should.have.property('parts');
msg.parts.should.have.property('index',1); msg.parts.should.have.property('index',c);
msg.parts.should.have.property('count',2);
msg.parts.should.have.property('type','string'); msg.parts.should.have.property('type','string');
msg.parts.should.have.property('ch','\n'); 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(); 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:""}); n1.receive({payload:""});
}); });