opfs-worker 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -27
- package/dist/{BaseOPFS-Cylz6ijk.js → BaseOPFS-BusBkyJX.js} +436 -358
- package/dist/BaseOPFS-BusBkyJX.js.map +1 -0
- package/dist/{BaseOPFS-BPw9AIQ-.cjs → BaseOPFS-CYwrD7-q.cjs} +4 -4
- package/dist/BaseOPFS-CYwrD7-q.cjs.map +1 -0
- package/dist/{OPFSAsync-s2P7y3sw.cjs → OPFSAsync-B2dR-2VR.cjs} +2 -2
- package/dist/{OPFSAsync-s2P7y3sw.cjs.map → OPFSAsync-B2dR-2VR.cjs.map} +1 -1
- package/dist/{OPFSAsync-BKSAJJeh.js → OPFSAsync-CO44W23e.js} +2 -2
- package/dist/{OPFSAsync-BKSAJJeh.js.map → OPFSAsync-CO44W23e.js.map} +1 -1
- package/dist/OPFSFacade-CEaSqTut.js +308 -0
- package/dist/OPFSFacade-CEaSqTut.js.map +1 -0
- package/dist/OPFSFacade-hrNXCn6w.cjs +2 -0
- package/dist/OPFSFacade-hrNXCn6w.cjs.map +1 -0
- package/dist/{OPFSSync-C7KBepe_.cjs → OPFSSync-BPrNtvFS.cjs} +2 -2
- package/dist/{OPFSSync-C7KBepe_.cjs.map → OPFSSync-BPrNtvFS.cjs.map} +1 -1
- package/dist/{OPFSSync-CsSQkpk5.js → OPFSSync-D7obTsl3.js} +2 -2
- package/dist/{OPFSSync-CsSQkpk5.js.map → OPFSSync-D7obTsl3.js.map} +1 -1
- package/dist/assets/dedicated.worker-DW6UqFo0.js.map +1 -0
- package/dist/core/BaseOPFS.d.ts +38 -7
- package/dist/core/BaseOPFS.d.ts.map +1 -1
- package/dist/createOPFSAsync-DdhJYoC0.js +10 -0
- package/dist/{createOPFSAsync-Y4Mg6qte.js.map → createOPFSAsync-DdhJYoC0.js.map} +1 -1
- package/dist/createOPFSAsync-DyWpvmL-.cjs +2 -0
- package/dist/{createOPFSAsync-BwKDmHsy.cjs.map → createOPFSAsync-DyWpvmL-.cjs.map} +1 -1
- package/dist/{createOPFSDedicated-CsnV6MLP.js → createOPFSDedicated-BwTc0NV5.js} +289 -215
- package/dist/{createOPFSDedicated-CsnV6MLP.js.map → createOPFSDedicated-BwTc0NV5.js.map} +1 -1
- package/dist/{createOPFSDedicated-pKTRWLdz.cjs → createOPFSDedicated-CEeRexom.cjs} +286 -212
- package/dist/{createOPFSDedicated-pKTRWLdz.cjs.map → createOPFSDedicated-CEeRexom.cjs.map} +1 -1
- package/dist/{createOPFSShared-CGdd__PH.js → createOPFSShared-BSFWsoLe.js} +2 -2
- package/dist/{createOPFSShared-CGdd__PH.js.map → createOPFSShared-BSFWsoLe.js.map} +1 -1
- package/dist/{createOPFSShared-CskROt7U.cjs → createOPFSShared-BoCft_BS.cjs} +2 -2
- package/dist/{createOPFSShared-CskROt7U.cjs.map → createOPFSShared-BoCft_BS.cjs.map} +1 -1
- package/dist/dedicated.worker.js +282 -208
- package/dist/dedicated.worker.js.map +1 -1
- package/dist/facade/OPFSFacade.d.ts +21 -3
- package/dist/facade/OPFSFacade.d.ts.map +1 -1
- package/dist/index.async.cjs +1 -1
- package/dist/index.async.js +4 -4
- package/dist/index.cjs +1 -1
- package/dist/index.js +7 -7
- package/dist/index.pure.cjs +1 -1
- package/dist/index.pure.js +3 -3
- package/dist/index.sharedworker.cjs +1 -1
- package/dist/index.sharedworker.js +4 -4
- package/dist/index.sync.cjs +1 -1
- package/dist/index.sync.js +4 -4
- package/dist/shared.worker.js +430 -356
- package/dist/shared.worker.js.map +1 -1
- package/dist/types.d.ts +33 -0
- package/dist/types.d.ts.map +1 -1
- package/docs/api/backend.md +3 -3
- package/docs/api/create.md +1 -1
- package/docs/api/facade.md +28 -27
- package/docs/choosing-a-mode.md +6 -6
- package/docs/file-descriptors.md +46 -604
- package/docs/guides/async.md +2 -2
- package/docs/guides/dedicated.md +5 -5
- package/docs/guides/hashing.md +2 -2
- package/docs/guides/pure.md +3 -3
- package/docs/guides/sharedworker.md +1 -1
- package/docs/guides/streaming.md +145 -11
- package/docs/guides/watching.md +3 -3
- package/docs/migration.md +3 -3
- package/docs/types.md +92 -154
- package/package.json +12 -2
- package/src/core/BaseOPFS.ts +148 -25
- package/src/facade/OPFSFacade.ts +51 -4
- package/src/types.ts +37 -0
- package/dist/BaseOPFS-BPw9AIQ-.cjs.map +0 -1
- package/dist/BaseOPFS-Cylz6ijk.js.map +0 -1
- package/dist/OPFSFacade-DePnNRgt.cjs +0 -2
- package/dist/OPFSFacade-DePnNRgt.cjs.map +0 -1
- package/dist/OPFSFacade-XohWlHuu.js +0 -281
- package/dist/OPFSFacade-XohWlHuu.js.map +0 -1
- package/dist/assets/dedicated.worker-nY3tK4zi.js.map +0 -1
- package/dist/createOPFSAsync-BwKDmHsy.cjs +0 -2
- package/dist/createOPFSAsync-Y4Mg6qte.js +0 -10
- package/docs/README.md +0 -30
package/docs/file-descriptors.md
CHANGED
|
@@ -1,190 +1,66 @@
|
|
|
1
1
|
# File Descriptors
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Positional I/O on the **dedicated / sync** backend (`createOPFSDedicated` / `createOPFS`, or `OPFSSync` from `/pure`). Async and SharedWorker throw `ENOTSUP`.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Requirements
|
|
8
|
-
|
|
9
|
-
If you talk to **`fs.backend`** from the main thread for FD `read`/`write`, transfer buffers with Comlink:
|
|
5
|
+
If you call FD `read`/`write` on **`fs.backend`** from the main thread, transfer buffers with Comlink. The [facade](./api/facade.md) does that for you.
|
|
10
6
|
|
|
11
7
|
```typescript
|
|
12
8
|
import { transfer } from 'comlink';
|
|
13
9
|
```
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
## Overview
|
|
18
|
-
|
|
19
|
-
Useful when you need chunks, random access, or to avoid loading a whole file into memory.
|
|
20
|
-
|
|
21
|
-
## Table of Contents
|
|
22
|
-
|
|
23
|
-
- [Requirements](#requirements)
|
|
24
|
-
- [File Descriptor Basics](#file-descriptor-basics)
|
|
25
|
-
- [Opening Files](#opening-files)
|
|
26
|
-
- [Reading Data](#reading-data)
|
|
27
|
-
- [Writing Data](#writing-data)
|
|
28
|
-
- [File Positioning](#file-positioning)
|
|
29
|
-
- [File Operations](#file-operations)
|
|
30
|
-
- [Closing Files](#closing-files)
|
|
31
|
-
- [Error Handling](#error-handling)
|
|
32
|
-
- [Performance Considerations](#performance-considerations)
|
|
33
|
-
- [Examples](#examples)
|
|
34
|
-
|
|
35
|
-
## File Descriptor Basics
|
|
36
|
-
|
|
37
|
-
A file descriptor is a small integer that represents an open file. In OPFS Worker, file descriptors provide:
|
|
38
|
-
|
|
39
|
-
- **Sequential access**: Read/write operations that advance the file position
|
|
40
|
-
- **Random access**: Read/write at specific file positions
|
|
41
|
-
- **Efficient I/O**: Direct access to file data without intermediate buffers
|
|
42
|
-
- **Resource management**: Automatic cleanup when closed
|
|
43
|
-
|
|
44
|
-
### File Descriptor Lifecycle
|
|
45
|
-
|
|
46
|
-
1. **Open**: Create a file descriptor with `open()`
|
|
47
|
-
2. **Use**: Perform read/write operations
|
|
48
|
-
3. **Close**: Release the file descriptor with `close()`
|
|
49
|
-
|
|
50
|
-
## Opening Files
|
|
51
|
-
|
|
52
|
-
### `open(path: string, options?: FileOpenOptions): Promise<number>`
|
|
53
|
-
|
|
54
|
-
Opens a file and returns a file descriptor number.
|
|
11
|
+
## `open(path, options?): Promise<number>`
|
|
55
12
|
|
|
56
13
|
```typescript
|
|
57
|
-
// Basic file opening
|
|
58
14
|
const fd = await fs.open('/data/config.json');
|
|
59
|
-
|
|
60
|
-
// Create new file if it doesn't exist
|
|
61
15
|
const fd = await fs.open('/data/new.txt', { create: true });
|
|
62
|
-
|
|
63
|
-
// Create file exclusively (fails if exists)
|
|
64
16
|
const fd = await fs.open('/data/unique.txt', { create: true, exclusive: true });
|
|
65
|
-
|
|
66
|
-
// Open and truncate file to zero length
|
|
67
17
|
const fd = await fs.open('/data/log.txt', { create: true, truncate: true });
|
|
68
18
|
```
|
|
69
19
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
- `options.truncate` (optional): Whether to truncate the file to zero length (default: `false`)
|
|
76
|
-
|
|
77
|
-
**Returns:** `Promise<number>` - File descriptor number
|
|
20
|
+
| Option | Default | |
|
|
21
|
+
| ------ | ------- | --- |
|
|
22
|
+
| `create` | `false` | Create the file if missing |
|
|
23
|
+
| `exclusive` | `false` | With `create`, fail if the file already exists |
|
|
24
|
+
| `truncate` | `false` | Truncate to zero length |
|
|
78
25
|
|
|
79
|
-
|
|
26
|
+
Throws `AlreadyExistsError` (`exclusive`), `FileTypeError` (path is a directory), or `FileSystemOperationError`.
|
|
80
27
|
|
|
81
|
-
|
|
82
|
-
- `FileTypeError` if path is a directory
|
|
83
|
-
- `FileSystemOperationError` for other failures
|
|
28
|
+
`exclusive` is best-effort only — two workers can still race. OPFS has no real file locking.
|
|
84
29
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
## Reading Data
|
|
88
|
-
|
|
89
|
-
### `read(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number | null): Promise<{bytesRead: number, buffer: Uint8Array}>`
|
|
90
|
-
|
|
91
|
-
Reads data from a file descriptor into a buffer.
|
|
30
|
+
## `read(fd, buffer, offset, length, position?): Promise<{ bytesRead, buffer }>`
|
|
92
31
|
|
|
93
32
|
```typescript
|
|
94
|
-
const fd = await fs.open('/data/file.txt');
|
|
95
|
-
|
|
96
|
-
// Read 1024 bytes starting at current position
|
|
97
33
|
const buffer = new Uint8Array(1024);
|
|
98
|
-
const
|
|
99
|
-
console.log(`Read ${result.bytesRead} bytes`);
|
|
100
|
-
|
|
101
|
-
// Read 512 bytes at specific position
|
|
102
|
-
const buffer2 = new Uint8Array(512);
|
|
103
|
-
const result2 = await fs.read(fd, buffer2, 0, 512, 1000);
|
|
104
|
-
console.log(`Read ${result2.bytesRead} bytes starting at position 1000`);
|
|
105
|
-
|
|
106
|
-
await fs.close(fd);
|
|
34
|
+
const { bytesRead, buffer: out } = await fs.read(fd, buffer, 0, 1024, null);
|
|
107
35
|
```
|
|
108
36
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
- `
|
|
112
|
-
- `buffer`: The buffer to read data into
|
|
113
|
-
- `offset`: Offset in the buffer to start writing at
|
|
114
|
-
- `length`: Number of bytes to read
|
|
115
|
-
- `position`: Position in file to read from (`null` for current position, `undefined` for current position)
|
|
116
|
-
|
|
117
|
-
**Returns:** `Promise<{bytesRead: number, buffer: Uint8Array}>` - Object containing bytes read and modified buffer
|
|
118
|
-
|
|
119
|
-
**⚠️ Node.js Compatibility Note**: While similar to Node.js `fs.read()`, the returned `buffer` may be a **different object** due to `Comlink.transfer()`. Unlike Node.js where the same buffer object is modified in-place, here you must use the returned buffer for safety.
|
|
120
|
-
|
|
121
|
-
**⚠️ CRITICAL WARNING**: The `read()` method uses `Comlink.transfer()` for efficient buffer handling across Web Worker boundaries. This requires **bidirectional transfer**:
|
|
122
|
-
|
|
123
|
-
1. **From main window to worker**: You must transfer buffer ownership using `transfer(buffer, [buffer.buffer])`
|
|
124
|
-
2. **From worker to main window**: The worker transfers the modified buffer back to you
|
|
125
|
-
|
|
126
|
-
**🚨 NEVER USE THE ORIGINAL BUFFER AFTER TRANSFER** - it becomes detached and unusable. **ALWAYS use the buffer returned from the method**. Using the original buffer will result in silent data corruption or errors.
|
|
37
|
+
- `position` `null` / `undefined` — current cursor, then advance
|
|
38
|
+
- `position` number — that offset, cursor unchanged
|
|
39
|
+
- Returns `bytesRead === 0` at EOF
|
|
127
40
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
**Behavior:**
|
|
131
|
-
|
|
132
|
-
- Returns `0` when reaching end of file
|
|
133
|
-
- Automatically advances file position if `position` is `null` or `undefined`
|
|
134
|
-
- Reads up to `length` bytes, but may read fewer if end of file is reached
|
|
135
|
-
- Throws error if reading beyond file bounds
|
|
136
|
-
|
|
137
|
-
### Buffer Transfer Usage
|
|
138
|
-
|
|
139
|
-
**Important**: When using `read()` from the main window, you must transfer buffer ownership to the worker. The worker then transfers the modified buffer back to you.
|
|
140
|
-
|
|
141
|
-
#### From Main Window (Browser)
|
|
41
|
+
With a Comlink proxy, buffers are transferred both ways. Use the **returned** buffer; the original is detached after transfer.
|
|
142
42
|
|
|
143
43
|
```typescript
|
|
144
44
|
import { transfer } from 'comlink';
|
|
145
45
|
|
|
146
|
-
// Create buffer in main window
|
|
147
46
|
const buffer = new Uint8Array(64);
|
|
148
|
-
|
|
149
|
-
// Transfer buffer ownership to worker when calling read()
|
|
150
|
-
const { bytesRead, buffer: modifiedBuffer } = await fs.read(
|
|
47
|
+
const { bytesRead, buffer: out } = await fs.read(
|
|
151
48
|
fd,
|
|
152
|
-
transfer(buffer, [buffer.buffer]),
|
|
49
|
+
transfer(buffer, [buffer.buffer]),
|
|
153
50
|
0,
|
|
154
51
|
buffer.length,
|
|
155
52
|
null
|
|
156
53
|
);
|
|
157
|
-
|
|
158
|
-
// Now modifiedBuffer is "alive" again in main window
|
|
159
|
-
console.log('bytesRead =', bytesRead);
|
|
160
|
-
|
|
161
|
-
// Work directly with the returned buffer
|
|
162
|
-
const text = new TextDecoder().decode(modifiedBuffer.subarray(0, bytesRead));
|
|
163
|
-
console.log('read:', text);
|
|
164
|
-
|
|
165
|
-
// Note: The original 'buffer' is now detached and cannot be used
|
|
166
|
-
// Always use 'modifiedBuffer' from the result
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
#### From Worker (Direct Usage)
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
// When using OPFSSync directly in a worker
|
|
173
|
-
const buffer = new Uint8Array(64);
|
|
174
|
-
const { bytesRead, buffer: modifiedBuffer } = await fs.read(fd, buffer, 0, buffer.length, null);
|
|
175
|
-
|
|
176
|
-
// modifiedBuffer contains the read data
|
|
177
|
-
const data = modifiedBuffer.subarray(0, bytesRead);
|
|
178
54
|
```
|
|
179
55
|
|
|
180
|
-
|
|
56
|
+
Inside a worker with `OPFSSync` directly, plain buffers are fine — no `transfer`.
|
|
181
57
|
|
|
182
|
-
|
|
58
|
+
Chunked read from the main thread:
|
|
183
59
|
|
|
184
60
|
```typescript
|
|
185
61
|
const fd = await fs.open('/data/large-file.txt');
|
|
186
62
|
const chunkSize = 1024;
|
|
187
|
-
let buffer = new Uint8Array(chunkSize);
|
|
63
|
+
let buffer = new Uint8Array(chunkSize);
|
|
188
64
|
|
|
189
65
|
try {
|
|
190
66
|
while (true) {
|
|
@@ -195,14 +71,8 @@ try {
|
|
|
195
71
|
chunkSize,
|
|
196
72
|
null
|
|
197
73
|
);
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
// Process the chunk
|
|
202
|
-
const chunk = result.buffer.subarray(0, result.bytesRead);
|
|
203
|
-
processChunk(chunk);
|
|
204
|
-
|
|
205
|
-
// Create new buffer for next iteration (previous one was transferred)
|
|
74
|
+
if (result.bytesRead === 0) break;
|
|
75
|
+
processChunk(result.buffer.subarray(0, result.bytesRead));
|
|
206
76
|
buffer = new Uint8Array(chunkSize);
|
|
207
77
|
}
|
|
208
78
|
} finally {
|
|
@@ -210,486 +80,58 @@ try {
|
|
|
210
80
|
}
|
|
211
81
|
```
|
|
212
82
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
```typescript
|
|
216
|
-
const fd = await fs.open('/data/file.txt');
|
|
217
|
-
const buffer = new Uint8Array(100);
|
|
218
|
-
|
|
219
|
-
// Read 100 bytes starting at position 500
|
|
220
|
-
const result = await fs.read(
|
|
221
|
-
fd,
|
|
222
|
-
transfer(buffer, [buffer.buffer]),
|
|
223
|
-
0,
|
|
224
|
-
100,
|
|
225
|
-
500
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
console.log(`Read ${result.bytesRead} bytes from position 500`);
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
## Writing Data
|
|
232
|
-
|
|
233
|
-
### `write(fd: number, buffer: Uint8Array, offset?: number, length?: number, position?: number | null): Promise<number>`
|
|
83
|
+
Prefer small chunks over allocating a buffer the size of the whole file.
|
|
234
84
|
|
|
235
|
-
|
|
85
|
+
## `write(fd, buffer, offset?, length?, position?): Promise<number>`
|
|
236
86
|
|
|
237
87
|
```typescript
|
|
238
|
-
const fd = await fs.open('/data/output.txt', { create: true });
|
|
239
|
-
|
|
240
|
-
// Write entire buffer at current position
|
|
241
88
|
const data = new TextEncoder().encode('Hello, World!');
|
|
242
89
|
const bytesWritten = await fs.write(fd, data);
|
|
243
|
-
console.log(`Wrote ${bytesWritten} bytes`);
|
|
244
|
-
|
|
245
|
-
// Write portion of buffer at specific position
|
|
246
|
-
const data2 = new TextEncoder().encode('Additional text');
|
|
247
90
|
const bytesWritten2 = await fs.write(fd, data2, 0, 10, 100);
|
|
248
|
-
console.log(`Wrote ${bytesWritten2} bytes at position 100`);
|
|
249
|
-
|
|
250
|
-
await fs.close(fd);
|
|
251
91
|
```
|
|
252
92
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
- `fd`: File descriptor to write to
|
|
256
|
-
- `buffer`: Buffer containing data to write
|
|
257
|
-
- `offset` (optional): Offset in buffer to start reading from (default: `0`)
|
|
258
|
-
- `length` (optional): Number of bytes to write (default: entire buffer from offset)
|
|
259
|
-
- `position` (optional): Position in file to write to (`null`/`undefined` for current position)
|
|
260
|
-
|
|
261
|
-
**Returns:** `Promise<number>` - Number of bytes actually written
|
|
262
|
-
|
|
263
|
-
**Behavior:**
|
|
93
|
+
Same `position` rules as `read`. Extends the file if you write past EOF. Triggers watch events.
|
|
264
94
|
|
|
265
|
-
|
|
266
|
-
- Extends file size if writing beyond current end
|
|
267
|
-
- Triggers file change notifications for watching systems
|
|
268
|
-
|
|
269
|
-
## File Positioning
|
|
270
|
-
|
|
271
|
-
File descriptors maintain a current position that advances with read/write operations. You can control positioning through the `position` parameter:
|
|
95
|
+
## Positioning
|
|
272
96
|
|
|
273
97
|
```typescript
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const buffer1 = new Uint8Array(10);
|
|
278
|
-
const result1 = await fs.read(fd, buffer1, 0, 10, null); // Position advances to 10
|
|
279
|
-
|
|
280
|
-
// Read at current position (continues from 10)
|
|
281
|
-
const buffer2 = new Uint8Array(10);
|
|
282
|
-
const result2 = await fs.read(fd, buffer2, 0, 10, null); // Position advances to 20
|
|
283
|
-
|
|
284
|
-
// Read at specific position (doesn't change current position)
|
|
285
|
-
const buffer3 = new Uint8Array(10);
|
|
286
|
-
const result3 = await fs.read(fd, buffer3, 0, 10, 0); // Reads from beginning, position stays at 20
|
|
287
|
-
|
|
288
|
-
await fs.close(fd);
|
|
98
|
+
await fs.read(fd, buf, 0, 10, null); // cursor 0 → 10
|
|
99
|
+
await fs.read(fd, buf, 0, 10, null); // cursor 10 → 20
|
|
100
|
+
await fs.read(fd, buf, 0, 10, 0); // reads at 0, cursor stays 20
|
|
289
101
|
```
|
|
290
102
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
- **`null`**: Use current position, then advance it
|
|
294
|
-
- **`undefined`**: Use current position, then advance it
|
|
295
|
-
- **`number`**: Use specified position, don't change current position
|
|
296
|
-
|
|
297
|
-
## File Operations
|
|
298
|
-
|
|
299
|
-
### `fstat(fd: number): Promise<FileStat>`
|
|
300
|
-
|
|
301
|
-
Get file status information by file descriptor.
|
|
103
|
+
## `fstat` / `ftruncate` / `fsync`
|
|
302
104
|
|
|
303
105
|
```typescript
|
|
304
|
-
const fd = await fs.open('/data/file.txt');
|
|
305
106
|
const stats = await fs.fstat(fd);
|
|
306
|
-
|
|
307
|
-
console.log(`File size: ${stats.size} bytes`);
|
|
308
|
-
console.log(`Last modified: ${stats.mtime}`);
|
|
309
|
-
console.log(`Is file: ${stats.isFile}`);
|
|
310
|
-
|
|
311
|
-
// If hashing is enabled, hash will be included
|
|
312
|
-
if (stats.hash) {
|
|
313
|
-
console.log(`Hash: ${stats.hash}`);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
await fs.close(fd);
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
**Returns:** `Promise<FileStat>` - File statistics object
|
|
320
|
-
|
|
321
|
-
### `ftruncate(fd: number, size: number = 0): Promise<void>`
|
|
322
|
-
|
|
323
|
-
Truncate file to specified size.
|
|
324
|
-
|
|
325
|
-
```typescript
|
|
326
|
-
const fd = await fs.open('/data/file.txt', { create: true });
|
|
327
|
-
|
|
328
|
-
// Write some data
|
|
329
|
-
const data = new TextEncoder().encode('Hello, World!');
|
|
330
|
-
await fs.write(fd, data);
|
|
331
|
-
|
|
332
|
-
// Truncate to 5 bytes
|
|
333
107
|
await fs.ftruncate(fd, 5);
|
|
334
|
-
|
|
335
|
-
// File now contains only "Hello"
|
|
336
|
-
const buffer = new Uint8Array(10);
|
|
337
|
-
const result = await fs.read(fd, buffer, 0, 10, 0);
|
|
338
|
-
console.log(new TextDecoder().decode(result.buffer.subarray(0, result.bytesRead))); // "Hello"
|
|
339
|
-
|
|
340
|
-
await fs.close(fd);
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
**Parameters:**
|
|
344
|
-
|
|
345
|
-
- `fd`: File descriptor
|
|
346
|
-
- `size`: New file size in bytes (default: `0`)
|
|
347
|
-
|
|
348
|
-
**Behavior:**
|
|
349
|
-
|
|
350
|
-
- Adjusts current position if it's beyond the new file size
|
|
351
|
-
- Triggers file change notifications
|
|
352
|
-
- Automatically flushes changes to storage
|
|
353
|
-
|
|
354
|
-
### `fsync(fd: number): Promise<void>`
|
|
355
|
-
|
|
356
|
-
Synchronize file data to storage (equivalent to POSIX `fsync`).
|
|
357
|
-
|
|
358
|
-
```typescript
|
|
359
|
-
const fd = await fs.open('/data/critical.txt', { create: true });
|
|
360
|
-
|
|
361
|
-
// Write critical data
|
|
362
|
-
const data = new TextEncoder().encode('Important information');
|
|
363
|
-
await fs.write(fd, data);
|
|
364
|
-
|
|
365
|
-
// Ensure data is written to storage
|
|
366
108
|
await fs.fsync(fd);
|
|
367
|
-
|
|
368
|
-
await fs.close(fd);
|
|
369
109
|
```
|
|
370
110
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
**Use Cases:**
|
|
111
|
+
`ftruncate` clamps the cursor if it sits past the new size, emits a watch event, and flushes.
|
|
374
112
|
|
|
375
|
-
-
|
|
376
|
-
- Database transaction commits
|
|
377
|
-
- Log file integrity
|
|
378
|
-
- Critical file operations
|
|
113
|
+
`fsync` is best-effort. Unlike POSIX, the browser does not guarantee durability on power loss.
|
|
379
114
|
|
|
380
|
-
##
|
|
115
|
+
## `close(fd)`
|
|
381
116
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
Closes a file descriptor and releases associated resources.
|
|
117
|
+
Always close in a `finally`. Pending writes are flushed; open FDs are also closed when the worker is disposed.
|
|
385
118
|
|
|
386
119
|
```typescript
|
|
387
120
|
const fd = await fs.open('/data/file.txt');
|
|
388
|
-
|
|
389
|
-
// Use the file descriptor
|
|
390
|
-
const buffer = new Uint8Array(1024);
|
|
391
|
-
await fs.read(fd, buffer, 0, 1024, null);
|
|
392
|
-
|
|
393
|
-
// Always close when done
|
|
394
|
-
await fs.close(fd);
|
|
395
|
-
```
|
|
396
|
-
|
|
397
|
-
**Important Notes:**
|
|
398
|
-
|
|
399
|
-
- **Always close file descriptors** when you're done with them
|
|
400
|
-
- Unclosed file descriptors consume system resources
|
|
401
|
-
- The file system automatically flushes pending writes before closing
|
|
402
|
-
- File descriptors are automatically closed when the worker is disposed
|
|
403
|
-
|
|
404
|
-
## Error Handling
|
|
405
|
-
|
|
406
|
-
File descriptor operations can fail for various reasons. Always handle errors appropriately:
|
|
407
|
-
|
|
408
|
-
```typescript
|
|
409
121
|
try {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
try {
|
|
413
|
-
const buffer = new Uint8Array(1024);
|
|
414
|
-
const result = await fs.read(fd, buffer, 0, 1024, null);
|
|
415
|
-
console.log(`Read ${result.bytesRead} bytes`);
|
|
416
|
-
} finally {
|
|
417
|
-
// Always close, even if read fails
|
|
418
|
-
await fs.close(fd);
|
|
419
|
-
}
|
|
420
|
-
} catch (error) {
|
|
421
|
-
if (error instanceof OPFSError) {
|
|
422
|
-
switch (error.code) {
|
|
423
|
-
case 'EEXIST':
|
|
424
|
-
console.error('File already exists');
|
|
425
|
-
break;
|
|
426
|
-
case 'EISDIR':
|
|
427
|
-
console.error('Path is a directory');
|
|
428
|
-
break;
|
|
429
|
-
default:
|
|
430
|
-
console.error('File operation failed:', error.message);
|
|
431
|
-
}
|
|
432
|
-
} else {
|
|
433
|
-
console.error('Unexpected error:', error);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
**Common Error Types:**
|
|
439
|
-
|
|
440
|
-
- `AlreadyExistsError`: File already exists (with `exclusive: true`)
|
|
441
|
-
- `ExistenceError`: File not found
|
|
442
|
-
- `FileTypeError`: Path is directory when file expected (or vice versa)
|
|
443
|
-
- `ValidationError`: Invalid arguments (offset, length, etc.)
|
|
444
|
-
- `FileSystemOperationError`: General file system operation failures
|
|
445
|
-
- `IOError`: I/O operation failures
|
|
446
|
-
- `PermissionError`: Permission denied
|
|
447
|
-
|
|
448
|
-
## Performance Considerations
|
|
449
|
-
|
|
450
|
-
### 🚨 **CRITICAL MEMORY WARNING**
|
|
451
|
-
|
|
452
|
-
**⚠️ FOR LARGE FILES - ALWAYS READ IN CHUNKS, NEVER READ THE ENTIRE FILE AT ONCE!**
|
|
453
|
-
|
|
454
|
-
**Reading large files in one operation can crash the browser tab due to memory exhaustion.** Browsers have limited memory per tab, and creating massive buffers (hundreds of MB or GB) will cause:
|
|
455
|
-
|
|
456
|
-
- **Tab crashes**
|
|
457
|
-
- **Browser freezes**
|
|
458
|
-
- **Out of memory errors**
|
|
459
|
-
- **Poor user experience**
|
|
460
|
-
|
|
461
|
-
**✅ GOOD**: `const buffer = new Uint8Array(8192); // 8KB chunks`
|
|
462
|
-
**❌ BAD**: `const buffer = new Uint8Array(fileSize); // Entire file!`
|
|
463
|
-
|
|
464
|
-
### Buffer Management
|
|
465
|
-
|
|
466
|
-
Reuse buffers when possible to reduce memory allocation:
|
|
467
|
-
|
|
468
|
-
```typescript
|
|
469
|
-
const fd = await fs.open('/data/large-file.txt');
|
|
470
|
-
const buffer = new Uint8Array(8192); // 8KB buffer
|
|
471
|
-
|
|
472
|
-
let totalBytes = 0;
|
|
473
|
-
let bytesRead;
|
|
474
|
-
|
|
475
|
-
// Read file in chunks using the same buffer
|
|
476
|
-
while (true) {
|
|
477
|
-
const result = await fs.read(fd, buffer, 0, buffer.length, null);
|
|
478
|
-
if (result.bytesRead === 0) break;
|
|
479
|
-
|
|
480
|
-
totalBytes += result.bytesRead;
|
|
481
|
-
// Process result.buffer.subarray(0, result.bytesRead)
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
await fs.close(fd);
|
|
485
|
-
```
|
|
486
|
-
|
|
487
|
-
### Batch Operations
|
|
488
|
-
|
|
489
|
-
Group related operations together:
|
|
490
|
-
|
|
491
|
-
```typescript
|
|
492
|
-
const fd = await fs.open('/data/log.txt', { create: true });
|
|
493
|
-
|
|
494
|
-
// Batch multiple writes
|
|
495
|
-
const entries = [
|
|
496
|
-
'User login',
|
|
497
|
-
'Data processed',
|
|
498
|
-
'Operation completed'
|
|
499
|
-
];
|
|
500
|
-
|
|
501
|
-
for (const entry of entries) {
|
|
502
|
-
const data = new TextEncoder().encode(entry + '\n');
|
|
503
|
-
await fs.write(fd, data);
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
// Single sync at the end
|
|
507
|
-
await fs.fsync(fd);
|
|
508
|
-
await fs.close(fd);
|
|
509
|
-
```
|
|
510
|
-
|
|
511
|
-
### File Descriptor Limits
|
|
512
|
-
|
|
513
|
-
Monitor the number of open file descriptors:
|
|
514
|
-
|
|
515
|
-
```typescript
|
|
516
|
-
// Check how many files are open
|
|
517
|
-
console.log(`Open files: ${fs.openFiles.size}`);
|
|
518
|
-
|
|
519
|
-
// Close unused file descriptors promptly
|
|
520
|
-
const unusedFds = [];
|
|
521
|
-
// ... track unused file descriptors ...
|
|
522
|
-
for (const fd of unusedFds) {
|
|
122
|
+
// …
|
|
123
|
+
} finally {
|
|
523
124
|
await fs.close(fd);
|
|
524
125
|
}
|
|
525
126
|
```
|
|
526
127
|
|
|
527
|
-
##
|
|
528
|
-
|
|
529
|
-
### Reading Large Files in Chunks
|
|
530
|
-
|
|
531
|
-
```typescript
|
|
532
|
-
async function readLargeFile(filePath: string, chunkSize: number = 8192) {
|
|
533
|
-
const fd = await fs.open(filePath);
|
|
534
|
-
const chunks: Uint8Array[] = [];
|
|
535
|
-
|
|
536
|
-
try {
|
|
537
|
-
while (true) {
|
|
538
|
-
// Create new buffer for each iteration (previous one was transferred)
|
|
539
|
-
const buffer = new Uint8Array(chunkSize);
|
|
540
|
-
|
|
541
|
-
const result = await fs.read(fd, buffer, 0, chunkSize, null);
|
|
542
|
-
if (result.bytesRead === 0) break;
|
|
543
|
-
|
|
544
|
-
// Copy the chunk data from the returned buffer
|
|
545
|
-
const chunk = new Uint8Array(result.bytesRead);
|
|
546
|
-
chunk.set(result.buffer.subarray(0, result.bytesRead));
|
|
547
|
-
chunks.push(chunk);
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
return chunks;
|
|
551
|
-
} finally {
|
|
552
|
-
await fs.close(fd);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
// Usage
|
|
557
|
-
const chunks = await readLargeFile('/data/large-file.dat', 16384);
|
|
558
|
-
console.log(`Read ${chunks.length} chunks`);
|
|
559
|
-
```
|
|
560
|
-
|
|
561
|
-
### Binary File Processing
|
|
562
|
-
|
|
563
|
-
```typescript
|
|
564
|
-
async function processBinaryFile(filePath: string) {
|
|
565
|
-
const fd = await fs.open(filePath);
|
|
566
|
-
|
|
567
|
-
try {
|
|
568
|
-
// Read header (first 16 bytes)
|
|
569
|
-
const headerBuffer = new Uint8Array(16);
|
|
570
|
-
const headerResult = await fs.read(fd, headerBuffer, 0, 16, 0);
|
|
571
|
-
|
|
572
|
-
// Process header
|
|
573
|
-
const header = new DataView(headerResult.buffer.buffer);
|
|
574
|
-
const fileType = header.getUint32(0, true);
|
|
575
|
-
const dataSize = header.getUint32(4, true);
|
|
576
|
-
|
|
577
|
-
// Read data section
|
|
578
|
-
const dataBuffer = new Uint8Array(dataSize);
|
|
579
|
-
const dataResult = await fs.read(fd, dataBuffer, 0, dataSize, 16);
|
|
580
|
-
|
|
581
|
-
return { fileType, dataSize, data: dataBuffer };
|
|
582
|
-
} finally {
|
|
583
|
-
await fs.close(fd);
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
```
|
|
587
|
-
|
|
588
|
-
### Log File Management
|
|
589
|
-
|
|
590
|
-
```typescript
|
|
591
|
-
class LogManager {
|
|
592
|
-
private fd: number | null = null;
|
|
593
|
-
private logPath: string;
|
|
594
|
-
|
|
595
|
-
constructor(logPath: string) {
|
|
596
|
-
this.logPath = logPath;
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
async open() {
|
|
600
|
-
this.fd = await fs.open(this.logPath, { create: true });
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
async writeLog(level: string, message: string) {
|
|
604
|
-
if (!this.fd) throw new Error('Log file not open');
|
|
605
|
-
|
|
606
|
-
const timestamp = new Date().toISOString();
|
|
607
|
-
const logEntry = `[${timestamp}] [${level}] ${message}\n`;
|
|
608
|
-
const data = new TextEncoder().encode(logEntry);
|
|
609
|
-
|
|
610
|
-
await fs.write(this.fd, data);
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
async flush() {
|
|
614
|
-
if (this.fd) {
|
|
615
|
-
await fs.fsync(this.fd);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
async close() {
|
|
620
|
-
if (this.fd) {
|
|
621
|
-
await fs.close(this.fd);
|
|
622
|
-
this.fd = null;
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// Usage
|
|
628
|
-
const logger = new LogManager('/logs/app.log');
|
|
629
|
-
await logger.open();
|
|
630
|
-
await logger.writeLog('INFO', 'Application started');
|
|
631
|
-
await logger.writeLog('ERROR', 'Something went wrong');
|
|
632
|
-
await logger.flush();
|
|
633
|
-
await logger.close();
|
|
634
|
-
```
|
|
635
|
-
|
|
636
|
-
### File Copy with Progress
|
|
637
|
-
|
|
638
|
-
```typescript
|
|
639
|
-
async function copyFileWithProgress(sourcePath: string, destPath: string, onProgress?: (bytes: number) => void) {
|
|
640
|
-
const sourceFd = await fs.open(sourcePath);
|
|
641
|
-
const destFd = await fs.open(destPath, { create: true, truncate: true });
|
|
642
|
-
|
|
643
|
-
try {
|
|
644
|
-
const buffer = new Uint8Array(8192);
|
|
645
|
-
let totalBytes = 0;
|
|
646
|
-
|
|
647
|
-
while (true) {
|
|
648
|
-
const result = await fs.read(sourceFd, buffer, 0, buffer.length, null);
|
|
649
|
-
if (result.bytesRead === 0) break;
|
|
650
|
-
|
|
651
|
-
await fs.write(destFd, result.buffer, 0, result.bytesRead);
|
|
652
|
-
totalBytes += result.bytesRead;
|
|
653
|
-
|
|
654
|
-
if (onProgress) {
|
|
655
|
-
onProgress(totalBytes);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
await fs.fsync(destFd);
|
|
660
|
-
} finally {
|
|
661
|
-
await fs.close(sourceFd);
|
|
662
|
-
await fs.close(destFd);
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
return totalBytes;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
// Usage
|
|
669
|
-
const totalBytes = await copyFileWithProgress(
|
|
670
|
-
'/source/large-file.dat',
|
|
671
|
-
'/dest/large-file.dat',
|
|
672
|
-
(bytes) => console.log(`Copied ${bytes} bytes`)
|
|
673
|
-
);
|
|
674
|
-
console.log(`Copy complete: ${totalBytes} bytes`);
|
|
675
|
-
```
|
|
128
|
+
## Errors
|
|
676
129
|
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
7. **Monitor resource usage** to avoid hitting file descriptor limits
|
|
686
|
-
8. **Use positioned I/O** when you need random access without changing current position
|
|
687
|
-
9. **Batch operations** when possible to reduce overhead
|
|
688
|
-
|
|
689
|
-
## Limitations
|
|
690
|
-
|
|
691
|
-
- **Browser-specific**: File descriptor behavior may vary between browsers
|
|
692
|
-
- **OPFS constraints**: Subject to browser storage limits and OPFS implementation details
|
|
693
|
-
- **No file locking**: Concurrent access to the same file from multiple workers may cause issues
|
|
694
|
-
- **Position limits**: File positions are limited by JavaScript number precision (2^53 - 1)
|
|
695
|
-
- **No direct memory mapping**: Files must be read into buffers rather than memory-mapped
|
|
130
|
+
| Error | When |
|
|
131
|
+
| ----- | ---- |
|
|
132
|
+
| `AlreadyExistsError` | `exclusive: true` and file exists |
|
|
133
|
+
| `ExistenceError` | Missing path |
|
|
134
|
+
| `FileTypeError` | File vs directory mismatch |
|
|
135
|
+
| `ValidationError` | Bad offset / length / args |
|
|
136
|
+
| `FileSystemOperationError` / `IOError` | Underlying FS / I/O failure |
|
|
137
|
+
| `PermissionError` | Access denied |
|