Skip to content

Commit

Permalink
fix: Match promisified fs.read call return with original implementati…
Browse files Browse the repository at this point in the history
…on (#2128)

* fix: Match promisified fs.read call return with original implementation

* chore: Updated versions

* test: Validate promisified fs.read matches correct return shape

* chore: Updated hook
  • Loading branch information
Mike-Dax authored Nov 17, 2020
1 parent b3ccfa3 commit 37d9741
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
18 changes: 17 additions & 1 deletion .pnp.js

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

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

declined:
- "@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-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-node-modules"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@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/pnpify"
- "@yarnpkg/shell"
14 changes: 14 additions & 0 deletions packages/yarnpkg-fslib/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export function patchFs(patchedFs: typeof fs, fakeFs: FakeFS<NativePath>): void
const orig = target[name];
target[name] = replacement;

// Preserve any util.promisify implementations
if (typeof orig?.[promisify.custom] !== `undefined`) {
replacement[promisify.custom] = orig[promisify.custom];
}
Expand Down Expand Up @@ -284,6 +285,19 @@ export function patchFs(patchedFs: typeof fs, fakeFs: FakeFS<NativePath>): void
// `fs.promises.realpath` doesn't have a `native` property
}
}

/** util.promisify implementations */
{
// Override the promisified version of `fs.read` 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/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);
return {bytesRead: await res, buffer};
};
}
}

export function extendFs(realFs: typeof fs, fakeFs: FakeFS<NativePath>): typeof fs {
Expand Down
17 changes: 17 additions & 0 deletions packages/yarnpkg-fslib/tests/patchedFs.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import {promisify} from 'util';

import {NodeFS} from '../sources/NodeFS';
import {PosixFS} from '../sources/PosixFS';
Expand Down Expand Up @@ -39,4 +40,20 @@ describe(`patchedFs`, () => {
done();
});
});

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

const file = ppath.join(npath.toPortablePath(__dirname), `patchedFs.test.ts` as Filename);

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

const bufferFs = Buffer.alloc(16);

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

expect(typeof result.bytesRead).toBe(`number`);
expect(Buffer.isBuffer(result.buffer)).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

0 comments on commit 37d9741

Please sign in to comment.