import { assert , stringToBuffer } from "@fluidframework/common-utils" ; import * as api from "@fluidframework/protocol-definitions" ; import { IOdspSnapshot , IOdspSnapshotCommit } from "./contracts" ; import { ISnapshotContents } from "./odspUtils" ; param flatTree param blobsShaToPathCache returns function buildHierarchy ( flatTree ) { const lookup = { } ; const root = { id : flatTree . id , blobs : { } , commits : { } , trees : { } } ; lookup [ "" ] = root ; for ( const entry of flatTree . entries ) { const lastIndex = entry . path . lastIndexOf ( "/" ) ; const entryPathDir = entry . path . slice ( 0 , Math . max ( 0 , lastIndex ) ) ; const entryPathBase = entry . path . slice ( lastIndex + 1 ) ; const node = lookup [ entryPathDir ] ; if ( entry . type === "tree" ) { const newTree = { blobs : { } , commits : { } , trees : { } , unreferenced : entry . unreferenced } ; node . trees [ decodeURIComponent ( entryPathBase ) ] = newTree ; lookup [ entry . path ] = newTree ; } else if ( entry . type === "blob" ) { node . blobs [ decodeURIComponent ( entryPathBase ) ] = entry . id ; } else if ( entry . type === "commit" ) { node . commits [ decodeURIComponent ( entryPathBase ) ] = entry . id ; } } return root ; } param odspSnapshot export function convertOdspSnapshotToSnapsohtTreeAndBlobs ( odspSnapshot ) { const blobsWithBufferContent = new Map < string , ArrayBuffer > ( ) ; if ( odspSnapshot . blobs ) { odspSnapshot . blobs . forEach ( ( blob ) => { assert ( blob . encoding === "base64" || blob . encoding === undefined , 0x0a4 ) ; blobsWithBufferContent . set ( blob . id , stringToBuffer ( blob . content , blob . encoding ?? "utf8" ) ) ; } ) ; } const val = { blobs : blobsWithBufferContent , ops : odspSnapshot . ops ?. map ( ( op ) => op . op ) ?? [ ] , sequenceNumber : odspSnapshot . trees && ( odspSnapshot . trees [ 0 ] ) . sequenceNumber , snapshotTree : buildHierarchy ( odspSnapshot . trees [ 0 ] ) } ; return val ; }