Repository: denoland/deno
File: ext/node/polyfills/internal/fs/handle.ts (TypeScript)

Code before:
  createReadStream(options?: CreateReadStreamOptions): ReadStream {
    return new ReadStream(undefined, { ...options, fd: this.fd });
  }

  createWriteStream(options?: CreateWriteStreamOptions): WriteStream {
    return new WriteStream(undefined, { ...options, fd: this.fd });
  }

  [SymbolAsyncDispose]() {
    return this.close();
  }

  async appendFile(
    data: string | ArrayBufferView | ArrayBuffer | DataView,
    options?: string | { encoding?: string; mode?: number; flag?: string },
  ): Promise<void> {
    const resolvedOptions =
      typeof options === "string" ? { encoding: options } : (options ?? {});

    const optsWithAppend = {
      ...resolvedOptions,
      flag: resolvedOptions.flag ?? "a",
    };

    return this.writeFile(data, optsWithAppend);
  }
}

function readPromise(
  rid: number,
  buffer: ArrayBufferView,
  offset?: number,
  length?: number,
  position?: number | null,
): Promise<ReadResult>;
function readPromise(
  rid: number,
  buffer: ArrayBufferView,
  options?: ReadAsyncOptions<NodeJS.ArrayBufferView>,
): Promise<ReadResult>;
function readPromise(
  rid: number,
  options?: ReadAsyncOptions<NodeJS.ArrayBufferView>,
): Promise<ReadResult>;
function readPromise(
  rid: number,
  bufferOrOpt?: ArrayBufferView | ReadAsyncOptions<NodeJS.ArrayBufferView>,
  offsetOrOpt?: number | ReadAsyncOptions<NodeJS.ArrayBufferView>,
  length?: number,
  position?: number | null,
): Promise<ReadResult> {

Review comment (suggestion):
Would you please wrap this using `fsCall` like the other methods? I also prefer if the implementation mimics the Node'js one: https://github.com/nodejs/node/blob/9ca46de766c00d9a7b9e72b51004b8a08d854488/lib/internal/fs/promises.js#L1263-L1268

Thanks

Code after:
function readPromise(
  rid: number,
  buffer: ArrayBufferView,
  options?: ReadAsyncOptions<NodeJS.ArrayBufferView>,
): Promise<ReadResult>;
function readPromise(
  rid: number,
  options?: ReadAsyncOptions<NodeJS.ArrayBufferView>,
): Promise<ReadResult>;
function readPromise(
  rid: number,
  bufferOrOpt?: ArrayBufferView | ReadAsyncOptions<NodeJS.ArrayBufferView>,
  offsetOrOpt?: number | ReadAsyncOptions<NodeJS.ArrayBufferView>,
  length?: number,
  position?: number | null,
): Promise<ReadResult> {
  if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, bufferOrOpt)) {
    if (typeof length !== "number" && typeof position !== "number") {
      return new Promise((resolve, reject) => {
        readAsync(
          rid,
          bufferOrOpt,
          offsetOrOpt,
          (err: Error, bytesRead: number, buffer: Buffer) => {
            if (err) reject(err);
            else resolve({ buffer, bytesRead });
          },
        );
      });
    }

    return new Promise((resolve, reject) => {
      readAsync(
        rid,
        bufferOrOpt,
        offsetOrOpt,
        length,
        position,
        (err: Error, bytesRead: number, buffer: Buffer) => {
          if (err) reject(err);
          else resolve({ buffer, bytesRead });
        },
      );
    });
  } else {
    return new Promise((resolve, reject) => {
      readAsync(
        rid,
        bufferOrOpt,
        (err: Error, bytesRead: number, buffer: Buffer) => {
          if (err) reject(err);