jimmywarting / native-file-system-adapter

File system, based on the spec reference implementation
https://jimmywarting.github.io/native-file-system-adapter/example/test.html
MIT License
496 stars 45 forks source link

feat: replace prefer polyfill with preferred methods #60

Open jimmywarting opened 1 year ago

jimmywarting commented 1 year ago

I sometimes get some confusing question on github issues as i most often have to guess what method of choices they use in order to save the files.

there are currently 3 method in place, 1) native 2) service worker 3) building a blob So it gets a bit hard to trying figuring out the path it takes.

so i want to switch out _preferPolyfill: false to preferedMethods

const fileHandle = await showSaveFilePicker({
- _preferPolyfill: false
+ _preferedMethods: [
+   'native', // req native support for window.showSaveFilePicker
+   'service-worker',  // requires installing a service worker to allow streaming large files
+   'constructing-blob'  // easiest/oldest method that req. more RAM but is fine for smaller file
+ ],
  suggestedName: "Untitled.png",
  types: [
    { accept: { "image/png": ["png"] } },
    { accept: { "image/jpg": ["jpg"] } },
    { accept: { "image/webp": ["webp"] } },
  ],
  excludeAcceptAllOption: false, // default
});

Perhaps later then we could maybe even throw in dropbox and google drive methods into the mix then if something like that ever comes up

Originally posted by @jimmywarting in https://github.com/jimmywarting/native-file-system-adapter/issues/59#issuecomment-1533145174

i want service-worker to be explicitly opted out as it requires more things to be set up in order to work correctly. and i also want this field to be required and not fallback to any default methods. so _preferedMethods will be a required field. But that would maybe perhaps be a too breaking change. perhaps maybe just do a console.warn() and set the default to using ['native', 'constructing-blob']

A problem with not using prefered method are that the service worker solution might not just work if folks install a service worker. cuz we only do feature detection by the occurrences if a service worker is installed or not. not by the fact that they have implemented the push algoritm.

jimmywarting commented 1 year ago

I maybe also even want to switch up the service worker to two solutions: using transferable streams or just the MessageChannel solution

The transferable stream don't require the service worker to stay alive and pinging it with postMessages.

so maybe: ['native', 'sw-transferable-stream', 'sw-message-channel', 'constructing-blob']