All files / src/fsa CoreFileSystemHandle.ts

100% Statements 13/13
100% Branches 6/6
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 674x                 4x       99x 99x     99x                 3x                       4x 4x     4x 1x       3x             1x                 1x      
import { CorePermissionStatus } from './CorePermissionStatus';
import type { IFileSystemHandle, FileSystemHandlePermissionDescriptor, CoreFsaContext } from './types';
 
/**
 * Represents a File System Access API file handle `FileSystemHandle` object,
 * which was created from a core `Superblock`.
 *
 * @see [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle)
 */
export abstract class CoreFileSystemHandle implements IFileSystemHandle {
  protected readonly ctx: CoreFsaContext;
 
  constructor(
    public readonly kind: 'file' | 'directory',
    public readonly name: string,
    ctx: CoreFsaContext,
  ) {
    this.ctx = ctx;
  }
 
  /**
   * Compares two handles to see if the associated entries (either a file or directory) match.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/isSameEntry
   */
  public isSameEntry(fileSystemHandle: CoreFileSystemHandle): boolean {
    return (
      this.constructor === fileSystemHandle.constructor && (this as any).__path === (fileSystemHandle as any).__path
    );
  }
 
  /**
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/queryPermission
   */
  public queryPermission(
    fileSystemHandlePermissionDescriptor: FileSystemHandlePermissionDescriptor,
  ): CorePermissionStatus {
    // Check if the requested mode is compatible with the context mode
    const requestedMode = fileSystemHandlePermissionDescriptor.mode;
    const contextMode = this.ctx.mode;
 
    // If requesting readwrite but context only allows read, deny
    if (requestedMode === 'readwrite' && contextMode === 'read') {
      return new CorePermissionStatus('denied', requestedMode);
    }
 
    // Otherwise grant the permission
    return new CorePermissionStatus('granted', requestedMode);
  }
 
  /**
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/remove
   */
  public async remove({ recursive }: { recursive?: boolean } = { recursive: false }): Promise<void> {
    throw new Error('Not implemented');
  }
 
  /**
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
   */
  public requestPermission(
    fileSystemHandlePermissionDescriptor: FileSystemHandlePermissionDescriptor,
  ): CorePermissionStatus {
    throw new Error('Not implemented');
  }
}