|
| 1 | +/* global self, ReadableStream, Response */ |
| 2 | + |
| 3 | +self.addEventListener('install', () => { |
| 4 | + self.skipWaiting() |
| 5 | +}) |
| 6 | + |
| 7 | +self.addEventListener('activate', event => { |
| 8 | + event.waitUntil(self.clients.claim()) |
| 9 | +}) |
| 10 | + |
| 11 | +const map = new Map() |
| 12 | + |
| 13 | +self.onmessage = event => { |
| 14 | + if (event.data === 'ping') { |
| 15 | + if (event.waitUntil) { |
| 16 | + event.waitUntil(Promise.resolve()) |
| 17 | + } |
| 18 | + |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + const data = event.data |
| 23 | + const downloadUrl = data.url || self.registration.scope + Math.random() + '/' + (typeof data === 'string' ? data : data.filename) |
| 24 | + const port = event.ports[0] |
| 25 | + const metadata = new Array(3) |
| 26 | + |
| 27 | + const startDownload = () => { |
| 28 | + map.set(downloadUrl, metadata) |
| 29 | + port.postMessage({ download: downloadUrl }) |
| 30 | + } |
| 31 | + |
| 32 | + metadata[1] = data |
| 33 | + metadata[2] = port |
| 34 | + |
| 35 | + if (event.data.readableStream) { |
| 36 | + metadata[0] = event.data.readableStream |
| 37 | + startDownload() |
| 38 | + } else if (event.data.transferringReadable) { |
| 39 | + port.onmessage = event => { |
| 40 | + port.onmessage = null |
| 41 | + metadata[0] = event.data.readableStream |
| 42 | + startDownload() |
| 43 | + } |
| 44 | + } else { |
| 45 | + metadata[0] = createStream(port) |
| 46 | + startDownload() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function createStream (port) { |
| 51 | + return new ReadableStream({ |
| 52 | + start (controller) { |
| 53 | + port.onmessage = ({ data }) => { |
| 54 | + if (data === 'end') { |
| 55 | + return controller.close() |
| 56 | + } |
| 57 | + |
| 58 | + if (data === 'abort') { |
| 59 | + controller.error('Aborted the download') |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + controller.enqueue(data) |
| 64 | + } |
| 65 | + }, |
| 66 | + cancel (reason) { |
| 67 | + console.log('user aborted', reason) |
| 68 | + port.postMessage({ abort: true }) |
| 69 | + } |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +self.onfetch = event => { |
| 74 | + const url = event.request.url |
| 75 | + |
| 76 | + if (url.endsWith('/ping')) { |
| 77 | + return event.respondWith(new Response('pong')) |
| 78 | + } |
| 79 | + |
| 80 | + const metadata = map.get(url) |
| 81 | + |
| 82 | + if (!metadata) { |
| 83 | + return null |
| 84 | + } |
| 85 | + |
| 86 | + const [stream, data, port] = metadata |
| 87 | + |
| 88 | + map.delete(url) |
| 89 | + |
| 90 | + const responseHeaders = new Headers({ |
| 91 | + 'Content-Type': 'application/octet-stream; charset=utf-8', |
| 92 | + 'Content-Security-Policy': "default-src 'none'", |
| 93 | + 'X-Content-Security-Policy': "default-src 'none'", |
| 94 | + 'X-WebKit-CSP': "default-src 'none'", |
| 95 | + 'X-XSS-Protection': '1; mode=block' |
| 96 | + }) |
| 97 | + |
| 98 | + const headers = new Headers(data.headers || {}) |
| 99 | + |
| 100 | + if (headers.has('Content-Length')) { |
| 101 | + responseHeaders.set('Content-Length', headers.get('Content-Length')) |
| 102 | + } |
| 103 | + |
| 104 | + if (headers.has('Content-Disposition')) { |
| 105 | + responseHeaders.set('Content-Disposition', headers.get('Content-Disposition')) |
| 106 | + } |
| 107 | + |
| 108 | + event.respondWith(new Response(stream, { headers: responseHeaders })) |
| 109 | + |
| 110 | + port.postMessage({ debug: 'Download started' }) |
| 111 | +} |
0 commit comments