@fluidframework/shared-object-base 0.54.2 → 0.56.0-49831
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +5 -2
- package/bench/src/index.ts +68 -0
- package/bench/src/util.ts +56 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/remoteObjectHandle.d.ts +32 -0
- package/dist/remoteObjectHandle.d.ts.map +1 -0
- package/dist/remoteObjectHandle.js +66 -0
- package/dist/remoteObjectHandle.js.map +1 -0
- package/dist/serializer.d.ts +82 -0
- package/dist/serializer.d.ts.map +1 -0
- package/dist/serializer.js +148 -0
- package/dist/serializer.js.map +1 -0
- package/dist/sharedObject.d.ts +55 -26
- package/dist/sharedObject.d.ts.map +1 -1
- package/dist/sharedObject.js +78 -75
- package/dist/sharedObject.js.map +1 -1
- package/dist/summarySerializer.d.ts +1 -1
- package/dist/summarySerializer.d.ts.map +1 -1
- package/dist/summarySerializer.js +2 -2
- package/dist/summarySerializer.js.map +1 -1
- package/dist/types.d.ts +8 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils.d.ts +10 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +15 -5
- package/dist/utils.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.d.ts.map +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/remoteObjectHandle.d.ts +32 -0
- package/lib/remoteObjectHandle.d.ts.map +1 -0
- package/lib/remoteObjectHandle.js +62 -0
- package/lib/remoteObjectHandle.js.map +1 -0
- package/lib/serializer.d.ts +82 -0
- package/lib/serializer.d.ts.map +1 -0
- package/lib/serializer.js +143 -0
- package/lib/serializer.js.map +1 -0
- package/lib/sharedObject.d.ts +55 -26
- package/lib/sharedObject.d.ts.map +1 -1
- package/lib/sharedObject.js +76 -74
- package/lib/sharedObject.js.map +1 -1
- package/lib/summarySerializer.d.ts +1 -1
- package/lib/summarySerializer.d.ts.map +1 -1
- package/lib/summarySerializer.js +1 -1
- package/lib/summarySerializer.js.map +1 -1
- package/lib/types.d.ts +8 -2
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib/utils.d.ts +10 -1
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +13 -4
- package/lib/utils.js.map +1 -1
- package/package.json +55 -19
- package/src/index.ts +1 -0
- package/src/packageVersion.ts +1 -1
- package/src/remoteObjectHandle.ts +79 -0
- package/src/serializer.ts +216 -0
- package/src/sharedObject.ts +117 -97
- package/src/summarySerializer.ts +1 -1
- package/src/types.ts +9 -2
- package/src/utils.ts +17 -8
- package/tsconfig.json +4 -4
package/lib/utils.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
+
import { SummaryTreeBuilder } from "@fluidframework/runtime-utils";
|
|
5
6
|
/**
|
|
6
7
|
* Given a mostly-plain object that may have handle objects embedded within, return a string representation of an object
|
|
7
8
|
* where the handle objects have been replaced with a serializable form.
|
|
@@ -11,7 +12,6 @@
|
|
|
11
12
|
* @param bind - Bind any other handles we find in the object against this given handle.
|
|
12
13
|
* @returns Result of strigifying an object
|
|
13
14
|
*/
|
|
14
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
15
15
|
export function serializeHandles(value, serializer, bind) {
|
|
16
16
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
17
17
|
return value !== undefined
|
|
@@ -30,10 +30,9 @@ export function serializeHandles(value, serializer, bind) {
|
|
|
30
30
|
* @param bind - Bind any other handles we find in the object against this given handle.
|
|
31
31
|
* @returns The fully-plain object
|
|
32
32
|
*/
|
|
33
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
34
33
|
export function makeHandlesSerializable(value, serializer, bind) {
|
|
35
34
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
36
|
-
return serializer.
|
|
35
|
+
return serializer.encode(value, bind);
|
|
37
36
|
}
|
|
38
37
|
/**
|
|
39
38
|
* Given a fully-plain object that may have serializable-form handles within, will return the mostly-plain object
|
|
@@ -43,9 +42,19 @@ export function makeHandlesSerializable(value, serializer, bind) {
|
|
|
43
42
|
* @param context - The handle context for the container
|
|
44
43
|
* @returns The mostly-plain object with handle objects within
|
|
45
44
|
*/
|
|
46
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
47
45
|
export function parseHandles(value, serializer) {
|
|
48
46
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
49
47
|
return value !== undefined ? serializer.parse(JSON.stringify(value)) : value;
|
|
50
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Create a new summary containing one blob
|
|
51
|
+
* @param key - the key for the blob in the summary
|
|
52
|
+
* @param content - blob content
|
|
53
|
+
* @returns The summary containing the blob
|
|
54
|
+
*/
|
|
55
|
+
export function createSingleBlobSummary(key, content) {
|
|
56
|
+
const builder = new SummaryTreeBuilder();
|
|
57
|
+
builder.addBlob(key, content);
|
|
58
|
+
return builder.getSummaryTree();
|
|
59
|
+
}
|
|
51
60
|
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAGnE;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAC5B,KAAU,EACV,UAA4B,EAC5B,IAAkB;IAElB,+DAA+D;IAC/D,OAAO,KAAK,KAAK,SAAS;QACtB,CAAC,CAAC,UAAU,CAAC,SAAS,CAClB,KAAK,EACL,IAAI,CAAC;QACT,CAAC,CAAC,KAAK,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,uBAAuB,CACnC,KAAU,EACV,UAA4B,EAC5B,IAAkB;IAElB,+DAA+D;IAC/D,OAAO,UAAU,CAAC,MAAM,CACpB,KAAK,EACL,IAAI,CAAC,CAAC;AACd,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CACxB,KAAU,EACV,UAA4B;IAE5B,+DAA+D;IAC/D,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AACjF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,GAAW,EAAE,OAA4B;IAC7E,MAAM,OAAO,GAAG,IAAI,kBAAkB,EAAE,CAAC;IACzC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,OAAO,CAAC,cAAc,EAAE,CAAC;AACpC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IFluidHandle } from \"@fluidframework/core-interfaces\";\nimport { ISummaryTreeWithStats } from \"@fluidframework/runtime-definitions\";\nimport { SummaryTreeBuilder } from \"@fluidframework/runtime-utils\";\nimport { IFluidSerializer } from \"./serializer\";\n\n/**\n * Given a mostly-plain object that may have handle objects embedded within, return a string representation of an object\n * where the handle objects have been replaced with a serializable form.\n * @param value - The mostly-plain object\n * @param serializer - The serializer that knows how to convert handles into serializable format\n * @param context - The handle context for the container\n * @param bind - Bind any other handles we find in the object against this given handle.\n * @returns Result of strigifying an object\n */\nexport function serializeHandles(\n value: any,\n serializer: IFluidSerializer,\n bind: IFluidHandle,\n): string | undefined {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return value !== undefined\n ? serializer.stringify(\n value,\n bind)\n : value;\n}\n\n/**\n * Given a mostly-plain object that may have handle objects embedded within, will return a fully-plain object\n * where any embedded IFluidHandles have been replaced with a serializable form.\n *\n * The original `input` object is not mutated. This method will shallowly clones all objects in the path from\n * the root to any replaced handles. (If no handles are found, returns the original object.)\n *\n * @param input - The mostly-plain object\n * @param context - The handle context for the container\n * @param bind - Bind any other handles we find in the object against this given handle.\n * @returns The fully-plain object\n */\nexport function makeHandlesSerializable(\n value: any,\n serializer: IFluidSerializer,\n bind: IFluidHandle,\n) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return serializer.encode(\n value,\n bind);\n}\n\n/**\n * Given a fully-plain object that may have serializable-form handles within, will return the mostly-plain object\n * with handle objects created instead.\n * @param value - The fully-plain object\n * @param serializer - The serializer that knows how to convert serializable-form handles into handle objects\n * @param context - The handle context for the container\n * @returns The mostly-plain object with handle objects within\n */\nexport function parseHandles(\n value: any,\n serializer: IFluidSerializer,\n) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return value !== undefined ? serializer.parse(JSON.stringify(value)) : value;\n}\n\n/**\n * Create a new summary containing one blob\n * @param key - the key for the blob in the summary\n * @param content - blob content\n * @returns The summary containing the blob\n */\nexport function createSingleBlobSummary(key: string, content: string | Uint8Array): ISummaryTreeWithStats {\n const builder = new SummaryTreeBuilder();\n builder.addBlob(key, content);\n return builder.getSummaryTree();\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/shared-object-base",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.56.0-49831",
|
|
4
4
|
"description": "Fluid base class for shared distributed data structures",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": "https://github.com/microsoft/FluidFramework",
|
|
@@ -11,54 +11,90 @@
|
|
|
11
11
|
"module": "lib/index.js",
|
|
12
12
|
"types": "dist/index.d.ts",
|
|
13
13
|
"scripts": {
|
|
14
|
+
"bench": "ts-node bench/src/index.ts",
|
|
14
15
|
"build": "npm run build:genver && concurrently npm:build:compile npm:lint && npm run build:docs",
|
|
15
|
-
"build:
|
|
16
|
+
"build:commonjs": "npm run tsc && npm run build:test",
|
|
17
|
+
"build:compile": "concurrently npm:build:commonjs npm:build:esnext",
|
|
16
18
|
"build:docs": "api-extractor run --local --typescript-compiler-folder ../../../node_modules/typescript && copyfiles -u 1 ./_api-extractor-temp/doc-models/* ../../../_api-extractor-temp/",
|
|
17
19
|
"build:esnext": "tsc --project ./tsconfig.esnext.json",
|
|
18
20
|
"build:full": "npm run build",
|
|
19
21
|
"build:full:compile": "npm run build:compile",
|
|
20
22
|
"build:genver": "gen-version",
|
|
23
|
+
"build:test": "tsc --project ./src/test/tsconfig.json",
|
|
21
24
|
"ci:build:docs": "api-extractor run --typescript-compiler-folder ../../../node_modules/typescript && copyfiles -u 1 ./_api-extractor-temp/* ../../../_api-extractor-temp/",
|
|
22
25
|
"clean": "rimraf dist lib *.tsbuildinfo *.build.log",
|
|
23
26
|
"eslint": "eslint --format stylish src",
|
|
24
|
-
"eslint:fix": "eslint --format stylish src --fix",
|
|
27
|
+
"eslint:fix": "eslint --format stylish src --fix --fix-type problem,suggestion,layout",
|
|
25
28
|
"lint": "npm run eslint",
|
|
26
29
|
"lint:fix": "npm run eslint:fix",
|
|
30
|
+
"test": "npm run test:mocha",
|
|
31
|
+
"test:coverage": "nyc npm test -- --reporter xunit --reporter-option output=nyc/junit-report.xml",
|
|
32
|
+
"test:mocha": "mocha --recursive dist/test -r node_modules/@fluidframework/mocha-test-setup --unhandled-rejections=strict",
|
|
33
|
+
"test:mocha:verbose": "cross-env FLUID_TEST_VERBOSE=1 npm run test:mocha",
|
|
27
34
|
"tsc": "tsc",
|
|
28
35
|
"tsfmt": "tsfmt --verify",
|
|
29
36
|
"tsfmt:fix": "tsfmt --replace"
|
|
30
37
|
},
|
|
38
|
+
"nyc": {
|
|
39
|
+
"all": true,
|
|
40
|
+
"cache-dir": "nyc/.cache",
|
|
41
|
+
"exclude": [
|
|
42
|
+
"src/test/**/*.ts",
|
|
43
|
+
"dist/test/**/*.js"
|
|
44
|
+
],
|
|
45
|
+
"exclude-after-remap": false,
|
|
46
|
+
"include": [
|
|
47
|
+
"src/**/*.ts",
|
|
48
|
+
"dist/**/*.js"
|
|
49
|
+
],
|
|
50
|
+
"report-dir": "nyc/report",
|
|
51
|
+
"reporter": [
|
|
52
|
+
"cobertura",
|
|
53
|
+
"html",
|
|
54
|
+
"text"
|
|
55
|
+
],
|
|
56
|
+
"temp-directory": "nyc/.nyc_output"
|
|
57
|
+
},
|
|
31
58
|
"dependencies": {
|
|
32
59
|
"@fluidframework/common-definitions": "^0.20.1",
|
|
33
60
|
"@fluidframework/common-utils": "^0.32.1",
|
|
34
|
-
"@fluidframework/container-definitions": "^0.
|
|
35
|
-
"@fluidframework/container-utils": "
|
|
36
|
-
"@fluidframework/core-interfaces": "^0.
|
|
37
|
-
"@fluidframework/datastore": "
|
|
38
|
-
"@fluidframework/datastore-definitions": "
|
|
61
|
+
"@fluidframework/container-definitions": "^0.45.0-0",
|
|
62
|
+
"@fluidframework/container-utils": "0.56.0-49831",
|
|
63
|
+
"@fluidframework/core-interfaces": "^0.42.0-0",
|
|
64
|
+
"@fluidframework/datastore": "0.56.0-49831",
|
|
65
|
+
"@fluidframework/datastore-definitions": "0.56.0-49831",
|
|
39
66
|
"@fluidframework/protocol-definitions": "^0.1026.0",
|
|
40
|
-
"@fluidframework/runtime-definitions": "
|
|
41
|
-
"@fluidframework/runtime-utils": "
|
|
42
|
-
"@fluidframework/telemetry-utils": "
|
|
67
|
+
"@fluidframework/runtime-definitions": "0.56.0-49831",
|
|
68
|
+
"@fluidframework/runtime-utils": "0.56.0-49831",
|
|
69
|
+
"@fluidframework/telemetry-utils": "0.56.0-49831",
|
|
43
70
|
"uuid": "^8.3.1"
|
|
44
71
|
},
|
|
45
72
|
"devDependencies": {
|
|
46
73
|
"@fluidframework/build-common": "^0.23.0",
|
|
47
|
-
"@fluidframework/eslint-config-fluid": "^0.
|
|
74
|
+
"@fluidframework/eslint-config-fluid": "^0.25.0",
|
|
75
|
+
"@fluidframework/mocha-test-setup": "0.56.0-49831",
|
|
48
76
|
"@microsoft/api-extractor": "^7.16.1",
|
|
77
|
+
"@rushstack/eslint-config": "^2.5.1",
|
|
78
|
+
"@types/benchmark": "^2.1.0",
|
|
79
|
+
"@types/mocha": "^8.2.2",
|
|
49
80
|
"@types/node": "^14.18.0",
|
|
50
|
-
"@typescript-eslint/eslint-plugin": "~
|
|
51
|
-
"@typescript-eslint/parser": "~
|
|
81
|
+
"@typescript-eslint/eslint-plugin": "~5.9.0",
|
|
82
|
+
"@typescript-eslint/parser": "~5.9.0",
|
|
83
|
+
"benchmark": "^2.1.4",
|
|
52
84
|
"concurrently": "^6.2.0",
|
|
53
85
|
"copyfiles": "^2.1.0",
|
|
54
|
-
"
|
|
86
|
+
"cross-env": "^7.0.2",
|
|
87
|
+
"eslint": "~8.6.0",
|
|
88
|
+
"eslint-plugin-editorconfig": "~3.2.0",
|
|
55
89
|
"eslint-plugin-eslint-comments": "~3.2.0",
|
|
56
|
-
"eslint-plugin-import": "~2.
|
|
90
|
+
"eslint-plugin-import": "~2.25.4",
|
|
57
91
|
"eslint-plugin-no-null": "~1.0.2",
|
|
58
|
-
"eslint-plugin-
|
|
59
|
-
"eslint-plugin-
|
|
60
|
-
"
|
|
92
|
+
"eslint-plugin-react": "~7.28.0",
|
|
93
|
+
"eslint-plugin-unicorn": "~40.0.0",
|
|
94
|
+
"mocha": "^8.4.0",
|
|
95
|
+
"nyc": "^15.0.0",
|
|
61
96
|
"rimraf": "^2.6.2",
|
|
97
|
+
"ts-node": "^7.0.1",
|
|
62
98
|
"typescript": "~4.1.3",
|
|
63
99
|
"typescript-formatter": "7.1.0"
|
|
64
100
|
}
|
package/src/index.ts
CHANGED
package/src/packageVersion.ts
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { assert } from "@fluidframework/common-utils";
|
|
7
|
+
import {
|
|
8
|
+
IFluidHandle,
|
|
9
|
+
IFluidHandleContext,
|
|
10
|
+
IRequest,
|
|
11
|
+
IResponse,
|
|
12
|
+
FluidObject,
|
|
13
|
+
IFluidRouter,
|
|
14
|
+
} from "@fluidframework/core-interfaces";
|
|
15
|
+
import { create404Response, exceptionToResponse, responseToException } from "@fluidframework/runtime-utils";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* This handle is used to dynamically load a Fluid object on a remote client and is created on parsing a serialized
|
|
19
|
+
* FluidObjectHandle.
|
|
20
|
+
* This class is used to generate an IFluidHandle when de-serializing any all handles (including handles to DDSs, custom
|
|
21
|
+
* objects) that are stored in SharedObjects. The Data Store or SharedObject corresponding to the IFluidHandle can be
|
|
22
|
+
* retrieved by calling `get` on it.
|
|
23
|
+
*/
|
|
24
|
+
export class RemoteFluidObjectHandle implements IFluidHandle {
|
|
25
|
+
public get IFluidRouter() { return this; }
|
|
26
|
+
public get IFluidHandleContext() { return this; }
|
|
27
|
+
public get IFluidHandle() { return this; }
|
|
28
|
+
|
|
29
|
+
public readonly isAttached = true;
|
|
30
|
+
private objectP: Promise<FluidObject> | undefined;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new RemoteFluidObjectHandle when parsing an IFluidHandle.
|
|
34
|
+
* @param absolutePath - The absolute path to the handle from the container runtime.
|
|
35
|
+
* @param routeContext - The root IFluidHandleContext that has a route to this handle.
|
|
36
|
+
*/
|
|
37
|
+
constructor(
|
|
38
|
+
public readonly absolutePath: string,
|
|
39
|
+
public readonly routeContext: IFluidHandleContext,
|
|
40
|
+
) {
|
|
41
|
+
assert(absolutePath.startsWith("/"), 0x19d /* "Handles should always have absolute paths" */);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public async get(): Promise<any> {
|
|
45
|
+
if (this.objectP === undefined) {
|
|
46
|
+
const request = { url: this.absolutePath };
|
|
47
|
+
this.objectP = this.routeContext.resolveHandle(request)
|
|
48
|
+
.then<FluidObject>((response) => {
|
|
49
|
+
if (response.mimeType === "fluid/object") {
|
|
50
|
+
const fluidObject: FluidObject = response.value;
|
|
51
|
+
return fluidObject;
|
|
52
|
+
}
|
|
53
|
+
throw responseToException(response, request);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return this.objectP;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public attachGraph(): void {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public bind(handle: IFluidHandle): void {
|
|
64
|
+
handle.attachGraph();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public async request(request: IRequest): Promise<IResponse> {
|
|
68
|
+
try {
|
|
69
|
+
const object: FluidObject<IFluidRouter> = await this.get();
|
|
70
|
+
const router = object.IFluidRouter;
|
|
71
|
+
|
|
72
|
+
return router !== undefined
|
|
73
|
+
? router.request(request)
|
|
74
|
+
: create404Response(request);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return exceptionToResponse(error);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// RATIONALE: Many methods consume and return 'any' by necessity.
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
8
|
+
|
|
9
|
+
import { generateHandleContextPath } from "@fluidframework/runtime-utils";
|
|
10
|
+
import { IFluidHandle, IFluidHandleContext } from "@fluidframework/core-interfaces";
|
|
11
|
+
import { RemoteFluidObjectHandle } from "./remoteObjectHandle";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* JSON serialized form of an IFluidHandle
|
|
15
|
+
*/
|
|
16
|
+
export interface ISerializedHandle {
|
|
17
|
+
// Marker to indicate to JSON.parse that the object is a Fluid handle
|
|
18
|
+
type: "__fluid_handle__";
|
|
19
|
+
|
|
20
|
+
// URL to the object. Relative URLs are relative to the handle context passed to the stringify.
|
|
21
|
+
url: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const isSerializedHandle = (value: any): value is ISerializedHandle =>
|
|
25
|
+
value?.type === "__fluid_handle__";
|
|
26
|
+
|
|
27
|
+
export interface IFluidSerializer {
|
|
28
|
+
/**
|
|
29
|
+
* Given a mostly-plain object that may have handle objects embedded within, will return a fully-plain object
|
|
30
|
+
* where any embedded IFluidHandles have been replaced with a serializable form.
|
|
31
|
+
*
|
|
32
|
+
* The original `input` object is not mutated. This method will shallowly clones all objects in the path from
|
|
33
|
+
* the root to any replaced handles. (If no handles are found, returns the original object.)
|
|
34
|
+
*/
|
|
35
|
+
encode(value: any, bind: IFluidHandle): any;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Given a fully-jsonable object tree that may have encoded handle objects embedded within, will return an
|
|
39
|
+
* equivalent object tree where any encoded IFluidHandles have been replaced with their decoded form.
|
|
40
|
+
*
|
|
41
|
+
* The original `input` object is not mutated. This method will shallowly clone all objects in the path from
|
|
42
|
+
* the root to any replaced handles. (If no handles are found, returns the original object.)
|
|
43
|
+
*
|
|
44
|
+
* The decoded handles are implicitly bound to the handle context of this serializer.
|
|
45
|
+
*/
|
|
46
|
+
decode(input: any): any;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Stringifies a given value. Converts any IFluidHandle to its stringified equivalent.
|
|
50
|
+
*/
|
|
51
|
+
stringify(value: any, bind: IFluidHandle): string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Parses the given JSON input string and returns the JavaScript object defined by it. Any Fluid
|
|
55
|
+
* handles will be realized as part of the parse
|
|
56
|
+
*/
|
|
57
|
+
parse(value: string): any;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Data Store serializer implementation
|
|
62
|
+
*/
|
|
63
|
+
export class FluidSerializer implements IFluidSerializer {
|
|
64
|
+
private readonly root: IFluidHandleContext;
|
|
65
|
+
|
|
66
|
+
public constructor(
|
|
67
|
+
private readonly context: IFluidHandleContext,
|
|
68
|
+
// To be called whenever a handle is parsed by this serializer.
|
|
69
|
+
private readonly handleParsedCb: (handle: IFluidHandle) => void,
|
|
70
|
+
) {
|
|
71
|
+
this.root = this.context;
|
|
72
|
+
while (this.root.routeContext !== undefined) {
|
|
73
|
+
this.root = this.root.routeContext;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public get IFluidSerializer() { return this; }
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Given a mostly-jsonable object tree that may have handle objects embedded within, will return a
|
|
81
|
+
* fully-jsonable object tree where any embedded IFluidHandles have been replaced with a serializable form.
|
|
82
|
+
*
|
|
83
|
+
* The original `input` object is not mutated. This method will shallowly clone all objects in the path from
|
|
84
|
+
* the root to any replaced handles. (If no handles are found, returns the original object.)
|
|
85
|
+
*
|
|
86
|
+
* Any unbound handles encountered are bound to the provided IFluidHandle.
|
|
87
|
+
*/
|
|
88
|
+
public encode(
|
|
89
|
+
input: any,
|
|
90
|
+
bind: IFluidHandle,
|
|
91
|
+
) {
|
|
92
|
+
// If the given 'input' cannot contain handles, return it immediately. Otherwise,
|
|
93
|
+
// return the result of 'recursivelyReplace()'.
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
95
|
+
return !!input && typeof input === "object"
|
|
96
|
+
? this.recursivelyReplace(input, this.encodeValue, bind)
|
|
97
|
+
: input;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Given a fully-jsonable object tree that may have encoded handle objects embedded within, will return an
|
|
102
|
+
* equivalent object tree where any encoded IFluidHandles have been replaced with their decoded form.
|
|
103
|
+
*
|
|
104
|
+
* The original `input` object is not mutated. This method will shallowly clone all objects in the path from
|
|
105
|
+
* the root to any replaced handles. (If no handles are found, returns the original object.)
|
|
106
|
+
*
|
|
107
|
+
* The decoded handles are implicitly bound to the handle context of this serializer.
|
|
108
|
+
*/
|
|
109
|
+
public decode(input: any) {
|
|
110
|
+
// If the given 'input' cannot contain handles, return it immediately. Otherwise,
|
|
111
|
+
// return the result of 'recursivelyReplace()'.
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
113
|
+
return !!input && typeof input === "object"
|
|
114
|
+
? this.recursivelyReplace(input, this.decodeValue)
|
|
115
|
+
: input;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public stringify(input: any, bind: IFluidHandle) {
|
|
119
|
+
return JSON.stringify(input, (key, value) => this.encodeValue(value, bind));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Parses the serialized data - context must match the context with which the JSON was stringified
|
|
123
|
+
public parse(input: string) {
|
|
124
|
+
return JSON.parse(input, (key, value) => this.decodeValue(value));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// If the given 'value' is an IFluidHandle, returns the encoded IFluidHandle.
|
|
128
|
+
// Otherwise returns the original 'value'. Used by 'encode()' and 'stringify()'.
|
|
129
|
+
private readonly encodeValue = (value: any, bind: IFluidHandle) => {
|
|
130
|
+
// Detect if 'value' is an IFluidHandle.
|
|
131
|
+
const handle = value?.IFluidHandle;
|
|
132
|
+
|
|
133
|
+
// If 'value' is an IFluidHandle return its encoded form.
|
|
134
|
+
return handle !== undefined
|
|
135
|
+
? this.serializeHandle(handle, bind)
|
|
136
|
+
: value;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// If the given 'value' is an encoded IFluidHandle, returns the decoded IFluidHandle.
|
|
140
|
+
// Otherwise returns the original 'value'. Used by 'decode()' and 'parse()'.
|
|
141
|
+
private readonly decodeValue = (value: any) => {
|
|
142
|
+
// If 'value' is a serialized IFluidHandle return the deserialized result.
|
|
143
|
+
if (isSerializedHandle(value)) {
|
|
144
|
+
// Old documents may have handles with relative path in their summaries. Convert these to absolute
|
|
145
|
+
// paths. This will ensure that future summaries will have absolute paths for these handles.
|
|
146
|
+
const absolutePath = value.url.startsWith("/")
|
|
147
|
+
? value.url
|
|
148
|
+
: generateHandleContextPath(value.url, this.context);
|
|
149
|
+
|
|
150
|
+
const parsedHandle = new RemoteFluidObjectHandle(absolutePath, this.root);
|
|
151
|
+
this.handleParsedCb(parsedHandle);
|
|
152
|
+
return parsedHandle;
|
|
153
|
+
} else {
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Invoked for non-null objects to recursively replace references to IFluidHandles.
|
|
159
|
+
// Clones as-needed to avoid mutating the `input` object. If no IFluidHandes are present,
|
|
160
|
+
// returns the original `input`.
|
|
161
|
+
private recursivelyReplace(
|
|
162
|
+
input: any,
|
|
163
|
+
replacer: (input: any, context: any) => any,
|
|
164
|
+
context?: any,
|
|
165
|
+
) {
|
|
166
|
+
// Note: Caller is responsible for ensuring that `input` is defined / non-null.
|
|
167
|
+
// (Required for Object.keys() below.)
|
|
168
|
+
|
|
169
|
+
// Execute the `replace` on the current input. Note that Caller is responsible for ensuring that `input`
|
|
170
|
+
// is a non-null object.
|
|
171
|
+
const maybeReplaced = replacer(input, context);
|
|
172
|
+
|
|
173
|
+
// If the replacer made a substitution there is no need to decscend further. IFluidHandles are always
|
|
174
|
+
// leaves in the object graph.
|
|
175
|
+
if (maybeReplaced !== input) {
|
|
176
|
+
return maybeReplaced;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Otherwise descend into the object graph looking for IFluidHandle instances.
|
|
180
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
181
|
+
let clone: object | undefined;
|
|
182
|
+
for (const key of Object.keys(input)) {
|
|
183
|
+
const value = input[key];
|
|
184
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
185
|
+
if (!!value && typeof value === "object") {
|
|
186
|
+
// Note: Except for IFluidHandle, `input` must not contain circular references (as object must
|
|
187
|
+
// be JSON serializable.) Therefore, guarding against infinite recursion here would only
|
|
188
|
+
// lead to a later error when attempting to stringify().
|
|
189
|
+
const replaced = this.recursivelyReplace(value, replacer, context);
|
|
190
|
+
|
|
191
|
+
// If the `replaced` object is different than the original `value` then the subgraph contained one
|
|
192
|
+
// or more handles. If this happens, we need to return a clone of the `input` object where the
|
|
193
|
+
// current property is replaced by the `replaced` value.
|
|
194
|
+
if (replaced !== value) {
|
|
195
|
+
// Lazily create a shallow clone of the `input` object if we haven't done so already.
|
|
196
|
+
clone = clone ?? (Array.isArray(input)
|
|
197
|
+
? [...input]
|
|
198
|
+
: { ...input });
|
|
199
|
+
|
|
200
|
+
// Overwrite the current property `key` in the clone with the `replaced` value.
|
|
201
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
202
|
+
clone![key] = replaced;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return clone ?? input;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
protected serializeHandle(handle: IFluidHandle, bind: IFluidHandle) {
|
|
210
|
+
bind.bind(handle);
|
|
211
|
+
return {
|
|
212
|
+
type: "__fluid_handle__",
|
|
213
|
+
url: handle.absolutePath,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|