@fluidframework/routerlicious-driver 2.0.0-dev.2.3.0.115467 → 2.0.0-dev.4.1.0.148229

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 (170) hide show
  1. package/.eslintrc.js +10 -12
  2. package/.mocharc.js +2 -2
  3. package/README.md +1 -1
  4. package/api-extractor.json +2 -2
  5. package/dist/cache.d.ts +3 -0
  6. package/dist/cache.d.ts.map +1 -1
  7. package/dist/cache.js +6 -4
  8. package/dist/cache.js.map +1 -1
  9. package/dist/createNewUtils.d.ts.map +1 -1
  10. package/dist/createNewUtils.js +4 -2
  11. package/dist/createNewUtils.js.map +1 -1
  12. package/dist/defaultTokenProvider.d.ts.map +1 -1
  13. package/dist/defaultTokenProvider.js.map +1 -1
  14. package/dist/definitions.d.ts.map +1 -1
  15. package/dist/definitions.js.map +1 -1
  16. package/dist/deltaStorageService.d.ts.map +1 -1
  17. package/dist/deltaStorageService.js +4 -1
  18. package/dist/deltaStorageService.js.map +1 -1
  19. package/dist/documentDeltaConnection.d.ts.map +1 -1
  20. package/dist/documentDeltaConnection.js.map +1 -1
  21. package/dist/documentService.d.ts +4 -2
  22. package/dist/documentService.d.ts.map +1 -1
  23. package/dist/documentService.js +28 -41
  24. package/dist/documentService.js.map +1 -1
  25. package/dist/documentServiceFactory.d.ts +0 -1
  26. package/dist/documentServiceFactory.d.ts.map +1 -1
  27. package/dist/documentServiceFactory.js +26 -13
  28. package/dist/documentServiceFactory.js.map +1 -1
  29. package/dist/documentStorageService.d.ts +1 -1
  30. package/dist/documentStorageService.d.ts.map +1 -1
  31. package/dist/documentStorageService.js +8 -6
  32. package/dist/documentStorageService.js.map +1 -1
  33. package/dist/errorUtils.d.ts +11 -3
  34. package/dist/errorUtils.d.ts.map +1 -1
  35. package/dist/errorUtils.js +19 -6
  36. package/dist/errorUtils.js.map +1 -1
  37. package/dist/index.d.ts +2 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +4 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/mapWithExpiration.d.ts +34 -0
  42. package/dist/mapWithExpiration.d.ts.map +1 -0
  43. package/dist/mapWithExpiration.js +105 -0
  44. package/dist/mapWithExpiration.js.map +1 -0
  45. package/dist/nullBlobStorageService.d.ts.map +1 -1
  46. package/dist/nullBlobStorageService.js.map +1 -1
  47. package/dist/packageVersion.d.ts +1 -1
  48. package/dist/packageVersion.js +1 -1
  49. package/dist/packageVersion.js.map +1 -1
  50. package/dist/policies.d.ts.map +1 -1
  51. package/dist/policies.js.map +1 -1
  52. package/dist/restWrapper.d.ts +8 -5
  53. package/dist/restWrapper.d.ts.map +1 -1
  54. package/dist/restWrapper.js +38 -44
  55. package/dist/restWrapper.js.map +1 -1
  56. package/dist/retriableGitManager.d.ts.map +1 -1
  57. package/dist/retriableGitManager.js.map +1 -1
  58. package/dist/shreddedSummaryDocumentStorageService.d.ts +1 -1
  59. package/dist/shreddedSummaryDocumentStorageService.d.ts.map +1 -1
  60. package/dist/shreddedSummaryDocumentStorageService.js +9 -5
  61. package/dist/shreddedSummaryDocumentStorageService.js.map +1 -1
  62. package/dist/tokens.d.ts +24 -7
  63. package/dist/tokens.d.ts.map +1 -1
  64. package/dist/tokens.js.map +1 -1
  65. package/dist/treeUtils.d.ts +51 -0
  66. package/dist/treeUtils.d.ts.map +1 -0
  67. package/dist/treeUtils.js +85 -0
  68. package/dist/treeUtils.js.map +1 -0
  69. package/dist/urlUtils.d.ts.map +1 -1
  70. package/dist/urlUtils.js.map +1 -1
  71. package/dist/wholeSummaryDocumentStorageService.d.ts +1 -1
  72. package/dist/wholeSummaryDocumentStorageService.d.ts.map +1 -1
  73. package/dist/wholeSummaryDocumentStorageService.js +30 -17
  74. package/dist/wholeSummaryDocumentStorageService.js.map +1 -1
  75. package/lib/cache.d.ts +3 -0
  76. package/lib/cache.d.ts.map +1 -1
  77. package/lib/cache.js +6 -4
  78. package/lib/cache.js.map +1 -1
  79. package/lib/createNewUtils.d.ts.map +1 -1
  80. package/lib/createNewUtils.js +4 -2
  81. package/lib/createNewUtils.js.map +1 -1
  82. package/lib/defaultTokenProvider.d.ts.map +1 -1
  83. package/lib/defaultTokenProvider.js.map +1 -1
  84. package/lib/definitions.d.ts.map +1 -1
  85. package/lib/definitions.js.map +1 -1
  86. package/lib/deltaStorageService.d.ts.map +1 -1
  87. package/lib/deltaStorageService.js +4 -1
  88. package/lib/deltaStorageService.js.map +1 -1
  89. package/lib/documentDeltaConnection.d.ts.map +1 -1
  90. package/lib/documentDeltaConnection.js.map +1 -1
  91. package/lib/documentService.d.ts +4 -2
  92. package/lib/documentService.d.ts.map +1 -1
  93. package/lib/documentService.js +30 -24
  94. package/lib/documentService.js.map +1 -1
  95. package/lib/documentServiceFactory.d.ts +0 -1
  96. package/lib/documentServiceFactory.d.ts.map +1 -1
  97. package/lib/documentServiceFactory.js +27 -14
  98. package/lib/documentServiceFactory.js.map +1 -1
  99. package/lib/documentStorageService.d.ts +1 -1
  100. package/lib/documentStorageService.d.ts.map +1 -1
  101. package/lib/documentStorageService.js +9 -7
  102. package/lib/documentStorageService.js.map +1 -1
  103. package/lib/errorUtils.d.ts +11 -3
  104. package/lib/errorUtils.d.ts.map +1 -1
  105. package/lib/errorUtils.js +18 -5
  106. package/lib/errorUtils.js.map +1 -1
  107. package/lib/index.d.ts +2 -1
  108. package/lib/index.d.ts.map +1 -1
  109. package/lib/index.js +3 -1
  110. package/lib/index.js.map +1 -1
  111. package/lib/mapWithExpiration.d.ts +34 -0
  112. package/lib/mapWithExpiration.d.ts.map +1 -0
  113. package/lib/mapWithExpiration.js +101 -0
  114. package/lib/mapWithExpiration.js.map +1 -0
  115. package/lib/nullBlobStorageService.d.ts.map +1 -1
  116. package/lib/nullBlobStorageService.js.map +1 -1
  117. package/lib/packageVersion.d.ts +1 -1
  118. package/lib/packageVersion.js +1 -1
  119. package/lib/packageVersion.js.map +1 -1
  120. package/lib/policies.d.ts.map +1 -1
  121. package/lib/policies.js.map +1 -1
  122. package/lib/restWrapper.d.ts +8 -5
  123. package/lib/restWrapper.d.ts.map +1 -1
  124. package/lib/restWrapper.js +38 -44
  125. package/lib/restWrapper.js.map +1 -1
  126. package/lib/retriableGitManager.d.ts.map +1 -1
  127. package/lib/retriableGitManager.js.map +1 -1
  128. package/lib/shreddedSummaryDocumentStorageService.d.ts +1 -1
  129. package/lib/shreddedSummaryDocumentStorageService.d.ts.map +1 -1
  130. package/lib/shreddedSummaryDocumentStorageService.js +10 -6
  131. package/lib/shreddedSummaryDocumentStorageService.js.map +1 -1
  132. package/lib/tokens.d.ts +24 -7
  133. package/lib/tokens.d.ts.map +1 -1
  134. package/lib/tokens.js.map +1 -1
  135. package/lib/treeUtils.d.ts +51 -0
  136. package/lib/treeUtils.d.ts.map +1 -0
  137. package/lib/treeUtils.js +80 -0
  138. package/lib/treeUtils.js.map +1 -0
  139. package/lib/urlUtils.d.ts.map +1 -1
  140. package/lib/urlUtils.js.map +1 -1
  141. package/lib/wholeSummaryDocumentStorageService.d.ts +1 -1
  142. package/lib/wholeSummaryDocumentStorageService.d.ts.map +1 -1
  143. package/lib/wholeSummaryDocumentStorageService.js +30 -17
  144. package/lib/wholeSummaryDocumentStorageService.js.map +1 -1
  145. package/package.json +55 -52
  146. package/prettier.config.cjs +1 -1
  147. package/src/cache.ts +21 -14
  148. package/src/createNewUtils.ts +24 -22
  149. package/src/defaultTokenProvider.ts +13 -15
  150. package/src/definitions.ts +2 -2
  151. package/src/deltaStorageService.ts +99 -95
  152. package/src/documentDeltaConnection.ts +53 -47
  153. package/src/documentService.ts +260 -241
  154. package/src/documentServiceFactory.ts +268 -237
  155. package/src/documentStorageService.ts +87 -83
  156. package/src/errorUtils.ts +91 -76
  157. package/src/index.ts +7 -1
  158. package/src/mapWithExpiration.ts +124 -0
  159. package/src/nullBlobStorageService.ts +24 -21
  160. package/src/packageVersion.ts +1 -1
  161. package/src/policies.ts +44 -44
  162. package/src/restWrapper.ts +270 -208
  163. package/src/retriableGitManager.ts +152 -151
  164. package/src/shreddedSummaryDocumentStorageService.ts +202 -194
  165. package/src/tokens.ts +69 -44
  166. package/src/treeUtils.ts +107 -0
  167. package/src/urlUtils.ts +26 -23
  168. package/src/wholeSummaryDocumentStorageService.ts +248 -228
  169. package/tsconfig.esnext.json +6 -6
  170. package/tsconfig.json +9 -13
@@ -5,11 +5,12 @@
5
5
 
6
6
  import { assert } from "@fluidframework/common-utils";
7
7
  import * as api from "@fluidframework/driver-definitions";
8
- import { RateLimiter } from "@fluidframework/driver-utils";
8
+ import { DriverErrorType } from "@fluidframework/driver-definitions";
9
+ import { RateLimiter, NetworkErrorBasic, canRetryOnError } from "@fluidframework/driver-utils";
9
10
  import { IClient } from "@fluidframework/protocol-definitions";
10
11
  import { GitManager, Historian, RestWrapper } from "@fluidframework/server-services-client";
11
12
  import io from "socket.io-client";
12
- import { PerformanceEvent } from "@fluidframework/telemetry-utils";
13
+ import { PerformanceEvent, wrapError } from "@fluidframework/telemetry-utils";
13
14
  import { ITelemetryLogger } from "@fluidframework/common-definitions";
14
15
  import { DeltaStorageService, DocumentDeltaStorageService } from "./deltaStorageService";
15
16
  import { DocumentStorageService } from "./documentStorageService";
@@ -20,6 +21,7 @@ import { RouterliciousOrdererRestWrapper, RouterliciousStorageRestWrapper } from
20
21
  import { IRouterliciousDriverPolicies } from "./policies";
21
22
  import { ICache } from "./cache";
22
23
  import { ISnapshotTreeVersion } from "./definitions";
24
+ import { pkgVersion as driverVersion } from "./packageVersion";
23
25
 
24
26
  /**
25
27
  * Amount of time between discoveries within which we don't need to rediscover on re-connect.
@@ -35,262 +37,279 @@ const RediscoverAfterTimeSinceDiscoveryMs = 5 * 60000; // 5 minute
35
37
  */
36
38
  // eslint-disable-next-line import/namespace
37
39
  export class DocumentService implements api.IDocumentService {
38
- private lastDiscoveredAt: number = Date.now();
39
- private discoverP: Promise<void> | undefined;
40
+ private lastDiscoveredAt: number = Date.now();
41
+ private discoverP: Promise<void> | undefined;
40
42
 
41
- private storageManager: GitManager | undefined;
42
- private noCacheStorageManager: GitManager | undefined;
43
- private ordererRestWrapper: RestWrapper | undefined;
43
+ private storageManager: GitManager | undefined;
44
+ private noCacheStorageManager: GitManager | undefined;
44
45
 
45
- public get resolvedUrl() {
46
- return this._resolvedUrl;
47
- }
46
+ public get resolvedUrl() {
47
+ return this._resolvedUrl;
48
+ }
48
49
 
49
- constructor(
50
- private _resolvedUrl: api.IResolvedUrl,
51
- protected ordererUrl: string,
52
- private deltaStorageUrl: string,
53
- private deltaStreamUrl: string,
54
- private storageUrl: string,
55
- private readonly logger: ITelemetryLogger,
56
- protected tokenProvider: ITokenProvider,
57
- protected tenantId: string,
58
- protected documentId: string,
59
- private readonly driverPolicies: IRouterliciousDriverPolicies,
60
- private readonly blobCache: ICache<ArrayBufferLike>,
61
- private readonly snapshotTreeCache: ICache<ISnapshotTreeVersion>,
62
- private readonly discoverFluidResolvedUrl: () => Promise<api.IFluidResolvedUrl>,
63
- ) {
64
- }
50
+ constructor(
51
+ private _resolvedUrl: api.IResolvedUrl,
52
+ protected ordererUrl: string,
53
+ private deltaStorageUrl: string,
54
+ private deltaStreamUrl: string,
55
+ private storageUrl: string,
56
+ private readonly logger: ITelemetryLogger,
57
+ protected tokenProvider: ITokenProvider,
58
+ protected tenantId: string,
59
+ protected documentId: string,
60
+ protected ordererRestWrapper: RouterliciousOrdererRestWrapper,
61
+ private readonly documentStorageServicePolicies: api.IDocumentStorageServicePolicies,
62
+ private readonly driverPolicies: IRouterliciousDriverPolicies,
63
+ private readonly blobCache: ICache<ArrayBufferLike>,
64
+ private readonly snapshotTreeCache: ICache<ISnapshotTreeVersion>,
65
+ private readonly discoverFluidResolvedUrl: () => Promise<api.IFluidResolvedUrl>,
66
+ ) {}
65
67
 
66
- private documentStorageService: DocumentStorageService | undefined;
68
+ private documentStorageService: DocumentStorageService | undefined;
67
69
 
68
- public dispose() { }
70
+ public dispose() {}
69
71
 
70
- /**
71
- * Connects to a storage endpoint for snapshot service.
72
- *
73
- * @returns returns the document storage service for routerlicious driver.
74
- */
75
- public async connectToStorage(): Promise<api.IDocumentStorageService> {
76
- if (this.documentStorageService !== undefined) {
77
- return this.documentStorageService;
78
- }
72
+ /**
73
+ * Connects to a storage endpoint for snapshot service.
74
+ *
75
+ * @returns returns the document storage service for routerlicious driver.
76
+ */
77
+ public async connectToStorage(): Promise<api.IDocumentStorageService> {
78
+ if (this.documentStorageService !== undefined) {
79
+ return this.documentStorageService;
80
+ }
79
81
 
80
- if (this.storageUrl === undefined) {
81
- return new NullBlobStorageService();
82
- }
82
+ if (this.storageUrl === undefined) {
83
+ return new NullBlobStorageService();
84
+ }
83
85
 
84
- const getStorageManager = async (disableCache?: boolean): Promise<GitManager> => {
85
- const shouldUpdateDiscoveredSessionInfo = this.shouldUpdateDiscoveredSessionInfo();
86
- if (shouldUpdateDiscoveredSessionInfo) {
87
- await this.refreshDiscovery();
88
- }
89
- if (!this.storageManager || !this.noCacheStorageManager || shouldUpdateDiscoveredSessionInfo) {
90
- const rateLimiter = new RateLimiter(this.driverPolicies.maxConcurrentStorageRequests);
91
- const storageRestWrapper = await RouterliciousStorageRestWrapper.load(
92
- this.tenantId,
93
- this.documentId,
94
- this.tokenProvider,
95
- this.logger,
96
- rateLimiter,
97
- this.driverPolicies.enableRestLess,
98
- this.storageUrl,
99
- );
100
- const historian = new Historian(
101
- this.storageUrl,
102
- true,
103
- false,
104
- storageRestWrapper);
105
- this.storageManager = new GitManager(historian);
106
- const noCacheHistorian = new Historian(
107
- this.storageUrl,
108
- true,
109
- true,
110
- storageRestWrapper);
111
- this.noCacheStorageManager = new GitManager(noCacheHistorian);
112
- }
86
+ const getStorageManager = async (disableCache?: boolean): Promise<GitManager> => {
87
+ const shouldUpdateDiscoveredSessionInfo = this.shouldUpdateDiscoveredSessionInfo();
88
+ if (shouldUpdateDiscoveredSessionInfo) {
89
+ await this.refreshDiscovery();
90
+ }
91
+ if (
92
+ !this.storageManager ||
93
+ !this.noCacheStorageManager ||
94
+ shouldUpdateDiscoveredSessionInfo
95
+ ) {
96
+ const rateLimiter = new RateLimiter(
97
+ this.driverPolicies.maxConcurrentStorageRequests,
98
+ );
99
+ const storageRestWrapper = await RouterliciousStorageRestWrapper.load(
100
+ this.tenantId,
101
+ this.documentId,
102
+ this.tokenProvider,
103
+ this.logger,
104
+ rateLimiter,
105
+ this.driverPolicies.enableRestLess,
106
+ this.storageUrl,
107
+ );
108
+ const historian = new Historian(this.storageUrl, true, false, storageRestWrapper);
109
+ this.storageManager = new GitManager(historian);
110
+ const noCacheHistorian = new Historian(
111
+ this.storageUrl,
112
+ true,
113
+ true,
114
+ storageRestWrapper,
115
+ );
116
+ this.noCacheStorageManager = new GitManager(noCacheHistorian);
117
+ }
113
118
 
114
- return disableCache ? this.noCacheStorageManager : this.storageManager;
115
- };
116
- // Initialize storageManager and noCacheStorageManager
117
- const storageManager = await getStorageManager();
118
- const noCacheStorageManager = await getStorageManager(true);
119
- const documentStorageServicePolicies: api.IDocumentStorageServicePolicies = {
120
- caching: this.driverPolicies.enablePrefetch
121
- ? api.LoaderCachingPolicy.Prefetch
122
- : api.LoaderCachingPolicy.NoCaching,
123
- minBlobSize: this.driverPolicies.aggregateBlobsSmallerThanBytes,
124
- };
119
+ return disableCache ? this.noCacheStorageManager : this.storageManager;
120
+ };
121
+ // Initialize storageManager and noCacheStorageManager
122
+ const storageManager = await getStorageManager();
123
+ const noCacheStorageManager = await getStorageManager(true);
124
+ this.documentStorageService = new DocumentStorageService(
125
+ this.documentId,
126
+ storageManager,
127
+ this.logger,
128
+ this.documentStorageServicePolicies,
129
+ this.driverPolicies,
130
+ this.blobCache,
131
+ this.snapshotTreeCache,
132
+ noCacheStorageManager,
133
+ getStorageManager,
134
+ );
135
+ return this.documentStorageService;
136
+ }
125
137
 
126
- this.documentStorageService = new DocumentStorageService(
127
- this.documentId,
128
- storageManager,
129
- this.logger,
130
- documentStorageServicePolicies,
131
- this.driverPolicies,
132
- this.blobCache,
133
- this.snapshotTreeCache,
134
- noCacheStorageManager,
135
- getStorageManager);
136
- return this.documentStorageService;
137
- }
138
+ /**
139
+ * Connects to a delta storage endpoint for getting ops between a range.
140
+ *
141
+ * @returns returns the document delta storage service for routerlicious driver.
142
+ */
143
+ public async connectToDeltaStorage(): Promise<api.IDocumentDeltaStorageService> {
144
+ await this.connectToStorage();
145
+ assert(!!this.documentStorageService, 0x0b1 /* "Storage service not initialized" */);
138
146
 
139
- /**
140
- * Connects to a delta storage endpoint for getting ops between a range.
141
- *
142
- * @returns returns the document delta storage service for routerlicious driver.
143
- */
144
- public async connectToDeltaStorage(): Promise<api.IDocumentDeltaStorageService> {
145
- await this.connectToStorage();
146
- assert(!!this.documentStorageService, 0x0b1 /* "Storage service not initialized" */);
147
+ const getRestWrapper = async (): Promise<RestWrapper> => {
148
+ const shouldUpdateDiscoveredSessionInfo = this.shouldUpdateDiscoveredSessionInfo();
147
149
 
148
- const getRestWrapper = async (): Promise<RestWrapper> => {
149
- const shouldUpdateDiscoveredSessionInfo = this.shouldUpdateDiscoveredSessionInfo();
150
- if (shouldUpdateDiscoveredSessionInfo) {
151
- await this.refreshDiscovery();
152
- }
153
- if (!this.ordererRestWrapper || shouldUpdateDiscoveredSessionInfo) {
154
- const rateLimiter = new RateLimiter(this.driverPolicies.maxConcurrentOrdererRequests);
155
- this.ordererRestWrapper = await RouterliciousOrdererRestWrapper.load(
156
- this.tenantId,
157
- this.documentId,
158
- this.tokenProvider,
159
- this.logger,
160
- rateLimiter,
161
- this.driverPolicies.enableRestLess,
162
- );
163
- }
164
- return this.ordererRestWrapper;
165
- };
166
- const restWrapper = await getRestWrapper();
167
- const deltaStorageService = new DeltaStorageService(
168
- this.deltaStorageUrl,
169
- restWrapper,
170
- this.logger,
171
- getRestWrapper,
172
- () => this.deltaStorageUrl,
173
- );
174
- return new DocumentDeltaStorageService(
175
- this.tenantId,
176
- this.documentId,
177
- deltaStorageService,
178
- this.documentStorageService,
179
- this.logger,
180
- );
181
- }
150
+ if (shouldUpdateDiscoveredSessionInfo) {
151
+ await this.refreshDiscovery();
152
+ const rateLimiter = new RateLimiter(
153
+ this.driverPolicies.maxConcurrentOrdererRequests,
154
+ );
155
+ this.ordererRestWrapper = await RouterliciousOrdererRestWrapper.load(
156
+ this.tenantId,
157
+ this.documentId,
158
+ this.tokenProvider,
159
+ this.logger,
160
+ rateLimiter,
161
+ this.driverPolicies.enableRestLess,
162
+ );
163
+ }
164
+ return this.ordererRestWrapper;
165
+ };
166
+ const restWrapper = await getRestWrapper();
167
+ const deltaStorageService = new DeltaStorageService(
168
+ this.deltaStorageUrl,
169
+ restWrapper,
170
+ this.logger,
171
+ getRestWrapper,
172
+ () => this.deltaStorageUrl,
173
+ );
174
+ return new DocumentDeltaStorageService(
175
+ this.tenantId,
176
+ this.documentId,
177
+ deltaStorageService,
178
+ this.documentStorageService,
179
+ this.logger,
180
+ );
181
+ }
182
182
 
183
- /**
184
- * Connects to a delta stream endpoint for emitting ops.
185
- *
186
- * @returns returns the document delta stream service for routerlicious driver.
187
- */
188
- public async connectToDeltaStream(client: IClient): Promise<api.IDocumentDeltaConnection> {
189
- const connect = async (refreshToken?: boolean) => {
190
- if (this.shouldUpdateDiscoveredSessionInfo()) {
191
- await this.refreshDiscovery();
192
- }
183
+ /**
184
+ * Connects to a delta stream endpoint for emitting ops.
185
+ *
186
+ * @returns returns the document delta stream service for routerlicious driver.
187
+ */
188
+ public async connectToDeltaStream(client: IClient): Promise<api.IDocumentDeltaConnection> {
189
+ const connect = async (refreshToken?: boolean) => {
190
+ let ordererToken = this.ordererRestWrapper.getToken();
191
+ if (this.shouldUpdateDiscoveredSessionInfo()) {
192
+ await this.refreshDiscovery();
193
+ }
193
194
 
194
- const ordererToken = await PerformanceEvent.timedExecAsync(
195
- this.logger,
196
- {
197
- eventName: "GetDeltaStreamToken",
198
- docId: this.documentId,
199
- details: JSON.stringify({
200
- refreshToken,
201
- }),
202
- },
203
- async () => {
204
- return this.tokenProvider.fetchOrdererToken(
205
- this.tenantId,
206
- this.documentId,
207
- refreshToken,
208
- );
209
- }
210
- );
195
+ if (refreshToken) {
196
+ ordererToken = await PerformanceEvent.timedExecAsync(
197
+ this.logger,
198
+ {
199
+ eventName: "GetDeltaStreamToken",
200
+ docId: this.documentId,
201
+ details: JSON.stringify({
202
+ refreshToken,
203
+ }),
204
+ },
205
+ async () =>
206
+ this.tokenProvider
207
+ .fetchOrdererToken(this.tenantId, this.documentId, refreshToken)
208
+ .then(
209
+ (newOrdererToken) => {
210
+ this.ordererRestWrapper.setToken(newOrdererToken);
211
+ return newOrdererToken;
212
+ },
213
+ (error) => {
214
+ const tokenError = wrapError(
215
+ error,
216
+ (errorMessage) =>
217
+ new NetworkErrorBasic(
218
+ `The Host-provided token fetcher threw an error`,
219
+ DriverErrorType.fetchTokenError,
220
+ canRetryOnError(error),
221
+ { errorMessage, driverVersion },
222
+ ),
223
+ );
224
+ throw tokenError;
225
+ },
226
+ ),
227
+ );
228
+ }
211
229
 
212
- return PerformanceEvent.timedExecAsync(
213
- this.logger,
214
- {
215
- eventName: "ConnectToDeltaStream",
216
- docId: this.documentId,
217
- },
218
- async () => {
219
- return R11sDocumentDeltaConnection.create(
220
- this.tenantId,
221
- this.documentId,
222
- ordererToken.jwt,
223
- io,
224
- client,
225
- this.deltaStreamUrl,
226
- this.logger,
227
- );
228
- }
229
- );
230
- };
230
+ return PerformanceEvent.timedExecAsync(
231
+ this.logger,
232
+ {
233
+ eventName: "ConnectToDeltaStream",
234
+ docId: this.documentId,
235
+ },
236
+ async () => {
237
+ return R11sDocumentDeltaConnection.create(
238
+ this.tenantId,
239
+ this.documentId,
240
+ ordererToken.jwt,
241
+ io,
242
+ client,
243
+ this.deltaStreamUrl,
244
+ this.logger,
245
+ );
246
+ },
247
+ );
248
+ };
231
249
 
232
- // Attempt to establish connection.
233
- // Retry with new token on authorization error; otherwise, allow container layer to handle.
234
- try {
235
- const connection = await connect();
236
- return connection;
237
- } catch (error: any) {
238
- if (error?.statusCode === 401) {
239
- // Fetch new token and retry once,
240
- // otherwise 401 will be bubbled up as non-retriable AuthorizationError.
241
- return connect(true /* refreshToken */);
242
- }
243
- throw error;
244
- }
245
- }
250
+ // Attempt to establish connection.
251
+ // Retry with new token on authorization error; otherwise, allow container layer to handle.
252
+ try {
253
+ const connection = await connect();
254
+ return connection;
255
+ } catch (error: any) {
256
+ if (error?.statusCode === 401) {
257
+ // Fetch new token and retry once,
258
+ // otherwise 401 will be bubbled up as non-retriable AuthorizationError.
259
+ return connect(true /* refreshToken */);
260
+ }
261
+ throw error;
262
+ }
263
+ }
246
264
 
247
- /**
248
- * Re-discover session URLs if necessary.
249
- */
250
- private async refreshDiscovery(): Promise<void> {
251
- if (!this.discoverP) {
252
- this.discoverP = PerformanceEvent.timedExecAsync(
253
- this.logger,
254
- {
255
- eventName: "RefreshDiscovery",
256
- },
257
- async () => this.refreshDiscoveryCore(),
258
- );
259
- }
260
- return this.discoverP;
261
- }
265
+ /**
266
+ * Re-discover session URLs if necessary.
267
+ */
268
+ private async refreshDiscovery(): Promise<void> {
269
+ if (!this.discoverP) {
270
+ this.discoverP = PerformanceEvent.timedExecAsync(
271
+ this.logger,
272
+ {
273
+ eventName: "RefreshDiscovery",
274
+ },
275
+ async () => this.refreshDiscoveryCore(),
276
+ );
277
+ }
278
+ return this.discoverP;
279
+ }
262
280
 
263
- private async refreshDiscoveryCore(): Promise<void> {
264
- const fluidResolvedUrl = await this.discoverFluidResolvedUrl();
265
- this._resolvedUrl = fluidResolvedUrl;
266
- this.storageUrl = fluidResolvedUrl.endpoints.storageUrl;
267
- this.ordererUrl = fluidResolvedUrl.endpoints.ordererUrl;
268
- this.deltaStorageUrl = fluidResolvedUrl.endpoints.deltaStorageUrl;
269
- this.deltaStreamUrl = fluidResolvedUrl.endpoints.deltaStreamUrl || this.ordererUrl;
270
- }
281
+ private async refreshDiscoveryCore(): Promise<void> {
282
+ const fluidResolvedUrl = await this.discoverFluidResolvedUrl();
283
+ this._resolvedUrl = fluidResolvedUrl;
284
+ this.storageUrl = fluidResolvedUrl.endpoints.storageUrl;
285
+ this.ordererUrl = fluidResolvedUrl.endpoints.ordererUrl;
286
+ this.deltaStorageUrl = fluidResolvedUrl.endpoints.deltaStorageUrl;
287
+ this.deltaStreamUrl = fluidResolvedUrl.endpoints.deltaStreamUrl || this.ordererUrl;
288
+ }
271
289
 
272
- /**
273
- * Whether enough time has passed since last disconnect to warrant a new discovery call on reconnect.
274
- */
275
- private shouldUpdateDiscoveredSessionInfo(): boolean {
276
- if (!this.driverPolicies.enableDiscovery) {
277
- return false;
278
- }
279
- const now = Date.now();
280
- // When connection is disconnected, we cannot know if session has moved or document has been deleted
281
- // without re-doing discovery on the next attempt to connect.
282
- // Disconnect event is not so reliable in local testing. To ensure re-discovery when necessary,
283
- // re-discover if enough time has passed since last discovery.
284
- const pastLastDiscoveryTimeThreshold = (now - this.lastDiscoveredAt) > RediscoverAfterTimeSinceDiscoveryMs;
285
- if (pastLastDiscoveryTimeThreshold) {
286
- // Reset discover promise and refresh discovery.
287
- this.lastDiscoveredAt = Date.now();
288
- this.discoverP = undefined;
289
- this.refreshDiscovery().catch(() => {
290
- // Undo discovery time set on failure, so that next check refreshes.
291
- this.lastDiscoveredAt = 0;
292
- });
293
- }
294
- return pastLastDiscoveryTimeThreshold;
295
- }
290
+ /**
291
+ * Whether enough time has passed since last disconnect to warrant a new discovery call on reconnect.
292
+ */
293
+ private shouldUpdateDiscoveredSessionInfo(): boolean {
294
+ if (!this.driverPolicies.enableDiscovery) {
295
+ return false;
296
+ }
297
+ const now = Date.now();
298
+ // When connection is disconnected, we cannot know if session has moved or document has been deleted
299
+ // without re-doing discovery on the next attempt to connect.
300
+ // Disconnect event is not so reliable in local testing. To ensure re-discovery when necessary,
301
+ // re-discover if enough time has passed since last discovery.
302
+ const pastLastDiscoveryTimeThreshold =
303
+ now - this.lastDiscoveredAt > RediscoverAfterTimeSinceDiscoveryMs;
304
+ if (pastLastDiscoveryTimeThreshold) {
305
+ // Reset discover promise and refresh discovery.
306
+ this.lastDiscoveredAt = Date.now();
307
+ this.discoverP = undefined;
308
+ this.refreshDiscovery().catch(() => {
309
+ // Undo discovery time set on failure, so that next check refreshes.
310
+ this.lastDiscoveredAt = 0;
311
+ });
312
+ }
313
+ return pastLastDiscoveryTimeThreshold;
314
+ }
296
315
  }