forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentDeltaConnection.ts
74 lines (67 loc) · 2.87 KB
/
documentDeltaConnection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { ITelemetryLogger } from "@fluidframework/common-definitions";
import { DocumentDeltaConnection } from "@fluidframework/driver-base";
import { IDocumentDeltaConnection } from "@fluidframework/driver-definitions";
import { IAnyDriverError } from "@fluidframework/driver-utils";
import { IClient, IConnect } from "@fluidframework/protocol-definitions";
import type { io as SocketIOClientStatic } from "socket.io-client";
import { errorObjectFromSocketError, IR11sSocketError } from "./errorUtils";
import { pkgVersion as driverVersion } from "./packageVersion";
const protocolVersions = ["^0.4.0", "^0.3.0", "^0.2.0", "^0.1.0"];
/**
* Wrapper over the shared one for driver specific translation.
*/
export class R11sDocumentDeltaConnection extends DocumentDeltaConnection {
public static async create(
tenantId: string,
id: string,
token: string | null,
io: typeof SocketIOClientStatic,
client: IClient,
url: string,
logger: ITelemetryLogger,
timeoutMs = 20000): Promise<IDocumentDeltaConnection> {
const socket = io(
url,
{
query: {
documentId: id,
tenantId,
},
reconnection: false,
// Default to websocket connection, with long-polling disabled
transports: ["websocket"],
timeout: timeoutMs,
});
const connectMessage: IConnect = {
client,
id,
mode: client.mode,
tenantId,
token, // Token is going to indicate tenant level information, etc...
versions: protocolVersions,
relayUserAgent: [client.details.environment, ` driverVersion:${driverVersion}`].join(";"),
};
// TODO: expose to host at factory level
const enableLongPollingDowngrades = true;
const deltaConnection = new R11sDocumentDeltaConnection(socket, id, logger, enableLongPollingDowngrades);
await deltaConnection.initialize(connectMessage, timeoutMs);
return deltaConnection;
}
/**
* Error raising for socket.io issues
*/
protected createErrorObject(handler: string, error?: any, canRetry = true): IAnyDriverError {
// Note: we suspect the incoming error object is either:
// - a socketError: add it to the R11sError object for driver to be able to parse it and reason over it.
// - anything else: let base class handle it
if (canRetry && Number.isInteger(error?.code) && typeof error?.message === "string") {
return errorObjectFromSocketError(error as IR11sSocketError, handler);
} else {
return super.createErrorObject(handler, error, canRetry);
}
}
}