opfs-worker 2.0.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.
Files changed (134) hide show
  1. package/README.md +64 -428
  2. package/dist/BaseOPFS-BusBkyJX.js +2114 -0
  3. package/dist/BaseOPFS-BusBkyJX.js.map +1 -0
  4. package/dist/BaseOPFS-CYwrD7-q.cjs +4 -0
  5. package/dist/BaseOPFS-CYwrD7-q.cjs.map +1 -0
  6. package/dist/OPFSAsync-B2dR-2VR.cjs +2 -0
  7. package/dist/OPFSAsync-B2dR-2VR.cjs.map +1 -0
  8. package/dist/OPFSAsync-CO44W23e.js +124 -0
  9. package/dist/OPFSAsync-CO44W23e.js.map +1 -0
  10. package/dist/OPFSFacade-CEaSqTut.js +308 -0
  11. package/dist/OPFSFacade-CEaSqTut.js.map +1 -0
  12. package/dist/OPFSFacade-hrNXCn6w.cjs +2 -0
  13. package/dist/OPFSFacade-hrNXCn6w.cjs.map +1 -0
  14. package/dist/OPFSSync-BPrNtvFS.cjs +2 -0
  15. package/dist/OPFSSync-BPrNtvFS.cjs.map +1 -0
  16. package/dist/OPFSSync-D7obTsl3.js +208 -0
  17. package/dist/OPFSSync-D7obTsl3.js.map +1 -0
  18. package/dist/assets/dedicated.worker-DW6UqFo0.js.map +1 -0
  19. package/dist/{OPFSWorker.d.ts → core/BaseOPFS.d.ts} +53 -292
  20. package/dist/core/BaseOPFS.d.ts.map +1 -0
  21. package/dist/core/OPFSAsync.d.ts +55 -0
  22. package/dist/core/OPFSAsync.d.ts.map +1 -0
  23. package/dist/core/OPFSSync.d.ts +54 -0
  24. package/dist/core/OPFSSync.d.ts.map +1 -0
  25. package/dist/createOPFSAsync-DdhJYoC0.js +10 -0
  26. package/dist/createOPFSAsync-DdhJYoC0.js.map +1 -0
  27. package/dist/createOPFSAsync-DyWpvmL-.cjs +2 -0
  28. package/dist/createOPFSAsync-DyWpvmL-.cjs.map +1 -0
  29. package/dist/createOPFSDedicated-BwTc0NV5.js +2407 -0
  30. package/dist/createOPFSDedicated-BwTc0NV5.js.map +1 -0
  31. package/dist/createOPFSDedicated-CEeRexom.cjs +2357 -0
  32. package/dist/createOPFSDedicated-CEeRexom.cjs.map +1 -0
  33. package/dist/createOPFSShared-BSFWsoLe.js +23 -0
  34. package/dist/createOPFSShared-BSFWsoLe.js.map +1 -0
  35. package/dist/createOPFSShared-BoCft_BS.cjs +2 -0
  36. package/dist/createOPFSShared-BoCft_BS.cjs.map +1 -0
  37. package/dist/dedicated.worker.js +2355 -0
  38. package/dist/dedicated.worker.js.map +1 -0
  39. package/dist/{OPFSFacade.d.ts → facade/OPFSFacade.d.ts} +51 -25
  40. package/dist/facade/OPFSFacade.d.ts.map +1 -0
  41. package/dist/facade/createOPFSAsync.d.ts +16 -0
  42. package/dist/facade/createOPFSAsync.d.ts.map +1 -0
  43. package/dist/facade/createOPFSDedicated.d.ts +17 -0
  44. package/dist/facade/createOPFSDedicated.d.ts.map +1 -0
  45. package/dist/facade/createOPFSShared.d.ts +24 -0
  46. package/dist/facade/createOPFSShared.d.ts.map +1 -0
  47. package/dist/index.async.cjs +2 -0
  48. package/dist/index.async.cjs.map +1 -0
  49. package/dist/index.async.d.ts +10 -0
  50. package/dist/index.async.d.ts.map +1 -0
  51. package/dist/index.async.js +55 -0
  52. package/dist/index.async.js.map +1 -0
  53. package/dist/index.cjs +1 -2477
  54. package/dist/index.cjs.map +1 -1
  55. package/dist/index.d.ts +17 -3
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +61 -2828
  58. package/dist/index.js.map +1 -1
  59. package/dist/index.pure.cjs +1 -1
  60. package/dist/index.pure.cjs.map +1 -1
  61. package/dist/index.pure.d.ts +3 -1
  62. package/dist/index.pure.d.ts.map +1 -1
  63. package/dist/index.pure.js +50 -1053
  64. package/dist/index.pure.js.map +1 -1
  65. package/dist/index.sharedworker.cjs +2 -0
  66. package/dist/index.sharedworker.cjs.map +1 -0
  67. package/dist/index.sharedworker.d.ts +11 -0
  68. package/dist/index.sharedworker.d.ts.map +1 -0
  69. package/dist/index.sharedworker.js +55 -0
  70. package/dist/index.sharedworker.js.map +1 -0
  71. package/dist/index.sync.cjs +2 -0
  72. package/dist/index.sync.cjs.map +1 -0
  73. package/dist/index.sync.d.ts +11 -0
  74. package/dist/index.sync.d.ts.map +1 -0
  75. package/dist/index.sync.js +55 -0
  76. package/dist/index.sync.js.map +1 -0
  77. package/dist/shared.worker.js +2240 -0
  78. package/dist/shared.worker.js.map +1 -0
  79. package/dist/types.d.ts +42 -4
  80. package/dist/types.d.ts.map +1 -1
  81. package/dist/worker/createDedicatedWorker.d.ts +30 -0
  82. package/dist/worker/createDedicatedWorker.d.ts.map +1 -0
  83. package/dist/worker/createSharedWorker.d.ts +37 -0
  84. package/dist/worker/createSharedWorker.d.ts.map +1 -0
  85. package/dist/worker/dedicated.worker.d.ts +2 -0
  86. package/dist/worker/dedicated.worker.d.ts.map +1 -0
  87. package/dist/worker/shared.worker.d.ts +2 -0
  88. package/dist/worker/shared.worker.d.ts.map +1 -0
  89. package/docs/api/backend.md +36 -0
  90. package/docs/api/create.md +82 -0
  91. package/docs/api/facade.md +53 -0
  92. package/docs/choosing-a-mode.md +41 -0
  93. package/docs/file-descriptors.md +46 -605
  94. package/docs/guides/async.md +36 -0
  95. package/docs/guides/dedicated.md +76 -0
  96. package/docs/guides/hashing.md +34 -0
  97. package/docs/guides/pure.md +46 -0
  98. package/docs/guides/sharedworker.md +57 -0
  99. package/docs/guides/streaming.md +185 -0
  100. package/docs/guides/watching.md +35 -0
  101. package/docs/migration.md +41 -0
  102. package/docs/types.md +105 -153
  103. package/package.json +53 -4
  104. package/src/{OPFSWorker.ts → core/BaseOPFS.ts} +159 -629
  105. package/src/core/OPFSAsync.ts +248 -0
  106. package/src/core/OPFSSync.ts +460 -0
  107. package/src/{OPFSFacade.ts → facade/OPFSFacade.ts} +134 -58
  108. package/src/facade/createOPFSAsync.ts +22 -0
  109. package/src/facade/createOPFSDedicated.ts +23 -0
  110. package/src/facade/createOPFSShared.ts +30 -0
  111. package/src/index.async.ts +11 -0
  112. package/src/index.pure.ts +3 -2
  113. package/src/index.sharedworker.ts +12 -0
  114. package/src/index.sync.ts +12 -0
  115. package/src/index.ts +37 -8
  116. package/src/types.ts +45 -4
  117. package/src/worker/createDedicatedWorker.ts +74 -0
  118. package/src/worker/createSharedWorker.ts +67 -0
  119. package/src/worker/dedicated.worker.ts +13 -0
  120. package/src/worker/shared.worker.ts +17 -0
  121. package/dist/OPFSFacade.d.ts.map +0 -1
  122. package/dist/OPFSWorker.d.ts.map +0 -1
  123. package/dist/assets/worker.entry-DUlEoroc.js.map +0 -1
  124. package/dist/createOPFSWorker.d.ts +0 -17
  125. package/dist/createOPFSWorker.d.ts.map +0 -1
  126. package/dist/helpers-DNj8ZoMu.cjs +0 -4
  127. package/dist/helpers-DNj8ZoMu.cjs.map +0 -1
  128. package/dist/helpers-WY2jfbOT.js +0 -1427
  129. package/dist/helpers-WY2jfbOT.js.map +0 -1
  130. package/dist/worker.entry.d.ts +0 -2
  131. package/dist/worker.entry.d.ts.map +0 -1
  132. package/docs/api-reference.md +0 -815
  133. package/src/createOPFSWorker.ts +0 -57
  134. package/src/worker.entry.ts +0 -6
@@ -1,6 +1,4 @@
1
- import { transfer } from 'comlink';
2
-
3
- import { WatchEventType } from './types';
1
+ import { WatchEventType } from '../types';
4
2
  import {
5
3
  AlreadyExistsError,
6
4
  DirectoryOperationError,
@@ -11,18 +9,13 @@ import {
11
9
  OPFSError,
12
10
  OperationNotSupportedError,
13
11
  PathError,
14
- ValidationError,
15
- createFDError,
16
12
  mapDomError
17
- } from './utils/errors';
13
+ } from '../utils/errors';
18
14
 
19
15
  import {
20
16
  basename,
21
17
  calculateFileHash,
22
- calculateReadLength,
23
18
  checkOPFSSupport,
24
- convertBlobToUint8Array,
25
- createSyncHandleSafe,
26
19
  dirname,
27
20
  joinPath,
28
21
  matchMinimatch,
@@ -30,44 +23,44 @@ import {
30
23
  normalizePath,
31
24
  removeEntry,
32
25
  resolvePath,
33
- safeCloseSyncHandle,
34
- splitPath,
35
- validateReadWriteArgs,
36
- withLock
37
- } from './utils/helpers';
38
-
39
- import type { DirentData, FileOpenOptions, FileStat, OPFSOptions, RenameOptions, WatchEvent, WatchOptions, WatchSnapshot } from './types';
26
+ splitPath
27
+ } from '../utils/helpers';
28
+
29
+ import type {
30
+ DirentData,
31
+ FileStat,
32
+ ImportFileData,
33
+ ImportFilesEntries,
34
+ ImportFilesProgress,
35
+ ImportFilesResult,
36
+ OPFSOptions,
37
+ RenameOptions,
38
+ WatchEvent,
39
+ WatchOptions,
40
+ WatchSnapshot,
41
+ } from '../types';
40
42
 
41
43
 
42
44
  /**
43
- * OPFS (Origin Private File System) File System implementation
44
- *
45
- * This class provides a high-level interface for working with the browser's
46
- * Origin Private File System API, offering file and directory operations
47
- * similar to Node.js fs module.
48
- *
49
- * @example
50
- * ```typescript
51
- * const fs = new OPFSWorker();
52
- * await fs.writeFile('/data/config.json', new TextEncoder().encode(JSON.stringify({ theme: 'dark' })));
53
- * const config = await fs.readFile('/data/config.json');
54
- * ```
45
+ * Shared OPFS logic (directories, meta, watch, high-level helpers).
46
+ * File I/O backends implement {@link readFile}, {@link writeFile}, {@link appendFile},
47
+ * and {@link writeStream}.
55
48
  */
56
- export class OPFSWorker {
49
+ export abstract class BaseOPFS {
57
50
  /** Root directory handle for the file system */
58
- private root!: FileSystemDirectoryHandle;
51
+ protected root!: FileSystemDirectoryHandle;
59
52
 
60
53
  /** Map of watched paths and options */
61
- private watchers = new Map<string, WatchSnapshot>();
54
+ protected watchers = new Map<string, WatchSnapshot>();
62
55
 
63
56
  /** Promise to prevent concurrent mount operations */
64
- private mountingPromise: Promise<boolean> | null = null;
57
+ protected mountingPromise: Promise<boolean> | null = null;
65
58
 
66
59
  /** BroadcastChannel instance for sending events */
67
- private broadcastChannel: BroadcastChannel | null = null;
60
+ protected broadcastChannel: BroadcastChannel | null = null;
68
61
 
69
62
  /** Configuration options */
70
- private options: Required<OPFSOptions> = {
63
+ protected options: Required<OPFSOptions> = {
71
64
  root: '/',
72
65
  namespace: '',
73
66
  maxFileSize: 50 * 1024 * 1024,
@@ -75,39 +68,6 @@ export class OPFSWorker {
75
68
  broadcastChannel: 'opfs-worker',
76
69
  };
77
70
 
78
- /** Shared sync handles per path (OPFS allows only one handle per file) */
79
- private openHandles = new Map<string, {
80
- fileHandle: FileSystemFileHandle;
81
- syncHandle: FileSystemSyncAccessHandle;
82
- refCount: number;
83
- }>();
84
-
85
- /** Map of open file descriptors to their metadata */
86
- private openFiles = new Map<number, {
87
- path: string;
88
- fileHandle: FileSystemFileHandle;
89
- syncHandle: FileSystemSyncAccessHandle;
90
- position: number;
91
- }>();
92
-
93
- /** Next available file descriptor number */
94
- private nextFd = 1;
95
-
96
- /**
97
- * Get file info by descriptor with validation
98
- * @private
99
- */
100
- private _getFileDescriptor(fd: number): { path: string; fileHandle: FileSystemFileHandle; syncHandle: FileSystemSyncAccessHandle; position: number } {
101
- const fileInfo = this.openFiles.get(fd);
102
-
103
- if (!fileInfo) {
104
- throw new ValidationError('descriptor', `Invalid file descriptor: ${ fd }`);
105
- }
106
-
107
- return fileInfo;
108
- }
109
-
110
-
111
71
  /**
112
72
  * Notify about internal changes to the file system
113
73
  *
@@ -116,7 +76,7 @@ export class OPFSWorker {
116
76
  *
117
77
  * @param event - The event describing the change
118
78
  */
119
- private async notifyChange(event: Omit<WatchEvent, 'timestamp' | 'hash' | 'namespace'>): Promise<void> {
79
+ protected async notifyChange(event: Omit<WatchEvent, 'timestamp' | 'hash' | 'namespace'>): Promise<void> {
120
80
  // This instance not configured to send events
121
81
  if (!this.options.broadcastChannel) {
122
82
  return;
@@ -167,16 +127,6 @@ export class OPFSWorker {
167
127
  }
168
128
  }
169
129
 
170
- /**
171
- * Creates a new OPFSWorker instance
172
- *
173
- * @param options - Optional configuration options
174
- * @param options.root - Root path for the file system (default: '/')
175
- * @param options.watchInterval - Polling interval in milliseconds for file watching
176
- * @param options.hashAlgorithm - Hash algorithm for file hashing
177
- * @param options.maxFileSize - Maximum file size for hashing in bytes (default: 50MB)
178
- * @throws {OPFSError} If OPFS is not supported in the current browser
179
- */
180
130
  constructor(options?: OPFSOptions) {
181
131
  checkOPFSSupport();
182
132
 
@@ -185,27 +135,27 @@ export class OPFSWorker {
185
135
  }
186
136
  }
187
137
 
138
+ /** Read file contents as bytes */
139
+ abstract readFile(path: string): Promise<Uint8Array>;
140
+
141
+ /** Create or overwrite a file with binary data */
142
+ abstract writeFile(path: string, data: Uint8Array | ArrayBuffer): Promise<void>;
143
+
144
+ /** Append binary data to a file */
145
+ abstract appendFile(path: string, data: Uint8Array | ArrayBuffer): Promise<void>;
146
+
147
+ /** Create or overwrite a file from a byte stream */
148
+ abstract writeStream(
149
+ path: string,
150
+ stream: ReadableStream<Uint8Array>,
151
+ onProgress?: (bytesWritten: number) => unknown
152
+ ): Promise<number>;
153
+
188
154
  /**
189
- * Initialize the file system within a given directory
190
- *
191
- * This method sets up the root directory for all subsequent operations.
192
- * If no root is specified, it will use the OPFS root directory.
193
- *
194
- * @returns Promise that resolves to true if initialization was successful
195
- * @throws {OPFSError} If initialization fails
196
- *
197
- * @example
198
- * ```typescript
199
- * const fs = new OPFSWorker();
200
- *
201
- * // Use OPFS root (default)
202
- * await fs.mount();
203
- *
204
- * // Use custom directory
205
- * await fs.mount('/my-app');
206
- * ```
155
+ * Initialize the file system within a given directory.
156
+ * If no root is specified, uses the OPFS root directory.
207
157
  */
208
- private async mount(): Promise<boolean> {
158
+ protected async mount(): Promise<boolean> {
209
159
  const root = this.options.root;
210
160
 
211
161
  // If already mounting, wait for previous operation to complete first
@@ -296,7 +246,7 @@ export class OPFSWorker {
296
246
  * const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
297
247
  * ```
298
248
  */
299
- private async getDirectoryHandle(path: string | string[], create: boolean = false, from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemDirectoryHandle> {
249
+ protected async getDirectoryHandle(path: string | string[], create: boolean = false, from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemDirectoryHandle> {
300
250
  const segments = Array.isArray(path) ? path : splitPath(path);
301
251
  let current = from;
302
252
 
@@ -326,7 +276,7 @@ export class OPFSWorker {
326
276
  * const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
327
277
  * ```
328
278
  */
329
- private async getFileHandle(path: string | string[], create = false, _from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemFileHandle> {
279
+ protected async getFileHandle(path: string | string[], create = false, _from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemFileHandle> {
330
280
  const segments = splitPath(path);
331
281
 
332
282
  if (segments.length === 0) {
@@ -396,149 +346,6 @@ export class OPFSWorker {
396
346
  return result;
397
347
  }
398
348
 
399
- /**
400
- * Read a file from the file system
401
- *
402
- * Reads the contents of a file and returns it as binary data.
403
- *
404
- * @param path - The path to the file to read
405
- * @returns Promise that resolves to the file contents as Uint8Array
406
- * @throws {FileNotFoundError} If the file does not exist
407
- * @throws {OPFSError} If reading the file fails
408
- *
409
- * @example
410
- * ```typescript
411
- * // Read as binary data
412
- * const content = await fs.readFile('/config/settings.json');
413
- *
414
- * // Read binary file
415
- * const binaryData = await fs.readFile('/images/logo.png');
416
- * ```
417
- */
418
- async readFile(path: string): Promise<Uint8Array> {
419
- await this.mount();
420
-
421
- try {
422
- return await withLock(path, async() => {
423
- const fd = await this.open(path);
424
-
425
- try {
426
- const { size } = await this.fstat(fd);
427
- const buffer = new Uint8Array(size);
428
-
429
- if (size > 0) {
430
- await this.read(fd, buffer, 0, size, 0);
431
- }
432
-
433
- return transfer(buffer, [buffer.buffer]);
434
- }
435
- finally {
436
- await this.close(fd);
437
- }
438
- });
439
- }
440
- catch (err) {
441
- if (err instanceof OPFSError) {
442
- throw err;
443
- }
444
-
445
- throw mapDomError(err, { path, isDirectory: false });
446
- }
447
- }
448
-
449
- /**
450
- * Write data to a file
451
- *
452
- * Creates or overwrites a file with the specified binary data. If the file already
453
- * exists, it will be truncated before writing.
454
- *
455
- * @param path - The path to the file to write
456
- * @param data - The binary data to write to the file (Uint8Array or ArrayBuffer)
457
- * @returns Promise that resolves when the write operation is complete
458
- * @throws {OPFSError} If writing the file fails
459
- *
460
- * @example
461
- * ```typescript
462
- * // Write binary data
463
- * const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
464
- * await fs.writeFile('/data/binary.dat', binaryData);
465
- *
466
- * // Write from ArrayBuffer
467
- * const arrayBuffer = new ArrayBuffer(10);
468
- * await fs.writeFile('/data/buffer.dat', arrayBuffer);
469
- * ```
470
- */
471
- async writeFile(
472
- path: string,
473
- data: Uint8Array | ArrayBuffer
474
- ): Promise<void> {
475
- await this.mount();
476
-
477
- const buffer = data instanceof Uint8Array ? data : new Uint8Array(data);
478
-
479
- await withLock(path, async() => {
480
- const existed = await this.exists(path);
481
- const fd = await this.open(path, { create: true, truncate: true });
482
-
483
- try {
484
- await this.write(fd, buffer, 0, buffer.length, null, false);
485
- await this.fsync(fd);
486
- }
487
- finally {
488
- await this.close(fd);
489
- }
490
-
491
- await this.notifyChange({ path, type: existed ? WatchEventType.Changed : WatchEventType.Added, isDirectory: false });
492
- });
493
- }
494
-
495
- /**
496
- * Append data to a file
497
- *
498
- * Adds binary data to the end of an existing file. If the file doesn't exist,
499
- * it will be created.
500
- *
501
- * @param path - The path to the file to append to
502
- * @param data - The binary data to append to the file (Uint8Array or ArrayBuffer)
503
- * @returns Promise that resolves when the append operation is complete
504
- * @throws {OPFSError} If appending to the file fails
505
- *
506
- * @example
507
- * ```typescript
508
- * // Append binary data
509
- * const additionalData = new Uint8Array([6, 7, 8]);
510
- * await fs.appendFile('/data/binary.dat', additionalData);
511
- *
512
- * // Append from ArrayBuffer
513
- * const arrayBuffer = new ArrayBuffer(5);
514
- * await fs.appendFile('/data/buffer.dat', arrayBuffer);
515
- * ```
516
- */
517
- async appendFile(
518
- path: string,
519
- data: Uint8Array | ArrayBuffer
520
- ): Promise<void> {
521
- await this.mount();
522
-
523
- const buffer = data instanceof Uint8Array ? data : new Uint8Array(data);
524
-
525
- await withLock(path, async() => {
526
- const fd = await this.open(path, { create: true });
527
-
528
- try {
529
- const { size } = await this.fstat(fd);
530
-
531
- await this.write(fd, buffer, 0, buffer.length, size, false);
532
- await this.fsync(fd);
533
- }
534
- finally {
535
- await this.close(fd);
536
- }
537
-
538
- await this.notifyChange({ path, type: WatchEventType.Changed, isDirectory: false });
539
- });
540
- }
541
-
542
349
  /**
543
350
  * Create a directory
544
351
  *
@@ -1095,376 +902,125 @@ export class OPFSWorker {
1095
902
  }
1096
903
 
1097
904
  /**
1098
- * Open a file and return a file descriptor
1099
- *
1100
- * @param path - The path to the file to open
1101
- * @param options - Options for opening the file
1102
- * @param options.create - Whether to create the file if it doesn't exist (default: false)
1103
- * @param options.exclusive - If true and create is true, fails if file already exists (default: false)
1104
- * Note: This is best-effort in OPFS, not fully atomic due to browser limitations
1105
- * @param options.truncate - Whether to truncate the file to zero length (default: false)
1106
- * @returns Promise that resolves to a file descriptor number
1107
- * @throws {OPFSError} If opening the file fails
1108
- *
905
+ * Read a file as a `Blob` without copying its bytes into memory.
906
+ *
907
+ * OPFS hands back a disk-backed `File`, so the browser can stream it on
908
+ * demand pass it to `URL.createObjectURL()` for `<video>` / `<audio>` /
909
+ * `<img>` and only the played range is actually read. Structured clone keeps
910
+ * the reference intact when the file system runs in a worker.
911
+ *
912
+ * @param path - Path to the file
913
+ * @returns Lazy `File` handle backed by OPFS storage
914
+ * @throws {OPFSError} If the path is missing or is a directory
915
+ *
1109
916
  * @example
1110
917
  * ```typescript
1111
- * // Open existing file for reading
1112
- * const fd = await fs.open('/data/config.json');
1113
- *
1114
- * // Create new file for writing
1115
- * const fd = await fs.open('/data/new.txt', { create: true });
1116
- *
1117
- * // Create file exclusively (fails if exists)
1118
- * const fd = await fs.open('/data/unique.txt', { create: true, exclusive: true });
1119
- *
1120
- * // Open and truncate file
1121
- * const fd = await fs.open('/data/log.txt', { create: true, truncate: true });
918
+ * const blob = await fs.readBlob('/media/clip.mp4');
919
+ * video.src = URL.createObjectURL(blob);
1122
920
  * ```
1123
921
  */
1124
- async open(path: string, options?: FileOpenOptions): Promise<number> {
922
+ async readBlob(path: string): Promise<Blob> {
1125
923
  await this.mount();
1126
924
 
1127
- const { create = false, exclusive = false, truncate = false } = options || {};
1128
-
1129
- // Normalize path to prevent path-related issues
1130
- const normalizedPath = normalizePath(resolvePath(path));
1131
-
1132
925
  try {
1133
- // Use lock for atomic operations when creating files
1134
- if (create && exclusive) {
1135
- return await withLock(normalizedPath, async() => {
1136
- const exists = await this.exists(normalizedPath);
926
+ const fileHandle = await this.getFileHandle(path, false);
1137
927
 
1138
- if (exists) {
1139
- throw new AlreadyExistsError(normalizedPath);
1140
- }
1141
-
1142
- return this._openFile(normalizedPath, create, truncate);
1143
- });
1144
- }
1145
-
1146
- return await this._openFile(normalizedPath, create, truncate);
928
+ return await fileHandle.getFile();
1147
929
  }
1148
930
  catch (error: any) {
1149
931
  if (error instanceof OPFSError) {
1150
932
  throw error;
1151
933
  }
1152
934
 
1153
- // TypeMismatchError here means the path actually refers to a directory
1154
- // so we map it as a directory-type error (EISDIR) for better Node.js parity.
1155
- const isTypeMismatchDirectory = error && error.name === 'TypeMismatchError';
1156
-
1157
- throw mapDomError(error, {
1158
- path: normalizedPath,
1159
- isDirectory: !!isTypeMismatchDirectory,
1160
- });
935
+ throw mapDomError(error, { path, isDirectory: error?.name === 'TypeMismatchError' });
1161
936
  }
1162
937
  }
1163
938
 
1164
939
  /**
1165
- * Internal method to open a file (without locking)
940
+ * Bulk-import files from `[path, data]` entries (strings, bytes, Blobs, or Files).
941
+ * Each entry is written via {@link writeStream} so large Blobs/Files are not
942
+ * fully buffered in memory.
1166
943
  *
1167
- * Multiple FDs for the same path share one sync access handle (OPFS limit),
1168
- * with independent per-FD positions — similar to Node.js.
1169
- * @private
1170
- */
1171
- private async _openFile(path: string, create: boolean, truncate: boolean): Promise<number> {
1172
- let shared = this.openHandles.get(path);
1173
-
1174
- if (!shared) {
1175
- const fileHandle = await this.getFileHandle(path, create);
1176
-
1177
- // Verify that we got a file handle, not a directory
1178
- try {
1179
- await fileHandle.getFile();
1180
- }
1181
- catch (error: any) {
1182
- throw mapDomError(error, { path, isDirectory: true });
1183
- }
1184
-
1185
- const syncHandle = await createSyncHandleSafe(fileHandle, path);
1186
-
1187
- shared = { fileHandle, syncHandle, refCount: 0 };
1188
- this.openHandles.set(path, shared);
1189
- }
1190
-
1191
- shared.refCount++;
1192
-
1193
- if (truncate) {
1194
- shared.syncHandle.truncate(0);
1195
- shared.syncHandle.flush();
1196
- }
1197
-
1198
- const fd = this.nextFd++;
1199
-
1200
- this.openFiles.set(fd, {
1201
- path,
1202
- fileHandle: shared.fileHandle,
1203
- syncHandle: shared.syncHandle,
1204
- position: 0,
1205
- });
1206
-
1207
- return fd;
1208
- }
1209
-
1210
- /**
1211
- * Close a file descriptor
1212
- *
1213
- * @param fd - The file descriptor to close
1214
- * @returns Promise that resolves when the file descriptor is closed
1215
- * @throws {OPFSError} If the file descriptor is invalid or closing fails
1216
- *
1217
- * @example
1218
- * ```typescript
1219
- * const fd = await fs.open('/data/file.txt');
1220
- * // ... use the file descriptor ...
1221
- * await fs.close(fd);
1222
- * ```
1223
- */
1224
- async close(fd: number): Promise<void> {
1225
- const fileInfo = this._getFileDescriptor(fd);
1226
-
1227
- this.openFiles.delete(fd);
1228
-
1229
- const shared = this.openHandles.get(fileInfo.path);
1230
-
1231
- if (!shared) {
1232
- return;
1233
- }
1234
-
1235
- shared.refCount--;
1236
-
1237
- // Only close the underlying sync handle when the last FD for this path is gone
1238
- if (shared.refCount <= 0) {
1239
- safeCloseSyncHandle(fd, shared.syncHandle, fileInfo.path);
1240
- this.openHandles.delete(fileInfo.path);
1241
- }
1242
- }
1243
-
1244
- /**
1245
- * Read data from a file descriptor
1246
- *
1247
- * @param fd - The file descriptor to read from
1248
- * @param buffer - The buffer to read data into
1249
- * @param offset - The offset in the buffer to start writing at
1250
- * @param length - The number of bytes to read
1251
- * @param position - The position in the file to read from (null for current position)
1252
- * @returns Promise that resolves to the number of bytes read and the modified buffer
1253
- * @throws {OPFSError} If the file descriptor is invalid or reading fails
1254
- *
1255
- * @note This method uses Comlink.transfer() to efficiently pass the buffer as a Transferable Object,
1256
- * ensuring zero-copy performance across Web Worker boundaries.
1257
- *
1258
- * @example
1259
- * ```typescript
1260
- * const fd = await fs.open('/data/file.txt');
1261
- * const buffer = new Uint8Array(1024);
1262
- * const { bytesRead, buffer: modifiedBuffer } = await fs.read(fd, buffer, 0, 1024, null);
1263
- * console.log(`Read ${bytesRead} bytes`);
1264
- * // Use modifiedBuffer which contains the actual data
1265
- * await fs.close(fd);
1266
- * ```
1267
- */
1268
- async read(
1269
- fd: number,
1270
- buffer: Uint8Array,
1271
- offset: number,
1272
- length: number,
1273
- position: number | null | undefined
1274
- ): Promise<{ bytesRead: number; buffer: Uint8Array }> {
1275
- const fileInfo = this._getFileDescriptor(fd);
1276
-
1277
- // Validate arguments
1278
- validateReadWriteArgs(buffer.length, offset, length, position);
1279
-
1280
- try {
1281
- const readPosition = position ?? fileInfo.position;
1282
-
1283
- // Get file size and calculate read length
1284
- const fileSize = fileInfo.syncHandle.getSize();
1285
- const { isEOF, actualLength } = calculateReadLength(readPosition, length, fileSize);
1286
-
1287
- if (isEOF) {
1288
- return transfer({ bytesRead: 0, buffer }, [buffer.buffer]); // End of file
1289
- }
1290
-
1291
- // Create a subarray view for the read operation
1292
- const targetBuffer = buffer.subarray(offset, offset + actualLength);
1293
-
1294
- // Perform efficient positioned read
1295
- const bytesRead = fileInfo.syncHandle.read(targetBuffer, { at: readPosition });
1296
-
1297
- // Update position if position was not explicitly specified (null means use current position)
1298
- if (position == null) {
1299
- fileInfo.position = readPosition + bytesRead;
1300
- }
1301
-
1302
- return transfer({ bytesRead, buffer }, [buffer.buffer]);
1303
- }
1304
- catch (error) {
1305
- throw createFDError('read', fd, fileInfo.path, error);
1306
- }
1307
- }
1308
-
1309
- /**
1310
- * Write data to a file descriptor
1311
- *
1312
- * @param fd - The file descriptor to write to
1313
- * @param buffer - The buffer containing data to write
1314
- * @param offset - The offset in the buffer to start reading from (default: 0)
1315
- * @param length - The number of bytes to write (default: entire buffer)
1316
- * @param position - The position in the file to write to (null/undefined for current position)
1317
- * @param emitEvent - Whether to emit a change event (default: true)
1318
- * @returns Promise that resolves to the number of bytes written
1319
- * @throws {OPFSError} If the file descriptor is invalid or writing fails
944
+ * Accepts an array of tuples, a `Map`, or any iterable of `[path, data]` pairs.
945
+ *
946
+ * @param entries - Files to write
947
+ * @param onProgress - Per-chunk progress with path, index, and byte counts
948
+ * @returns Written paths, count, and total bytes
949
+ * @throws {OPFSError} If a write fails
1320
950
  *
1321
951
  * @example
1322
952
  * ```typescript
1323
- * const fd = await fs.open('/data/file.txt', { create: true });
1324
- * const data = new TextEncoder().encode('Hello, World!');
1325
- * const bytesWritten = await fs.write(fd, data, 0, data.length, null);
1326
- * console.log(`Wrote ${bytesWritten} bytes`);
1327
- * await fs.close(fd);
953
+ * const result = await fs.importFiles([
954
+ * ['/config.json', JSON.stringify({ theme: 'dark' })],
955
+ * ['/data/binary.dat', new Uint8Array([1, 2, 3, 4])],
956
+ * ['/upload.txt', fileFromInput],
957
+ * ], (p) => console.log(`${p.path}: ${p.bytesWritten}/${p.bytesTotal}`));
958
+ *
959
+ * console.log(result.count, result.bytesWritten, result.paths);
1328
960
  * ```
1329
961
  */
1330
- async write(
1331
- fd: number,
1332
- buffer: Uint8Array,
1333
- offset: number = 0,
1334
- length?: number,
1335
- position?: number | null | undefined,
1336
- emitEvent: boolean = true
1337
- ): Promise<number> {
1338
- const fileInfo = this._getFileDescriptor(fd);
1339
-
1340
- // Calculate actual length to write
1341
- const actualLength = length ?? (buffer.length - offset);
1342
-
1343
- // Validate arguments using helper
1344
- validateReadWriteArgs(buffer.length, offset, actualLength, position);
1345
-
1346
- try {
1347
- // Determine write position: use specified position, or current position if null/undefined
1348
- const writePosition = position ?? fileInfo.position;
1349
-
1350
- // Create a subarray view for the write operation
1351
- const sourceBuffer = buffer.subarray(offset, offset + actualLength);
1352
-
1353
- // Perform efficient positioned write
1354
- const bytesWritten = fileInfo.syncHandle.write(sourceBuffer, { at: writePosition });
962
+ async importFiles(
963
+ entries: ImportFilesEntries,
964
+ onProgress?: (progress: ImportFilesProgress) => unknown
965
+ ): Promise<ImportFilesResult> {
966
+ await this.mount();
1355
967
 
1356
- // Update position if position was null or undefined (i.e., use current position)
1357
- // Also update position when writing at current position (position === fileInfo.position)
1358
- if (position == null || position === fileInfo.position) {
1359
- fileInfo.position = writePosition + bytesWritten;
1360
- }
968
+ const list = [...entries].map(([path, data]) => [normalizePath(path), data] as [string, ImportFileData]);
969
+ const count = list.length;
970
+ const paths = list.map(([path]) => path);
971
+ const totalBytes = list.reduce((sum, [, data]) => sum + importEntryByteLength(data), 0);
972
+ let totalBytesWritten = 0;
1361
973
 
1362
- if (emitEvent) {
1363
- await this.notifyChange({ path: fileInfo.path, type: WatchEventType.Changed, isDirectory: false });
974
+ try {
975
+ for (let index = 0; index < count; index++) {
976
+ const [path, data] = list[index]!;
977
+ const { stream, size: bytesTotal } = toImportStream(data);
978
+ const base = totalBytesWritten;
979
+
980
+ const written = await this.writeStream(
981
+ path,
982
+ stream,
983
+ onProgress
984
+ ? bytesWritten => onProgress({
985
+ path,
986
+ index,
987
+ count,
988
+ bytesWritten,
989
+ bytesTotal,
990
+ totalBytesWritten: base + bytesWritten,
991
+ totalBytes,
992
+ })
993
+ : undefined
994
+ );
995
+
996
+ totalBytesWritten += written;
1364
997
  }
1365
998
 
1366
- return bytesWritten;
999
+ return {
1000
+ paths,
1001
+ count,
1002
+ bytesWritten: totalBytesWritten,
1003
+ };
1367
1004
  }
1368
1005
  catch (error) {
1369
- throw createFDError('write', fd, fileInfo.path, error);
1370
- }
1371
- }
1372
-
1373
- /**
1374
- * Get file status information by file descriptor
1375
- *
1376
- * @param fd - The file descriptor
1377
- * @returns Promise that resolves to FileStat object
1378
- * @throws {OPFSError} If the file descriptor is invalid
1379
- *
1380
- * @example
1381
- * ```typescript
1382
- * const fd = await fs.open('/data/file.txt');
1383
- * const stats = await fs.fstat(fd);
1384
- * console.log(`File size: ${stats.size} bytes`);
1385
- * console.log(`Last modified: ${stats.mtime}`);
1386
- *
1387
- * // If hashing is enabled, hash will be included
1388
- * if (stats.hash) {
1389
- * console.log(`Hash: ${stats.hash}`);
1390
- * }
1391
- * ```
1392
- */
1393
- async fstat(fd: number): Promise<FileStat> {
1394
- const fileInfo = this._getFileDescriptor(fd);
1395
-
1396
- // Simply reuse existing stat() method with the file path
1397
- return this.stat(fileInfo.path);
1398
- }
1399
-
1400
- /**
1401
- * Truncate file to specified size
1402
- *
1403
- * @param fd - The file descriptor
1404
- * @param size - The new size of the file (default: 0)
1405
- * @returns Promise that resolves when truncation is complete
1406
- * @throws {OPFSError} If the file descriptor is invalid or truncation fails
1407
- *
1408
- * @example
1409
- * ```typescript
1410
- * const fd = await fs.open('/data/file.txt', { create: true });
1411
- * await fs.truncate(fd, 100); // Truncate to 100 bytes
1412
- * ```
1413
- */
1414
- async ftruncate(fd: number, size: number = 0): Promise<void> {
1415
- const fileInfo = this._getFileDescriptor(fd);
1416
-
1417
- // Validate size parameter
1418
- if (size < 0 || !Number.isInteger(size)) {
1419
- throw new ValidationError('argument', 'Invalid size');
1420
- }
1421
-
1422
- try {
1423
- fileInfo.syncHandle.truncate(size);
1424
- fileInfo.syncHandle.flush();
1425
-
1426
- // Adjust position if it's beyond the new file size
1427
- if (fileInfo.position > size) {
1428
- fileInfo.position = size;
1006
+ if (error instanceof OPFSError) {
1007
+ throw error;
1429
1008
  }
1430
1009
 
1431
- await this.notifyChange({ path: fileInfo.path, type: WatchEventType.Changed, isDirectory: false });
1432
- }
1433
- catch (error) {
1434
- throw createFDError('truncate', fd, fileInfo.path, error);
1010
+ throw mapDomError(error);
1435
1011
  }
1436
1012
  }
1437
1013
 
1438
1014
  /**
1439
- * Synchronize file data to storage (fsync equivalent)
1440
- *
1441
- * @param fd - The file descriptor
1442
- * @returns Promise that resolves when synchronization is complete
1443
- * @throws {OPFSError} If the file descriptor is invalid or sync fails
1444
- *
1445
- * @example
1446
- * ```typescript
1447
- * const fd = await fs.open('/data/file.txt', { create: true });
1448
- * await fs.write(fd, data);
1449
- * await fs.fsync(fd); // Ensure data is written to storage
1450
- * ```
1015
+ * @deprecated Use {@link importFiles} instead.
1451
1016
  */
1452
- async fsync(fd: number): Promise<void> {
1453
- const fileInfo = this._getFileDescriptor(fd);
1454
-
1455
- try {
1456
- fileInfo.syncHandle.flush();
1457
- }
1458
- catch (error) {
1459
- throw createFDError('sync', fd, fileInfo.path, error);
1460
- }
1017
+ async createIndex(entries: ImportFilesEntries): Promise<void> {
1018
+ warnCreateIndexDeprecated();
1019
+ await this.importFiles(entries);
1461
1020
  }
1462
1021
 
1463
1022
  /**
1464
1023
  * Dispose of resources and clean up the file system instance
1465
- *
1466
- * This method should be called when the file system instance is no longer needed
1467
- * to properly clean up resources like the broadcast channel and watch timers.
1468
1024
  */
1469
1025
  dispose(): void {
1470
1026
  if (this.broadcastChannel) {
@@ -1473,72 +1029,46 @@ export class OPFSWorker {
1473
1029
  }
1474
1030
 
1475
1031
  this.watchers.clear();
1032
+ }
1033
+ }
1476
1034
 
1477
- // Close each shared sync handle once (not per FD)
1478
- for (const [path, shared] of this.openHandles) {
1479
- safeCloseSyncHandle(-1, shared.syncHandle, path);
1480
- }
1035
+ function importEntryByteLength(data: ImportFileData): number {
1036
+ if (typeof data === 'string') {
1037
+ return new TextEncoder().encode(data).byteLength;
1038
+ }
1481
1039
 
1482
- this.openHandles.clear();
1483
- this.openFiles.clear();
1484
- this.nextFd = 1;
1040
+ if (data instanceof Blob) {
1041
+ return data.size;
1485
1042
  }
1486
1043
 
1487
- /**
1488
- * Synchronize the file system with external data
1489
- *
1490
- * Syncs the file system with an array of entries containing paths and data.
1491
- * This is useful for importing data from external sources or syncing with remote data.
1492
- *
1493
- * @param entries - Array of [path, data] tuples to sync
1494
- * @returns Promise that resolves when synchronization is complete
1495
- * @throws {OPFSError} If the synchronization fails
1496
- *
1497
- * @example
1498
- * ```typescript
1499
- * // Sync with external data
1500
- * const entries: [string, string | Uint8Array | Blob][] = [
1501
- * ['/config.json', JSON.stringify({ theme: 'dark' })],
1502
- * ['/data/binary.dat', new Uint8Array([1, 2, 3, 4])],
1503
- * ['/upload.txt', new Blob(['file content'], { type: 'text/plain' })]
1504
- * ];
1505
- *
1506
- * // Sync without clearing existing files
1507
- * await fs.sync(entries);
1508
- *
1509
- * // Clean file system and then sync
1510
- * await fs.sync(entries, { cleanBefore: true });
1511
- * ```
1512
- */
1513
- async createIndex(entries: [string, string | Uint8Array | Blob][]): Promise<void> {
1514
- await this.mount();
1044
+ return data.byteLength;
1045
+ }
1515
1046
 
1516
- try {
1517
- for (const [path, data] of entries) {
1518
- const normalizedPath = normalizePath(path);
1047
+ function toImportStream(data: ImportFileData): { stream: ReadableStream<Uint8Array>; size: number } {
1048
+ if (data instanceof Blob) {
1049
+ return { stream: data.stream(), size: data.size };
1050
+ }
1519
1051
 
1520
- let fileData: Uint8Array;
1052
+ const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
1521
1053
 
1522
- if (data instanceof Blob) {
1523
- fileData = await convertBlobToUint8Array(data);
1524
- }
1525
- else if (typeof data === 'string') {
1526
- // Convert string to Uint8Array using UTF-8 encoding
1527
- fileData = new TextEncoder().encode(data);
1528
- }
1529
- else {
1530
- fileData = data;
1531
- }
1054
+ return {
1055
+ size: bytes.byteLength,
1056
+ stream: new ReadableStream({
1057
+ start(controller) {
1058
+ controller.enqueue(bytes);
1059
+ controller.close();
1060
+ },
1061
+ }),
1062
+ };
1063
+ }
1532
1064
 
1533
- await this.writeFile(normalizedPath, fileData);
1534
- }
1535
- }
1536
- catch (error) {
1537
- if (error instanceof OPFSError) {
1538
- throw error;
1539
- }
1065
+ let createIndexWarned = false;
1540
1066
 
1541
- throw mapDomError(error);
1542
- }
1067
+ function warnCreateIndexDeprecated(): void {
1068
+ if (createIndexWarned) {
1069
+ return;
1543
1070
  }
1071
+
1072
+ createIndexWarned = true;
1073
+ console.warn('[opfs-worker] createIndex() is deprecated; use importFiles() instead');
1544
1074
  }