diff --git a/sw-stream-cancellation/index.html b/sw-stream-cancellation/index.html index 8f6a8bf..2255c72 100644 --- a/sw-stream-cancellation/index.html +++ b/sw-stream-cancellation/index.html @@ -1,42 +1,112 @@ - - ServiceWorker - Response.body cancellation test + + ServiceWorker - Response.body cancellation test + + +

Test progress is outputted to console

+ Related chromium issue: + 638494 + - Response.body cancellation should be notified to the service worker. +
+ View source + on GitHub +

+ - - - -

Test progress is outputted to console

- Related chromium issue: 638494 - Response.body cancellation should be notified to the service worker.
- View source on GitHub diff --git a/sw-stream-cancellation/sw.js b/sw-stream-cancellation/sw.js index 6084020..de16396 100644 --- a/sw-stream-cancellation/sw.js +++ b/sw-stream-cancellation/sw.js @@ -1,52 +1,33 @@ -self.addEventListener('install', event => { - console.log('[SW] install event fired') -}); +let readable; -self.addEventListener('activate', event => { - console.log('[SW] activate event fired') - event.waitUntil(clients.claim()) +self.addEventListener('message', async (event) => { + readable = event.data; + console.log(event); + event.source.postMessage('ServiceWorker ready to serve download'); }); self.addEventListener('fetch', (event) => { - let url = new URL(event.request.url) - if (url.pathname === '/get-file') { - let interval - let counter = 0 - let cancelNotification = false - let initialCancelReason = false - let rs = new ReadableStream({ - start(c) { - interval = setInterval( () => { - if (counter > 10) { - console.log('[SW] FAIL: Stream must be already closed, but still not') - clearInterval(interval) - c.close() - return - } - console.log('[SW] Stream: enqueue chunk', counter) - try { - c.enqueue( new Uint8Array([counter++]) ) - } catch(e) { - if (cancelNotification) { - console.log('[SW] OK: Stream has been closed with prior notification about cancellation') - if (!initialCancelReason) - console.log('[SW] WARN: Stream cancellation reason is not matched to initial') - } - else - console.log('[SW] FAIL: Stream has been closed without prior notification about cancellation') - clearInterval(interval) - } - }, 2000) - }, - cancel(reason) { - console.log('[SW] Stream cancelled with reason:', reason) - if (reason === url.pathname) - initialCancelReason = true - cancelNotification = true - } + console.log(event); + let url = new URL(event.request.url); + if (url.pathname.includes('get-file')) { + console.log({ readable }); + const headers = { + 'content-disposition': 'attachment; filename="filename.txt"', + }; + event.respondWith( + new Response(readable, { + headers, }) - let r = new Response(rs) - event.respondWith( Promise.resolve(r) ) - } else - event.respondWith( fetch(event.request) ) + ); + } +}); + +self.addEventListener('install', (event) => { + console.log(event); + event.waitUntil(self.skipWaiting()); +}); + +self.addEventListener('activate', (event) => { + console.log(event); + event.waitUntil(self.clients.claim()); });