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

Fix problem with multi-byte character (#1391)

This commit is contained in:
HirokiUchikawa 2017-09-17 16:46:14 +09:00 committed by Nick O'Leary
parent 85392496e7
commit ff40b521b7

View File

@ -45,62 +45,64 @@ function getFileMeta(root,path) {
var meta = {}; var meta = {};
var read = 0; var read = 0;
var length = 10; var length = 10;
var remaining = ""; var remaining = Buffer(0);
var buffer = Buffer(length); var buffer = Buffer(length);
var idx = -1;
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"); while((idx = data.indexOf("\n")) != -1){
remaining = parts.splice(-1); var part = data.slice(0,idx+1);
for (var i=0;i<parts.length;i+=1) { var match = /^\/\/ (\w+): (.*)/.exec(part.toString());
var match = /^\/\/ (\w+): (.*)/.exec(parts[i]);
if (match) { if (match) {
meta[match[1]] = match[2]; meta[match[1]] = match[2];
} else { } else {
read = size; read = size;
break; break;
} }
data = data.slice(idx+1);
} }
remaining = data;
} }
fs.closeSync(fd); fs.closeSync(fd);
return meta; return meta;
} }
function getFileBody(root,path) { function getFileBody(root,path) {
var body = ""; var body = Buffer(0);
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 scanning = true; var scanning = true;
var read = 0; var read = 0;
var length = 50; var length = 50;
var remaining = ""; var remaining = Buffer(0);
var buffer = Buffer(length); var buffer = Buffer(length);
var idx = -1;
while(read < size) { while(read < size) {
var thisRead = fs.readSync(fd,buffer,0,length); var thisRead = fs.readSync(fd,buffer,0,length);
read += thisRead; read += thisRead;
if (scanning) { if (scanning) {
var data = remaining+buffer.slice(0,thisRead).toString(); var data = Buffer.concat([remaining,buffer.slice(0,thisRead)]);
var parts = data.split("\n"); while((idx = data.indexOf("\n")) != -1){
remaining = parts.splice(-1)[0]; var part = data.slice(0,idx+1);
for (var i=0;i<parts.length;i+=1) { if (! /^\/\/ \w+: /.test(part.toString())) {
if (! /^\/\/ \w+: /.test(parts[i])) {
scanning = false; scanning = false;
body += parts[i]+"\n"; body = Buffer.concat([body,data]);
break;
} }
data = data.slice(idx+1);
} }
if (! /^\/\/ \w+: /.test(remaining)) { remaining = data;
scanning = false; if (scanning && read >= size) {
} body = Buffer.concat([body,remaining]);
if (!scanning) {
body += remaining;
} }
} else { } else {
body += buffer.slice(0,thisRead).toString(); body = Buffer.concat([body,buffer.slice(0,thisRead)]);
} }
} }
fs.closeSync(fd); fs.closeSync(fd);
return body; return body.toString();
} }
/** /**