- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.2k
Description
Self-service
- I'd be willing to implement a fix
Describe the bug
According to nodeJS documentation of FileHandle, read method should support Buffer | TypedArray | DataView as first argument. However, the patched fs in pnp.cjs does not handle TypedArray | DataView properly.
To reproduce
./test.txt
Some Data Here.....
Some Data Here.....
Some Data Here....../typed-array-test.js
const fs = require("fs/promises");
async function main() {
  const fh = await fs.open("./test.txt");
  const array = new Uint8Array(20);
  await fh.read(array, 0, 20);
  console.log(
    `First 20 bytes of file:\n${Buffer.from(array.buffer).toString()}`
  );
}
main();./data-view-test.js
const fs = require("fs/promises");
async function main() {
  const fh = await fs.open("./test.txt");
  const array = new DataView(new ArrayBuffer(20));
  await fh.read(array, 0, 20);
  console.log(
    `First 20 bytes of file:\n${Buffer.from(array.buffer).toString()}`
  );
}
main();It works properly when using node to exec the scripts.
node ./typed-array-test.js
node ./data-view-test.jsIt does not work when using yarn node to exec the scripts.
yarn node ./typed-array-test.js
yarn node ./data-view-test.jsIn both fail cases, it shows the followings.
TypeError [ERR_INVALID_ARG_TYPE]: The "buffer" argument must be an instance of Buffer, TypedArray, or DataView. Received an instance of ArrayBuffer
Environment
System:
    OS: Linux 6.5 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i5-12450H
  Binaries:
    Node: 20.11.1 - /tmp/xfs-af62f90a/node
    Yarn: 4.1.0 - /tmp/xfs-af62f90a/yarn
    npm: 10.2.4 - /usr/local/bin/npmAdditional context
Potential cause:
See (https://github.com/yarnpkg/berry/blob/master/packages/yarnpkg-fslib/sources/patchFs/FileHandle.ts#L154)
The Buffer.isBuffer checking does not account for TypedArray or DataView.
Potential fix:
Include additional checking condition, i.e.
!Buffer.isBuffer(bufferOrOptions) && !ArrayBuffer.isView(bufferOrOptions)I am not familar with the codebase and don't know how the above change would affect current behavior. Also, I am not sure if this kind of checking should be applied to other methods.
Hope the above information would help solve the issue. Thanks.
Note:
For those who are facing similar issue, my current workaround is to set nodeLinker to node-modules in .yarnrc.yml.