Preparing search index...

    Provides safe file system operations within a specified directory. All operations are scoped to the storage directory to prevent unauthorized file access.

    // Create storage in a temporary directory
    const tempStorage = new ScopedStorage();

    // Create storage in a specific directory
    const storage = new ScopedStorage('/path/to/directory');
    // Write and read data
    const storage = new ScopedStorage();
    const configPath = storage.createFile('config.json', JSON.stringify({
    version: '1.0',
    enabled: true
    }));

    const configData = storage.readFile(configPath);
    const config = JSON.parse(configData);

    Implements

    Index

    Constructors

    • Creates a new ScopedStorage instance.

      Parameters

      • Optionalpath: string | Path

        Optional base directory path for storage operations. If not provided, a temporary directory is created.

      Returns ScopedStorage

      const tempStorage = new ScopedStorage();
      const customStorage = new ScopedStorage('/path/to/directory');

    Properties

    deducePath: any

    Converts a string or Editor.Path to Editor.Path.

    The path to convert

    The path as an Editor.Path object

    directory: Directory
    read: any

    Generic read method that accepts a custom read function. Ensures the file is within the storage directory before reading.

    Path to the file (relative to storage directory or absolute)

    Custom function to read the file

    Result of the read function, or null if the file is outside the storage directory

    const data = storage.read('/path/to/file', FileSystem.readFile);
    
    verifyPath: any

    Verifies that a file path is within the storage directory. Prevents directory traversal attacks and unauthorized file access.

    The file path to verify

    True if the path is within the storage directory, false otherwise

    Accessors

    Methods

    • Creates a new file in the storage directory.

      Parameters

      • fileName: string

        The name of the file to create

      • content: string | Uint8Array

        The file content as a string or byte array

      Returns Path

      The full path to the created file

      If the resolved path is outside the storage directory

      const filePath = storage.createFile('config.json', '{"key": "value"}');
      const imagePath = storage.createFile('image.png', imageBytes);
    • Reads a file as a byte array.

      Parameters

      • path: string | Path

        Path to the file (relative to storage directory or absolute)

      Returns Uint8Array

      File content as Uint8Array, or null if the file is outside the storage directory

      const bytes = storage.readBytes(storage.path.appended(new Editor.Path('image.png')));
      
    • Reads a file as a string.

      Parameters

      • path: string | Path

        Path to the file (relative to storage directory or absolute)

      Returns string

      File content as string, or null if the file is outside the storage directory

      const content = storage.readFile(storage.path.appended(new Editor.Path('config.json')));
      const config = JSON.parse(content);
    • Unpacks a ZIP archive into the storage directory.

      Parameters

      • archivePath: string | Path

        Path to the ZIP archive

      Returns Path

      Path to the unpacked directory

      const unpackedPath = storage.unpackContent(storage.path.appended(new Editor.Path('archive.zip')));