@itwin/presentation-backend 4.0.0-dev.28 → 4.0.0-dev.32

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