@itwin/presentation-backend 3.6.0-dev.52 → 3.6.0-dev.54

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 (65) hide show
  1. package/lib/cjs/assets/primary-presentation-rules/ElementProperties.PresentationRuleSet.json +14 -14
  2. package/lib/cjs/assets/supplemental-presentation-rules/BisCore.PresentationRuleSet.json +761 -761
  3. package/lib/cjs/assets/supplemental-presentation-rules/Functional.PresentationRuleSet.json +139 -139
  4. package/lib/cjs/presentation-backend/BackendLoggerCategory.d.ts +41 -41
  5. package/lib/cjs/presentation-backend/BackendLoggerCategory.js +50 -50
  6. package/lib/cjs/presentation-backend/BackendLoggerCategory.js.map +1 -1
  7. package/lib/cjs/presentation-backend/Constants.d.ts +8 -8
  8. package/lib/cjs/presentation-backend/Constants.js +36 -36
  9. package/lib/cjs/presentation-backend/Constants.js.map +1 -1
  10. package/lib/cjs/presentation-backend/ElementPropertiesHelper.d.ts +11 -11
  11. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js +274 -274
  12. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js.map +1 -1
  13. package/lib/cjs/presentation-backend/NativePlatform.d.ts +87 -86
  14. package/lib/cjs/presentation-backend/NativePlatform.d.ts.map +1 -1
  15. package/lib/cjs/presentation-backend/NativePlatform.js +140 -139
  16. package/lib/cjs/presentation-backend/NativePlatform.js.map +1 -1
  17. package/lib/cjs/presentation-backend/Presentation.d.ts +106 -106
  18. package/lib/cjs/presentation-backend/Presentation.js +150 -150
  19. package/lib/cjs/presentation-backend/Presentation.js.map +1 -1
  20. package/lib/cjs/presentation-backend/PresentationIpcHandler.d.ts +12 -12
  21. package/lib/cjs/presentation-backend/PresentationIpcHandler.js +41 -41
  22. package/lib/cjs/presentation-backend/PresentationIpcHandler.js.map +1 -1
  23. package/lib/cjs/presentation-backend/PresentationManager.d.ts +441 -436
  24. package/lib/cjs/presentation-backend/PresentationManager.d.ts.map +1 -1
  25. package/lib/cjs/presentation-backend/PresentationManager.js +303 -292
  26. package/lib/cjs/presentation-backend/PresentationManager.js.map +1 -1
  27. package/lib/cjs/presentation-backend/PresentationManagerDetail.d.ts +61 -60
  28. package/lib/cjs/presentation-backend/PresentationManagerDetail.d.ts.map +1 -1
  29. package/lib/cjs/presentation-backend/PresentationManagerDetail.js +430 -420
  30. package/lib/cjs/presentation-backend/PresentationManagerDetail.js.map +1 -1
  31. package/lib/cjs/presentation-backend/PresentationRpcImpl.d.ts +62 -61
  32. package/lib/cjs/presentation-backend/PresentationRpcImpl.d.ts.map +1 -1
  33. package/lib/cjs/presentation-backend/PresentationRpcImpl.js +378 -365
  34. package/lib/cjs/presentation-backend/PresentationRpcImpl.js.map +1 -1
  35. package/lib/cjs/presentation-backend/RulesetEmbedder.d.ts +104 -104
  36. package/lib/cjs/presentation-backend/RulesetEmbedder.js +282 -282
  37. package/lib/cjs/presentation-backend/RulesetEmbedder.js.map +1 -1
  38. package/lib/cjs/presentation-backend/RulesetManager.d.ts +53 -53
  39. package/lib/cjs/presentation-backend/RulesetManager.js +73 -73
  40. package/lib/cjs/presentation-backend/RulesetManager.js.map +1 -1
  41. package/lib/cjs/presentation-backend/RulesetVariablesManager.d.ts +140 -140
  42. package/lib/cjs/presentation-backend/RulesetVariablesManager.js +129 -129
  43. package/lib/cjs/presentation-backend/RulesetVariablesManager.js.map +1 -1
  44. package/lib/cjs/presentation-backend/SelectionScopesHelper.d.ts +35 -35
  45. package/lib/cjs/presentation-backend/SelectionScopesHelper.js +229 -229
  46. package/lib/cjs/presentation-backend/SelectionScopesHelper.js.map +1 -1
  47. package/lib/cjs/presentation-backend/TemporaryStorage.d.ts +123 -123
  48. package/lib/cjs/presentation-backend/TemporaryStorage.js +151 -151
  49. package/lib/cjs/presentation-backend/TemporaryStorage.js.map +1 -1
  50. package/lib/cjs/presentation-backend/UpdatesTracker.d.ts +27 -27
  51. package/lib/cjs/presentation-backend/UpdatesTracker.js +54 -54
  52. package/lib/cjs/presentation-backend/UpdatesTracker.js.map +1 -1
  53. package/lib/cjs/presentation-backend/Utils.d.ts +49 -49
  54. package/lib/cjs/presentation-backend/Utils.js +106 -106
  55. package/lib/cjs/presentation-backend/Utils.js.map +1 -1
  56. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.d.ts +16 -16
  57. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js +51 -51
  58. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js.map +1 -1
  59. package/lib/cjs/presentation-backend/domain/RulesetElements.d.ts +21 -21
  60. package/lib/cjs/presentation-backend/domain/RulesetElements.js +38 -38
  61. package/lib/cjs/presentation-backend/domain/RulesetElements.js.map +1 -1
  62. package/lib/cjs/presentation-backend.d.ts +19 -19
  63. package/lib/cjs/presentation-backend.js +35 -35
  64. package/lib/cjs/presentation-backend.js.map +1 -1
  65. package/package.json +13 -13
@@ -1,366 +1,379 @@
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
- async getPagedNodes(token, requestOptions) {
189
- return this.makeRequest(token, "getPagedNodes", requestOptions, async (options) => {
190
- options = enforceValidPageSize({
191
- ...options,
192
- parentKey: nodeKeyFromJson(options.parentKey),
193
- });
194
- const [nodes, count] = await Promise.all([
195
- this.getManager(requestOptions.clientId).getDetail().getNodes(options),
196
- this.getManager(requestOptions.clientId).getNodesCount(options),
197
- ]);
198
- return { total: count, items: nodes.map(presentation_common_1.Node.toJSON) };
199
- });
200
- }
201
- async getNodePaths(token, requestOptions) {
202
- return this.makeRequest(token, "getNodePaths", requestOptions, async (options) => {
203
- const result = await this.getManager(requestOptions.clientId).getNodePaths(options);
204
- return result.map(presentation_common_1.NodePathElement.toJSON);
205
- });
206
- }
207
- async getFilteredNodePaths(token, requestOptions) {
208
- return this.makeRequest(token, "getFilteredNodePaths", requestOptions, async (options) => {
209
- const result = await this.getManager(requestOptions.clientId).getFilteredNodePaths(options);
210
- return result.map(presentation_common_1.NodePathElement.toJSON);
211
- });
212
- }
213
- async getContentSources(token, requestOptions) {
214
- return this.makeRequest(token, "getContentSources", requestOptions, async (options) => {
215
- const result = await this.getManager(requestOptions.clientId).getContentSources(options);
216
- const classesMap = {};
217
- const selectClasses = result.map((sci) => presentation_common_1.SelectClassInfo.toCompressedJSON(sci, classesMap));
218
- return { sources: selectClasses, classesMap };
219
- });
220
- }
221
- async getContentDescriptor(token, requestOptions) {
222
- return this.makeRequest(token, "getContentDescriptor", requestOptions, async (options) => {
223
- options = {
224
- ...options,
225
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
226
- };
227
- if (options.transport === "unparsed-json") {
228
- // Here we send a plain JSON string but we will parse it to DescriptorJSON on the frontend. This way we are
229
- // bypassing unnecessary deserialization and serialization.
230
- return Presentation_1.Presentation.getManager().getDetail().getContentDescriptor(options);
231
- }
232
- else {
233
- // Support for older frontends that still expect a parsed descriptor
234
- const descriptor = await Presentation_1.Presentation.getManager().getContentDescriptor(options);
235
- return descriptor === null || descriptor === void 0 ? void 0 : descriptor.toJSON();
236
- }
237
- });
238
- }
239
- async getContentSetSize(token, requestOptions) {
240
- return this.makeRequest(token, "getContentSetSize", requestOptions, async (options) => {
241
- options = {
242
- ...options,
243
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
244
- };
245
- return this.getManager(requestOptions.clientId).getContentSetSize(options);
246
- });
247
- }
248
- async getPagedContent(token, requestOptions) {
249
- return this.makeRequest(token, "getPagedContent", requestOptions, async (options) => {
250
- options = enforceValidPageSize({
251
- ...options,
252
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
253
- });
254
- const [size, content] = await Promise.all([
255
- this.getManager(requestOptions.clientId).getContentSetSize(options),
256
- this.getManager(requestOptions.clientId).getDetail().getContent(options),
257
- ]);
258
- if (!content)
259
- return undefined;
260
- return {
261
- descriptor: content.descriptor.toJSON(),
262
- contentSet: {
263
- total: size,
264
- items: content.contentSet.map((i) => i.toJSON()),
265
- },
266
- };
267
- });
268
- }
269
- async getPagedContentSet(token, requestOptions) {
270
- const content = await this.getPagedContent(token, requestOptions);
271
- return this.successResponse(content.result ? content.result.contentSet : { total: 0, items: [] });
272
- }
273
- async getElementProperties(token, requestOptions) {
274
- return this.makeRequest(token, "getElementProperties", { ...requestOptions }, async (options) => {
275
- return this.getManager(requestOptions.clientId).getDetail().getElementProperties(options);
276
- });
277
- }
278
- async getPagedDistinctValues(token, requestOptions) {
279
- return this.makeRequest(token, "getPagedDistinctValues", requestOptions, async (options) => {
280
- options = enforceValidPageSize({
281
- ...options,
282
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
283
- });
284
- const response = await this.getManager(requestOptions.clientId).getPagedDistinctValues(options);
285
- return {
286
- ...response,
287
- items: response.items.map(presentation_common_1.DisplayValueGroup.toJSON),
288
- };
289
- });
290
- }
291
- async getContentInstanceKeys(token, requestOptions) {
292
- return this.makeRequest(token, "getContentInstanceKeys", requestOptions, async (options) => {
293
- const { displayType, ...optionsNoDisplayType } = options;
294
- options = enforceValidPageSize({
295
- ...optionsNoDisplayType,
296
- keys: presentation_common_1.KeySet.fromJSON(optionsNoDisplayType.keys),
297
- descriptor: {
298
- displayType,
299
- contentFlags: presentation_common_1.ContentFlags.KeysOnly,
300
- },
301
- }, exports.MAX_ALLOWED_KEYS_PAGE_SIZE);
302
- const [size, content] = await Promise.all([
303
- this.getManager(requestOptions.clientId).getContentSetSize(options),
304
- this.getManager(requestOptions.clientId).getDetail().getContent(options),
305
- ]);
306
- if (size === 0 || !content)
307
- return { total: 0, items: new presentation_common_1.KeySet().toJSON() };
308
- return {
309
- total: size,
310
- items: content.contentSet.reduce((keys, item) => keys.add(item.primaryKeys), new presentation_common_1.KeySet()).toJSON(),
311
- };
312
- });
313
- }
314
- async getDisplayLabelDefinition(token, requestOptions) {
315
- return this.makeRequest(token, "getDisplayLabelDefinition", requestOptions, async (options) => {
316
- const label = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinition(options);
317
- return presentation_common_1.LabelDefinition.toJSON(label);
318
- });
319
- }
320
- async getPagedDisplayLabelDefinitions(token, requestOptions) {
321
- const pageOpts = enforceValidPageSize({ paging: { start: 0, size: requestOptions.keys.length } });
322
- if (pageOpts.paging.size < requestOptions.keys.length)
323
- requestOptions.keys.splice(pageOpts.paging.size);
324
- return this.makeRequest(token, "getPagedDisplayLabelDefinitions", requestOptions, async (options) => {
325
- const labels = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinitions({ ...options, keys: options.keys.map(presentation_common_1.InstanceKey.fromJSON) });
326
- return {
327
- total: options.keys.length,
328
- items: labels.map(presentation_common_1.LabelDefinition.toJSON),
329
- };
330
- });
331
- }
332
- async getSelectionScopes(token, requestOptions) {
333
- return this.makeRequest(token, "getSelectionScopes", requestOptions, async (options) => this.getManager(requestOptions.clientId).getSelectionScopes(options));
334
- }
335
- async computeSelection(token, requestOptions, ids, scopeId) {
336
- return this.makeRequest(token, "computeSelection", requestOptions, async (options) => {
337
- if (!(0, presentation_common_1.isComputeSelectionRequestOptions)(options)) {
338
- options = {
339
- ...options,
340
- elementIds: ids,
341
- scope: { id: scopeId },
342
- };
343
- }
344
- const keys = await this.getManager(requestOptions.clientId).computeSelection(options);
345
- return keys.toJSON();
346
- });
347
- }
348
- }
349
- exports.PresentationRpcImpl = PresentationRpcImpl;
350
- const enforceValidPageSize = (requestOptions, maxPageSize = exports.MAX_ALLOWED_PAGE_SIZE) => {
351
- var _a;
352
- const validPageSize = getValidPageSize((_a = requestOptions.paging) === null || _a === void 0 ? void 0 : _a.size, maxPageSize);
353
- if (!requestOptions.paging || requestOptions.paging.size !== validPageSize)
354
- return { ...requestOptions, paging: { ...requestOptions.paging, size: validPageSize } };
355
- return requestOptions;
356
- };
357
- const getValidPageSize = (size, maxPageSize) => {
358
- const requestedSize = size !== null && size !== void 0 ? size : 0;
359
- return (requestedSize === 0 || requestedSize > maxPageSize) ? maxPageSize : requestedSize;
360
- };
361
- const nodeKeyFromJson = (json) => {
362
- if (!json)
363
- return undefined;
364
- return presentation_common_1.NodeKey.fromJSON(json);
365
- };
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
+ async getPagedNodes(token, requestOptions) {
189
+ return this.makeRequest(token, "getPagedNodes", requestOptions, async (options) => {
190
+ options = enforceValidPageSize({
191
+ ...options,
192
+ parentKey: nodeKeyFromJson(options.parentKey),
193
+ });
194
+ const [serializedNodesJson, count] = await Promise.all([
195
+ this.getManager(requestOptions.clientId).getDetail().getNodes(options),
196
+ this.getManager(requestOptions.clientId).getNodesCount(options),
197
+ ]);
198
+ const nodesJson = JSON.parse(serializedNodesJson);
199
+ return {
200
+ total: count,
201
+ items: nodesJson.nodes,
202
+ };
203
+ });
204
+ }
205
+ async getNodesDescriptor(token, requestOptions) {
206
+ return this.makeRequest(token, "getNodesDescriptor", requestOptions, async (options) => {
207
+ options = {
208
+ ...options,
209
+ parentKey: nodeKeyFromJson(options.parentKey),
210
+ };
211
+ return this.getManager().getDetail().getNodesDescriptor(options);
212
+ });
213
+ }
214
+ async getNodePaths(token, requestOptions) {
215
+ return this.makeRequest(token, "getNodePaths", requestOptions, async (options) => {
216
+ const result = await this.getManager(requestOptions.clientId).getNodePaths(options);
217
+ return result.map(presentation_common_1.NodePathElement.toJSON);
218
+ });
219
+ }
220
+ async getFilteredNodePaths(token, requestOptions) {
221
+ return this.makeRequest(token, "getFilteredNodePaths", requestOptions, async (options) => {
222
+ const result = await this.getManager(requestOptions.clientId).getFilteredNodePaths(options);
223
+ return result.map(presentation_common_1.NodePathElement.toJSON);
224
+ });
225
+ }
226
+ async getContentSources(token, requestOptions) {
227
+ return this.makeRequest(token, "getContentSources", requestOptions, async (options) => {
228
+ const result = await this.getManager(requestOptions.clientId).getContentSources(options);
229
+ const classesMap = {};
230
+ const selectClasses = result.map((sci) => presentation_common_1.SelectClassInfo.toCompressedJSON(sci, classesMap));
231
+ return { sources: selectClasses, classesMap };
232
+ });
233
+ }
234
+ async getContentDescriptor(token, requestOptions) {
235
+ return this.makeRequest(token, "getContentDescriptor", requestOptions, async (options) => {
236
+ options = {
237
+ ...options,
238
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
239
+ };
240
+ if (options.transport === "unparsed-json") {
241
+ // Here we send a plain JSON string but we will parse it to DescriptorJSON on the frontend. This way we are
242
+ // bypassing unnecessary deserialization and serialization.
243
+ return Presentation_1.Presentation.getManager().getDetail().getContentDescriptor(options);
244
+ }
245
+ else {
246
+ // Support for older frontends that still expect a parsed descriptor
247
+ const descriptor = await Presentation_1.Presentation.getManager().getContentDescriptor(options);
248
+ return descriptor === null || descriptor === void 0 ? void 0 : descriptor.toJSON();
249
+ }
250
+ });
251
+ }
252
+ async getContentSetSize(token, requestOptions) {
253
+ return this.makeRequest(token, "getContentSetSize", requestOptions, async (options) => {
254
+ options = {
255
+ ...options,
256
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
257
+ };
258
+ return this.getManager(requestOptions.clientId).getContentSetSize(options);
259
+ });
260
+ }
261
+ async getPagedContent(token, requestOptions) {
262
+ return this.makeRequest(token, "getPagedContent", requestOptions, async (options) => {
263
+ options = enforceValidPageSize({
264
+ ...options,
265
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
266
+ });
267
+ const [size, content] = await Promise.all([
268
+ this.getManager(requestOptions.clientId).getContentSetSize(options),
269
+ this.getManager(requestOptions.clientId).getDetail().getContent(options),
270
+ ]);
271
+ if (!content)
272
+ return undefined;
273
+ return {
274
+ descriptor: content.descriptor.toJSON(),
275
+ contentSet: {
276
+ total: size,
277
+ items: content.contentSet.map((i) => i.toJSON()),
278
+ },
279
+ };
280
+ });
281
+ }
282
+ async getPagedContentSet(token, requestOptions) {
283
+ const content = await this.getPagedContent(token, requestOptions);
284
+ return this.successResponse(content.result ? content.result.contentSet : { total: 0, items: [] });
285
+ }
286
+ async getElementProperties(token, requestOptions) {
287
+ return this.makeRequest(token, "getElementProperties", { ...requestOptions }, async (options) => {
288
+ return this.getManager(requestOptions.clientId).getDetail().getElementProperties(options);
289
+ });
290
+ }
291
+ async getPagedDistinctValues(token, requestOptions) {
292
+ return this.makeRequest(token, "getPagedDistinctValues", requestOptions, async (options) => {
293
+ options = enforceValidPageSize({
294
+ ...options,
295
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
296
+ });
297
+ const response = await this.getManager(requestOptions.clientId).getPagedDistinctValues(options);
298
+ return {
299
+ ...response,
300
+ items: response.items.map(presentation_common_1.DisplayValueGroup.toJSON),
301
+ };
302
+ });
303
+ }
304
+ async getContentInstanceKeys(token, requestOptions) {
305
+ return this.makeRequest(token, "getContentInstanceKeys", requestOptions, async (options) => {
306
+ const { displayType, ...optionsNoDisplayType } = options;
307
+ options = enforceValidPageSize({
308
+ ...optionsNoDisplayType,
309
+ keys: presentation_common_1.KeySet.fromJSON(optionsNoDisplayType.keys),
310
+ descriptor: {
311
+ displayType,
312
+ contentFlags: presentation_common_1.ContentFlags.KeysOnly,
313
+ },
314
+ }, exports.MAX_ALLOWED_KEYS_PAGE_SIZE);
315
+ const [size, content] = await Promise.all([
316
+ this.getManager(requestOptions.clientId).getContentSetSize(options),
317
+ this.getManager(requestOptions.clientId).getDetail().getContent(options),
318
+ ]);
319
+ if (size === 0 || !content)
320
+ return { total: 0, items: new presentation_common_1.KeySet().toJSON() };
321
+ return {
322
+ total: size,
323
+ items: content.contentSet.reduce((keys, item) => keys.add(item.primaryKeys), new presentation_common_1.KeySet()).toJSON(),
324
+ };
325
+ });
326
+ }
327
+ async getDisplayLabelDefinition(token, requestOptions) {
328
+ return this.makeRequest(token, "getDisplayLabelDefinition", requestOptions, async (options) => {
329
+ const label = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinition(options);
330
+ return presentation_common_1.LabelDefinition.toJSON(label);
331
+ });
332
+ }
333
+ async getPagedDisplayLabelDefinitions(token, requestOptions) {
334
+ const pageOpts = enforceValidPageSize({ paging: { start: 0, size: requestOptions.keys.length } });
335
+ if (pageOpts.paging.size < requestOptions.keys.length)
336
+ requestOptions.keys.splice(pageOpts.paging.size);
337
+ return this.makeRequest(token, "getPagedDisplayLabelDefinitions", requestOptions, async (options) => {
338
+ const labels = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinitions({ ...options, keys: options.keys.map(presentation_common_1.InstanceKey.fromJSON) });
339
+ return {
340
+ total: options.keys.length,
341
+ items: labels.map(presentation_common_1.LabelDefinition.toJSON),
342
+ };
343
+ });
344
+ }
345
+ async getSelectionScopes(token, requestOptions) {
346
+ return this.makeRequest(token, "getSelectionScopes", requestOptions, async (options) => this.getManager(requestOptions.clientId).getSelectionScopes(options));
347
+ }
348
+ async computeSelection(token, requestOptions, ids, scopeId) {
349
+ return this.makeRequest(token, "computeSelection", requestOptions, async (options) => {
350
+ if (!(0, presentation_common_1.isComputeSelectionRequestOptions)(options)) {
351
+ options = {
352
+ ...options,
353
+ elementIds: ids,
354
+ scope: { id: scopeId },
355
+ };
356
+ }
357
+ const keys = await this.getManager(requestOptions.clientId).computeSelection(options);
358
+ return keys.toJSON();
359
+ });
360
+ }
361
+ }
362
+ exports.PresentationRpcImpl = PresentationRpcImpl;
363
+ const enforceValidPageSize = (requestOptions, maxPageSize = exports.MAX_ALLOWED_PAGE_SIZE) => {
364
+ var _a;
365
+ const validPageSize = getValidPageSize((_a = requestOptions.paging) === null || _a === void 0 ? void 0 : _a.size, maxPageSize);
366
+ if (!requestOptions.paging || requestOptions.paging.size !== validPageSize)
367
+ return { ...requestOptions, paging: { ...requestOptions.paging, size: validPageSize } };
368
+ return requestOptions;
369
+ };
370
+ const getValidPageSize = (size, maxPageSize) => {
371
+ const requestedSize = size !== null && size !== void 0 ? size : 0;
372
+ return (requestedSize === 0 || requestedSize > maxPageSize) ? maxPageSize : requestedSize;
373
+ };
374
+ const nodeKeyFromJson = (json) => {
375
+ if (!json)
376
+ return undefined;
377
+ return presentation_common_1.NodeKey.fromJSON(json);
378
+ };
366
379
  //# sourceMappingURL=PresentationRpcImpl.js.map