The problem with multi-byte character when reading library

Hello.

A problem of Node-RED@1.0.3 has been reported that multibyte character strings are garbled in the import library function.
It has already been issued, but it has been raised here again.

How to reproduce

  1. Load a sample flow from a file.
    (Currently not garbled.)
  2. If you export to the library and import it again, it will be garbled.

Proposed changes

this is because the library file is read separately and cannot be encoded well.

function getFileBody(root,path) { 
     var body = ""; 
     var fn = fspath.join(root,path); 
     var fd = fs.openSync(fn,"r"); 
     var size = fs.fstatSync(fd).size; 
     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(); 
         } 
     } 
     fs.closeSync(fd); 
     return body; 
 } 

Encoding errors do not occur when files are read in batch.

function getFileBody(root,path) {
    var body = "";
    var fn = fspath.join(root,path);
    var data = fs.readFileSync(fn,'utf8');
    var parts = data.split("\n");

    parts.forEach((part) => {
        if (! /^\/\/ \w+: /.test(part)) {
            body += (body.length > 0?"\n":"")+ part;
        }
    });
    return body;
}

This issue was Pull Requested #2401

Hi @okhiroyuki

I'm just checking - are you just sharing the issue and the pr with the forum, or are you reporting a different problem?

Hi @knolleary.
I just shared the issue and the pr with the forum.