sw.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* global self ReadableStream Response */
  2. self.addEventListener('install', () => {
  3. self.skipWaiting()
  4. })
  5. self.addEventListener('activate', event => {
  6. event.waitUntil(self.clients.claim())
  7. })
  8. const map = new Map()
  9. // This should be called once per download
  10. // Each event has a dataChannel that the data will be piped through
  11. self.onmessage = event => {
  12. // We send a heartbeat every x second to keep the
  13. // service worker alive if a transferable stream is not sent
  14. if (event.data === 'ping') {
  15. return
  16. }
  17. const data = event.data
  18. const downloadUrl = data.url || Math.random() + '/' + (typeof data === 'string' ? data : data.filename)
  19. const port = event.ports[0]
  20. const metadata = new Array(3) // [stream, data, port]
  21. metadata[1] = data
  22. metadata[2] = port
  23. // Note to self:
  24. // old streamsaver v1.2.0 might still use `readableStream`...
  25. // but v2.0.0 will always transfer the stream through MessageChannel #94
  26. if (event.data.readableStream) {
  27. metadata[0] = event.data.readableStream
  28. } else if (event.data.transferringReadable) {
  29. port.onmessage = evt => {
  30. port.onmessage = null
  31. metadata[0] = evt.data.readableStream
  32. }
  33. } else {
  34. metadata[0] = createStream(port)
  35. }
  36. map.set(downloadUrl, metadata)
  37. port.postMessage({ download: downloadUrl })
  38. }
  39. function createStream (port) {
  40. // ReadableStream is only supported by chrome 52
  41. return new ReadableStream({
  42. start (controller) {
  43. // When we receive data on the messageChannel, we write
  44. port.onmessage = ({ data }) => {
  45. if (data === 'end') {
  46. return controller.close()
  47. }
  48. if (data === 'abort') {
  49. controller.error('Aborted the download')
  50. return
  51. }
  52. controller.enqueue(data)
  53. }
  54. },
  55. cancel (reason) {
  56. console.log('user aborted', reason)
  57. port.postMessage({ abort: true })
  58. }
  59. })
  60. }
  61. self.onfetch = event => {
  62. const url = event.request.url
  63. // this only works for Firefox
  64. if (url.endsWith('/ping')) {
  65. return event.respondWith(new Response('pong'))
  66. }
  67. const hijacke = map.get(url)
  68. if (!hijacke) return null
  69. const [ stream, data, port ] = hijacke
  70. map.delete(url)
  71. // Not comfortable letting any user control all headers
  72. // so we only copy over the length & disposition
  73. const responseHeaders = new Headers({
  74. 'Content-Type': 'application/octet-stream; charset=utf-8',
  75. // To be on the safe side, The link can be opened in a iframe.
  76. // but octet-stream should stop it.
  77. 'Content-Security-Policy': "default-src 'none'",
  78. 'X-Content-Security-Policy': "default-src 'none'",
  79. 'Cross-Origin-Embedder-Policy': 'require-corp',
  80. 'X-WebKit-CSP': "default-src 'none'",
  81. 'X-XSS-Protection': '1; mode=block'
  82. })
  83. let headers = new Headers(data.headers || {})
  84. if (headers.has('Content-Length')) {
  85. responseHeaders.set('Content-Length', headers.get('Content-Length'))
  86. }
  87. if (headers.has('Content-Disposition')) {
  88. responseHeaders.set('Content-Disposition', headers.get('Content-Disposition'))
  89. }
  90. // data, data.filename and size should not be used anymore
  91. if (data.size) {
  92. console.warn('Depricated')
  93. responseHeaders.set('Content-Length', data.size)
  94. }
  95. let fileName = typeof data === 'string' ? data : data.filename
  96. if (fileName) {
  97. console.warn('Depricated')
  98. // Make filename RFC5987 compatible
  99. fileName = encodeURIComponent(fileName).replace(/['()]/g, escape).replace(/\*/g, '%2A')
  100. responseHeaders.set('Content-Disposition', "attachment; filename*=UTF-8''" + fileName)
  101. }
  102. event.respondWith(new Response(stream, { headers: responseHeaders }))
  103. port.postMessage({ debug: 'Download started' })
  104. }