- Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
91 lines (66 loc) · 4.38 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
title: FileSystemSyncAccessHandle
slug: Web/API/FileSystemSyncAccessHandle
page-type: web-api-interface
browser-compat: api.FileSystemSyncAccessHandle
---
{{securecontext_header}}{{APIRef("File System API")}}{{AvailableInWorkers("dedicated")}}
The **`FileSystemSyncAccessHandle`** interface of the {{domxref("File System API", "File System API", "", "nocode")}} represents a synchronous handle to a file system entry.
This class is only accessible inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) (so that its methods do not block execution on the main thread) for files within the [origin private file system](/en-US/docs/Web/API/File_System_API/Origin_private_file_system), which is not visible to end-users.
As a result, its methods are not subject to the same security checks as methods running on files within the user-visible file system, and so are much more performant. This makes them suitable for significant, large-scale file updates such as [SQLite](https://sqlite.org/wasm) database modifications.
The interface is accessed through the {{domxref('FileSystemFileHandle.createSyncAccessHandle()')}} method.
> [!NOTE]
> In earlier versions of the spec, {{domxref("FileSystemSyncAccessHandle.close()", "close()")}}, {{domxref("FileSystemSyncAccessHandle.flush()", "flush()")}}, {{domxref("FileSystemSyncAccessHandle.getSize()", "getSize()")}}, and {{domxref("FileSystemSyncAccessHandle.truncate()", "truncate()")}} were wrongly specified as asynchronous methods, and older versions of some browsers implement them in this way. However, all current browsers that support these methods implement them as synchronous methods.
## Instance properties
None.
## Instance methods
- {{domxref('FileSystemSyncAccessHandle.close', 'close()')}}
- : 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.
- {{domxref('FileSystemSyncAccessHandle.flush', 'flush()')}}
- : Persists any changes made to the file associated with the handle via the {{domxref('FileSystemSyncAccessHandle.write', 'write()')}} method to disk.
- {{domxref('FileSystemSyncAccessHandle.getSize', 'getSize()')}}
- : Returns the size of the file associated with the handle in bytes.
- {{domxref('FileSystemSyncAccessHandle.read', 'read()')}}
- : Reads the content of the file associated with the handle into a specified buffer, optionally at a given offset.
- {{domxref('FileSystemSyncAccessHandle.truncate', 'truncate()')}}
- : Resizes the file associated with the handle to a specified number of bytes.
- {{domxref('FileSystemSyncAccessHandle.write', 'write()')}}
- : Writes the content of a specified buffer to the file associated with the handle, optionally at a given offset.
## 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
constmessage=e.data;
// Get handle to draft file
constroot=awaitnavigator.storage.getDirectory();
constdraftHandle=awaitroot.getFileHandle("draft.txt", { create:true });
// Get sync access handle
constaccessHandle=awaitdraftHandle.createSyncAccessHandle();
// Get size of the file.
constfileSize=accessHandle.getSize();
// Read file content to a buffer.
constbuffer=newDataView(newArrayBuffer(fileSize));
constreadBuffer=accessHandle.read(buffer, { at:0 });
// Write the message to the end of the file.
constencoder=newTextEncoder();
constencodedMessage=encoder.encode(message);
constwriteBuffer=accessHandle.write(encodedMessage, { at: readBuffer });
// Persist changes to disk.
accessHandle.flush();
// Always close FileSystemSyncAccessHandle if done.
accessHandle.close();
};
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
-[File System API](/en-US/docs/Web/API/File_System_API)
-[The File System Access API: simplifying access to local files](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access)