Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2015-01-04 12:13:02 +0300
committerFeross Aboukhadijeh <feross@feross.org>2015-01-04 12:13:02 +0300
commitf2a79a7cbcfca415c30fb238354339517254e148 (patch)
treedadc1e265bb636fa044ca1980ad384f863064a49
parent1aee189a736c0d1c9d8341ee3a4ec599c6cc1af4 (diff)
parent5c78cf25fe40f8b88680559ddbdbbc7a3f004a25 (diff)
Merge pull request #230 from feross/get-buffer
Add file.getBuffer() API
-rw-r--r--README.md19
-rw-r--r--lib/storage.js24
-rw-r--r--lib/torrent.js7
-rw-r--r--package.json2
-rw-r--r--test/content/blocklist.txt.gzbin0 -> 91 bytes
-rw-r--r--test/download-dht-magnet.js12
-rw-r--r--test/download-dht-torrent.js12
-rw-r--r--test/download-tracker-magnet.js12
-rw-r--r--test/download-tracker-torrent.js12
-rw-r--r--test/server.js22
10 files changed, 79 insertions, 43 deletions
diff --git a/README.md b/README.md
index 7c5a1d2..b9ac698 100644
--- a/README.md
+++ b/README.md
@@ -428,13 +428,28 @@ You can pass `opts` to stream only a slice of a file.
Both `start` and `end` are inclusive.
+#### `file.getBuffer(function callback (err, url) {})`
+
+Get the file contents as a `Buffer`.
+
+The file will be fetched from the network with highest priority, and `callback` will be
+called once the file is ready. `callback` must be specified, and will be called with a an
+`Error` (or `null`) and the file contents as a `Buffer`.
+
+```js
+file.getBuffer(function (err, buffer) {
+ if (err) throw err
+ console.log(buffer) // <Buffer 00 98 00 01 01 00 00 00 50 ae 07 04 01 00 00 00 0a 00 00 00 00 00 00 00 78 ae 07 04 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
+})
+```
+
#### `file.getBlobURL(function callback (err, url) {})`
Get a url which can be used in the browser to refer to the file.
The file will be fetched from the network with highest priority, and `callback` will be
-called when it is ready. `callback` must be specified and may be called with a an `Error`
-or the blob url (`String`) when the file data is available and ready to be used.
+called once the file is ready. `callback` must be specified, and will be called with a an
+`Error` (or `null`) and the Blob URL (`String`).
```js
file.getBlobURL(function (err, url) {
diff --git a/lib/storage.js b/lib/storage.js
index 5454a38..d652c66 100644
--- a/lib/storage.js
+++ b/lib/storage.js
@@ -254,20 +254,32 @@ File.prototype.createReadStream = function (opts) {
}
/**
- * TODO: detect errors and call callback with error
* @param {function} cb
*/
File.prototype.getBlobURL = function (cb) {
var self = this
- var chunks = []
+ self.getBuffer(function (err, buf) {
+ if (err) return cb(err)
+ var url = URL.createObjectURL(new Blob([ buf ]))
+ cb(null, url)
+ })
+}
+
+/**
+ * TODO: detect errors and call callback with error
+ * @param {function} cb
+ */
+File.prototype.getBuffer = function (cb) {
+ var self = this
+ var buf = new Buffer(self.length)
+ var start = 0
self.createReadStream()
.on('data', function (chunk) {
- chunks.push(chunk)
+ chunk.copy(buf, start)
+ start += chunk.length
})
.on('end', function () {
- var buf = Buffer.concat(chunks)
- var url = URL.createObjectURL(new Blob([ buf ]))
- cb(null, url)
+ cb(null, buf)
})
}
diff --git a/lib/torrent.js b/lib/torrent.js
index bf110f9..83cd358 100644
--- a/lib/torrent.js
+++ b/lib/torrent.js
@@ -1,7 +1,6 @@
module.exports = Torrent
var addrToIPPort = require('addr-to-ip-port') // browser exclude
-var concat = require('concat-stream') // browser exclude
var debug = require('debug')('webtorrent:torrent')
var Discovery = require('torrent-discovery')
var EventEmitter = require('events').EventEmitter
@@ -79,13 +78,13 @@ function Torrent (torrentId, opts) {
} else if (typeof get === 'function' && /^https?:/.test(torrentId)) {
// http or https url to torrent file
- get({
+ get.concat({
url: torrentId,
headers: { 'user-agent': 'WebTorrent (http://webtorrent.io)' }
- }, function (err, res) {
+ }, function (err, data) {
if (err)
return self.emit('error', new Error('error downloading torrent: ' + err.message))
- res.pipe(concat(onTorrentId))
+ onTorrentId(data)
})
} else if (typeof fs.readFile === 'function') {
diff --git a/package.json b/package.json
index 5c70f76..d160ca6 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,6 @@
"addr-to-ip-port": false,
"bittorrent-dht/client": false,
"bittorrent-swarm": "webtorrent-swarm",
- "concat-stream": false,
"load-ip-set": false,
"simple-get": false,
"ut_pex": false
@@ -38,7 +37,6 @@
"block-stream": "0.0.7",
"browserify-versionify": "^1.0.2",
"clivas": "^0.1.4",
- "concat-stream": "^1.4.6",
"create-torrent": "^3.4.0",
"debug": "^2.1.0",
"dezalgo": "^1.0.1",
diff --git a/test/content/blocklist.txt.gz b/test/content/blocklist.txt.gz
new file mode 100644
index 0000000..5f401ed
--- /dev/null
+++ b/test/content/blocklist.txt.gz
Binary files differ
diff --git a/test/download-dht-magnet.js b/test/download-dht-magnet.js
index 1a0b3bc..639f485 100644
--- a/test/download-dht-magnet.js
+++ b/test/download-dht-magnet.js
@@ -5,7 +5,8 @@ var parseTorrent = require('parse-torrent')
var test = require('tape')
var WebTorrent = require('../')
-var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesFile = fs.readFileSync(leavesPath)
var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesParsed = parseTorrent(leavesTorrent)
@@ -14,7 +15,7 @@ leavesParsed.announce = []
leavesParsed.announceList = []
test('Download using DHT (via magnet uri)', function (t) {
- t.plan(7)
+ t.plan(8)
var dhtServer = new DHT({ bootstrap: false })
dhtServer.on('error', function (err) {
@@ -55,7 +56,7 @@ test('Download using DHT (via magnet uri)', function (t) {
maybeDone(null)
})
- torrent.storage.load(fs.createReadStream(leavesFile), function (err) {
+ torrent.storage.load(fs.createReadStream(leavesPath), function (err) {
wroteStorage = true
maybeDone(err)
})
@@ -73,7 +74,10 @@ test('Download using DHT (via magnet uri)', function (t) {
client2.on('torrent', function (torrent) {
torrent.files.forEach(function (file) {
- file.createReadStream()
+ file.getBuffer(function (err, buf) {
+ if (err) throw err
+ t.deepEqual(buf, leavesFile, 'downloaded correct content')
+ })
})
torrent.once('done', function () {
diff --git a/test/download-dht-torrent.js b/test/download-dht-torrent.js
index 19b998e..0e38512 100644
--- a/test/download-dht-torrent.js
+++ b/test/download-dht-torrent.js
@@ -5,7 +5,8 @@ var fs = require('fs')
var parseTorrent = require('parse-torrent')
var test = require('tape')
-var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesFile = fs.readFileSync(leavesPath)
var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesParsed = parseTorrent(leavesTorrent)
@@ -14,7 +15,7 @@ leavesParsed.announce = []
leavesParsed.announceList = []
test('Download using DHT (via .torrent file)', function (t) {
- t.plan(7)
+ t.plan(8)
var dhtServer = new DHT({ bootstrap: false })
@@ -54,7 +55,7 @@ test('Download using DHT (via .torrent file)', function (t) {
maybeDone(null)
})
- torrent.storage.load(fs.createReadStream(leavesFile), function (err) {
+ torrent.storage.load(fs.createReadStream(leavesPath), function (err) {
wroteStorage = true
maybeDone(err)
})
@@ -72,7 +73,10 @@ test('Download using DHT (via .torrent file)', function (t) {
client2.on('torrent', function (torrent) {
torrent.files.forEach(function (file) {
- file.createReadStream()
+ file.getBuffer(function (err, buf) {
+ if (err) throw err
+ t.deepEqual(buf, leavesFile, 'downloaded correct content')
+ })
})
torrent.once('done', function () {
diff --git a/test/download-tracker-magnet.js b/test/download-tracker-magnet.js
index 8d44abc..4564a3a 100644
--- a/test/download-tracker-magnet.js
+++ b/test/download-tracker-magnet.js
@@ -5,7 +5,8 @@ var test = require('tape')
var TrackerServer = require('bittorrent-tracker/server')
var WebTorrent = require('../')
-var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesFile = fs.readFileSync(leavesPath)
var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesParsed = parseTorrent(leavesTorrent)
@@ -18,7 +19,7 @@ test('Download using HTTP tracker (via magnet uri)', function (t) {
})
function magnetDownloadTest (t, serverType) {
- t.plan(8)
+ t.plan(9)
var trackerStartCount = 0
var magnetUri
@@ -65,7 +66,7 @@ function magnetDownloadTest (t, serverType) {
t.deepEqual(torrent.files.map(function (file) { return file.name }), names)
- torrent.storage.load(fs.createReadStream(leavesFile), function (err) {
+ torrent.storage.load(fs.createReadStream(leavesPath), function (err) {
cb(err, client1)
})
})
@@ -79,7 +80,10 @@ function magnetDownloadTest (t, serverType) {
client2.on('torrent', function (torrent) {
torrent.files.forEach(function (file) {
- file.createReadStream()
+ file.getBuffer(function (err, buf) {
+ if (err) throw err
+ t.deepEqual(buf, leavesFile, 'downloaded correct content')
+ })
})
torrent.once('done', function () {
diff --git a/test/download-tracker-torrent.js b/test/download-tracker-torrent.js
index 0838b0b..f0ecf03 100644
--- a/test/download-tracker-torrent.js
+++ b/test/download-tracker-torrent.js
@@ -5,7 +5,8 @@ var test = require('tape')
var TrackerServer = require('bittorrent-tracker/server')
var WebTorrent = require('../')
-var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesFile = fs.readFileSync(leavesPath)
var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesParsed = parseTorrent(leavesTorrent)
@@ -18,7 +19,7 @@ test('Download using HTTP tracker (via .torrent file)', function (t) {
})
function torrentDownloadTest (t, serverType) {
- t.plan(8)
+ t.plan(9)
var trackerStartCount = 0
@@ -65,7 +66,7 @@ function torrentDownloadTest (t, serverType) {
t.deepEqual(torrent.files.map(function (file) { return file.name }), names)
- torrent.storage.load(fs.createReadStream(leavesFile), function (err) {
+ torrent.storage.load(fs.createReadStream(leavesPath), function (err) {
cb(err, client1)
})
})
@@ -79,7 +80,10 @@ function torrentDownloadTest (t, serverType) {
client2.on('torrent', function (torrent) {
torrent.files.forEach(function (file) {
- file.createReadStream()
+ file.getBuffer(function (err, buf) {
+ if (err) throw err
+ t.deepEqual(buf, leavesFile, 'downloaded correct content')
+ })
})
torrent.once('done', function () {
diff --git a/test/server.js b/test/server.js
index f00c22b..f8b2722 100644
--- a/test/server.js
+++ b/test/server.js
@@ -1,11 +1,10 @@
-var concat = require('concat-stream')
+var get = require('simple-get')
var fs = require('fs')
-var http = require('http')
var portfinder = require('portfinder')
var test = require('tape')
var WebTorrent = require('../')
-var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
+var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
test('start http server programmatically', function (t) {
@@ -18,20 +17,17 @@ test('start http server programmatically', function (t) {
var server = torrent.createServer()
server.listen(port)
- http.get('http://localhost:' + port + '/0', function (res) {
- res.pipe(concat(function (data) {
+ get.concat('http://localhost:' + port + '/0', function (err, data) {
+ // Verify data for first (and only file)
+ t.deepEqual(data, fs.readFileSync(leavesPath))
- // Verify data for first (and only file)
- t.deepEqual(data, fs.readFileSync(leavesFile))
-
- server.close()
- client.destroy()
- t.end()
- }))
+ server.close()
+ client.destroy()
+ t.end()
})
})
})
torrent.on('ready', function () {
- torrent.storage.load(fs.createReadStream(leavesFile))
+ torrent.storage.load(fs.createReadStream(leavesPath))
})
})