import { fromBase64ToUtf8 , fromUtf8ToBase64 } from "@fluidframework/common-utils" ; import { OdspFluidDataStoreLocator } from "./contractsPublic" ; import { OdcFileSiteOrigin , OdcApiSiteOrigin } from "./constants" ; const fluidSignature = "1" ; const fluidSignatureParamName = "fluid" ; const fluidSitePathParamName = "s" ; const fluidDriveIdParamName = "d" ; const fluidItemIdParamName = "f" ; const fluidDataStorePathParamName = "c" ; const fluidAppNameParamName = "a" ; const fluidContainerPackageNameParamName = "p" ; const fluidFileVersionParamName = "v" ; param locator returns export function encodeOdspFluidDataStoreLocator ( locator ) { const siteUrl = new URL ( locator . siteUrl ) ; const sitePath = encodeURIComponent ( siteUrl . pathname ) ; const driveId = encodeURIComponent ( locator . driveId ) ; const itemId = encodeURIComponent ( locator . itemId ) ; const dataStorePath = encodeURIComponent ( locator . dataStorePath ) ; let locatorSerialized = `${ fluidSitePathParamName }=${ sitePath }&${ fluidDriveIdParamName }=${ driveId }&${ fluidItemIdParamName }=${ itemId }&${ fluidDataStorePathParamName }=${ dataStorePath }&${ fluidSignatureParamName }=${ fluidSignature }` ; if ( locator . appName ) { locatorSerialized += `&${ fluidAppNameParamName }=${ encodeURIComponent ( locator . appName ) }` ; } if ( locator . containerPackageName ) { locatorSerialized += `&${ fluidContainerPackageNameParamName }=${ encodeURIComponent ( locator . containerPackageName ) }` ; } if ( locator . fileVersion ) { locatorSerialized += `&${ fluidFileVersionParamName }=${ encodeURIComponent ( locator . fileVersion ) }` ; } return fromUtf8ToBase64 ( locatorSerialized ) ; } param encodedLocatorValue param siteOriginUrl returns function decodeOdspFluidDataStoreLocator ( encodedLocatorValue , siteOriginUrl ) { const locatorInfo = new URLSearchParams ( fromBase64ToUtf8 ( encodedLocatorValue ) ) ; const signatureValue = locatorInfo . get ( fluidSignatureParamName ) ; if ( signatureValue !== "1" ) { return undefined ; } const sitePath = locatorInfo . get ( fluidSitePathParamName ) ; const driveId = locatorInfo . get ( fluidDriveIdParamName ) ; const itemId = locatorInfo . get ( fluidItemIdParamName ) ; const dataStorePath = locatorInfo . get ( fluidDataStorePathParamName ) ; const appName = locatorInfo . get ( fluidAppNameParamName ) ?? undefined ; const containerPackageName = locatorInfo . get ( fluidContainerPackageNameParamName ) ?? undefined ; const fileVersion = locatorInfo . get ( fluidFileVersionParamName ) ?? undefined ; if ( ! sitePath || ! driveId || ! itemId || dataStorePath === null ) { return undefined ; } let siteUrl ; try { siteUrl = new URL ( sitePath , siteOriginUrl ) ; } catch { } if ( ! siteUrl ) { return undefined ; } return { siteUrl : siteUrl . href , driveId , itemId , dataStorePath , appName , containerPackageName , fileVersion } ; } export const locatorQueryParamName = "nav" ; param url param locator export function storeLocatorInOdspUrl ( url , locator ) { const encodedLocatorValue = encodeOdspFluidDataStoreLocator ( locator ) ; url . searchParams . set ( locatorQueryParamName , encodedLocatorValue ) ; } param url returns export function getLocatorFromOdspUrl ( url ) { const encodedLocatorValue = url . searchParams . get ( locatorQueryParamName ) ; if ( ! encodedLocatorValue ) { return undefined ; } const siteOriginUrl = url . origin . toLowerCase ( ) === OdcFileSiteOrigin ? OdcApiSiteOrigin : url . origin ; return decodeOdspFluidDataStoreLocator ( encodedLocatorValue , siteOriginUrl ) ; }