import { createReadStream , Stats } from 'fs' ; import { createHash } from 'crypto' ; import * as Rx from 'rxjs' ; import { map , takeUntil } from 'rxjs/operators' ; export const generateFileHash = ( fd ) : Promise < string > => { const hash = createHash ( 'sha1' ) ; const read = createReadStream ( null as any , { fd , start : 0 , autoClose : false , } ) ; return Rx . merge ( Rx . fromEvent < Buffer > ( read , 'data' ) , Rx . fromEvent < Error > ( read , 'error' ) . pipe ( map ( ( error ) => { throw error ; } ) ) ) . pipe ( takeUntil ( Rx . fromEvent ( read , 'end' ) ) ) . forEach ( ( chunk ) => hash . update ( chunk ) ) . then ( ( ) => hash . digest ( 'hex' ) ) ; } ; export const getFileCacheKey = ( path , stat ) => `${ path }:${ stat . ino }:${ stat . size }:${ stat . mtime . getTime ( ) }` ;