From 5d970f70e544b33f92b014574ea6b7257fb6c95c Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Apr 2014 01:32:42 -0700 Subject: [PATCH] make bignum an optional dependency --- index.js | 19 +++++++++++++++++-- package.json | 4 +++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index feae75d..63f00cf 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,12 @@ exports.Client = Client exports.Server = Server -var bignum = require('bignum') +// optional compiled dependency. if it doesn't compile, no big deal. +var bignum +try { + bignum = require('bignum') +} catch (e) {} + var bncode = require('bncode') var compact2string = require('compact2string') var dgram = require('dgram') @@ -18,6 +23,7 @@ var url = require('url') var CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ]) var ACTIONS = { CONNECT: 0, ANNOUNCE: 1 } var EVENTS = { completed: 1, started: 2, stopped: 3 } +var MAX_UINT = 4294967295 inherits(Client, EventEmitter) @@ -522,7 +528,16 @@ function toUInt32 (n) { } function toUInt64 (n) { - return bignum(n).toBuffer({ size: 8 }) + if (typeof bignum === 'function') { + return bignum(n).toBuffer({ size: 8 }) + } else { + // optional compiled dependency 'bignum' is not available, so round down to MAX_UINT. + // These values are only used for tracker stats anyway. + if (n > MAX_UINT) { + n = MAX_UINT + } + return Buffer.concat([toUInt32(0), toUInt32(n)]) + } } function bytewiseEncodeURIComponent (buf) { diff --git a/package.json b/package.json index 6ec46ad..be42c89 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,6 @@ "url": "https://github.com/feross/bittorrent-tracker/issues" }, "dependencies": { - "bignum": "^0.6.2", "bncode": "^0.5.2", "compact2string": "^1.2.0", "extend.js": "0.0.1", @@ -26,6 +25,9 @@ "portfinder": "^0.2.1", "tape": "2.x" }, + "optionalDependencies": { + "bignum": "^0.6.2" + }, "homepage": "http://webtorrent.io", "keywords": [ "torrent",