From c0d1c9ccb7a281fcfc507f858be418dacb7a5f32 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 16 Dec 2014 18:08:03 -0800 Subject: [PATCH 1/4] add client.seed test for path to folder (string) --- test/basic-node.js | 19 +++++++++++++++++++ test/content/numbers/1.txt | 1 + test/content/numbers/2.txt | 1 + test/content/numbers/3.txt | 1 + 4 files changed, 22 insertions(+) create mode 100644 test/content/numbers/1.txt create mode 100644 test/content/numbers/2.txt create mode 100644 test/content/numbers/3.txt diff --git a/test/basic-node.js b/test/basic-node.js index caf69b6c..9304afeb 100644 --- a/test/basic-node.js +++ b/test/basic-node.js @@ -9,6 +9,7 @@ var leavesPath = __dirname + '/torrents/leaves.torrent' var leaves = fs.readFileSync(leavesPath) var leavesTorrent = parseTorrent(leaves) var leavesBookPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' +var numbersPath = __dirname + '/content/numbers' test('client.add (http url to a torrent file (string))', function (t) { t.plan(1) @@ -50,3 +51,21 @@ test('client.seed (filesystem path to file (string))', function (t) { client.destroy() }) }) + +test('client.seed (filesystem path to folder (string))', function (t) { + t.plan(1) + + var opts = { + pieceLength: 32768, // force piece length to 32KB so info-hash will + // match what transmission generated, since we use + // a different algo for picking piece length + + private: false // also force `private: false` to match transmission + } + + var client = new WebTorrent({ dht: false, trackers: false }) + client.seed(numbersPath, opts, function (torrent) { + t.equal(torrent.infoHash, '80562f38656b385ea78959010e51a2cc9db41ea0') + client.destroy() + }) +}) diff --git a/test/content/numbers/1.txt b/test/content/numbers/1.txt new file mode 100644 index 00000000..56a6051c --- /dev/null +++ b/test/content/numbers/1.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/test/content/numbers/2.txt b/test/content/numbers/2.txt new file mode 100644 index 00000000..8fdd954d --- /dev/null +++ b/test/content/numbers/2.txt @@ -0,0 +1 @@ +22 \ No newline at end of file diff --git a/test/content/numbers/3.txt b/test/content/numbers/3.txt new file mode 100644 index 00000000..4f376707 --- /dev/null +++ b/test/content/numbers/3.txt @@ -0,0 +1 @@ +333 \ No newline at end of file From 9609b381f54dff948d7088a1b77bfa185cba1e80 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 16 Dec 2014 18:09:11 -0800 Subject: [PATCH 2/4] support seeding entire folders (string path to folder) --- index.js | 57 ++++++++++++++++++---------------------------------- package.json | 2 +- 2 files changed, 20 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index d3424559..403c026a 100644 --- a/index.js +++ b/index.js @@ -186,7 +186,7 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) { * - W3C FileList object (basically an array of `File` objects) * - Array of `File` objects * - * @param {string|File|FileList|Blob|Buffer|Array.} input + * @param {string|File|FileList|Buffer|Array.} input * @param {Object} opts * @param {function} onseed */ @@ -199,45 +199,26 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { if (!opts) opts = {} // TODO: support an array of paths - // TODO: support path to folder (currently, only path to file supported) - - if (typeof FileList !== 'undefined' && input instanceof FileList) - input = Array.prototype.slice.call(input) - - if (isBlob(input) || Buffer.isBuffer(input)) - input = [ input ] - - var streams - if (Array.isArray(input) && input.length > 0) { - streams = input.map(function (item) { - if (isBlob(item)) return new FileReadStream(item) - else if (Buffer.isBuffer(item)) { - var s = new stream.PassThrough() - s.end(item) - return s - } else { - throw new Error('Array must contain only File|Blob|Buffer objects') - } - }) - } else if (typeof input === 'string') { - streams = [ fs.createReadStream(input) ] - } else { - throw new Error('invalid input type') - } - createTorrent(input, opts, function (err, torrentBuf) { + createTorrent.parseInput(input, opts, function (err, files) { if (err) return self.emit('error', err) - self.add(torrentBuf, opts, function (torrent) { - var tasks = [function (cb) { - torrent.storage.load(streams, cb) - }] - if (self.dht) tasks.push(function (cb) { - torrent.on('dhtAnnounce', cb) - }) - parallel(tasks, function (err) { - if (err) return self.emit('error', err) - if (onseed) onseed(torrent) - self.emit('seed', torrent) + var streams = files.map(function (file) { return file.getStream }) + + createTorrent(input, opts, function (err, torrentBuf) { + if (err) return self.emit('error', err) + + self.add(torrentBuf, opts, function (torrent) { + var tasks = [function (cb) { + torrent.storage.load(streams, cb) + }] + if (self.dht) tasks.push(function (cb) { + torrent.on('dhtAnnounce', cb) + }) + parallel(tasks, function (err) { + if (err) return self.emit('error', err) + if (onseed) onseed(torrent) + self.emit('seed', torrent) + }) }) }) }) diff --git a/package.json b/package.json index 5444d896..a8af1e42 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "block-stream": "0.0.7", "clivas": "^0.1.4", "concat-stream": "^1.4.6", - "create-torrent": "^3.1.0", + "create-torrent": "^3.4.0", "debug": "^2.1.0", "dezalgo": "^1.0.1", "end-of-stream": "^1.0.0", From 6cab794d4df6f3aa6d30d419b3acff6e9ce89f73 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 16 Dec 2014 18:09:30 -0800 Subject: [PATCH 3/4] delete comments; don't repeat readme --- README.md | 3 +-- index.js | 21 +-------------------- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index cb4c136d..d0cbafa5 100644 --- a/README.md +++ b/README.md @@ -266,10 +266,9 @@ Start seeding a new torrent. - path to the file or folder on filesystem (string) - W3C [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object (from an `` or drag and drop) - W3C [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) object (basically an array of `File` objects) -- W3C [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object - Node [Buffer](http://nodejs.org/api/buffer.html) object (works in [the browser](https://www.npmjs.org/package/buffer)) -Or, an **array of `File`, `Blob`, or `Buffer` objects**. +Or, an **array of `File` or `Buffer` objects**. If `opts` is specified, it should contain the following types of options: diff --git a/index.js b/index.js index 403c026a..8cf90737 100644 --- a/index.js +++ b/index.js @@ -121,15 +121,6 @@ WebTorrent.prototype.get = function (torrentId) { /** * Start downloading a new torrent. Aliased as `client.download`. - * - * `torrentId` can be one of: - * - magnet uri (utf8 string) - * - torrent file (buffer) - * - info hash (hex string or buffer) - * - parsed torrent (from [parse-torrent](https://github.com/feross/parse-torrent)) - * - http/https url to a torrent file (string) - * - filesystem path to a torrent file (string) - * * @param {string|Buffer|Object} torrentId * @param {Object} opts torrent-specific options * @param {function=} ontorrent called when the torrent is ready (has metadata) @@ -179,13 +170,6 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) { /** * Start seeding a new torrent. - * - * `input` can be any of the following: - * - path to the file or folder on filesystem (string) - * - W3C File object (from an `` or drag and drop) - * - W3C FileList object (basically an array of `File` objects) - * - Array of `File` objects - * * @param {string|File|FileList|Buffer|Array.} input * @param {Object} opts * @param {function} onseed @@ -226,7 +210,6 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { /** * Remove a torrent from the client. - * * @param {string|Buffer} torrentId * @param {function} cb */ @@ -241,8 +224,6 @@ WebTorrent.prototype.remove = function (torrentId, cb) { /** * Destroy the client, including all torrents and connections to peers. - * - * @override * @param {function} cb */ WebTorrent.prototype.destroy = function (cb) { @@ -263,7 +244,7 @@ WebTorrent.prototype.destroy = function (cb) { } /** - * Check if `obj` is a W3C Blob object (which is the superclass of W3C File) + * Check if `obj` is a W3C Blob object (which is the superclass of W3C File). * @param {*} obj * @return {boolean} */ From d0436e41e72780d001048079bf265c08ecb1fd53 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 16 Dec 2014 18:37:19 -0800 Subject: [PATCH 4/4] remove hack from tests --- test/basic-node.js | 2 +- test/basic.js | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/test/basic-node.js b/test/basic-node.js index 9304afeb..60394057 100644 --- a/test/basic-node.js +++ b/test/basic-node.js @@ -63,7 +63,7 @@ test('client.seed (filesystem path to folder (string))', function (t) { private: false // also force `private: false` to match transmission } - var client = new WebTorrent({ dht: false, trackers: false }) + var client = new WebTorrent({ dht: false, tracker: false }) client.seed(numbersPath, opts, function (torrent) { t.equal(torrent.infoHash, '80562f38656b385ea78959010e51a2cc9db41ea0') client.destroy() diff --git a/test/basic.js b/test/basic.js index 912c4b03..5de4f67a 100644 --- a/test/basic.js +++ b/test/basic.js @@ -52,13 +52,7 @@ test('client.seed (Buffer, Blob)', function (t) { // Blob if (typeof Blob !== 'undefined') { var client2 = new WebTorrent({ dht: false, tracker: false }) - var blob = new Blob([ leavesBook ]) - - // TODO: just pass name in the opts object – this should work - // Doing it this way until we use the create-torrent code to process inputs - // in client.seed - blob.name = opts.name - client2.seed(blob, function (torrent) { + client2.seed(new Blob([ leavesBook ]), opts, function (torrent) { verify(t, client2, torrent) }) } else {