diff --git a/files/en-us/web/api/file_system_api/origin_private_file_system/index.md b/files/en-us/web/api/file_system_api/origin_private_file_system/index.md
index f73e8a6be27f944..7319b34e72656ed 100644
--- a/files/en-us/web/api/file_system_api/origin_private_file_system/index.md
+++ b/files/en-us/web/api/file_system_api/origin_private_file_system/index.md
@@ -179,7 +179,7 @@ size = accessHandle.getSize();
const dataView = new DataView(new ArrayBuffer(size));
// Read the entire file into the data view.
-accessHandle.read(dataView);
+accessHandle.read(dataView, { at: 0 });
// Logs `"Some textMore content"`.
console.log(textDecoder.decode(dataView));
diff --git a/files/en-us/web/api/filesystemfilehandle/createsyncaccesshandle/index.md b/files/en-us/web/api/filesystemfilehandle/createsyncaccesshandle/index.md
index 7ee8bfed17b8a30..5ad51df7c904db7 100644
--- a/files/en-us/web/api/filesystemfilehandle/createsyncaccesshandle/index.md
+++ b/files/en-us/web/api/filesystemfilehandle/createsyncaccesshandle/index.md
@@ -19,11 +19,24 @@ Creating a {{domxref('FileSystemSyncAccessHandle')}} takes an exclusive lock on
```js-nolint
createSyncAccessHandle()
+createSyncAccessHandle(options)
```
### Parameters
-None.
+- `options` {{optional_inline}}
+
+ - : An object with the following properties:
+
+ - `mode` {{optional_inline}} {{non-standard_inline}}
+ - : A string specifying the locking mode for the access handle. The default value is `"readwrite"`.
+ Possible values are:
+ - `"read-only"`
+ - : Multiple `FileSystemSyncAccessHandle` objects can be opened simultaneously on a file (for example when using the same app in multiple tabs), provided they are all opened in `"read-only"` mode. Once opened, read-like methods can be called on the handles — {{domxref("FileSystemSyncAccessHandle.read", "read()")}}, {{domxref("FileSystemSyncAccessHandle.getSize", "getSize()")}}, and {{domxref("FileSystemSyncAccessHandle.close", "close()")}}.
+ - `"readwrite"`
+ - : Only one `FileSystemSyncAccessHandle` object can be opened on a file. Attempting to open subsequent handles before the first handle is closed results in a `NoModificationAllowedError` exception being thrown. Once opened, any available method can be called on the handle.
+ - `"readwrite-unsafe"`
+ - : Multiple `FileSystemSyncAccessHandle` objects can be opened simultaneously on a file, provided they are all opened in `"readwrite-unsafe"` mode. Once opened, any available method can be called on the handles.
### Return value
@@ -38,10 +51,12 @@ A {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemSyncAccessHandl
- `NotFoundError` {{domxref("DOMException")}}
- : Thrown if current entry is not found.
- `NoModificationAllowedError` {{domxref("DOMException")}}
- - : Thrown if the browser is not able to acquire a lock on the file associated with the file handle.
+ - : Thrown if the browser is not able to acquire a lock on the file associated with the file handle. This could be because `mode` is set to `readwrite` and an attempt is made to open multiple handles simultaneously.
## Examples
+### Basic usage
+
The following asynchronous event handler function is contained inside a Web Worker. The snippet inside it creates a synchronous file access handle.
```js
@@ -62,6 +77,139 @@ onmessage = async (e) => {
};
```
+### Complete example with `mode` option
+
+Our [`createSyncAccessHandle()` mode test](https://createsyncaccesshandle-mode-test.glitch.me/) example provides an {{htmlelement("input")}} field to enter text into, and two buttons — one to write entered text to the end of a file in the origin private file system, and one to empty the file when it becomes too full.
+
+Try exploring the demo above, with the browser developer console open so you can see what is happening. If you try opening the demo in multiple browser tabs, you will find that multiple handles can be opened at once to write to the file at the same time. This is because `mode: "readwrite-unsafe"` is set on the `createSyncAccessHandle()` calls.
+
+Below we'll explore the code.
+
+#### HTML
+
+The two {{htmlelement("button")}} elements and text {{htmlelement("input")}} field look like this:
+
+```html
+
+
+
+
+
+
+ Write your text to the file:
+
+
+ Empty the file if it gets too full:
+
+
+
+```
+
+#### Main JavaScript
+
+The main thread JavaScript inside the HTML file is shown below. We grab references to the write text button, empty file button, and text input field, then we create a new web worker using the {{domxref("Worker.Worker", "Worker()")}} constructor. We then define two functions and set them as event handlers on the buttons:
+
+- `writeToOPFS()` is run when the write text button is clicked. This function posts the entered value of the text field to the worker inside an object using the {{domxref("Worker.postMessage()")}} method, then empties the text field, ready for the next addition. Note how the passed object also includes a `command: "write"` property to specify that we want to trigger a write action with this message.
+- `emptyOPFS()` is run when the empty file button is clicked. This posts an object containing a `command: "empty"` property to the worker specifying that the file is to be emptied.
+
+```js
+const writeBtn = document.querySelector(".write");
+const emptyBtn = document.querySelector(".empty");
+const fileText = document.querySelector("#filetext");
+
+const opfsWorker = new Worker("worker.js");
+
+function writeToOPFS() {
+ opfsWorker.postMessage({
+ command: "write",
+ content: fileText.value,
+ });
+ console.log("Main script: Text posted to worker");
+ fileText.value = "";
+}
+
+function emptyOPFS() {
+ opfsWorker.postMessage({
+ command: "empty",
+ });
+}
+
+writeBtn.addEventListener("click", writeToOPFS);
+emptyBtn.addEventListener("click", emptyOPFS);
+```
+
+#### Worker JavaScript
+
+The worker JavaScript is shown below.
+
+First, we run a function called `initOPFS()` that gets a reference to the OPFS root using {{domxref("StorageManager.getDirectory()")}}, creates a file and returns its handle using {{domxref("FileSystemDirectoryHandle.getFileHandle()")}}, and then returns a {{domxref("FileSystemSyncAccessHandle")}} using `createSyncAccessHandle()`. This call includes the `mode: "readwrite-unsafe"` property, allowing multiple handles to access the same file simultaneously.
+
+```js
+let accessHandle;
+
+async function initOPFS() {
+ const opfsRoot = await navigator.storage.getDirectory();
+ const fileHandle = await opfsRoot.getFileHandle("file.txt", { create: true });
+ accessHandle = await fileHandle.createSyncAccessHandle({
+ mode: "readwrite-unsafe",
+ });
+}
+
+initOPFS();
+```
+
+Inside the worker's [message event](/en-US/docs/Web/API/Worker/message_event) handler function, we first get the size of the file using {{domxref("FileSystemSyncAccessHandle.getSize", "getSize()")}}. We then check to see whether the data sent in the message includes a `command` property value of `"empty"`. If so, we empty the file using {{domxref("FileSystemSyncAccessHandle.truncate", "truncate()")}} with a value of `0`, and update the file size contained in the `size` variable.
+
+If the message data is something else, we:
+
+- Create a new {{domxref("TextEncoder")}} and {{domxref("TextDecoder")}} to handle encoding and decoding the text content later on.
+- Encode the message data and write the result to the end of the file using {{domxref("FileSystemSyncAccessHandle.write", "write()")}}, then update the file size contained in the `size` variable.
+- Create a {{domxref("DataView")}} to contain the file contents, and read the content into it using {{domxref("FileSystemSyncAccessHandle.read", "read()")}}.
+- Decode the `DataView` contents and log it to the console.
+
+```js
+onmessage = function (e) {
+ console.log("Worker: Message received from main script");
+
+ // Get the current size of the file
+ let size = accessHandle.getSize();
+
+ if (e.data.command === "empty") {
+ // Truncate the file to 0 bytes
+ accessHandle.truncate(0);
+
+ // Get the current size of the file
+ size = accessHandle.getSize();
+ } else {
+ const textEncoder = new TextEncoder();
+ const textDecoder = new TextDecoder();
+
+ // Encode content to write to the file
+ const content = textEncoder.encode(e.data.content);
+ // Write the content at the end of the file
+ accessHandle.write(content, { at: size });
+
+ // Get the current size of the file
+ size = accessHandle.getSize();
+
+ // Prepare a data view of the length of the file
+ const dataView = new DataView(new ArrayBuffer(size));
+
+ // Read the entire file into the data view
+ accessHandle.read(dataView, { at: 0 });
+
+ // Log the current file contents to the console
+ console.log("File contents: " + textDecoder.decode(dataView));
+
+ // Flush the changes
+ accessHandle.flush();
+ }
+
+ // Log the size of the file to the console
+ console.log("Size: " + size);
+};
+```
+
## Specifications
{{Specifications}}
diff --git a/files/en-us/web/api/filesystemfilehandle/createwritable/index.md b/files/en-us/web/api/filesystemfilehandle/createwritable/index.md
index b2f134bf2ceb623..b9ec49a28d7dbb1 100644
--- a/files/en-us/web/api/filesystemfilehandle/createwritable/index.md
+++ b/files/en-us/web/api/filesystemfilehandle/createwritable/index.md
@@ -31,6 +31,13 @@ createWritable(options)
- : A {{jsxref('Boolean')}}. Default `false`.
When set to `true` if the file exists, the existing file is first copied to the temporary file.
Otherwise the temporary file starts out empty.
+ - `mode` {{optional_inline}} {{non-standard_inline}}
+ - : A string specifying the locking mode for the writable file stream. The default value is `"siloed"`.
+ Possible values are:
+ - `"exclusive"`
+ - : Only one `FileSystemWritableFileStream` writer can be opened. Attempting to open subsequent writers before the first writer is closed results in a `NoModificationAllowedError` exception being thrown.
+ - `"siloed"`
+ - : Multiple `FileSystemWritableFileStream` writers can be opened at the same time, each with its own swap file, for example when using the same app in multiple tabs. The last writer opened has its data written, as the data gets flushed when each writer is closed.
### Return value
@@ -43,12 +50,14 @@ A {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemWritableFileStr
- `NotFoundError` {{domxref("DOMException")}}
- : Thrown if current entry is not found.
- `NoModificationAllowedError` {{domxref("DOMException")}}
- - : Thrown if the browser is not able to acquire a lock on the file associated with the file handle.
+ - : Thrown if the browser is not able to acquire a lock on the file associated with the file handle. This could be because `mode` is set to `exclusive` and an attempt is made to open multiple writers simultaneously.
- `AbortError` {{domxref("DOMException")}}
- : Thrown if implementation-defined malware scans and safe-browsing checks fails.
## Examples
+### Basic usage
+
The following asynchronous function writes the given contents to the file handle, and thus to disk.
```js
@@ -64,6 +73,121 @@ async function writeFile(fileHandle, contents) {
}
```
+### Expanded usage with options
+
+Our [`createWritable()` mode test](https://createwritable-mode-test.glitch.me/) example provides a {{htmlelement("button")}} to select a file to write to, a text {{htmlelement("input")}} field into which you can enter some text to write to the file, and a second `