Skip to content
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

Add Access Handles to spec #21

Merged
merged 15 commits into from
Sep 29, 2022
50 changes: 6 additions & 44 deletions AccessHandle.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
- [New data access surface](#new-data-access-surface)
- [Locking semantics](#locking-semantics)
- [Open Questions](#open-questions)
- [Naming](#naming)
- [Assurances on non-awaited consistency](#assurances-on-non-awaited-consistency)
- [Trying It Out](#trying-it-out)
- [Appendix](#appendix)
- [AccessHandle IDL](#accesshandle-idl)
- [References & acknowledgements](#references--acknowledgements)
Expand Down Expand Up @@ -124,7 +124,7 @@ API](https://docs.google.com/document/d/1cOdnvuNIWWyJHz1uu8K_9DEgntMtedxfCzShI7d
// In all contexts
// For details on the `mode` parameter see "Exposing AccessHandles on all
// filesystems" below
const handle = await file.createAccessHandle({ mode: "in-place" });
const handle = await file.createAccessHandle();
await handle.writable.getWriter().write(buffer);
const reader = handle.readable.getReader({ mode: "byob" });
// Assumes seekable streams, and SharedArrayBuffer support are available
Expand Down Expand Up @@ -158,9 +158,9 @@ default reader and writer with a *seek()* method.
### Locking semantics

```javascript
const handle1 = await file.createAccessHandle({ mode: "in-place" });
const handle1 = await file.createAccessHandle();
try {
const handle2 = await file.createAccessHandle({ mode: "in-place" });
const handle2 = await file.createAccessHandle();
} catch (e) {
// This catch will always be executed, since there is an open access handle
}
Expand All @@ -184,36 +184,6 @@ observe changes done through the new API, even if a lock is still being held.

## Open Questions

### Naming

The exact name of the new methods hasn’t been defined. The current placeholder
for data access is *createAccessHandle()* and *createSyncAccessHandle()*.
*createUnflushedStreams()* and *createDuplexStream()* have been suggested.

### Exposing AccessHandles on all filesystems

This proposal only currently considers additions to OPFS, but it would probably
be worthwhile to expand the new functionality to arbitrary file handles. While
the exact behavior of *AccessHandles* outside of OPFS would need to be defined
in detail, it's almost certain that the one described in this proposal should
not be the default. To avoid setting it as such, we propose adding an optional
*mode* string parameter to *createAccessHandle()* and
*createSyncAccessHandle()*. Some possible values *mode* could take are:

* 'shared': The current behavior seen in File System Access API in general,
there is no locking and modifications are atomic (meaning that they would
only actually change the file when the *AccessHandle* is closed). This mode
would be a safe choice as a default value.
* 'exclusive': An exclusive write lock is taken on the file, but modifications
are still atomic. This is a useful mode for developers that want to
coordinate various writing threads but still want "all or nothing" writes.
* 'in-place': The behavior described in this proposal, allowing developers to
use high performance access to files at the cost of not having atomic writes.
It's possible that this mode would only be allowed in OPFS.

Both the naming and semantics of the *mode* parameter have to be more concretely
defined.

### Assurances on non-awaited consistency

It would be possible to clearly specify the behavior of an immediate async read
Expand Down Expand Up @@ -249,19 +219,11 @@ interface FileSystemFileHandle : FileSystemHandle {
Promise<File> getFile();
Promise<FileSystemWritableFileStream> createWritable(optional FileSystemCreateWritableOptions options = {});

Promise<FileSystemAccessHandle> createAccessHandle(optional FileSystemFileHandleCreateAccessHandleOptions options = {});
Promise<FileSystemAccessHandle> createAccessHandle();
[Exposed=DedicatedWorker]
Promise<FileSystemSyncAccessHandle> createSyncAccessHandle(optional FileSystemFileHandleCreateAccessHandleOptions options = {});
};

dictionary FileSystemFileHandleCreateAccessHandleOptions {
AccessHandleMode mode;
Promise<FileSystemSyncAccessHandle> createSyncAccessHandle();
};

// For more details and possible modes, see "Exposing AccessHandles on all
// filesystems" above
enum AccessHandleMode { "in-place" };

interface FileSystemAccessHandle {
// Assumes seekable streams are available. The
// Seekable extended attribute is ad-hoc notation for this proposal.
Expand Down
Loading