opfs-worker 2.0.0 → 2.1.0

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 (135) hide show
  1. package/README.md +62 -439
  2. package/dist/BaseOPFS-BPw9AIQ-.cjs +4 -0
  3. package/dist/BaseOPFS-BPw9AIQ-.cjs.map +1 -0
  4. package/dist/BaseOPFS-Cylz6ijk.js +2036 -0
  5. package/dist/BaseOPFS-Cylz6ijk.js.map +1 -0
  6. package/dist/OPFSAsync-BKSAJJeh.js +124 -0
  7. package/dist/OPFSAsync-BKSAJJeh.js.map +1 -0
  8. package/dist/OPFSAsync-s2P7y3sw.cjs +2 -0
  9. package/dist/OPFSAsync-s2P7y3sw.cjs.map +1 -0
  10. package/dist/OPFSFacade-DePnNRgt.cjs +2 -0
  11. package/dist/OPFSFacade-DePnNRgt.cjs.map +1 -0
  12. package/dist/OPFSFacade-XohWlHuu.js +281 -0
  13. package/dist/OPFSFacade-XohWlHuu.js.map +1 -0
  14. package/dist/OPFSSync-C7KBepe_.cjs +2 -0
  15. package/dist/OPFSSync-C7KBepe_.cjs.map +1 -0
  16. package/dist/OPFSSync-CsSQkpk5.js +208 -0
  17. package/dist/OPFSSync-CsSQkpk5.js.map +1 -0
  18. package/dist/assets/dedicated.worker-nY3tK4zi.js.map +1 -0
  19. package/dist/{OPFSWorker.d.ts → core/BaseOPFS.d.ts} +34 -304
  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-BwKDmHsy.cjs +2 -0
  26. package/dist/createOPFSAsync-BwKDmHsy.cjs.map +1 -0
  27. package/dist/createOPFSAsync-Y4Mg6qte.js +10 -0
  28. package/dist/createOPFSAsync-Y4Mg6qte.js.map +1 -0
  29. package/dist/createOPFSDedicated-CsnV6MLP.js +2333 -0
  30. package/dist/createOPFSDedicated-CsnV6MLP.js.map +1 -0
  31. package/dist/createOPFSDedicated-pKTRWLdz.cjs +2283 -0
  32. package/dist/createOPFSDedicated-pKTRWLdz.cjs.map +1 -0
  33. package/dist/createOPFSShared-CGdd__PH.js +23 -0
  34. package/dist/createOPFSShared-CGdd__PH.js.map +1 -0
  35. package/dist/createOPFSShared-CskROt7U.cjs +2 -0
  36. package/dist/createOPFSShared-CskROt7U.cjs.map +1 -0
  37. package/dist/dedicated.worker.js +2281 -0
  38. package/dist/dedicated.worker.js.map +1 -0
  39. package/dist/{OPFSFacade.d.ts → facade/OPFSFacade.d.ts} +31 -23
  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 +2166 -0
  78. package/dist/shared.worker.js.map +1 -0
  79. package/dist/types.d.ts +9 -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/README.md +30 -0
  90. package/docs/api/backend.md +36 -0
  91. package/docs/api/create.md +82 -0
  92. package/docs/api/facade.md +52 -0
  93. package/docs/choosing-a-mode.md +41 -0
  94. package/docs/file-descriptors.md +8 -9
  95. package/docs/guides/async.md +36 -0
  96. package/docs/guides/dedicated.md +76 -0
  97. package/docs/guides/hashing.md +34 -0
  98. package/docs/guides/pure.md +46 -0
  99. package/docs/guides/sharedworker.md +57 -0
  100. package/docs/guides/streaming.md +51 -0
  101. package/docs/guides/watching.md +35 -0
  102. package/docs/migration.md +41 -0
  103. package/docs/types.md +20 -6
  104. package/package.json +42 -3
  105. package/src/{OPFSWorker.ts → core/BaseOPFS.ts} +54 -647
  106. package/src/core/OPFSAsync.ts +248 -0
  107. package/src/core/OPFSSync.ts +460 -0
  108. package/src/{OPFSFacade.ts → facade/OPFSFacade.ts} +83 -54
  109. package/src/facade/createOPFSAsync.ts +22 -0
  110. package/src/facade/createOPFSDedicated.ts +23 -0
  111. package/src/facade/createOPFSShared.ts +30 -0
  112. package/src/index.async.ts +11 -0
  113. package/src/index.pure.ts +3 -2
  114. package/src/index.sharedworker.ts +12 -0
  115. package/src/index.sync.ts +12 -0
  116. package/src/index.ts +37 -8
  117. package/src/types.ts +8 -4
  118. package/src/worker/createDedicatedWorker.ts +74 -0
  119. package/src/worker/createSharedWorker.ts +67 -0
  120. package/src/worker/dedicated.worker.ts +13 -0
  121. package/src/worker/shared.worker.ts +17 -0
  122. package/dist/OPFSFacade.d.ts.map +0 -1
  123. package/dist/OPFSWorker.d.ts.map +0 -1
  124. package/dist/assets/worker.entry-DUlEoroc.js.map +0 -1
  125. package/dist/createOPFSWorker.d.ts +0 -17
  126. package/dist/createOPFSWorker.d.ts.map +0 -1
  127. package/dist/helpers-DNj8ZoMu.cjs +0 -4
  128. package/dist/helpers-DNj8ZoMu.cjs.map +0 -1
  129. package/dist/helpers-WY2jfbOT.js +0 -1427
  130. package/dist/helpers-WY2jfbOT.js.map +0 -1
  131. package/dist/worker.entry.d.ts +0 -2
  132. package/dist/worker.entry.d.ts.map +0 -1
  133. package/docs/api-reference.md +0 -815
  134. package/src/createOPFSWorker.ts +0 -57
  135. 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,14 @@ 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
19
  convertBlobToUint8Array,
25
- createSyncHandleSafe,
26
20
  dirname,
27
21
  joinPath,
28
22
  matchMinimatch,
@@ -30,44 +24,32 @@ import {
30
24
  normalizePath,
31
25
  removeEntry,
32
26
  resolvePath,
33
- safeCloseSyncHandle,
34
- splitPath,
35
- validateReadWriteArgs,
36
- withLock
37
- } from './utils/helpers';
27
+ splitPath
28
+ } from '../utils/helpers';
38
29
 
39
- import type { DirentData, FileOpenOptions, FileStat, OPFSOptions, RenameOptions, WatchEvent, WatchOptions, WatchSnapshot } from './types';
30
+ import type { DirentData, FileStat, OPFSOptions, RenameOptions, WatchEvent, WatchOptions, WatchSnapshot } from '../types';
40
31
 
41
32
 
42
33
  /**
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
- * ```
34
+ * Shared OPFS logic (directories, meta, watch, high-level helpers).
35
+ * File I/O backends implement {@link readFile}, {@link writeFile}, {@link appendFile},
36
+ * and {@link writeStream}.
55
37
  */
56
- export class OPFSWorker {
38
+ export abstract class BaseOPFS {
57
39
  /** Root directory handle for the file system */
58
- private root!: FileSystemDirectoryHandle;
40
+ protected root!: FileSystemDirectoryHandle;
59
41
 
60
42
  /** Map of watched paths and options */
61
- private watchers = new Map<string, WatchSnapshot>();
43
+ protected watchers = new Map<string, WatchSnapshot>();
62
44
 
63
45
  /** Promise to prevent concurrent mount operations */
64
- private mountingPromise: Promise<boolean> | null = null;
46
+ protected mountingPromise: Promise<boolean> | null = null;
65
47
 
66
48
  /** BroadcastChannel instance for sending events */
67
- private broadcastChannel: BroadcastChannel | null = null;
49
+ protected broadcastChannel: BroadcastChannel | null = null;
68
50
 
69
51
  /** Configuration options */
70
- private options: Required<OPFSOptions> = {
52
+ protected options: Required<OPFSOptions> = {
71
53
  root: '/',
72
54
  namespace: '',
73
55
  maxFileSize: 50 * 1024 * 1024,
@@ -75,39 +57,6 @@ export class OPFSWorker {
75
57
  broadcastChannel: 'opfs-worker',
76
58
  };
77
59
 
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
60
  /**
112
61
  * Notify about internal changes to the file system
113
62
  *
@@ -116,7 +65,7 @@ export class OPFSWorker {
116
65
  *
117
66
  * @param event - The event describing the change
118
67
  */
119
- private async notifyChange(event: Omit<WatchEvent, 'timestamp' | 'hash' | 'namespace'>): Promise<void> {
68
+ protected async notifyChange(event: Omit<WatchEvent, 'timestamp' | 'hash' | 'namespace'>): Promise<void> {
120
69
  // This instance not configured to send events
121
70
  if (!this.options.broadcastChannel) {
122
71
  return;
@@ -167,16 +116,6 @@ export class OPFSWorker {
167
116
  }
168
117
  }
169
118
 
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
119
  constructor(options?: OPFSOptions) {
181
120
  checkOPFSSupport();
182
121
 
@@ -185,27 +124,27 @@ export class OPFSWorker {
185
124
  }
186
125
  }
187
126
 
127
+ /** Read file contents as bytes */
128
+ abstract readFile(path: string): Promise<Uint8Array>;
129
+
130
+ /** Create or overwrite a file with binary data */
131
+ abstract writeFile(path: string, data: Uint8Array | ArrayBuffer): Promise<void>;
132
+
133
+ /** Append binary data to a file */
134
+ abstract appendFile(path: string, data: Uint8Array | ArrayBuffer): Promise<void>;
135
+
136
+ /** Create or overwrite a file from a byte stream */
137
+ abstract writeStream(
138
+ path: string,
139
+ stream: ReadableStream<Uint8Array>,
140
+ onProgress?: (bytesWritten: number) => unknown
141
+ ): Promise<number>;
142
+
188
143
  /**
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
- * ```
144
+ * Initialize the file system within a given directory.
145
+ * If no root is specified, uses the OPFS root directory.
207
146
  */
208
- private async mount(): Promise<boolean> {
147
+ protected async mount(): Promise<boolean> {
209
148
  const root = this.options.root;
210
149
 
211
150
  // If already mounting, wait for previous operation to complete first
@@ -296,7 +235,7 @@ export class OPFSWorker {
296
235
  * const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
297
236
  * ```
298
237
  */
299
- private async getDirectoryHandle(path: string | string[], create: boolean = false, from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemDirectoryHandle> {
238
+ protected async getDirectoryHandle(path: string | string[], create: boolean = false, from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemDirectoryHandle> {
300
239
  const segments = Array.isArray(path) ? path : splitPath(path);
301
240
  let current = from;
302
241
 
@@ -326,7 +265,7 @@ export class OPFSWorker {
326
265
  * const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
327
266
  * ```
328
267
  */
329
- private async getFileHandle(path: string | string[], create = false, _from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemFileHandle> {
268
+ protected async getFileHandle(path: string | string[], create = false, _from: FileSystemDirectoryHandle | null = this.root): Promise<FileSystemFileHandle> {
330
269
  const segments = splitPath(path);
331
270
 
332
271
  if (segments.length === 0) {
@@ -396,149 +335,6 @@ export class OPFSWorker {
396
335
  return result;
397
336
  }
398
337
 
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
338
  /**
543
339
  * Create a directory
544
340
  *
@@ -1095,419 +891,18 @@ export class OPFSWorker {
1095
891
  }
1096
892
 
1097
893
  /**
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
- *
1109
- * @example
1110
- * ```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 });
1122
- * ```
1123
- */
1124
- async open(path: string, options?: FileOpenOptions): Promise<number> {
1125
- await this.mount();
1126
-
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
- 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);
1137
-
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);
1147
- }
1148
- catch (error: any) {
1149
- if (error instanceof OPFSError) {
1150
- throw error;
1151
- }
1152
-
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
- });
1161
- }
1162
- }
1163
-
1164
- /**
1165
- * Internal method to open a file (without locking)
894
+ * Bulk-create files from `[path, data]` entries (strings, bytes, or Blobs).
1166
895
  *
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
896
+ * @param entries - Array of `[path, data]` tuples
897
+ * @throws {OPFSError} If a write fails
1320
898
  *
1321
899
  * @example
1322
900
  * ```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);
1328
- * ```
1329
- */
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 });
1355
-
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
- }
1361
-
1362
- if (emitEvent) {
1363
- await this.notifyChange({ path: fileInfo.path, type: WatchEventType.Changed, isDirectory: false });
1364
- }
1365
-
1366
- return bytesWritten;
1367
- }
1368
- 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;
1429
- }
1430
-
1431
- await this.notifyChange({ path: fileInfo.path, type: WatchEventType.Changed, isDirectory: false });
1432
- }
1433
- catch (error) {
1434
- throw createFDError('truncate', fd, fileInfo.path, error);
1435
- }
1436
- }
1437
-
1438
- /**
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
- * ```
1451
- */
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
- }
1461
- }
1462
-
1463
- /**
1464
- * 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
- */
1469
- dispose(): void {
1470
- if (this.broadcastChannel) {
1471
- this.broadcastChannel.close();
1472
- this.broadcastChannel = null;
1473
- }
1474
-
1475
- this.watchers.clear();
1476
-
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
- }
1481
-
1482
- this.openHandles.clear();
1483
- this.openFiles.clear();
1484
- this.nextFd = 1;
1485
- }
1486
-
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][] = [
901
+ * await fs.createIndex([
1501
902
  * ['/config.json', JSON.stringify({ theme: 'dark' })],
1502
903
  * ['/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 });
904
+ * ['/upload.txt', new Blob(['file content'], { type: 'text/plain' })],
905
+ * ]);
1511
906
  * ```
1512
907
  */
1513
908
  async createIndex(entries: [string, string | Uint8Array | Blob][]): Promise<void> {
@@ -1541,4 +936,16 @@ export class OPFSWorker {
1541
936
  throw mapDomError(error);
1542
937
  }
1543
938
  }
939
+
940
+ /**
941
+ * Dispose of resources and clean up the file system instance
942
+ */
943
+ dispose(): void {
944
+ if (this.broadcastChannel) {
945
+ this.broadcastChannel.close();
946
+ this.broadcastChannel = null;
947
+ }
948
+
949
+ this.watchers.clear();
950
+ }
1544
951
  }