/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import { IFluidHandle, IRequest, IResponse, type IFluidLoadable, } from "@fluidframework/core-interfaces"; import { assert } from "@fluidframework/core-utils/internal"; import { FluidDataStoreRuntime, FluidObjectHandle, mixinRequestHandler, } from "@fluidframework/datastore/internal"; import { IChannelFactory, IFluidDataStoreRuntime, } from "@fluidframework/datastore-definitions/internal"; import { ISharedMap, SharedMap } from "@fluidframework/map/internal"; import { IFluidDataStoreChannel, IFluidDataStoreContext, IFluidDataStoreFactory, } from "@fluidframework/runtime-definitions/internal"; import { create404Response } from "@fluidframework/runtime-utils/internal"; import type { ISharedObject } from "@fluidframework/shared-object-base/internal"; import { ITestFluidObject } from "./interfaces.js"; /** * A test Fluid object that will create a shared object for each key-value pair in the initialSharedObjectsFactories passed to load. * The shared objects can be retrieved by passing the key of the entry to getSharedObject. * It exposes the IFluidDataStoreContext and IFluidDataStoreRuntime. * @privateRemarks * TODO: * Usage of this outside this repo (via ITestFluidObject) should probably be phased out. * Once thats done, ITestFluidObject can be made internal and this class can be replaced with the simplified TestFluidObjectInternal. * @internal */ export class TestFluidObject implements ITestFluidObject { public get ITestFluidObject(): ITestFluidObject { return this; } public get IFluidLoadable(): IFluidLoadable { return this; } public readonly handle: IFluidHandle; public root!: ISharedMap; private initializationPromise: Promise | undefined; /** * Creates a new TestFluidObject. * @param runtime - The data store runtime. * @param context - The data store context. * @param initialSharedObjectsFactories - A list of id to IChannelFactory mapping. For each item in the list, * a shared object is created which can be retrieved by calling getSharedObject() with the id; */ constructor( public readonly runtime: IFluidDataStoreRuntime, public readonly channel: IFluidDataStoreChannel, public readonly context: IFluidDataStoreContext, private readonly initialSharedObjectsFactories: ReadonlyMap< string, IChannelFactory >, ) { this.handle = new FluidObjectHandle(this, "", runtime.objectsRoutingContext); } /** * Retrieves a shared object with the given id. * @param id - The id of the shared object to retrieve. */ public async getSharedObject(id: string): Promise { if (this.initialSharedObjectsFactories === undefined) { throw new Error("Shared objects were not provided during creation."); } if (this.initialSharedObjectsFactories.has(id)) { const handle = this.root.get>(id); if (handle === undefined) { throw new Error( `Shared object with id '${id}' is in initialSharedObjectsFactories but not found under root.`, ); } return handle.get(); } throw new Error(`Shared object with id ${id} not found.`); } public async request(request: IRequest): Promise { return request.url === "" || request.url === "/" || request.url.startsWith("/?") ? { mimeType: "fluid/object", status: 200, value: this } : create404Response(request); } public async initialize(existing: boolean): Promise { const doInitialization = async (): Promise => { if (!existing) { this.root = SharedMap.create(this.runtime, "root"); this.initialSharedObjectsFactories.forEach( (sharedObjectFactory: IChannelFactory, key: string) => { const sharedObject = this.runtime.createChannel(key, sharedObjectFactory.type); this.root.set(key, sharedObject.handle); }, ); this.root.bindToContext(); } this.root = (await this.runtime.getChannel("root")) as ISharedMap; }; this.initializationPromise ??= doInitialization(); return this.initializationPromise; } } /** * Iterable\<[ChannelId, IChannelFactory]\>. * @internal */ export type ChannelFactoryRegistry = Iterable<[string | undefined, IChannelFactory]>; /** * Kind of test data object which {@link TestFluidObjectFactory} can create. * @internal */ export type TestDataObjectKind = new ( runtime: IFluidDataStoreRuntime, channel: IFluidDataStoreChannel, context: IFluidDataStoreContext, initialSharedObjectsFactories: ReadonlyMap>, ) => IFluidLoadable & { request(request: IRequest): Promise; initialize(existing: boolean): Promise; }; /** * Creates a factory for a TestFluidObject with the given object factory entries. It creates a data store runtime * with the object factories in the entry list. All the entries with an id other than undefined are passed to the * Fluid object so that it can create a shared object for each. * * @example * * The following will create a Fluid object that creates and loads a SharedString and SharedDirectory. * It will add SparseMatrix to the data store's factory so that it can be created later. * * ```typescript * new TestFluidObjectFactory([ * [ "sharedString", SharedString.getFactory() ], * [ "sharedDirectory", SharedDirectory.getFactory() ], * [ undefined, SparseMatrix.getFactory() ], * ]); * ``` * * The SharedString and SharedDirectory can be retrieved via getSharedObject() on the TestFluidObject as follows: * * ```typescript * sharedString = testFluidObject.getSharedObject("sharedString"); * sharedDir = testFluidObject.getSharedObject("sharedDirectory"); * ``` * * @privateRemarks Beware that using this class generally forfeits some compatibility coverage * `describeCompat` aims to provide: * `SharedMap`s always reference the current version of SharedMap. * AB#4670 tracks improving this situation. * @internal */ export class TestFluidObjectFactory implements IFluidDataStoreFactory { public get IFluidDataStoreFactory(): IFluidDataStoreFactory { return this; } /** * Creates a new TestFluidObjectFactory. * @param initialSharedObjectsFactories - A list of id to IChannelFactory mapping. It creates a data store runtime with each * IChannelFactory. Entries with string ids are passed to the Fluid object so that it can create a shared object * for it. */ constructor( private readonly initialSharedObjectsFactories: ChannelFactoryRegistry, public readonly type = "TestFluidObjectFactory", private readonly dataObjectKind: TestDataObjectKind = TestFluidObject, ) {} public async instantiateDataStore( context: IFluidDataStoreContext, existing: boolean, ): Promise { const dataTypes = new Map(); // Add SharedMap's factory which will be used to create the root map. const sharedMapFactory = SharedMap.getFactory(); dataTypes.set(sharedMapFactory.type, sharedMapFactory); // Add the object factories to the list to be sent to data store runtime. for (const [, factory] of this.initialSharedObjectsFactories) { dataTypes.set(factory.type, factory); } // Create a map from the factory entries with entries that don't have the id as undefined. This will be // passed to the Fluid object. const factoryEntriesMapForObject = new Map>(); for (const [id, factory] of this.initialSharedObjectsFactories) { if (id !== undefined) { // Here we assume the factory produces an ISharedObject. factoryEntriesMapForObject.set(id, factory as IChannelFactory); } } const runtimeClass = mixinRequestHandler( async (request: IRequest, rt: FluidDataStoreRuntime) => { // The provideEntryPoint callback below always returns TestFluidObject. const dataObject = await rt.entryPoint.get(); assert( dataObject instanceof this.dataObjectKind, "entryPoint should have been initialized by now", ); return dataObject.request(request); }, ); const runtime: FluidDataStoreRuntime = new runtimeClass( context, dataTypes, existing, async () => { await instance.initialize(true); return instance; }, ); const instance = new this.dataObjectKind( runtime, // runtime runtime, // channel context, factoryEntriesMapForObject, ); if (!existing) { await instance.initialize(false); } return runtime; } }