@itwin/presentation-backend 4.0.0-dev.21 → 4.0.0-dev.23

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 +274 -274
  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 +140 -140
  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 +150 -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/PresentationIpcHandler.js.map +1 -1
  22. package/lib/cjs/presentation-backend/PresentationManager.d.ts +445 -445
  23. package/lib/cjs/presentation-backend/PresentationManager.js +311 -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.js +388 -388
  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 +282 -282
  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 +229 -229
  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 +106 -106
  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,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
+ /**
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
+ };
389
389
  //# sourceMappingURL=PresentationRpcImpl.js.map