Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions .pnp.cjs
100755 → 100644

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

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

declined:
- "@yarnpkg/plugin-catalog"
- "@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-jsr"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@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/libzip"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe(`Commands`, () => {
});

expect(manifest.resolutions).toEqual({
[`no-deps@npm:1.0.0`]: expect.stringMatching(/^patch:no-deps/),
[`no-deps@npm:1.0.0`]: expect.stringMatching(/^patch:no-deps/),
});
}),
);
Expand Down
131 changes: 65 additions & 66 deletions packages/yarnpkg-fslib/sources/patchFs/FileHandle.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
import type {BigIntStats, ReadStream, StatOptions, Stats, WriteStream, WriteVResult} from 'fs';
import {createInterface} from 'readline';

import type {CreateReadStreamOptions, CreateWriteStreamOptions, FakeFS} from '../FakeFS';
import type {Path} from '../path';

// Types copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/9e2e5af93f9cc2cf434a96e3249a573100e87351/types/node/v16
// Implementation based on https://github.com/nodejs/node/blob/10493b48c7edb227c13a493d0a2c75efe878d7e9/lib/internal/fs/promises.js#L124-L336

interface ObjectEncodingOptions {
encoding?: BufferEncoding | null | undefined;
}

interface FlagAndOpenMode {
mode?: Mode | undefined;
flag?: OpenMode | undefined;
}

type OpenMode = number | string;
type Mode = number | string;

interface FileReadResult<T extends ArrayBufferView> {
bytesRead: number;
buffer: T;
}

interface FileReadOptions<T extends ArrayBufferView = Buffer> {
buffer?: T;
offset?: number | null;
length?: number | null;
position?: number | null;
}

interface ReadVResult {
bytesRead: number;
buffers: Array<NodeJS.ArrayBufferView>;
}

interface AbortSignal {
readonly aborted: boolean;
}

interface Abortable {
signal?: AbortSignal | undefined;
}

import type {Abortable} from 'events';
import type {FlagAndOpenMode, FileReadResult, FileReadOptions} from 'fs/promises';
import {createInterface} from 'readline';

import type {CreateReadStreamOptions, CreateWriteStreamOptions, FakeFS} from '../FakeFS';
import type {Path} from '../path';

import type {
BigIntStats,
ObjectEncodingOptions,
OpenMode,
ReadStream,
ReadVResult,
StatOptions,
Stats,
WriteStream,
WriteVResult,
} from 'fs';

// Implementation based on https://github.com/nodejs/node/blob/v18.12.0/lib/internal/fs/promises.js#L132-L351

type WriteArgsBuffer<TBuffer extends Uint8Array> = [
buffer: TBuffer,
Expand Down Expand Up @@ -133,45 +107,70 @@ export class FileHandle<P extends Path> {
throw new Error(`Method not implemented.`);
}

async read(options?: FileReadOptions<Buffer>): Promise<FileReadResult<Buffer>>;
async read(
buffer: Buffer,
// TODO: Once we drop Node 20 support, switch to ReadOptions and ReadOptionsWithoutBuffer from `@types/node`
async read<T extends NodeJS.ArrayBufferView>(
buffer: T,
offset?: number | null,
length?: number | null,
position?: number | null
): Promise<FileReadResult<Buffer>>;
position?: number | null,
): Promise<FileReadResult<T>>;
async read<T extends NodeJS.ArrayBufferView>(
buffer: T,
options?: Omit<FileReadOptions<T>, `buffer`>,
): Promise<FileReadResult<T>>;
async read<T extends NodeJS.ArrayBufferView = NonSharedBuffer>(
options: FileReadOptions<T> & {buffer: T},
): Promise<FileReadResult<T>>;
async read(
bufferOrOptions?: Buffer | FileReadOptions<Buffer>,
offset?: number | null,
options?: FileReadOptions<NonSharedBuffer> & {buffer?: never},
): Promise<FileReadResult<NonSharedBuffer>>;
async read<T extends NodeJS.ArrayBufferView>(
bufferOrOptions?: T | FileReadOptions<T>,
offsetOrOptions?: number | null | Omit<FileReadOptions<T>, `buffer`>,
length?: number | null,
position?: number | null,
): Promise<FileReadResult<Buffer>> {
): Promise<FileReadResult<T>> {
try {
this[kRef](this.read);

let buffer: Buffer;

if (!Buffer.isBuffer(bufferOrOptions)) {
bufferOrOptions ??= {};
buffer = bufferOrOptions.buffer ?? Buffer.alloc(16384);
offset = bufferOrOptions.offset || 0;
length = bufferOrOptions.length ?? buffer.byteLength;
position = bufferOrOptions.position ?? null;
let buffer: T;
let offset: number;

if (!ArrayBuffer.isView(bufferOrOptions)) {
// read([options])
// TypeScript isn't able to infer that the coalescing happens only in the no-generic case
buffer = bufferOrOptions?.buffer ?? Buffer.alloc(16384) as unknown as T;
offset = bufferOrOptions?.offset ?? 0;
length = bufferOrOptions?.length ?? buffer.byteLength - offset;
position = bufferOrOptions?.position ?? null;
} else if (typeof offsetOrOptions === `object` && offsetOrOptions !== null) {
// read(buffer[, options])
buffer = bufferOrOptions;
offset = offsetOrOptions?.offset ?? 0;
length = offsetOrOptions?.length ?? buffer.byteLength - offset;
position = offsetOrOptions?.position ?? null;
} else {
// read(buffer, offset[, length[, position]])
buffer = bufferOrOptions;
offset = offsetOrOptions ?? 0;
length ??= 0;
}

offset ??= 0;
length ??= 0;

if (length === 0) {
return {
bytesRead: length,
buffer,
};
}

const bytesRead = await this[kBaseFs].readPromise(this.fd, buffer, offset, length, position);
const bytesRead = await this[kBaseFs].readPromise(
this.fd,
// FIXME: FakeFS should support ArrayBufferViews directly
Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength),
offset,
length,
position,
);

return {
bytesRead,
Expand Down
Binary file added packages/yarnpkg-fslib/tests/fixtures/foo.zip
Binary file not shown.
Loading