All files / src/node-to-fsa NodeFileSystemDirectoryHandle.ts

89.47% Statements 85/95
80.76% Branches 42/52
100% Functions 8/8
90% Lines 72/80

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 2216x 6x                 6x                             6x           289x       289x 289x 289x 289x                   7x 11x             9x 9x 9x 11x 11x 11x 11x 5x                     12x                                 106x 91x 91x 91x 76x 73x   18x 15x 15x   15x 10x 9x 9x   5x                                         126x 110x 110x 110x 84x 74x   36x 26x 26x   26x 19x 18x 18x   7x                                         38x 36x 20x 20x 20x 20x 18x 4x 14x 14x     6x 6x 6x   2x           4x                                 5x       5x 5x 5x 4x 4x 3x 3x 3x          
import { NodeFileSystemHandle } from './NodeFileSystemHandle';
import {
  assertCanWrite,
  assertName,
  basename,
  ctx as createCtx,
  newNotAllowedError,
  newNotFoundError,
  newTypeMismatchError,
} from './util';
import { NodeFileSystemFileHandle } from './NodeFileSystemFileHandle';
import type { NodeFsaContext, NodeFsaFs } from './types';
import type Dirent from '../node/Dirent';
import type {
  GetDirectoryHandleOptions,
  GetFileHandleOptions,
  IFileSystemDirectoryHandle,
  IFileSystemFileHandle,
  IFileSystemHandle,
  RemoveEntryOptions,
} from '../fsa/types';
 
/**
 * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
 */
export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implements IFileSystemDirectoryHandle {
  protected readonly ctx: NodeFsaContext;
  /** Directory path with trailing slash. */
  public readonly __path: string;
 
  public constructor(
    protected readonly fs: NodeFsaFs,
    path: string,
    ctx: Partial<NodeFsaContext> = {},
  ) {
    const fullCtx = createCtx(ctx);
    super('directory', basename(path, fullCtx.separator));
    this.ctx = fullCtx;
    this.__path = path[path.length - 1] === this.ctx.separator ? path : path + this.ctx.separator;
  }
 
  /**
   * Returns a new array iterator containing the keys for each item in
   * {@link NodeFileSystemDirectoryHandle} object.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/keys
   */
  public async *keys(): AsyncIterableIterator<string> {
    const list = await this.fs.promises.readdir(this.__path);
    for (const name of list) yield '' + name;
  }
 
  /**
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/entries
   */
  public async *entries(): AsyncIterableIterator<[string, NodeFileSystemHandle]> {
    const { __path: path, fs, ctx } = this;
    const list = await fs.promises.readdir(path, { withFileTypes: true });
    for (const d of list) {
      const dirent = d as Dirent;
      const name = dirent.name + '';
      const newPath = path + name;
      if (dirent.isDirectory()) yield [name, new NodeFileSystemDirectoryHandle(fs, newPath, ctx)];
      else if (dirent.isFile()) yield [name, new NodeFileSystemFileHandle(fs, newPath, ctx)];
    }
  }
 
  /**
   * Returns a new array iterator containing the values for each index in the
   * {@link FileSystemDirectoryHandle} object.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/values
   */
  public async *values(): AsyncIterableIterator<NodeFileSystemHandle> {
    for await (const [, value] of this.entries()) yield value;
  }
 
  /**
   * Returns a {@link NodeFileSystemDirectoryHandle} for a subdirectory with the specified
   * name within the directory handle on which the method is called.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getDirectoryHandle
   * @param name A string representing the {@link NodeFileSystemHandle} name of
   *        the subdirectory you wish to retrieve.
   * @param options An optional object containing options for the retrieved
   *        subdirectory.
   */
  public async getDirectoryHandle(
    name: string,
    options?: GetDirectoryHandleOptions,
  ): Promise<IFileSystemDirectoryHandle> {
    assertName(name, 'getDirectoryHandle', 'FileSystemDirectoryHandle');
    const filename = this.__path + name;
    try {
      const stats = await this.fs.promises.stat(filename);
      if (!stats.isDirectory()) throw newTypeMismatchError();
      return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx);
    } catch (error) {
      if (error instanceof DOMException) throw error;
      if (error && typeof error === 'object') {
        switch (error.code) {
          case 'ENOENT': {
            if (options?.create) {
              assertCanWrite(this.ctx.mode!);
              await this.fs.promises.mkdir(filename);
              return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx);
            }
            throw newNotFoundError();
          }
          case 'EPERM':
          case 'EACCES':
            throw newNotAllowedError();
        }
      }
      throw error;
    }
  }
 
  /**
   * Returns a {@link FileSystemFileHandle} for a file with the specified name,
   * within the directory the method is called.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getFileHandle
   * @param name A string representing the {@link NodeFileSystemHandle} name of
   *        the file you wish to retrieve.
   * @param options An optional object containing options for the retrieved file.
   */
  public async getFileHandle(name: string, options?: GetFileHandleOptions): Promise<IFileSystemFileHandle> {
    assertName(name, 'getFileHandle', 'FileSystemDirectoryHandle');
    const filename = this.__path + name;
    try {
      const stats = await this.fs.promises.stat(filename);
      if (!stats.isFile()) throw newTypeMismatchError();
      return new NodeFileSystemFileHandle(this.fs, filename, this.ctx);
    } catch (error) {
      if (error instanceof DOMException) throw error;
      if (error && typeof error === 'object') {
        switch (error.code) {
          case 'ENOENT': {
            if (options?.create) {
              assertCanWrite(this.ctx.mode!);
              await this.fs.promises.writeFile(filename, '');
              return new NodeFileSystemFileHandle(this.fs, filename, this.ctx);
            }
            throw newNotFoundError();
          }
          case 'EPERM':
          case 'EACCES':
            throw newNotAllowedError();
        }
      }
      throw error;
    }
  }
 
  /**
   * Attempts to remove an entry if the directory handle contains a file or
   * directory called the name specified.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/removeEntry
   * @param name A string representing the {@link FileSystemHandle} name of the
   *        entry you wish to remove.
   * @param options An optional object containing options.
   */
  public async removeEntry(name: string, { recursive = false }: RemoveEntryOptions = {}): Promise<void> {
    assertCanWrite(this.ctx.mode!);
    assertName(name, 'removeEntry', 'FileSystemDirectoryHandle');
    const filename = this.__path + name;
    const promises = this.fs.promises;
    try {
      const stats = await promises.stat(filename);
      if (stats.isFile()) {
        await promises.unlink(filename);
      } else if (stats.isDirectory()) {
        await promises.rmdir(filename, { recursive });
      } else Ethrow newTypeMismatchError();
    } catch (error) {
      Iif (error instanceof DOMException) throw error;
      if (error && typeof error === 'object') {
        switch (error.code) {
          case 'ENOENT': {
            throw newNotFoundError();
          }
          case 'EPERM':
          case 'EACCES':
            throw newNotAllowedError();
          case 'ENOTEMPTY':
            throw new DOMException('The object can not be modified in this way.', 'InvalidModificationError');
        }
      }
      throw error;
    }
  }
 
  /**
   * The `resolve()` method of the {@link FileSystemDirectoryHandle} interface
   * returns an {@link Array} of directory names from the parent handle to the specified
   * child entry, with the name of the child entry as the last array item.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/resolve
   * @param possibleDescendant The {@link IFileSystemHandle} from which
   *        to return the relative path.
   */
  public async resolve(possibleDescendant: IFileSystemHandle): Promise<string[] | null> {
    if (
      possibleDescendant instanceof NodeFileSystemDirectoryHandle ||
      possibleDescendant instanceof NodeFileSystemFileHandle
    ) {
      const path = this.__path;
      const childPath = (possibleDescendant as any).__path;
      if (!childPath.startsWith(path)) return null;
      let relative = childPath.slice(path.length);
      if (relative === '') return [];
      const separator = this.ctx.separator!;
      Iif (relative[0] === separator) relative = relative.slice(1);
      return relative.split(separator);
    }
    return null;
  }
}