import { StreamState , Stream , StreamUtils } from '@ceramicnetwork/common' ; import StreamID from '@ceramicnetwork/streamid' ; import { StateStore } from '@ceramicnetwork/core' ; import LevelUp from 'levelup' ; import S3LevelDOWN from 's3leveldown' ; import toArray from 'stream-to-array' ; /**
 * Ceramic store for saving stream state to S3
 */ export class S3StateStore implements StateStore { readonly #bucketName : string ; #store ; constructor ( bucketName ) { this . #bucketName = bucketName ; } /**
     * Open pinning service
     */ open ( networkName ) { const location = this . #bucketName + '/ceramic/' + networkName + '/state-store' ; this . #store = new LevelUp ( new S3LevelDOWN ( location ) ) ; } param stream async save ( stream ) { await this . #store . put ( stream . id . baseID . toString ( ) , JSON . stringify ( StreamUtils . serializeState ( stream . state ) ) ) ; } param streamId async load ( streamId ) { try { const state = await this . #store . get ( streamId . baseID . toString ( ) ) ; if ( state ) { return StreamUtils . deserializeState ( JSON . parse ( state ) ) ; } else { return null ; } } catch ( err ) { if ( err . notFound ) { return null ; } throw err ; } } param streamId async remove ( streamId ) { await this . #store . del ( streamId . baseID . toString ( ) ) ; } param streamId async list ( streamId ? ) { if ( streamId == null ) { const bufArray = await toArray ( this . #store . createKeyStream ( ) ) ; return bufArray . map ( ( buf ) => buf . toString ( ) ) ; } else { const exists = Boolean ( await this . load ( streamId . baseID ) ) ; return exists ? [ streamId . toString ( ) ] : [ ] ; } } /**
     * Close pinning service
     */ async close ( ) { this . #store . close ( ) ; } }