webtorrent/node-bencode

Incorrect encoding of Buffer

ngorchilov opened this issue · 4 comments

This code produces invald result on encoding binary data (like info/pieces section of torrent file) as default type casting to string is assuming utf-8 encoding
if( data instanceof Buffer )
return data.length + ':' + data
The correct way is to specify 'binary' encoding explicitly. I.e.
if( data instanceof Buffer )
return data.length + ':' + data.toString('binary')

Yup, you're wright, but the binary encoding method is deprecated and shouldn't be used, as stated in the docs:

This encoding method is deprecated and should be avoided in favor of Buffer objects where possible.
This encoding will be removed in future versions of Node.

I'll have a look at this and add a test case for a real life torrent file.

Then, the only option IMHO is bencode to produce a Buffer instead of string.

I think producing a Buffer when encoding is absolutely okay. It feels more correct than a string.
But do we need to worry about breaking the API?


  • I guess we can close this now, has been fixed in b746034 - sent via Hojoki