import Level from 'level-ts' ; import { StreamState , StreamStateHolder , StreamUtils } from '@ceramicnetwork/common' ; import { StateStore } from './state-store' ; import StreamID from '@ceramicnetwork/streamid' ; import * as fs from 'fs' ; import path from 'path' ; /**
 * Ceramic store for saving stream state to a local leveldb instance
 */ export class LevelStateStore implements StateStore { #store : Level ; constructor ( private storeRoot ) { } /**
     * Gets internal db
     */ get store ( ) : Level { return this . #store ; } /**
     * Open pinning service
     */ open ( networkName ) { const storePath = path . join ( this . storeRoot , networkName ) ; if ( fs ) { fs . mkdirSync ( storePath , { recursive : true } ) ; } this . #store = new Level ( storePath ) ; } param streamStateHolder async save ( streamStateHolder ) { await this . #store . put ( streamStateHolder . id . toString ( ) , StreamUtils . serializeState ( streamStateHolder . state ) ) ; } param streamId async load ( streamId ) { try { const state = await this . #store . get ( streamId . baseID . toString ( ) ) ; if ( state ) { return StreamUtils . deserializeState ( 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 ? ) { let streamIds ; if ( streamId == null ) { return this . #store . stream ( { keys : true , values : false } ) ; } else { const exists = Boolean ( await this . load ( streamId . baseID ) ) ; streamIds = exists ? [ streamId . toString ( ) ] : [ ] ; } return streamIds ; } /**
     * Close pinning service
     */ async close ( ) { } }