async readdir()

in packages/web-ide-fs/src/browserfs/OverlayFSImpl.ts [164:189]


  async readdir(p: string): Promise<string[]> {
    // Original impl https://github.com/jvilk/BrowserFS/blob/v1.4.3/src/backend/OverlayFS.ts#L628
    // See "Read Strategy" in class description
    const stat = await this.stat(p, false);

    if (!stat.isDirectory()) {
      throw ApiError.ENOTDIR(p);
    }

    const wfiles = await readdirOrEmpty(this.#writable, p);

    // If this path is deleted, we can short circuit and just return files from writable
    if (await this.#deletedFiles.isDeleted(p)) {
      return wfiles;
    }

    const rfilesFilteredAsync = (await readdirOrEmpty(this.#readable, p)).map(async file => {
      // If the readable child is deleted, don't include
      const isDeleted = await this.#deletedFiles.isDeleted(joinPaths(p, file));

      return isDeleted ? [] : [file];
    });
    const rfiles = (await Promise.all(rfilesFilteredAsync)).flatMap(x => x);

    return uniq([...wfiles, ...rfiles]);
  }