opfs-worker 1.0.0 → 1.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.
- package/README.md +19 -0
- package/dist/assets/worker-CvILLJKw.js.map +1 -0
- package/dist/helpers-CKqaiMjI.cjs +4 -0
- package/dist/helpers-CKqaiMjI.cjs.map +1 -0
- package/dist/{helpers-CTCvNFs1.js → helpers-DS5dyURe.js} +575 -510
- package/dist/helpers-DS5dyURe.js.map +1 -0
- package/dist/index.cjs +1074 -781
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1109 -810
- package/dist/index.js.map +1 -1
- package/dist/raw.cjs +1 -1
- package/dist/raw.cjs.map +1 -1
- package/dist/raw.js +331 -97
- package/dist/raw.js.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/encoder.d.ts +19 -0
- package/dist/utils/encoder.d.ts.map +1 -1
- package/dist/utils/errors.d.ts +23 -0
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/helpers.d.ts +40 -18
- package/dist/utils/helpers.d.ts.map +1 -1
- package/dist/worker.d.ts +159 -2
- package/dist/worker.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/assets/worker-XHCzp1qX.js.map +0 -1
- package/dist/helpers-CTCvNFs1.js.map +0 -1
- package/dist/helpers-Cvjm0f_r.cjs +0 -4
- package/dist/helpers-Cvjm0f_r.cjs.map +0 -1
package/dist/worker.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DirentData, Encoding, FileStat, OPFSOptions, RenameOptions, WatchOptions } from './types';
|
|
1
|
+
import type { DirentData, Encoding, FileOpenOptions, FileStat, OPFSOptions, RenameOptions, WatchOptions } from './types';
|
|
2
2
|
import type { BufferEncoding } from 'typescript';
|
|
3
3
|
/**
|
|
4
4
|
* OPFS (Origin Private File System) File System implementation
|
|
@@ -26,6 +26,15 @@ export declare class OPFSWorker {
|
|
|
26
26
|
private broadcastChannel;
|
|
27
27
|
/** Configuration options */
|
|
28
28
|
private options;
|
|
29
|
+
/** Map of open file descriptors to their metadata */
|
|
30
|
+
private openFiles;
|
|
31
|
+
/** Next available file descriptor number */
|
|
32
|
+
private nextFd;
|
|
33
|
+
/**
|
|
34
|
+
* Get file info by descriptor with validation
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
private _getFileDescriptor;
|
|
29
38
|
/**
|
|
30
39
|
* Notify about internal changes to the file system
|
|
31
40
|
*
|
|
@@ -107,7 +116,7 @@ export declare class OPFSWorker {
|
|
|
107
116
|
*
|
|
108
117
|
* @param path - The path to the file (string or array of segments)
|
|
109
118
|
* @param create - Whether to create the file if it doesn't exist (default: false)
|
|
110
|
-
* @param
|
|
119
|
+
* @param _from - The directory to start from (default: root directory)
|
|
111
120
|
* @returns Promise that resolves to the file handle
|
|
112
121
|
* @throws {PathError} If the path is empty
|
|
113
122
|
* @throws {OPFSError} If the file cannot be accessed or created
|
|
@@ -445,6 +454,154 @@ export declare class OPFSWorker {
|
|
|
445
454
|
* Stop watching a previously watched path
|
|
446
455
|
*/
|
|
447
456
|
unwatch(path: string): void;
|
|
457
|
+
/**
|
|
458
|
+
* Open a file and return a file descriptor
|
|
459
|
+
*
|
|
460
|
+
* @param path - The path to the file to open
|
|
461
|
+
* @param options - Options for opening the file
|
|
462
|
+
* @param options.create - Whether to create the file if it doesn't exist (default: false)
|
|
463
|
+
* @param options.exclusive - If true and create is true, fails if file already exists (default: false)
|
|
464
|
+
* Note: This is best-effort in OPFS, not fully atomic due to browser limitations
|
|
465
|
+
* @param options.truncate - Whether to truncate the file to zero length (default: false)
|
|
466
|
+
* @returns Promise that resolves to a file descriptor number
|
|
467
|
+
* @throws {OPFSError} If opening the file fails
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* ```typescript
|
|
471
|
+
* // Open existing file for reading
|
|
472
|
+
* const fd = await fs.open('/data/config.json');
|
|
473
|
+
*
|
|
474
|
+
* // Create new file for writing
|
|
475
|
+
* const fd = await fs.open('/data/new.txt', { create: true });
|
|
476
|
+
*
|
|
477
|
+
* // Create file exclusively (fails if exists)
|
|
478
|
+
* const fd = await fs.open('/data/unique.txt', { create: true, exclusive: true });
|
|
479
|
+
*
|
|
480
|
+
* // Open and truncate file
|
|
481
|
+
* const fd = await fs.open('/data/log.txt', { create: true, truncate: true });
|
|
482
|
+
* ```
|
|
483
|
+
*/
|
|
484
|
+
open(path: string, options?: FileOpenOptions): Promise<number>;
|
|
485
|
+
/**
|
|
486
|
+
* Internal method to open a file (without locking)
|
|
487
|
+
* @private
|
|
488
|
+
*/
|
|
489
|
+
private _openFile;
|
|
490
|
+
/**
|
|
491
|
+
* Close a file descriptor
|
|
492
|
+
*
|
|
493
|
+
* @param fd - The file descriptor to close
|
|
494
|
+
* @returns Promise that resolves when the file descriptor is closed
|
|
495
|
+
* @throws {OPFSError} If the file descriptor is invalid or closing fails
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
498
|
+
* ```typescript
|
|
499
|
+
* const fd = await fs.open('/data/file.txt');
|
|
500
|
+
* // ... use the file descriptor ...
|
|
501
|
+
* await fs.close(fd);
|
|
502
|
+
* ```
|
|
503
|
+
*/
|
|
504
|
+
close(fd: number): Promise<void>;
|
|
505
|
+
/**
|
|
506
|
+
* Read data from a file descriptor
|
|
507
|
+
*
|
|
508
|
+
* @param fd - The file descriptor to read from
|
|
509
|
+
* @param buffer - The buffer to read data into
|
|
510
|
+
* @param offset - The offset in the buffer to start writing at
|
|
511
|
+
* @param length - The number of bytes to read
|
|
512
|
+
* @param position - The position in the file to read from (null for current position)
|
|
513
|
+
* @returns Promise that resolves to the number of bytes read and the modified buffer
|
|
514
|
+
* @throws {OPFSError} If the file descriptor is invalid or reading fails
|
|
515
|
+
*
|
|
516
|
+
* @note This method uses Comlink.transfer() to efficiently pass the buffer as a Transferable Object,
|
|
517
|
+
* ensuring zero-copy performance across Web Worker boundaries.
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* ```typescript
|
|
521
|
+
* const fd = await fs.open('/data/file.txt');
|
|
522
|
+
* const buffer = new Uint8Array(1024);
|
|
523
|
+
* const { bytesRead, buffer: modifiedBuffer } = await fs.read(fd, buffer, 0, 1024, null);
|
|
524
|
+
* console.log(`Read ${bytesRead} bytes`);
|
|
525
|
+
* // Use modifiedBuffer which contains the actual data
|
|
526
|
+
* await fs.close(fd);
|
|
527
|
+
* ```
|
|
528
|
+
*/
|
|
529
|
+
read(fd: number, buffer: Uint8Array, offset: number, length: number, position: number | null | undefined): Promise<{
|
|
530
|
+
bytesRead: number;
|
|
531
|
+
buffer: Uint8Array;
|
|
532
|
+
}>;
|
|
533
|
+
/**
|
|
534
|
+
* Write data to a file descriptor
|
|
535
|
+
*
|
|
536
|
+
* @param fd - The file descriptor to write to
|
|
537
|
+
* @param buffer - The buffer containing data to write
|
|
538
|
+
* @param offset - The offset in the buffer to start reading from (default: 0)
|
|
539
|
+
* @param length - The number of bytes to write (default: entire buffer)
|
|
540
|
+
* @param position - The position in the file to write to (null/undefined for current position)
|
|
541
|
+
* @returns Promise that resolves to the number of bytes written
|
|
542
|
+
* @throws {OPFSError} If the file descriptor is invalid or writing fails
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* ```typescript
|
|
546
|
+
* const fd = await fs.open('/data/file.txt', { create: true });
|
|
547
|
+
* const data = new TextEncoder().encode('Hello, World!');
|
|
548
|
+
* const bytesWritten = await fs.write(fd, data, 0, data.length, null);
|
|
549
|
+
* console.log(`Wrote ${bytesWritten} bytes`);
|
|
550
|
+
* await fs.close(fd);
|
|
551
|
+
* ```
|
|
552
|
+
*/
|
|
553
|
+
write(fd: number, buffer: Uint8Array, offset?: number, length?: number, position?: number | null | undefined): Promise<number>;
|
|
554
|
+
/**
|
|
555
|
+
* Get file status information by file descriptor
|
|
556
|
+
*
|
|
557
|
+
* @param fd - The file descriptor
|
|
558
|
+
* @returns Promise that resolves to FileStat object
|
|
559
|
+
* @throws {OPFSError} If the file descriptor is invalid
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```typescript
|
|
563
|
+
* const fd = await fs.open('/data/file.txt');
|
|
564
|
+
* const stats = await fs.fstat(fd);
|
|
565
|
+
* console.log(`File size: ${stats.size} bytes`);
|
|
566
|
+
* console.log(`Last modified: ${stats.mtime}`);
|
|
567
|
+
*
|
|
568
|
+
* // If hashing is enabled, hash will be included
|
|
569
|
+
* if (stats.hash) {
|
|
570
|
+
* console.log(`Hash: ${stats.hash}`);
|
|
571
|
+
* }
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
fstat(fd: number): Promise<FileStat>;
|
|
575
|
+
/**
|
|
576
|
+
* Truncate file to specified size
|
|
577
|
+
*
|
|
578
|
+
* @param fd - The file descriptor
|
|
579
|
+
* @param size - The new size of the file (default: 0)
|
|
580
|
+
* @returns Promise that resolves when truncation is complete
|
|
581
|
+
* @throws {OPFSError} If the file descriptor is invalid or truncation fails
|
|
582
|
+
*
|
|
583
|
+
* @example
|
|
584
|
+
* ```typescript
|
|
585
|
+
* const fd = await fs.open('/data/file.txt', { create: true });
|
|
586
|
+
* await fs.truncate(fd, 100); // Truncate to 100 bytes
|
|
587
|
+
* ```
|
|
588
|
+
*/
|
|
589
|
+
ftruncate(fd: number, size?: number): Promise<void>;
|
|
590
|
+
/**
|
|
591
|
+
* Synchronize file data to storage (fsync equivalent)
|
|
592
|
+
*
|
|
593
|
+
* @param fd - The file descriptor
|
|
594
|
+
* @returns Promise that resolves when synchronization is complete
|
|
595
|
+
* @throws {OPFSError} If the file descriptor is invalid or sync fails
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```typescript
|
|
599
|
+
* const fd = await fs.open('/data/file.txt', { create: true });
|
|
600
|
+
* await fs.write(fd, data);
|
|
601
|
+
* await fs.fsync(fd); // Ensure data is written to storage
|
|
602
|
+
* ```
|
|
603
|
+
*/
|
|
604
|
+
fsync(fd: number): Promise<void>;
|
|
448
605
|
/**
|
|
449
606
|
* Dispose of resources and clean up the file system instance
|
|
450
607
|
*
|
package/dist/worker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAiCA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAc,YAAY,EAAiB,MAAM,SAAS,CAAC;AACpJ,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAGjD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,UAAU;IACnB,gDAAgD;IAChD,OAAO,CAAC,IAAI,CAA6B;IAEzC,uCAAuC;IACvC,OAAO,CAAC,QAAQ,CAAoC;IAEpD,qDAAqD;IACrD,OAAO,CAAC,eAAe,CAAiC;IAExD,mDAAmD;IACnD,OAAO,CAAC,gBAAgB,CAAiC;IAEzD,4BAA4B;IAC5B,OAAO,CAAC,OAAO,CAMb;IAEF,qDAAqD;IACrD,OAAO,CAAC,SAAS,CAKZ;IAEL,4CAA4C;IAC5C,OAAO,CAAC,MAAM,CAAK;IAEnB;;;OAGG;IACH,OAAO,CAAC,kBAAkB;IAW1B;;;;;;;;OAQG;YACW,YAAY;IAmD1B;;;;;;;;;OASG;gBACS,OAAO,CAAC,EAAE,WAAW;IAQjC;;;;;;;;;;;;;;;;;;;;OAoBG;YACW,KAAK;IA6BnB;;;;;;;;;OASG;IACG,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAkCrD;;;;;;;;;;;;;;;;;OAiBG;YACW,kBAAkB;IAWhC;;;;;;;;;;;;;;;;;;OAkBG;YACW,aAAa;IAa3B;;;;;;;;;;;;;;;;;;OAkBG;IACG,KAA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