@fluid-experimental/attributor 2.0.0-internal.3.3.0

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.
Files changed (63) hide show
  1. package/.eslintrc.js +20 -0
  2. package/.mocharc.js +12 -0
  3. package/LICENSE +21 -0
  4. package/README.md +106 -0
  5. package/api-extractor.json +4 -0
  6. package/dist/attributor.d.ts +56 -0
  7. package/dist/attributor.d.ts.map +1 -0
  8. package/dist/attributor.js +67 -0
  9. package/dist/attributor.js.map +1 -0
  10. package/dist/encoders.d.ts +28 -0
  11. package/dist/encoders.d.ts.map +1 -0
  12. package/dist/encoders.js +80 -0
  13. package/dist/encoders.js.map +1 -0
  14. package/dist/index.d.ts +7 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +16 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/lz4Encoder.d.ts +7 -0
  19. package/dist/lz4Encoder.d.ts.map +1 -0
  20. package/dist/lz4Encoder.js +29 -0
  21. package/dist/lz4Encoder.js.map +1 -0
  22. package/dist/mixinAttributor.d.ts +60 -0
  23. package/dist/mixinAttributor.d.ts.map +1 -0
  24. package/dist/mixinAttributor.js +153 -0
  25. package/dist/mixinAttributor.js.map +1 -0
  26. package/dist/stringInterner.d.ts +55 -0
  27. package/dist/stringInterner.d.ts.map +1 -0
  28. package/dist/stringInterner.js +66 -0
  29. package/dist/stringInterner.js.map +1 -0
  30. package/lib/attributor.d.ts +56 -0
  31. package/lib/attributor.d.ts.map +1 -0
  32. package/lib/attributor.js +62 -0
  33. package/lib/attributor.js.map +1 -0
  34. package/lib/encoders.d.ts +28 -0
  35. package/lib/encoders.d.ts.map +1 -0
  36. package/lib/encoders.js +75 -0
  37. package/lib/encoders.js.map +1 -0
  38. package/lib/index.d.ts +7 -0
  39. package/lib/index.d.ts.map +1 -0
  40. package/lib/index.js +7 -0
  41. package/lib/index.js.map +1 -0
  42. package/lib/lz4Encoder.d.ts +7 -0
  43. package/lib/lz4Encoder.d.ts.map +1 -0
  44. package/lib/lz4Encoder.js +25 -0
  45. package/lib/lz4Encoder.js.map +1 -0
  46. package/lib/mixinAttributor.d.ts +60 -0
  47. package/lib/mixinAttributor.d.ts.map +1 -0
  48. package/lib/mixinAttributor.js +148 -0
  49. package/lib/mixinAttributor.js.map +1 -0
  50. package/lib/stringInterner.d.ts +55 -0
  51. package/lib/stringInterner.d.ts.map +1 -0
  52. package/lib/stringInterner.js +62 -0
  53. package/lib/stringInterner.js.map +1 -0
  54. package/package.json +106 -0
  55. package/prettier.config.cjs +8 -0
  56. package/src/attributor.ts +105 -0
  57. package/src/encoders.ts +112 -0
  58. package/src/index.ts +12 -0
  59. package/src/lz4Encoder.ts +27 -0
  60. package/src/mixinAttributor.ts +283 -0
  61. package/src/stringInterner.ts +86 -0
  62. package/tsconfig.esnext.json +7 -0
  63. package/tsconfig.json +10 -0
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mixinAttributor = exports.createRuntimeAttributor = exports.IRuntimeAttributor = exports.enableOnNewFileKey = void 0;
4
+ const container_runtime_1 = require("@fluidframework/container-runtime");
5
+ const runtime_utils_1 = require("@fluidframework/runtime-utils");
6
+ const common_utils_1 = require("@fluidframework/common-utils");
7
+ const container_utils_1 = require("@fluidframework/container-utils");
8
+ const telemetry_utils_1 = require("@fluidframework/telemetry-utils");
9
+ const attributor_1 = require("./attributor");
10
+ const encoders_1 = require("./encoders");
11
+ const lz4Encoder_1 = require("./lz4Encoder");
12
+ // Summary tree keys
13
+ const attributorTreeName = ".attributor";
14
+ const opBlobName = "op";
15
+ /**
16
+ * @alpha
17
+ * Feature Gate Key -
18
+ * Whether or not a container runtime instantiated using `mixinAttributor`'s load should generate an attributor on
19
+ * new files. See package README for more notes on integration.
20
+ */
21
+ exports.enableOnNewFileKey = "Fluid.Attribution.EnableOnNewFile";
22
+ /**
23
+ * @alpha
24
+ */
25
+ exports.IRuntimeAttributor = "IRuntimeAttributor";
26
+ /**
27
+ * @returns an IRuntimeAttributor for usage with `mixinAttributor`. The attributor will only be populated with data
28
+ * once it's passed via scope to a container runtime load flow. See {@link mixinAttributor}.
29
+ * @alpha
30
+ */
31
+ function createRuntimeAttributor() {
32
+ return new RuntimeAttributor();
33
+ }
34
+ exports.createRuntimeAttributor = createRuntimeAttributor;
35
+ /**
36
+ * Mixes in logic to load and store runtime-based attribution functionality.
37
+ *
38
+ * The `scope` passed to `load` should implement `IProvideRuntimeAttributor`.
39
+ *
40
+ * Existing documents without stored attributors will not start storing attribution information: if an
41
+ * IRuntimeAttributor is passed via scope to load a document that never previously had attribution information,
42
+ * that attributor's `has` method will always return `false`.
43
+ * @param Base - base class, inherits from FluidAttributorRuntime
44
+ * @alpha
45
+ */
46
+ const mixinAttributor = (Base = container_runtime_1.ContainerRuntime) => class ContainerRuntimeWithAttributor extends Base {
47
+ static async load(context, registryEntries, requestHandler, runtimeOptions = {}, containerScope = context.scope, existing, ctor = ContainerRuntimeWithAttributor) {
48
+ var _a, _b, _c, _d;
49
+ var _e;
50
+ const runtimeAttributor = (_a = containerScope) === null || _a === void 0 ? void 0 : _a.IRuntimeAttributor;
51
+ if (!runtimeAttributor) {
52
+ throw new container_utils_1.UsageError("ContainerRuntimeWithAttributor must be passed a scope implementing IProvideRuntimeAttributor");
53
+ }
54
+ const pendingRuntimeState = context.pendingLocalState;
55
+ const baseSnapshot = (_b = pendingRuntimeState === null || pendingRuntimeState === void 0 ? void 0 : pendingRuntimeState.baseSnapshot) !== null && _b !== void 0 ? _b : context.baseSnapshot;
56
+ const { audience, deltaManager } = context;
57
+ (0, common_utils_1.assert)(audience !== undefined, 0x508 /* Audience must exist when instantiating attribution-providing runtime */);
58
+ const mc = (0, telemetry_utils_1.loggerToMonitoringContext)(context.taggedLogger);
59
+ const shouldTrackAttribution = (_c = mc.config.getBoolean(exports.enableOnNewFileKey)) !== null && _c !== void 0 ? _c : false;
60
+ if (shouldTrackAttribution) {
61
+ ((_d = (_e = context.options).attribution) !== null && _d !== void 0 ? _d : (_e.attribution = {})).track = true;
62
+ }
63
+ const runtime = (await Base.load(context, registryEntries, requestHandler, runtimeOptions, containerScope, existing, ctor));
64
+ runtime.runtimeAttributor = runtimeAttributor;
65
+ // Note: this fetches attribution blobs relatively eagerly in the load flow; we may want to optimize
66
+ // this to avoid blocking on such information until application actually requests some op-based attribution
67
+ // info or we need to summarize. All that really needs to happen immediately is to start recording
68
+ // op seq# -> attributionInfo for new ops.
69
+ await runtime.runtimeAttributor.initialize(deltaManager, audience, baseSnapshot, async (id) => runtime.storage.readBlob(id), shouldTrackAttribution);
70
+ return runtime;
71
+ }
72
+ addContainerStateToSummary(summaryTree, fullTree, trackState, telemetryContext) {
73
+ var _a;
74
+ super.addContainerStateToSummary(summaryTree, fullTree, trackState, telemetryContext);
75
+ const attributorSummary = (_a = this.runtimeAttributor) === null || _a === void 0 ? void 0 : _a.summarize();
76
+ if (attributorSummary) {
77
+ (0, runtime_utils_1.addSummarizeResultToSummary)(summaryTree, attributorTreeName, attributorSummary);
78
+ }
79
+ }
80
+ };
81
+ exports.mixinAttributor = mixinAttributor;
82
+ class RuntimeAttributor {
83
+ constructor() {
84
+ this.encoder = {
85
+ encode: common_utils_1.unreachableCase,
86
+ decode: common_utils_1.unreachableCase,
87
+ };
88
+ this.isEnabled = false;
89
+ }
90
+ get IRuntimeAttributor() {
91
+ return this;
92
+ }
93
+ get(key) {
94
+ (0, common_utils_1.assert)(this.opAttributor !== undefined, 0x509 /* RuntimeAttributor must be initialized before getAttributionInfo can be called */);
95
+ if (key.type === "detached") {
96
+ throw new Error("Attribution of detached keys is not yet supported.");
97
+ }
98
+ if (key.type === "local") {
99
+ // Note: we can *almost* orchestrate this correctly with internal-only changes by looking up the current
100
+ // client id in the audience. However, for read->write client transition, the container might have not yet
101
+ // received a client id. This is left as a TODO as it might be more easily solved once the detached case
102
+ // is settled (e.g. if it's reasonable for the host to know the current user information at container
103
+ // creation time, we could just use that here as well).
104
+ throw new Error("Attribution of local keys is not yet supported.");
105
+ }
106
+ return this.opAttributor.getAttributionInfo(key.seq);
107
+ }
108
+ has(key) {
109
+ var _a;
110
+ if (key.type === "detached") {
111
+ return false;
112
+ }
113
+ if (key.type === "local") {
114
+ return false;
115
+ }
116
+ return ((_a = this.opAttributor) === null || _a === void 0 ? void 0 : _a.tryGetAttributionInfo(key.seq)) !== undefined;
117
+ }
118
+ async initialize(deltaManager, audience, baseSnapshot, readBlob, shouldAddAttributorOnNewFile) {
119
+ const attributorTree = baseSnapshot === null || baseSnapshot === void 0 ? void 0 : baseSnapshot.trees[attributorTreeName];
120
+ // Existing documents that don't already have a snapshot containing runtime attribution info shouldn't
121
+ // inject any for now--this causes some back-compat integration problems that aren't fully worked out.
122
+ const shouldExcludeAttributor = (baseSnapshot !== undefined && attributorTree === undefined) ||
123
+ (baseSnapshot === undefined && !shouldAddAttributorOnNewFile);
124
+ if (shouldExcludeAttributor) {
125
+ // This gives a consistent error for calls to `get` on keys that don't exist.
126
+ this.opAttributor = new attributor_1.Attributor();
127
+ return;
128
+ }
129
+ this.isEnabled = true;
130
+ this.encoder = (0, encoders_1.chain)(new encoders_1.AttributorSerializer((entries) => new attributor_1.OpStreamAttributor(deltaManager, audience, entries), encoders_1.deltaEncoder), (0, lz4Encoder_1.makeLZ4Encoder)());
131
+ if (attributorTree !== undefined) {
132
+ const id = attributorTree.blobs[opBlobName];
133
+ (0, common_utils_1.assert)(id !== undefined, 0x50a /* Attributor tree should have op attributor summary blob. */);
134
+ const blobContents = await readBlob(id);
135
+ const attributorSnapshot = (0, common_utils_1.bufferToString)(blobContents, "utf8");
136
+ this.opAttributor = this.encoder.decode(attributorSnapshot);
137
+ }
138
+ else {
139
+ this.opAttributor = new attributor_1.OpStreamAttributor(deltaManager, audience);
140
+ }
141
+ }
142
+ summarize() {
143
+ if (!this.isEnabled) {
144
+ // Loaded existing document without attributor data: avoid injecting any data.
145
+ return undefined;
146
+ }
147
+ (0, common_utils_1.assert)(this.opAttributor !== undefined, 0x50b /* RuntimeAttributor should be initialized before summarization */);
148
+ const builder = new runtime_utils_1.SummaryTreeBuilder();
149
+ builder.addBlob(opBlobName, this.encoder.encode(this.opAttributor));
150
+ return builder.getSummaryTree();
151
+ }
152
+ }
153
+ //# sourceMappingURL=mixinAttributor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mixinAttributor.js","sourceRoot":"","sources":["../src/mixinAttributor.ts"],"names":[],"mappings":";;;AAUA,yEAAqE;AASrE,iEAAgG;AAGhG,+DAAuF;AACvF,qEAA6D;AAC7D,qEAA4E;AAC5E,6CAA2E;AAC3E,yCAAgF;AAChF,6CAA8C;AAE9C,oBAAoB;AACpB,MAAM,kBAAkB,GAAG,aAAa,CAAC;AACzC,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB;;;;;GAKG;AACU,QAAA,kBAAkB,GAAG,mCAAmC,CAAC;AAEtE;;GAEG;AACU,QAAA,kBAAkB,GAAoC,oBAAoB,CAAC;AAkCxF;;;;GAIG;AACH,SAAgB,uBAAuB;IACtC,OAAO,IAAI,iBAAiB,EAAE,CAAC;AAChC,CAAC;AAFD,0DAEC;AAED;;;;;;;;;;GAUG;AACI,MAAM,eAAe,GAAG,CAAC,OAAgC,oCAAgB,EAAE,EAAE,CACnF,MAAM,8BAA+B,SAAQ,IAAI;IACzC,MAAM,CAAC,KAAK,CAAC,IAAI,CACvB,OAA0B,EAC1B,eAAmD,EACnD,cAEY,EACZ,iBAAuD,EAAE,EACzD,iBAA0C,OAAO,CAAC,KAAK,EACvD,QAA8B,EAC9B,OAAgC,8BAAoE;;;QAEpG,MAAM,iBAAiB,GAAG,MACzB,cACA,0CAAE,kBAAkB,CAAC;QACtB,IAAI,CAAC,iBAAiB,EAAE;YACvB,MAAM,IAAI,4BAAU,CACnB,8FAA8F,CAC9F,CAAC;SACF;QAED,MAAM,mBAAmB,GAAG,OAAO,CAAC,iBAEnC,CAAC;QACF,MAAM,YAAY,GACjB,MAA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AAC;IAEM,SAAS;QACf,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACpB,8EAA8E;YAC9E,OAAO,SAAS,CAAC;SACjB;QAED,IAAA,qBAAM,EACL,IAAI,CAAC,YAAY,KAAK,SAAS,EAC/B,KAAK,CAAC,kEAAkE,CACxE,CAAC;QACF,MAAM,OAAO,GAAG,IAAI,kCAAkB,EAAE,CAAC;QACzC,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;QACpE,OAAO,OAAO,CAAC,cAAc,EAAE,CAAC;IACjC,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\nimport {\n\tIDocumentMessage,\n\tISequencedDocumentMessage,\n\tISnapshotTree,\n} from \"@fluidframework/protocol-definitions\";\nimport { IAudience, IContainerContext, IDeltaManager } from \"@fluidframework/container-definitions\";\nimport { ContainerRuntime } from \"@fluidframework/container-runtime\";\nimport type { IContainerRuntimeOptions } from \"@fluidframework/container-runtime\";\nimport {\n\tAttributionInfo,\n\tAttributionKey,\n\tISummaryTreeWithStats,\n\tITelemetryContext,\n\tNamedFluidDataStoreRegistryEntries,\n} from \"@fluidframework/runtime-definitions\";\nimport { addSummarizeResultToSummary, SummaryTreeBuilder } from \"@fluidframework/runtime-utils\";\nimport { IContainerRuntime } from \"@fluidframework/container-runtime-definitions\";\nimport { IRequest, IResponse, FluidObject } from \"@fluidframework/core-interfaces\";\nimport { assert, bufferToString, unreachableCase } from \"@fluidframework/common-utils\";\nimport { UsageError } from \"@fluidframework/container-utils\";\nimport { loggerToMonitoringContext } from \"@fluidframework/telemetry-utils\";\nimport { Attributor, IAttributor, OpStreamAttributor } from \"./attributor\";\nimport { AttributorSerializer, chain, deltaEncoder, Encoder } from \"./encoders\";\nimport { makeLZ4Encoder } from \"./lz4Encoder\";\n\n// Summary tree keys\nconst attributorTreeName = \".attributor\";\nconst opBlobName = \"op\";\n\n/**\n * @alpha\n * Feature Gate Key -\n * Whether or not a container runtime instantiated using `mixinAttributor`'s load should generate an attributor on\n * new files. See package README for more notes on integration.\n */\nexport const enableOnNewFileKey = \"Fluid.Attribution.EnableOnNewFile\";\n\n/**\n * @alpha\n */\nexport const IRuntimeAttributor: keyof IProvideRuntimeAttributor = \"IRuntimeAttributor\";\n\n/**\n * @alpha\n */\nexport interface IProvideRuntimeAttributor {\n\treadonly IRuntimeAttributor: IRuntimeAttributor;\n}\n\n/**\n * Provides access to attribution information stored on the container runtime.\n *\n * Attributors are only populated after the container runtime they are injected into has initialized.\n * @sealed\n * @alpha\n */\nexport interface IRuntimeAttributor extends IProvideRuntimeAttributor {\n\t/**\n\t * @throws - If no AttributionInfo exists for this key.\n\t */\n\tget(key: AttributionKey): AttributionInfo;\n\n\t/**\n\t * @returns - Whether any AttributionInfo exists for the provided key.\n\t */\n\thas(key: AttributionKey): boolean;\n\n\t/**\n\t * @returns - Whether the runtime is currently tracking attribution information for the loaded container.\n\t * See {@link mixinAttributor} for more details on when this happens.\n\t */\n\treadonly isEnabled: boolean;\n}\n\n/**\n * @returns an IRuntimeAttributor for usage with `mixinAttributor`. The attributor will only be populated with data\n * once it's passed via scope to a container runtime load flow. See {@link mixinAttributor}.\n * @alpha\n */\nexport function createRuntimeAttributor(): IRuntimeAttributor {\n\treturn new RuntimeAttributor();\n}\n\n/**\n * Mixes in logic to load and store runtime-based attribution functionality.\n *\n * The `scope` passed to `load` should implement `IProvideRuntimeAttributor`.\n *\n * Existing documents without stored attributors will not start storing attribution information: if an\n * IRuntimeAttributor is passed via scope to load a document that never previously had attribution information,\n * that attributor's `has` method will always return `false`.\n * @param Base - base class, inherits from FluidAttributorRuntime\n * @alpha\n */\nexport const mixinAttributor = (Base: typeof ContainerRuntime = ContainerRuntime) =>\n\tclass ContainerRuntimeWithAttributor extends Base {\n\t\tpublic static async load(\n\t\t\tcontext: IContainerContext,\n\t\t\tregistryEntries: NamedFluidDataStoreRegistryEntries,\n\t\t\trequestHandler?:\n\t\t\t\t| ((request: IRequest, runtime: IContainerRuntime) => Promise<IResponse>)\n\t\t\t\t| undefined,\n\t\t\truntimeOptions: IContainerRuntimeOptions | undefined = {},\n\t\t\tcontainerScope: FluidObject | undefined = context.scope,\n\t\t\texisting?: boolean | undefined,\n\t\t\tctor: typeof ContainerRuntime = ContainerRuntimeWithAttributor as unknown as typeof ContainerRuntime,\n\t\t): Promise<ContainerRuntime> {\n\t\t\tconst runtimeAttributor = (\n\t\t\t\tcontainerScope as FluidObject<IProvideRuntimeAttributor> | undefined\n\t\t\t)?.IRuntimeAttributor;\n\t\t\tif (!runtimeAttributor) {\n\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\"ContainerRuntimeWithAttributor must be passed a scope implementing IProvideRuntimeAttributor\",\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst pendingRuntimeState = context.pendingLocalState as {\n\t\t\t\tbaseSnapshot?: ISnapshotTree;\n\t\t\t};\n\t\t\tconst baseSnapshot: ISnapshotTree | undefined =\n\t\t\t\tpendingRuntimeState?.baseSnapshot ?? context.baseSnapshot;\n\n\t\t\tconst { audience, deltaManager } = context;\n\t\t\tassert(\n\t\t\t\taudience !== undefined,\n\t\t\t\t0x508 /* Audience must exist when instantiating attribution-providing runtime */,\n\t\t\t);\n\n\t\t\tconst mc = loggerToMonitoringContext(context.taggedLogger);\n\t\t\tconst shouldTrackAttribution = mc.config.getBoolean(enableOnNewFileKey) ?? false;\n\t\t\tif (shouldTrackAttribution) {\n\t\t\t\t(context.options.attribution ??= {}).track = true;\n\t\t\t}\n\n\t\t\tconst runtime = (await Base.load(\n\t\t\t\tcontext,\n\t\t\t\tregistryEntries,\n\t\t\t\trequestHandler,\n\t\t\t\truntimeOptions,\n\t\t\t\tcontainerScope,\n\t\t\t\texisting,\n\t\t\t\tctor,\n\t\t\t)) as ContainerRuntimeWithAttributor;\n\t\t\truntime.runtimeAttributor = runtimeAttributor as RuntimeAttributor;\n\n\t\t\t// Note: this fetches attribution blobs relatively eagerly in the load flow; we may want to optimize\n\t\t\t// this to avoid blocking on such information until application actually requests some op-based attribution\n\t\t\t// info or we need to summarize. All that really needs to happen immediately is to start recording\n\t\t\t// op seq# -> attributionInfo for new ops.\n\t\t\tawait runtime.runtimeAttributor.initialize(\n\t\t\t\tdeltaManager,\n\t\t\t\taudience,\n\t\t\t\tbaseSnapshot,\n\t\t\t\tasync (id) => runtime.storage.readBlob(id),\n\t\t\t\tshouldTrackAttribution,\n\t\t\t);\n\t\t\treturn runtime;\n\t\t}\n\n\t\tprivate runtimeAttributor: RuntimeAttributor | undefined;\n\n\t\tprotected addContainerStateToSummary(\n\t\t\tsummaryTree: ISummaryTreeWithStats,\n\t\t\tfullTree: boolean,\n\t\t\ttrackState: boolean,\n\t\t\ttelemetryContext?: ITelemetryContext,\n\t\t) {\n\t\t\tsuper.addContainerStateToSummary(summaryTree, fullTree, trackState, telemetryContext);\n\t\t\tconst attributorSummary = this.runtimeAttributor?.summarize();\n\t\t\tif (attributorSummary) {\n\t\t\t\taddSummarizeResultToSummary(summaryTree, attributorTreeName, attributorSummary);\n\t\t\t}\n\t\t}\n\t} as unknown as typeof ContainerRuntime;\n\nclass RuntimeAttributor implements IRuntimeAttributor {\n\tpublic get IRuntimeAttributor(): IRuntimeAttributor {\n\t\treturn this;\n\t}\n\n\tpublic get(key: AttributionKey): AttributionInfo {\n\t\tassert(\n\t\t\tthis.opAttributor !== undefined,\n\t\t\t0x509 /* RuntimeAttributor must be initialized before getAttributionInfo can be called */,\n\t\t);\n\n\t\tif (key.type === \"detached\") {\n\t\t\tthrow new Error(\"Attribution of detached keys is not yet supported.\");\n\t\t}\n\n\t\tif (key.type === \"local\") {\n\t\t\t// Note: we can *almost* orchestrate this correctly with internal-only changes by looking up the current\n\t\t\t// client id in the audience. However, for read->write client transition, the container might have not yet\n\t\t\t// received a client id. This is left as a TODO as it might be more easily solved once the detached case\n\t\t\t// is settled (e.g. if it's reasonable for the host to know the current user information at container\n\t\t\t// creation time, we could just use that here as well).\n\t\t\tthrow new Error(\"Attribution of local keys is not yet supported.\");\n\t\t}\n\n\t\treturn this.opAttributor.getAttributionInfo(key.seq);\n\t}\n\n\tpublic has(key: AttributionKey): boolean {\n\t\tif (key.type === \"detached\") {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (key.type === \"local\") {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn this.opAttributor?.tryGetAttributionInfo(key.seq) !== undefined;\n\t}\n\n\tprivate encoder: Encoder<IAttributor, string> = {\n\t\tencode: unreachableCase,\n\t\tdecode: unreachableCase,\n\t};\n\n\tprivate opAttributor: IAttributor | undefined;\n\tpublic isEnabled = false;\n\n\tpublic async initialize(\n\t\tdeltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,\n\t\taudience: IAudience,\n\t\tbaseSnapshot: ISnapshotTree | undefined,\n\t\treadBlob: (id: string) => Promise<ArrayBufferLike>,\n\t\tshouldAddAttributorOnNewFile: boolean,\n\t): Promise<void> {\n\t\tconst attributorTree = baseSnapshot?.trees[attributorTreeName];\n\t\t// Existing documents that don't already have a snapshot containing runtime attribution info shouldn't\n\t\t// inject any for now--this causes some back-compat integration problems that aren't fully worked out.\n\t\tconst shouldExcludeAttributor =\n\t\t\t(baseSnapshot !== undefined && attributorTree === undefined) ||\n\t\t\t(baseSnapshot === undefined && !shouldAddAttributorOnNewFile);\n\t\tif (shouldExcludeAttributor) {\n\t\t\t// This gives a consistent error for calls to `get` on keys that don't exist.\n\t\t\tthis.opAttributor = new Attributor();\n\t\t\treturn;\n\t\t}\n\n\t\tthis.isEnabled = true;\n\t\tthis.encoder = chain(\n\t\t\tnew AttributorSerializer(\n\t\t\t\t(entries) => new OpStreamAttributor(deltaManager, audience, entries),\n\t\t\t\tdeltaEncoder,\n\t\t\t),\n\t\t\tmakeLZ4Encoder(),\n\t\t);\n\n\t\tif (attributorTree !== undefined) {\n\t\t\tconst id = attributorTree.blobs[opBlobName];\n\t\t\tassert(\n\t\t\t\tid !== undefined,\n\t\t\t\t0x50a /* Attributor tree should have op attributor summary blob. */,\n\t\t\t);\n\t\t\tconst blobContents = await readBlob(id);\n\t\t\tconst attributorSnapshot = bufferToString(blobContents, \"utf8\");\n\t\t\tthis.opAttributor = this.encoder.decode(attributorSnapshot);\n\t\t} else {\n\t\t\tthis.opAttributor = new OpStreamAttributor(deltaManager, audience);\n\t\t}\n\t}\n\n\tpublic summarize(): ISummaryTreeWithStats | undefined {\n\t\tif (!this.isEnabled) {\n\t\t\t// Loaded existing document without attributor data: avoid injecting any data.\n\t\t\treturn undefined;\n\t\t}\n\n\t\tassert(\n\t\t\tthis.opAttributor !== undefined,\n\t\t\t0x50b /* RuntimeAttributor should be initialized before summarization */,\n\t\t);\n\t\tconst builder = new SummaryTreeBuilder();\n\t\tbuilder.addBlob(opBlobName, this.encoder.encode(this.opAttributor));\n\t\treturn builder.getSummaryTree();\n\t}\n}\n"]}
@@ -0,0 +1,55 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * The ID of the string that has been interned, which can be used by a {@link StringInterner} to retrieve the
7
+ * original string.
8
+ * @public
9
+ */
10
+ export declare type InternedStringId = number & {
11
+ readonly InternedStringId: "e221abc9-9d17-4493-8db0-70c871a1c27c";
12
+ };
13
+ /**
14
+ * Interns strings as integers.
15
+ */
16
+ export interface StringInterner {
17
+ getInternedId(input: string): InternedStringId | undefined;
18
+ getString(internedId: number): string;
19
+ getSerializable(): readonly string[];
20
+ }
21
+ /**
22
+ * Interns strings as integers.
23
+ * Given a string, this class will produce a unique integer associated with that string that can then be used to
24
+ * retrieve the string.
25
+ */
26
+ export declare class MutableStringInterner implements StringInterner {
27
+ private readonly stringToInternedIdMap;
28
+ private readonly internedStrings;
29
+ /**
30
+ * @param inputStrings - A list of strings to intern in the order given. Can be used to rehydrate from a previous
31
+ * `StringInterner`'s {@link StringInterner.getSerializable} return value.
32
+ */
33
+ constructor(inputStrings?: readonly string[]);
34
+ /**
35
+ * @param input - The string to get the associated intern ID for
36
+ * @returns an intern ID that is uniquely associated with the input string
37
+ */
38
+ getOrCreateInternedId(input: string): InternedStringId;
39
+ getInternedId(input: string): InternedStringId | undefined;
40
+ /**
41
+ *
42
+ * @param internId - The intern ID to get the associated string for. Can only retrieve strings that have been
43
+ * used as inputs to calls of `getInternId`.
44
+ * @returns a string that is uniquely associated with the given intern ID
45
+ */
46
+ getString(internId: number): string;
47
+ /**
48
+ * @returns The list of strings interned where the indices map to the associated {@link InternedStringId} of
49
+ * each string.
50
+ */
51
+ getSerializable(): readonly string[];
52
+ /** Create a new interned id. Assumes without validation that the input doesn't already have an interned id. */
53
+ private createNewId;
54
+ }
55
+ //# sourceMappingURL=stringInterner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringInterner.d.ts","sourceRoot":"","sources":["../src/stringInterner.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;;GAIG;AACH,oBAAY,gBAAgB,GAAG,MAAM,GAAG;IACvC,QAAQ,CAAC,gBAAgB,EAAE,sCAAsC,CAAC;CAClE,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,cAAc;IAC9B,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAC3D,SAAS,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,eAAe,IAAI,SAAS,MAAM,EAAE,CAAC;CACrC;AAED;;;;GAIG;AACH,qBAAa,qBAAsB,YAAW,cAAc;IAC3D,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAuC;IAC7E,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAgB;IAEhD;;;OAGG;gBACS,YAAY,GAAE,SAAS,MAAM,EAAO;IAMhD;;;OAGG;IACI,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB;IAItD,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAIjE;;;;;OAKG;IACI,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM;IAQ1C;;;OAGG;IACI,eAAe,IAAI,SAAS,MAAM,EAAE;IAI3C,+GAA+G;IAC/G,OAAO,CAAC,WAAW;CAMnB"}
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.MutableStringInterner = void 0;
8
+ const container_utils_1 = require("@fluidframework/container-utils");
9
+ /**
10
+ * Interns strings as integers.
11
+ * Given a string, this class will produce a unique integer associated with that string that can then be used to
12
+ * retrieve the string.
13
+ */
14
+ class MutableStringInterner {
15
+ /**
16
+ * @param inputStrings - A list of strings to intern in the order given. Can be used to rehydrate from a previous
17
+ * `StringInterner`'s {@link StringInterner.getSerializable} return value.
18
+ */
19
+ constructor(inputStrings = []) {
20
+ this.stringToInternedIdMap = new Map();
21
+ this.internedStrings = [];
22
+ for (const value of inputStrings) {
23
+ this.getOrCreateInternedId(value);
24
+ }
25
+ }
26
+ /**
27
+ * @param input - The string to get the associated intern ID for
28
+ * @returns an intern ID that is uniquely associated with the input string
29
+ */
30
+ getOrCreateInternedId(input) {
31
+ var _a;
32
+ return (_a = this.getInternedId(input)) !== null && _a !== void 0 ? _a : this.createNewId(input);
33
+ }
34
+ getInternedId(input) {
35
+ return this.stringToInternedIdMap.get(input);
36
+ }
37
+ /**
38
+ *
39
+ * @param internId - The intern ID to get the associated string for. Can only retrieve strings that have been
40
+ * used as inputs to calls of `getInternId`.
41
+ * @returns a string that is uniquely associated with the given intern ID
42
+ */
43
+ getString(internId) {
44
+ const result = this.internedStrings[internId];
45
+ if (!result) {
46
+ throw new container_utils_1.UsageError(`No string associated with ${internId}.`);
47
+ }
48
+ return result;
49
+ }
50
+ /**
51
+ * @returns The list of strings interned where the indices map to the associated {@link InternedStringId} of
52
+ * each string.
53
+ */
54
+ getSerializable() {
55
+ return this.internedStrings;
56
+ }
57
+ /** Create a new interned id. Assumes without validation that the input doesn't already have an interned id. */
58
+ createNewId(input) {
59
+ const internedId = this.stringToInternedIdMap.size;
60
+ this.stringToInternedIdMap.set(input, internedId);
61
+ this.internedStrings.push(input);
62
+ return internedId;
63
+ }
64
+ }
65
+ exports.MutableStringInterner = MutableStringInterner;
66
+ //# sourceMappingURL=stringInterner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringInterner.js","sourceRoot":"","sources":["../src/stringInterner.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,qEAA6D;AAoB7D;;;;GAIG;AACH,MAAa,qBAAqB;IAIjC;;;OAGG;IACH,YAAY,eAAkC,EAAE;QAP/B,0BAAqB,GAAG,IAAI,GAAG,EAA4B,CAAC;QAC5D,oBAAe,GAAa,EAAE,CAAC;QAO/C,KAAK,MAAM,KAAK,IAAI,YAAY,EAAE;YACjC,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAC;SAClC;IACF,CAAC;IAED;;;OAGG;IACI,qBAAqB,CAAC,KAAa;;QACzC,OAAO,MAAA,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,mCAAI,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAC7D,CAAC;IAEM,aAAa,CAAC,KAAa;QACjC,OAAO,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;OAKG;IACI,SAAS,CAAC,QAAgB;QAChC,MAAM,MAAM,GAAG,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;QAC9C,IAAI,CAAC,MAAM,EAAE;YACZ,MAAM,IAAI,4BAAU,CAAC,6BAA6B,QAAQ,GAAG,CAAC,CAAC;SAC/D;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;;OAGG;IACI,eAAe;QACrB,OAAO,IAAI,CAAC,eAAe,CAAC;IAC7B,CAAC;IAED,+GAA+G;IACvG,WAAW,CAAC,KAAa;QAChC,MAAM,UAAU,GAAG,IAAI,CAAC,qBAAqB,CAAC,IAAwB,CAAC;QACvE,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QAClD,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACjC,OAAO,UAAU,CAAC;IACnB,CAAC;CACD;AAvDD,sDAuDC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { UsageError } from \"@fluidframework/container-utils\";\n\n/**\n * The ID of the string that has been interned, which can be used by a {@link StringInterner} to retrieve the\n * original string.\n * @public\n */\nexport type InternedStringId = number & {\n\treadonly InternedStringId: \"e221abc9-9d17-4493-8db0-70c871a1c27c\";\n};\n\n/**\n * Interns strings as integers.\n */\nexport interface StringInterner {\n\tgetInternedId(input: string): InternedStringId | undefined;\n\tgetString(internedId: number): string;\n\tgetSerializable(): readonly string[];\n}\n\n/**\n * Interns strings as integers.\n * Given a string, this class will produce a unique integer associated with that string that can then be used to\n * retrieve the string.\n */\nexport class MutableStringInterner implements StringInterner {\n\tprivate readonly stringToInternedIdMap = new Map<string, InternedStringId>();\n\tprivate readonly internedStrings: string[] = [];\n\n\t/**\n\t * @param inputStrings - A list of strings to intern in the order given. Can be used to rehydrate from a previous\n\t * `StringInterner`'s {@link StringInterner.getSerializable} return value.\n\t */\n\tconstructor(inputStrings: readonly string[] = []) {\n\t\tfor (const value of inputStrings) {\n\t\t\tthis.getOrCreateInternedId(value);\n\t\t}\n\t}\n\n\t/**\n\t * @param input - The string to get the associated intern ID for\n\t * @returns an intern ID that is uniquely associated with the input string\n\t */\n\tpublic getOrCreateInternedId(input: string): InternedStringId {\n\t\treturn this.getInternedId(input) ?? this.createNewId(input);\n\t}\n\n\tpublic getInternedId(input: string): InternedStringId | undefined {\n\t\treturn this.stringToInternedIdMap.get(input);\n\t}\n\n\t/**\n\t *\n\t * @param internId - The intern ID to get the associated string for. Can only retrieve strings that have been\n\t * used as inputs to calls of `getInternId`.\n\t * @returns a string that is uniquely associated with the given intern ID\n\t */\n\tpublic getString(internId: number): string {\n\t\tconst result = this.internedStrings[internId];\n\t\tif (!result) {\n\t\t\tthrow new UsageError(`No string associated with ${internId}.`);\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * @returns The list of strings interned where the indices map to the associated {@link InternedStringId} of\n\t * each string.\n\t */\n\tpublic getSerializable(): readonly string[] {\n\t\treturn this.internedStrings;\n\t}\n\n\t/** Create a new interned id. Assumes without validation that the input doesn't already have an interned id. */\n\tprivate createNewId(input: string): InternedStringId {\n\t\tconst internedId = this.stringToInternedIdMap.size as InternedStringId;\n\t\tthis.stringToInternedIdMap.set(input, internedId);\n\t\tthis.internedStrings.push(input);\n\t\treturn internedId;\n\t}\n}\n"]}
@@ -0,0 +1,56 @@
1
+ import { IDocumentMessage, ISequencedDocumentMessage } from "@fluidframework/protocol-definitions";
2
+ import { AttributionInfo } from "@fluidframework/runtime-definitions";
3
+ import { IAudience, IDeltaManager } from "@fluidframework/container-definitions";
4
+ /**
5
+ * Provides lookup between attribution keys and their associated attribution information.
6
+ * @alpha
7
+ */
8
+ export interface IAttributor {
9
+ /**
10
+ * Retrieves attribution information associated with a particular key.
11
+ * @param key - Attribution key to look up.
12
+ * @throws If no attribution information is recorded for that key.
13
+ */
14
+ getAttributionInfo(key: number): AttributionInfo;
15
+ /**
16
+ * @param key - Attribution key to look up.
17
+ * @returns the attribution information associated with the provided key, or undefined if no information exists.
18
+ */
19
+ tryGetAttributionInfo(key: number): AttributionInfo | undefined;
20
+ /**
21
+ * @returns an iterable of (attribution key, attribution info) pairs for each stored key.
22
+ */
23
+ entries(): IterableIterator<[number, AttributionInfo]>;
24
+ }
25
+ /**
26
+ * {@inheritdoc IAttributor}
27
+ * @alpha
28
+ */
29
+ export declare class Attributor implements IAttributor {
30
+ protected readonly keyToInfo: Map<number, AttributionInfo>;
31
+ /**
32
+ * @param initialEntries - Any entries which should be populated on instantiation.
33
+ */
34
+ constructor(initialEntries?: Iterable<[number, AttributionInfo]>);
35
+ /**
36
+ * {@inheritdoc IAttributor.getAttributionInfo}
37
+ */
38
+ getAttributionInfo(key: number): AttributionInfo;
39
+ /**
40
+ * {@inheritdoc IAttributor.tryGetAttributionInfo}
41
+ */
42
+ tryGetAttributionInfo(key: number): AttributionInfo | undefined;
43
+ /**
44
+ * {@inheritdoc IAttributor.entries}
45
+ */
46
+ entries(): IterableIterator<[number, AttributionInfo]>;
47
+ }
48
+ /**
49
+ * Attributor which listens to an op stream and records entries for each op.
50
+ * Sequence numbers are used as attribution keys.
51
+ * @alpha
52
+ */
53
+ export declare class OpStreamAttributor extends Attributor implements IAttributor {
54
+ constructor(deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>, audience: IAudience, initialEntries?: Iterable<[number, AttributionInfo]>);
55
+ }
56
+ //# sourceMappingURL=attributor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributor.d.ts","sourceRoot":"","sources":["../src/attributor.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,sCAAsC,CAAC;AACnG,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAEtE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AAEjF;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC3B;;;;OAIG;IACH,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAAC;IAEjD;;;OAGG;IACH,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAAC;IAEhE;;OAEG;IACH,OAAO,IAAI,gBAAgB,CAAC,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC;CAIvD;AAED;;;GAGG;AACH,qBAAa,UAAW,YAAW,WAAW;IAC7C,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAE3D;;OAEG;gBACS,cAAc,CAAC,EAAE,QAAQ,CAAC,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAIhE;;OAEG;IACI,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe;IAQvD;;OAEG;IACI,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAItE;;OAEG;IACI,OAAO,IAAI,gBAAgB,CAAC,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CAG7D;AAED;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,UAAW,YAAW,WAAW;gBAEvE,YAAY,EAAE,aAAa,CAAC,yBAAyB,EAAE,gBAAgB,CAAC,EACxE,QAAQ,EAAE,SAAS,EACnB,cAAc,CAAC,EAAE,QAAQ,CAAC,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CAkBrD"}
@@ -0,0 +1,62 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { assert } from "@fluidframework/common-utils";
6
+ import { UsageError } from "@fluidframework/container-utils";
7
+ /**
8
+ * {@inheritdoc IAttributor}
9
+ * @alpha
10
+ */
11
+ export class Attributor {
12
+ /**
13
+ * @param initialEntries - Any entries which should be populated on instantiation.
14
+ */
15
+ constructor(initialEntries) {
16
+ this.keyToInfo = new Map(initialEntries !== null && initialEntries !== void 0 ? initialEntries : []);
17
+ }
18
+ /**
19
+ * {@inheritdoc IAttributor.getAttributionInfo}
20
+ */
21
+ getAttributionInfo(key) {
22
+ const result = this.tryGetAttributionInfo(key);
23
+ if (!result) {
24
+ throw new UsageError(`Requested attribution information for unstored key: ${key}.`);
25
+ }
26
+ return result;
27
+ }
28
+ /**
29
+ * {@inheritdoc IAttributor.tryGetAttributionInfo}
30
+ */
31
+ tryGetAttributionInfo(key) {
32
+ return this.keyToInfo.get(key);
33
+ }
34
+ /**
35
+ * {@inheritdoc IAttributor.entries}
36
+ */
37
+ entries() {
38
+ return this.keyToInfo.entries();
39
+ }
40
+ }
41
+ /**
42
+ * Attributor which listens to an op stream and records entries for each op.
43
+ * Sequence numbers are used as attribution keys.
44
+ * @alpha
45
+ */
46
+ export class OpStreamAttributor extends Attributor {
47
+ constructor(deltaManager, audience, initialEntries) {
48
+ super(initialEntries);
49
+ deltaManager.on("op", (message) => {
50
+ const client = audience.getMember(message.clientId);
51
+ if (message.type === "op") {
52
+ // TODO: This case may be legitimate, and if so we need to figure out how to handle it.
53
+ assert(client !== undefined, 0x4af /* Received message from user not in the audience */);
54
+ this.keyToInfo.set(message.sequenceNumber, {
55
+ user: client.user,
56
+ timestamp: message.timestamp,
57
+ });
58
+ }
59
+ });
60
+ }
61
+ }
62
+ //# sourceMappingURL=attributor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributor.js","sourceRoot":"","sources":["../src/attributor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAC;AAGtD,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AA8B7D;;;GAGG;AACH,MAAM,OAAO,UAAU;IAGtB;;OAEG;IACH,YAAY,cAAoD;QAC/D,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,CAAC,cAAc,aAAd,cAAc,cAAd,cAAc,GAAI,EAAE,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACI,kBAAkB,CAAC,GAAW;QACpC,MAAM,MAAM,GAAG,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,EAAE;YACZ,MAAM,IAAI,UAAU,CAAC,uDAAuD,GAAG,GAAG,CAAC,CAAC;SACpF;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;OAEG;IACI,qBAAqB,CAAC,GAAW;QACvC,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED;;OAEG;IACI,OAAO;QACb,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;IACjC,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,OAAO,kBAAmB,SAAQ,UAAU;IACjD,YACC,YAAwE,EACxE,QAAmB,EACnB,cAAoD;QAEpD,KAAK,CAAC,cAAc,CAAC,CAAC;QACtB,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,OAAkC,EAAE,EAAE;YAC5D,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YACpD,IAAI,OAAO,CAAC,IAAI,KAAK,IAAI,EAAE;gBAC1B,uFAAuF;gBACvF,MAAM,CACL,MAAM,KAAK,SAAS,EACpB,KAAK,CAAC,oDAAoD,CAC1D,CAAC;gBACF,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,cAAc,EAAE;oBAC1C,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,SAAS,EAAE,OAAO,CAAC,SAAS;iBAC5B,CAAC,CAAC;aACH;QACF,CAAC,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { assert } from \"@fluidframework/common-utils\";\nimport { IDocumentMessage, ISequencedDocumentMessage } from \"@fluidframework/protocol-definitions\";\nimport { AttributionInfo } from \"@fluidframework/runtime-definitions\";\nimport { UsageError } from \"@fluidframework/container-utils\";\nimport { IAudience, IDeltaManager } from \"@fluidframework/container-definitions\";\n\n/**\n * Provides lookup between attribution keys and their associated attribution information.\n * @alpha\n */\nexport interface IAttributor {\n\t/**\n\t * Retrieves attribution information associated with a particular key.\n\t * @param key - Attribution key to look up.\n\t * @throws If no attribution information is recorded for that key.\n\t */\n\tgetAttributionInfo(key: number): AttributionInfo;\n\n\t/**\n\t * @param key - Attribution key to look up.\n\t * @returns the attribution information associated with the provided key, or undefined if no information exists.\n\t */\n\ttryGetAttributionInfo(key: number): AttributionInfo | undefined;\n\n\t/**\n\t * @returns an iterable of (attribution key, attribution info) pairs for each stored key.\n\t */\n\tentries(): IterableIterator<[number, AttributionInfo]>;\n\n\t// TODO:\n\t// - GC\n}\n\n/**\n * {@inheritdoc IAttributor}\n * @alpha\n */\nexport class Attributor implements IAttributor {\n\tprotected readonly keyToInfo: Map<number, AttributionInfo>;\n\n\t/**\n\t * @param initialEntries - Any entries which should be populated on instantiation.\n\t */\n\tconstructor(initialEntries?: Iterable<[number, AttributionInfo]>) {\n\t\tthis.keyToInfo = new Map(initialEntries ?? []);\n\t}\n\n\t/**\n\t * {@inheritdoc IAttributor.getAttributionInfo}\n\t */\n\tpublic getAttributionInfo(key: number): AttributionInfo {\n\t\tconst result = this.tryGetAttributionInfo(key);\n\t\tif (!result) {\n\t\t\tthrow new UsageError(`Requested attribution information for unstored key: ${key}.`);\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * {@inheritdoc IAttributor.tryGetAttributionInfo}\n\t */\n\tpublic tryGetAttributionInfo(key: number): AttributionInfo | undefined {\n\t\treturn this.keyToInfo.get(key);\n\t}\n\n\t/**\n\t * {@inheritdoc IAttributor.entries}\n\t */\n\tpublic entries(): IterableIterator<[number, AttributionInfo]> {\n\t\treturn this.keyToInfo.entries();\n\t}\n}\n\n/**\n * Attributor which listens to an op stream and records entries for each op.\n * Sequence numbers are used as attribution keys.\n * @alpha\n */\nexport class OpStreamAttributor extends Attributor implements IAttributor {\n\tconstructor(\n\t\tdeltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,\n\t\taudience: IAudience,\n\t\tinitialEntries?: Iterable<[number, AttributionInfo]>,\n\t) {\n\t\tsuper(initialEntries);\n\t\tdeltaManager.on(\"op\", (message: ISequencedDocumentMessage) => {\n\t\t\tconst client = audience.getMember(message.clientId);\n\t\t\tif (message.type === \"op\") {\n\t\t\t\t// TODO: This case may be legitimate, and if so we need to figure out how to handle it.\n\t\t\t\tassert(\n\t\t\t\t\tclient !== undefined,\n\t\t\t\t\t0x4af /* Received message from user not in the audience */,\n\t\t\t\t);\n\t\t\t\tthis.keyToInfo.set(message.sequenceNumber, {\n\t\t\t\t\tuser: client.user,\n\t\t\t\t\ttimestamp: message.timestamp,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import { AttributionInfo } from "@fluidframework/runtime-definitions";
2
+ import { IAttributor } from "./attributor";
3
+ import { InternedStringId } from "./stringInterner";
4
+ export interface Encoder<TDecoded, TEncoded> {
5
+ encode(decoded: TDecoded): TEncoded;
6
+ decode(encoded: TEncoded): TDecoded;
7
+ }
8
+ export declare type TimestampEncoder = Encoder<number[], number[]>;
9
+ export declare const deltaEncoder: TimestampEncoder;
10
+ export declare type IAttributorSerializer = Encoder<IAttributor, SerializedAttributor>;
11
+ export interface SerializedAttributor {
12
+ interner: readonly string[];
13
+ seqs: number[];
14
+ timestamps: number[];
15
+ attributionRefs: InternedStringId[];
16
+ }
17
+ export declare class AttributorSerializer implements IAttributorSerializer {
18
+ private readonly makeAttributor;
19
+ private readonly timestampEncoder;
20
+ constructor(makeAttributor: (entries: Iterable<[number, AttributionInfo]>) => IAttributor, timestampEncoder: TimestampEncoder);
21
+ encode(attributor: IAttributor): SerializedAttributor;
22
+ decode(encoded: SerializedAttributor): IAttributor;
23
+ }
24
+ /**
25
+ * @returns an encoder which composes `a` and `b`.
26
+ */
27
+ export declare const chain: <T1, T2, T3>(a: Encoder<T1, T2>, b: Encoder<T2, T3>) => Encoder<T1, T3>;
28
+ //# sourceMappingURL=encoders.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoders.d.ts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ved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { assert } from "@fluidframework/common-utils";
6
+ import { MutableStringInterner } from "./stringInterner";
7
+ export const deltaEncoder = {
8
+ encode: (timestamps) => {
9
+ const deltaTimestamps = new Array(timestamps.length);
10
+ let prev = 0;
11
+ for (let i = 0; i < timestamps.length; i++) {
12
+ deltaTimestamps[i] = timestamps[i] - prev;
13
+ prev = timestamps[i];
14
+ }
15
+ return deltaTimestamps;
16
+ },
17
+ decode: (encoded) => {
18
+ assert(Array.isArray(encoded), 0x4b0 /* Encoded timestamps should be an array of nummbers */);
19
+ const timestamps = new Array(encoded.length);
20
+ let cumulativeSum = 0;
21
+ for (let i = 0; i < encoded.length; i++) {
22
+ cumulativeSum += encoded[i];
23
+ timestamps[i] = cumulativeSum;
24
+ }
25
+ return timestamps;
26
+ },
27
+ };
28
+ export class AttributorSerializer {
29
+ constructor(makeAttributor, timestampEncoder) {
30
+ this.makeAttributor = makeAttributor;
31
+ this.timestampEncoder = timestampEncoder;
32
+ }
33
+ encode(attributor) {
34
+ const interner = new MutableStringInterner();
35
+ const seqs = [];
36
+ const timestamps = [];
37
+ const attributionRefs = [];
38
+ for (const [seq, { user, timestamp }] of attributor.entries()) {
39
+ seqs.push(seq);
40
+ timestamps.push(timestamp);
41
+ const ref = interner.getOrCreateInternedId(JSON.stringify(user));
42
+ attributionRefs.push(ref);
43
+ }
44
+ const serialized = {
45
+ interner: interner.getSerializable(),
46
+ seqs,
47
+ timestamps: this.timestampEncoder.encode(timestamps),
48
+ attributionRefs,
49
+ };
50
+ return serialized;
51
+ }
52
+ decode(encoded) {
53
+ const interner = new MutableStringInterner(encoded.interner);
54
+ const { seqs, timestamps: encodedTimestamps, attributionRefs } = encoded;
55
+ const timestamps = this.timestampEncoder.decode(encodedTimestamps);
56
+ assert(seqs.length === timestamps.length && timestamps.length === attributionRefs.length, 0x4b1 /* serialized attribution columns should have the same length */);
57
+ const entries = new Array(seqs.length);
58
+ for (let i = 0; i < seqs.length; i++) {
59
+ const key = seqs[i];
60
+ const timestamp = timestamps[i];
61
+ const ref = attributionRefs[i];
62
+ const user = JSON.parse(interner.getString(ref));
63
+ entries[i] = [key, { user, timestamp }];
64
+ }
65
+ return this.makeAttributor(entries);
66
+ }
67
+ }
68
+ /**
69
+ * @returns an encoder which composes `a` and `b`.
70
+ */
71
+ export const chain = (a, b) => ({
72
+ encode: (content) => b.encode(a.encode(content)),
73
+ decode: (content) => a.decode(b.decode(content)),
74
+ });
75
+ //# sourceMappingURL=encoders.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoders.js","sourceRoot":"","sources":["../src/encoders.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAC;AAKtD,OAAO,EAAoB,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAY3E,MAAM,CAAC,MAAM,YAAY,GAAqB;IAC7C,MAAM,EAAE,CAAC,UAAoB,EAAE,EAAE;QAChC,MAAM,eAAe,GAAa,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAC/D,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC3C,eAAe,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;YAC1C,IAAI,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;SACrB;QACD,OAAO,eAAe,CAAC;IACxB,CAAC;IACD,MAAM,EAAE,CAAC,OAAiB,EAAE,EAAE;QAC7B,MAAM,CACL,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EACtB,KAAK,CAAC,uDAAuD,CAC7D,CAAC;QACF,MAAM,UAAU,GAAa,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACvD,IAAI,aAAa,GAAG,CAAC,CAAC;Q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M,CAAC,OAAO,CAAC,CAAC;CAChD,CAAC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { assert } from \"@fluidframework/common-utils\";\nimport { Jsonable } from \"@fluidframework/datastore-definitions\";\nimport { IUser } from \"@fluidframework/protocol-definitions\";\nimport { AttributionInfo } from \"@fluidframework/runtime-definitions\";\nimport { IAttributor } from \"./attributor\";\nimport { InternedStringId, MutableStringInterner } from \"./stringInterner\";\n\nexport interface Encoder<TDecoded, TEncoded> {\n\tencode(decoded: TDecoded): TEncoded;\n\n\tdecode(encoded: TEncoded): TDecoded;\n}\n\n// Note: the encoded format doesn't matter as long as it's serializable;\n// these types could be weakened.\nexport type TimestampEncoder = Encoder<number[], number[]>;\n\nexport const deltaEncoder: TimestampEncoder = {\n\tencode: (timestamps: number[]) => {\n\t\tconst deltaTimestamps: number[] = new Array(timestamps.length);\n\t\tlet prev = 0;\n\t\tfor (let i = 0; i < timestamps.length; i++) {\n\t\t\tdeltaTimestamps[i] = timestamps[i] - prev;\n\t\t\tprev = timestamps[i];\n\t\t}\n\t\treturn deltaTimestamps;\n\t},\n\tdecode: (encoded: Jsonable) => {\n\t\tassert(\n\t\t\tArray.isArray(encoded),\n\t\t\t0x4b0 /* Encoded timestamps should be an array of nummbers */,\n\t\t);\n\t\tconst timestamps: number[] = new Array(encoded.length);\n\t\tlet cumulativeSum = 0;\n\t\tfor (let i = 0; i < encoded.length; i++) {\n\t\t\tcumulativeSum += encoded[i];\n\t\t\ttimestamps[i] = cumulativeSum;\n\t\t}\n\t\treturn timestamps;\n\t},\n};\n\nexport type IAttributorSerializer = Encoder<IAttributor, SerializedAttributor>;\n\nexport interface SerializedAttributor {\n\tinterner: readonly string[] /* result of calling getSerializable() on a StringInterner */;\n\tseqs: number[];\n\ttimestamps: number[];\n\tattributionRefs: InternedStringId[];\n}\n\nexport class AttributorSerializer implements IAttributorSerializer {\n\tconstructor(\n\t\tprivate readonly makeAttributor: (\n\t\t\tentries: Iterable<[number, AttributionInfo]>,\n\t\t) => IAttributor,\n\t\tprivate readonly timestampEncoder: TimestampEncoder,\n\t) {}\n\n\tpublic encode(attributor: IAttributor): SerializedAttributor {\n\t\tconst interner = new MutableStringInterner();\n\t\tconst seqs: number[] = [];\n\t\tconst timestamps: number[] = [];\n\t\tconst attributionRefs: InternedStringId[] = [];\n\t\tfor (const [seq, { user, timestamp }] of attributor.entries()) {\n\t\t\tseqs.push(seq);\n\t\t\ttimestamps.push(timestamp);\n\t\t\tconst ref = interner.getOrCreateInternedId(JSON.stringify(user));\n\t\t\tattributionRefs.push(ref);\n\t\t}\n\n\t\tconst serialized: SerializedAttributor = {\n\t\t\tinterner: interner.getSerializable(),\n\t\t\tseqs,\n\t\t\ttimestamps: this.timestampEncoder.encode(timestamps),\n\t\t\tattributionRefs,\n\t\t};\n\n\t\treturn serialized;\n\t}\n\n\tpublic decode(encoded: SerializedAttributor): IAttributor {\n\t\tconst interner = new MutableStringInterner(encoded.interner);\n\t\tconst { seqs, timestamps: encodedTimestamps, attributionRefs } = encoded;\n\t\tconst timestamps = this.timestampEncoder.decode(encodedTimestamps);\n\t\tassert(\n\t\t\tseqs.length === timestamps.length && timestamps.length === attributionRefs.length,\n\t\t\t0x4b1 /* serialized attribution columns should have the same length */,\n\t\t);\n\t\tconst entries = new Array(seqs.length);\n\t\tfor (let i = 0; i < seqs.length; i++) {\n\t\t\tconst key = seqs[i];\n\t\t\tconst timestamp = timestamps[i];\n\t\t\tconst ref = attributionRefs[i];\n\t\t\tconst user: IUser = JSON.parse(interner.getString(ref));\n\t\t\tentries[i] = [key, { user, timestamp }];\n\t\t}\n\t\treturn this.makeAttributor(entries);\n\t}\n}\n\n/**\n * @returns an encoder which composes `a` and `b`.\n */\nexport const chain = <T1, T2, T3>(a: Encoder<T1, T2>, b: Encoder<T2, T3>): Encoder<T1, T3> => ({\n\tencode: (content) => b.encode(a.encode(content)),\n\tdecode: (content) => a.decode(b.decode(content)),\n});\n"]}
package/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export { Attributor, OpStreamAttributor, IAttributor } from "./attributor";
6
+ export { createRuntimeAttributor, enableOnNewFileKey, IProvideRuntimeAttributor, IRuntimeAttributor, mixinAttributor, } from "./mixinAttributor";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3E,OAAO,EACN,uBAAuB,EACvB,kBAAkB,EAClB,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,GACf,MAAM,mBAAmB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export { Attributor, OpStreamAttributor } from "./attributor";
6
+ export { createRuntimeAttributor, enableOnNewFileKey, IRuntimeAttributor, mixinAttributor, } from "./mixinAttributor";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAe,MAAM,cAAc,CAAC;AAC3E,OAAO,EACN,uBAAuB,EACvB,kBAAkB,EAElB,kBAAkB,EAClB,eAAe,GACf,MAAM,mBAAmB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\nexport { Attributor, OpStreamAttributor, IAttributor } from \"./attributor\";\nexport {\n\tcreateRuntimeAttributor,\n\tenableOnNewFileKey,\n\tIProvideRuntimeAttributor,\n\tIRuntimeAttributor,\n\tmixinAttributor,\n} from \"./mixinAttributor\";\n"]}
@@ -0,0 +1,7 @@
1
+ import { Jsonable } from "@fluidframework/datastore-definitions";
2
+ import { Encoder } from "./encoders";
3
+ /**
4
+ * @alpha
5
+ */
6
+ export declare function makeLZ4Encoder<T>(): Encoder<Jsonable<T>, string>;
7
+ //# sourceMappingURL=lz4Encoder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lz4Encoder.d.ts","sourceRoot":"","sources":["../src/lz4Encoder.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAE,MAAM,uCAAuC,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC;;GAEG;AACH,wBAAgB,cAAc,CAAC,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAchE"}