some major fucking fixes

This commit is contained in:
ThaUnknown 2020-09-06 11:53:19 +02:00
parent 37aad6ba73
commit 0754686c33
2 changed files with 94 additions and 186 deletions

View file

@ -1,60 +1,55 @@
// 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('activate', evt => {
return self.clients.claim()
})
self.addEventListener('fetch', evt => {
const { request } = evt
const { url, method } = request
const headers = [...request.headers]
const { url, method, headers } = request
if (!url.includes(self.registration.scope + 'webtorrent/')) return null
function getConsumer(clients) {
return new Promise((rs, rj) => {
return new Promise(rs => {
// 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])
const {port1, port2} = mc
port1.onmessage = evt => {
rs([evt.data, mc])
}
client.postMessage({
url,
method,
headers,
headers: [...headers],
scope: self.registration.scope
}, [mc.port2])
}, [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
}
})
.then(getConsumer)
.then(([data, mc]) => {
const body = data.body === 'stream' ? new ReadableStream({
pull(controller) {
return new Promise(rs => {
mc.port1.onmessage = evt => {
evt.data
? controller.enqueue(evt.data) // evt.data is Uint8Array
: controller.close() // evt.data is null, means the stream ended
rs()
}
mc.port1.postMessage(true) // send a pull request
})
},
cancel() {
// This event is never executed
mc.port1.postMessage(false) // send a cancel request
}
}) : data.body
return new Response(readable, data)
})
.catch(console.error)
return new Response(body, data)
})
.catch(console.error)
)
})
})

View file

@ -1,6 +1,4 @@
const client = new WebTorrent(),
dummyTorrent = client.add('06d67cc41f44fd57241551b6d95c2d1de38121ae'),
torrentPrototype = Object.getPrototypeOf(dummyTorrent),
announceList = [
['wss://tracker.openwebtorrent.com'],
['wss://tracker.btorrent.xyz'],
@ -23,8 +21,9 @@ const client = new WebTorrent(),
videoExtensions = [
'.avi', '.mp4', '.m4v', '.webm', '.mov', '.mkv', '.mpg', '.mpeg',
'.ogv', '.webm', '.wmv', '.m2ts'
]
client.remove('06d67cc41f44fd57241551b6d95c2d1de38121ae')
],
scope = '/',
sw = navigator.serviceWorker.register('/sw.js', { scope })
//for debugging
function t(a) {
@ -44,16 +43,18 @@ WEBTORRENT_ANNOUNCE = announceList
.filter(function (url) {
return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0
})
var nowPlaying
function addTorrent(magnet) {
if (client.torrents[0]) {
client.remove(client.torrents[0].infoHash)
}
// if (client.torrents[0]) {
// client.remove(client.torrents[0].infoHash)
// }
client.add(magnet, async function (torrent) {
const server = await torrent.createServer()
await server.listen()
await sw
function onProgress() {
peers.textContent = torrent.numPeers
downSpeed.textContent = prettyBytes(torrent.downloadSpeed) + '/s'
upSpeed.textContent = prettyBytes(torrent.uploadSpeed) + '/s'
}
torrent.on('download', onProgress)
torrent.on('upload', onProgress)
// torrent.on('warning', console.warn) // too spammy for now
@ -76,14 +77,6 @@ function addTorrent(magnet) {
fillType: ""
});
})
function onProgress() {
peers.textContent = torrent.numPeers
downSpeed.textContent = prettyBytes(torrent.downloadSpeed) + '/s'
upSpeed.textContent = prettyBytes(torrent.uploadSpeed) + '/s'
}
// TODO: load video after downloading torrent data
let videoFile = torrent.files[0]
torrent.files.forEach(file => {
if (file.length > videoFile.length) {
@ -91,156 +84,76 @@ function addTorrent(magnet) {
}
})
video.src = `/webtorrent/${torrent.infoHash}/${encodeURI(videoFile.path)}`
torrent.files[0].createReadStream().pipe(parser)
// torrent.files[0].createReadStream().pipe(parser)
document.location.href = "#player"
nowPlaying(selected)
halfmoon.toggleModal("tsearch")
})
}
function getPageHTML(title, pageHtml) {
return `<!DOCTYPE html><html><head><meta><title>${title}</title></head><body>${pageHtml}<body></html>`;
}
// 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) =>
`<li><a x_download="${file.name}" href="${registration.scope}webtorrent/${torrent.infoHash}/${file.path}">${file.path}</a> (${file.length} bytes)</li>`
).join('<br>')
const body = getPageHTML(
`${torrent.name} - WebTorrent`,
`<h1>${torrent.name}</h1><ol>${listHtml}</ol>`
)
return {
status: 200,
headers: {
'Content-Type': 'text/html'
},
body,
function serveFile(file, req) {
const res = {
status: 200,
headers: {
'Content-Type': file._getMimeType(),
// Support range-requests
'Accept-Ranges': 'bytes'
}
}
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, req.headers.get('range') || '')
// `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
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]
// 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
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
}
res.body = req.method === 'HEAD' ? '' : 'stream'
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)
return [res, req.method === 'GET' && file.createReadStream(range)]
}
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)
// kind of a fetch event from service worker but for the main thread.
navigator.serviceWorker.addEventListener('message', evt => {
const request = new Request(evt.data.url, {
headers: evt.data.headers,
method: evt.data.method
})
const res = {
async listen(port) {
const scope = `./`
res.scope = scope
const swReg = await navigator.serviceWorker.getRegistration(scope)
const swReg_1 = swReg || navigator.serviceWorker.register('sw.js', {
scope
})
registration = swReg_1
res.scope = registration.scope
res.registration = registration
let swRegTmp = swReg_1.installing || swReg_1.waiting
if (swReg_1.active)
return
return new Promise(rs => {
swRegTmp.onstatechange = () => {
if (swRegTmp.state === 'activated')
rs()
}
})
},
close() {
registration && registration.unregister()
}
const [port] = evt.ports
const respondWith = msg => port.postMessage(msg)
const pathname = request.url.split(evt.data.scope + 'webtorrent/')[1]
let [infoHash, ...filePath] = pathname.split('/')
filePath = decodeURI(filePath.join('/'))
if (!infoHash || !filePath) return
const torrent = client.get(infoHash)
const file = torrent.files.find(file => file.path === filePath)
const [response, stream] = serveFile(file, request)
const asyncIterator = stream && stream[Symbol.asyncIterator]()
respondWith(response)
async function pull() {
respondWith((await asyncIterator.next()).value)
}
return res
}
port.onmessage = pull
})
function prettyBytes(num) {
var exponent, unit, neg = num < 0, units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']