-
Notifications
You must be signed in to change notification settings - Fork 22.6k
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 content for FileSystemSyncAccessHandle #21815
Merged
teoli2003
merged 21 commits into
mdn:main
from
chrisdavidmills:add-content-for-FileSystemSyncAccessHandle
Dec 5, 2022
+846
−10
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
97fca21
add content for FileSystemSyncAccessHandle
chrisdavidmills 13b8e5e
Update files/en-us/web/api/file_system_access_api/index.md
chrisdavidmills 1e5a833
Update files/en-us/web/api/file_system_access_api/index.md
chrisdavidmills 97e4ae4
Update files/en-us/web/api/file_system_access_api/index.md
chrisdavidmills 161b3d6
Making fix according to tomayac comment
chrisdavidmills cea2ec7
Merge branch 'add-content-for-FileSystemSyncAccessHandle' of github.c…
chrisdavidmills a52a0c6
fixing code examples according to tomayac comments
chrisdavidmills 0418aa8
Merge branch 'main' into add-content-for-FileSystemSyncAccessHandle
chrisdavidmills 705ab9c
Update files/en-us/web/api/filesystemfilehandle/createsyncaccesshandl…
chrisdavidmills d2b8610
Update files/en-us/web/api/filesystemfilehandle/createsyncaccesshandl…
chrisdavidmills 9ac69ce
Update files/en-us/web/api/filesystemfilehandle/index.md
chrisdavidmills f9d12bf
Update files/en-us/web/api/filesystemsyncaccesshandle/close/index.md
chrisdavidmills 5dd5ac6
Update files/en-us/web/api/filesystemsyncaccesshandle/flush/index.md
chrisdavidmills 514c0fd
Update files/en-us/web/api/filesystemsyncaccesshandle/getsize/index.md
chrisdavidmills 1685336
Update files/en-us/web/api/filesystemsyncaccesshandle/index.md
chrisdavidmills 8bdd5fa
Update files/en-us/web/api/filesystemsyncaccesshandle/read/index.md
chrisdavidmills b9b16df
Update files/en-us/web/api/filesystemsyncaccesshandle/read/index.md
chrisdavidmills 6c518da
Update files/en-us/web/api/filesystemsyncaccesshandle/write/index.md
chrisdavidmills 4551547
Respond to a-sully review comments and make some other fixes
chrisdavidmills 2a359bd
more fixes for a-sully comments
chrisdavidmills 66e92ef
Update files/en-us/web/api/file_system_access_api/index.md
teoli2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
files/en-us/web/api/filesystemfilehandle/createsyncaccesshandle/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
title: FileSystemFileHandle.createSyncAccessHandle() | ||
slug: Web/API/FileSystemFileHandle/createSyncAccessHandle | ||
page-type: web-api-instance-method | ||
tags: | ||
- Directory | ||
- File | ||
- File System Access API | ||
- FileSystemFileHandle | ||
- Method | ||
- createSyncAccessHandle | ||
- working with files | ||
browser-compat: api.FileSystemFileHandle.createSyncAccessHandle | ||
--- | ||
|
||
{{securecontext_header}}{{APIRef("File System Access API")}} | ||
|
||
The **`createSyncAccessHandle()`** method of the | ||
{{domxref("FileSystemFileHandle")}} interface returns a {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemSyncAccessHandle')}} object | ||
that can be used to synchronously read from and write to a file. The synchronous nature of this method brings performance advantages, | ||
but it is only usable inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) for files within the [origin private file system](https://fs.spec.whatwg.org/#origin-private-file-system). | ||
|
||
Creating a {{domxref('FileSystemSyncAccessHandle')}} takes an exclusive lock on the file associated with the file handle. This prevents the creation of further {{domxref('FileSystemSyncAccessHandle')}}s or {{domxref('FileSystemWritableFileStream')}}s for the file until the existing access handle is closed. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
createSyncAccessHandle() | ||
``` | ||
|
||
### Parameters | ||
|
||
None. | ||
|
||
### Return value | ||
|
||
A {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemSyncAccessHandle')}} object. | ||
|
||
### Exceptions | ||
|
||
- `InvalidStateError` {{domxref("DOMException")}} | ||
- : Thrown if the {{domxref('FileSystemSyncAccessHandle')}} object does not represent a file in the [origin private file system](https://fs.spec.whatwg.org/#origin-private-file-system). | ||
- `NoModificationAllowedError` {{domxref("DOMException")}} | ||
- : Thrown if the browser is not able to acquire a lock on the file associated with the file handle. | ||
- `NotAllowedError` {{domxref("DOMException")}} | ||
- : Thrown if the permission has not been granted at the API level (i.e. {{domxref("FileSystemHandle.requestPermission")}} is required). | ||
|
||
## Examples | ||
|
||
The following asynchronous event handler function is contained inside a Web Worker. The snippet inside it creates a synchronous file access handle. | ||
|
||
```js | ||
onmessage = async (e) => { | ||
// Retrieve message sent to work from main script | ||
const message = e.data; | ||
|
||
// Get handle to draft file | ||
const root = await navigator.storage.getDirectory(); | ||
const draftHandle = await root.getFileHandle('draft.txt', { create: true }); | ||
// Get sync access handle | ||
const accessHandle = await draftHandle.createSyncAccessHandle(); | ||
|
||
// … | ||
|
||
// Always close FileSystemSyncAccessHandle if done. | ||
accessHandle.close(); | ||
} | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- [File System Access API](/en-US/docs/Web/API/File_System_Access_API) | ||
- [The File System Access API: simplifying access to local files](https://web.dev/file-system-access/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
files/en-us/web/api/filesystemsyncaccesshandle/close/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
title: FileSystemSyncAccessHandle.close() | ||
slug: Web/API/FileSystemSyncAccessHandle/close | ||
page-type: web-api-instance-method | ||
tags: | ||
- close | ||
- Directory | ||
- File | ||
- File System Access API | ||
- FileSystemSyncAccessHandle | ||
- Method | ||
- stream | ||
- working with files | ||
browser-compat: api.FileSystemSyncAccessHandle.close | ||
--- | ||
|
||
{{securecontext_header}}{{APIRef("File System Access API")}} | ||
|
||
The **`close()`** method of the | ||
{{domxref("FileSystemSyncAccessHandle")}} interface closes an open synchronous file handle, disabling any further operations on it and releasing the exclusive lock previously put on the file associated with the file handle. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
close() | ||
``` | ||
|
||
### Parameters | ||
|
||
None. | ||
|
||
### Return value | ||
|
||
A {{jsxref('Promise')}} which resolves to undefined. | ||
chrisdavidmills marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Exceptions | ||
|
||
None. | ||
|
||
## Examples | ||
|
||
The following asynchronous event handler function is contained inside a Web Worker. On receiving a message from the main thread it: | ||
|
||
- Creates a synchronous file access handle. | ||
- Gets the size of the file and creates an {{jsxref("ArrayBuffer")}} to contain it. | ||
- Reads the file contents into the buffer. | ||
- Encodes the message and writes it to the end of the file. | ||
- Persists the changes to disk and closes the access handle. | ||
|
||
```js | ||
onmessage = async (e) => { | ||
// Retrieve message sent to work from main script | ||
const message = e.data; | ||
|
||
// Get handle to draft file | ||
const root = await navigator.storage.getDirectory(); | ||
const draftHandle = await root.getFileHandle('draft.txt', { create: true }); | ||
// Get sync access handle | ||
const accessHandle = await draftHandle.createSyncAccessHandle(); | ||
|
||
// Get size of the file. | ||
const fileSize = accessHandle.getSize(); | ||
// Read file content to a buffer. | ||
const buffer = new DataView(new ArrayBuffer(fileSize)); | ||
const readBuffer = accessHandle.read(buffer, { at: 0 }); | ||
|
||
// Write the message to the end of the file. | ||
const encoder = new TextEncoder(); | ||
const encodedMessage = encoder.encode(message); | ||
const writeBuffer = accessHandle.write(encodedMessage, { at: readBuffer }); | ||
|
||
// Persist changes to disk. | ||
accessHandle.flush(); | ||
|
||
// Always close FileSystemSyncAccessHandle if done. | ||
accessHandle.close(); | ||
} | ||
``` | ||
|
||
> **Note:** In earlier versions of the spec, `close()`, {{domxref("FileSystemSyncAccessHandle.flush()", "flush()")}}, {{domxref("FileSystemSyncAccessHandle.getSize()", "getSize()")}}, and {{domxref("FileSystemSyncAccessHandle.truncate()", "truncate()")}} were wrongly specified as asynchronous methods. This has now been [amended](https://github.com/whatwg/fs/issues/7), but some browsers still support the asynchronous versions. | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- [File System Access API](/en-US/docs/Web/API/File_System_Access_API) | ||
- [The File System Access API: simplifying access to local files](https://web.dev/file-system-access/) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I put up whatwg/fs#57 a while back to try to better specify the exceptions this API throws. You're welcome to use that as a guide to expand this section
That being said, we can't quite guarantee that a particular error code maps to a specific action, since if the underlying OS provides an error when performing a file operation we often just map that to the closest
DOMException
(which may beNoModificationAllowedError
, for example, which we'd otherwise prefer to have reserved for file locking errors)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.
Thanks for the comments @a-sully !
So for this one, I've made a couple of changes to these descriptions based on your linked descriptions (see next commit). That's a really useful issue text! Let me know what you think.
I think defining what exceptions a feature may throw on MDN has always been tricky, especially if, as in this case, the OS may muddy the waters with its own exceptions. To figure out what exceptions to write about, I tend to just read the algorithm for the feature in the spec to see what exceptions are fired at each stage, do a bit of testing to try to observe behavior, and then write about what I find. Is there a better way, or have you got any tips for making this better?
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.
Understandable! Unfortunately I don't have any other suggestions... Is it reasonable to link to whatwg/fs#57?
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.
I think it is useful to link it, but I'm not sure where for best exposure. For now, I've added a note to the main API landing page "Concepts and usage" section, which links to it and explains what the issue is: