diff options
-rw-r--r-- | client-web/source/resource/file.ts | 32 | ||||
-rw-r--r-- | client-web/source/sw/download_stream.ts | 14 | ||||
-rw-r--r-- | client-web/source/sw/worker.ts | 17 |
3 files changed, 40 insertions, 23 deletions
diff --git a/client-web/source/resource/file.ts b/client-web/source/resource/file.ts index 2b4c7fc..97da70b 100644 --- a/client-web/source/resource/file.ts +++ b/client-web/source/resource/file.ts @@ -31,15 +31,30 @@ export const resource_file: ResourceHandlerDecl = { on_statechange(_s) { }, on_enable(channel, disable) { if (!(channel instanceof RTCDataChannel)) throw new Error("not a data channel"); - const download = StreamDownload( - info.size!, info.label ?? "file", - position => { - display.status = `${display_filesize(position)} / ${display_filesize(info.size!)}` - } - ); const display = transfer_status_el() this.el.appendChild(display.el) + const reset = () => { + download_button.disabled = false + download_button.textContent = "Download again" + this.el.removeChild(display.el) + disable() + } + + const download = StreamDownload( + { + size: info.size!, + filename: info.label ?? "file", + progress(position) { + display.status = `${display_filesize(position)} / ${display_filesize(info.size!)}` + }, + cancel() { + channel.close() + log({ scope: "*", warn: true }, "download stream aborted") + reset() + } + } + ); channel.onopen = _ev => { log("dc", `${user.display_name}: channel open`); @@ -49,11 +64,8 @@ export const resource_file: ResourceHandlerDecl = { } channel.onclose = _ev => { log("dc", `${user.display_name}: channel closed`); - this.el.removeChild(display.el) download.close() - download_button.disabled = false - download_button.textContent = "Download" - disable() + reset() } channel.onmessage = ev => { // console.log(ev.data); diff --git a/client-web/source/sw/download_stream.ts b/client-web/source/sw/download_stream.ts index 1a2fec8..0ba6289 100644 --- a/client-web/source/sw/download_stream.ts +++ b/client-web/source/sw/download_stream.ts @@ -26,7 +26,12 @@ function FallbackStreamDownload(size: number, filename?: string, progress?: (pos } } -export function StreamDownload(size: number, filename?: string, progress?: (position: number) => void) { +export function StreamDownload({ size, filename, cancel, progress }: { + size: number, + filename: string, + cancel: () => void, + progress: (position: number) => void +}) { if (!SW) FallbackStreamDownload(size, filename, progress) let position = 0 @@ -41,6 +46,13 @@ export function StreamDownload(size: number, filename?: string, progress?: (posi a.target = "_blank" a.click() + port1.onmessage = ev => { + if (ev.data.abort) { + cancel() + port1.close() + } + } + return { close() { port1.postMessage("end") diff --git a/client-web/source/sw/worker.ts b/client-web/source/sw/worker.ts index 5c185f3..327aba0 100644 --- a/client-web/source/sw/worker.ts +++ b/client-web/source/sw/worker.ts @@ -12,8 +12,6 @@ declare const self: ServiceWorkerGlobalScope; export { }; console.log("hello from the keks-meet service worker"); console.log(self.origin) -// let cache: Cache; - self.addEventListener("install", event => { console.log("install"); self.skipWaiting() @@ -22,13 +20,6 @@ self.addEventListener("install", event => { self.addEventListener("activate", _event => { console.log("activate"); self.clients.claim() - // event.waitUntil((async () => { - // cache = await caches.open("v1") - // cache.addAll([ - // "/assets/bundle.js", - // "/assets/sw.js", - // ]) - // })()) }) self.addEventListener("unload", () => { console.log("unload") @@ -72,14 +63,16 @@ self.addEventListener("fetch", event => { stream.readable, { headers: new Headers({ - "Content-Type": "application/octet-stream; charset=utf-8", // TODO transmit and set accordingly - "Content-Security-Policy": "default-src 'none'", - "Content-Length": `${stream.size}`, + "content-type": "application/octet-stream; charset=utf-8", // TODO transmit and set accordingly + "content-security-policy": "default-src 'none'", + "content-length": `${stream.size}`, }) } ) ) } + if (path == "/swtest") return event.respondWith(new Response("works!", { headers: new Headers({ "content-type": "text/plain" }) })) + event.respondWith(fetch(request)) }) |