# The problem with multi-byte character when reading library

**URL:** https://discourse.nodered.org/t/the-problem-with-multi-byte-character-when-reading-library/19092
**Category:** General
**Created:** [12 December 2019 23:51 UTC](https://discourse.nodered.org/t/the-problem-with-multi-byte-character-when-reading-library/19092 "2019-12-12T23:51:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![okhiroyuki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/okhiroyuki/32/15056_2.png) [@okhiroyuki](https://discourse.nodered.org/u/okhiroyuki)
#### Post date: [12 December 2019 23:51 UTC](https://discourse.nodered.org/t/the-problem-with-multi-byte-character-when-reading-library/19092/1 "2019-12-12T23:51:27Z")

</div>

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.

> <https://github.com/node-red/node-red/issues/2391>
>
> What are the steps to reproduce?
> sample flow
> https://gist.github.com/utaani/52966e681e3b7911d0238a191c919299
> First load the sample flow from the file.
> (Currently, it is not garbled.)
> After that, if you...

## 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.

 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1beae1fd3b36bb179a6241de3f4e5fe7ae1fee1c.png)

## Proposed changes

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

```auto
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.

```auto
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](https://github.com/node-red/node-red/pull/2401)

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 December 2019 00:08 UTC](https://discourse.nodered.org/t/the-problem-with-multi-byte-character-when-reading-library/19092/2 "2019-12-13T00:08:55Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![okhiroyuki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/okhiroyuki/32/15056_2.png) [@okhiroyuki](https://discourse.nodered.org/u/okhiroyuki)
#### Post date: [13 December 2019 00:13 UTC](https://discourse.nodered.org/t/the-problem-with-multi-byte-character-when-reading-library/19092/3 "2019-12-13T00:13:43Z")

</div>

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