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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) 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 +22 -41
  24. package/dist/documentService.js.map +1 -1
  25. package/dist/documentServiceFactory.d.ts.map +1 -1
  26. package/dist/documentServiceFactory.js +23 -9
  27. package/dist/documentServiceFactory.js.map +1 -1
  28. package/dist/documentStorageService.d.ts +1 -1
  29. package/dist/documentStorageService.d.ts.map +1 -1
  30. package/dist/documentStorageService.js +8 -6
  31. package/dist/documentStorageService.js.map +1 -1
  32. package/dist/errorUtils.d.ts +2 -1
  33. package/dist/errorUtils.d.ts.map +1 -1
  34. package/dist/errorUtils.js +6 -0
  35. package/dist/errorUtils.js.map +1 -1
  36. package/dist/index.d.ts +1 -1
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js.map +1 -1
  39. package/dist/nullBlobStorageService.d.ts.map +1 -1
  40. package/dist/nullBlobStorageService.js.map +1 -1
  41. package/dist/packageVersion.d.ts +1 -1
  42. package/dist/packageVersion.js +1 -1
  43. package/dist/packageVersion.js.map +1 -1
  44. package/dist/policies.d.ts.map +1 -1
  45. package/dist/policies.js.map +1 -1
  46. package/dist/restWrapper.d.ts +8 -5
  47. package/dist/restWrapper.d.ts.map +1 -1
  48. package/dist/restWrapper.js +38 -44
  49. package/dist/restWrapper.js.map +1 -1
  50. package/dist/retriableGitManager.d.ts.map +1 -1
  51. package/dist/retriableGitManager.js.map +1 -1
  52. package/dist/shreddedSummaryDocumentStorageService.d.ts +1 -1
  53. package/dist/shreddedSummaryDocumentStorageService.d.ts.map +1 -1
  54. package/dist/shreddedSummaryDocumentStorageService.js +9 -5
  55. package/dist/shreddedSummaryDocumentStorageService.js.map +1 -1
  56. package/dist/tokens.d.ts.map +1 -1
  57. package/dist/tokens.js.map +1 -1
  58. package/dist/urlUtils.d.ts.map +1 -1
  59. package/dist/urlUtils.js.map +1 -1
  60. package/dist/wholeSummaryDocumentStorageService.d.ts +1 -1
  61. package/dist/wholeSummaryDocumentStorageService.d.ts.map +1 -1
  62. package/dist/wholeSummaryDocumentStorageService.js +25 -11
  63. package/dist/wholeSummaryDocumentStorageService.js.map +1 -1
  64. package/lib/cache.d.ts +3 -0
  65. package/lib/cache.d.ts.map +1 -1
  66. package/lib/cache.js +6 -4
  67. package/lib/cache.js.map +1 -1
  68. package/lib/createNewUtils.d.ts.map +1 -1
  69. package/lib/createNewUtils.js +4 -2
  70. package/lib/createNewUtils.js.map +1 -1
  71. package/lib/defaultTokenProvider.d.ts.map +1 -1
  72. package/lib/defaultTokenProvider.js.map +1 -1
  73. package/lib/definitions.d.ts.map +1 -1
  74. package/lib/definitions.js.map +1 -1
  75. package/lib/deltaStorageService.d.ts.map +1 -1
  76. package/lib/deltaStorageService.js +4 -1
  77. package/lib/deltaStorageService.js.map +1 -1
  78. package/lib/documentDeltaConnection.d.ts.map +1 -1
  79. package/lib/documentDeltaConnection.js.map +1 -1
  80. package/lib/documentService.d.ts +4 -2
  81. package/lib/documentService.d.ts.map +1 -1
  82. package/lib/documentService.js +22 -22
  83. package/lib/documentService.js.map +1 -1
  84. package/lib/documentServiceFactory.d.ts.map +1 -1
  85. package/lib/documentServiceFactory.js +23 -9
  86. package/lib/documentServiceFactory.js.map +1 -1
  87. package/lib/documentStorageService.d.ts +1 -1
  88. package/lib/documentStorageService.d.ts.map +1 -1
  89. package/lib/documentStorageService.js +9 -7
  90. package/lib/documentStorageService.js.map +1 -1
  91. package/lib/errorUtils.d.ts +2 -1
  92. package/lib/errorUtils.d.ts.map +1 -1
  93. package/lib/errorUtils.js +6 -0
  94. package/lib/errorUtils.js.map +1 -1
  95. package/lib/index.d.ts +1 -1
  96. package/lib/index.d.ts.map +1 -1
  97. package/lib/index.js +1 -1
  98. package/lib/index.js.map +1 -1
  99. package/lib/nullBlobStorageService.d.ts.map +1 -1
  100. package/lib/nullBlobStorageService.js.map +1 -1
  101. package/lib/packageVersion.d.ts +1 -1
  102. package/lib/packageVersion.js +1 -1
  103. package/lib/packageVersion.js.map +1 -1
  104. package/lib/policies.d.ts.map +1 -1
  105. package/lib/policies.js.map +1 -1
  106. package/lib/restWrapper.d.ts +8 -5
  107. package/lib/restWrapper.d.ts.map +1 -1
  108. package/lib/restWrapper.js +38 -44
  109. package/lib/restWrapper.js.map +1 -1
  110. package/lib/retriableGitManager.d.ts.map +1 -1
  111. package/lib/retriableGitManager.js.map +1 -1
  112. package/lib/shreddedSummaryDocumentStorageService.d.ts +1 -1
  113. package/lib/shreddedSummaryDocumentStorageService.d.ts.map +1 -1
  114. package/lib/shreddedSummaryDocumentStorageService.js +10 -6
  115. package/lib/shreddedSummaryDocumentStorageService.js.map +1 -1
  116. package/lib/tokens.d.ts.map +1 -1
  117. package/lib/tokens.js.map +1 -1
  118. package/lib/urlUtils.d.ts.map +1 -1
  119. package/lib/urlUtils.js.map +1 -1
  120. package/lib/wholeSummaryDocumentStorageService.d.ts +1 -1
  121. package/lib/wholeSummaryDocumentStorageService.d.ts.map +1 -1
  122. package/lib/wholeSummaryDocumentStorageService.js +27 -13
  123. package/lib/wholeSummaryDocumentStorageService.js.map +1 -1
  124. package/package.json +19 -15
  125. package/prettier.config.cjs +1 -1
  126. package/src/cache.ts +21 -14
  127. package/src/createNewUtils.ts +24 -22
  128. package/src/defaultTokenProvider.ts +13 -15
  129. package/src/definitions.ts +2 -2
  130. package/src/deltaStorageService.ts +99 -95
  131. package/src/documentDeltaConnection.ts +53 -47
  132. package/src/documentService.ts +243 -239
  133. package/src/documentServiceFactory.ts +267 -237
  134. package/src/documentStorageService.ts +87 -83
  135. package/src/errorUtils.ts +78 -75
  136. package/src/index.ts +4 -1
  137. package/src/nullBlobStorageService.ts +24 -21
  138. package/src/packageVersion.ts +1 -1
  139. package/src/policies.ts +44 -44
  140. package/src/restWrapper.ts +270 -208
  141. package/src/retriableGitManager.ts +152 -151
  142. package/src/shreddedSummaryDocumentStorageService.ts +202 -194
  143. package/src/tokens.ts +51 -43
  144. package/src/urlUtils.ts +26 -23
  145. package/src/wholeSummaryDocumentStorageService.ts +246 -228
  146. package/tsconfig.esnext.json +6 -6
  147. package/tsconfig.json +9 -13
@@ -17,55 +17,61 @@ const protocolVersions = ["^0.4.0", "^0.3.0", "^0.2.0", "^0.1.0"];
17
17
  * Wrapper over the shared one for driver specific translation.
18
18
  */
19
19
  export class R11sDocumentDeltaConnection extends DocumentDeltaConnection {
20
- public static async create(
21
- tenantId: string,
22
- id: string,
23
- token: string | null,
24
- io: typeof SocketIOClientStatic,
25
- client: IClient,
26
- url: string,
27
- logger: ITelemetryLogger,
28
- timeoutMs = 20000): Promise<IDocumentDeltaConnection> {
29
- const socket = io(
30
- url,
31
- {
32
- query: {
33
- documentId: id,
34
- tenantId,
35
- },
36
- reconnection: false,
37
- // Default to websocket connection, with long-polling disabled
38
- transports: ["websocket"],
39
- timeout: timeoutMs,
40
- });
20
+ public static async create(
21
+ tenantId: string,
22
+ id: string,
23
+ token: string | null,
24
+ io: typeof SocketIOClientStatic,
25
+ client: IClient,
26
+ url: string,
27
+ logger: ITelemetryLogger,
28
+ timeoutMs = 20000,
29
+ ): Promise<IDocumentDeltaConnection> {
30
+ const socket = io(url, {
31
+ query: {
32
+ documentId: id,
33
+ tenantId,
34
+ },
35
+ reconnection: false,
36
+ // Default to websocket connection, with long-polling disabled
37
+ transports: ["websocket"],
38
+ timeout: timeoutMs,
39
+ });
41
40
 
42
- const connectMessage: IConnect = {
43
- client,
44
- id,
45
- mode: client.mode,
46
- tenantId,
47
- token, // Token is going to indicate tenant level information, etc...
48
- versions: protocolVersions,
49
- relayUserAgent: [client.details.environment, ` driverVersion:${driverVersion}`].join(";"),
50
- };
41
+ const connectMessage: IConnect = {
42
+ client,
43
+ id,
44
+ mode: client.mode,
45
+ tenantId,
46
+ token, // Token is going to indicate tenant level information, etc...
47
+ versions: protocolVersions,
48
+ relayUserAgent: [client.details.environment, ` driverVersion:${driverVersion}`].join(
49
+ ";",
50
+ ),
51
+ };
51
52
 
52
- // TODO: expose to host at factory level
53
- const enableLongPollingDowngrades = true;
54
- const deltaConnection = new R11sDocumentDeltaConnection(socket, id, logger, enableLongPollingDowngrades);
53
+ // TODO: expose to host at factory level
54
+ const enableLongPollingDowngrades = true;
55
+ const deltaConnection = new R11sDocumentDeltaConnection(
56
+ socket,
57
+ id,
58
+ logger,
59
+ enableLongPollingDowngrades,
60
+ );
55
61
 
56
- await deltaConnection.initialize(connectMessage, timeoutMs);
57
- return deltaConnection;
58
- }
62
+ await deltaConnection.initialize(connectMessage, timeoutMs);
63
+ return deltaConnection;
64
+ }
59
65
 
60
- /**
61
- * Error raising for socket.io issues
62
- */
63
- protected createErrorObject(handler: string, error?: any, canRetry = true): IAnyDriverError {
64
- // Note: we suspect the incoming error object is either:
65
- // - a socketError: add it to the R11sError object for driver to be able to parse it and reason over it.
66
- // - anything else: let base class handle it
67
- return canRetry && Number.isInteger(error?.code) && typeof error?.message === "string"
68
- ? errorObjectFromSocketError(error as IR11sSocketError, handler)
69
- : super.createErrorObject(handler, error, canRetry);
70
- }
66
+ /**
67
+ * Error raising for socket.io issues
68
+ */
69
+ protected createErrorObject(handler: string, error?: any, canRetry = true): IAnyDriverError {
70
+ // Note: we suspect the incoming error object is either:
71
+ // - a socketError: add it to the R11sError object for driver to be able to parse it and reason over it.
72
+ // - anything else: let base class handle it
73
+ return canRetry && Number.isInteger(error?.code) && typeof error?.message === "string"
74
+ ? errorObjectFromSocketError(error as IR11sSocketError, handler)
75
+ : super.createErrorObject(handler, error, canRetry);
76
+ }
71
77
  }
@@ -35,262 +35,266 @@ const RediscoverAfterTimeSinceDiscoveryMs = 5 * 60000; // 5 minute
35
35
  */
36
36
  // eslint-disable-next-line import/namespace
37
37
  export class DocumentService implements api.IDocumentService {
38
- private lastDiscoveredAt: number = Date.now();
39
- private discoverP: Promise<void> | undefined;
38
+ private lastDiscoveredAt: number = Date.now();
39
+ private discoverP: Promise<void> | undefined;
40
40
 
41
- private storageManager: GitManager | undefined;
42
- private noCacheStorageManager: GitManager | undefined;
43
- private ordererRestWrapper: RestWrapper | undefined;
41
+ private storageManager: GitManager | undefined;
42
+ private noCacheStorageManager: GitManager | undefined;
44
43
 
45
- public get resolvedUrl() {
46
- return this._resolvedUrl;
47
- }
44
+ public get resolvedUrl() {
45
+ return this._resolvedUrl;
46
+ }
48
47
 
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
- }
48
+ constructor(
49
+ private _resolvedUrl: api.IResolvedUrl,
50
+ protected ordererUrl: string,
51
+ private deltaStorageUrl: string,
52
+ private deltaStreamUrl: string,
53
+ private storageUrl: string,
54
+ private readonly logger: ITelemetryLogger,
55
+ protected tokenProvider: ITokenProvider,
56
+ protected tenantId: string,
57
+ protected documentId: string,
58
+ protected ordererRestWrapper: RouterliciousOrdererRestWrapper,
59
+ private readonly documentStorageServicePolicies: api.IDocumentStorageServicePolicies,
60
+ private readonly driverPolicies: IRouterliciousDriverPolicies,
61
+ private readonly blobCache: ICache<ArrayBufferLike>,
62
+ private readonly snapshotTreeCache: ICache<ISnapshotTreeVersion>,
63
+ private readonly discoverFluidResolvedUrl: () => Promise<api.IFluidResolvedUrl>,
64
+ ) {}
65
65
 
66
- private documentStorageService: DocumentStorageService | undefined;
66
+ private documentStorageService: DocumentStorageService | undefined;
67
67
 
68
- public dispose() { }
68
+ public dispose() {}
69
69
 
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
- }
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
+ }
79
79
 
80
- if (this.storageUrl === undefined) {
81
- return new NullBlobStorageService();
82
- }
80
+ if (this.storageUrl === undefined) {
81
+ return new NullBlobStorageService();
82
+ }
83
83
 
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
- }
84
+ const getStorageManager = async (disableCache?: boolean): Promise<GitManager> => {
85
+ const shouldUpdateDiscoveredSessionInfo = this.shouldUpdateDiscoveredSessionInfo();
86
+ if (shouldUpdateDiscoveredSessionInfo) {
87
+ await this.refreshDiscovery();
88
+ }
89
+ if (
90
+ !this.storageManager ||
91
+ !this.noCacheStorageManager ||
92
+ shouldUpdateDiscoveredSessionInfo
93
+ ) {
94
+ const rateLimiter = new RateLimiter(
95
+ this.driverPolicies.maxConcurrentStorageRequests,
96
+ );
97
+ const storageRestWrapper = await RouterliciousStorageRestWrapper.load(
98
+ this.tenantId,
99
+ this.documentId,
100
+ this.tokenProvider,
101
+ this.logger,
102
+ rateLimiter,
103
+ this.driverPolicies.enableRestLess,
104
+ this.storageUrl,
105
+ );
106
+ const historian = new Historian(this.storageUrl, true, false, storageRestWrapper);
107
+ this.storageManager = new GitManager(historian);
108
+ const noCacheHistorian = new Historian(
109
+ this.storageUrl,
110
+ true,
111
+ true,
112
+ storageRestWrapper,
113
+ );
114
+ this.noCacheStorageManager = new GitManager(noCacheHistorian);
115
+ }
113
116
 
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
- };
117
+ return disableCache ? this.noCacheStorageManager : this.storageManager;
118
+ };
119
+ // Initialize storageManager and noCacheStorageManager
120
+ const storageManager = await getStorageManager();
121
+ const noCacheStorageManager = await getStorageManager(true);
122
+ this.documentStorageService = new DocumentStorageService(
123
+ this.documentId,
124
+ storageManager,
125
+ this.logger,
126
+ this.documentStorageServicePolicies,
127
+ this.driverPolicies,
128
+ this.blobCache,
129
+ this.snapshotTreeCache,
130
+ noCacheStorageManager,
131
+ getStorageManager,
132
+ );
133
+ return this.documentStorageService;
134
+ }
125
135
 
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
- }
136
+ /**
137
+ * Connects to a delta storage endpoint for getting ops between a range.
138
+ *
139
+ * @returns returns the document delta storage service for routerlicious driver.
140
+ */
141
+ public async connectToDeltaStorage(): Promise<api.IDocumentDeltaStorageService> {
142
+ await this.connectToStorage();
143
+ assert(!!this.documentStorageService, 0x0b1 /* "Storage service not initialized" */);
138
144
 
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" */);
145
+ const getRestWrapper = async (): Promise<RestWrapper> => {
146
+ const shouldUpdateDiscoveredSessionInfo = this.shouldUpdateDiscoveredSessionInfo();
147
147
 
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
- }
148
+ if (shouldUpdateDiscoveredSessionInfo) {
149
+ await this.refreshDiscovery();
150
+ const rateLimiter = new RateLimiter(
151
+ this.driverPolicies.maxConcurrentOrdererRequests,
152
+ );
153
+ this.ordererRestWrapper = await RouterliciousOrdererRestWrapper.load(
154
+ this.tenantId,
155
+ this.documentId,
156
+ this.tokenProvider,
157
+ this.logger,
158
+ rateLimiter,
159
+ this.driverPolicies.enableRestLess,
160
+ );
161
+ }
162
+ return this.ordererRestWrapper;
163
+ };
164
+ const restWrapper = await getRestWrapper();
165
+ const deltaStorageService = new DeltaStorageService(
166
+ this.deltaStorageUrl,
167
+ restWrapper,
168
+ this.logger,
169
+ getRestWrapper,
170
+ () => this.deltaStorageUrl,
171
+ );
172
+ return new DocumentDeltaStorageService(
173
+ this.tenantId,
174
+ this.documentId,
175
+ deltaStorageService,
176
+ this.documentStorageService,
177
+ this.logger,
178
+ );
179
+ }
182
180
 
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
- }
181
+ /**
182
+ * Connects to a delta stream endpoint for emitting ops.
183
+ *
184
+ * @returns returns the document delta stream service for routerlicious driver.
185
+ */
186
+ public async connectToDeltaStream(client: IClient): Promise<api.IDocumentDeltaConnection> {
187
+ const connect = async (refreshToken?: boolean) => {
188
+ let ordererToken = this.ordererRestWrapper.getToken();
189
+ if (this.shouldUpdateDiscoveredSessionInfo()) {
190
+ await this.refreshDiscovery();
191
+ }
193
192
 
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
- );
193
+ if (refreshToken) {
194
+ 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
+ const newOrdererToken = await this.tokenProvider.fetchOrdererToken(
205
+ this.tenantId,
206
+ this.documentId,
207
+ refreshToken,
208
+ );
209
+ this.ordererRestWrapper.setToken(newOrdererToken);
210
+ return newOrdererToken;
211
+ },
212
+ );
213
+ }
211
214
 
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
- };
215
+ return PerformanceEvent.timedExecAsync(
216
+ this.logger,
217
+ {
218
+ eventName: "ConnectToDeltaStream",
219
+ docId: this.documentId,
220
+ },
221
+ async () => {
222
+ return R11sDocumentDeltaConnection.create(
223
+ this.tenantId,
224
+ this.documentId,
225
+ ordererToken.jwt,
226
+ io,
227
+ client,
228
+ this.deltaStreamUrl,
229
+ this.logger,
230
+ );
231
+ },
232
+ );
233
+ };
231
234
 
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
- }
235
+ // Attempt to establish connection.
236
+ // Retry with new token on authorization error; otherwise, allow container layer to handle.
237
+ try {
238
+ const connection = await connect();
239
+ return connection;
240
+ } catch (error: any) {
241
+ if (error?.statusCode === 401) {
242
+ // Fetch new token and retry once,
243
+ // otherwise 401 will be bubbled up as non-retriable AuthorizationError.
244
+ return connect(true /* refreshToken */);
245
+ }
246
+ throw error;
247
+ }
248
+ }
246
249
 
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
- }
250
+ /**
251
+ * Re-discover session URLs if necessary.
252
+ */
253
+ private async refreshDiscovery(): Promise<void> {
254
+ if (!this.discoverP) {
255
+ this.discoverP = PerformanceEvent.timedExecAsync(
256
+ this.logger,
257
+ {
258
+ eventName: "RefreshDiscovery",
259
+ },
260
+ async () => this.refreshDiscoveryCore(),
261
+ );
262
+ }
263
+ return this.discoverP;
264
+ }
262
265
 
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
- }
266
+ private async refreshDiscoveryCore(): Promise<void> {
267
+ const fluidResolvedUrl = await this.discoverFluidResolvedUrl();
268
+ this._resolvedUrl = fluidResolvedUrl;
269
+ this.storageUrl = fluidResolvedUrl.endpoints.storageUrl;
270
+ this.ordererUrl = fluidResolvedUrl.endpoints.ordererUrl;
271
+ this.deltaStorageUrl = fluidResolvedUrl.endpoints.deltaStorageUrl;
272
+ this.deltaStreamUrl = fluidResolvedUrl.endpoints.deltaStreamUrl || this.ordererUrl;
273
+ }
271
274
 
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
- }
275
+ /**
276
+ * Whether enough time has passed since last disconnect to warrant a new discovery call on reconnect.
277
+ */
278
+ private shouldUpdateDiscoveredSessionInfo(): boolean {
279
+ if (!this.driverPolicies.enableDiscovery) {
280
+ return false;
281
+ }
282
+ const now = Date.now();
283
+ // When connection is disconnected, we cannot know if session has moved or document has been deleted
284
+ // without re-doing discovery on the next attempt to connect.
285
+ // Disconnect event is not so reliable in local testing. To ensure re-discovery when necessary,
286
+ // re-discover if enough time has passed since last discovery.
287
+ const pastLastDiscoveryTimeThreshold =
288
+ now - this.lastDiscoveredAt > RediscoverAfterTimeSinceDiscoveryMs;
289
+ if (pastLastDiscoveryTimeThreshold) {
290
+ // Reset discover promise and refresh discovery.
291
+ this.lastDiscoveredAt = Date.now();
292
+ this.discoverP = undefined;
293
+ this.refreshDiscovery().catch(() => {
294
+ // Undo discovery time set on failure, so that next check refreshes.
295
+ this.lastDiscoveredAt = 0;
296
+ });
297
+ }
298
+ return pastLastDiscoveryTimeThreshold;
299
+ }
296
300
  }