revelrylabs / elixir-nodejs

An Elixir API for calling Node.js functions
MIT License
214 stars 31 forks source link

Broken encoding #72

Closed utopos closed 3 months ago

utopos commented 2 years ago

Hi!

I have been trying the library (which is fantastic!) but I'm experiencing issues with characters encoding. If the return string from the called JS function contains UTF-8 characters, the return on Elixir side will be a binary.

Please, allow me to illustrate with en example with a letter "Ł":

Javascript:

module.exports = function (text){
    return text
}

Elixir:

iex> {:ok, x} = NodeJS.call("test",["hełło"])
iex> String.codepoints(x)
["h", "e", "Å", <<194, 130>>, "Å", <<194, 130>>, "o"]

Thank you!

29decibel commented 1 year ago

Created a PR here https://github.com/revelrylabs/elixir-nodejs/pull/77

Nicolab commented 7 months ago

From the README:

In order to cope with Unicode character it is necessary to specify the binary option:

NodeJS.call("echo", ["’"], binary: true) # => {:ok, "’"}

grossvogel commented 3 months ago

Hey, @29decibel thanks for filing the issue and PR, and sorry it's been sitting here so long without attention. If you're still interested in moving this forward, I'd be interested in your take on a couple of things:

If the latter is the case, then it seems like something that'd make the library simpler and easier to use, though we'd want to be careful not to break other use cases that are currently working fine.

29decibel commented 3 months ago

Thank you @grossvogel ! I missed this option then, I think we can close this PR. :-)

grossvogel commented 3 months ago

@utopos I'm going to close this under the understanding that the binary: true option solves your needs. If that's not the case, please let me know!