jimmywarting / StreamSaver.js

StreamSaver writes stream to the filesystem directly asynchronous
https://jimmywarting.github.io/StreamSaver.js/example.html
MIT License
3.97k stars 413 forks source link

can't start a download offline #273

Open OmerDavidson opened 2 years ago

OmerDavidson commented 2 years ago

Hi, I am trying out the example from the readme and I just can't for the life of me get it to work offline. I am not seeing any popup or any download starting.

I am using http (but i am on localhost so there is secure context for now). I am hosting mitm on my own. I put mitm.html and sw.js on my public folder. I also registered the service worker.

This is the code:

import streamSaver from 'streamsaver'
streamSaver.mitm = '/mitm.html'

(async () => {
  console.log('start')
  const uInt8 = new TextEncoder().encode('StreamSaver is awesome')

  const fileStream = streamSaver.createWriteStream('filename.txt', {
    size: uInt8.byteLength,
    writableStrategy: undefined, 
    readableStrategy: undefined 
  })

  const writer = fileStream.getWriter()
  await writer.write(uInt8)
  await writer.close()
  console.log('end')
})()

both 'start' and 'end' are being printed.

streamsaver version: 2.0.6 Chrome version: 94

whitersa commented 10 months ago

I guess you no longer pay attention to this issue,just show my solution for others who may encounter this condition if you are using vue, you may use “register-service-worker” , you just need to copy mitm.html and sw.js to your public folder and modify mitm.html

            .getRegistration('./')
            .then(swReg => {
                if (swReg.active.scriptURL.includes('sw')) {
                    return swReg;
                }
                return navigator.serviceWorker.register('sw.js', { scope: './' });
            })

i guess before code getRegistration('./').then(swReg => { return swReg || navigator.serviceWorker.register('sw.js', { scope: './' }) will always return vue default register-service-worker