Skip to content

Commit 8a61f8e

Browse files
committed
feat(fslib): Update FileHandle#read impl to match Node 18.12.0
1 parent 6c32b0f commit 8a61f8e

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

.pnp.cjs

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/yarnpkg-fslib/sources/patchFs/FileHandle.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {CreateReadStreamOptions, CreateWriteStreamOptions, FakeFS}
55
import type {Path} from '../path';
66

77
// Types copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/9e2e5af93f9cc2cf434a96e3249a573100e87351/types/node/v16
8-
// Implementation based on https://github.com/nodejs/node/blob/10493b48c7edb227c13a493d0a2c75efe878d7e9/lib/internal/fs/promises.js#L124-L336
8+
// Implementation based on https://github.com/nodejs/node/blob/v18.12.0/lib/internal/fs/promises.js#L132-L351
99

1010
interface ObjectEncodingOptions {
1111
encoding?: BufferEncoding | null | undefined;
@@ -44,7 +44,6 @@ interface Abortable {
4444
signal?: AbortSignal | undefined;
4545
}
4646

47-
4847
type WriteArgsBuffer<TBuffer extends Uint8Array> = [
4948
buffer: TBuffer,
5049
offset?: number | null,
@@ -133,43 +132,55 @@ export class FileHandle<P extends Path> {
133132
throw new Error(`Method not implemented.`);
134133
}
135134

136-
async read<T extends NodeJS.ArrayBufferView = Buffer>(
137-
options: FileReadOptions<T> & {buffer: T},
138-
): Promise<FileReadResult<T>>;
139-
async read(
140-
options?: FileReadOptions<Buffer> & {buffer?: never},
141-
): Promise<FileReadResult<Buffer>>;
135+
// TODO: Once we drop Node 20 support, switch to ReadOptions and ReadOptionsWithoutBuffer from `@types/node`
142136
async read<T extends NodeJS.ArrayBufferView>(
143137
buffer: T,
144138
offset?: number | null,
145139
length?: number | null,
146140
position?: number | null,
147141
): Promise<FileReadResult<T>>;
142+
async read<T extends NodeJS.ArrayBufferView>(
143+
buffer: T,
144+
options?: Omit<FileReadOptions<T>, `buffer`>,
145+
): Promise<FileReadResult<T>>;
146+
async read<T extends NodeJS.ArrayBufferView = NonSharedBuffer>(
147+
options: FileReadOptions<T> & {buffer: T},
148+
): Promise<FileReadResult<T>>;
149+
async read(
150+
options?: FileReadOptions<NonSharedBuffer> & {buffer?: never},
151+
): Promise<FileReadResult<NonSharedBuffer>>;
148152
async read<T extends NodeJS.ArrayBufferView>(
149153
bufferOrOptions?: T | FileReadOptions<T>,
150-
offset?: number | null,
154+
offsetOrOptions?: number | null | Omit<FileReadOptions<T>, `buffer`>,
151155
length?: number | null,
152156
position?: number | null,
153157
): Promise<FileReadResult<T>> {
154158
try {
155159
this[kRef](this.read);
156160

157161
let buffer: T;
162+
let offset: number;
158163

159164
if (!ArrayBuffer.isView(bufferOrOptions)) {
160-
bufferOrOptions ??= {};
165+
// read([options])
161166
// TypeScript isn't able to infer that the coalescing happens only in the no-generic case
162-
buffer = bufferOrOptions.buffer ?? Buffer.alloc(16384) as unknown as T;
163-
offset = bufferOrOptions.offset || 0;
164-
length = bufferOrOptions.length ?? buffer.byteLength;
165-
position = bufferOrOptions.position ?? null;
167+
buffer = bufferOrOptions?.buffer ?? Buffer.alloc(16384) as unknown as T;
168+
offset = bufferOrOptions?.offset ?? 0;
169+
length = bufferOrOptions?.length ?? buffer.byteLength - offset;
170+
position = bufferOrOptions?.position ?? null;
171+
} else if (typeof offsetOrOptions === `object` && offsetOrOptions !== null) {
172+
// read(buffer[, options])
173+
buffer = bufferOrOptions;
174+
offset = offsetOrOptions?.offset ?? 0;
175+
length = offsetOrOptions?.length ?? buffer.byteLength - offset;
176+
position = offsetOrOptions?.position ?? null;
166177
} else {
178+
// read(buffer, offset[, length[, position]])
167179
buffer = bufferOrOptions;
180+
offset = offsetOrOptions ?? 0;
181+
length ??= 0;
168182
}
169183

170-
offset ??= 0;
171-
length ??= 0;
172-
173184
if (length === 0) {
174185
return {
175186
bytesRead: length,

packages/yarnpkg-pnp/sources/hook.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)