export * from './file.config.js' ; import { fsa as fsaSource , FsAwsS3 } from '@chunkd/fs' ; import { promisify } from 'util' ; import { createGzip , gunzip } from 'zlib' ; import { FileConfig , isConfigS3Role } from './file.config.js' ; const pGunzip = promisify ( gunzip ) as ( data : Buffer ) => Promise < Buffer > ; export type FsaJson = typeof fsaSource & { readJson < T > ( filePath ) : Promise < T > ; writeJson < T > ( filePath , obj ) : Promise < void > ; /** Add a file system configuration */ configure ( fs ) : void ; } ; export const fsa = fsaSource as any as FsaJson ; fsa . readJson = async function readJson < T > ( filePath ) : Promise < T > { const data = await this . read ( filePath ) ; if ( filePath . endsWith ( '.gz' ) ) { return JSON . parse ( ( await pGunzip ( data ) ) . toString ( ) ) as T ; } else { return JSON . parse ( data . toString ( ) ) as T ; } } ; fsa . writeJson = async function writeJson < T > ( filePath , obj ) : Promise < void > { const json = Buffer . from ( JSON . stringify ( obj , undefined , 2 ) ) ; if ( filePath . endsWith ( '.gz' ) ) { const gzip = createGzip ( ) ; gzip . end ( json ) ; return this . write ( filePath , gzip ) ; } else { return this . write ( filePath , json ) ; } } ; fsa . configure = function configure ( cfg ) : void { if ( cfg . type !== 's3' ) return ; if ( ! isConfigS3Role ( cfg ) ) return ; const { bucket } = FsAwsS3 . parse ( cfg . path ) ; const bucketUri = `s3://${ bucket }/` ; this . register ( bucketUri , FsAwsS3 . fromRoleArn ( cfg . roleArn , cfg . externalId ) ) ; } ; /**
 *  Default to using the file handler useful for folders like
 * "C:\" or "directory_name/sub_directory"
 */ fsa . register ( '' , fsa . get ( 'file://' ) ) ;