import { GetObjectCommand , PutObjectCommand , S3Client , } from '@aws-sdk/client-s3' ; import { Readable } from 'stream' ; import { TogglesResp } from '.' ; import { bucket , key } from './config' ; async function streamToString ( stream ) { return new Promise ( ( resolve , reject ) => { const chunks = [ ] ; stream . on ( 'data' , chunk => chunks . push ( chunk ) ) ; stream . on ( 'error' , reject ) ; stream . on ( 'end' , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( 'utf-8' ) ) ) ; } ) ; } export async function getTogglesObject ( client ) { const getObjectCommand = new GetObjectCommand ( { Bucket : bucket , Key : key } ) ; const { Body : body } = await client . send ( getObjectCommand ) ; const toggles = JSON . parse ( await streamToString ( body as Readable ) ) ; return toggles ; } export async function putTogglesObject ( client , obj ) { const putObjectCommand = new PutObjectCommand ( { Bucket : bucket , Key : `${ key }` , Body : JSON . stringify ( obj ) , ACL : 'public-read' , ContentType : 'application/json' } ) ; const response = await client . send ( putObjectCommand ) ; return response ; }