-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add opfs
driver
#319
base: main
Are you sure you want to change the base?
Conversation
β Live Preview ready!
|
An additional thought about naming: I explained my decision to stick with the To this date, I have not had any involvement with any serverless environments, but I am aware that those are increasingly modeled after standardized Web APIs. So, somebody with more exposure to this topic should probably check if any established cloud provider uses the File System API in their services already. In that case, it may be worth reconsidering the naming of this driver (and possibly also stop defaulting to the OPFS and instead improve the description of the EDIT: The more I think about this, the more I'm convinced that we probably should name this driver "File System API" or something similar, despite the ambiguities. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments but really nice work β€οΈ π―
Re naming, i will be make some more research to give feedback on naming once back from holidays. But don't worry we can always iterate to introduce new aliases. Current name is good to me! |
Alright, then let's stick with the name for now. I've dealt with the change requests, so let me know when there's anything else I can help with. Also, enjoy your holidays. π |
Hey all. Is there anything I can help with to finish this one? :) |
Hi! First, I want to save the name of the directory chosen by the user. The only way to do that is to manage the async function requestNativeBrowserFSPermission(dirHandle: FileSystemDirectoryHandle) {
const opts: any = {}
opts.writable = true
// For Chrome 86 and later...
opts.mode = 'readwrite'
const perms = await dirHandle.requestPermission(opts)
return perms === 'granted'
}
let fs
if (source.base === 'directoryPicker') {
fs = await window.showDirectoryPicker()
if (!(await requestNativeBrowserFSPermission(fs))) return
} else {
const dir = await navigator.storage.getDirectory()
fs = await dir.getDirectoryHandle(source.base, { create: true })
if (!(await requestNativeBrowserFSPermission(fs))) return
}
storage.mount(`opfs:${fs.name}`, opfsDriver({ fs }))
return fs.name The first time, I don't know where I'm wrong. Have you got an idea on this ? |
π Linked issue
Resolves #231
β Type of change
π Description
This PR adds a driver for the browser-based origin private file system, as requested in #231.
Strictly speaking,
opfs
is not an exactly accurate name as the driver actually supports any file system following the File System API. This PR sticks to theopfs
naming for two reasons:Of these, only the OPFS is supported across browser engines and is therefore the default file system used by this driver.
The driver and its tests are modeled quite closely after the
fs-lite
driver, especially with regards to the options and the tests.Some key differences are:
The
base
OptionThe
base
option is available, yet not required.Other than with Node's
fs
module, it's safe for the File System API to operate on its root directory since it only exists in a sandboxed area of the device's file system.While namespacing with
base
should be considered a best practice (which is why it's also used in the documentation example), requiring it can be a footgun, especially when working with alternative file systems.The
fs
OptionAs described above, this driver can work with file systems other than the OPFS.
Therefore, there is the additional
fs
option which can be passed an alternativeFileSystemDirectoryHandle
(or a Promise resolving to one).One way to use this would be to work with the user's real file system in supported browsers.
A naΓ―ve way to use that option would look like this:
Of course, this code is silly as it has no checks for browser support or other safe guards, but technically, it works.
The
readOnly
andnoClear
OptionsThe
readOnly
andnoClear
options have been carried over from thefs
/fs-lite
drivers, but other than with those, they have also been added to the documentation.I'm not sure whether they are currently missing from the Node FS driver docs or whether they've been omitted on purpose.
If so, please request a change to drop them from the OPFS documentation as well.
Different
hasItem
BehaviorThe
hasItem
method returnsfalse
when the target path exists but is a directory.To be fair, I consider this a bug in the
fs
/fs-lite
drivers. Since a directory can not be accessed withgetItem
, it should not report to exist viahasItem
either.Substitutions for Node Built-ins
Since the driver should run in the browser, it uses naΓ―ve reimplementations of some
node:path
functions used infs-lite
. They are not as sophisticated as the ones provided by Node, but they should easily be up to the task of handling path-like keys while being a lot leaner in size.π Checklist