import { ICreateBlobParams , ICreateTreeEntry , ICreateTreeParams } from "@fluidframework/gitresources" ; import { getGitMode , getGitType } from "@fluidframework/protocol-base" ; import { SummaryObject , SummaryType } from "@fluidframework/protocol-definitions" ; import { IWholeSummaryBlob , IWholeSummaryPayload , IWholeSummaryTree , IWholeSummaryTreeHandleEntry , IWholeSummaryTreeValueEntry , NetworkError , WholeSummaryTreeEntry , } from "@fluidframework/server-services-client" ; function getSummaryObjectFromWholeSummaryTreeEntry ( entry ) { if ( ( entry as IWholeSummaryTreeHandleEntry ) . id !== undefined ) { return { type : SummaryType . Handle , handleType : entry . type === "tree" ? SummaryType . Tree : SummaryType . Blob , handle : ( entry as IWholeSummaryTreeHandleEntry ) . id } ; } if ( entry . type === "blob" ) { return { type : SummaryType . Blob , content : "" } ; } if ( entry . type === "tree" ) { return { type : SummaryType . Tree , tree : { } , unreferenced : ( entry as IWholeSummaryTreeValueEntry ) . unreferenced } ; } throw new NetworkError ( 400 , `Unknown entry type: ${ entry . type }` ) ; } export class WholeSummaryWriteGitManager { constructor ( /**
     * Write blob to storage and return the git sha.
     */ private readonly writeBlob , /**
     * Write tree to storage and return the git sha.
     */ private readonly writeTree ) { } public async writeSummary ( payload ) { return this . writeSummaryTreeCore ( payload . entries ) ; } private async writeSummaryTreeCore ( wholeSummaryTreeEntries ) { const entries = await Promise . all ( wholeSummaryTreeEntries . map ( async ( entry ) => { const summaryObject = getSummaryObjectFromWholeSummaryTreeEntry ( entry ) ; const pathHandle = await this . writeSummaryTreeObject ( entry , summaryObject ) ; return { mode : getGitMode ( summaryObject ) , path : entry . path , sha : pathHandle , type : getGitType ( summaryObject ) } ; } ) ) ; const treeHandle = await this . writeTree ( { tree : entries } ) ; return treeHandle ; } private async writeSummaryTreeObject ( wholeSummaryTreeEntry , summaryObject ) { switch ( summaryObject . type ) { case SummaryType . Blob : return this . writeSummaryBlob ( ( ( wholeSummaryTreeEntry as IWholeSummaryTreeValueEntry ) . value as IWholeSummaryBlob ) ) ; case SummaryType . Tree : return this . writeSummaryTreeCore ( ( ( wholeSummaryTreeEntry as IWholeSummaryTreeValueEntry ) . value as IWholeSummaryTree ) . entries ?? [ ] ) ; case SummaryType . Handle : return ( wholeSummaryTreeEntry as IWholeSummaryTreeHandleEntry ) . id ; default : throw new NetworkError ( 501 , "Not Implemented" ) ; } } private async writeSummaryBlob ( blob ) { const blobSha = await this . writeBlob ( { content : blob . content , encoding : blob . encoding } ) ; return blobSha ; } }