WebTorrent working for retrieval - though won't serve files or use the WSS trackers.

This commit is contained in:
Mitra Ardron 2018-04-17 10:45:02 +10:00
parent e949806d75
commit 9632cb44a0

View File

@ -259,32 +259,49 @@ class TransportWEBTORRENT extends Transport {
:param boolean verbose: true for debugging output :param boolean verbose: true for debugging output
:returns stream: The readable stream. :returns stream: The readable stream.
*/ */
verbose = true; //TODO-SW just for debugging
if (verbose) console.log("TransportWEBTORRENT createreadstream %o %o", file.name, opts); if (verbose) console.log("TransportWEBTORRENT createreadstream %o %o", file.name, opts);
try { try {
const through = new stream.PassThrough(); const through = new stream.PassThrough();
const fileStream = file.createReadStream(opts); const fileStream = file.createReadStream(opts);
fileStream.pipe(through); fileStream.pipe(through);
return through; return through;
//return fileStream; //TODO-SW-WT this was "return through" and will need to be for non-SW cases
} catch(err) { } catch(err) {
console.log("TransportWEBTORRENT caught error", err) console.log("TransportWEBTORRENT caught error", err)
if (typeof through.destroy === 'function') through.destroy(err) if (typeof through.destroy === 'function') through.destroy(err)
else through.emit('error', err) else through.emit('error', err)
}; }
} }
async p_createReadStream(url, opts, verbose) { async p_createReadableStream(url, opts, verbose) {
//Return a readable stream (suitable for a HTTP response) from a node type stream from webtorrent.
let filet = await this._p_fileTorrentFromUrl(url); let filet = await this._p_fileTorrentFromUrl(url);
return this.createReadStream(filet, opts, verbose); return new ReadableStream({
start (controller) {
console.log('start', url, opts);
// Create a webtorrent file stream
const filestream = filet.createReadStream(opts);
// When data comes out of webtorrent node.js style stream, put it into the WHATWG stream
filestream.on('data', value => {
controller.enqueue(value)
});
filestream.on('end', () => {
controller.close()
})
},
cancel (reason) {
throw new errors.TransportError(`cancelled ${url}, ${opts} ${reason}`);
}
});
} }
static async p_test(opts, verbose) { static async p_test(opts, verbose) {
try { try {
let transport = await this.p_setup(opts, verbose); // Assumes IPFS already setup let transport = await this.p_setup(opts, verbose); // Assumes IPFS already setup
if (verbose) console.log(transport.name, "setup"); if (verbose) console.log(transport.name, "setup");
let res = await transport.p_status(verbose); let res = await transport.p_status(verbose);
console.assert(res === Transport.STATUS_CONNECTED) console.assert(res === Transport.STATUS_CONNECTED);
// Creative commons torrent, copied from https://webtorrent.io/free-torrents // Creative commons torrent, copied from https://webtorrent.io/free-torrents
let bigBuckBunny = 'magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent/Big Buck Bunny.en.srt'; let bigBuckBunny = 'magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent/Big Buck Bunny.en.srt';