smol-rs / async-fs

Async filesystem primitives
Apache License 2.0
131 stars 8 forks source link

Web support #31

Open daxpedda opened 10 months ago

daxpedda commented 10 months ago

Would there be interest in adding support for Web through the File System API?

It would be pretty straightforward, as it's all natively async anyway and there would be no need to spawn any blocking threads. Probably the most interesting part is documentation and converting errors to io::Error.

All major browsers already support this, except Safari which doesn't support writing files in async, but only in sync.

Happy to make the PR of course!

notgull commented 10 months ago

I guess this falls under #24.

My main concern is how the File System API you linked would map onto the std::fs API. From a brief look at the API it looks like it doesn't map too well onto the file system of paths. I guess reading and writing would be relatively the same, but I'm concerned about how APIs like create_dir or metadata would map onto the web API.

I guess we could have a set of extension functions for opening the Files, having the Files open for reading and writing, and make all of the other APIs return "unsupported" errors. Does this make much sense?

daxpedda commented 10 months ago

I guess reading and writing would be relatively the same, but I'm concerned about how APIs like create_dir or metadata would map onto the web API.

I think we should use OPFS by default, see StorageManager.getDirectory(). This would allow us to implement create_dir() through FileSystemDirectoryHandle.getDirectoryHandle().

metadata is also mostly covered, but we would have to create our own type that mirrors Std's Metadata:

Will look into making a PR soon!