UNPKG

opfs-worker

Version:

A robust TypeScript library for working with Origin Private File System (OPFS) through Web Workers

203 lines 7.44 kB
import type { BufferEncoding } from 'typescript'; /** * Common binary file extensions */ export declare const BINARY_FILE_EXTENSIONS: readonly [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg", ".ico", ".tiff", ".tga", ".mp3", ".wav", ".ogg", ".flac", ".aac", ".wma", ".m4a", ".mp4", ".avi", ".mov", ".wmv", ".flv", ".webm", ".mkv", ".m4v", ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".zip", ".rar", ".7z", ".tar", ".gz", ".bz2", ".exe", ".dll", ".so", ".dylib", ".bin", ".dat", ".db", ".sqlite", ".bin", ".obj", ".fbx", ".3ds"]; /** * Check if a file extension indicates a binary file * * @param path - The file path or filename * @returns True if the file extension suggests binary content * * @example * ```typescript * isBinaryFileExtension('/path/to/image.jpg'); // true * isBinaryFileExtension('/path/to/document.txt'); // false * isBinaryFileExtension('data.bin'); // true * ``` */ export declare function isBinaryFileExtension(path: string): boolean; /** * Check if the browser supports the OPFS API * * @throws {OPFSNotSupportedError} If the browser does not support the OPFS API */ export declare function checkOPFSSupport(): void; export declare function withLock<T>(path: string, mode: 'shared' | 'exclusive', fn: () => Promise<T>): Promise<T>; /** * Split a path into an array of segments * * @param path - The path to split * @returns The array of segments * * @example * ```typescript * splitPath('/path/to/file'); // ['path', 'to', 'file'] * splitPath('~/path/to/file'); // ['path', 'to', 'file'] (home dir handled) * splitPath('relative/path'); // ['relative', 'path'] * ``` */ export declare function splitPath(path: string | string[]): string[]; /** * Join an array of path segments into a single path * * @param segments - The array of path segments * @returns The joined path */ export declare function joinPath(segments: string[] | string): string; /** * Extract the filename from a path * * @param path - The file path * @returns The filename without the directory path * * @example * ```typescript * basename('/path/to/file.txt'); // 'file.txt' * basename('/path/to/directory/'); // '' * basename('file.txt'); // 'file.txt' * ``` */ export declare function basename(path: string): string; /** * Extract the directory path from a file path * * @param path - The file path * @returns The directory path without the filename * * @example * ```typescript * dirname('/path/to/file.txt'); // '/path/to' * dirname('/path/to/directory/'); // '/path/to/directory' * dirname('file.txt'); // '/' * ``` */ export declare function dirname(path: string): string; /** * Normalize a path to ensure it starts with '/' * * @param path - The path to normalize * @returns The normalized path * * @example * ```typescript * normalizePath('path/to/file'); // '/path/to/file' * normalizePath('/path/to/file'); // '/path/to/file' * normalizePath('~/path/to/file'); // '/path/to/file' (home dir normalized to root) * normalizePath(''); // '/' * ``` */ export declare function normalizePath(path: string): string; export declare function normalizeMinimatch(path: string, recursive?: boolean): string; export declare function matchMinimatch(path: string, pattern: string): boolean; /** * Check if a path matches any of the provided exclude patterns (minimatch syntax) * * @param path - Absolute or relative path * @param patterns - Glob pattern(s) to match against * @returns true if excluded, false otherwise */ export declare function isPathExcluded(path: string, patterns?: string | string[]): boolean; /** * Resolve a path to an absolute path, handling relative segments * * @param path - The path to resolve * @returns The resolved absolute path * * @example * ```typescript * resolvePath('./config/../data/file.txt'); // '/data/file.txt' * resolvePath('/path/to/../file.txt'); // '/path/file.txt' * resolvePath('../../file.txt'); // '/file.txt' (truncated to root) * resolvePath('~/config/../data/file.txt'); // '/data/file.txt' (home dir normalized to root) * ``` */ export declare function resolvePath(path: string): string; /** * Get the file extension from a path * * @param path - The file path * @returns The file extension including the dot, or empty string if no extension * * @example * ```typescript * extname('/path/to/file.txt'); // '.txt' * extname('/path/to/file'); // '' * extname('/path/to/file.name.ext'); // '.ext' * extname('/path/to/.hidden'); // '' * ``` */ export declare function extname(path: string): string; export declare function createBuffer(data: string | Uint8Array | ArrayBuffer, encoding?: BufferEncoding): Uint8Array; /** * Read raw binary data from a file using a file handle * * @param fileHandle - The file handle to read from * @returns The raw binary data as Uint8Array */ export declare function readFileData(fileHandle: FileSystemFileHandle, path: string): Promise<Uint8Array>; /** * Write data to a file using a file handle * * @param fileHandle - The file handle to write to * @param data - The data to write to the file * @param encoding - The encoding to use * @param options - Write options (truncate or append) * @param path - Optional path for locking (if not provided, no locking is used) */ export declare function writeFileData(fileHandle: FileSystemFileHandle, data: string | Uint8Array | ArrayBuffer, encoding?: BufferEncoding, path?: string, options?: { truncate?: boolean; append?: boolean; }): Promise<void>; /** * Calculate file hash using Web Crypto API * * @param buffer - The file content as File, ArrayBuffer, or Uint8Array * @param algorithm - Hash algorithm to use (default: 'SHA-1') * @param maxSize - Maximum file size in bytes. If file is larger, throws error (default: 50MB) * @returns Promise that resolves to the hash string * @throws Error if file size exceeds maxSize */ export declare function calculateFileHash(buffer: File | ArrayBuffer | Uint8Array, algorithm?: string, maxSize?: number): Promise<string>; /** * Compare two Uint8Array buffers for equality * * @param a - First buffer * @param b - Second buffer * @returns true if buffers are equal, false otherwise */ export declare function buffersEqual(a: Uint8Array, b: Uint8Array): boolean; /** * Convert a Blob to Uint8Array * * This function converts a Blob object to a Uint8Array for use with file operations. * It's useful when working with file uploads or other Blob data sources. * * @param blob - The Blob to convert * @returns Promise that resolves to the Uint8Array representation of the Blob * * @example * ```typescript * const fileInput = document.getElementById('file') as HTMLInputElement; * const file = fileInput.files?.[0]; * if (file) { * const data = await convertBlobToUint8Array(file); * await fs.writeFile('/uploaded-file', data); * } * } * ``` */ export declare function convertBlobToUint8Array(blob: Blob): Promise<Uint8Array>; /** * Remove a file or directory entry using a directory handle * * @param parentHandle - The parent directory handle * @param path - The full path of the entry to remove * @param options - Remove options (recursive, force, useTrash) */ export declare function removeEntry(parentHandle: FileSystemDirectoryHandle, path: string, options?: { recursive?: boolean; force?: boolean; useTrash?: boolean; }): Promise<void>; //# sourceMappingURL=helpers.d.ts.map