Skip to content

Commit

Permalink
fix(fslib): fix return shape of promisified fs.write (#4411)
Browse files Browse the repository at this point in the history
* fix(fslib): fix return shape of promisified `fs.write`

* chore: update hook

* chore: versions

Co-authored-by: merceyz <merceyz@users.noreply.github.com>
  • Loading branch information
mhassan1 and merceyz committed Apr 30, 2022
1 parent 8e0c4b8 commit 8f1ff59
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
8 changes: 6 additions & 2 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions .yarn/versions/7e1696fd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/fslib": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch
"@yarnpkg/pnpify": patch

declined:
- "@yarnpkg/esbuild-plugin-pnp"
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/json-proxy"
- "@yarnpkg/nm"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
14 changes: 11 additions & 3 deletions packages/yarnpkg-fslib/sources/patchFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,23 @@ export function patchFs(patchedFs: typeof fs, fakeFs: FakeFS<NativePath>): void

/** util.promisify implementations */
{
// Override the promisified version of `fs.read` to return an object as per
// TODO add promisified `fs.readv` and `fs.writev`, once they are implemented
// Override the promisified versions of `fs.read` and `fs.write` to return an object as per
// https://github.com/nodejs/node/blob/dc79f3f37caf6f25b8efee4623bec31e2c20f595/lib/fs.js#L559-L560
// and
// https://github.com/nodejs/node/blob/dc79f3f37caf6f25b8efee4623bec31e2c20f595/lib/fs.js#L690-L691
// and
// https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L293
// @ts-expect-error
patchedFs.read[promisify.custom] = async (p: number, buffer: Buffer, ...args: Array<any>) => {
const res = fakeFs.readPromise(p, buffer, ...args);
patchedFs.read[promisify.custom] = async (fd: number, buffer: Buffer, ...args: Array<any>) => {
const res = fakeFs.readPromise(fd, buffer, ...args);
return {bytesRead: await res, buffer};
};
// @ts-expect-error
patchedFs.write[promisify.custom] = async (fd: number, buffer: Buffer, ...args: Array<any>) => {
const res = fakeFs.writePromise(fd, buffer, ...args);
return {bytesWritten: await res, buffer};
};
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/yarnpkg-fslib/tests/patchedFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ describe(`patchedFs`, () => {
expect(Buffer.isBuffer(result.buffer)).toBeTruthy();
});

it(`matches the util.promisify return shape of node: fs.write`, async () => {
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));
const patchedFsWriteAsync = promisify(patchedFs.write);

const tmpdir = npath.fromPortablePath(xfs.mktempSync());

const file = npath.join(tmpdir, `file.txt`);

const fd = fs.openSync(file, `w`);

const bufferFs = Buffer.alloc(16);

const result = await patchedFsWriteAsync(fd, bufferFs, 0, 16, 0);

expect(typeof result.bytesWritten).toBe(`number`);
expect(Buffer.isBuffer(result.buffer)).toBeTruthy();
});

it(`should support URL instances`, () => {
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

0 comments on commit 8f1ff59

Please sign in to comment.