Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fixes in creation driver #1125

Merged
merged 4 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/drivers/create-new-driver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@microsoft/fluid-core-utils": "^0.12.0",
"@microsoft/fluid-driver-definitions": "^0.12.0",
"@microsoft/fluid-component-core-interfaces": "^0.12.0",
"@microsoft/fluid-protocol-definitions": "^0.1000.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
// import * as assert from "assert";
import { IDocumentDeltaStorageService } from "@microsoft/fluid-driver-definitions";
import * as api from "@microsoft/fluid-protocol-definitions";
import { CreationServerMessagesHandler } from ".";

/**
* Provides access to the false delta storage.
*/
export class CreationDeltaStorageService implements IDocumentDeltaStorageService {

constructor() {
constructor(private readonly serverMessagesHandler: CreationServerMessagesHandler) {
}

public async get(
from?: number,
to?: number,
): Promise<api.ISequencedDocumentMessage[]> {
return [];
return this.serverMessagesHandler.queuedMessages.slice(from, to);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ import { CreationServerMessagesHandler } from "./creationDriverServer";
* The DocumentService connects to in memory endpoints for storage/socket for faux document service.
*/
export class CreationDocumentService implements api.IDocumentService {

private readonly creationServer: CreationServerMessagesHandler;
constructor(
private readonly documentId: string,
private readonly tenantId: string) {
this.creationServer = CreationServerMessagesHandler.getInstance(this.documentId);
}

public async connectToStorage(): Promise<api.IDocumentStorageService> {
return new CreationDocumentStorageService();
}

public async connectToDeltaStorage(): Promise<api.IDocumentDeltaStorageService> {
return new CreationDeltaStorageService();
return new CreationDeltaStorageService(this.creationServer);
}

/**
Expand All @@ -35,8 +38,7 @@ export class CreationDocumentService implements api.IDocumentService {
public async connectToDeltaStream(
client: IClient,
mode: ConnectionMode): Promise<api.IDocumentDeltaConnection> {
const creationServer = CreationServerMessagesHandler.getInstance(this.documentId);
return new CreationDocumentDeltaConnection(client, mode, this.documentId, this.tenantId, creationServer);
return new CreationDocumentDeltaConnection(client, mode, this.documentId, this.tenantId, this.creationServer);
}

public async branch(): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IDocumentServiceFactory,
IResolvedUrl,
} from "@microsoft/fluid-driver-definitions";
import { parse } from "url";
import { CreationDocumentService } from "./creationDocumentService";

/**
Expand All @@ -20,7 +21,17 @@ export class CreationDocumentServiceFactory implements IDocumentServiceFactory {
constructor() {
}

public async createDocumentService(url: IResolvedUrl): Promise<IDocumentService> {
return new CreationDocumentService("createNewFileDoc", "createNewFileDocTenant");
public async createDocumentService(resolvedUrl: IResolvedUrl): Promise<IDocumentService> {
if (resolvedUrl.type !== "fluid") {
// tslint:disable-next-line:max-line-length
return Promise.reject("Only Fluid components currently supported in the RouterliciousDocumentServiceFactory");
}

const fluidResolvedUrl = resolvedUrl;
const parsedUrl = parse(fluidResolvedUrl.url);
const [, , documentId] = parsedUrl.pathname!.split("/");
return new CreationDocumentService(
documentId,
"createNewFileDocTenant");
}
}
15 changes: 10 additions & 5 deletions packages/drivers/create-new-driver/src/creationDriverServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ interface IAugmentedDocumentMessage {
*/
export class CreationServerMessagesHandler {

public static getInstance(documentId?: string): CreationServerMessagesHandler {
if (CreationServerMessagesHandler.instance === undefined && documentId !== undefined) {
CreationServerMessagesHandler.instance = new CreationServerMessagesHandler(documentId);
public static getInstance(documentId: string): CreationServerMessagesHandler {
if (CreationServerMessagesHandler.urlMap.has(documentId)) {
return CreationServerMessagesHandler.urlMap.get(documentId)!;
} else {
const instance = new CreationServerMessagesHandler(documentId);
CreationServerMessagesHandler.urlMap.set(documentId, instance);
return instance;
}
return CreationServerMessagesHandler.instance;
}

private static instance: CreationServerMessagesHandler;
// This map is from url to instance of server for that url. So this leads to creation of only 1 server instance
// for different clients of same file but different instances for different files.
private static readonly urlMap: Map<string, CreationServerMessagesHandler> = new Map();

// These are the queues for messages, signals, contents that will be pushed to server when
// an actual connection is created.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { IRequest } from "@microsoft/fluid-component-core-interfaces";
import { IFluidResolvedUrl, IResolvedUrl, IUrlResolver } from "@microsoft/fluid-driver-definitions";

export class CreationDriverUrlResolver implements IUrlResolver {
constructor() { }

public async resolve(request: IRequest): Promise<IResolvedUrl> {
const [, queryString] = request.url.split("?");

const searchParams = new URLSearchParams(queryString);

const uniqueId = searchParams.get("uniqueId");
if (uniqueId === null) {
throw new Error("URL for creation driver should contain the uniqueId");
}
const response: IFluidResolvedUrl = {
endpoints: { snapshotStorageUrl: "" },
tokens: {},
type: "fluid",
url: `fluid-creation://placeholder/placeholder/${uniqueId}`,
};

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
* Licensed under the MIT License.
*/

import { IDocumentDeltaConnection, IDocumentService, IResolvedUrl } from "@microsoft/fluid-driver-definitions";
import { IDocumentDeltaConnection, IDocumentService, IFluidResolvedUrl } from "@microsoft/fluid-driver-definitions";
import { IClient, IDocumentMessage, MessageType, ScopeType } from "@microsoft/fluid-protocol-definitions";
import * as assert from "assert";
import { CreationServerMessagesHandler } from "..";
import { CreationDocumentServiceFactory } from "../creationDocumentServiceFactory";
import { CreationDriverUrlResolver } from "../creationDriverUrlResolver";

describe("Creation Driver", () => {

let service: IDocumentService;
let client: IClient;
let documentDeltaConnection1: IDocumentDeltaConnection;
let documentDeltaConnection2: IDocumentDeltaConnection;
const docId = "docId";
let resolved: IFluidResolvedUrl;
beforeEach(async () => {
const resolver: CreationDriverUrlResolver = new CreationDriverUrlResolver();
const factory = new CreationDocumentServiceFactory();
const resolved: IResolvedUrl = {endpoints: {}, type: "fluid", url: "", tokens: {}};
resolved = (await resolver.resolve({url: `http://fluid.com?uniqueId=${docId}`})) as IFluidResolvedUrl;
service = await factory.createDocumentService(resolved);
client = {
mode: "write",
Expand Down Expand Up @@ -48,7 +52,8 @@ describe("Creation Driver", () => {
referenceSequenceNumber: 0,
type: MessageType.Operation,
};
const creationServerMessagesHandler = CreationServerMessagesHandler.getInstance();
const creationServerMessagesHandler =
CreationServerMessagesHandler.getInstance(docId);
assert.equal(creationServerMessagesHandler.queuedMessages.length, 2,
"Total messages should be 2 at this time including join messages");
documentDeltaConnection1.submit([message]);
Expand Down