import * as fs from "fs" ; import { promisify } from "util" ; import { EncodingOptions , ServerProxy } from "../common/proxy" ; import { ReadableProxy , WritableProxy } from "./stream" ; /**
 * A serializable version of fs.Stats.
 */ export interface Stats { dev : number ; ino : number ; mode : number ; nlink : number ; uid : number ; gid : number ; rdev : number ; size : number ; blksize : number ; blocks : number ; atimeMs : number ; mtimeMs : number ; ctimeMs : number ; birthtimeMs : number ; atime : Date ; mtime : Date ; ctime : Date ; birthtime : Date ; _isFile : boolean ; _isDirectory : boolean ; _isBlockDevice : boolean ; _isCharacterDevice : boolean ; _isSymbolicLink : boolean ; _isFIFO : boolean ; _isSocket : boolean ; } export class ReadStreamProxy extends ReadableProxy < fs . ReadStream > { public constructor ( stream ) { super ( stream , [ "open" ] ) ; } public async close ( ) { this . instance . close ( ) ; } public async dispose ( ) { this . instance . close ( ) ; await super . dispose ( ) ; } } export class WriteStreamProxy extends WritableProxy < fs . WriteStream > { public constructor ( stream ) { super ( stream , [ "open" ] ) ; } public async close ( ) { this . instance . close ( ) ; } public async dispose ( ) { this . instance . close ( ) ; await super . dispose ( ) ; } } export class WatcherProxy extends ServerProxy < fs . FSWatcher > { public constructor ( watcher ) { super ( { bindEvents : [ "change" , "close" , "error" ] , doneEvents : [ "close" , "error" ] , instance : watcher , } ) ; } public async close ( ) { this . instance . close ( ) ; } public async dispose ( ) { this . instance . close ( ) ; await super . dispose ( ) ; } } export class FsModuleProxy { public access ( path , mode ? ) { return promisify ( fs . access ) ( path , mode ) ; } public appendFile ( file , data , options ? ) { return promisify ( fs . appendFile ) ( file , data , options ) ; } public chmod ( path , mode ) { return promisify ( fs . chmod ) ( path , mode ) ; } public chown ( path , uid , gid ) { return promisify ( fs . chown ) ( path , uid , gid ) ; } public close ( fd ) { return promisify ( fs . close ) ( fd ) ; } public copyFile ( src , dest , flags ? ) { return promisify ( fs . copyFile ) ( src , dest , flags ) ; } public async createReadStream ( path , options ? ) { return new ReadStreamProxy ( fs . createReadStream ( path , options ) ) ; } public async createWriteStream ( path , options ? ) { return new WriteStreamProxy ( fs . createWriteStream ( path , options ) ) ; } public exists ( path ) { return promisify ( fs . exists ) ( path ) ; } public fchmod ( fd , mode ) { return promisify ( fs . fchmod ) ( fd , mode ) ; } public fchown ( fd , uid , gid ) { return promisify ( fs . fchown ) ( fd , uid , gid ) ; } public fdatasync ( fd ) { return promisify ( fs . fdatasync ) ( fd ) ; } public async fstat ( fd ) { return this . makeStatsSerializable ( await promisify ( fs . fstat ) ( fd ) ) ; } public fsync ( fd ) { return promisify ( fs . fsync ) ( fd ) ; } public ftruncate ( fd , len ? ) { return promisify ( fs . ftruncate ) ( fd , len ) ; } public futimes ( fd , atime , mtime ) { return promisify ( fs . futimes ) ( fd , atime , mtime ) ; } public lchmod ( path , mode ) { return promisify ( fs . lchmod ) ( path , mode ) ; } public lchown ( path , uid , gid ) { return promisify ( fs . lchown ) ( path , uid , gid ) ; } public link ( existingPath , newPath ) { return promisify ( fs . link ) ( existingPath , newPath ) ; } public async lstat ( path ) { return this . makeStatsSerializable ( await promisify ( fs . lstat ) ( path ) ) ; } public async lstatBatch ( args ) { return Promise . all ( args . map ( ( a ) => this . lstat ( a . path ) . catch ( ( e ) => e ) ) ) ; } public mkdir ( path , mode ) { return promisify ( fs . mkdir ) ( path , mode ) ; } public mkdtemp ( prefix , options ) { return promisify ( fs . mkdtemp ) ( prefix , options ) ; } public open ( path , flags , mode ) { return promisify ( fs . open ) ( path , flags , mode ) ; } public read ( fd , length , position ) { const buffer = Buffer . alloc ( length ) ; return promisify ( fs . read ) ( fd , buffer , 0 , length , position ) ; } public readFile ( path , options ) { return promisify ( fs . readFile ) ( path , options ) ; } public readdir ( path , options ) { return promisify ( fs . readdir ) ( path , options ) ; } public readdirBatch ( args ) { return Promise . all ( args . map ( ( a ) => this . readdir ( a . path , a . options ) . catch ( ( e ) => e ) ) ) ; } public readlink ( path , options ) { return promisify ( fs . readlink ) ( path , options ) ; } public realpath ( path , options ) { return promisify ( fs . realpath ) ( path , options ) ; } public rename ( oldPath , newPath ) { return promisify ( fs . rename ) ( oldPath , newPath ) ; } public rmdir ( path ) { return promisify ( fs . rmdir ) ( path ) ; } public async stat ( path ) { return this . makeStatsSerializable ( await promisify ( fs . stat ) ( path ) ) ; } public async statBatch ( args ) { return Promise . all ( args . map ( ( a ) => this . stat ( a . path ) . catch ( ( e ) => e ) ) ) ; } public symlink ( target , path , type ? ) { return promisify ( fs . symlink ) ( target , path , type ) ; } public truncate ( path , len ? ) { return promisify ( fs . truncate ) ( path , len ) ; } public unlink ( path ) { return promisify ( fs . unlink ) ( path ) ; } public utimes ( path , atime , mtime ) { return promisify ( fs . utimes ) ( path , atime , mtime ) ; } public async write ( fd , buffer , offset ? , length ? , position ? ) { return promisify ( fs . write ) ( fd , buffer , offset , length , position ) ; } public writeFile ( path , data , options ) { return promisify ( fs . writeFile ) ( path , data , options ) ; } public async watch ( filename , options ? ) { return new WatcherProxy ( fs . watch ( filename , options ) ) ; } private makeStatsSerializable ( stats ) { return { ... stats , /**
             * We need to check if functions exist because nexe's implemented FS
             * lib doesnt implement fs.stats properly.
             */ _isBlockDevice : stats . isBlockDevice ? stats . isBlockDevice ( ) : false , _isCharacterDevice : stats . isCharacterDevice ? stats . isCharacterDevice ( ) : false , _isDirectory : stats . isDirectory ( ) , _isFIFO : stats . isFIFO ? stats . isFIFO ( ) : false , _isFile : stats . isFile ( ) , _isSocket : stats . isSocket ? stats . isSocket ( ) : false , _isSymbolicLink : stats . isSymbolicLink ? stats . isSymbolicLink ( ) : false , } ; } }