Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLOSED] TypeError: Cannot read property 'byteLength' of undefined #98

Closed
feross opened this issue Sep 21, 2014 · 4 comments
Closed

[CLOSED] TypeError: Cannot read property 'byteLength' of undefined #98

feross opened this issue Sep 21, 2014 · 4 comments

Comments

@feross
Copy link
Member

@feross feross commented Sep 21, 2014

Issue by iamale
Thursday Apr 24, 2014 at 17:24 GMT
Originally opened as https://github.com/feross/bittorrent-client/issues/2


Seems like quite an issue for me. Code:

#!/usr/bin/env node

var BitTorrentClient = require('bittorrent-client');

var client = BitTorrentClient();
var magnet = "magnet:?xt=urn:btih:1e69917fbaa2c767bca463a96b5572785c6d8a12";
client.add(magnet);
// client.on("torrent", ...);

Output:

[lots of peer-connected-lines, like]
82.xx.xx.105:34xxx supports DHT
Connecting to 187.xx.xx.88:10xxx
Connecting to 71.xx.xx.176:10xxx
Connecting to 189.xx.xx.172:56xxx
86.xx.xx.176:29xxx supports DHT

/home/ale/sandbox/node_modules/bittorrent-client/node_modules/rusha-browserify/rusha.js:150
      var len = str.byteLength || str.length;
                   ^
TypeError: Cannot read property 'byteLength' of undefined
    at Rusha.rawDigest.rawDigest (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/rusha-browserify/rusha.js:150:20)
    at Rusha.digest.digestFromString.digestFromBuffer.digestFromArrayBuffer (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/rusha-browserify/rusha.js:174:18)
    at sha1 (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/ut_metadata/index.js:11:24)
    at ut_metadata._checkDone (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/ut_metadata/index.js:186:9)
    at ut_metadata._onData (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/ut_metadata/index.js:147:10)
    at ut_metadata.onMessage (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/ut_metadata/index.js:80:14)
    at Wire._onExtended (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/bittorrent-swarm/node_modules/bittorrent-protocol/index.js:438:24)
    at Wire._onmessage (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/bittorrent-swarm/node_modules/bittorrent-protocol/index.js:582:19)
    at Wire._write (/home/ale/sandbox/node_modules/bittorrent-client/node_modules/bittorrent-swarm/node_modules/bittorrent-protocol/index.js:481:10)
    at doWrite (_stream_writable.js:219:10)

You're getting a similar error on Travis, too. And you might have noticed the problem before, but I'm posting it just in case.

@feross

This comment has been minimized.

Copy link
Member Author

@feross feross commented Sep 21, 2014

Comment by feross
Friday May 02, 2014 at 16:51 GMT


Thanks for the report. Will fix.

@feross

This comment has been minimized.

Copy link
Member Author

@feross feross commented Sep 21, 2014

Comment by fisch0920
Tuesday May 06, 2014 at 07:00 GMT


I've really interested in this project and you've done a great job so far! I started a deep-dive of the bittorrent-* libs mainly to familiarize myself with everything but with this immediate bug in mind. To that end, I've found a few related issues.

  • ut_metadata only deals with the info-dictionary part of the .torrent file, and in ut_metadata._checkDone, the line
info = bncode.encode(bncode.decode(this.metadata).info)

should be changed to:

info = bncode.encode(bncode.decode(this.metadata))

which is almost a noop except for validating that the metadata is valid bncoded.

Then, the metadata that ut_metadata emits to listeners should be in the form { info : this.metadata }.

The problem then comes in parsing the torrent's metadata where the following line fails because we just have the metadata info dictionary and not the whole .torrent file.
ensure(torrent['announce-list'] || torrent.announce, 'announce-list/announce')

I'm not nearly as familiar with the ins & outs of the protocols, but these areas seem to have contradicting assumptions.

The other issue I ran into deals with the ordering of the onHandshake/onWire logic. Specifically, ut_metadata.onHandshake should be called before ut_metadata._checkDone, because _checkDone relies on having an infohash set, but in reality ut_metadata.onHandshake is never called. This appears to be because Wire._onHandshake (bittorrent-protocol) emits the handshake event after calling the extensions onHandshake, but in this case the handshake event that is emitted from Wire trickles up the stack to Swarm._onwire and Torrent._onWire, which is where the ut_metadata is initialized on the wire via

wire.use(ut_metadata(self.metadata))

If I rearrange the emit to happen before calling the registered extension handshakes (allowing the torrent to register ut_metadata on the wire), this problem is resolved. That being said, the control flow for handshakes is confusing, so there may be a better solution to this problem.

e.g. change:

Wire.prototype._onHandshake = function (infoHash, peerId, extensions) {
  this.peerId = peerId
  this.peerExtensions = extensions

  for (var name in this._ext) {
    this._ext[name].onHandshake(infoHash, peerId, extensions)
  }
  this.emit('handshake', infoHash, peerId, extensions)

to

Wire.prototype._onHandshake = function (infoHash, peerId, extensions) {
  this.peerId = peerId
  this.peerExtensions = extensions

  this.emit('handshake', infoHash, peerId, extensions)
  for (var name in this._ext) {
    this._ext[name].onHandshake(infoHash, peerId, extensions)
  }
@feross

This comment has been minimized.

Copy link
Member Author

@feross feross commented Sep 21, 2014

Comment by feross
Wednesday May 07, 2014 at 19:57 GMT


@fisch0920 You're right on all counts. Thanks for all the PRs! They've all been merged 👍

@feross

This comment has been minimized.

Copy link
Member Author

@feross feross commented Sep 21, 2014

Comment by iamale
Thursday May 08, 2014 at 02:21 GMT


👍 Thanks guys, you're awesome!

@lock lock bot locked as resolved and limited conversation to collaborators May 7, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.