33
44import * as nodeJsPath from 'path' ;
55import * as fs from 'fs' ;
6+ import * as fsPromises from 'fs/promises' ;
67import * as fsx from 'fs-extra' ;
78
89import { Text , type NewlineKind , Encoding } from './Text' ;
910import { 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 ;
0 commit comments