@fluidframework/container-loader 2.0.0-dev.2.2.0.111723 → 2.0.0-dev.3.1.0.125672

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 (159) hide show
  1. package/.eslintrc.js +18 -21
  2. package/.mocharc.js +2 -2
  3. package/README.md +58 -40
  4. package/api-extractor.json +2 -2
  5. package/dist/audience.d.ts +0 -1
  6. package/dist/audience.d.ts.map +1 -1
  7. package/dist/audience.js +6 -1
  8. package/dist/audience.js.map +1 -1
  9. package/dist/catchUpMonitor.d.ts.map +1 -1
  10. package/dist/catchUpMonitor.js.map +1 -1
  11. package/dist/collabWindowTracker.d.ts.map +1 -1
  12. package/dist/collabWindowTracker.js +5 -4
  13. package/dist/collabWindowTracker.js.map +1 -1
  14. package/dist/connectionManager.d.ts +5 -5
  15. package/dist/connectionManager.d.ts.map +1 -1
  16. package/dist/connectionManager.js +66 -32
  17. package/dist/connectionManager.js.map +1 -1
  18. package/dist/connectionState.d.ts.map +1 -1
  19. package/dist/connectionState.js.map +1 -1
  20. package/dist/connectionStateHandler.d.ts +3 -3
  21. package/dist/connectionStateHandler.d.ts.map +1 -1
  22. package/dist/connectionStateHandler.js +46 -24
  23. package/dist/connectionStateHandler.js.map +1 -1
  24. package/dist/container.d.ts +27 -0
  25. package/dist/container.d.ts.map +1 -1
  26. package/dist/container.js +191 -57
  27. package/dist/container.js.map +1 -1
  28. package/dist/containerContext.d.ts +3 -2
  29. package/dist/containerContext.d.ts.map +1 -1
  30. package/dist/containerContext.js +11 -6
  31. package/dist/containerContext.js.map +1 -1
  32. package/dist/containerStorageAdapter.d.ts.map +1 -1
  33. package/dist/containerStorageAdapter.js +2 -4
  34. package/dist/containerStorageAdapter.js.map +1 -1
  35. package/dist/contracts.d.ts +9 -1
  36. package/dist/contracts.d.ts.map +1 -1
  37. package/dist/contracts.js.map +1 -1
  38. package/dist/deltaManager.d.ts +18 -6
  39. package/dist/deltaManager.d.ts.map +1 -1
  40. package/dist/deltaManager.js +110 -37
  41. package/dist/deltaManager.js.map +1 -1
  42. package/dist/deltaManagerProxy.d.ts.map +1 -1
  43. package/dist/deltaManagerProxy.js.map +1 -1
  44. package/dist/deltaQueue.d.ts.map +1 -1
  45. package/dist/deltaQueue.js +4 -2
  46. package/dist/deltaQueue.js.map +1 -1
  47. package/dist/index.d.ts +1 -1
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js.map +1 -1
  50. package/dist/loader.d.ts +3 -3
  51. package/dist/loader.d.ts.map +1 -1
  52. package/dist/loader.js +16 -8
  53. package/dist/loader.js.map +1 -1
  54. package/dist/packageVersion.d.ts +1 -1
  55. package/dist/packageVersion.js +1 -1
  56. package/dist/packageVersion.js.map +1 -1
  57. package/dist/protocol.d.ts.map +1 -1
  58. package/dist/protocol.js +2 -1
  59. package/dist/protocol.js.map +1 -1
  60. package/dist/protocolTreeDocumentStorageService.d.ts.map +1 -1
  61. package/dist/protocolTreeDocumentStorageService.js.map +1 -1
  62. package/dist/quorum.d.ts.map +1 -1
  63. package/dist/quorum.js.map +1 -1
  64. package/dist/retriableDocumentStorageService.d.ts.map +1 -1
  65. package/dist/retriableDocumentStorageService.js +6 -2
  66. package/dist/retriableDocumentStorageService.js.map +1 -1
  67. package/dist/utils.d.ts.map +1 -1
  68. package/dist/utils.js +6 -4
  69. package/dist/utils.js.map +1 -1
  70. package/lib/audience.d.ts +0 -1
  71. package/lib/audience.d.ts.map +1 -1
  72. package/lib/audience.js +6 -1
  73. package/lib/audience.js.map +1 -1
  74. package/lib/catchUpMonitor.d.ts.map +1 -1
  75. package/lib/catchUpMonitor.js.map +1 -1
  76. package/lib/collabWindowTracker.d.ts.map +1 -1
  77. package/lib/collabWindowTracker.js +5 -4
  78. package/lib/collabWindowTracker.js.map +1 -1
  79. package/lib/connectionManager.d.ts +5 -5
  80. package/lib/connectionManager.d.ts.map +1 -1
  81. package/lib/connectionManager.js +68 -34
  82. package/lib/connectionManager.js.map +1 -1
  83. package/lib/connectionState.d.ts.map +1 -1
  84. package/lib/connectionState.js.map +1 -1
  85. package/lib/connectionStateHandler.d.ts +3 -3
  86. package/lib/connectionStateHandler.d.ts.map +1 -1
  87. package/lib/connectionStateHandler.js +46 -24
  88. package/lib/connectionStateHandler.js.map +1 -1
  89. package/lib/container.d.ts +27 -0
  90. package/lib/container.d.ts.map +1 -1
  91. package/lib/container.js +194 -61
  92. package/lib/container.js.map +1 -1
  93. package/lib/containerContext.d.ts +3 -2
  94. package/lib/containerContext.d.ts.map +1 -1
  95. package/lib/containerContext.js +11 -6
  96. package/lib/containerContext.js.map +1 -1
  97. package/lib/containerStorageAdapter.d.ts.map +1 -1
  98. package/lib/containerStorageAdapter.js +2 -4
  99. package/lib/containerStorageAdapter.js.map +1 -1
  100. package/lib/contracts.d.ts +9 -1
  101. package/lib/contracts.d.ts.map +1 -1
  102. package/lib/contracts.js.map +1 -1
  103. package/lib/deltaManager.d.ts +18 -6
  104. package/lib/deltaManager.d.ts.map +1 -1
  105. package/lib/deltaManager.js +112 -39
  106. package/lib/deltaManager.js.map +1 -1
  107. package/lib/deltaManagerProxy.d.ts.map +1 -1
  108. package/lib/deltaManagerProxy.js.map +1 -1
  109. package/lib/deltaQueue.d.ts.map +1 -1
  110. package/lib/deltaQueue.js +4 -2
  111. package/lib/deltaQueue.js.map +1 -1
  112. package/lib/index.d.ts +1 -1
  113. package/lib/index.d.ts.map +1 -1
  114. package/lib/index.js.map +1 -1
  115. package/lib/loader.d.ts +3 -3
  116. package/lib/loader.d.ts.map +1 -1
  117. package/lib/loader.js +16 -8
  118. package/lib/loader.js.map +1 -1
  119. package/lib/packageVersion.d.ts +1 -1
  120. package/lib/packageVersion.js +1 -1
  121. package/lib/packageVersion.js.map +1 -1
  122. package/lib/protocol.d.ts.map +1 -1
  123. package/lib/protocol.js +2 -1
  124. package/lib/protocol.js.map +1 -1
  125. package/lib/protocolTreeDocumentStorageService.d.ts.map +1 -1
  126. package/lib/protocolTreeDocumentStorageService.js.map +1 -1
  127. package/lib/quorum.d.ts.map +1 -1
  128. package/lib/quorum.js.map +1 -1
  129. package/lib/retriableDocumentStorageService.d.ts.map +1 -1
  130. package/lib/retriableDocumentStorageService.js +6 -2
  131. package/lib/retriableDocumentStorageService.js.map +1 -1
  132. package/lib/utils.d.ts.map +1 -1
  133. package/lib/utils.js +6 -4
  134. package/lib/utils.js.map +1 -1
  135. package/package.json +22 -19
  136. package/prettier.config.cjs +1 -1
  137. package/src/audience.ts +52 -42
  138. package/src/catchUpMonitor.ts +39 -37
  139. package/src/collabWindowTracker.ts +75 -70
  140. package/src/connectionManager.ts +1009 -938
  141. package/src/connectionState.ts +19 -19
  142. package/src/connectionStateHandler.ts +544 -462
  143. package/src/container.ts +2040 -1785
  144. package/src/containerContext.ts +352 -337
  145. package/src/containerStorageAdapter.ts +163 -153
  146. package/src/contracts.ts +155 -145
  147. package/src/deltaManager.ts +1069 -945
  148. package/src/deltaManagerProxy.ts +143 -137
  149. package/src/deltaQueue.ts +155 -151
  150. package/src/index.ts +14 -17
  151. package/src/loader.ts +427 -422
  152. package/src/packageVersion.ts +1 -1
  153. package/src/protocol.ts +93 -87
  154. package/src/protocolTreeDocumentStorageService.ts +30 -33
  155. package/src/quorum.ts +34 -34
  156. package/src/retriableDocumentStorageService.ts +118 -102
  157. package/src/utils.ts +89 -82
  158. package/tsconfig.esnext.json +6 -6
  159. package/tsconfig.json +8 -12
@@ -6,44 +6,40 @@
6
6
  import { ITelemetryLogger } from "@fluidframework/common-definitions";
7
7
  import { LazyPromise } from "@fluidframework/common-utils";
8
8
  import {
9
- IAudience,
10
- IContainerContext,
11
- IDeltaManager,
12
- ILoader,
13
- IRuntime,
14
- ICriticalContainerError,
15
- AttachState,
16
- ILoaderOptions,
17
- IRuntimeFactory,
18
- IProvideRuntimeFactory,
19
- IFluidCodeDetails,
20
- IFluidCodeDetailsComparer,
21
- IProvideFluidCodeDetailsComparer,
22
- ICodeDetailsLoader,
23
- IFluidModuleWithDetails,
24
- ISnapshotTreeWithBlobContents,
25
- IBatchMessage,
9
+ IAudience,
10
+ IContainerContext,
11
+ IDeltaManager,
12
+ ILoader,
13
+ IRuntime,
14
+ ICriticalContainerError,
15
+ AttachState,
16
+ ILoaderOptions,
17
+ IRuntimeFactory,
18
+ IProvideRuntimeFactory,
19
+ IFluidCodeDetails,
20
+ IFluidCodeDetailsComparer,
21
+ IProvideFluidCodeDetailsComparer,
22
+ ICodeDetailsLoader,
23
+ IFluidModuleWithDetails,
24
+ ISnapshotTreeWithBlobContents,
25
+ IBatchMessage,
26
26
  } from "@fluidframework/container-definitions";
27
- import {
28
- IRequest,
29
- IResponse,
30
- FluidObject,
31
- } from "@fluidframework/core-interfaces";
27
+ import { IRequest, IResponse, FluidObject } from "@fluidframework/core-interfaces";
32
28
  import { IDocumentStorageService } from "@fluidframework/driver-definitions";
33
29
  import { isFluidResolvedUrl } from "@fluidframework/driver-utils";
34
30
  import {
35
- IClientConfiguration,
36
- IClientDetails,
37
- IDocumentMessage,
38
- IQuorum,
39
- IQuorumClients,
40
- ISequencedDocumentMessage,
41
- ISignalMessage,
42
- ISnapshotTree,
43
- ISummaryTree,
44
- IVersion,
45
- MessageType,
46
- ISummaryContent,
31
+ IClientConfiguration,
32
+ IClientDetails,
33
+ IDocumentMessage,
34
+ IQuorum,
35
+ IQuorumClients,
36
+ ISequencedDocumentMessage,
37
+ ISignalMessage,
38
+ ISnapshotTree,
39
+ ISummaryTree,
40
+ IVersion,
41
+ MessageType,
42
+ ISummaryContent,
47
43
  } from "@fluidframework/protocol-definitions";
48
44
  import { PerformanceEvent } from "@fluidframework/telemetry-utils";
49
45
  import { Container } from "./container";
@@ -51,307 +47,326 @@ import { Container } from "./container";
51
47
  const PackageNotFactoryError = "Code package does not implement IRuntimeFactory";
52
48
 
53
49
  export class ContainerContext implements IContainerContext {
54
- public static async createOrLoad(
55
- container: Container,
56
- scope: FluidObject,
57
- codeLoader: ICodeDetailsLoader,
58
- codeDetails: IFluidCodeDetails,
59
- baseSnapshot: ISnapshotTree | undefined,
60
- deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
61
- quorum: IQuorum,
62
- loader: ILoader,
63
- submitFn: (type: MessageType, contents: any, batch: boolean, appData: any) => number,
64
- submitSummaryFn: (summaryOp: ISummaryContent) => number,
65
- submitBatchFn: (batch: IBatchMessage[]) => number,
66
- submitSignalFn: (contents: any) => void,
67
- closeFn: (error?: ICriticalContainerError) => void,
68
- version: string,
69
- updateDirtyContainerState: (dirty: boolean) => void,
70
- existing: boolean,
71
- pendingLocalState?: unknown,
72
- ): Promise<ContainerContext> {
73
- const context = new ContainerContext(
74
- container,
75
- scope,
76
- codeLoader,
77
- codeDetails,
78
- baseSnapshot,
79
- deltaManager,
80
- quorum,
81
- loader,
82
- submitFn,
83
- submitSummaryFn,
84
- submitBatchFn,
85
- submitSignalFn,
86
- closeFn,
87
- version,
88
- updateDirtyContainerState,
89
- existing,
90
- pendingLocalState);
91
- await context.instantiateRuntime(existing);
92
- return context;
93
- }
94
-
95
- public readonly taggedLogger: ITelemetryLogger;
96
-
97
- public get clientId(): string | undefined {
98
- return this.container.clientId;
99
- }
100
-
101
- /**
102
- * DISCLAIMER: this id is only for telemetry purposes. Not suitable for any other usages.
103
- */
104
- public get id(): string {
105
- const resolvedUrl = this.container.resolvedUrl;
106
- if (isFluidResolvedUrl(resolvedUrl)) {
107
- return resolvedUrl.id;
108
- }
109
- return "";
110
- }
111
-
112
- public get clientDetails(): IClientDetails {
113
- return this.container.clientDetails;
114
- }
115
-
116
- private _connected: boolean;
117
- /**
118
- * When true, ops are free to flow
119
- * When false, ops should be kept as pending or rejected
120
- */
121
- public get connected(): boolean {
122
- return this._connected;
123
- }
124
-
125
- public get canSummarize(): boolean {
126
- return "summarize" in this.runtime;
127
- }
128
-
129
- public get serviceConfiguration(): IClientConfiguration | undefined {
130
- return this.container.serviceConfiguration;
131
- }
132
-
133
- public get audience(): IAudience {
134
- return this.container.audience;
135
- }
136
-
137
- public get options(): ILoaderOptions {
138
- return this.container.options;
139
- }
140
-
141
- public get baseSnapshot() {
142
- return this._baseSnapshot;
143
- }
144
-
145
- public get storage(): IDocumentStorageService {
146
- return this.container.storage;
147
- }
148
-
149
- private _runtime: IRuntime | undefined;
150
- private get runtime() {
151
- if (this._runtime === undefined) {
152
- throw new Error("Attempted to access runtime before it was defined");
153
- }
154
- return this._runtime;
155
- }
156
-
157
- private _disposed = false;
158
-
159
- public get disposed() {
160
- return this._disposed;
161
- }
162
-
163
- public get codeDetails() { return this._codeDetails; }
164
-
165
- private readonly _quorum: IQuorum;
166
- public get quorum(): IQuorumClients { return this._quorum; }
167
-
168
- private readonly _fluidModuleP: Promise<IFluidModuleWithDetails>;
169
-
170
- constructor(
171
- private readonly container: Container,
172
- public readonly scope: FluidObject,
173
- private readonly codeLoader: ICodeDetailsLoader,
174
- private readonly _codeDetails: IFluidCodeDetails,
175
- private _baseSnapshot: ISnapshotTree | undefined,
176
- public readonly deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
177
- quorum: IQuorum,
178
- public readonly loader: ILoader,
179
- public readonly submitFn: (type: MessageType, contents: any, batch: boolean, appData: any) => number,
180
- public readonly submitSummaryFn: (summaryOp: ISummaryContent) => number,
181
- /** @returns clientSequenceNumber of last message in a batch */
182
- public readonly submitBatchFn: (batch: IBatchMessage[]) => number,
183
- public readonly submitSignalFn: (contents: any) => void,
184
- public readonly closeFn: (error?: ICriticalContainerError) => void,
185
- public readonly version: string,
186
- public readonly updateDirtyContainerState: (dirty: boolean) => void,
187
- public readonly existing: boolean,
188
- public readonly pendingLocalState?: unknown,
189
-
190
- ) {
191
- this._connected = this.container.connected;
192
- this._quorum = quorum;
193
- this.taggedLogger = container.subLogger;
194
- this._fluidModuleP = new LazyPromise<IFluidModuleWithDetails>(
195
- async () => this.loadCodeModule(_codeDetails),
196
- );
197
- this.attachListener();
198
- }
199
-
200
- /**
201
- * @deprecated Temporary migratory API, to be removed when customers no longer need it.
202
- * When removed, `ContainerContext` should only take an {@link @fluidframework/container-definitions#IQuorumClients}
203
- * rather than an {@link @fluidframework/protocol-definitions#IQuorum}.
204
- * See {@link @fluidframework/container-definitions#IContainerContext} for more details.
205
- */
206
- public getSpecifiedCodeDetails(): IFluidCodeDetails | undefined {
207
- return (this._quorum.get("code") ?? this._quorum.get("code2")) as IFluidCodeDetails | undefined;
208
- }
209
-
210
- public dispose(error?: Error): void {
211
- if (this._disposed) {
212
- return;
213
- }
214
- this._disposed = true;
215
-
216
- this.runtime.dispose(error);
217
- this._quorum.dispose();
218
- this.deltaManager.dispose();
219
- }
220
-
221
- public getLoadedFromVersion(): IVersion | undefined {
222
- return this.container.loadedFromVersion;
223
- }
224
-
225
- public get attachState(): AttachState {
226
- return this.container.attachState;
227
- }
228
-
229
- /**
230
- * Create a summary. Used when attaching or serializing a detached container.
231
- *
232
- * @param blobRedirectTable - A table passed during the attach process. While detached, blob upload is supported
233
- * using IDs generated locally. After attach, these IDs cannot be used, so this table maps the old local IDs to the
234
- * new storage IDs so requests can be redirected.
235
- */
236
- public createSummary(blobRedirectTable?: Map<string, string>): ISummaryTree {
237
- return this.runtime.createSummary(blobRedirectTable);
238
- }
239
-
240
- public setConnectionState(connected: boolean, clientId?: string) {
241
- const runtime = this.runtime;
242
- this._connected = connected;
243
- runtime.setConnectionState(connected, clientId);
244
- }
245
-
246
- public process(message: ISequencedDocumentMessage, local: boolean) {
247
- this.runtime.process(message, local);
248
- }
249
-
250
- public processSignal(message: ISignalMessage, local: boolean) {
251
- this.runtime.processSignal(message, local);
252
- }
253
-
254
- public async request(path: IRequest): Promise<IResponse> {
255
- return this.runtime.request(path);
256
- }
257
-
258
- public async getAbsoluteUrl(relativeUrl: string): Promise<string | undefined> {
259
- return this.container.getAbsoluteUrl(relativeUrl);
260
- }
261
-
262
- public getPendingLocalState(): unknown {
263
- return this.runtime.getPendingLocalState();
264
- }
265
-
266
- /**
267
- * Determines if the current code details of the context
268
- * satisfy the incoming constraint code details
269
- */
270
- public async satisfies(constraintCodeDetails: IFluidCodeDetails) {
271
- const comparers: IFluidCodeDetailsComparer[] = [];
272
-
273
- const maybeCompareCodeLoader = this.codeLoader;
274
- if (maybeCompareCodeLoader.IFluidCodeDetailsComparer !== undefined) {
275
- comparers.push(maybeCompareCodeLoader.IFluidCodeDetailsComparer);
276
- }
277
-
278
- const moduleWithDetails = await this._fluidModuleP;
279
- const maybeCompareExport: Partial<IProvideFluidCodeDetailsComparer> | undefined =
280
- moduleWithDetails.module?.fluidExport;
281
- if (maybeCompareExport?.IFluidCodeDetailsComparer !== undefined) {
282
- comparers.push(maybeCompareExport.IFluidCodeDetailsComparer);
283
- }
284
-
285
- // if there are not comparers it is not possible to know
286
- // if the current satisfy the incoming, so return false,
287
- // as assuming they do not satisfy is safer .e.g we will
288
- // reload, rather than potentially running with
289
- // incompatible code
290
- if (comparers.length === 0) {
291
- return false;
292
- }
293
-
294
- for (const comparer of comparers) {
295
- const satisfies = await comparer.satisfies(
296
- moduleWithDetails.details,
297
- constraintCodeDetails,
298
- );
299
- if (satisfies === false) {
300
- return false;
301
- }
302
- }
303
- return true;
304
- }
305
-
306
- public notifyAttaching(snapshot: ISnapshotTreeWithBlobContents) {
307
- this._baseSnapshot = snapshot;
308
- this.runtime.notifyAttaching?.(snapshot);
309
- this.runtime.setAttachState(AttachState.Attaching);
310
- }
311
-
312
- // #region private
313
-
314
- private async getRuntimeFactory(): Promise<IRuntimeFactory> {
315
- const fluidExport: FluidObject<IProvideRuntimeFactory> | undefined =
316
- (await this._fluidModuleP).module?.fluidExport;
317
- const runtimeFactory = fluidExport?.IRuntimeFactory;
318
- if (runtimeFactory === undefined) {
319
- throw new Error(PackageNotFactoryError);
320
- }
321
-
322
- return runtimeFactory;
323
- }
324
-
325
- private async instantiateRuntime(existing: boolean) {
326
- const runtimeFactory = await this.getRuntimeFactory();
327
- this._runtime = await runtimeFactory.instantiateRuntime(this, existing);
328
- }
329
-
330
- private attachListener() {
331
- this.container.once("attached", () => {
332
- this.runtime.setAttachState(AttachState.Attached);
333
- });
334
- }
335
-
336
- private async loadCodeModule(codeDetails: IFluidCodeDetails): Promise<IFluidModuleWithDetails> {
337
- const loadCodeResult = await PerformanceEvent.timedExecAsync(
338
- this.taggedLogger,
339
- { eventName: "CodeLoad" },
340
- async () => this.codeLoader.load(codeDetails),
341
- );
342
-
343
- if ("module" in loadCodeResult) {
344
- const { module, details } = loadCodeResult;
345
- return {
346
- module,
347
- details: details ?? codeDetails,
348
- };
349
- } else {
350
- // If "module" is not in the result, we are using a legacy ICodeLoader. Fix the result up with details.
351
- // Once usage drops to 0 we can remove this compat path.
352
- this.taggedLogger.sendTelemetryEvent({ eventName: "LegacyCodeLoader" });
353
- return loadCodeResult;
354
- }
355
- }
356
- // #endregion
50
+ public static async createOrLoad(
51
+ container: Container,
52
+ scope: FluidObject,
53
+ codeLoader: ICodeDetailsLoader,
54
+ codeDetails: IFluidCodeDetails,
55
+ baseSnapshot: ISnapshotTree | undefined,
56
+ deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
57
+ quorum: IQuorum,
58
+ loader: ILoader,
59
+ submitFn: (type: MessageType, contents: any, batch: boolean, appData: any) => number,
60
+ submitSummaryFn: (summaryOp: ISummaryContent) => number,
61
+ submitBatchFn: (batch: IBatchMessage[]) => number,
62
+ submitSignalFn: (contents: any) => void,
63
+ disposeFn: (error?: ICriticalContainerError) => void,
64
+ closeFn: (error?: ICriticalContainerError) => void,
65
+ version: string,
66
+ updateDirtyContainerState: (dirty: boolean) => void,
67
+ existing: boolean,
68
+ pendingLocalState?: unknown,
69
+ ): Promise<ContainerContext> {
70
+ const context = new ContainerContext(
71
+ container,
72
+ scope,
73
+ codeLoader,
74
+ codeDetails,
75
+ baseSnapshot,
76
+ deltaManager,
77
+ quorum,
78
+ loader,
79
+ submitFn,
80
+ submitSummaryFn,
81
+ submitBatchFn,
82
+ submitSignalFn,
83
+ disposeFn,
84
+ closeFn,
85
+ version,
86
+ updateDirtyContainerState,
87
+ existing,
88
+ pendingLocalState,
89
+ );
90
+ await context.instantiateRuntime(existing);
91
+ return context;
92
+ }
93
+
94
+ public readonly taggedLogger: ITelemetryLogger;
95
+
96
+ public get clientId(): string | undefined {
97
+ return this.container.clientId;
98
+ }
99
+
100
+ /**
101
+ * DISCLAIMER: this id is only for telemetry purposes. Not suitable for any other usages.
102
+ */
103
+ public get id(): string {
104
+ const resolvedUrl = this.container.resolvedUrl;
105
+ if (isFluidResolvedUrl(resolvedUrl)) {
106
+ return resolvedUrl.id;
107
+ }
108
+ return "";
109
+ }
110
+
111
+ public get clientDetails(): IClientDetails {
112
+ return this.container.clientDetails;
113
+ }
114
+
115
+ private _connected: boolean;
116
+ /**
117
+ * When true, ops are free to flow
118
+ * When false, ops should be kept as pending or rejected
119
+ */
120
+ public get connected(): boolean {
121
+ return this._connected;
122
+ }
123
+
124
+ public get canSummarize(): boolean {
125
+ return "summarize" in this.runtime;
126
+ }
127
+
128
+ public get serviceConfiguration(): IClientConfiguration | undefined {
129
+ return this.container.serviceConfiguration;
130
+ }
131
+
132
+ public get audience(): IAudience {
133
+ return this.container.audience;
134
+ }
135
+
136
+ public get options(): ILoaderOptions {
137
+ return this.container.options;
138
+ }
139
+
140
+ public get baseSnapshot() {
141
+ return this._baseSnapshot;
142
+ }
143
+
144
+ public get storage(): IDocumentStorageService {
145
+ return this.container.storage;
146
+ }
147
+
148
+ private _runtime: IRuntime | undefined;
149
+ private get runtime() {
150
+ if (this._runtime === undefined) {
151
+ throw new Error("Attempted to access runtime before it was defined");
152
+ }
153
+ return this._runtime;
154
+ }
155
+
156
+ private _disposed = false;
157
+
158
+ public get disposed() {
159
+ return this._disposed;
160
+ }
161
+
162
+ public get codeDetails() {
163
+ return this._codeDetails;
164
+ }
165
+
166
+ private readonly _quorum: IQuorum;
167
+ public get quorum(): IQuorumClients {
168
+ return this._quorum;
169
+ }
170
+
171
+ private readonly _fluidModuleP: Promise<IFluidModuleWithDetails>;
172
+
173
+ constructor(
174
+ private readonly container: Container,
175
+ public readonly scope: FluidObject,
176
+ private readonly codeLoader: ICodeDetailsLoader,
177
+ private readonly _codeDetails: IFluidCodeDetails,
178
+ private _baseSnapshot: ISnapshotTree | undefined,
179
+ public readonly deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
180
+ quorum: IQuorum,
181
+ public readonly loader: ILoader,
182
+ public readonly submitFn: (
183
+ type: MessageType,
184
+ contents: any,
185
+ batch: boolean,
186
+ appData: any,
187
+ ) => number,
188
+ public readonly submitSummaryFn: (summaryOp: ISummaryContent) => number,
189
+ /** @returns clientSequenceNumber of last message in a batch */
190
+ public readonly submitBatchFn: (batch: IBatchMessage[]) => number,
191
+ public readonly submitSignalFn: (contents: any) => void,
192
+ public readonly disposeFn: (error?: ICriticalContainerError) => void,
193
+ public readonly closeFn: (error?: ICriticalContainerError) => void,
194
+ public readonly version: string,
195
+ public readonly updateDirtyContainerState: (dirty: boolean) => void,
196
+ public readonly existing: boolean,
197
+ public readonly pendingLocalState?: unknown,
198
+ ) {
199
+ this._connected = this.container.connected;
200
+ this._quorum = quorum;
201
+ this.taggedLogger = container.subLogger;
202
+ this._fluidModuleP = new LazyPromise<IFluidModuleWithDetails>(async () =>
203
+ this.loadCodeModule(_codeDetails),
204
+ );
205
+ this.attachListener();
206
+ }
207
+
208
+ /**
209
+ * @deprecated Temporary migratory API, to be removed when customers no longer need it.
210
+ * When removed, `ContainerContext` should only take an {@link @fluidframework/container-definitions#IQuorumClients}
211
+ * rather than an {@link @fluidframework/protocol-definitions#IQuorum}.
212
+ * See {@link @fluidframework/container-definitions#IContainerContext} for more details.
213
+ */
214
+ public getSpecifiedCodeDetails(): IFluidCodeDetails | undefined {
215
+ return (this._quorum.get("code") ?? this._quorum.get("code2")) as
216
+ | IFluidCodeDetails
217
+ | undefined;
218
+ }
219
+
220
+ public dispose(error?: Error): void {
221
+ if (this._disposed) {
222
+ return;
223
+ }
224
+ this._disposed = true;
225
+
226
+ this.runtime.dispose(error);
227
+ this._quorum.dispose();
228
+ this.deltaManager.dispose();
229
+ }
230
+
231
+ public getLoadedFromVersion(): IVersion | undefined {
232
+ return this.container.loadedFromVersion;
233
+ }
234
+
235
+ public get attachState(): AttachState {
236
+ return this.container.attachState;
237
+ }
238
+
239
+ /**
240
+ * Create a summary. Used when attaching or serializing a detached container.
241
+ *
242
+ * @param blobRedirectTable - A table passed during the attach process. While detached, blob upload is supported
243
+ * using IDs generated locally. After attach, these IDs cannot be used, so this table maps the old local IDs to the
244
+ * new storage IDs so requests can be redirected.
245
+ */
246
+ public createSummary(blobRedirectTable?: Map<string, string>): ISummaryTree {
247
+ return this.runtime.createSummary(blobRedirectTable);
248
+ }
249
+
250
+ public setConnectionState(connected: boolean, clientId?: string) {
251
+ const runtime = this.runtime;
252
+ this._connected = connected;
253
+ runtime.setConnectionState(connected, clientId);
254
+ }
255
+
256
+ public process(message: ISequencedDocumentMessage, local: boolean) {
257
+ this.runtime.process(message, local);
258
+ }
259
+
260
+ public processSignal(message: ISignalMessage, local: boolean) {
261
+ this.runtime.processSignal(message, local);
262
+ }
263
+
264
+ public async request(path: IRequest): Promise<IResponse> {
265
+ return this.runtime.request(path);
266
+ }
267
+
268
+ public async getAbsoluteUrl(relativeUrl: string): Promise<string | undefined> {
269
+ return this.container.getAbsoluteUrl(relativeUrl);
270
+ }
271
+
272
+ public getPendingLocalState(): unknown {
273
+ return this.runtime.getPendingLocalState();
274
+ }
275
+
276
+ /**
277
+ * Determines if the current code details of the context
278
+ * satisfy the incoming constraint code details
279
+ */
280
+ public async satisfies(constraintCodeDetails: IFluidCodeDetails) {
281
+ const comparers: IFluidCodeDetailsComparer[] = [];
282
+
283
+ const maybeCompareCodeLoader = this.codeLoader;
284
+ if (maybeCompareCodeLoader.IFluidCodeDetailsComparer !== undefined) {
285
+ comparers.push(maybeCompareCodeLoader.IFluidCodeDetailsComparer);
286
+ }
287
+
288
+ const moduleWithDetails = await this._fluidModuleP;
289
+ const maybeCompareExport: Partial<IProvideFluidCodeDetailsComparer> | undefined =
290
+ moduleWithDetails.module?.fluidExport;
291
+ if (maybeCompareExport?.IFluidCodeDetailsComparer !== undefined) {
292
+ comparers.push(maybeCompareExport.IFluidCodeDetailsComparer);
293
+ }
294
+
295
+ // if there are not comparers it is not possible to know
296
+ // if the current satisfy the incoming, so return false,
297
+ // as assuming they do not satisfy is safer .e.g we will
298
+ // reload, rather than potentially running with
299
+ // incompatible code
300
+ if (comparers.length === 0) {
301
+ return false;
302
+ }
303
+
304
+ for (const comparer of comparers) {
305
+ const satisfies = await comparer.satisfies(
306
+ moduleWithDetails.details,
307
+ constraintCodeDetails,
308
+ );
309
+ if (satisfies === false) {
310
+ return false;
311
+ }
312
+ }
313
+ return true;
314
+ }
315
+
316
+ public notifyAttaching(snapshot: ISnapshotTreeWithBlobContents) {
317
+ this._baseSnapshot = snapshot;
318
+ this.runtime.notifyAttaching?.(snapshot);
319
+ this.runtime.setAttachState(AttachState.Attaching);
320
+ }
321
+
322
+ // #region private
323
+
324
+ private async getRuntimeFactory(): Promise<IRuntimeFactory> {
325
+ const fluidExport: FluidObject<IProvideRuntimeFactory> | undefined = (
326
+ await this._fluidModuleP
327
+ ).module?.fluidExport;
328
+ const runtimeFactory = fluidExport?.IRuntimeFactory;
329
+ if (runtimeFactory === undefined) {
330
+ throw new Error(PackageNotFactoryError);
331
+ }
332
+
333
+ return runtimeFactory;
334
+ }
335
+
336
+ private async instantiateRuntime(existing: boolean) {
337
+ const runtimeFactory = await this.getRuntimeFactory();
338
+ this._runtime = await PerformanceEvent.timedExecAsync(
339
+ this.taggedLogger,
340
+ { eventName: "InstantiateRuntime" },
341
+ async () => runtimeFactory.instantiateRuntime(this, existing),
342
+ );
343
+ }
344
+
345
+ private attachListener() {
346
+ this.container.once("attached", () => {
347
+ this.runtime.setAttachState(AttachState.Attached);
348
+ });
349
+ }
350
+
351
+ private async loadCodeModule(codeDetails: IFluidCodeDetails): Promise<IFluidModuleWithDetails> {
352
+ const loadCodeResult = await PerformanceEvent.timedExecAsync(
353
+ this.taggedLogger,
354
+ { eventName: "CodeLoad" },
355
+ async () => this.codeLoader.load(codeDetails),
356
+ );
357
+
358
+ if ("module" in loadCodeResult) {
359
+ const { module, details } = loadCodeResult;
360
+ return {
361
+ module,
362
+ details: details ?? codeDetails,
363
+ };
364
+ } else {
365
+ // If "module" is not in the result, we are using a legacy ICodeLoader. Fix the result up with details.
366
+ // Once usage drops to 0 we can remove this compat path.
367
+ this.taggedLogger.sendTelemetryEvent({ eventName: "LegacyCodeLoader" });
368
+ return loadCodeResult;
369
+ }
370
+ }
371
+ // #endregion
357
372
  }