mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Fix garbled characters in library (#2457)
* update getFileBody * add suitable unit tests Co-authored-by: Hiroyuki Okada <ok.okada.hiroyuki@gmail.com>
This commit is contained in:
parent
634a51635c
commit
5c199d3bb4
@ -25,22 +25,22 @@ var settings;
|
|||||||
var libDir;
|
var libDir;
|
||||||
var libFlowsDir;
|
var libFlowsDir;
|
||||||
|
|
||||||
|
|
||||||
function getFileMeta(root, path) {
|
function getFileMeta(root, path) {
|
||||||
var fn = fspath.join(root, path);
|
var fn = fspath.join(root, path);
|
||||||
var fd = fs.openSync(fn,"r");
|
var fd = fs.openSync(fn, 'r');
|
||||||
var size = fs.fstatSync(fd).size;
|
var size = fs.fstatSync(fd).size;
|
||||||
var meta = {};
|
var meta = {};
|
||||||
var read = 0;
|
var read = 0;
|
||||||
var length = 10;
|
var length = 10;
|
||||||
var remaining = "";
|
var remaining = Buffer.alloc(0);
|
||||||
var buffer = Buffer.alloc(length);
|
var buffer = Buffer.alloc(length);
|
||||||
while (read < size) {
|
while (read < size) {
|
||||||
read += fs.readSync(fd, buffer, 0, length);
|
read += fs.readSync(fd, buffer, 0, length);
|
||||||
var data = remaining+buffer.toString();
|
var data = Buffer.concat([remaining, buffer]);
|
||||||
var parts = data.split("\n");
|
var index = data.lastIndexOf(0x0a);
|
||||||
remaining = parts.splice(-1);
|
if (index !== -1) {
|
||||||
for (var i=0;i<parts.length;i+=1) {
|
var parts = data.slice(0, index).toString().split('\n');
|
||||||
|
for (var i = 0; i < parts.length; i++) {
|
||||||
var match = /^\/\/ (\w+): (.*)/.exec(parts[i]);
|
var match = /^\/\/ (\w+): (.*)/.exec(parts[i]);
|
||||||
if (match) {
|
if (match) {
|
||||||
meta[match[1]] = match[2];
|
meta[match[1]] = match[2];
|
||||||
@ -49,49 +49,30 @@ function getFileMeta(root,path) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
remaining = data.slice(index + 1);
|
||||||
|
} else {
|
||||||
|
remaining = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fs.closeSync(fd);
|
fs.closeSync(fd);
|
||||||
return meta;
|
return meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileBody(root, path) {
|
function getFileBody(root, path) {
|
||||||
var body = "";
|
var body = '';
|
||||||
var fn = fspath.join(root, path);
|
var fn = fspath.join(root, path);
|
||||||
var fd = fs.openSync(fn,"r");
|
var data = fs.readFileSync(fn, 'utf8');
|
||||||
var size = fs.fstatSync(fd).size;
|
var parts = data.split('\n');
|
||||||
var scanning = true;
|
var scanning = true;
|
||||||
var read = 0;
|
for (var i = 0; i < parts.length; i++) {
|
||||||
var length = 50;
|
if (! /^\/\/ \w+: /.test(parts[i]) || !scanning) {
|
||||||
var remaining = "";
|
body += (body.length > 0 ? '\n' : '') + parts[i];
|
||||||
var buffer = Buffer.alloc(length);
|
|
||||||
while(read < size) {
|
|
||||||
var thisRead = fs.readSync(fd,buffer,0,length);
|
|
||||||
if (scanning) {
|
|
||||||
var data = remaining+buffer.slice(0,thisRead).toString();
|
|
||||||
read += thisRead;
|
|
||||||
var parts = data.split("\n");
|
|
||||||
if (read < size) {
|
|
||||||
remaining = parts.splice(-1)[0];
|
|
||||||
} else {
|
|
||||||
remaining = "";
|
|
||||||
}
|
|
||||||
for (var i=0;i<parts.length;i+=1) {
|
|
||||||
if (! /^\/\/ \w+: /.test(parts[i])) {
|
|
||||||
scanning = false;
|
scanning = false;
|
||||||
body += (body.length > 0?"\n":"")+parts[i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!scanning) {
|
|
||||||
body += remaining;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
read += thisRead;
|
|
||||||
body += buffer.slice(0,thisRead).toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fs.closeSync(fd);
|
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLibraryEntry(type,path) {
|
function getLibraryEntry(type,path) {
|
||||||
var root = fspath.join(libDir,type);
|
var root = fspath.join(libDir,type);
|
||||||
var rootPath = fspath.join(libDir,type,path);
|
var rootPath = fspath.join(libDir,type,path);
|
||||||
|
@ -81,9 +81,11 @@ describe('storage/localfilesystem/library', function() {
|
|||||||
fs.mkdirSync(path.join(objLib, "A"));
|
fs.mkdirSync(path.join(objLib, "A"));
|
||||||
fs.mkdirSync(path.join(objLib, "B"));
|
fs.mkdirSync(path.join(objLib, "B"));
|
||||||
fs.mkdirSync(path.join(objLib, "B", "C"));
|
fs.mkdirSync(path.join(objLib, "B", "C"));
|
||||||
|
fs.mkdirSync(path.join(objLib, "D"));
|
||||||
if (type === "functions" || type === "object") {
|
if (type === "functions" || type === "object") {
|
||||||
fs.writeFileSync(path.join(objLib, "file1.js"), "// abc: def\n// not a metaline \n\n Hi", 'utf8');
|
fs.writeFileSync(path.join(objLib, "file1.js"), "// abc: def\n// not a metaline \n\n Hi", 'utf8');
|
||||||
fs.writeFileSync(path.join(objLib, "B", "file2.js"), "// ghi: jkl\n// not a metaline \n\n Hi", 'utf8');
|
fs.writeFileSync(path.join(objLib, "B", "file2.js"), "// ghi: jkl\n// not a metaline \n\n Hi", 'utf8');
|
||||||
|
fs.writeFileSync(path.join(objLib, "D", "file3.js"), "// mno: 日本語テスト\n\nこんにちわ", 'utf8');
|
||||||
}
|
}
|
||||||
if (type === "flows" || type === "object") {
|
if (type === "flows" || type === "object") {
|
||||||
fs.writeFileSync(path.join(objLib, "B", "flow.json"), "Hi", 'utf8');
|
fs.writeFileSync(path.join(objLib, "B", "flow.json"), "Hi", 'utf8');
|
||||||
@ -95,11 +97,13 @@ describe('storage/localfilesystem/library', function() {
|
|||||||
createObjectLibrary();
|
createObjectLibrary();
|
||||||
|
|
||||||
localfilesystemLibrary.getLibraryEntry('object', '').then(function (flows) {
|
localfilesystemLibrary.getLibraryEntry('object', '').then(function (flows) {
|
||||||
flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
|
flows.should.eql([ 'A', 'B', 'D', { abc: 'def', fn: 'file1.js' }]);
|
||||||
localfilesystemLibrary.getLibraryEntry('object', 'B').then(function (flows) {
|
localfilesystemLibrary.getLibraryEntry('object', 'B').then(function (flows) {
|
||||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' }, { fn: 'flow.json' }]);
|
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' }, { fn: 'flow.json' }]);
|
||||||
localfilesystemLibrary.getLibraryEntry('object', 'B/C').then(function (flows) {
|
localfilesystemLibrary.getLibraryEntry('object', 'B/C').then(function (flows) {
|
||||||
flows.should.eql([]);
|
flows.should.eql([]);
|
||||||
|
localfilesystemLibrary.getLibraryEntry('object', 'D').then(function (flows) {
|
||||||
|
flows.should.eql([{ mno: '日本語テスト', fn: 'file3.js' }]);
|
||||||
done();
|
done();
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
done(err);
|
done(err);
|
||||||
@ -113,6 +117,9 @@ describe('storage/localfilesystem/library', function() {
|
|||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
|
}).catch(function (err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load a flow library object with .json unspecified', function(done) {
|
it('should load a flow library object with .json unspecified', function(done) {
|
||||||
@ -203,4 +210,35 @@ describe('storage/localfilesystem/library', function() {
|
|||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return a newly saved library flow (multi-byte character)',function(done) {
|
||||||
|
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||||
|
createObjectLibrary("flows");
|
||||||
|
localfilesystemLibrary.getLibraryEntry('flows','B').then(function(flows) {
|
||||||
|
flows.should.eql([ 'C', {fn:'flow.json'} ]);
|
||||||
|
var ft = path.join("B","D","file4");
|
||||||
|
localfilesystemLibrary.saveLibraryEntry('flows',ft,{mno:'pqr'},"こんにちわこんにちわこんにちわ").then(function() {
|
||||||
|
setTimeout(function() {
|
||||||
|
localfilesystemLibrary.getLibraryEntry('flows',path.join("B","D")).then(function(flows) {
|
||||||
|
flows.should.eql([ { mno: 'pqr', fn: 'file4.json' } ]);
|
||||||
|
localfilesystemLibrary.getLibraryEntry('flows',ft+".json").then(function(body) {
|
||||||
|
body.should.eql("こんにちわこんにちわこんにちわ");
|
||||||
|
done();
|
||||||
|
}).catch(function(err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
}).catch(function(err) {
|
||||||
|
done(err);
|
||||||
|
})
|
||||||
|
}, 50);
|
||||||
|
}).catch(function(err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
}).catch(function(err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
}).catch(function(err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user