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,73 +25,54 @@ var settings;
|
||||
var libDir;
|
||||
var libFlowsDir;
|
||||
|
||||
|
||||
function getFileMeta(root,path) {
|
||||
var fn = fspath.join(root,path);
|
||||
var fd = fs.openSync(fn,"r");
|
||||
function getFileMeta(root, path) {
|
||||
var fn = fspath.join(root, path);
|
||||
var fd = fs.openSync(fn, 'r');
|
||||
var size = fs.fstatSync(fd).size;
|
||||
var meta = {};
|
||||
var read = 0;
|
||||
var length = 10;
|
||||
var remaining = "";
|
||||
var remaining = Buffer.alloc(0);
|
||||
var buffer = Buffer.alloc(length);
|
||||
while(read < size) {
|
||||
read+=fs.readSync(fd,buffer,0,length);
|
||||
var data = remaining+buffer.toString();
|
||||
var parts = data.split("\n");
|
||||
remaining = parts.splice(-1);
|
||||
for (var i=0;i<parts.length;i+=1) {
|
||||
var match = /^\/\/ (\w+): (.*)/.exec(parts[i]);
|
||||
if (match) {
|
||||
meta[match[1]] = match[2];
|
||||
} else {
|
||||
read = size;
|
||||
break;
|
||||
while (read < size) {
|
||||
read += fs.readSync(fd, buffer, 0, length);
|
||||
var data = Buffer.concat([remaining, buffer]);
|
||||
var index = data.lastIndexOf(0x0a);
|
||||
if (index !== -1) {
|
||||
var parts = data.slice(0, index).toString().split('\n');
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
var match = /^\/\/ (\w+): (.*)/.exec(parts[i]);
|
||||
if (match) {
|
||||
meta[match[1]] = match[2];
|
||||
} else {
|
||||
read = size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
remaining = data.slice(index + 1);
|
||||
} else {
|
||||
remaining = data;
|
||||
}
|
||||
}
|
||||
fs.closeSync(fd);
|
||||
return meta;
|
||||
}
|
||||
|
||||
function getFileBody(root,path) {
|
||||
var body = "";
|
||||
var fn = fspath.join(root,path);
|
||||
var fd = fs.openSync(fn,"r");
|
||||
var size = fs.fstatSync(fd).size;
|
||||
function getFileBody(root, path) {
|
||||
var body = '';
|
||||
var fn = fspath.join(root, path);
|
||||
var data = fs.readFileSync(fn, 'utf8');
|
||||
var parts = data.split('\n');
|
||||
var scanning = true;
|
||||
var read = 0;
|
||||
var length = 50;
|
||||
var remaining = "";
|
||||
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;
|
||||
body += (body.length > 0?"\n":"")+parts[i];
|
||||
}
|
||||
}
|
||||
if (!scanning) {
|
||||
body += remaining;
|
||||
}
|
||||
} else {
|
||||
read += thisRead;
|
||||
body += buffer.slice(0,thisRead).toString();
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
if (! /^\/\/ \w+: /.test(parts[i]) || !scanning) {
|
||||
body += (body.length > 0 ? '\n' : '') + parts[i];
|
||||
scanning = false;
|
||||
}
|
||||
}
|
||||
fs.closeSync(fd);
|
||||
return body;
|
||||
}
|
||||
|
||||
function getLibraryEntry(type,path) {
|
||||
var root = fspath.join(libDir,type);
|
||||
var rootPath = fspath.join(libDir,type,path);
|
||||
|
@ -72,45 +72,52 @@ describe('storage/localfilesystem/library', function() {
|
||||
});
|
||||
|
||||
function createObjectLibrary(type) {
|
||||
type = type ||"object";
|
||||
var objLib = path.join(userDir,"lib",type);
|
||||
type = type || "object";
|
||||
var objLib = path.join(userDir, "lib", type);
|
||||
try {
|
||||
fs.mkdirSync(objLib);
|
||||
} catch(err) {
|
||||
} catch (err) {
|
||||
}
|
||||
fs.mkdirSync(path.join(objLib,"A"));
|
||||
fs.mkdirSync(path.join(objLib,"B"));
|
||||
fs.mkdirSync(path.join(objLib,"B","C"));
|
||||
fs.mkdirSync(path.join(objLib, "A"));
|
||||
fs.mkdirSync(path.join(objLib, "B"));
|
||||
fs.mkdirSync(path.join(objLib, "B", "C"));
|
||||
fs.mkdirSync(path.join(objLib, "D"));
|
||||
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,"B","file2.js"),"// ghi: jkl\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, "D", "file3.js"), "// mno: 日本語テスト\n\nこんにちわ", 'utf8');
|
||||
}
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
it('should return a directory listing of library objects',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
it('should return a directory listing of library objects', function (done) {
|
||||
localfilesystemLibrary.init({userDir: userDir}).then(function () {
|
||||
createObjectLibrary();
|
||||
|
||||
localfilesystemLibrary.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('object','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' }, { fn: 'flow.json' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('object','B/C').then(function(flows) {
|
||||
localfilesystemLibrary.getLibraryEntry('object', '').then(function (flows) {
|
||||
flows.should.eql([ 'A', 'B', 'D', { abc: 'def', fn: 'file1.js' }]);
|
||||
localfilesystemLibrary.getLibraryEntry('object', 'B').then(function (flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' }, { fn: 'flow.json' }]);
|
||||
localfilesystemLibrary.getLibraryEntry('object', 'B/C').then(function (flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
localfilesystemLibrary.getLibraryEntry('object', 'D').then(function (flows) {
|
||||
flows.should.eql([{ mno: '日本語テスト', fn: 'file3.js' }]);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function (err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
}).catch(function (err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
}).catch(function (err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
}).catch(function (err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
@ -203,4 +210,35 @@ describe('storage/localfilesystem/library', function() {
|
||||
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