opfs-tools
Version:
EN: A simple, high-performance, and comprehensive file system API running in the browser, built on [OPFS](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system).
87 lines (84 loc) • 3.03 kB
TypeScript
import { OTDir, dir } from './directory';
/**
* Retrieves a file wrapper instance for the specified file path.
* @param {string} filePath - The path of the file.
* @param {'r' | 'rw' | 'rw-unsafe'} mode - A string specifying the locking mode for the access handle. The default value is "rw"
* return A OTFile instance.
*
* @see [MDN createSyncAccessHandle](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle)
*
* @example
* // Read content from a file
const fileContent = await file('/path/to/file.txt', 'r').text();
console.log('File content:', fileContent);
// Check if a file exists
const fileExists = await file('/path/to/file.txt').exists();
console.log('File exists:', fileExists);
// Remove a file
await file('/path/to/file.txt').remove();
*/
export declare function file(filePath: string, mode?: ShortOpenMode): OTFile;
/**
* Writes content to the specified file.
* @param {string} target - The path of the file.
* @param {string | BufferSource | ReadableStream<BufferSource>} content - The content to write to the file.
* return A promise that resolves when the content is written to the file.
*
* @example
* // Write content to a file
await write('/path/to/file.txt', 'Hello, world!');
*/
export declare function write(target: string | OTFile, content: string | BufferSource | ReadableStream<BufferSource> | OTFile, opts?: {
overwrite: boolean;
}): Promise<void>;
type ShortOpenMode = 'r' | 'rw' | 'rw-unsafe';
/**
* Represents a wrapper for interacting with a file in the filesystem.
*/
export declare class OTFile {
#private;
get kind(): 'file';
get path(): string;
get name(): string;
get parent(): ReturnType<typeof dir> | null;
constructor(filePath: string, mode: ShortOpenMode);
/**
* Random write to file
*/
createWriter(): Promise<{
write: (chunk: string | BufferSource, opts?: {
at?: number;
}) => Promise<number>;
truncate: (size: number) => Promise<void>;
flush: () => Promise<void>;
close: () => Promise<void>;
}>;
/**
* Random access to file
*/
createReader(): Promise<{
read: (size: number, opts?: {
at?: number;
}) => Promise<ArrayBuffer>;
getSize: () => Promise<number>;
close: () => Promise<void>;
}>;
text(): Promise<string>;
arrayBuffer(): Promise<ArrayBuffer>;
stream(): Promise<ReadableStream<Uint8Array>>;
getOriginFile(): Promise<File | undefined>;
getSize(): Promise<number>;
exists(): Promise<boolean>;
remove(): Promise<void>;
/**
* If the target is a file, use current overwrite the target;
* if the target is a folder, copy the current file into that folder.
*/
copyTo(target: OTDir | OTFile): Promise<OTFile>;
copyTo(target: FileSystemFileHandle): Promise<null>;
/**
* move file, copy then remove current
*/
moveTo(target: OTDir | OTFile): Promise<OTFile>;
}
export {};