async open()

in packages/web-ide-fs/src/browserfs/OverlayFSImpl.ts [318:362]


  async open(p: string, flag: FileFlag, mode: number): Promise<File> {
    // Original impl https://github.com/jvilk/BrowserFS/blob/v1.4.3/src/backend/OverlayFS.ts#L395
    // See "Write Strategy" in class description
    this.#throwIfProtectedPath(p);

    if (!(await this.exists(p))) {
      if (flag.pathNotExistsAction() === ActionType.CREATE_FILE) {
        await mkdirp(this.#writable, dirname(p));
        return this.#writable.open(p, flag, mode);
      }

      throw ApiError.ENOENT(p);
    }

    const action = flag.pathExistsAction();

    if (action === ActionType.TRUNCATE_FILE) {
      await mkdirp(this.#writable, dirname(p));
      return this.#writable.open(p, flag, mode);
    }

    if (action === ActionType.NOP) {
      const existsInWritable = await this.#writable.exists(p);

      if (existsInWritable) {
        return this.#writable.open(p, flag, mode);
      }

      const stats = await this.#readable.stat(p, false);
      stats.mode = mode;

      // We know this is a Buffer since we passed "null" encoding
      const content = <Buffer>await this.#readable.readFile(p, null, FileFlag.getFileFlag('r'));

      if (stats.size === -1) {
        stats.size = content.length;
      }

      return this.#createOverlayFile(p, flag, stats, content);
      // const f = new OverlayFile(this, p, flag, stats!, data);
      // cb(null, f);
    }

    throw ApiError.EEXIST(p);
  }