@fluidframework/container-loader 1.4.0-121020 → 2.0.0-dev-rc.1.0.0.225277

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 (335) hide show
  1. package/.eslintrc.js +18 -21
  2. package/.mocharc.js +12 -0
  3. package/CHANGELOG.md +364 -0
  4. package/README.md +152 -56
  5. package/api-extractor-esm.json +4 -0
  6. package/api-extractor-lint.json +4 -0
  7. package/api-extractor.json +2 -2
  8. package/api-report/container-loader.api.md +143 -0
  9. package/dist/{audience.js → audience.cjs} +15 -13
  10. package/dist/audience.cjs.map +1 -0
  11. package/dist/audience.d.ts +4 -6
  12. package/dist/audience.d.ts.map +1 -1
  13. package/dist/catchUpMonitor.cjs +43 -0
  14. package/dist/catchUpMonitor.cjs.map +1 -0
  15. package/dist/catchUpMonitor.d.ts +29 -0
  16. package/dist/catchUpMonitor.d.ts.map +1 -0
  17. package/dist/{connectionManager.js → connectionManager.cjs} +397 -240
  18. package/dist/connectionManager.cjs.map +1 -0
  19. package/dist/connectionManager.d.ts +23 -33
  20. package/dist/connectionManager.d.ts.map +1 -1
  21. package/dist/{connectionState.js → connectionState.cjs} +5 -7
  22. package/dist/connectionState.cjs.map +1 -0
  23. package/dist/connectionState.d.ts +3 -5
  24. package/dist/connectionState.d.ts.map +1 -1
  25. package/dist/connectionStateHandler.cjs +474 -0
  26. package/dist/connectionStateHandler.cjs.map +1 -0
  27. package/dist/connectionStateHandler.d.ts +127 -29
  28. package/dist/connectionStateHandler.d.ts.map +1 -1
  29. package/dist/container-loader-alpha.d.ts +274 -0
  30. package/dist/container-loader-beta.d.ts +75 -0
  31. package/dist/container-loader-public.d.ts +75 -0
  32. package/dist/container-loader-untrimmed.d.ts +331 -0
  33. package/dist/container.cjs +1585 -0
  34. package/dist/container.cjs.map +1 -0
  35. package/dist/container.d.ts +227 -83
  36. package/dist/container.d.ts.map +1 -1
  37. package/dist/containerContext.cjs +74 -0
  38. package/dist/containerContext.cjs.map +1 -0
  39. package/dist/containerContext.d.ts +33 -59
  40. package/dist/containerContext.d.ts.map +1 -1
  41. package/dist/containerStorageAdapter.cjs +234 -0
  42. package/dist/containerStorageAdapter.cjs.map +1 -0
  43. package/dist/containerStorageAdapter.d.ts +48 -23
  44. package/dist/containerStorageAdapter.d.ts.map +1 -1
  45. package/dist/{contracts.js → contracts.cjs} +5 -5
  46. package/dist/contracts.cjs.map +1 -0
  47. package/dist/contracts.d.ts +45 -17
  48. package/dist/contracts.d.ts.map +1 -1
  49. package/dist/debugLogger.cjs +101 -0
  50. package/dist/debugLogger.cjs.map +1 -0
  51. package/dist/debugLogger.d.ts +30 -0
  52. package/dist/debugLogger.d.ts.map +1 -0
  53. package/dist/{deltaManager.js → deltaManager.cjs} +379 -186
  54. package/dist/deltaManager.cjs.map +1 -0
  55. package/dist/deltaManager.d.ts +54 -18
  56. package/dist/deltaManager.d.ts.map +1 -1
  57. package/dist/{deltaQueue.js → deltaQueue.cjs} +29 -28
  58. package/dist/deltaQueue.cjs.map +1 -0
  59. package/dist/deltaQueue.d.ts +3 -4
  60. package/dist/deltaQueue.d.ts.map +1 -1
  61. package/dist/disposal.cjs +25 -0
  62. package/dist/disposal.cjs.map +1 -0
  63. package/dist/disposal.d.ts +13 -0
  64. package/dist/disposal.d.ts.map +1 -0
  65. package/dist/error.cjs +32 -0
  66. package/dist/error.cjs.map +1 -0
  67. package/dist/error.d.ts +23 -0
  68. package/dist/error.d.ts.map +1 -0
  69. package/dist/index.cjs +19 -0
  70. package/dist/index.cjs.map +1 -0
  71. package/dist/index.d.ts +5 -2
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/loader.cjs +148 -0
  74. package/dist/loader.cjs.map +1 -0
  75. package/dist/loader.d.ts +38 -19
  76. package/dist/loader.d.ts.map +1 -1
  77. package/dist/location-redirection-utilities/index.cjs +11 -0
  78. package/dist/location-redirection-utilities/index.cjs.map +1 -0
  79. package/dist/location-redirection-utilities/index.d.ts +6 -0
  80. package/dist/location-redirection-utilities/index.d.ts.map +1 -0
  81. package/dist/location-redirection-utilities/resolveWithLocationRedirection.cjs +53 -0
  82. package/dist/location-redirection-utilities/resolveWithLocationRedirection.cjs.map +1 -0
  83. package/dist/location-redirection-utilities/resolveWithLocationRedirection.d.ts +24 -0
  84. package/dist/location-redirection-utilities/resolveWithLocationRedirection.d.ts.map +1 -0
  85. package/dist/{collabWindowTracker.js → noopHeuristic.cjs} +37 -39
  86. package/dist/noopHeuristic.cjs.map +1 -0
  87. package/dist/noopHeuristic.d.ts +23 -0
  88. package/dist/noopHeuristic.d.ts.map +1 -0
  89. package/dist/{packageVersion.js → packageVersion.cjs} +2 -2
  90. package/dist/packageVersion.cjs.map +1 -0
  91. package/dist/packageVersion.d.ts +1 -1
  92. package/dist/packageVersion.d.ts.map +1 -1
  93. package/dist/protocol.cjs +99 -0
  94. package/dist/protocol.cjs.map +1 -0
  95. package/dist/protocol.d.ts +38 -0
  96. package/dist/protocol.d.ts.map +1 -0
  97. package/dist/{protocolTreeDocumentStorageService.js → protocolTreeDocumentStorageService.cjs} +8 -5
  98. package/dist/protocolTreeDocumentStorageService.cjs.map +1 -0
  99. package/dist/protocolTreeDocumentStorageService.d.ts +8 -4
  100. package/dist/protocolTreeDocumentStorageService.d.ts.map +1 -1
  101. package/dist/quorum.cjs +16 -0
  102. package/dist/quorum.cjs.map +1 -0
  103. package/dist/quorum.d.ts +1 -14
  104. package/dist/quorum.d.ts.map +1 -1
  105. package/dist/{retriableDocumentStorageService.js → retriableDocumentStorageService.cjs} +36 -21
  106. package/dist/retriableDocumentStorageService.cjs.map +1 -0
  107. package/dist/retriableDocumentStorageService.d.ts +7 -5
  108. package/dist/retriableDocumentStorageService.d.ts.map +1 -1
  109. package/dist/tsdoc-metadata.json +11 -0
  110. package/dist/{utils.js → utils.cjs} +52 -14
  111. package/dist/utils.cjs.map +1 -0
  112. package/dist/utils.d.ts +34 -1
  113. package/dist/utils.d.ts.map +1 -1
  114. package/lib/{audience.d.ts → audience.d.mts} +5 -11
  115. package/lib/audience.d.mts.map +1 -0
  116. package/lib/{audience.js → audience.mjs} +15 -17
  117. package/lib/audience.mjs.map +1 -0
  118. package/lib/catchUpMonitor.d.mts +29 -0
  119. package/lib/catchUpMonitor.d.mts.map +1 -0
  120. package/lib/catchUpMonitor.mjs +39 -0
  121. package/lib/catchUpMonitor.mjs.map +1 -0
  122. package/lib/{connectionManager.d.ts → connectionManager.d.mts} +24 -34
  123. package/lib/connectionManager.d.mts.map +1 -0
  124. package/lib/{connectionManager.js → connectionManager.mjs} +378 -218
  125. package/lib/connectionManager.mjs.map +1 -0
  126. package/lib/{connectionState.d.ts → connectionState.d.mts} +4 -6
  127. package/lib/connectionState.d.mts.map +1 -0
  128. package/lib/{connectionState.js → connectionState.mjs} +4 -6
  129. package/lib/connectionState.mjs.map +1 -0
  130. package/lib/connectionStateHandler.d.mts +179 -0
  131. package/lib/connectionStateHandler.d.mts.map +1 -0
  132. package/lib/connectionStateHandler.mjs +469 -0
  133. package/lib/connectionStateHandler.mjs.map +1 -0
  134. package/lib/container-loader-alpha.d.mts +274 -0
  135. package/lib/container-loader-beta.d.mts +75 -0
  136. package/lib/container-loader-public.d.mts +75 -0
  137. package/lib/container-loader-untrimmed.d.mts +331 -0
  138. package/lib/container.d.mts +382 -0
  139. package/lib/container.d.mts.map +1 -0
  140. package/lib/container.mjs +1579 -0
  141. package/lib/container.mjs.map +1 -0
  142. package/lib/containerContext.d.mts +58 -0
  143. package/lib/containerContext.d.mts.map +1 -0
  144. package/lib/containerContext.mjs +70 -0
  145. package/lib/containerContext.mjs.map +1 -0
  146. package/lib/containerStorageAdapter.d.mts +73 -0
  147. package/lib/containerStorageAdapter.d.mts.map +1 -0
  148. package/lib/containerStorageAdapter.mjs +228 -0
  149. package/lib/containerStorageAdapter.mjs.map +1 -0
  150. package/lib/{contracts.d.ts → contracts.d.mts} +46 -18
  151. package/lib/contracts.d.mts.map +1 -0
  152. package/lib/{contracts.js → contracts.mjs} +4 -4
  153. package/lib/contracts.mjs.map +1 -0
  154. package/lib/debugLogger.d.mts +30 -0
  155. package/lib/debugLogger.d.mts.map +1 -0
  156. package/lib/debugLogger.mjs +93 -0
  157. package/lib/debugLogger.mjs.map +1 -0
  158. package/lib/{deltaManager.d.ts → deltaManager.d.mts} +55 -19
  159. package/lib/deltaManager.d.mts.map +1 -0
  160. package/lib/{deltaManager.js → deltaManager.mjs} +361 -165
  161. package/lib/deltaManager.mjs.map +1 -0
  162. package/lib/{deltaQueue.d.ts → deltaQueue.d.mts} +4 -5
  163. package/lib/deltaQueue.d.mts.map +1 -0
  164. package/lib/{deltaQueue.js → deltaQueue.mjs} +25 -24
  165. package/lib/deltaQueue.mjs.map +1 -0
  166. package/lib/disposal.d.mts +13 -0
  167. package/lib/disposal.d.mts.map +1 -0
  168. package/lib/disposal.mjs +21 -0
  169. package/lib/disposal.mjs.map +1 -0
  170. package/lib/error.d.mts +23 -0
  171. package/lib/error.d.mts.map +1 -0
  172. package/lib/error.mjs +28 -0
  173. package/lib/error.mjs.map +1 -0
  174. package/lib/index.d.mts +11 -0
  175. package/lib/index.d.mts.map +1 -0
  176. package/lib/index.mjs +10 -0
  177. package/lib/index.mjs.map +1 -0
  178. package/lib/{loader.d.ts → loader.d.mts} +40 -21
  179. package/lib/loader.d.mts.map +1 -0
  180. package/lib/loader.mjs +143 -0
  181. package/lib/loader.mjs.map +1 -0
  182. package/lib/location-redirection-utilities/index.d.mts +6 -0
  183. package/lib/location-redirection-utilities/index.d.mts.map +1 -0
  184. package/lib/location-redirection-utilities/index.mjs +6 -0
  185. package/lib/location-redirection-utilities/index.mjs.map +1 -0
  186. package/lib/location-redirection-utilities/resolveWithLocationRedirection.d.mts +24 -0
  187. package/lib/location-redirection-utilities/resolveWithLocationRedirection.d.mts.map +1 -0
  188. package/lib/location-redirection-utilities/resolveWithLocationRedirection.mjs +48 -0
  189. package/lib/location-redirection-utilities/resolveWithLocationRedirection.mjs.map +1 -0
  190. package/lib/noopHeuristic.d.mts +23 -0
  191. package/lib/noopHeuristic.d.mts.map +1 -0
  192. package/lib/{collabWindowTracker.js → noopHeuristic.mjs} +33 -35
  193. package/lib/noopHeuristic.mjs.map +1 -0
  194. package/lib/{packageVersion.d.ts → packageVersion.d.mts} +2 -2
  195. package/lib/packageVersion.d.mts.map +1 -0
  196. package/lib/{packageVersion.js → packageVersion.mjs} +2 -2
  197. package/lib/packageVersion.mjs.map +1 -0
  198. package/lib/protocol.d.mts +38 -0
  199. package/lib/protocol.d.mts.map +1 -0
  200. package/lib/protocol.mjs +94 -0
  201. package/lib/protocol.mjs.map +1 -0
  202. package/lib/{protocolTreeDocumentStorageService.d.ts → protocolTreeDocumentStorageService.d.mts} +9 -5
  203. package/lib/protocolTreeDocumentStorageService.d.mts.map +1 -0
  204. package/lib/{protocolTreeDocumentStorageService.js → protocolTreeDocumentStorageService.mjs} +8 -5
  205. package/lib/protocolTreeDocumentStorageService.mjs.map +1 -0
  206. package/lib/quorum.d.mts +4 -0
  207. package/lib/quorum.d.mts.map +1 -0
  208. package/lib/quorum.mjs +12 -0
  209. package/lib/quorum.mjs.map +1 -0
  210. package/lib/{retriableDocumentStorageService.d.ts → retriableDocumentStorageService.d.mts} +8 -6
  211. package/lib/retriableDocumentStorageService.d.mts.map +1 -0
  212. package/lib/{retriableDocumentStorageService.js → retriableDocumentStorageService.mjs} +35 -20
  213. package/lib/retriableDocumentStorageService.mjs.map +1 -0
  214. package/lib/utils.d.mts +67 -0
  215. package/lib/utils.d.mts.map +1 -0
  216. package/lib/{utils.js → utils.mjs} +47 -11
  217. package/lib/utils.mjs.map +1 -0
  218. package/package.json +189 -69
  219. package/prettier.config.cjs +8 -0
  220. package/src/audience.ts +59 -49
  221. package/src/catchUpMonitor.ts +61 -0
  222. package/src/connectionManager.ts +1154 -910
  223. package/src/connectionState.ts +22 -25
  224. package/src/connectionStateHandler.ts +689 -319
  225. package/src/container.ts +2476 -1792
  226. package/src/containerContext.ts +98 -330
  227. package/src/containerStorageAdapter.ts +301 -105
  228. package/src/contracts.ts +184 -146
  229. package/src/debugLogger.ts +123 -0
  230. package/src/deltaManager.ts +1165 -900
  231. package/src/deltaQueue.ts +156 -152
  232. package/src/disposal.ts +25 -0
  233. package/src/error.ts +44 -0
  234. package/src/index.ts +14 -15
  235. package/src/loader.ts +356 -427
  236. package/src/location-redirection-utilities/index.ts +9 -0
  237. package/src/location-redirection-utilities/resolveWithLocationRedirection.ts +61 -0
  238. package/src/noopHeuristic.ts +107 -0
  239. package/src/packageVersion.ts +1 -1
  240. package/src/protocol.ts +150 -0
  241. package/src/protocolTreeDocumentStorageService.ts +35 -35
  242. package/src/quorum.ts +11 -50
  243. package/src/retriableDocumentStorageService.ts +135 -95
  244. package/src/utils.ts +159 -86
  245. package/tsc-multi.test.json +4 -0
  246. package/tsconfig.json +10 -12
  247. package/dist/audience.js.map +0 -1
  248. package/dist/collabWindowTracker.d.ts +0 -19
  249. package/dist/collabWindowTracker.d.ts.map +0 -1
  250. package/dist/collabWindowTracker.js.map +0 -1
  251. package/dist/connectionManager.js.map +0 -1
  252. package/dist/connectionState.js.map +0 -1
  253. package/dist/connectionStateHandler.js +0 -280
  254. package/dist/connectionStateHandler.js.map +0 -1
  255. package/dist/container.js +0 -1284
  256. package/dist/container.js.map +0 -1
  257. package/dist/containerContext.js +0 -217
  258. package/dist/containerContext.js.map +0 -1
  259. package/dist/containerStorageAdapter.js +0 -104
  260. package/dist/containerStorageAdapter.js.map +0 -1
  261. package/dist/contracts.js.map +0 -1
  262. package/dist/deltaManager.js.map +0 -1
  263. package/dist/deltaManagerProxy.d.ts +0 -54
  264. package/dist/deltaManagerProxy.d.ts.map +0 -1
  265. package/dist/deltaManagerProxy.js +0 -115
  266. package/dist/deltaManagerProxy.js.map +0 -1
  267. package/dist/deltaQueue.js.map +0 -1
  268. package/dist/index.js +0 -16
  269. package/dist/index.js.map +0 -1
  270. package/dist/loader.js +0 -241
  271. package/dist/loader.js.map +0 -1
  272. package/dist/packageVersion.js.map +0 -1
  273. package/dist/protocolTreeDocumentStorageService.js.map +0 -1
  274. package/dist/quorum.js +0 -44
  275. package/dist/quorum.js.map +0 -1
  276. package/dist/retriableDocumentStorageService.js.map +0 -1
  277. package/dist/utils.js.map +0 -1
  278. package/lib/audience.d.ts.map +0 -1
  279. package/lib/audience.js.map +0 -1
  280. package/lib/collabWindowTracker.d.ts +0 -19
  281. package/lib/collabWindowTracker.d.ts.map +0 -1
  282. package/lib/collabWindowTracker.js.map +0 -1
  283. package/lib/connectionManager.d.ts.map +0 -1
  284. package/lib/connectionManager.js.map +0 -1
  285. package/lib/connectionState.d.ts.map +0 -1
  286. package/lib/connectionState.js.map +0 -1
  287. package/lib/connectionStateHandler.d.ts +0 -81
  288. package/lib/connectionStateHandler.d.ts.map +0 -1
  289. package/lib/connectionStateHandler.js +0 -276
  290. package/lib/connectionStateHandler.js.map +0 -1
  291. package/lib/container.d.ts +0 -238
  292. package/lib/container.d.ts.map +0 -1
  293. package/lib/container.js +0 -1276
  294. package/lib/container.js.map +0 -1
  295. package/lib/containerContext.d.ts +0 -84
  296. package/lib/containerContext.d.ts.map +0 -1
  297. package/lib/containerContext.js +0 -213
  298. package/lib/containerContext.js.map +0 -1
  299. package/lib/containerStorageAdapter.d.ts +0 -48
  300. package/lib/containerStorageAdapter.d.ts.map +0 -1
  301. package/lib/containerStorageAdapter.js +0 -99
  302. package/lib/containerStorageAdapter.js.map +0 -1
  303. package/lib/contracts.d.ts.map +0 -1
  304. package/lib/contracts.js.map +0 -1
  305. package/lib/deltaManager.d.ts.map +0 -1
  306. package/lib/deltaManager.js.map +0 -1
  307. package/lib/deltaManagerProxy.d.ts +0 -54
  308. package/lib/deltaManagerProxy.d.ts.map +0 -1
  309. package/lib/deltaManagerProxy.js +0 -110
  310. package/lib/deltaManagerProxy.js.map +0 -1
  311. package/lib/deltaQueue.d.ts.map +0 -1
  312. package/lib/deltaQueue.js.map +0 -1
  313. package/lib/index.d.ts +0 -8
  314. package/lib/index.d.ts.map +0 -1
  315. package/lib/index.js +0 -8
  316. package/lib/index.js.map +0 -1
  317. package/lib/loader.d.ts.map +0 -1
  318. package/lib/loader.js +0 -236
  319. package/lib/loader.js.map +0 -1
  320. package/lib/packageVersion.d.ts.map +0 -1
  321. package/lib/packageVersion.js.map +0 -1
  322. package/lib/protocolTreeDocumentStorageService.d.ts.map +0 -1
  323. package/lib/protocolTreeDocumentStorageService.js.map +0 -1
  324. package/lib/quorum.d.ts +0 -21
  325. package/lib/quorum.d.ts.map +0 -1
  326. package/lib/quorum.js +0 -38
  327. package/lib/quorum.js.map +0 -1
  328. package/lib/retriableDocumentStorageService.d.ts.map +0 -1
  329. package/lib/retriableDocumentStorageService.js.map +0 -1
  330. package/lib/utils.d.ts +0 -34
  331. package/lib/utils.d.ts.map +0 -1
  332. package/lib/utils.js.map +0 -1
  333. package/src/collabWindowTracker.ts +0 -102
  334. package/src/deltaManagerProxy.ts +0 -158
  335. package/tsconfig.esnext.json +0 -7
package/lib/container.js DELETED
@@ -1,1276 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- // eslint-disable-next-line import/no-internal-modules
6
- import merge from "lodash/merge";
7
- import { v4 as uuid } from "uuid";
8
- import { assert, performance, unreachableCase } from "@fluidframework/common-utils";
9
- import { AttachState, isFluidCodeDetails, } from "@fluidframework/container-definitions";
10
- import { DataCorruptionError, extractSafePropertiesFromMessage, GenericError, UsageError, } from "@fluidframework/container-utils";
11
- import { readAndParse, OnlineStatus, isOnline, ensureFluidResolvedUrl, combineAppAndProtocolSummary, runWithRetry, isFluidResolvedUrl, isRuntimeMessage, isUnpackedRuntimeMessage, } from "@fluidframework/driver-utils";
12
- import { ProtocolOpHandlerWithClientValidation, } from "@fluidframework/protocol-base";
13
- import { MessageType, SummaryType, } from "@fluidframework/protocol-definitions";
14
- import { ChildLogger, EventEmitterWithErrorHandling, PerformanceEvent, raiseConnectedEvent, TelemetryLogger, connectedEventName, disconnectedEventName, normalizeError, loggerToMonitoringContext, wrapError, } from "@fluidframework/telemetry-utils";
15
- import { Audience } from "./audience";
16
- import { ContainerContext } from "./containerContext";
17
- import { ReconnectMode, getPackageName } from "./contracts";
18
- import { DeltaManager } from "./deltaManager";
19
- import { DeltaManagerProxy } from "./deltaManagerProxy";
20
- import { RelativeLoader } from "./loader";
21
- import { pkgVersion } from "./packageVersion";
22
- import { ConnectionStateHandler } from "./connectionStateHandler";
23
- import { RetriableDocumentStorageService } from "./retriableDocumentStorageService";
24
- import { ProtocolTreeStorageService } from "./protocolTreeDocumentStorageService";
25
- import { BlobOnlyStorage, ContainerStorageAdapter } from "./containerStorageAdapter";
26
- import { getProtocolSnapshotTree, getSnapshotTreeFromSerializedContainer } from "./utils";
27
- import { initQuorumValuesFromCodeDetails, getCodeDetailsFromQuorumValues, QuorumProxy } from "./quorum";
28
- import { CollabWindowTracker } from "./collabWindowTracker";
29
- import { ConnectionManager } from "./connectionManager";
30
- import { ConnectionState } from "./connectionState";
31
- const detachedContainerRefSeqNumber = 0;
32
- const dirtyContainerEvent = "dirty";
33
- const savedContainerEvent = "saved";
34
- /**
35
- * Waits until container connects to delta storage and gets up-to-date
36
- * Useful when resolving URIs and hitting 404, due to container being loaded from (stale) snapshot and not being
37
- * up to date. Host may chose to wait in such case and retry resolving URI.
38
- * Warning: Will wait infinitely for connection to establish if there is no connection.
39
- * May result in deadlock if Container.disconnect() is called and never followed by a call to Container.connect().
40
- * @returns true: container is up to date, it processed all the ops that were know at the time of first connection
41
- * false: storage does not provide indication of how far the client is. Container processed
42
- * all the ops known to it, but it maybe still behind.
43
- * @throws an error beginning with `"Container closed"` if the container is closed before it catches up.
44
- */
45
- export async function waitContainerToCatchUp(container) {
46
- // Make sure we stop waiting if container is closed.
47
- if (container.closed) {
48
- throw new UsageError("waitContainerToCatchUp: Container closed");
49
- }
50
- return new Promise((resolve, reject) => {
51
- const deltaManager = container.deltaManager;
52
- const closedCallback = (err) => {
53
- container.off("closed", closedCallback);
54
- const baseMessage = "Container closed while waiting to catch up";
55
- reject(err !== undefined
56
- ? wrapError(err, (innerMessage) => new GenericError(`${baseMessage}: ${innerMessage}`))
57
- : new GenericError(baseMessage));
58
- };
59
- container.on("closed", closedCallback);
60
- const waitForOps = () => {
61
- assert(container.connectionState === ConnectionState.CatchingUp
62
- || container.connectionState === ConnectionState.Connected, 0x0cd /* "Container disconnected while waiting for ops!" */);
63
- const hasCheckpointSequenceNumber = deltaManager.hasCheckpointSequenceNumber;
64
- const connectionOpSeqNumber = deltaManager.lastKnownSeqNumber;
65
- assert(deltaManager.lastSequenceNumber <= connectionOpSeqNumber, 0x266 /* "lastKnownSeqNumber should never be below last processed sequence number" */);
66
- if (deltaManager.lastSequenceNumber === connectionOpSeqNumber) {
67
- container.off("closed", closedCallback);
68
- resolve(hasCheckpointSequenceNumber);
69
- return;
70
- }
71
- const callbackOps = (message) => {
72
- if (connectionOpSeqNumber <= message.sequenceNumber) {
73
- container.off("closed", closedCallback);
74
- resolve(hasCheckpointSequenceNumber);
75
- deltaManager.off("op", callbackOps);
76
- }
77
- };
78
- deltaManager.on("op", callbackOps);
79
- };
80
- // We can leverage DeltaManager's "connect" event here and test for ConnectionState.Disconnected
81
- // But that works only if service provides us checkPointSequenceNumber
82
- // Our internal testing is based on R11S that does not, but almost all tests connect as "write" and
83
- // use this function to catch up, so leveraging our own join op as a fence/barrier
84
- if (container.connectionState === ConnectionState.Connected) {
85
- waitForOps();
86
- return;
87
- }
88
- const callback = () => {
89
- container.off(connectedEventName, callback);
90
- waitForOps();
91
- };
92
- container.on(connectedEventName, callback);
93
- if (container.connectionState === ConnectionState.Disconnected) {
94
- container.connect();
95
- }
96
- });
97
- }
98
- const getCodeProposal =
99
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
100
- (quorum) => { var _a; return (_a = quorum.get("code")) !== null && _a !== void 0 ? _a : quorum.get("code2"); };
101
- /**
102
- * Helper function to report to telemetry cases where operation takes longer than expected (1s)
103
- * @param logger - logger to use
104
- * @param eventName - event name
105
- * @param action - functor to call and measure
106
- */
107
- async function ReportIfTooLong(logger, eventName, action) {
108
- const event = PerformanceEvent.start(logger, { eventName });
109
- const props = await action();
110
- if (event.duration > 1000) {
111
- event.end(props);
112
- }
113
- }
114
- const summarizerClientType = "summarizer";
115
- export class Container extends EventEmitterWithErrorHandling {
116
- constructor(loader, config) {
117
- var _a, _b;
118
- super((name, error) => {
119
- this.mc.logger.sendErrorEvent({
120
- eventName: "ContainerEventHandlerException",
121
- name: typeof name === "string" ? name : undefined,
122
- }, error);
123
- });
124
- this.loader = loader;
125
- // Tells if container can reconnect on losing fist connection
126
- // If false, container gets closed on loss of connection.
127
- this._canReconnect = true;
128
- this._lifecycleState = "loading";
129
- this._attachState = AttachState.Detached;
130
- /** During initialization we pause the inbound queues. We track this state to ensure we only call resume once */
131
- this.inboundQueuePausedFromInit = true;
132
- this.firstConnection = true;
133
- this.connectionTransitionTimes = [];
134
- this.messageCountAfterDisconnection = 0;
135
- this.attachStarted = false;
136
- this._dirtyContainer = false;
137
- this.setAutoReconnectTime = performance.now();
138
- this._audience = new Audience();
139
- this.clientDetailsOverride = config.clientDetailsOverride;
140
- this._resolvedUrl = config.resolvedUrl;
141
- if (config.canReconnect !== undefined) {
142
- this._canReconnect = config.canReconnect;
143
- }
144
- // Create logger for data stores to use
145
- const type = this.client.details.type;
146
- const interactive = this.client.details.capabilities.interactive;
147
- const clientType = `${interactive ? "interactive" : "noninteractive"}${type !== undefined && type !== "" ? `/${type}` : ""}`;
148
- // Need to use the property getter for docId because for detached flow we don't have the docId initially.
149
- // We assign the id later so property getter is used.
150
- this.subLogger = ChildLogger.create(loader.services.subLogger, undefined, {
151
- all: {
152
- clientType,
153
- containerId: uuid(),
154
- docId: () => { var _a, _b; return (_b = (_a = this._resolvedUrl) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : undefined; },
155
- containerAttachState: () => this._attachState,
156
- containerLifecycleState: () => this._lifecycleState,
157
- containerConnectionState: () => ConnectionState[this.connectionState],
158
- serializedContainer: config.serializedContainerState !== undefined,
159
- },
160
- // we need to be judicious with our logging here to avoid generating too much data
161
- // all data logged here should be broadly applicable, and not specific to a
162
- // specific error or class of errors
163
- error: {
164
- // load information to associate errors with the specific load point
165
- dmInitialSeqNumber: () => { var _a; return (_a = this._deltaManager) === null || _a === void 0 ? void 0 : _a.initialSequenceNumber; },
166
- dmLastProcessedSeqNumber: () => { var _a; return (_a = this._deltaManager) === null || _a === void 0 ? void 0 : _a.lastSequenceNumber; },
167
- dmLastKnownSeqNumber: () => { var _a; return (_a = this._deltaManager) === null || _a === void 0 ? void 0 : _a.lastKnownSeqNumber; },
168
- containerLoadedFromVersionId: () => { var _a; return (_a = this.loadedFromVersion) === null || _a === void 0 ? void 0 : _a.id; },
169
- containerLoadedFromVersionDate: () => { var _a; return (_a = this.loadedFromVersion) === null || _a === void 0 ? void 0 : _a.date; },
170
- // message information to associate errors with the specific execution state
171
- // dmLastMsqSeqNumber: if present, same as dmLastProcessedSeqNumber
172
- dmLastMsqSeqNumber: () => { var _a, _b; return (_b = (_a = this.deltaManager) === null || _a === void 0 ? void 0 : _a.lastMessage) === null || _b === void 0 ? void 0 : _b.sequenceNumber; },
173
- dmLastMsqSeqTimestamp: () => { var _a, _b; return (_b = (_a = this.deltaManager) === null || _a === void 0 ? void 0 : _a.lastMessage) === null || _b === void 0 ? void 0 : _b.timestamp; },
174
- dmLastMsqSeqClientId: () => { var _a, _b; return (_b = (_a = this.deltaManager) === null || _a === void 0 ? void 0 : _a.lastMessage) === null || _b === void 0 ? void 0 : _b.clientId; },
175
- connectionStateDuration: () => performance.now() - this.connectionTransitionTimes[this.connectionState],
176
- },
177
- });
178
- // Prefix all events in this file with container-loader
179
- this.mc = loggerToMonitoringContext(ChildLogger.create(this.subLogger, "Container"));
180
- const summarizeProtocolTree = (_a = this.mc.config.getBoolean("Fluid.Container.summarizeProtocolTree")) !== null && _a !== void 0 ? _a : this.loader.services.options.summarizeProtocolTree;
181
- this.options = Object.assign(Object.assign({}, this.loader.services.options), { summarizeProtocolTree });
182
- this.connectionStateHandler = new ConnectionStateHandler({
183
- quorumClients: () => { var _a; return (_a = this._protocolHandler) === null || _a === void 0 ? void 0 : _a.quorum; },
184
- logConnectionStateChangeTelemetry: (value, oldState, reason) => this.logConnectionStateChangeTelemetry(value, oldState, reason),
185
- shouldClientJoinWrite: () => this._deltaManager.connectionManager.shouldJoinWrite(),
186
- maxClientLeaveWaitTime: this.loader.services.options.maxClientLeaveWaitTime,
187
- logConnectionIssue: (eventName, details) => {
188
- // We get here when socket does not receive any ops on "write" connection, including
189
- // its own join op. Attempt recovery option.
190
- this._deltaManager.logConnectionIssue(Object.assign({ eventName, duration: performance.now() - this.connectionTransitionTimes[ConnectionState.CatchingUp] }, (details === undefined ? {} : { details: JSON.stringify(details) })));
191
- },
192
- connectionStateChanged: () => {
193
- // Fire events only if container is fully loaded and not closed
194
- if (this._lifecycleState === "loaded") {
195
- this.propagateConnectionState();
196
- }
197
- },
198
- }, this.mc.logger, (_b = config.serializedContainerState) === null || _b === void 0 ? void 0 : _b.clientId);
199
- this.on(savedContainerEvent, () => {
200
- this.connectionStateHandler.containerSaved();
201
- });
202
- this._deltaManager = this.createDeltaManager();
203
- this._storage = new ContainerStorageAdapter(() => {
204
- if (this.attachState !== AttachState.Attached) {
205
- if (this.loader.services.detachedBlobStorage !== undefined) {
206
- return new BlobOnlyStorage(this.loader.services.detachedBlobStorage, this.mc.logger);
207
- }
208
- this.mc.logger.sendErrorEvent({
209
- eventName: "NoRealStorageInDetachedContainer",
210
- });
211
- throw new Error("Real storage calls not allowed in Unattached container");
212
- }
213
- return this.storageService;
214
- });
215
- const isDomAvailable = typeof document === "object" &&
216
- document !== null &&
217
- typeof document.addEventListener === "function" &&
218
- document.addEventListener !== null;
219
- // keep track of last time page was visible for telemetry
220
- if (isDomAvailable) {
221
- this.lastVisible = document.hidden ? performance.now() : undefined;
222
- this.visibilityEventHandler = () => {
223
- if (document.hidden) {
224
- this.lastVisible = performance.now();
225
- }
226
- else {
227
- // settimeout so this will hopefully fire after disconnect event if being hidden caused it
228
- setTimeout(() => { this.lastVisible = undefined; }, 0);
229
- }
230
- };
231
- document.addEventListener("visibilitychange", this.visibilityEventHandler);
232
- }
233
- // We observed that most users of platform do not check Container.connected event on load, causing bugs.
234
- // As such, we are raising events when new listener pops up.
235
- // Note that we can raise both "disconnected" & "connect" events at the same time,
236
- // if we are in connecting stage.
237
- this.on("newListener", (event, listener) => {
238
- // Fire events on the end of JS turn, giving a chance for caller to be in consistent state.
239
- Promise.resolve().then(() => {
240
- switch (event) {
241
- case dirtyContainerEvent:
242
- if (this._dirtyContainer) {
243
- listener();
244
- }
245
- break;
246
- case savedContainerEvent:
247
- if (!this._dirtyContainer) {
248
- listener();
249
- }
250
- break;
251
- case connectedEventName:
252
- if (this.connected) {
253
- listener(this.clientId);
254
- }
255
- break;
256
- case disconnectedEventName:
257
- if (!this.connected) {
258
- listener();
259
- }
260
- break;
261
- default:
262
- }
263
- }).catch((error) => {
264
- this.mc.logger.sendErrorEvent({ eventName: "RaiseConnectedEventError" }, error);
265
- });
266
- });
267
- }
268
- /**
269
- * Load an existing container.
270
- */
271
- static async load(loader, loadOptions, pendingLocalState) {
272
- const container = new Container(loader, {
273
- clientDetailsOverride: loadOptions.clientDetailsOverride,
274
- resolvedUrl: loadOptions.resolvedUrl,
275
- canReconnect: loadOptions.canReconnect,
276
- serializedContainerState: pendingLocalState,
277
- });
278
- return PerformanceEvent.timedExecAsync(container.mc.logger, { eventName: "Load" }, async (event) => new Promise((resolve, reject) => {
279
- var _a, _b;
280
- const version = loadOptions.version;
281
- const defaultMode = { opsBeforeReturn: "cached" };
282
- // if we have pendingLocalState, anything we cached is not useful and we shouldn't wait for connection
283
- // to return container, so ignore this value and use undefined for opsBeforeReturn
284
- const mode = pendingLocalState
285
- ? Object.assign(Object.assign({}, ((_a = loadOptions.loadMode) !== null && _a !== void 0 ? _a : defaultMode)), { opsBeforeReturn: undefined }) : (_b = loadOptions.loadMode) !== null && _b !== void 0 ? _b : defaultMode;
286
- const onClosed = (err) => {
287
- // pre-0.58 error message: containerClosedWithoutErrorDuringLoad
288
- reject(err !== null && err !== void 0 ? err : new GenericError("Container closed without error during load"));
289
- };
290
- container.on("closed", onClosed);
291
- container.load(version, mode, pendingLocalState)
292
- .finally(() => {
293
- container.removeListener("closed", onClosed);
294
- })
295
- .then((props) => {
296
- event.end(Object.assign(Object.assign({}, props), loadOptions.loadMode));
297
- resolve(container);
298
- }, (error) => {
299
- const err = normalizeError(error);
300
- // Depending where error happens, we can be attempting to connect to web socket
301
- // and continuously retrying (consider offline mode)
302
- // Host has no container to close, so it's prudent to do it here
303
- container.close(err);
304
- onClosed(err);
305
- });
306
- }), { start: true, end: true, cancel: "generic" });
307
- }
308
- /**
309
- * Create a new container in a detached state.
310
- */
311
- static async createDetached(loader, codeDetails) {
312
- const container = new Container(loader, {});
313
- return PerformanceEvent.timedExecAsync(container.mc.logger, { eventName: "CreateDetached" }, async (_event) => {
314
- await container.createDetached(codeDetails);
315
- return container;
316
- }, { start: true, end: true, cancel: "generic" });
317
- }
318
- /**
319
- * Create a new container in a detached state that is initialized with a
320
- * snapshot from a previous detached container.
321
- */
322
- static async rehydrateDetachedFromSnapshot(loader, snapshot) {
323
- const container = new Container(loader, {});
324
- return PerformanceEvent.timedExecAsync(container.mc.logger, { eventName: "RehydrateDetachedFromSnapshot" }, async (_event) => {
325
- const deserializedSummary = JSON.parse(snapshot);
326
- await container.rehydrateDetachedFromSnapshot(deserializedSummary);
327
- return container;
328
- }, { start: true, end: true, cancel: "generic" });
329
- }
330
- setLoaded() {
331
- // It's conceivable the container could be closed when this is called
332
- // Only transition states if currently loading
333
- if (this._lifecycleState === "loading") {
334
- // Propagate current connection state through the system.
335
- this.propagateConnectionState();
336
- this._lifecycleState = "loaded";
337
- }
338
- }
339
- get closed() {
340
- return (this._lifecycleState === "closing" || this._lifecycleState === "closed");
341
- }
342
- get storage() {
343
- return this._storage;
344
- }
345
- get storageService() {
346
- if (this._storageService === undefined) {
347
- throw new Error("Attempted to access storageService before it was defined");
348
- }
349
- return this._storageService;
350
- }
351
- get context() {
352
- if (this._context === undefined) {
353
- throw new GenericError("Attempted to access context before it was defined");
354
- }
355
- return this._context;
356
- }
357
- get protocolHandler() {
358
- if (this._protocolHandler === undefined) {
359
- throw new Error("Attempted to access protocolHandler before it was defined");
360
- }
361
- return this._protocolHandler;
362
- }
363
- get connectionMode() { return this._deltaManager.connectionManager.connectionMode; }
364
- get IFluidRouter() { return this; }
365
- get resolvedUrl() {
366
- return this._resolvedUrl;
367
- }
368
- get loadedFromVersion() {
369
- return this._loadedFromVersion;
370
- }
371
- get readOnlyInfo() {
372
- return this._deltaManager.readOnlyInfo;
373
- }
374
- get closeSignal() {
375
- return this._deltaManager.closeAbortController.signal;
376
- }
377
- /**
378
- * Tracks host requiring read-only mode.
379
- */
380
- forceReadonly(readonly) {
381
- this._deltaManager.connectionManager.forceReadonly(readonly);
382
- }
383
- get deltaManager() {
384
- return this._deltaManager;
385
- }
386
- get connectionState() {
387
- return this.connectionStateHandler.connectionState;
388
- }
389
- get connected() {
390
- return this.connectionStateHandler.connected;
391
- }
392
- /**
393
- * Service configuration details. If running in offline mode will be undefined otherwise will contain service
394
- * configuration details returned as part of the initial connection.
395
- */
396
- get serviceConfiguration() {
397
- return this._deltaManager.serviceConfiguration;
398
- }
399
- /**
400
- * The server provided id of the client.
401
- * Set once this.connected is true, otherwise undefined
402
- */
403
- get clientId() {
404
- return this.connectionStateHandler.clientId;
405
- }
406
- /**
407
- * The server provided claims of the client.
408
- * Set once this.connected is true, otherwise undefined
409
- */
410
- get scopes() {
411
- return this._deltaManager.connectionManager.scopes;
412
- }
413
- get clientDetails() {
414
- return this._deltaManager.clientDetails;
415
- }
416
- /**
417
- * Get the code details that are currently specified for the container.
418
- * @returns The current code details if any are specified, undefined if none are specified.
419
- */
420
- getSpecifiedCodeDetails() {
421
- return this.getCodeDetailsFromQuorum();
422
- }
423
- /**
424
- * Get the code details that were used to load the container.
425
- * @returns The code details that were used to load the container if it is loaded, undefined if it is not yet
426
- * loaded.
427
- */
428
- getLoadedCodeDetails() {
429
- var _a;
430
- return (_a = this._context) === null || _a === void 0 ? void 0 : _a.codeDetails;
431
- }
432
- /**
433
- * Retrieves the audience associated with the document
434
- */
435
- get audience() {
436
- return this._audience;
437
- }
438
- /**
439
- * Returns true if container is dirty.
440
- * Which means data loss if container is closed at that same moment
441
- * Most likely that happens when there is no network connection to ordering service
442
- */
443
- get isDirty() {
444
- return this._dirtyContainer;
445
- }
446
- get serviceFactory() { return this.loader.services.documentServiceFactory; }
447
- get urlResolver() { return this.loader.services.urlResolver; }
448
- get scope() { return this.loader.services.scope; }
449
- get codeLoader() { return this.loader.services.codeLoader; }
450
- /**
451
- * Retrieves the quorum associated with the document
452
- */
453
- getQuorum() {
454
- return this.protocolHandler.quorum;
455
- }
456
- close(error) {
457
- // 1. Ensure that close sequence is exactly the same no matter if it's initiated by host or by DeltaManager
458
- // 2. We need to ensure that we deliver disconnect event to runtime properly. See connectionStateChanged
459
- // handler. We only deliver events if container fully loaded. Transitioning from "loading" ->
460
- // "closing" will lose that info (can also solve by tracking extra state).
461
- this._deltaManager.close(error);
462
- assert(this.connectionState === ConnectionState.Disconnected, 0x0cf /* "disconnect event was not raised!" */);
463
- assert(this._lifecycleState === "closed", 0x314 /* Container properly closed */);
464
- }
465
- closeCore(error) {
466
- var _a, _b, _c, _d;
467
- assert(!this.closed, 0x315 /* re-entrancy */);
468
- try {
469
- // Ensure that we raise all key events even if one of these throws
470
- try {
471
- // Raise event first, to ensure we capture _lifecycleState before transition.
472
- // This gives us a chance to know what errors happened on open vs. on fully loaded container.
473
- this.mc.logger.sendTelemetryEvent({
474
- eventName: "ContainerClose",
475
- category: error === undefined ? "generic" : "error",
476
- }, error);
477
- this._lifecycleState = "closing";
478
- (_a = this._protocolHandler) === null || _a === void 0 ? void 0 : _a.close();
479
- this.connectionStateHandler.dispose();
480
- (_b = this._context) === null || _b === void 0 ? void 0 : _b.dispose(error !== undefined ? new Error(error.message) : undefined);
481
- (_c = this._storageService) === null || _c === void 0 ? void 0 : _c.dispose();
482
- // Notify storage about critical errors. They may be due to disconnect between client & server knowledge
483
- // about file, like file being overwritten in storage, but client having stale local cache.
484
- // Driver need to ensure all caches are cleared on critical errors
485
- (_d = this.service) === null || _d === void 0 ? void 0 : _d.dispose(error);
486
- }
487
- catch (exception) {
488
- this.mc.logger.sendErrorEvent({ eventName: "ContainerCloseException" }, exception);
489
- }
490
- this.emit("closed", error);
491
- this.removeAllListeners();
492
- if (this.visibilityEventHandler !== undefined) {
493
- document.removeEventListener("visibilitychange", this.visibilityEventHandler);
494
- }
495
- }
496
- finally {
497
- this._lifecycleState = "closed";
498
- }
499
- }
500
- closeAndGetPendingLocalState() {
501
- // runtime matches pending ops to successful ones by clientId and client seq num, so we need to close the
502
- // container at the same time we get pending state, otherwise this container could reconnect and resubmit with
503
- // a new clientId and a future container using stale pending state without the new clientId would resubmit them
504
- assert(this.attachState === AttachState.Attached, 0x0d1 /* "Container should be attached before close" */);
505
- assert(this.resolvedUrl !== undefined && this.resolvedUrl.type === "fluid", 0x0d2 /* "resolved url should be valid Fluid url" */);
506
- assert(!!this._protocolHandler, 0x2e3 /* "Must have a valid protocol handler instance" */);
507
- assert(this._protocolHandler.attributes.term !== undefined, 0x30b /* Must have a valid protocol handler instance */);
508
- const pendingState = {
509
- pendingRuntimeState: this.context.getPendingLocalState(),
510
- url: this.resolvedUrl.url,
511
- protocol: this.protocolHandler.getProtocolState(),
512
- term: this._protocolHandler.attributes.term,
513
- clientId: this.clientId,
514
- };
515
- this.close();
516
- return JSON.stringify(pendingState);
517
- }
518
- get attachState() {
519
- return this._attachState;
520
- }
521
- serialize() {
522
- assert(this.attachState === AttachState.Detached, 0x0d3 /* "Should only be called in detached container" */);
523
- const appSummary = this.context.createSummary();
524
- const protocolSummary = this.captureProtocolSummary();
525
- const combinedSummary = combineAppAndProtocolSummary(appSummary, protocolSummary);
526
- if (this.loader.services.detachedBlobStorage && this.loader.services.detachedBlobStorage.size > 0) {
527
- combinedSummary.tree[".hasAttachmentBlobs"] = { type: SummaryType.Blob, content: "true" };
528
- }
529
- return JSON.stringify(combinedSummary);
530
- }
531
- async attach(request) {
532
- await PerformanceEvent.timedExecAsync(this.mc.logger, { eventName: "Attach" }, async () => {
533
- if (this._lifecycleState !== "loaded") {
534
- // pre-0.58 error message: containerNotValidForAttach
535
- throw new UsageError(`The Container is not in a valid state for attach [${this._lifecycleState}]`);
536
- }
537
- // If container is already attached or attach is in progress, throw an error.
538
- assert(this._attachState === AttachState.Detached && !this.attachStarted, 0x205 /* "attach() called more than once" */);
539
- this.attachStarted = true;
540
- // If attachment blobs were uploaded in detached state we will go through a different attach flow
541
- const hasAttachmentBlobs = this.loader.services.detachedBlobStorage !== undefined
542
- && this.loader.services.detachedBlobStorage.size > 0;
543
- try {
544
- assert(this.deltaManager.inbound.length === 0, 0x0d6 /* "Inbound queue should be empty when attaching" */);
545
- let summary;
546
- if (!hasAttachmentBlobs) {
547
- // Get the document state post attach - possibly can just call attach but we need to change the
548
- // semantics around what the attach means as far as async code goes.
549
- const appSummary = this.context.createSummary();
550
- const protocolSummary = this.captureProtocolSummary();
551
- summary = combineAppAndProtocolSummary(appSummary, protocolSummary);
552
- // Set the state as attaching as we are starting the process of attaching container.
553
- // This should be fired after taking the summary because it is the place where we are
554
- // starting to attach the container to storage.
555
- // Also, this should only be fired in detached container.
556
- this._attachState = AttachState.Attaching;
557
- this.context.notifyAttaching(getSnapshotTreeFromSerializedContainer(summary));
558
- }
559
- // Actually go and create the resolved document
560
- const createNewResolvedUrl = await this.urlResolver.resolve(request);
561
- ensureFluidResolvedUrl(createNewResolvedUrl);
562
- if (this.service === undefined) {
563
- assert(this.client.details.type !== summarizerClientType, 0x2c4 /* "client should not be summarizer before container is created" */);
564
- this.service = await runWithRetry(async () => this.serviceFactory.createContainer(summary, createNewResolvedUrl, this.subLogger, false), "containerAttach", this.mc.logger, {
565
- cancel: this.closeSignal,
566
- });
567
- }
568
- const resolvedUrl = this.service.resolvedUrl;
569
- ensureFluidResolvedUrl(resolvedUrl);
570
- this._resolvedUrl = resolvedUrl;
571
- await this.connectStorageService();
572
- if (hasAttachmentBlobs) {
573
- // upload blobs to storage
574
- assert(!!this.loader.services.detachedBlobStorage, 0x24e /* "assertion for type narrowing" */);
575
- // build a table mapping IDs assigned locally to IDs assigned by storage and pass it to runtime to
576
- // support blob handles that only know about the local IDs
577
- const redirectTable = new Map();
578
- // if new blobs are added while uploading, upload them too
579
- while (redirectTable.size < this.loader.services.detachedBlobStorage.size) {
580
- const newIds = this.loader.services.detachedBlobStorage.getBlobIds().filter((id) => !redirectTable.has(id));
581
- for (const id of newIds) {
582
- const blob = await this.loader.services.detachedBlobStorage.readBlob(id);
583
- const response = await this.storageService.createBlob(blob);
584
- redirectTable.set(id, response.id);
585
- }
586
- }
587
- // take summary and upload
588
- const appSummary = this.context.createSummary(redirectTable);
589
- const protocolSummary = this.captureProtocolSummary();
590
- summary = combineAppAndProtocolSummary(appSummary, protocolSummary);
591
- this._attachState = AttachState.Attaching;
592
- this.context.notifyAttaching(getSnapshotTreeFromSerializedContainer(summary));
593
- await this.storageService.uploadSummaryWithContext(summary, {
594
- referenceSequenceNumber: 0,
595
- ackHandle: undefined,
596
- proposalHandle: undefined,
597
- });
598
- }
599
- this._attachState = AttachState.Attached;
600
- this.emit("attached");
601
- // Propagate current connection state through the system.
602
- this.propagateConnectionState();
603
- if (!this.closed) {
604
- this.resumeInternal({ fetchOpsFromStorage: false, reason: "createDetached" });
605
- }
606
- }
607
- catch (error) {
608
- // add resolved URL on error object so that host has the ability to find this document and delete it
609
- const newError = normalizeError(error);
610
- const resolvedUrl = this.resolvedUrl;
611
- if (isFluidResolvedUrl(resolvedUrl)) {
612
- newError.addTelemetryProperties({ resolvedUrl: resolvedUrl.url });
613
- }
614
- this.close(newError);
615
- throw newError;
616
- }
617
- }, { start: true, end: true, cancel: "generic" });
618
- }
619
- async request(path) {
620
- return PerformanceEvent.timedExecAsync(this.mc.logger, { eventName: "Request" }, async () => this.context.request(path), { end: true, cancel: "error" });
621
- }
622
- setAutoReconnectInternal(mode) {
623
- const currentMode = this._deltaManager.connectionManager.reconnectMode;
624
- if (currentMode === mode) {
625
- return;
626
- }
627
- const now = performance.now();
628
- const duration = now - this.setAutoReconnectTime;
629
- this.setAutoReconnectTime = now;
630
- this.mc.logger.sendTelemetryEvent({
631
- eventName: mode === ReconnectMode.Enabled ? "AutoReconnectEnabled" : "AutoReconnectDisabled",
632
- connectionMode: this.connectionMode,
633
- connectionState: ConnectionState[this.connectionState],
634
- duration,
635
- });
636
- this._deltaManager.connectionManager.setAutoReconnect(mode);
637
- }
638
- connect() {
639
- if (this.closed) {
640
- throw new UsageError(`The Container is closed and cannot be connected`);
641
- }
642
- else if (this._attachState !== AttachState.Attached) {
643
- throw new UsageError(`The Container is not attached and cannot be connected`);
644
- }
645
- else if (!this.connected) {
646
- // Note: no need to fetch ops as we do it preemptively as part of DeltaManager.attachOpHandler().
647
- // If there is gap, we will learn about it once connected, but the gap should be small (if any),
648
- // assuming that connect() is called quickly after initial container boot.
649
- this.connectInternal({ reason: "DocumentConnect", fetchOpsFromStorage: false });
650
- }
651
- }
652
- connectInternal(args) {
653
- assert(!this.closed, 0x2c5 /* "Attempting to connect() a closed Container" */);
654
- assert(this._attachState === AttachState.Attached, 0x2c6 /* "Attempting to connect() a container that is not attached" */);
655
- // Resume processing ops and connect to delta stream
656
- this.resumeInternal(args);
657
- // Set Auto Reconnect Mode
658
- const mode = ReconnectMode.Enabled;
659
- this.setAutoReconnectInternal(mode);
660
- }
661
- disconnect() {
662
- if (this.closed) {
663
- throw new UsageError(`The Container is closed and cannot be disconnected`);
664
- }
665
- else {
666
- this.disconnectInternal();
667
- }
668
- }
669
- disconnectInternal() {
670
- assert(!this.closed, 0x2c7 /* "Attempting to disconnect() a closed Container" */);
671
- // Set Auto Reconnect Mode
672
- const mode = ReconnectMode.Disabled;
673
- this.setAutoReconnectInternal(mode);
674
- }
675
- resumeInternal(args) {
676
- assert(!this.closed, 0x0d9 /* "Attempting to connect() a closed DeltaManager" */);
677
- // Resume processing ops
678
- if (this.inboundQueuePausedFromInit) {
679
- this.inboundQueuePausedFromInit = false;
680
- this._deltaManager.inbound.resume();
681
- this._deltaManager.inboundSignal.resume();
682
- }
683
- // Ensure connection to web socket
684
- this.connectToDeltaStream(args);
685
- }
686
- async getAbsoluteUrl(relativeUrl) {
687
- var _a;
688
- if (this.resolvedUrl === undefined) {
689
- return undefined;
690
- }
691
- return this.urlResolver.getAbsoluteUrl(this.resolvedUrl, relativeUrl, getPackageName((_a = this._context) === null || _a === void 0 ? void 0 : _a.codeDetails));
692
- }
693
- async proposeCodeDetails(codeDetails) {
694
- if (!isFluidCodeDetails(codeDetails)) {
695
- throw new Error("Provided codeDetails are not IFluidCodeDetails");
696
- }
697
- if (this.codeLoader.IFluidCodeDetailsComparer) {
698
- const comparison = await this.codeLoader.IFluidCodeDetailsComparer.compare(codeDetails, this.getCodeDetailsFromQuorum());
699
- if (comparison !== undefined && comparison <= 0) {
700
- throw new Error("Proposed code details should be greater than the current");
701
- }
702
- }
703
- return this.protocolHandler.quorum.propose("code", codeDetails)
704
- .then(() => true)
705
- .catch(() => false);
706
- }
707
- async processCodeProposal() {
708
- const codeDetails = this.getCodeDetailsFromQuorum();
709
- await Promise.all([
710
- this.deltaManager.inbound.pause(),
711
- this.deltaManager.inboundSignal.pause()
712
- ]);
713
- if ((await this.context.satisfies(codeDetails) === true)) {
714
- this.deltaManager.inbound.resume();
715
- this.deltaManager.inboundSignal.resume();
716
- return;
717
- }
718
- // pre-0.58 error message: existingContextDoesNotSatisfyIncomingProposal
719
- this.close(new GenericError("Existing context does not satisfy incoming proposal"));
720
- }
721
- async getVersion(version) {
722
- const versions = await this.storageService.getVersions(version, 1);
723
- return versions[0];
724
- }
725
- recordConnectStartTime() {
726
- if (this.connectionTransitionTimes[ConnectionState.Disconnected] === undefined) {
727
- this.connectionTransitionTimes[ConnectionState.Disconnected] = performance.now();
728
- }
729
- }
730
- connectToDeltaStream(args) {
731
- this.recordConnectStartTime();
732
- // All agents need "write" access, including summarizer.
733
- if (!this._canReconnect || !this.client.details.capabilities.interactive) {
734
- args.mode = "write";
735
- }
736
- this._deltaManager.connect(args);
737
- }
738
- /**
739
- * Load container.
740
- *
741
- * @param specifiedVersion - one of the following
742
- * - undefined - fetch latest snapshot
743
- * - otherwise, version sha to load snapshot
744
- */
745
- async load(specifiedVersion, loadMode, pendingLocalState) {
746
- if (this._resolvedUrl === undefined) {
747
- throw new Error("Attempting to load without a resolved url");
748
- }
749
- this.service = await this.serviceFactory.createDocumentService(this._resolvedUrl, this.subLogger, this.client.details.type === summarizerClientType);
750
- // Ideally we always connect as "read" by default.
751
- // Currently that works with SPO & r11s, because we get "write" connection when connecting to non-existing file.
752
- // We should not rely on it by (one of them will address the issue, but we need to address both)
753
- // 1) switching create new flow to one where we create file by posting snapshot
754
- // 2) Fixing quorum workflows (have retry logic)
755
- // That all said, "read" does not work with memorylicious workflows (that opens two simultaneous
756
- // connections to same file) in two ways:
757
- // A) creation flow breaks (as one of the clients "sees" file as existing, and hits #2 above)
758
- // B) Once file is created, transition from view-only connection to write does not work - some bugs to be fixed.
759
- const connectionArgs = { reason: "DocumentOpen", mode: "write", fetchOpsFromStorage: false };
760
- // Start websocket connection as soon as possible. Note that there is no op handler attached yet, but the
761
- // DeltaManager is resilient to this and will wait to start processing ops until after it is attached.
762
- if (loadMode.deltaConnection === undefined) {
763
- this.connectToDeltaStream(connectionArgs);
764
- }
765
- if (!pendingLocalState) {
766
- await this.connectStorageService();
767
- }
768
- else {
769
- // if we have pendingLocalState we can load without storage; don't wait for connection
770
- this.connectStorageService().catch((error) => this.close(error));
771
- }
772
- this._attachState = AttachState.Attached;
773
- // Fetch specified snapshot.
774
- const { snapshot, versionId } = pendingLocalState === undefined
775
- ? await this.fetchSnapshotTree(specifiedVersion)
776
- : { snapshot: undefined, versionId: undefined };
777
- assert(snapshot !== undefined || pendingLocalState !== undefined, 0x237 /* "Snapshot should exist" */);
778
- const attributes = pendingLocalState === undefined
779
- ? await this.getDocumentAttributes(this.storageService, snapshot)
780
- : {
781
- sequenceNumber: pendingLocalState.protocol.sequenceNumber,
782
- minimumSequenceNumber: pendingLocalState.protocol.minimumSequenceNumber,
783
- term: pendingLocalState.term,
784
- };
785
- let opsBeforeReturnP;
786
- // Attach op handlers to finish initialization and be able to start processing ops
787
- // Kick off any ops fetching if required.
788
- switch (loadMode.opsBeforeReturn) {
789
- case undefined:
790
- // Start prefetch, but not set opsBeforeReturnP - boot is not blocked by it!
791
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
792
- this.attachDeltaManagerOpHandler(attributes, loadMode.deltaConnection !== "none" ? "all" : "none");
793
- break;
794
- case "cached":
795
- opsBeforeReturnP = this.attachDeltaManagerOpHandler(attributes, "cached");
796
- break;
797
- case "all":
798
- opsBeforeReturnP = this.attachDeltaManagerOpHandler(attributes, "all");
799
- break;
800
- default:
801
- unreachableCase(loadMode.opsBeforeReturn);
802
- }
803
- // ...load in the existing quorum
804
- // Initialize the protocol handler
805
- this._protocolHandler = pendingLocalState === undefined
806
- ? await this.initializeProtocolStateFromSnapshot(attributes, this.storageService, snapshot)
807
- : await this.initializeProtocolState(attributes, pendingLocalState.protocol.members, pendingLocalState.protocol.proposals, pendingLocalState.protocol.values);
808
- const codeDetails = this.getCodeDetailsFromQuorum();
809
- await this.instantiateContext(true, // existing
810
- codeDetails, snapshot, pendingLocalState === null || pendingLocalState === void 0 ? void 0 : pendingLocalState.pendingRuntimeState);
811
- // We might have hit some failure that did not manifest itself in exception in this flow,
812
- // do not start op processing in such case - static version of Container.load() will handle it correctly.
813
- if (!this.closed) {
814
- if (opsBeforeReturnP !== undefined) {
815
- this._deltaManager.inbound.resume();
816
- await ReportIfTooLong(this.mc.logger, "WaitOps", async () => { await opsBeforeReturnP; return {}; });
817
- await ReportIfTooLong(this.mc.logger, "WaitOpProcessing", async () => this._deltaManager.inbound.waitTillProcessingDone());
818
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
819
- this._deltaManager.inbound.pause();
820
- }
821
- switch (loadMode.deltaConnection) {
822
- case undefined:
823
- case "delayed":
824
- assert(this.inboundQueuePausedFromInit, 0x346 /* inboundQueuePausedFromInit should be true */);
825
- this.inboundQueuePausedFromInit = false;
826
- this._deltaManager.inbound.resume();
827
- this._deltaManager.inboundSignal.resume();
828
- break;
829
- case "none":
830
- break;
831
- default:
832
- unreachableCase(loadMode.deltaConnection);
833
- }
834
- }
835
- // Safety net: static version of Container.load() should have learned about it through "closed" handler.
836
- // But if that did not happen for some reason, fail load for sure.
837
- // Otherwise we can get into situations where container is closed and does not try to connect to ordering
838
- // service, but caller does not know that (callers do expect container to be not closed on successful path
839
- // and listen only on "closed" event)
840
- if (this.closed) {
841
- throw new Error("Container was closed while load()");
842
- }
843
- // Internal context is fully loaded at this point
844
- this.setLoaded();
845
- return {
846
- sequenceNumber: attributes.sequenceNumber,
847
- version: versionId,
848
- dmLastProcessedSeqNumber: this._deltaManager.lastSequenceNumber,
849
- dmLastKnownSeqNumber: this._deltaManager.lastKnownSeqNumber,
850
- };
851
- }
852
- async createDetached(source) {
853
- const attributes = {
854
- sequenceNumber: detachedContainerRefSeqNumber,
855
- term: 1,
856
- minimumSequenceNumber: 0,
857
- };
858
- await this.attachDeltaManagerOpHandler(attributes);
859
- // Need to just seed the source data in the code quorum. Quorum itself is empty
860
- const qValues = initQuorumValuesFromCodeDetails(source);
861
- this._protocolHandler = await this.initializeProtocolState(attributes, [], // members
862
- [], // proposals
863
- qValues);
864
- // The load context - given we seeded the quorum - will be great
865
- await this.instantiateContextDetached(false);
866
- this.setLoaded();
867
- }
868
- async rehydrateDetachedFromSnapshot(detachedContainerSnapshot) {
869
- if (detachedContainerSnapshot.tree[".hasAttachmentBlobs"] !== undefined) {
870
- assert(!!this.loader.services.detachedBlobStorage && this.loader.services.detachedBlobStorage.size > 0, 0x250 /* "serialized container with attachment blobs must be rehydrated with detached blob storage" */);
871
- delete detachedContainerSnapshot.tree[".hasAttachmentBlobs"];
872
- }
873
- const snapshotTree = getSnapshotTreeFromSerializedContainer(detachedContainerSnapshot);
874
- this._storage.loadSnapshotForRehydratingContainer(snapshotTree);
875
- const attributes = await this.getDocumentAttributes(this._storage, snapshotTree);
876
- await this.attachDeltaManagerOpHandler(attributes);
877
- // Initialize the protocol handler
878
- const baseTree = getProtocolSnapshotTree(snapshotTree);
879
- const qValues = await readAndParse(this._storage, baseTree.blobs.quorumValues);
880
- const codeDetails = getCodeDetailsFromQuorumValues(qValues);
881
- this._protocolHandler =
882
- await this.initializeProtocolState(attributes, [], // members
883
- [], // proposals
884
- codeDetails !== undefined ? initQuorumValuesFromCodeDetails(codeDetails) : []);
885
- await this.instantiateContextDetached(true, // existing
886
- snapshotTree);
887
- this.setLoaded();
888
- }
889
- async connectStorageService() {
890
- var _a, _b;
891
- if (this._storageService !== undefined) {
892
- return;
893
- }
894
- assert(this.service !== undefined, 0x1ef /* "services must be defined" */);
895
- const storageService = await this.service.connectToStorage();
896
- this._storageService =
897
- new RetriableDocumentStorageService(storageService, this.mc.logger);
898
- if (this.options.summarizeProtocolTree === true) {
899
- this.mc.logger.sendTelemetryEvent({ eventName: "summarizeProtocolTreeEnabled" });
900
- this._storageService =
901
- new ProtocolTreeStorageService(this._storageService, () => this.captureProtocolSummary());
902
- }
903
- // ensure we did not lose that policy in the process of wrapping
904
- assert(((_a = storageService.policies) === null || _a === void 0 ? void 0 : _a.minBlobSize) === ((_b = this.storageService.policies) === null || _b === void 0 ? void 0 : _b.minBlobSize), 0x0e0 /* "lost minBlobSize policy" */);
905
- }
906
- async getDocumentAttributes(storage, tree) {
907
- if (tree === undefined) {
908
- return {
909
- minimumSequenceNumber: 0,
910
- sequenceNumber: 0,
911
- term: 1,
912
- };
913
- }
914
- // Backward compatibility: old docs would have ".attributes" instead of "attributes"
915
- const attributesHash = ".protocol" in tree.trees
916
- ? tree.trees[".protocol"].blobs.attributes
917
- : tree.blobs[".attributes"];
918
- const attributes = await readAndParse(storage, attributesHash);
919
- // Backward compatibility for older summaries with no term
920
- if (attributes.term === undefined) {
921
- attributes.term = 1;
922
- }
923
- return attributes;
924
- }
925
- async initializeProtocolStateFromSnapshot(attributes, storage, snapshot) {
926
- let members = [];
927
- let proposals = [];
928
- let values = [];
929
- if (snapshot !== undefined) {
930
- const baseTree = getProtocolSnapshotTree(snapshot);
931
- [members, proposals, values] = await Promise.all([
932
- readAndParse(storage, baseTree.blobs.quorumMembers),
933
- readAndParse(storage, baseTree.blobs.quorumProposals),
934
- readAndParse(storage, baseTree.blobs.quorumValues),
935
- ]);
936
- }
937
- const protocolHandler = await this.initializeProtocolState(attributes, members, proposals, values);
938
- return protocolHandler;
939
- }
940
- async initializeProtocolState(attributes, members, proposals, values) {
941
- const protocol = new ProtocolOpHandlerWithClientValidation(attributes.minimumSequenceNumber, attributes.sequenceNumber, attributes.term, members, proposals, values, (key, value) => this.submitMessage(MessageType.Propose, { key, value }));
942
- const protocolLogger = ChildLogger.create(this.subLogger, "ProtocolHandler");
943
- protocol.quorum.on("error", (error) => {
944
- protocolLogger.sendErrorEvent(error);
945
- });
946
- // Track membership changes and update connection state accordingly
947
- this.connectionStateHandler.initProtocol(protocol);
948
- protocol.quorum.on("addProposal", (proposal) => {
949
- if (proposal.key === "code" || proposal.key === "code2") {
950
- this.emit("codeDetailsProposed", proposal.value, proposal);
951
- }
952
- });
953
- protocol.quorum.on("approveProposal", (sequenceNumber, key, value) => {
954
- if (key === "code" || key === "code2") {
955
- if (!isFluidCodeDetails(value)) {
956
- this.mc.logger.sendErrorEvent({
957
- eventName: "CodeProposalNotIFluidCodeDetails",
958
- });
959
- }
960
- this.processCodeProposal().catch((error) => {
961
- this.close(normalizeError(error));
962
- throw error;
963
- });
964
- }
965
- });
966
- return protocol;
967
- }
968
- captureProtocolSummary() {
969
- const quorumSnapshot = this.protocolHandler.snapshot();
970
- const summary = {
971
- tree: {
972
- attributes: {
973
- content: JSON.stringify(this.protocolHandler.attributes),
974
- type: SummaryType.Blob,
975
- },
976
- quorumMembers: {
977
- content: JSON.stringify(quorumSnapshot.members),
978
- type: SummaryType.Blob,
979
- },
980
- quorumProposals: {
981
- content: JSON.stringify(quorumSnapshot.proposals),
982
- type: SummaryType.Blob,
983
- },
984
- quorumValues: {
985
- content: JSON.stringify(quorumSnapshot.values),
986
- type: SummaryType.Blob,
987
- },
988
- },
989
- type: SummaryType.Tree,
990
- };
991
- return summary;
992
- }
993
- getCodeDetailsFromQuorum() {
994
- const quorum = this.protocolHandler.quorum;
995
- const pkg = getCodeProposal(quorum);
996
- return pkg;
997
- }
998
- get client() {
999
- var _a;
1000
- const client = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.client) !== undefined
1001
- ? this.options.client
1002
- : {
1003
- details: {
1004
- capabilities: { interactive: true },
1005
- },
1006
- mode: "read",
1007
- permission: [],
1008
- scopes: [],
1009
- user: { id: "" },
1010
- };
1011
- if (this.clientDetailsOverride !== undefined) {
1012
- merge(client.details, this.clientDetailsOverride);
1013
- }
1014
- client.details.environment = [client.details.environment, ` loaderVersion:${pkgVersion}`].join(";");
1015
- return client;
1016
- }
1017
- /**
1018
- * Returns true if connection is active, i.e. it's "write" connection and
1019
- * container runtime was notified about this connection (i.e. we are up-to-date and could send ops).
1020
- * This happens after client received its own joinOp and thus is in the quorum.
1021
- * If it's not true, runtime is not in position to send ops.
1022
- */
1023
- activeConnection() {
1024
- return this.connectionState === ConnectionState.Connected &&
1025
- this.connectionMode === "write";
1026
- }
1027
- createDeltaManager() {
1028
- const serviceProvider = () => this.service;
1029
- const deltaManager = new DeltaManager(serviceProvider, ChildLogger.create(this.subLogger, "DeltaManager"), () => this.activeConnection(), (props) => new ConnectionManager(serviceProvider, this.client, this._canReconnect, ChildLogger.create(this.subLogger, "ConnectionManager"), props));
1030
- // Disable inbound queues as Container is not ready to accept any ops until we are fully loaded!
1031
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
1032
- deltaManager.inbound.pause();
1033
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
1034
- deltaManager.inboundSignal.pause();
1035
- deltaManager.on("connect", (details, opsBehind) => {
1036
- var _a;
1037
- // Back-compat for new client and old server.
1038
- this._audience.clear();
1039
- for (const priorClient of (_a = details.initialClients) !== null && _a !== void 0 ? _a : []) {
1040
- this._audience.addMember(priorClient.clientId, priorClient.client);
1041
- }
1042
- this.connectionStateHandler.receivedConnectEvent(this.connectionMode, details);
1043
- });
1044
- deltaManager.on("disconnect", (reason) => {
1045
- var _a;
1046
- (_a = this.collabWindowTracker) === null || _a === void 0 ? void 0 : _a.stopSequenceNumberUpdate();
1047
- this.connectionStateHandler.receivedDisconnectEvent(reason);
1048
- });
1049
- deltaManager.on("throttled", (warning) => {
1050
- const warn = warning;
1051
- // Some "warning" events come from outside the container and are logged
1052
- // elsewhere (e.g. summarizing container). We shouldn't log these here.
1053
- if (warn.logged !== true) {
1054
- this.logContainerError(warn);
1055
- }
1056
- this.emit("warning", warn);
1057
- });
1058
- deltaManager.on("readonly", (readonly) => {
1059
- this.emit("readonly", readonly);
1060
- });
1061
- deltaManager.on("closed", (error) => {
1062
- this.closeCore(error);
1063
- });
1064
- return deltaManager;
1065
- }
1066
- async attachDeltaManagerOpHandler(attributes, prefetchType) {
1067
- var _a;
1068
- return this._deltaManager.attachOpHandler(attributes.minimumSequenceNumber, attributes.sequenceNumber, (_a = attributes.term) !== null && _a !== void 0 ? _a : 1, {
1069
- process: (message) => this.processRemoteMessage(message),
1070
- processSignal: (message) => {
1071
- this.processSignal(message);
1072
- },
1073
- }, prefetchType);
1074
- }
1075
- logConnectionStateChangeTelemetry(value, oldState, reason) {
1076
- var _a;
1077
- // Log actual event
1078
- const time = performance.now();
1079
- this.connectionTransitionTimes[value] = time;
1080
- const duration = time - this.connectionTransitionTimes[oldState];
1081
- let durationFromDisconnected;
1082
- let connectionInitiationReason;
1083
- let autoReconnect;
1084
- let checkpointSequenceNumber;
1085
- let opsBehind;
1086
- if (value === ConnectionState.Disconnected) {
1087
- autoReconnect = this._deltaManager.connectionManager.reconnectMode;
1088
- }
1089
- else {
1090
- if (value === ConnectionState.Connected) {
1091
- durationFromDisconnected = time - this.connectionTransitionTimes[ConnectionState.Disconnected];
1092
- durationFromDisconnected = TelemetryLogger.formatTick(durationFromDisconnected);
1093
- }
1094
- else {
1095
- // This info is of most interest on establishing connection only.
1096
- checkpointSequenceNumber = this.deltaManager.lastKnownSeqNumber;
1097
- if (this.deltaManager.hasCheckpointSequenceNumber) {
1098
- opsBehind = checkpointSequenceNumber - this.deltaManager.lastSequenceNumber;
1099
- }
1100
- }
1101
- if (this.firstConnection) {
1102
- connectionInitiationReason = "InitialConnect";
1103
- }
1104
- else {
1105
- connectionInitiationReason = "AutoReconnect";
1106
- }
1107
- }
1108
- this.mc.logger.sendPerformanceEvent(Object.assign({ eventName: `ConnectionStateChange_${ConnectionState[value]}`, from: ConnectionState[oldState], duration,
1109
- durationFromDisconnected,
1110
- reason,
1111
- connectionInitiationReason, pendingClientId: this.connectionStateHandler.pendingClientId, clientId: this.clientId, autoReconnect,
1112
- opsBehind, online: OnlineStatus[isOnline()], lastVisible: this.lastVisible !== undefined ? performance.now() - this.lastVisible : undefined, checkpointSequenceNumber, quorumSize: (_a = this._protocolHandler) === null || _a === void 0 ? void 0 : _a.quorum.getMembers().size }, this._deltaManager.connectionProps));
1113
- if (value === ConnectionState.Connected) {
1114
- this.firstConnection = false;
1115
- }
1116
- }
1117
- propagateConnectionState() {
1118
- var _a;
1119
- const logOpsOnReconnect = this.connectionState === ConnectionState.Connected &&
1120
- !this.firstConnection &&
1121
- this.connectionMode === "write";
1122
- if (logOpsOnReconnect) {
1123
- this.messageCountAfterDisconnection = 0;
1124
- }
1125
- const state = this.connectionState === ConnectionState.Connected;
1126
- // Both protocol and context should not be undefined if we got so far.
1127
- if (((_a = this._context) === null || _a === void 0 ? void 0 : _a.disposed) === false) {
1128
- this.context.setConnectionState(state, this.clientId);
1129
- }
1130
- this.protocolHandler.setConnectionState(state, this.clientId);
1131
- raiseConnectedEvent(this.mc.logger, this, state, this.clientId);
1132
- if (logOpsOnReconnect) {
1133
- this.mc.logger.sendTelemetryEvent({ eventName: "OpsSentOnReconnect", count: this.messageCountAfterDisconnection });
1134
- }
1135
- }
1136
- submitContainerMessage(type, contents, batch, metadata) {
1137
- const outboundMessageType = type;
1138
- switch (outboundMessageType) {
1139
- case MessageType.Operation:
1140
- case MessageType.RemoteHelp:
1141
- break;
1142
- case MessageType.Summarize: {
1143
- // github #6451: this is only needed for staging so the server
1144
- // know when the protocol tree is included
1145
- // this can be removed once all clients send
1146
- // protocol tree by default
1147
- const summary = contents;
1148
- if (summary.details === undefined) {
1149
- summary.details = {};
1150
- }
1151
- summary.details.includesProtocolTree =
1152
- this.options.summarizeProtocolTree === true;
1153
- break;
1154
- }
1155
- default:
1156
- this.close(new GenericError("invalidContainerSubmitOpType", undefined /* error */, { messageType: type }));
1157
- return -1;
1158
- }
1159
- return this.submitMessage(type, contents, batch, metadata);
1160
- }
1161
- submitMessage(type, contents, batch, metadata) {
1162
- var _a;
1163
- if (this.connectionState !== ConnectionState.Connected) {
1164
- this.mc.logger.sendErrorEvent({ eventName: "SubmitMessageWithNoConnection", type });
1165
- return -1;
1166
- }
1167
- this.messageCountAfterDisconnection += 1;
1168
- (_a = this.collabWindowTracker) === null || _a === void 0 ? void 0 : _a.stopSequenceNumberUpdate();
1169
- return this._deltaManager.submit(type, contents, batch, metadata);
1170
- }
1171
- processRemoteMessage(message) {
1172
- const local = this.clientId === message.clientId;
1173
- // Allow the protocol handler to process the message
1174
- let result = { immediateNoOp: false };
1175
- try {
1176
- result = this.protocolHandler.processMessage(message, local);
1177
- }
1178
- catch (error) {
1179
- this.close(wrapError(error, (errorMessage) => new DataCorruptionError(errorMessage, extractSafePropertiesFromMessage(message))));
1180
- }
1181
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1182
- if (isUnpackedRuntimeMessage(message) && !isRuntimeMessage(message)) {
1183
- this.mc.logger.sendTelemetryEvent({ eventName: "UnpackedRuntimeMessage", type: message.type });
1184
- }
1185
- // Forward non system messages to the loaded runtime for processing
1186
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1187
- if (isRuntimeMessage(message) || isUnpackedRuntimeMessage(message)) {
1188
- this.context.process(message, local, undefined);
1189
- }
1190
- // Inactive (not in quorum or not writers) clients don't take part in the minimum sequence number calculation.
1191
- if (this.activeConnection()) {
1192
- if (this.collabWindowTracker === undefined) {
1193
- // Note that config from first connection will be used for this container's lifetime.
1194
- // That means that if relay service changes settings, such changes will impact only newly booted
1195
- // clients.
1196
- // All existing will continue to use settings they got earlier.
1197
- assert(this.serviceConfiguration !== undefined, 0x2e4 /* "there should be service config for active connection" */);
1198
- this.collabWindowTracker = new CollabWindowTracker((type, contents) => {
1199
- assert(this.activeConnection(), 0x241 /* "disconnect should result in stopSequenceNumberUpdate() call" */);
1200
- this.submitMessage(type, contents);
1201
- }, this.serviceConfiguration.noopTimeFrequency, this.serviceConfiguration.noopCountFrequency);
1202
- }
1203
- this.collabWindowTracker.scheduleSequenceNumberUpdate(message, result.immediateNoOp === true);
1204
- }
1205
- this.emit("op", message);
1206
- return result;
1207
- }
1208
- submitSignal(message) {
1209
- this._deltaManager.submitSignal(JSON.stringify(message));
1210
- }
1211
- processSignal(message) {
1212
- // No clientId indicates a system signal message.
1213
- if (message.clientId === null) {
1214
- const innerContent = message.content;
1215
- if (innerContent.type === MessageType.ClientJoin) {
1216
- const newClient = innerContent.content;
1217
- this._audience.addMember(newClient.clientId, newClient.client);
1218
- }
1219
- else if (innerContent.type === MessageType.ClientLeave) {
1220
- const leftClientId = innerContent.content;
1221
- this._audience.removeMember(leftClientId);
1222
- }
1223
- }
1224
- else {
1225
- const local = this.clientId === message.clientId;
1226
- this.context.processSignal(message, local);
1227
- }
1228
- }
1229
- /**
1230
- * Get the most recent snapshot, or a specific version.
1231
- * @param specifiedVersion - The specific version of the snapshot to retrieve
1232
- * @returns The snapshot requested, or the latest snapshot if no version was specified, plus version ID
1233
- */
1234
- async fetchSnapshotTree(specifiedVersion) {
1235
- var _a;
1236
- const version = await this.getVersion(specifiedVersion !== null && specifiedVersion !== void 0 ? specifiedVersion : null);
1237
- if (version === undefined && specifiedVersion !== undefined) {
1238
- // We should have a defined version to load from if specified version requested
1239
- this.mc.logger.sendErrorEvent({ eventName: "NoVersionFoundWhenSpecified", id: specifiedVersion });
1240
- }
1241
- this._loadedFromVersion = version;
1242
- const snapshot = (_a = await this.storageService.getSnapshotTree(version)) !== null && _a !== void 0 ? _a : undefined;
1243
- if (snapshot === undefined && version !== undefined) {
1244
- this.mc.logger.sendErrorEvent({ eventName: "getSnapshotTreeFailed", id: version.id });
1245
- }
1246
- return { snapshot, versionId: version === null || version === void 0 ? void 0 : version.id };
1247
- }
1248
- async instantiateContextDetached(existing, snapshot) {
1249
- const codeDetails = this.getCodeDetailsFromQuorum();
1250
- if (codeDetails === undefined) {
1251
- throw new Error("pkg should be provided in create flow!!");
1252
- }
1253
- await this.instantiateContext(existing, codeDetails, snapshot);
1254
- }
1255
- async instantiateContext(existing, codeDetails, snapshot, pendingLocalState) {
1256
- var _a;
1257
- assert(((_a = this._context) === null || _a === void 0 ? void 0 : _a.disposed) !== false, 0x0dd /* "Existing context not disposed" */);
1258
- // The relative loader will proxy requests to '/' to the loader itself assuming no non-cache flags
1259
- // are set. Global requests will still go directly to the loader
1260
- const loader = new RelativeLoader(this, this.loader);
1261
- this._context = await ContainerContext.createOrLoad(this, this.scope, this.codeLoader, codeDetails, snapshot, new DeltaManagerProxy(this._deltaManager), new QuorumProxy(this.protocolHandler.quorum), loader, (type, contents, batch, metadata) => this.submitContainerMessage(type, contents, batch, metadata), (message) => this.submitSignal(message), (error) => this.close(error), Container.version, (dirty) => this.updateDirtyContainerState(dirty), existing, pendingLocalState);
1262
- this.emit("contextChanged", codeDetails);
1263
- }
1264
- updateDirtyContainerState(dirty) {
1265
- if (this._dirtyContainer === dirty) {
1266
- return;
1267
- }
1268
- this._dirtyContainer = dirty;
1269
- this.emit(dirty ? dirtyContainerEvent : savedContainerEvent);
1270
- }
1271
- logContainerError(warning) {
1272
- this.mc.logger.sendErrorEvent({ eventName: "ContainerWarning" }, warning);
1273
- }
1274
- }
1275
- Container.version = "^0.1.0";
1276
- //# sourceMappingURL=container.js.map