Skip to content

Commit f4fe47e

Browse files
authored
Remove the dependency on LegacyAdatpers in FileSystem. (#5170)
1 parent 9fff7e5 commit f4fe47e

5 files changed

Lines changed: 49 additions & 25 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "",
5+
"type": "none",
6+
"packageName": "@rushstack/node-core-library"
7+
}
8+
],
9+
"packageName": "@rushstack/node-core-library",
10+
"email": "iclanton@users.noreply.github.com"
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/node-core-library",
5+
"comment": "Expand `FileSystem.writeBuffersToFile` and `FileSystem.writeBuffersToFileAsync` to take more kinds of buffers.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/node-core-library"
10+
}

common/reviews/api/node-core-library.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ export class FileSystem {
201201
static readLinkAsync(path: string): Promise<string>;
202202
static updateTimes(path: string, times: IFileSystemUpdateTimeParameters): void;
203203
static updateTimesAsync(path: string, times: IFileSystemUpdateTimeParameters): Promise<void>;
204-
static writeBuffersToFile(filePath: string, contents: ReadonlyArray<Uint8Array>, options?: IFileSystemWriteBinaryFileOptions): void;
205-
static writeBuffersToFileAsync(filePath: string, contents: ReadonlyArray<Uint8Array>, options?: IFileSystemWriteBinaryFileOptions): Promise<void>;
204+
static writeBuffersToFile(filePath: string, contents: ReadonlyArray<NodeJS.ArrayBufferView>, options?: IFileSystemWriteBinaryFileOptions): void;
205+
static writeBuffersToFileAsync(filePath: string, contents: ReadonlyArray<NodeJS.ArrayBufferView>, options?: IFileSystemWriteBinaryFileOptions): Promise<void>;
206206
static writeFile(filePath: string, contents: string | Buffer, options?: IFileSystemWriteFileOptions): void;
207207
static writeFileAsync(filePath: string, contents: string | Buffer, options?: IFileSystemWriteFileOptions): Promise<void>;
208208
}

libraries/node-core-library/src/FileSystem.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
import * as nodeJsPath from 'path';
55
import * as fs from 'fs';
6+
import * as fsPromises from 'fs/promises';
67
import * as fsx from 'fs-extra';
78

89
import { Text, type NewlineKind, Encoding } from './Text';
910
import { PosixModeBits } from './PosixModeBits';
10-
import { LegacyAdapters } from './LegacyAdapters';
1111

1212
/**
1313
* An alias for the Node.js `fs.Stats` object.
@@ -684,11 +684,7 @@ export class FileSystem {
684684
...options
685685
};
686686

687-
const folderEntries: FolderItem[] = await LegacyAdapters.convertCallbackToPromise(
688-
fs.readdir,
689-
folderPath,
690-
{ withFileTypes: true }
691-
);
687+
const folderEntries: FolderItem[] = await fsPromises.readdir(folderPath, { withFileTypes: true });
692688
if (options.absolutePaths) {
693689
return folderEntries.map((folderEntry) => {
694690
folderEntry.name = nodeJsPath.resolve(folderPath, folderEntry.name);
@@ -806,13 +802,13 @@ export class FileSystem {
806802
*/
807803
public static writeBuffersToFile(
808804
filePath: string,
809-
contents: ReadonlyArray<Uint8Array>,
805+
contents: ReadonlyArray<NodeJS.ArrayBufferView>,
810806
options?: IFileSystemWriteBinaryFileOptions
811807
): void {
812808
FileSystem._wrapException(() => {
813809
// Need a mutable copy of the iterable to handle incomplete writes,
814810
// since writev() doesn't take an argument for where to start writing.
815-
const toCopy: Uint8Array[] = [...contents];
811+
const toCopy: NodeJS.ArrayBufferView[] = [...contents];
816812

817813
let fd: number | undefined;
818814
try {
@@ -837,7 +833,12 @@ export class FileSystem {
837833
const bytesInCurrentBuffer: number = toCopy[buffersWritten].byteLength;
838834
if (bytesWritten < bytesInCurrentBuffer) {
839835
// This buffer was partially written.
840-
toCopy[buffersWritten] = toCopy[buffersWritten].subarray(bytesWritten);
836+
const currentToCopy: NodeJS.ArrayBufferView = toCopy[buffersWritten];
837+
toCopy[buffersWritten] = new Uint8Array(
838+
currentToCopy.buffer,
839+
currentToCopy.byteOffset + bytesWritten,
840+
currentToCopy.byteLength - bytesWritten
841+
);
841842
break;
842843
}
843844
bytesWritten -= bytesInCurrentBuffer;
@@ -896,25 +897,25 @@ export class FileSystem {
896897
*/
897898
public static async writeBuffersToFileAsync(
898899
filePath: string,
899-
contents: ReadonlyArray<Uint8Array>,
900+
contents: ReadonlyArray<NodeJS.ArrayBufferView>,
900901
options?: IFileSystemWriteBinaryFileOptions
901902
): Promise<void> {
902903
await FileSystem._wrapExceptionAsync(async () => {
903904
// Need a mutable copy of the iterable to handle incomplete writes,
904905
// since writev() doesn't take an argument for where to start writing.
905-
const toCopy: Uint8Array[] = [...contents];
906+
const toCopy: NodeJS.ArrayBufferView[] = [...contents];
906907

907-
let handle: fs.promises.FileHandle | undefined;
908+
let handle: fsPromises.FileHandle | undefined;
908909
try {
909-
handle = await fs.promises.open(filePath, 'w');
910+
handle = await fsPromises.open(filePath, 'w');
910911
} catch (error) {
911912
if (!options?.ensureFolderExists || !FileSystem.isNotExistError(error as Error)) {
912913
throw error;
913914
}
914915

915916
const folderPath: string = nodeJsPath.dirname(filePath);
916917
await FileSystem.ensureFolderAsync(folderPath);
917-
handle = await fs.promises.open(filePath, 'w');
918+
handle = await fsPromises.open(filePath, 'w');
918919
}
919920

920921
try {
@@ -927,7 +928,12 @@ export class FileSystem {
927928
const bytesInCurrentBuffer: number = toCopy[buffersWritten].byteLength;
928929
if (bytesWritten < bytesInCurrentBuffer) {
929930
// This buffer was partially written.
930-
toCopy[buffersWritten] = toCopy[buffersWritten].subarray(bytesWritten);
931+
const currentToCopy: NodeJS.ArrayBufferView = toCopy[buffersWritten];
932+
toCopy[buffersWritten] = new Uint8Array(
933+
currentToCopy.buffer,
934+
currentToCopy.byteOffset + bytesWritten,
935+
currentToCopy.byteLength - bytesWritten
936+
);
931937
break;
932938
}
933939
bytesWritten -= bytesInCurrentBuffer;

libraries/node-core-library/src/test/writeBuffersToFile.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ jest.mock('fs-extra', () => {
1818
writevSync
1919
};
2020
});
21+
jest.mock('fs/promises', () => {
22+
return {
23+
open: openHandle
24+
};
25+
});
2126
jest.mock('../Text', () => {
2227
return {
2328
Encoding: {
@@ -26,14 +31,6 @@ jest.mock('../Text', () => {
2631
};
2732
});
2833

29-
import fs from 'node:fs';
30-
31-
jest.spyOn(fs, 'promises', 'get').mockImplementation(() => {
32-
return {
33-
open: openHandle
34-
} as unknown as typeof fs.promises;
35-
});
36-
3734
describe('FileSystem', () => {
3835
const content: Uint8Array[] = [];
3936
let totalBytes: number = 0;

0 commit comments

Comments
 (0)