From d967842595ca518676e7403d0a188912ffcfecb8 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Aug 2020 20:11:36 +0200 Subject: [PATCH] cleanup --- test/index.html | 18 ---- test/range-parser.js | 162 ------------------------------- test/sw.js | 60 ------------ test/torrent-handler.js | 210 ---------------------------------------- 4 files changed, 450 deletions(-) delete mode 100644 test/index.html delete mode 100644 test/range-parser.js delete mode 100644 test/sw.js delete mode 100644 test/torrent-handler.js diff --git a/test/index.html b/test/index.html deleted file mode 100644 index f4f2120..0000000 --- a/test/index.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - browser server test - - - - - - - // addTorrent('magnet:?xt=urn:btih:051f0b074ffbdb44c51c5bc0f11b42200b14c3e4&dn=%5BHorribleSubs%5D%20The%20God%20of%20High%20School%20-%2007%20%5B1080p%5D.mkv&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce') - // addTorrent('magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&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') - - - - \ No newline at end of file diff --git a/test/range-parser.js b/test/range-parser.js deleted file mode 100644 index 4955a70..0000000 --- a/test/range-parser.js +++ /dev/null @@ -1,162 +0,0 @@ -/*! - * range-parser - * Copyright(c) 2012-2014 TJ Holowaychuk - * Copyright(c) 2015-2016 Douglas Christopher Wilson - * MIT Licensed - */ - -'use strict' - -/** - * Module exports. - * @public - */ - -// module.exports = rangeParser - -/** - * Parse "Range" header `str` relative to the given file `size`. - * - * @param {Number} size - * @param {String} str - * @param {Object} [options] - * @return {Array} - * @public - */ - -function rangeParser (size, str, options) { - if (typeof str !== 'string') { - throw new TypeError('argument str must be a string') - } - - var index = str.indexOf('=') - - if (index === -1) { - return -2 - } - - // split the range string - var arr = str.slice(index + 1).split(',') - var ranges = [] - - // add ranges type - ranges.type = str.slice(0, index) - - // parse all ranges - for (var i = 0; i < arr.length; i++) { - var range = arr[i].split('-') - var start = parseInt(range[0], 10) - var end = parseInt(range[1], 10) - - // -nnn - if (isNaN(start)) { - start = size - end - end = size - 1 - // nnn- - } else if (isNaN(end)) { - end = size - 1 - } - - // limit last-byte-pos to current length - if (end > size - 1) { - end = size - 1 - } - - // invalid or unsatisifiable - if (isNaN(start) || isNaN(end) || start > end || start < 0) { - continue - } - - // add range - ranges.push({ - start: start, - end: end - }) - } - - if (ranges.length < 1) { - // unsatisifiable - return -1 - } - - return options && options.combine - ? combineRanges(ranges) - : ranges -} - -/** - * Combine overlapping & adjacent ranges. - * @private - */ - -function combineRanges (ranges) { - var ordered = ranges.map(mapWithIndex).sort(sortByRangeStart) - - for (var j = 0, i = 1; i < ordered.length; i++) { - var range = ordered[i] - var current = ordered[j] - - if (range.start > current.end + 1) { - // next range - ordered[++j] = range - } else if (range.end > current.end) { - // extend range - current.end = range.end - current.index = Math.min(current.index, range.index) - } - } - - // trim ordered array - ordered.length = j + 1 - - // generate combined range - var combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex) - - // copy ranges type - combined.type = ranges.type - - return combined -} - -/** - * Map function to add index value to ranges. - * @private - */ - -function mapWithIndex (range, index) { - return { - start: range.start, - end: range.end, - index: index - } -} - -/** - * Map function to remove index value from ranges. - * @private - */ - -function mapWithoutIndex (range) { - return { - start: range.start, - end: range.end - } -} - -/** - * Sort function to sort ranges by index. - * @private - */ - -function sortByRangeIndex (a, b) { - return a.index - b.index -} - -/** - * Sort function to sort ranges by start position. - * @private - */ - -function sortByRangeStart (a, b) { - return a.start - b.start -} diff --git a/test/sw.js b/test/sw.js deleted file mode 100644 index a258a13..0000000 --- a/test/sw.js +++ /dev/null @@ -1,60 +0,0 @@ -// Activate event -// Be sure to call self.clients.claim() -self.addEventListener('activate', evt => { - // `claim()` sets this worker as the active worker for all clients that - // match the workers scope and triggers an `oncontrollerchange` event for - // the clients. - return self.clients.claim() -}) - -self.addEventListener('fetch', evt => { - const { request } = evt - const { url, method } = request - const headers = [...request.headers] - if (!url.includes(self.registration.scope + 'webtorrent/')) return null - - function getConsumer(clients) { - return new Promise((rs, rj) => { - // Use race condition for whoever controls the response stream - for (const client of clients) { - const mc = new MessageChannel() - mc.port1.onmessage = evt => rs([evt.data, mc]) - client.postMessage({ - url, - method, - headers, - scope: self.registration.scope - }, [mc.port2]) - } - }) - } - - evt.respondWith( - clients.matchAll({ type: 'window', includeUncontrolled: true }) - .then(getConsumer) - .then(([data, consumer]) => { - const readable = 'body' in data ? data.body : new ReadableStream({ - pull(controller) { - console.log('requesting data') - return new Promise(rs => { - consumer.port1.onmessage = evt => { - evt.data - ? controller.enqueue(evt.data) // evt.data is Uint8Array - : controller.close() // evt.data is null, means the stream ended - rs() - } - consumer.port1.postMessage(true) // send a pull request - }) - }, - cancel() { - // This event is never executed - console.log('request aborted') - consumer.port1.postMessage(false) // send a cancel request - } - }) - - return new Response(readable, data) - }) - .catch(console.error) - ) -}) diff --git a/test/torrent-handler.js b/test/torrent-handler.js deleted file mode 100644 index 43ac172..0000000 --- a/test/torrent-handler.js +++ /dev/null @@ -1,210 +0,0 @@ -// Wish there where a easier way to get some of webtorrent's classes so i can patch stuff -// const WebTorrent = require('webtorrent') -// const { Torrent } = WebTorrent - -const client = new WebTorrent(), - dummyTorrent = client.add('06d67cc41f44fd57241551b6d95c2d1de38121ae'), - torrentPrototype = Object.getPrototypeOf(dummyTorrent), - announceList = [ - ['udp://tracker.openbittorrent.com:80'], - ['udp://tracker.internetwarriors.net:1337'], - ['udp://tracker.leechers-paradise.org:6969'], - ['udp://tracker.coppersurfer.tk:6969'], - ['udp://exodus.desync.com:6969'], - ['wss://tracker.webtorrent.io'], - ['wss://tracker.btorrent.xyz'], - ['wss://tracker.openwebtorrent.com'], - ['wss://tracker.fastcast.nz'] - ] -client.remove('06d67cc41f44fd57241551b6d95c2d1de38121ae') - - -WEBTORRENT_ANNOUNCE = announceList - .map(function (arr) { - return arr[0] - }) - .filter(function (url) { - return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0 - }) - -function addTorrent(magnet) { - if (client.torrents[0]) { - client.remove(client.torrents[0].infoHash) - } - client.add(magnet, async function (torrent) { - const server = await torrent.createServer() - await server.listen() - - let a = document.createElement('a') - a.href = a.innerText = `/webtorrent/${torrent.infoHash}/` - a.target = '_blank' - document.body.appendChild(a) - - let videoFile = torrent.files[0] - torrent.files.forEach(file => { - if (file.length > videoFile.length) { - videoFile = file - } - } - - ) - video.src = `/webtorrent/${torrent.infoHash}/${encodeURI(videoFile.path)}` - }) -} - - -function getPageHTML(title, pageHtml) { - return "" + title + "" + pageHtml + ""; -} - -// From https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent -function encodeRFC5987(str) { - return encodeURIComponent(str) - // Note that although RFC3986 reserves "!", RFC5987 does not, - // so we do not need to escape it - .replace(/['()]/g, escape) // i.e., %27 %28 %29 - .replace(/\*/g, '%2A') - // The following are not required for percent-encoding per RFC5987, - // so we can allow for a little better readability over the wire: |`^ - .replace(/%(?:7C|60|5E)/g, unescape) -} - -torrentPrototype.createServer = function (requestListener) { - if (this.destroyed) throw new Error('torrent is destroyed') - - let registration = null - const torrent = this - - function serveIndexPage() { - const listHtml = torrent.files.map((file, i) => - `
  • ${file.path} (${file.length} bytes)
  • ` - ).join('
    ') - - const body = getPageHTML( - `${torrent.name} - WebTorrent`, - `

    ${torrent.name}

      ${listHtml}
    ` - ) - - return { - status: 200, - headers: { - 'Content-Type': 'text/html' - }, - body, - } - } - - function serve404Page() { - return { - status: 404, - headers: { - 'Content-Type': 'text/html' - }, - body: getPageHTML('404 - Not Found', '

    404 - Not Found

    ') - } - } - - function serveFile(file, req) { - const res = { - status: 200, - headers: { - 'Content-Type': file._getMimeType(), - // Support range-requests - 'Accept-Ranges': 'bytes', - // Set name of file (for "Save Page As..." dialog) - 'Content-Disposition': `inline; filename*=UTF-8''${encodeRFC5987(file.name)}` - } - } - - // `rangeParser` returns an array of ranges, or an error code (number) if - // there was an error parsing the range. - let range = rangeParser(file.length, new Headers(req.headers).get('range') || '') - - if (Array.isArray(range)) { - res.status = 206 // indicates that range-request was understood - - // no support for multi-range request, just use the first range - range = range[0] - - res.headers['Content-Range'] = `bytes ${range.start}-${range.end}/${file.length}` - res.headers['Content-Length'] = `${range.end - range.start + 1}` - } else { - range = null - res.headers['Content-Length'] = file.length - } - - if (req.method === 'HEAD') res.body = '' - else res.stream = file.createReadStream(range) - - return res - } - - - navigator.serviceWorker.addEventListener('message', evt => { - const root = new URL(registration.scope).pathname - const url = new URL(evt.data.url) - const pathname = url.pathname.split(`webtorrent/${torrent.infoHash}/`)[1] - const respond = msg => evt.ports[0].postMessage(msg) - - if (pathname === '') { - return respond(serveIndexPage()) - } - - const file = torrent.files.find(f => f.path == decodeURIComponent(pathname)) - const res = serveFile(file, evt.data) - if (res.stream) { - const stream = res.stream - delete res.stream - - stream.once('end', () => { - respond(null) // Signal end event - evt.ports[0].onmessage = null - }) - - evt.ports[0].onmessage = evt => { - const chunk = stream.read() - if (chunk === null) { - stream.once('readable', () => { - const chunk = stream.read() - respond(new Uint8Array(chunk)) - }) - } else { - respond(new Uint8Array(chunk)) - } - } - } - - respond(res) - }) - - const res = { - listen(port) { - const scope = `./` - res.scope = scope - return navigator.serviceWorker.getRegistration(scope).then(swReg => { - return swReg || navigator.serviceWorker.register('sw.js', { - scope - }) - }).then(swReg => { - registration = swReg - res.scope = registration.scope - res.registration = registration - let swRegTmp = swReg.installing || swReg.waiting - - if (swReg.active) - return - - return new Promise(rs => { - swRegTmp.onstatechange = () => { - if (swRegTmp.state === 'activated') rs() - } - }) - }) - }, - close() { - registration && registration.unregister() - } - } - - return res -} \ No newline at end of file