Repository: denoland/deno
File: tests/unit_node/_fs/_fs_handle_test.ts (TypeScript)

Code before:

    assertEquals(stream.bytesRead, 0);
    assertEquals(stream.readable, true);

    let bytesRead = 0;

    stream.on("open", () => assertEquals(stream.bytesRead, 0));

    stream.on("data", (data) => {
      assertEquals(data instanceof Buffer, true);
      assertEquals((data as Buffer).byteOffset % 8, 0);
      bytesRead += data.length;
      assertEquals(stream.bytesRead, bytesRead);
    });

    stream.on("end", () => {
      assertEquals(stream.bytesRead, fileSize);
      assertEquals(bytesRead, fileSize);
    });

    // Wait for the 'close' event so that the test won't finish prematurely
    await new Promise<void>((resolve) => {
      stream.on("close", resolve);
    });
    
    // await fileHandle.close();
  }
);

Deno.test(
  "[node/fs filehandle.createWriteStream] Create a write stream",
  async function () {
    const tempFile: string = await Deno.makeTempFile();
    const fileHandle = await fs.open(tempFile, "w");
    const stream = fileHandle.createWriteStream();
    
    // Data to write
    const data1 = Buffer.from("Hello, ");
    const data2 = Buffer.from("world!");
    const allData = Buffer.concat([data1, data2]);
    
    // Initial state checks
    assertEquals(stream.bytesWritten, 0);
    assertEquals(stream.writable, true);

    let bytesWritten = 0;

    stream.on("open", () => {
      assertEquals(stream.bytesWritten, 0);
    });


Review comment (suggestion):
In both these tests, I had to comment out the `fileHandle.close()` call

If I keep the call, I get

```
error: BadResource: Bad resource ID
    await fileHandle.close();
                     ^
    at FileHandle.close (ext:deno_node/internal/fs/handle.ts:158:33)
    at file:///home/git/deno/tests/unit_node/_fs/_fs_handle_test.ts:350:2
```

I don't know why this error would be thrown here. I think @kt3k is the expert on this part of the code so maybe he has an idea

Code after:

    assertEquals(stream.bytesRead, 0);
    assertEquals(stream.readable, true);

    let bytesRead = 0;

    stream.on("open", () => assertEquals(stream.bytesRead, 0));

    stream.on("data", (data) => {
      assertEquals(data instanceof Buffer, true);
      assertEquals((data as Buffer).byteOffset % 8, 0);
      bytesRead += data.length;
      assertEquals(stream.bytesRead, bytesRead);
    });

    stream.on("end", () => {
      assertEquals(stream.bytesRead, fileSize);
      assertEquals(bytesRead, fileSize);
    });

    // Wait for the 'close' event so that the test won't finish prematurely
    // note: stream automatically closes fd once all the data is read
    await new Promise<void>((resolve) => {
      stream.on("close", resolve);
    });
  },
);

Deno.test(
  "[node/fs filehandle.createWriteStream] Create a write stream",
  async function () {
    const tempFile: string = await Deno.makeTempFile();
    try {
      const fileHandle = await fs.open(tempFile, "w");
      const stream = fileHandle.createWriteStream();
      const { promise, resolve } = Promise.withResolvers<void>();
      stream.on("close", resolve);
      stream.end("a\n", "utf8");
      await promise;
      assertEquals(await Deno.readTextFile(tempFile), "a\n");
    } finally {
      await Deno.remove(tempFile);
    }
  },
);