@itwin/presentation-backend 4.0.0-dev.8 → 4.0.0-dev.80

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 (59) hide show
  1. package/CHANGELOG.md +47 -1
  2. package/lib/cjs/assets/supplemental-presentation-rules/BisCore.PresentationRuleSet.json +5 -3
  3. package/lib/cjs/presentation-backend/BackendLoggerCategory.d.ts +41 -41
  4. package/lib/cjs/presentation-backend/BackendLoggerCategory.js +50 -50
  5. package/lib/cjs/presentation-backend/Constants.d.ts +8 -8
  6. package/lib/cjs/presentation-backend/Constants.js +40 -36
  7. package/lib/cjs/presentation-backend/Constants.js.map +1 -1
  8. package/lib/cjs/presentation-backend/ElementPropertiesHelper.d.ts +11 -11
  9. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js +260 -262
  10. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js.map +1 -1
  11. package/lib/cjs/presentation-backend/NativePlatform.d.ts +87 -87
  12. package/lib/cjs/presentation-backend/NativePlatform.d.ts.map +1 -1
  13. package/lib/cjs/presentation-backend/NativePlatform.js +140 -140
  14. package/lib/cjs/presentation-backend/NativePlatform.js.map +1 -1
  15. package/lib/cjs/presentation-backend/Presentation.d.ts +106 -106
  16. package/lib/cjs/presentation-backend/Presentation.d.ts.map +1 -1
  17. package/lib/cjs/presentation-backend/Presentation.js +148 -150
  18. package/lib/cjs/presentation-backend/Presentation.js.map +1 -1
  19. package/lib/cjs/presentation-backend/PresentationIpcHandler.d.ts +12 -12
  20. package/lib/cjs/presentation-backend/PresentationIpcHandler.js +41 -41
  21. package/lib/cjs/presentation-backend/PresentationManager.d.ts +456 -445
  22. package/lib/cjs/presentation-backend/PresentationManager.d.ts.map +1 -1
  23. package/lib/cjs/presentation-backend/PresentationManager.js +309 -311
  24. package/lib/cjs/presentation-backend/PresentationManager.js.map +1 -1
  25. package/lib/cjs/presentation-backend/PresentationManagerDetail.d.ts +61 -61
  26. package/lib/cjs/presentation-backend/PresentationManagerDetail.js +430 -430
  27. package/lib/cjs/presentation-backend/PresentationManagerDetail.js.map +1 -1
  28. package/lib/cjs/presentation-backend/PresentationRpcImpl.d.ts +62 -62
  29. package/lib/cjs/presentation-backend/PresentationRpcImpl.d.ts.map +1 -1
  30. package/lib/cjs/presentation-backend/PresentationRpcImpl.js +388 -388
  31. package/lib/cjs/presentation-backend/PresentationRpcImpl.js.map +1 -1
  32. package/lib/cjs/presentation-backend/RulesetEmbedder.d.ts +102 -102
  33. package/lib/cjs/presentation-backend/RulesetEmbedder.d.ts.map +1 -1
  34. package/lib/cjs/presentation-backend/RulesetEmbedder.js +284 -279
  35. package/lib/cjs/presentation-backend/RulesetEmbedder.js.map +1 -1
  36. package/lib/cjs/presentation-backend/RulesetManager.d.ts +53 -53
  37. package/lib/cjs/presentation-backend/RulesetManager.js +73 -73
  38. package/lib/cjs/presentation-backend/RulesetVariablesManager.d.ts +140 -140
  39. package/lib/cjs/presentation-backend/RulesetVariablesManager.js +129 -129
  40. package/lib/cjs/presentation-backend/SelectionScopesHelper.d.ts +28 -28
  41. package/lib/cjs/presentation-backend/SelectionScopesHelper.js +210 -213
  42. package/lib/cjs/presentation-backend/SelectionScopesHelper.js.map +1 -1
  43. package/lib/cjs/presentation-backend/TemporaryStorage.d.ts +123 -123
  44. package/lib/cjs/presentation-backend/TemporaryStorage.js +151 -151
  45. package/lib/cjs/presentation-backend/UpdatesTracker.d.ts +27 -27
  46. package/lib/cjs/presentation-backend/UpdatesTracker.js +54 -54
  47. package/lib/cjs/presentation-backend/Utils.d.ts +49 -49
  48. package/lib/cjs/presentation-backend/Utils.d.ts.map +1 -1
  49. package/lib/cjs/presentation-backend/Utils.js +107 -106
  50. package/lib/cjs/presentation-backend/Utils.js.map +1 -1
  51. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.d.ts +16 -16
  52. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js +55 -51
  53. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js.map +1 -1
  54. package/lib/cjs/presentation-backend/domain/RulesetElements.d.ts +21 -21
  55. package/lib/cjs/presentation-backend/domain/RulesetElements.js +38 -38
  56. package/lib/cjs/presentation-backend.d.ts +19 -19
  57. package/lib/cjs/presentation-backend.js +39 -35
  58. package/lib/cjs/presentation-backend.js.map +1 -1
  59. package/package.json +18 -18
@@ -1,389 +1,389 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module RPC
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.PresentationRpcImpl = exports.MAX_ALLOWED_KEYS_PAGE_SIZE = exports.MAX_ALLOWED_PAGE_SIZE = void 0;
11
- const core_backend_1 = require("@itwin/core-backend");
12
- const core_bentley_1 = require("@itwin/core-bentley");
13
- const presentation_common_1 = require("@itwin/presentation-common");
14
- const BackendLoggerCategory_1 = require("./BackendLoggerCategory");
15
- const Presentation_1 = require("./Presentation");
16
- const TemporaryStorage_1 = require("./TemporaryStorage");
17
- // eslint-disable-next-line @typescript-eslint/no-var-requires
18
- const packageJsonVersion = require("../../../package.json").version;
19
- /** @internal */
20
- exports.MAX_ALLOWED_PAGE_SIZE = 1000;
21
- /** @internal */
22
- exports.MAX_ALLOWED_KEYS_PAGE_SIZE = 10000;
23
- /**
24
- * The backend implementation of PresentationRpcInterface. All it's basically
25
- * responsible for is forwarding calls to [[Presentation.manager]].
26
- *
27
- * @internal
28
- */
29
- class PresentationRpcImpl extends presentation_common_1.PresentationRpcInterface {
30
- constructor(props) {
31
- var _a;
32
- super();
33
- this._requestTimeout = (_a = props === null || props === void 0 ? void 0 : props.requestTimeout) !== null && _a !== void 0 ? _a : 90 * 1000;
34
- this._pendingRequests = new TemporaryStorage_1.TemporaryStorage({
35
- // remove the pending request after request timeout + 10 seconds - this gives
36
- // frontend 10 seconds to re-send the request until it's removed from requests' cache
37
- unusedValueLifetime: (this._requestTimeout > 0) ? (this._requestTimeout + 10 * 1000) : undefined,
38
- // attempt to clean up every second
39
- cleanupInterval: 1000,
40
- cleanupHandler: (id, _, reason) => {
41
- var _a;
42
- if (reason !== "request") {
43
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Cleaning up request without frontend retrieving it: ${id}.`);
44
- // istanbul ignore next
45
- (_a = this._cancelEvents.get(id)) === null || _a === void 0 ? void 0 : _a.raiseEvent();
46
- }
47
- this._cancelEvents.delete(id);
48
- },
49
- });
50
- this._cancelEvents = new Map();
51
- }
52
- dispose() {
53
- this._pendingRequests.dispose();
54
- }
55
- get requestTimeout() { return this._requestTimeout; }
56
- get pendingRequests() { return this._pendingRequests; }
57
- /** Returns an ok response with result inside */
58
- successResponse(result, diagnostics) {
59
- return {
60
- statusCode: presentation_common_1.PresentationStatus.Success,
61
- result,
62
- diagnostics,
63
- };
64
- }
65
- /** Returns a bad request response with empty result and an error code */
66
- errorResponse(errorCode, errorMessage, diagnostics) {
67
- return {
68
- statusCode: errorCode,
69
- result: undefined,
70
- errorMessage,
71
- diagnostics,
72
- };
73
- }
74
- /**
75
- * Get the [[PresentationManager]] used by this RPC impl.
76
- */
77
- getManager(clientId) {
78
- return Presentation_1.Presentation.getManager(clientId);
79
- }
80
- getIModel(token) {
81
- let imodel;
82
- try {
83
- imodel = core_backend_1.IModelDb.findByKey(token.key);
84
- }
85
- catch {
86
- throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.InvalidArgument, "IModelRpcProps doesn't point to a valid iModel");
87
- }
88
- return imodel;
89
- }
90
- async makeRequest(token, requestId, requestOptions, request) {
91
- const requestKey = JSON.stringify({ iModelKey: token.key, requestId, requestOptions });
92
- core_bentley_1.Logger.logInfo(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Received '${requestId}' request. Params: ${requestKey}`);
93
- let resultPromise = this._pendingRequests.getValue(requestKey);
94
- if (resultPromise) {
95
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request already pending`);
96
- }
97
- else {
98
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request not found, creating a new one`);
99
- let imodel;
100
- try {
101
- imodel = this.getIModel(token);
102
- }
103
- catch (e) {
104
- (0, core_bentley_1.assert)(e instanceof Error);
105
- return this.errorResponse(presentation_common_1.PresentationStatus.InvalidArgument, e.message);
106
- }
107
- const { clientId: _, diagnostics: diagnosticsOptions, rulesetVariables, ...options } = requestOptions;
108
- const managerRequestOptions = {
109
- ...options,
110
- imodel,
111
- cancelEvent: new core_bentley_1.BeEvent(),
112
- };
113
- // set up ruleset variables
114
- if (rulesetVariables)
115
- managerRequestOptions.rulesetVariables = rulesetVariables.map(presentation_common_1.RulesetVariable.fromJSON);
116
- // set up diagnostics listener
117
- let diagnostics;
118
- const getDiagnostics = () => {
119
- if (!diagnostics)
120
- diagnostics = {};
121
- return diagnostics;
122
- };
123
- if (diagnosticsOptions) {
124
- if (diagnosticsOptions.backendVersion) {
125
- getDiagnostics().backendVersion = packageJsonVersion;
126
- }
127
- managerRequestOptions.diagnostics = {
128
- ...diagnosticsOptions,
129
- handler: (d) => {
130
- if (d.logs) {
131
- const target = getDiagnostics();
132
- if (target.logs)
133
- target.logs.push(...d.logs);
134
- else
135
- target.logs = [...d.logs];
136
- }
137
- },
138
- };
139
- }
140
- // initiate request
141
- resultPromise = request(managerRequestOptions)
142
- .then((result) => this.successResponse(result, diagnostics))
143
- .catch((e) => this.errorResponse(e.errorNumber, e.message, diagnostics));
144
- // store the request promise
145
- this._pendingRequests.addValue(requestKey, resultPromise);
146
- this._cancelEvents.set(requestKey, managerRequestOptions.cancelEvent);
147
- }
148
- if (this._requestTimeout === 0) {
149
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout not configured, returning promise without a timeout.`);
150
- resultPromise.finally(() => {
151
- this._pendingRequests.deleteValue(requestKey);
152
- });
153
- return resultPromise;
154
- }
155
- let timeout;
156
- const timeoutPromise = new Promise((_resolve, reject) => {
157
- timeout = setTimeout(() => {
158
- reject("Timed out");
159
- }, this._requestTimeout);
160
- });
161
- /* eslint-disable @typescript-eslint/indent */
162
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Returning a promise with a timeout of ${this._requestTimeout}.`);
163
- return Promise
164
- .race([resultPromise, timeoutPromise])
165
- .catch(() => {
166
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout, returning "BackendTimeout" status.`);
167
- return this.errorResponse(presentation_common_1.PresentationStatus.BackendTimeout);
168
- })
169
- .then((response) => {
170
- if (response.statusCode !== presentation_common_1.PresentationStatus.BackendTimeout) {
171
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request completed, returning result.`);
172
- this._pendingRequests.deleteValue(requestKey);
173
- }
174
- clearTimeout(timeout);
175
- return response;
176
- });
177
- /* eslint-enable @typescript-eslint/indent */
178
- }
179
- async getNodesCount(token, requestOptions) {
180
- return this.makeRequest(token, "getNodesCount", requestOptions, async (options) => {
181
- options = {
182
- ...options,
183
- parentKey: nodeKeyFromJson(options.parentKey),
184
- };
185
- return this.getManager(requestOptions.clientId).getNodesCount(options);
186
- });
187
- }
188
- // eslint-disable-next-line deprecation/deprecation
189
- async getPagedNodes(token, requestOptions) {
190
- return this.makeRequest(token, "getPagedNodes", requestOptions, async (options) => {
191
- options = enforceValidPageSize({
192
- ...options,
193
- parentKey: nodeKeyFromJson(options.parentKey),
194
- });
195
- const [serializedNodesJson, count] = await Promise.all([
196
- this.getManager(requestOptions.clientId).getDetail().getNodes(options),
197
- this.getManager(requestOptions.clientId).getNodesCount(options),
198
- ]);
199
- // eslint-disable-next-line deprecation/deprecation
200
- const nodesJson = JSON.parse(serializedNodesJson);
201
- return {
202
- total: count,
203
- items: nodesJson.nodes,
204
- };
205
- });
206
- }
207
- async getNodesDescriptor(token, requestOptions) {
208
- return this.makeRequest(token, "getNodesDescriptor", requestOptions, async (options) => {
209
- options = {
210
- ...options,
211
- parentKey: nodeKeyFromJson(options.parentKey),
212
- };
213
- return this.getManager().getDetail().getNodesDescriptor(options);
214
- });
215
- }
216
- // eslint-disable-next-line deprecation/deprecation
217
- async getNodePaths(token, requestOptions) {
218
- return this.makeRequest(token, "getNodePaths", requestOptions, async (options) => {
219
- const result = await this.getManager(requestOptions.clientId).getNodePaths(options);
220
- // eslint-disable-next-line deprecation/deprecation
221
- return result.map(presentation_common_1.NodePathElement.toJSON);
222
- });
223
- }
224
- // eslint-disable-next-line deprecation/deprecation
225
- async getFilteredNodePaths(token, requestOptions) {
226
- return this.makeRequest(token, "getFilteredNodePaths", requestOptions, async (options) => {
227
- const result = await this.getManager(requestOptions.clientId).getFilteredNodePaths(options);
228
- // eslint-disable-next-line deprecation/deprecation
229
- return result.map(presentation_common_1.NodePathElement.toJSON);
230
- });
231
- }
232
- async getContentSources(token, requestOptions) {
233
- return this.makeRequest(token, "getContentSources", requestOptions, async (options) => {
234
- const result = await this.getManager(requestOptions.clientId).getContentSources(options);
235
- const classesMap = {};
236
- const selectClasses = result.map((sci) => presentation_common_1.SelectClassInfo.toCompressedJSON(sci, classesMap));
237
- return { sources: selectClasses, classesMap };
238
- });
239
- }
240
- async getContentDescriptor(token, requestOptions) {
241
- return this.makeRequest(token, "getContentDescriptor", requestOptions, async (options) => {
242
- options = {
243
- ...options,
244
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
245
- };
246
- if (options.transport === "unparsed-json") {
247
- // Here we send a plain JSON string but we will parse it to DescriptorJSON on the frontend. This way we are
248
- // bypassing unnecessary deserialization and serialization.
249
- return Presentation_1.Presentation.getManager().getDetail().getContentDescriptor(options);
250
- }
251
- else {
252
- // Support for older frontends that still expect a parsed descriptor
253
- const descriptor = await Presentation_1.Presentation.getManager().getContentDescriptor(options);
254
- return descriptor === null || descriptor === void 0 ? void 0 : descriptor.toJSON();
255
- }
256
- });
257
- }
258
- async getContentSetSize(token, requestOptions) {
259
- return this.makeRequest(token, "getContentSetSize", requestOptions, async (options) => {
260
- options = {
261
- ...options,
262
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
263
- };
264
- return this.getManager(requestOptions.clientId).getContentSetSize(options);
265
- });
266
- }
267
- async getPagedContent(token, requestOptions) {
268
- return this.makeRequest(token, "getPagedContent", requestOptions, async (options) => {
269
- options = enforceValidPageSize({
270
- ...options,
271
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
272
- });
273
- const [size, content] = await Promise.all([
274
- this.getManager(requestOptions.clientId).getContentSetSize(options),
275
- this.getManager(requestOptions.clientId).getDetail().getContent(options),
276
- ]);
277
- if (!content)
278
- return undefined;
279
- return {
280
- descriptor: content.descriptor.toJSON(),
281
- contentSet: {
282
- total: size,
283
- items: content.contentSet.map((i) => i.toJSON()),
284
- },
285
- };
286
- });
287
- }
288
- async getPagedContentSet(token, requestOptions) {
289
- const content = await this.getPagedContent(token, requestOptions);
290
- return this.successResponse(content.result ? content.result.contentSet : { total: 0, items: [] });
291
- }
292
- async getElementProperties(token, requestOptions) {
293
- return this.makeRequest(token, "getElementProperties", { ...requestOptions }, async (options) => {
294
- return this.getManager(requestOptions.clientId).getDetail().getElementProperties(options);
295
- });
296
- }
297
- // eslint-disable-next-line deprecation/deprecation
298
- async getPagedDistinctValues(token, requestOptions) {
299
- return this.makeRequest(token, "getPagedDistinctValues", requestOptions, async (options) => {
300
- options = enforceValidPageSize({
301
- ...options,
302
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
303
- });
304
- const response = await this.getManager(requestOptions.clientId).getPagedDistinctValues(options);
305
- return {
306
- ...response,
307
- // eslint-disable-next-line deprecation/deprecation
308
- items: response.items.map(presentation_common_1.DisplayValueGroup.toJSON),
309
- };
310
- });
311
- }
312
- async getContentInstanceKeys(token, requestOptions) {
313
- return this.makeRequest(token, "getContentInstanceKeys", requestOptions, async (options) => {
314
- const { displayType, ...optionsNoDisplayType } = options;
315
- options = enforceValidPageSize({
316
- ...optionsNoDisplayType,
317
- keys: presentation_common_1.KeySet.fromJSON(optionsNoDisplayType.keys),
318
- descriptor: {
319
- displayType,
320
- contentFlags: presentation_common_1.ContentFlags.KeysOnly,
321
- },
322
- }, exports.MAX_ALLOWED_KEYS_PAGE_SIZE);
323
- const [size, content] = await Promise.all([
324
- this.getManager(requestOptions.clientId).getContentSetSize(options),
325
- this.getManager(requestOptions.clientId).getDetail().getContent(options),
326
- ]);
327
- if (size === 0 || !content)
328
- return { total: 0, items: new presentation_common_1.KeySet().toJSON() };
329
- return {
330
- total: size,
331
- items: content.contentSet.reduce((keys, item) => keys.add(item.primaryKeys), new presentation_common_1.KeySet()).toJSON(),
332
- };
333
- });
334
- }
335
- async getDisplayLabelDefinition(token, requestOptions) {
336
- return this.makeRequest(token, "getDisplayLabelDefinition", requestOptions, async (options) => {
337
- const label = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinition(options);
338
- return label;
339
- });
340
- }
341
- async getPagedDisplayLabelDefinitions(token, requestOptions) {
342
- const pageOpts = enforceValidPageSize({ paging: { start: 0, size: requestOptions.keys.length } });
343
- if (pageOpts.paging.size < requestOptions.keys.length)
344
- requestOptions.keys.splice(pageOpts.paging.size);
345
- return this.makeRequest(token, "getPagedDisplayLabelDefinitions", requestOptions, async (options) => {
346
- const labels = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinitions({ ...options, keys: options.keys });
347
- return {
348
- total: options.keys.length,
349
- items: labels,
350
- };
351
- });
352
- }
353
- async getSelectionScopes(token, requestOptions) {
354
- return this.makeRequest(token, "getSelectionScopes", requestOptions, async (options) => this.getManager(requestOptions.clientId).getSelectionScopes(options));
355
- }
356
- async computeSelection(token, requestOptions, ids, scopeId) {
357
- return this.makeRequest(token, "computeSelection", requestOptions, async (options) => {
358
- if (!(0, presentation_common_1.isComputeSelectionRequestOptions)(options)) {
359
- options = {
360
- ...options,
361
- elementIds: ids,
362
- scope: { id: scopeId },
363
- };
364
- }
365
- const keys = await this.getManager(requestOptions.clientId).computeSelection(options);
366
- return keys.toJSON();
367
- });
368
- }
369
- }
370
- exports.PresentationRpcImpl = PresentationRpcImpl;
371
- const enforceValidPageSize = (requestOptions, maxPageSize = exports.MAX_ALLOWED_PAGE_SIZE) => {
372
- var _a;
373
- const validPageSize = getValidPageSize((_a = requestOptions.paging) === null || _a === void 0 ? void 0 : _a.size, maxPageSize);
374
- if (!requestOptions.paging || requestOptions.paging.size !== validPageSize)
375
- return { ...requestOptions, paging: { ...requestOptions.paging, size: validPageSize } };
376
- return requestOptions;
377
- };
378
- const getValidPageSize = (size, maxPageSize) => {
379
- const requestedSize = size !== null && size !== void 0 ? size : 0;
380
- return (requestedSize === 0 || requestedSize > maxPageSize) ? maxPageSize : requestedSize;
381
- };
382
- // eslint-disable-next-line deprecation/deprecation
383
- const nodeKeyFromJson = (json) => {
384
- if (!json)
385
- return undefined;
386
- // eslint-disable-next-line deprecation/deprecation
387
- return presentation_common_1.NodeKey.fromJSON(json);
388
- };
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module RPC
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.PresentationRpcImpl = exports.MAX_ALLOWED_KEYS_PAGE_SIZE = exports.MAX_ALLOWED_PAGE_SIZE = void 0;
11
+ const core_backend_1 = require("@itwin/core-backend");
12
+ const core_bentley_1 = require("@itwin/core-bentley");
13
+ const presentation_common_1 = require("@itwin/presentation-common");
14
+ const BackendLoggerCategory_1 = require("./BackendLoggerCategory");
15
+ const Presentation_1 = require("./Presentation");
16
+ const TemporaryStorage_1 = require("./TemporaryStorage");
17
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
18
+ const packageJsonVersion = require("../../../package.json").version;
19
+ /** @internal */
20
+ exports.MAX_ALLOWED_PAGE_SIZE = 1000;
21
+ /** @internal */
22
+ exports.MAX_ALLOWED_KEYS_PAGE_SIZE = 10000;
23
+ const DEFAULT_REQUEST_TIMEOUT = 5000;
24
+ /**
25
+ * The backend implementation of PresentationRpcInterface. All it's basically
26
+ * responsible for is forwarding calls to [[Presentation.manager]].
27
+ *
28
+ * @internal
29
+ */
30
+ class PresentationRpcImpl extends presentation_common_1.PresentationRpcInterface {
31
+ constructor(props) {
32
+ super();
33
+ this._requestTimeout = props?.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT;
34
+ this._pendingRequests = new TemporaryStorage_1.TemporaryStorage({
35
+ // remove the pending request after request timeout + 10 seconds - this gives
36
+ // frontend 10 seconds to re-send the request until it's removed from requests' cache
37
+ unusedValueLifetime: (this._requestTimeout > 0) ? (this._requestTimeout + 10 * 1000) : undefined,
38
+ // attempt to clean up every second
39
+ cleanupInterval: 1000,
40
+ cleanupHandler: (id, _, reason) => {
41
+ if (reason !== "request") {
42
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Cleaning up request without frontend retrieving it: ${id}.`);
43
+ // istanbul ignore next
44
+ this._cancelEvents.get(id)?.raiseEvent();
45
+ }
46
+ this._cancelEvents.delete(id);
47
+ },
48
+ });
49
+ this._cancelEvents = new Map();
50
+ }
51
+ dispose() {
52
+ this._pendingRequests.dispose();
53
+ }
54
+ get requestTimeout() { return this._requestTimeout; }
55
+ get pendingRequests() { return this._pendingRequests; }
56
+ /** Returns an ok response with result inside */
57
+ successResponse(result, diagnostics) {
58
+ return {
59
+ statusCode: presentation_common_1.PresentationStatus.Success,
60
+ result,
61
+ diagnostics,
62
+ };
63
+ }
64
+ /** Returns a bad request response with empty result and an error code */
65
+ errorResponse(errorCode, errorMessage, diagnostics) {
66
+ return {
67
+ statusCode: errorCode,
68
+ result: undefined,
69
+ errorMessage,
70
+ diagnostics,
71
+ };
72
+ }
73
+ /**
74
+ * Get the [[PresentationManager]] used by this RPC impl.
75
+ */
76
+ getManager(clientId) {
77
+ return Presentation_1.Presentation.getManager(clientId);
78
+ }
79
+ getIModel(token) {
80
+ let imodel;
81
+ try {
82
+ imodel = core_backend_1.IModelDb.findByKey(token.key);
83
+ }
84
+ catch {
85
+ throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.InvalidArgument, "IModelRpcProps doesn't point to a valid iModel");
86
+ }
87
+ return imodel;
88
+ }
89
+ async makeRequest(token, requestId, requestOptions, request) {
90
+ const requestKey = JSON.stringify({ iModelKey: token.key, requestId, requestOptions });
91
+ core_bentley_1.Logger.logInfo(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Received '${requestId}' request. Params: ${requestKey}`);
92
+ let resultPromise = this._pendingRequests.getValue(requestKey);
93
+ if (resultPromise) {
94
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request already pending`);
95
+ }
96
+ else {
97
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request not found, creating a new one`);
98
+ let imodel;
99
+ try {
100
+ imodel = this.getIModel(token);
101
+ }
102
+ catch (e) {
103
+ (0, core_bentley_1.assert)(e instanceof Error);
104
+ return this.errorResponse(presentation_common_1.PresentationStatus.InvalidArgument, e.message);
105
+ }
106
+ const { clientId: _, diagnostics: diagnosticsOptions, rulesetVariables, ...options } = requestOptions;
107
+ const managerRequestOptions = {
108
+ ...options,
109
+ imodel,
110
+ cancelEvent: new core_bentley_1.BeEvent(),
111
+ };
112
+ // set up ruleset variables
113
+ if (rulesetVariables)
114
+ managerRequestOptions.rulesetVariables = rulesetVariables.map(presentation_common_1.RulesetVariable.fromJSON);
115
+ // set up diagnostics listener
116
+ let diagnostics;
117
+ const getDiagnostics = () => {
118
+ if (!diagnostics)
119
+ diagnostics = {};
120
+ return diagnostics;
121
+ };
122
+ if (diagnosticsOptions) {
123
+ if (diagnosticsOptions.backendVersion) {
124
+ getDiagnostics().backendVersion = packageJsonVersion;
125
+ }
126
+ managerRequestOptions.diagnostics = {
127
+ ...diagnosticsOptions,
128
+ handler: (d) => {
129
+ if (d.logs) {
130
+ const target = getDiagnostics();
131
+ if (target.logs)
132
+ target.logs.push(...d.logs);
133
+ else
134
+ target.logs = [...d.logs];
135
+ }
136
+ },
137
+ };
138
+ }
139
+ // initiate request
140
+ resultPromise = request(managerRequestOptions)
141
+ .then((result) => this.successResponse(result, diagnostics))
142
+ .catch((e) => this.errorResponse(e.errorNumber, e.message, diagnostics));
143
+ // store the request promise
144
+ this._pendingRequests.addValue(requestKey, resultPromise);
145
+ this._cancelEvents.set(requestKey, managerRequestOptions.cancelEvent);
146
+ }
147
+ if (this._requestTimeout === 0) {
148
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout not configured, returning promise without a timeout.`);
149
+ resultPromise.finally(() => {
150
+ this._pendingRequests.deleteValue(requestKey);
151
+ });
152
+ return resultPromise;
153
+ }
154
+ let timeout;
155
+ const timeoutPromise = new Promise((_resolve, reject) => {
156
+ timeout = setTimeout(() => {
157
+ reject();
158
+ }, this._requestTimeout);
159
+ });
160
+ /* eslint-disable @typescript-eslint/indent */
161
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Returning a promise with a timeout of ${this._requestTimeout}.`);
162
+ return Promise
163
+ .race([resultPromise, timeoutPromise])
164
+ .catch(() => {
165
+ // note: error responses from the manager get handled when creating `resultPromise`, so we can only get here due
166
+ // to a timeout exception
167
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout, returning "BackendTimeout" status.`);
168
+ return this.errorResponse(presentation_common_1.PresentationStatus.BackendTimeout);
169
+ })
170
+ .then((response) => {
171
+ if (response.statusCode !== presentation_common_1.PresentationStatus.BackendTimeout) {
172
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request completed, returning result.`);
173
+ this._pendingRequests.deleteValue(requestKey);
174
+ }
175
+ clearTimeout(timeout);
176
+ return response;
177
+ });
178
+ /* eslint-enable @typescript-eslint/indent */
179
+ }
180
+ async getNodesCount(token, requestOptions) {
181
+ return this.makeRequest(token, "getNodesCount", requestOptions, async (options) => {
182
+ options = {
183
+ ...options,
184
+ parentKey: nodeKeyFromJson(options.parentKey),
185
+ };
186
+ return this.getManager(requestOptions.clientId).getNodesCount(options);
187
+ });
188
+ }
189
+ // eslint-disable-next-line deprecation/deprecation
190
+ async getPagedNodes(token, requestOptions) {
191
+ return this.makeRequest(token, "getPagedNodes", requestOptions, async (options) => {
192
+ options = enforceValidPageSize({
193
+ ...options,
194
+ parentKey: nodeKeyFromJson(options.parentKey),
195
+ });
196
+ const [serializedNodesJson, count] = await Promise.all([
197
+ this.getManager(requestOptions.clientId).getDetail().getNodes(options),
198
+ this.getManager(requestOptions.clientId).getNodesCount(options),
199
+ ]);
200
+ // eslint-disable-next-line deprecation/deprecation
201
+ const nodesJson = JSON.parse(serializedNodesJson);
202
+ return {
203
+ total: count,
204
+ items: nodesJson.nodes,
205
+ };
206
+ });
207
+ }
208
+ async getNodesDescriptor(token, requestOptions) {
209
+ return this.makeRequest(token, "getNodesDescriptor", requestOptions, async (options) => {
210
+ options = {
211
+ ...options,
212
+ parentKey: nodeKeyFromJson(options.parentKey),
213
+ };
214
+ return this.getManager().getDetail().getNodesDescriptor(options);
215
+ });
216
+ }
217
+ // eslint-disable-next-line deprecation/deprecation
218
+ async getNodePaths(token, requestOptions) {
219
+ return this.makeRequest(token, "getNodePaths", requestOptions, async (options) => {
220
+ const result = await this.getManager(requestOptions.clientId).getNodePaths(options);
221
+ // eslint-disable-next-line deprecation/deprecation
222
+ return result.map(presentation_common_1.NodePathElement.toJSON);
223
+ });
224
+ }
225
+ // eslint-disable-next-line deprecation/deprecation
226
+ async getFilteredNodePaths(token, requestOptions) {
227
+ return this.makeRequest(token, "getFilteredNodePaths", requestOptions, async (options) => {
228
+ const result = await this.getManager(requestOptions.clientId).getFilteredNodePaths(options);
229
+ // eslint-disable-next-line deprecation/deprecation
230
+ return result.map(presentation_common_1.NodePathElement.toJSON);
231
+ });
232
+ }
233
+ async getContentSources(token, requestOptions) {
234
+ return this.makeRequest(token, "getContentSources", requestOptions, async (options) => {
235
+ const result = await this.getManager(requestOptions.clientId).getContentSources(options);
236
+ const classesMap = {};
237
+ const selectClasses = result.map((sci) => presentation_common_1.SelectClassInfo.toCompressedJSON(sci, classesMap));
238
+ return { sources: selectClasses, classesMap };
239
+ });
240
+ }
241
+ async getContentDescriptor(token, requestOptions) {
242
+ return this.makeRequest(token, "getContentDescriptor", requestOptions, async (options) => {
243
+ options = {
244
+ ...options,
245
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
246
+ };
247
+ if (options.transport === "unparsed-json") {
248
+ // Here we send a plain JSON string but we will parse it to DescriptorJSON on the frontend. This way we are
249
+ // bypassing unnecessary deserialization and serialization.
250
+ return Presentation_1.Presentation.getManager().getDetail().getContentDescriptor(options);
251
+ }
252
+ else {
253
+ // Support for older frontends that still expect a parsed descriptor
254
+ const descriptor = await Presentation_1.Presentation.getManager().getContentDescriptor(options);
255
+ return descriptor?.toJSON();
256
+ }
257
+ });
258
+ }
259
+ async getContentSetSize(token, requestOptions) {
260
+ return this.makeRequest(token, "getContentSetSize", requestOptions, async (options) => {
261
+ options = {
262
+ ...options,
263
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
264
+ };
265
+ return this.getManager(requestOptions.clientId).getContentSetSize(options);
266
+ });
267
+ }
268
+ async getPagedContent(token, requestOptions) {
269
+ return this.makeRequest(token, "getPagedContent", requestOptions, async (options) => {
270
+ options = enforceValidPageSize({
271
+ ...options,
272
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
273
+ });
274
+ const [size, content] = await Promise.all([
275
+ this.getManager(requestOptions.clientId).getContentSetSize(options),
276
+ this.getManager(requestOptions.clientId).getDetail().getContent(options),
277
+ ]);
278
+ if (!content)
279
+ return undefined;
280
+ return {
281
+ descriptor: content.descriptor.toJSON(),
282
+ contentSet: {
283
+ total: size,
284
+ items: content.contentSet.map((i) => i.toJSON()),
285
+ },
286
+ };
287
+ });
288
+ }
289
+ async getPagedContentSet(token, requestOptions) {
290
+ const content = await this.getPagedContent(token, requestOptions);
291
+ return this.successResponse(content.result ? content.result.contentSet : { total: 0, items: [] });
292
+ }
293
+ async getElementProperties(token, requestOptions) {
294
+ return this.makeRequest(token, "getElementProperties", { ...requestOptions }, async (options) => {
295
+ return this.getManager(requestOptions.clientId).getDetail().getElementProperties(options);
296
+ });
297
+ }
298
+ // eslint-disable-next-line deprecation/deprecation
299
+ async getPagedDistinctValues(token, requestOptions) {
300
+ return this.makeRequest(token, "getPagedDistinctValues", requestOptions, async (options) => {
301
+ options = enforceValidPageSize({
302
+ ...options,
303
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
304
+ });
305
+ const response = await this.getManager(requestOptions.clientId).getPagedDistinctValues(options);
306
+ return {
307
+ ...response,
308
+ // eslint-disable-next-line deprecation/deprecation
309
+ items: response.items.map(presentation_common_1.DisplayValueGroup.toJSON),
310
+ };
311
+ });
312
+ }
313
+ async getContentInstanceKeys(token, requestOptions) {
314
+ return this.makeRequest(token, "getContentInstanceKeys", requestOptions, async (options) => {
315
+ const { displayType, ...optionsNoDisplayType } = options;
316
+ options = enforceValidPageSize({
317
+ ...optionsNoDisplayType,
318
+ keys: presentation_common_1.KeySet.fromJSON(optionsNoDisplayType.keys),
319
+ descriptor: {
320
+ displayType,
321
+ contentFlags: presentation_common_1.ContentFlags.KeysOnly,
322
+ },
323
+ }, exports.MAX_ALLOWED_KEYS_PAGE_SIZE);
324
+ const [size, content] = await Promise.all([
325
+ this.getManager(requestOptions.clientId).getContentSetSize(options),
326
+ this.getManager(requestOptions.clientId).getDetail().getContent(options),
327
+ ]);
328
+ if (size === 0 || !content)
329
+ return { total: 0, items: new presentation_common_1.KeySet().toJSON() };
330
+ return {
331
+ total: size,
332
+ items: content.contentSet.reduce((keys, item) => keys.add(item.primaryKeys), new presentation_common_1.KeySet()).toJSON(),
333
+ };
334
+ });
335
+ }
336
+ async getDisplayLabelDefinition(token, requestOptions) {
337
+ return this.makeRequest(token, "getDisplayLabelDefinition", requestOptions, async (options) => {
338
+ const label = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinition(options);
339
+ return label;
340
+ });
341
+ }
342
+ async getPagedDisplayLabelDefinitions(token, requestOptions) {
343
+ const pageOpts = enforceValidPageSize({ paging: { start: 0, size: requestOptions.keys.length } });
344
+ if (pageOpts.paging.size < requestOptions.keys.length)
345
+ requestOptions.keys.splice(pageOpts.paging.size);
346
+ return this.makeRequest(token, "getPagedDisplayLabelDefinitions", requestOptions, async (options) => {
347
+ const labels = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinitions({ ...options, keys: options.keys });
348
+ return {
349
+ total: options.keys.length,
350
+ items: labels,
351
+ };
352
+ });
353
+ }
354
+ async getSelectionScopes(token, requestOptions) {
355
+ return this.makeRequest(token, "getSelectionScopes", requestOptions, async (options) => this.getManager(requestOptions.clientId).getSelectionScopes(options));
356
+ }
357
+ async computeSelection(token, requestOptions, ids, scopeId) {
358
+ return this.makeRequest(token, "computeSelection", requestOptions, async (options) => {
359
+ if (!(0, presentation_common_1.isComputeSelectionRequestOptions)(options)) {
360
+ options = {
361
+ ...options,
362
+ elementIds: ids,
363
+ scope: { id: scopeId },
364
+ };
365
+ }
366
+ const keys = await this.getManager(requestOptions.clientId).computeSelection(options);
367
+ return keys.toJSON();
368
+ });
369
+ }
370
+ }
371
+ exports.PresentationRpcImpl = PresentationRpcImpl;
372
+ const enforceValidPageSize = (requestOptions, maxPageSize = exports.MAX_ALLOWED_PAGE_SIZE) => {
373
+ const validPageSize = getValidPageSize(requestOptions.paging?.size, maxPageSize);
374
+ if (!requestOptions.paging || requestOptions.paging.size !== validPageSize)
375
+ return { ...requestOptions, paging: { ...requestOptions.paging, size: validPageSize } };
376
+ return requestOptions;
377
+ };
378
+ const getValidPageSize = (size, maxPageSize) => {
379
+ const requestedSize = size ?? 0;
380
+ return (requestedSize === 0 || requestedSize > maxPageSize) ? maxPageSize : requestedSize;
381
+ };
382
+ // eslint-disable-next-line deprecation/deprecation
383
+ const nodeKeyFromJson = (json) => {
384
+ if (!json)
385
+ return undefined;
386
+ // eslint-disable-next-line deprecation/deprecation
387
+ return presentation_common_1.NodeKey.fromJSON(json);
388
+ };
389
389
  //# sourceMappingURL=PresentationRpcImpl.js.map