Giter Site home page Giter Site logo

bncode's People

Contributors

a2800276 avatar clarkf avatar fent avatar feross avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bncode's Issues

It sorts dictionary entries incorrectly.

Bug

bncode/bncode.js

Lines 370 to 372 in 704e8d5

function encodeDict (obj) {
var func = function (obj, pos) {
var keys = Object.keys(obj).sort()

     var keys = Object.keys(obj).sort()

This is not the correct way of sorting dictionary entries. The encoder is producing specification-incompliant octets that’ll be floating around in the wild, the network.


When you say “strings” in the context of Bencoding, you mean “binary strings,” or more specifically, “8-bit byte sequences.”

BEP 52 — The BitTorrent protocol specification version 2

Note that, in the context of bencoding, strings, including dictionary keys, are arbitrary byte sequences (uint8_t[]).

And Array.prototype.sort compares 16-bit units by default.

If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

The simple .sort() results in a different order (sorted_in_utf16) than the correct one (sorted_in_utf8). Observe:

const A = String.fromCodePoint(0xFF61);
const B = String.fromCodePoint(0x10002);
const sorted_in_utf8 = [A, B].sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))); // [A, B]
const sorted_in_utf16 = [A, B].sort(); // [B, A]

The related issue

Keys as strings in bencoded dictionaries

Hey, so, I've run into a slight issue with this portion of the doc:

The exception to this is strings appearing as keys in bencoded dicts. These are decoded as Javascript Strings, as they should always be strings of (ascii) characters and if they weren't decoded as JS Strings, dict's couldn't be mapped to Javascript objects.

While this is technically true, it does present a problem. While communicating with a tracker, often times, the infohashes are returned as the dictionary keys. For example, a simple scrape response:

d5:filesd20:[info_hash]d8:completei23492e10:incompletei4544e10:downloadedi1024eeee

Where [info_hash] is a 20-byte, binary sha1sum that isn't necessarily well-suited to be a String. Say we decode that, either using bncode.decode or new bncode.decoder/decode/result. We'll get something along the lines of:

{
    "files": {
        "[incorrect info_hash]": {
            "complete": 23492,
            "incomplete": 4544,
            "downloaded": 1024
        }
    }
}

Where [incorrect info_hash] is a String with length 1. Obviously this is the fault of the String, but it's a relatively common use-case. Perhaps it might be wise to create a class for a BDict or something, that allows the use of Buffers as keys?

var data = "d5:dict5:valueee"
data = bncode.decode(data);
console.log(data.get(0)); //<Buffer ...> or "value"
console.log(data.key(0)); //<Buffer ...> or "dict"

Obviously this would add complexity to the library, but, I've been butting my head up against this problem for a few weeks now. Any thoughts/suggestions? I'd be more than willing to prepare a pull request with any guidance.

Consider modifying encodeDict (change for (var i in keys) to forEach)

I am currently integrating torrent-stream to node-Tor/Peersm.

I don't know for what reason encodeDict is using for (var i in keys) to enumerate the sorted result of Object.keys which is an Array instead of a usual forEach

If you have added properties to Array.prototype (like concatBuffers in my case), then the properties get enumerated too and bncode crashes, I don't want to use proto with enumerable false and setPrototypeOf is not available in all browsers.

So I would suggest to use forEach instead.

Consider renaming this repo to "bncode"

It's a bit confusing to have the names differ. It's not a huge deal, just a suggestion. GitHub automatically handles URL redirects for both the website and git remotes if you rename a repo.

Stream for encoding

Currently we can stream file and decoding it.
Is is possible to implement encoding stream?

I want to do this

var cryptoStream = crypto.createHash('sha1');
cryptoStream.on('end', function(){
    console.log(cryptoStream.digest('hex'));
});

var bencStream = new benc.EncodeStream({prop: []});
bencStream.on('data', function(chunk){
    cryptoStream.update(chunk);
});
bencStream.on('end', function(){
    cryptoStream.update(null);
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.