import { IChannelStorageService } from "@fluidframework/datastore-definitions" ; import { stringToBuffer } from "@fluidframework/common-utils" ; import { IBlob , ITree , TreeEntry } from "@fluidframework/protocol-definitions" ; import { listBlobsAtTreePath } from "@fluidframework/runtime-utils" ; export class LocalChannelStorageService implements IChannelStorageService { constructor ( private readonly tree ) { } public async readBlob ( path ) { const blob = this . readBlobSync ( path ) ; if ( blob === undefined ) { throw new Error ( "Blob Not Found" ) ; } return stringToBuffer ( blob . contents , blob . encoding ) ; } public async contains ( path ) { const blob = this . readBlobSync ( path ) ; return blob !== undefined ? blob . contents !== undefined : false ; } public async list ( path ) { return listBlobsAtTreePath ( this . tree , path ) ; } private readBlobSync ( path ) { return this . readBlobSyncInternal ( path , this . tree ) ; } private readBlobSyncInternal ( path , tree ) { for ( const entry of tree . entries ) { switch ( entry . type ) { case TreeEntry . Blob : if ( path === entry . path ) { return entry . value ; } break ; case TreeEntry . Tree : if ( path . startsWith ( entry . path ) ) { return this . readBlobSyncInternal ( path . substr ( entry . path . length + 1 ) , entry . value ) ; } break ; default : } } return undefined ; } }