Skip to content

Commit

Permalink
Fix for chunks on external domains
Browse files Browse the repository at this point in the history
  • Loading branch information
pldubouilh committed Feb 20, 2018
1 parent 5559634 commit 829f839
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
5 changes: 2 additions & 3 deletions client/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ console.logNoisy = (msg, color) => !(new Date().getTime() % 12) && console.logCo

navigator.serviceWorker.register('sw.js').then(() => {
navigator.serviceWorker.addEventListener('message', msg => {
return msg.data.ab ? newSeed(msg.data.url, msg.data.ab) : newTorrent(msg.data.magnets)
return msg.data.ab ? newSeed(msg.data.name, msg.data.ab) : newTorrent(msg.data.magnets)
})
}).catch(() => console.log('SW registration failure'))

Expand Down Expand Up @@ -64,8 +64,7 @@ function newTorrent (magnets) {
})
}

function newSeed (url, ab) {
const name = url.match(/\w+\d+(\.ts)/g)[0]
function newSeed (name, ab) {
console.logColor(`+ Server loaded ${name} - seeding content now`, 'cadetblue')

if (isTorrentAdded(name)) {
Expand Down
5 changes: 2 additions & 3 deletions client/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ console.logNoisy = (msg, color) => !(new Date().getTime() % 12) && console.logCo

navigator.serviceWorker.register('sw.js').then(() => {
navigator.serviceWorker.addEventListener('message', msg => {
return msg.data.ab ? newSeed(msg.data.url, msg.data.ab) : newTorrent(msg.data.magnets)
return msg.data.ab ? newSeed(msg.data.name, msg.data.ab) : newTorrent(msg.data.magnets)
})
}).catch(() => console.log('SW registration failure'))

Expand Down Expand Up @@ -62,8 +62,7 @@ function newTorrent (magnets) {
})
}

function newSeed (url, ab) {
const name = url.match(/\w+\d+(\.ts)/g)[0]
function newSeed (name, ab) {
console.logColor(`+ Server loaded ${name} - seeding content now`, 'cadetblue')

if (isTorrentAdded(name)) {
Expand Down
27 changes: 14 additions & 13 deletions client/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ async function msgClient (id, msg) {
const c = await self.clients.get(id)
if (!c) return
const chan = new MessageChannel()
if (msg.ab) {
c.postMessage(msg, [chan.port2, msg.ab])
} else {
c.postMessage(msg, [chan.port2])
}
c.postMessage(msg, msg.ab ? [chan.port2, msg.ab] : [chan.port2])
}

const chunkName = url => url.match(/\w+\d+(\.ts)/g)[0]

self.addEventListener('message', event => {
// Data from main thread ! Just store AB on look up table
const { url, ab } = event.data
localCache[location.origin + '/' + url] = ab // TODO: fix location. Use chunkname instead of path
console.log('- Received p2p data from main thread data for ' + url)
const name = chunkName(url)

localCache[name] = ab
console.log('- Received p2p data from main thread data for ' + name)

// Clean LUT if needed
localCacheOrder.push(url)
localCacheOrder.push(name)
if (localCacheOrder.length > 5) {
const pastUrl = localCacheOrder.shift()
delete localCache[pastUrl]
const pastName = localCacheOrder.shift()
delete localCache[pastName]
}
})

Expand Down Expand Up @@ -55,14 +55,15 @@ async function loadManifest (req, url, id) {

async function loadChunk (req, url, id) {
// Request has already been fetched by p2p !
if (localCache[url]) {
return new Response(localCache[url])
const name = chunkName(url)
if (localCache[name]) {
return new Response(localCache[name])
}

// If not prefetched, go fetch it. Message the arraybuffer back to main thread for seeding.
const res = await fetch(req)
const ab = await res.clone().arrayBuffer()
msgClient(id, { url, ab })
msgClient(id, { name, ab })
return res
}

Expand Down

0 comments on commit 829f839

Please sign in to comment.