import * as url from "url" ; import { IFluidCodeDetails } from "@fluidframework/core-interfaces" ; import { Loader } from "@fluidframework/container-loader" ; import { IFluidResolvedUrl } from "@fluidframework/driver-definitions" ; import { ITokenClaims , IUser } from "@fluidframework/protocol-definitions" ; import { RouterliciousDocumentServiceFactory } from "@fluidframework/routerlicious-driver" ; import { ContainerUrlResolver } from "@fluidframework/routerlicious-host" ; import { InsecureTokenProvider } from "@fluidframework/test-runtime-utils" ; import * as jwt from "jsonwebtoken" ; import { v4 as uuid } from "uuid" ; import { NodeCodeLoader } from "./nodeCodeloader" ; import { fetchFluidObject } from "./utils" ; const ordererEndpoint = "http://localhost:7070" ; const storageEndpoint = "http://localhost:7070" ; const tenantId = "tinylicious" ; const tenantKey = "12345" ; const bearerSecret = "12345" ; const defaultPackage = "@fluid-example/key-value-cache@0.49.0" ; const installPath = "/tmp/fluid-objects" ; const timeoutMS = 120000 ; const docId = "" ; const user = { id : "node-user" , name : "Node User" } as IUser ; export async function start ( ) { const createNew = docId . length === 0 ; const documentId = docId . length === 0 ? uuid ( ) : docId ; const hostToken = jwt . sign ( { user } , bearerSecret ) ; const claims = { documentId , scopes : [ "doc:read" , "doc:write" , "summary:write" ] , tenantId , user , iat : Math . round ( new Date ( ) . getTime ( ) / 1000 ) , exp : Math . round ( new Date ( ) . getTime ( ) / 1000 ) + 60 * 60 , ver : "1.0" } ; const token = jwt . sign ( claims , tenantKey ) ; const encodedTenantId = encodeURIComponent ( tenantId ) ; const encodedDocId = encodeURIComponent ( documentId ) ; const documentUrl = `fluid://${ url . parse ( ordererEndpoint ) . host }/${ encodedTenantId }/${ encodedDocId }` ; const deltaStorageUrl = `${ ordererEndpoint }/deltas/${ encodedTenantId }/${ encodedDocId }` ; const storageUrl = `${ storageEndpoint }/repos/${ encodedTenantId }` ; const requestUrl = `http://${ url . parse ( ordererEndpoint ) . host }/${ encodedTenantId }/${ encodedDocId }` ; const resolved = { endpoints : { deltaStorageUrl , ordererUrl : ordererEndpoint , storageUrl } , id : documentId , tokens : { jwt : token } , type : "fluid" , url : documentUrl } ; const urlResolver = new ContainerUrlResolver ( ordererEndpoint , hostToken , new Map ( [ [ requestUrl , resolved ] ] ) ) ; const codeLoader = new NodeCodeLoader ( installPath , timeoutMS ) ; const tokenProvider = new InsecureTokenProvider ( tenantKey , user ) ; const loader = new Loader ( { urlResolver , documentServiceFactory : new RouterliciousDocumentServiceFactory ( tokenProvider ) , codeLoader } ) ; const details = { config : { } , package : defaultPackage } ; let container ; if ( createNew ) { container = await loader . createDetachedContainer ( details ) ; await container . attach ( { url : requestUrl } ) ; } else { container = await loader . resolve ( { url : requestUrl } ) ; } await fetchFluidObject ( loader , container , requestUrl ) ; } start ( ) ;