espruino / EspruinoTools

JavaScript library of tools for Espruino - used for the Web IDE, CLI, etc.
Apache License 2.0
150 stars 89 forks source link

fileSaveDialog changes binary #135

Closed notEvil closed 3 years ago

notEvil commented 3 years ago

Hi,

when I download a binary file in the Chrome web IDE, the target files content doesn't match the source. When I view the source file in the IDE it shows correctly. So I tracked down the issue and found that when fileSaveDialog puts data into a Blob, it gets reinterpreted (a 10 character string suddenly had Blob length 17). When I changed the part to behave like the non-Chrome part with

          var rawdata = new Uint8Array(data.length);
          for (var i=0;i<data.length;i++) rawdata[i]=data.charCodeAt(i);
          var blob = new Blob([rawdata.buffer],{ type: "text/plain"} );

it worked.

gfwilliams commented 3 years ago

Thanks for this! So you're saying specifically to change this line? https://github.com/espruino/EspruinoTools/blob/master/core/utils.js#L592

I guess probably pull the common Blob code right to the top of the function?

notEvil commented 3 years ago

Yes, I reproduced the issue with var st=require('Storage');st.erase('t');st.write('t','\1A\x9D\xC2\xC2\x88\xCB\0F\0'); (source 10bytes, downloaded 15bytes). And it only appears in the Chrome app, not in Chrome web.

I'm not familiar with js enough to suggest a more lightweight fix than this.

gfwilliams commented 3 years ago

Thanks! Just added a fix