@elliemae/ssf-host 2.0.0-next.29

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 (103) hide show
  1. package/dist/cjs/app.config.json +20 -0
  2. package/dist/cjs/guest.js +196 -0
  3. package/dist/cjs/host.js +930 -0
  4. package/dist/cjs/ihost.js +16 -0
  5. package/dist/cjs/index.html +183 -0
  6. package/dist/cjs/index.js +33 -0
  7. package/dist/cjs/package.json +4 -0
  8. package/dist/cjs/tests/guest.html +14 -0
  9. package/dist/cjs/tests/logger.js +31 -0
  10. package/dist/cjs/tests/scriptingObjects/appEvents.js +16 -0
  11. package/dist/cjs/tests/scriptingObjects/appObjects.js +16 -0
  12. package/dist/cjs/tests/scriptingObjects/appraisalServiceModule.js +127 -0
  13. package/dist/cjs/tests/scriptingObjects/constant.js +38 -0
  14. package/dist/cjs/tests/scriptingObjects/floodServiceModule.js +49 -0
  15. package/dist/cjs/tests/server.js +29 -0
  16. package/dist/cjs/tests/serverHandlers.js +50 -0
  17. package/dist/cjs/tests/utils.js +76 -0
  18. package/dist/cjs/types.js +46 -0
  19. package/dist/cjs/utils.js +57 -0
  20. package/dist/esm/app.config.json +20 -0
  21. package/dist/esm/guest.js +177 -0
  22. package/dist/esm/host.js +919 -0
  23. package/dist/esm/ihost.js +0 -0
  24. package/dist/esm/index.html +183 -0
  25. package/dist/esm/index.js +13 -0
  26. package/dist/esm/package.json +4 -0
  27. package/dist/esm/tests/guest.html +14 -0
  28. package/dist/esm/tests/logger.js +15 -0
  29. package/dist/esm/tests/scriptingObjects/appEvents.js +0 -0
  30. package/dist/esm/tests/scriptingObjects/appObjects.js +0 -0
  31. package/dist/esm/tests/scriptingObjects/appraisalServiceModule.js +112 -0
  32. package/dist/esm/tests/scriptingObjects/constant.js +18 -0
  33. package/dist/esm/tests/scriptingObjects/floodServiceModule.js +29 -0
  34. package/dist/esm/tests/server.js +9 -0
  35. package/dist/esm/tests/serverHandlers.js +20 -0
  36. package/dist/esm/tests/utils.js +56 -0
  37. package/dist/esm/types.js +26 -0
  38. package/dist/esm/utils.js +37 -0
  39. package/dist/public/app.config.json +20 -0
  40. package/dist/public/autoFill.js +3 -0
  41. package/dist/public/autoFill.js.br +0 -0
  42. package/dist/public/autoFill.js.gz +0 -0
  43. package/dist/public/autoFill.js.map +1 -0
  44. package/dist/public/index.html +1 -0
  45. package/dist/public/init.js +3 -0
  46. package/dist/public/init.js.br +0 -0
  47. package/dist/public/init.js.gz +0 -0
  48. package/dist/public/init.js.map +1 -0
  49. package/dist/public/js/emuiSsfHost.c8f735724b6fe44f4dd6.js +3 -0
  50. package/dist/public/js/emuiSsfHost.c8f735724b6fe44f4dd6.js.br +0 -0
  51. package/dist/public/js/emuiSsfHost.c8f735724b6fe44f4dd6.js.gz +0 -0
  52. package/dist/public/js/emuiSsfHost.c8f735724b6fe44f4dd6.js.map +1 -0
  53. package/dist/public/loan-object.js +3 -0
  54. package/dist/public/loan-object.js.br +1 -0
  55. package/dist/public/loan-object.js.gz +0 -0
  56. package/dist/public/loan-object.js.map +1 -0
  57. package/dist/public/utils.js +3 -0
  58. package/dist/public/utils.js.br +0 -0
  59. package/dist/public/utils.js.gz +0 -0
  60. package/dist/public/utils.js.map +1 -0
  61. package/dist/types/guest.d.ts +140 -0
  62. package/dist/types/host.d.ts +107 -0
  63. package/dist/types/ihost.d.ts +138 -0
  64. package/dist/types/index.d.ts +8 -0
  65. package/dist/types/tests/event.test.d.ts +1 -0
  66. package/dist/types/tests/guest.test.d.ts +1 -0
  67. package/dist/types/tests/guestAsHost.test.d.ts +1 -0
  68. package/dist/types/tests/host.test.d.ts +1 -0
  69. package/dist/types/tests/logger.d.ts +3 -0
  70. package/dist/types/tests/scriptingObject.test.d.ts +1 -0
  71. package/dist/types/tests/scriptingObjects/appEvents.d.ts +3 -0
  72. package/dist/types/tests/scriptingObjects/appObjects.d.ts +7 -0
  73. package/dist/types/tests/scriptingObjects/appraisalServiceModule.d.ts +46 -0
  74. package/dist/types/tests/scriptingObjects/constant.d.ts +8 -0
  75. package/dist/types/tests/scriptingObjects/floodServiceModule.d.ts +17 -0
  76. package/dist/types/tests/server.d.ts +1 -0
  77. package/dist/types/tests/serverHandlers.d.ts +1 -0
  78. package/dist/types/tests/utils.d.ts +28 -0
  79. package/dist/types/types.d.ts +145 -0
  80. package/dist/types/utils.d.ts +8 -0
  81. package/dist/umd/app.config.json +20 -0
  82. package/dist/umd/autoFill.js +3 -0
  83. package/dist/umd/autoFill.js.br +0 -0
  84. package/dist/umd/autoFill.js.gz +0 -0
  85. package/dist/umd/autoFill.js.map +1 -0
  86. package/dist/umd/index.html +1 -0
  87. package/dist/umd/index.js +3 -0
  88. package/dist/umd/index.js.br +0 -0
  89. package/dist/umd/index.js.gz +0 -0
  90. package/dist/umd/index.js.map +1 -0
  91. package/dist/umd/init.js +3 -0
  92. package/dist/umd/init.js.br +0 -0
  93. package/dist/umd/init.js.gz +0 -0
  94. package/dist/umd/init.js.map +1 -0
  95. package/dist/umd/loan-object.js +3 -0
  96. package/dist/umd/loan-object.js.br +1 -0
  97. package/dist/umd/loan-object.js.gz +0 -0
  98. package/dist/umd/loan-object.js.map +1 -0
  99. package/dist/umd/utils.js +3 -0
  100. package/dist/umd/utils.js.br +0 -0
  101. package/dist/umd/utils.js.gz +0 -0
  102. package/dist/umd/utils.js.map +1 -0
  103. package/package.json +69 -0
@@ -0,0 +1,930 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => {
21
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+ return value;
23
+ };
24
+ var __accessCheck = (obj, member, msg) => {
25
+ if (!member.has(obj))
26
+ throw TypeError("Cannot " + msg);
27
+ };
28
+ var __privateGet = (obj, member, getter) => {
29
+ __accessCheck(obj, member, "read from private field");
30
+ return getter ? getter.call(obj) : member.get(obj);
31
+ };
32
+ var __privateAdd = (obj, member, value) => {
33
+ if (member.has(obj))
34
+ throw TypeError("Cannot add the same private member more than once");
35
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
36
+ };
37
+ var __privateSet = (obj, member, value, setter) => {
38
+ __accessCheck(obj, member, "write to private field");
39
+ setter ? setter.call(obj, value) : member.set(obj, value);
40
+ return value;
41
+ };
42
+ var host_exports = {};
43
+ __export(host_exports, {
44
+ SSFHost: () => SSFHost
45
+ });
46
+ module.exports = __toCommonJS(host_exports);
47
+ var import_uuid = require("uuid");
48
+ var import_ssf_common = require("@elliemae/ssf-common");
49
+ var import_types = require("./types.js");
50
+ var import_guest = require("./guest.js");
51
+ var import_utils = require("./utils.js");
52
+ var _remoting, _correlationId, _logger, _nextGuestId, _guests, _publishedObjects, _readyStateCallback, _popupGuestMonitor, _closeAllPopupGuests, _getGuestForWindow, _getGuestForUrl, _isScriptingObject, _encodeResponse, _encodeException, _dispatchConfigEvent, _invoke, _handleGuestReadyComplete, _handleGuestReady, _handleGuestClose, _handleObjectGet, _handleGuestResize, _handleObjectInvoke, _subscribeToGuestResizeEvent, _connect, _isProxyEvent, _attachGuest, _monitorPopupGuests, _openPopupGuest, _openEmbedGuest, _getGuestUrl;
53
+ const SANDBOX_DEFAULT = [
54
+ import_types.IFrameSandboxValues.AllowScripts,
55
+ import_types.IFrameSandboxValues.AllowPopups,
56
+ import_types.IFrameSandboxValues.AllowModals,
57
+ import_types.IFrameSandboxValues.AllowForms,
58
+ import_types.IFrameSandboxValues.AllowDownloads,
59
+ import_types.IFrameSandboxValues.AllowSameOrigin
60
+ ].join(" ");
61
+ class SSFHost {
62
+ /**
63
+ * Create a new host
64
+ *
65
+ * @param hostId unique identifier for the host
66
+ * @param {HostOption} option - options for the host
67
+ */
68
+ constructor(hostId, option) {
69
+ /* eslint-enable indent */
70
+ /**
71
+ * unique identifier for the host
72
+ */
73
+ __publicField(this, "hostId");
74
+ /**
75
+ * reference to the remoting object
76
+ */
77
+ __privateAdd(this, _remoting, void 0);
78
+ __privateAdd(this, _correlationId, void 0);
79
+ /**
80
+ * reference to the logger
81
+ */
82
+ __privateAdd(this, _logger, void 0);
83
+ /**
84
+ * next guest id
85
+ */
86
+ __privateAdd(this, _nextGuestId, 1);
87
+ /**
88
+ * list of guests
89
+ */
90
+ __privateAdd(this, _guests, /* @__PURE__ */ new Map());
91
+ /**
92
+ * list of published scripting objects
93
+ */
94
+ __privateAdd(this, _publishedObjects, /* @__PURE__ */ new Map());
95
+ /**
96
+ * callback for ready state
97
+ */
98
+ __privateAdd(this, _readyStateCallback, null);
99
+ /**
100
+ *
101
+ */
102
+ __privateAdd(this, _popupGuestMonitor, null);
103
+ __privateAdd(this, _closeAllPopupGuests, () => {
104
+ for (const guest of __privateGet(this, _guests).values()) {
105
+ if (guest.openMode === import_types.OpenMode.Popup) {
106
+ this.unloadGuest(guest.id);
107
+ }
108
+ }
109
+ });
110
+ /**
111
+ * get the reference to the guest application by its window
112
+ *
113
+ * @param guestWindow reference to the guest window
114
+ * @returns reference to the guest
115
+ */
116
+ __privateAdd(this, _getGuestForWindow, (guestWindow) => {
117
+ for (const guest of __privateGet(this, _guests).values()) {
118
+ if (guest.window === guestWindow)
119
+ return guest;
120
+ }
121
+ return null;
122
+ });
123
+ /**
124
+ * get the reference to the guest application by its window
125
+ *
126
+ * @param url url of the guest application
127
+ * @returns reference to the guest
128
+ */
129
+ __privateAdd(this, _getGuestForUrl, (url) => {
130
+ for (const guest of __privateGet(this, _guests).values()) {
131
+ if (guest.url === url)
132
+ return guest;
133
+ }
134
+ return null;
135
+ });
136
+ /**
137
+ * check if a object is a scripting object
138
+ *
139
+ * @param value javascript object
140
+ * @returns true if the object is a scripting object
141
+ */
142
+ __privateAdd(this, _isScriptingObject, (value) => (
143
+ // eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-unsafe-member-access
144
+ typeof value?._toJSON === "function"
145
+ ));
146
+ __privateAdd(this, _encodeResponse, (val) => {
147
+ if (__privateGet(this, _isScriptingObject).call(this, val)) {
148
+ this.addScriptingObject(val);
149
+ return { type: "object", object: val._toJSON() };
150
+ }
151
+ return { type: "value", value: val };
152
+ });
153
+ __privateAdd(this, _encodeException, (ex) => {
154
+ if (typeof ex === "string") {
155
+ return ex;
156
+ }
157
+ if (ex instanceof Error) {
158
+ return ex.message;
159
+ }
160
+ return "An unexpected error occurred in the host application";
161
+ });
162
+ __privateAdd(this, _dispatchConfigEvent, (guest) => {
163
+ if (guest.ready) {
164
+ __privateGet(this, _remoting).send({
165
+ targetWin: guest.window,
166
+ targetOrigin: guest.origin,
167
+ messageType: import_ssf_common.MessageType.HostConfig,
168
+ messageBody: {
169
+ logLevel: __privateGet(this, _logger).getLogLevel(),
170
+ ...guest.getInfo()
171
+ }
172
+ });
173
+ }
174
+ });
175
+ __privateAdd(this, _invoke, ({
176
+ guest,
177
+ obj,
178
+ functionName,
179
+ functionParams
180
+ }) => {
181
+ const func = obj[functionName];
182
+ if (!(0, import_utils.isFunction)(func)) {
183
+ __privateGet(this, _logger).warn(
184
+ `Attempt to call invalid function on object type ${obj.objectType}: ${String(functionName)}`
185
+ );
186
+ return Promise.reject(
187
+ new Error(
188
+ `Method '${functionName}' not found in Scripting Object '${obj.id}'`
189
+ )
190
+ );
191
+ }
192
+ __privateGet(this, _logger).debug(
193
+ `Invoking host implementation of ${obj.id}.${String(functionName)}()`
194
+ );
195
+ return new Promise((resolve) => {
196
+ if (!Object.prototype.hasOwnProperty.call(func, "callContext")) {
197
+ Object.defineProperty(func, "callContext", {
198
+ value: { guest },
199
+ enumerable: true
200
+ });
201
+ }
202
+ resolve(func(...functionParams));
203
+ });
204
+ });
205
+ __privateAdd(this, _handleGuestReadyComplete, ({
206
+ sourceWin,
207
+ sourceOrigin,
208
+ requestId
209
+ }) => {
210
+ var _a;
211
+ const guest = __privateGet(this, _getGuestForWindow).call(this, sourceWin);
212
+ if (!guest) {
213
+ __privateGet(this, _logger).warn(
214
+ `Received ready event for unknown guest. requestId: ${requestId}`
215
+ );
216
+ return;
217
+ }
218
+ if (!guest.initialized) {
219
+ __privateGet(this, _logger).warn(
220
+ "Guest must be initialized before it is marked as ready"
221
+ );
222
+ __privateGet(this, _remoting).raiseException({
223
+ targetWin: sourceWin,
224
+ targetOrigin: sourceOrigin,
225
+ requestId,
226
+ ex: "Guest must be initialized before it is marked as ready"
227
+ });
228
+ return;
229
+ }
230
+ if (!guest.ready) {
231
+ guest.ready = true;
232
+ __privateGet(this, _dispatchConfigEvent).call(this, guest);
233
+ (_a = __privateGet(this, _readyStateCallback)) == null ? void 0 : _a.call(this, guest);
234
+ __privateGet(this, _logger).audit({
235
+ message: `Guest is ready`,
236
+ ...guest.getInfo()
237
+ });
238
+ }
239
+ });
240
+ // Initializes a guest when the guest is full loaded
241
+ __privateAdd(this, _handleGuestReady, ({
242
+ sourceWin,
243
+ sourceOrigin,
244
+ requestId,
245
+ body
246
+ }) => {
247
+ const guest = __privateGet(this, _getGuestForWindow).call(this, sourceWin);
248
+ if (!guest) {
249
+ __privateGet(this, _logger).warn(
250
+ `Received ready event for unknown guest. requestid = ${requestId}`
251
+ );
252
+ return;
253
+ }
254
+ if (!guest.initialized) {
255
+ guest.initialized = true;
256
+ guest.capabilities = body || {};
257
+ __privateGet(this, _logger).audit({
258
+ message: `Guest is initialized`,
259
+ ...guest.getInfo()
260
+ });
261
+ }
262
+ if (!body || !body.onReady) {
263
+ __privateGet(this, _handleGuestReadyComplete).call(this, {
264
+ sourceWin,
265
+ sourceOrigin,
266
+ requestId,
267
+ type: "",
268
+ body: null
269
+ });
270
+ }
271
+ });
272
+ __privateAdd(this, _handleGuestClose, ({ sourceWin }) => {
273
+ if (sourceWin?.window)
274
+ this.unloadGuest(sourceWin);
275
+ });
276
+ // Handles object get requests from the remote automation framework
277
+ __privateAdd(this, _handleObjectGet, ({
278
+ sourceWin,
279
+ sourceOrigin,
280
+ requestId,
281
+ body
282
+ }) => {
283
+ const { objectId } = body;
284
+ __privateGet(this, _logger).debug(
285
+ `Processing getObject request for object ${objectId}. requestId = ${requestId}`
286
+ );
287
+ const guest = __privateGet(this, _getGuestForWindow).call(this, sourceWin);
288
+ if (!guest) {
289
+ __privateGet(this, _logger).warn("Rejected object request from unknown guest window");
290
+ __privateGet(this, _remoting).raiseException({
291
+ targetWin: sourceWin,
292
+ targetOrigin: sourceOrigin,
293
+ requestId,
294
+ ex: "Specified window is not a known guest"
295
+ });
296
+ return false;
297
+ }
298
+ const obj = this.getScriptingObject(objectId);
299
+ if (!obj) {
300
+ __privateGet(this, _logger).warn(
301
+ `unknown or unauthorized object ${objectId} from guest ${guest.id}`
302
+ );
303
+ __privateGet(this, _remoting).raiseException({
304
+ targetWin: sourceWin,
305
+ targetOrigin: sourceOrigin,
306
+ requestId,
307
+ ex: `The requested object (${objectId}) is not available`
308
+ });
309
+ return false;
310
+ }
311
+ __privateGet(this, _remoting).respond({
312
+ targetWin: sourceWin,
313
+ targetOrigin: sourceOrigin,
314
+ requestId,
315
+ response: __privateGet(this, _encodeResponse).call(this, obj)
316
+ });
317
+ __privateGet(this, _logger).audit({
318
+ message: `Scripting Object returned`,
319
+ requestId,
320
+ scriptingObject: objectId,
321
+ ...guest.getInfo()
322
+ });
323
+ return true;
324
+ });
325
+ // handles guest resize event
326
+ __privateAdd(this, _handleGuestResize, ({
327
+ sourceWin,
328
+ requestId,
329
+ body
330
+ }) => {
331
+ const guest = __privateGet(this, _getGuestForWindow).call(this, sourceWin);
332
+ if (!guest) {
333
+ __privateGet(this, _logger).warn(
334
+ `Received resize event from unknown guest. requestid = ${requestId}`
335
+ );
336
+ return;
337
+ }
338
+ if (guest.domElement) {
339
+ guest.domElement.style.height = `${body.height}px`;
340
+ }
341
+ __privateGet(this, _logger).debug(
342
+ `Guest ${guest.id} resized to ${body.width}x${body.height}`
343
+ );
344
+ });
345
+ // Handles object invoke requests from the remote automation framework
346
+ __privateAdd(this, _handleObjectInvoke, ({
347
+ sourceWin,
348
+ sourceOrigin,
349
+ requestId,
350
+ body
351
+ }) => {
352
+ const { objectId } = body;
353
+ const guest = __privateGet(this, _getGuestForWindow).call(this, sourceWin);
354
+ if (!guest) {
355
+ __privateGet(this, _logger).warn(
356
+ "Rejected method invocation request from unknown guest window"
357
+ );
358
+ __privateGet(this, _remoting).raiseException({
359
+ targetWin: sourceWin,
360
+ targetOrigin: sourceOrigin,
361
+ requestId,
362
+ ex: "Specified window is not a known guest"
363
+ });
364
+ return false;
365
+ }
366
+ __privateGet(this, _logger).debug(
367
+ `Function ${objectId}.${String(body.functionName)}() called from guest "${guest.id}" (requestId = ${requestId})`
368
+ );
369
+ const obj = this.getScriptingObject(objectId);
370
+ if (!obj) {
371
+ __privateGet(this, _logger).warn(
372
+ `Invocation of unknown or unauthorized object ${objectId} from guest ${guest.id}`
373
+ );
374
+ __privateGet(this, _remoting).raiseException({
375
+ targetWin: sourceWin,
376
+ targetOrigin: sourceOrigin,
377
+ requestId,
378
+ ex: `The requested object (${objectId}) is not available`
379
+ });
380
+ return false;
381
+ }
382
+ __privateGet(this, _invoke).call(this, {
383
+ guest,
384
+ obj,
385
+ functionName: body.functionName,
386
+ functionParams: body.functionParams
387
+ }).then((val) => {
388
+ __privateGet(this, _remoting).respond({
389
+ targetWin: sourceWin,
390
+ targetOrigin: sourceOrigin,
391
+ requestId,
392
+ response: __privateGet(this, _encodeResponse).call(this, val)
393
+ });
394
+ __privateGet(this, _logger).audit({
395
+ message: `Value returned for Scripting Object method call`,
396
+ requestId,
397
+ scriptingObject: objectId,
398
+ scriptingMethod: body.functionName,
399
+ ...guest.getInfo()
400
+ });
401
+ }).catch((ex) => {
402
+ __privateGet(this, _remoting).raiseException({
403
+ targetWin: sourceWin,
404
+ targetOrigin: guest.origin,
405
+ requestId,
406
+ ex: __privateGet(this, _encodeException).call(this, ex)
407
+ });
408
+ __privateGet(this, _logger).audit({
409
+ message: `Exception thrown for Scripting Object method call`,
410
+ requestId,
411
+ scriptingObject: objectId,
412
+ scriptingMethod: body.functionName,
413
+ ...guest.getInfo()
414
+ });
415
+ });
416
+ return true;
417
+ });
418
+ __privateAdd(this, _subscribeToGuestResizeEvent, () => {
419
+ __privateGet(this, _remoting).listen({
420
+ messageType: import_ssf_common.MessageType.GuestResize,
421
+ callback: __privateGet(this, _handleGuestResize)
422
+ });
423
+ });
424
+ __privateAdd(this, _connect, () => {
425
+ __privateGet(this, _remoting).listen({
426
+ messageType: import_ssf_common.MessageType.GuestReady,
427
+ callback: __privateGet(this, _handleGuestReady)
428
+ });
429
+ __privateGet(this, _remoting).listen({
430
+ messageType: import_ssf_common.MessageType.GuestReadyComplete,
431
+ callback: __privateGet(this, _handleGuestReadyComplete)
432
+ });
433
+ __privateGet(this, _remoting).listen({
434
+ messageType: import_ssf_common.MessageType.GuestClose,
435
+ callback: __privateGet(this, _handleGuestClose)
436
+ });
437
+ __privateGet(this, _remoting).listen({
438
+ messageType: import_ssf_common.MessageType.ObjectGet,
439
+ callback: __privateGet(this, _handleObjectGet)
440
+ });
441
+ __privateGet(this, _remoting).listen({
442
+ messageType: import_ssf_common.MessageType.ObjectInvoke,
443
+ callback: __privateGet(this, _handleObjectInvoke)
444
+ });
445
+ window.addEventListener("visibilitychange", __privateGet(this, _closeAllPopupGuests));
446
+ });
447
+ __privateAdd(this, _isProxyEvent, (value) => (
448
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
449
+ value?.constructor?.name === "ProxyEvent"
450
+ ));
451
+ /**
452
+ * Register pre-rendered guest application to host
453
+ *
454
+ * @param {AttachParam} param - parameters for attaching the guest
455
+ * @returns reference to the guest object
456
+ */
457
+ __privateAdd(this, _attachGuest, (param) => {
458
+ const guest = new import_guest.Guest({ ...param, remoting: __privateGet(this, _remoting) });
459
+ guest.init();
460
+ __privateGet(this, _guests).set(param.guestId, guest);
461
+ return guest;
462
+ });
463
+ __privateAdd(this, _monitorPopupGuests, () => {
464
+ __privateSet(this, _popupGuestMonitor, setInterval(() => {
465
+ const guestsToRemove = [];
466
+ __privateGet(this, _guests).forEach((guest) => {
467
+ if (guest.openMode === import_types.OpenMode.Popup && guest.window.closed) {
468
+ guestsToRemove.push(guest);
469
+ }
470
+ });
471
+ guestsToRemove.forEach((guest) => {
472
+ this.unloadGuest(guest.id);
473
+ });
474
+ }, 1e3));
475
+ });
476
+ __privateAdd(this, _openPopupGuest, (param) => {
477
+ const { url, title, windowFeatures = {}, params, guestId } = param;
478
+ const { width = 800, height = 600, top = 100, left = 100 } = windowFeatures;
479
+ let guest = __privateGet(this, _getGuestForUrl).call(this, url);
480
+ if (guest) {
481
+ if (!guest.window.closed) {
482
+ guest.send({
483
+ messageType: import_ssf_common.MessageType.GuestFocus,
484
+ messageBody: {}
485
+ });
486
+ }
487
+ } else {
488
+ const popupWindowFeatures = [
489
+ { key: "width", value: width },
490
+ { key: "height", value: height },
491
+ { key: "top", value: top },
492
+ { key: "left", value: left }
493
+ ].reduce((acc, cur, curIndex) => {
494
+ if (curIndex > 0 && cur.value)
495
+ acc += ",";
496
+ return cur.value ? `${acc}${cur.key}=${cur.value}` : acc;
497
+ }, "");
498
+ const guestWindow = window.open(
499
+ url,
500
+ title,
501
+ `popup, ${popupWindowFeatures}`
502
+ );
503
+ if (!guestWindow)
504
+ throw new Error("Failed to open guest application in popup window");
505
+ guestWindow.opener = null;
506
+ guest = __privateGet(this, _attachGuest).call(this, {
507
+ guestId,
508
+ window: guestWindow,
509
+ title,
510
+ url,
511
+ params,
512
+ openMode: import_types.OpenMode.Popup
513
+ });
514
+ }
515
+ return guest;
516
+ });
517
+ __privateAdd(this, _openEmbedGuest, (param) => {
518
+ const {
519
+ url,
520
+ title,
521
+ targetElement,
522
+ params,
523
+ guestId,
524
+ options = {}
525
+ } = param;
526
+ const targetElementDocument = targetElement.ownerDocument ?? document;
527
+ const {
528
+ fitToContent = false,
529
+ disableSandbox = false,
530
+ sandboxValues = []
531
+ } = options;
532
+ if (!title)
533
+ throw new Error("title is required");
534
+ if (fitToContent)
535
+ __privateGet(this, _subscribeToGuestResizeEvent).call(this);
536
+ const frame = targetElementDocument.createElement("iframe");
537
+ frame.setAttribute("id", guestId);
538
+ frame.addEventListener("load", () => {
539
+ __privateGet(this, _logger).debug(`frame loaded for guest with id '${guestId}'`);
540
+ });
541
+ frame.setAttribute("style", "min-width: 100%; height: 100%; border: 0px;");
542
+ if (!disableSandbox) {
543
+ frame.setAttribute(
544
+ "sandbox",
545
+ `${SANDBOX_DEFAULT} ${sandboxValues.join(" ")}`
546
+ );
547
+ }
548
+ frame.setAttribute("title", title);
549
+ frame.setAttribute("src", url);
550
+ targetElement.appendChild(frame);
551
+ const guestFrameEle = targetElementDocument.getElementById(
552
+ guestId
553
+ );
554
+ return __privateGet(this, _attachGuest).call(this, {
555
+ guestId,
556
+ domElement: guestFrameEle,
557
+ window: guestFrameEle.contentWindow,
558
+ title,
559
+ url,
560
+ params,
561
+ openMode: import_types.OpenMode.Embed
562
+ });
563
+ });
564
+ __privateAdd(this, _getGuestUrl, (url, params) => {
565
+ let querystring = "";
566
+ Object.keys(params).forEach((queryParam) => {
567
+ querystring += `${(querystring.length ? "&" : "") + encodeURIComponent(queryParam)}=${encodeURIComponent(params[queryParam])}`;
568
+ });
569
+ return url + (!querystring ? "" : (url.indexOf("?") >= 0 ? "&" : "?") + querystring);
570
+ });
571
+ /**
572
+ * adds scripting object to host
573
+ *
574
+ * @param so scripting object
575
+ * @throws Error if the scripting object is invalid
576
+ */
577
+ __publicField(this, "addScriptingObject", (so) => {
578
+ if (!so?.id || !so?._toJSON) {
579
+ throw new Error("Object is not derived from ScriptingObject");
580
+ }
581
+ if (so.id.toLowerCase() === "module")
582
+ throw new Error(
583
+ "Do not publish the module object. Create a concrete object derived from it."
584
+ );
585
+ __privateGet(this, _publishedObjects).set(so.id, so);
586
+ });
587
+ /**
588
+ * Clone parent host's scripting object for exposing to grand children
589
+ *
590
+ * @param proxy - reference to the scripting object obtained through getObject method
591
+ * @param guest - reference to the ssf guest
592
+ * @returns cloned version of the scripting object
593
+ */
594
+ __publicField(this, "cloneScriptingObject", (proxy, guest) => {
595
+ if (!proxy)
596
+ throw new Error("proxy is required");
597
+ if (!guest)
598
+ throw new Error("guest is required");
599
+ const so = new import_ssf_common.ScriptingObject(proxy.id, proxy.objectType);
600
+ let unsubscribers = [];
601
+ Object.keys(proxy).forEach((propName) => {
602
+ const propValue = proxy[propName];
603
+ if (__privateGet(this, _isProxyEvent).call(this, propValue)) {
604
+ const event = new import_ssf_common.Event({
605
+ name: propValue.name,
606
+ requiresFeedback: false,
607
+ so
608
+ });
609
+ Object.defineProperty(so, propName, {
610
+ value: event,
611
+ enumerable: true
612
+ });
613
+ const listener = ({
614
+ eventParams,
615
+ eventOptions
616
+ }) => this.dispatchEvent({
617
+ event,
618
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
619
+ eventParams,
620
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
621
+ eventOptions
622
+ });
623
+ const token = guest.subscribe({
624
+ eventId: propValue.id,
625
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
626
+ callback: listener
627
+ });
628
+ unsubscribers.push(() => {
629
+ guest.unsubscribe({ eventId: propValue.id, token });
630
+ });
631
+ } else if ((0, import_utils.isFunction)(propValue)) {
632
+ Object.defineProperty(so, propName, {
633
+ value: async (...args) => {
634
+ const retVal = await propValue(...args);
635
+ return (0, import_utils.isProxy)(retVal) ? this.cloneScriptingObject(retVal, guest) : retVal;
636
+ },
637
+ enumerable: true
638
+ });
639
+ if (propName === "dispose") {
640
+ const defaultImpl = so.dispose;
641
+ Object.defineProperty(so, propName, {
642
+ value: () => {
643
+ so._dispose();
644
+ return defaultImpl.apply(so);
645
+ },
646
+ enumerable: true
647
+ });
648
+ }
649
+ }
650
+ });
651
+ so._dispose = () => {
652
+ unsubscribers.forEach((unsub) => {
653
+ unsub?.();
654
+ });
655
+ unsubscribers = [];
656
+ };
657
+ return so;
658
+ });
659
+ /**
660
+ * dispose the resources used by the host application
661
+ */
662
+ __publicField(this, "close", () => {
663
+ clearInterval(__privateGet(this, _popupGuestMonitor));
664
+ __privateGet(this, _remoting).close();
665
+ window.removeEventListener("visibilitychange", __privateGet(this, _closeAllPopupGuests));
666
+ __privateGet(this, _logger).debug(
667
+ `host is closed. hostId: ${this.hostId}, correlationId: ${__privateGet(this, _correlationId)}`
668
+ );
669
+ });
670
+ /**
671
+ * dispatch event to guest application(s)
672
+ *
673
+ * @param {EventDetail} details details of the event to be dispatched
674
+ * @returns promise that resolves to an array with result from each guest application
675
+ */
676
+ __publicField(this, "dispatchEvent", async (details) => {
677
+ const {
678
+ event: { id, name, scriptingObject },
679
+ eventParams,
680
+ eventOptions = {}
681
+ } = details;
682
+ const {
683
+ eventHandler = null,
684
+ timeout = null,
685
+ window: targetWindow = null
686
+ } = eventOptions;
687
+ const eventObj = {
688
+ // eslint-disable-next-line no-underscore-dangle
689
+ object: scriptingObject._toJSON(),
690
+ eventName: name,
691
+ eventParams,
692
+ eventHandler,
693
+ eventOptions: {
694
+ allowsFeedback: false
695
+ }
696
+ };
697
+ if (timeout && !Number.isNaN(timeout)) {
698
+ eventObj.eventOptions = {
699
+ allowsFeedback: true,
700
+ timeout: Number(timeout)
701
+ };
702
+ }
703
+ const guestPromises = [];
704
+ __privateGet(this, _guests).forEach((guest) => {
705
+ if (!targetWindow || targetWindow === guest.window) {
706
+ if (timeout && guest?.capabilities?.eventFeedback) {
707
+ guestPromises.push(guest.dispatchEvent(eventObj, timeout));
708
+ __privateGet(this, _logger).audit({
709
+ message: "Event dispatched and awaiting feedback",
710
+ scriptingEventId: id,
711
+ ...guest.getInfo()
712
+ });
713
+ } else {
714
+ guest.send({
715
+ messageType: import_ssf_common.MessageType.ObjectEvent,
716
+ messageBody: eventObj
717
+ });
718
+ __privateGet(this, _logger).audit({
719
+ message: "Event dispatched",
720
+ scriptingEventId: id,
721
+ ...guest.getInfo()
722
+ });
723
+ }
724
+ }
725
+ });
726
+ const retValue = await Promise.all(guestPromises).then((values) => {
727
+ __privateGet(this, _logger).audit({
728
+ message: "Event feedback received",
729
+ scriptingEventId: id
730
+ });
731
+ return (0, import_utils.flatten)(values);
732
+ }).catch((ex) => {
733
+ __privateGet(this, _logger).error({
734
+ message: "Error processing event",
735
+ eventId: id,
736
+ exception: ex
737
+ });
738
+ throw ex;
739
+ });
740
+ return retValue;
741
+ });
742
+ /**
743
+ * get reference to all guest applications
744
+ *
745
+ * @returns list of guest application references
746
+ */
747
+ __publicField(this, "getGuests", () => {
748
+ const guestList = [];
749
+ __privateGet(this, _guests).forEach((guest) => {
750
+ guestList.push(guest);
751
+ });
752
+ return guestList;
753
+ });
754
+ /**
755
+ * get the scripting object by id
756
+ *
757
+ * @param objectId - id of the scripting object
758
+ * @returns scripting object
759
+ */
760
+ __publicField(this, "getScriptingObject", (objectId) => {
761
+ if (!__privateGet(this, _publishedObjects).has(objectId))
762
+ return null;
763
+ return __privateGet(this, _publishedObjects).get(objectId);
764
+ });
765
+ /**
766
+ * loads guest application inside sandboxed iframe
767
+ *
768
+ * @param {LoadGuestParam} param - parameters for loading the guest application
769
+ * @returns reference to the guest object
770
+ */
771
+ __publicField(this, "loadGuest", (param) => {
772
+ const {
773
+ url,
774
+ targetElement,
775
+ title,
776
+ params = {},
777
+ options = {}
778
+ } = param;
779
+ const { openMode = import_types.OpenMode.Embed, windowFeatures = {} } = options;
780
+ const guestId = `guest-${this.hostId}-sandbox-${__privateGet(this, _nextGuestId)}`;
781
+ __privateSet(this, _nextGuestId, __privateGet(this, _nextGuestId) + 1);
782
+ const srcUrl = __privateGet(this, _getGuestUrl).call(this, url, params);
783
+ let guest = null;
784
+ if (openMode === import_types.OpenMode.Popup) {
785
+ guest = __privateGet(this, _openPopupGuest).call(this, {
786
+ guestId,
787
+ url: srcUrl,
788
+ title,
789
+ params,
790
+ windowFeatures
791
+ });
792
+ } else if (openMode === import_types.OpenMode.Embed) {
793
+ guest = __privateGet(this, _openEmbedGuest).call(this, {
794
+ guestId,
795
+ url: srcUrl,
796
+ title,
797
+ targetElement,
798
+ params,
799
+ options
800
+ });
801
+ } else {
802
+ throw new Error(`Invalid openMode: ${openMode}`);
803
+ }
804
+ __privateGet(this, _logger).audit({
805
+ message: "Guest loaded",
806
+ ...guest.getInfo()
807
+ });
808
+ return guest;
809
+ });
810
+ /**
811
+ * loads guest application inside sandboxed iframe with different params
812
+ *
813
+ * @param {LoadGuestsParam} param - parameters for loading the guest application
814
+ */
815
+ __publicField(this, "loadGuests", (param) => {
816
+ const {
817
+ url,
818
+ targetElement,
819
+ title,
820
+ paramsList = [],
821
+ options = {}
822
+ } = param;
823
+ paramsList.forEach((params) => {
824
+ this.loadGuest({ url, title, targetElement, params, options });
825
+ }, this);
826
+ });
827
+ /**
828
+ * removes all scripting objects from host
829
+ *
830
+ */
831
+ __publicField(this, "removeAllScriptingObjects", () => {
832
+ const objectIds = Array.from(__privateGet(this, _publishedObjects).keys());
833
+ objectIds.forEach((objectId) => {
834
+ this.removeScriptingObject(objectId);
835
+ });
836
+ });
837
+ /**
838
+ * removes scripting object from host
839
+ *
840
+ * @param objectId unique id of the scripting object
841
+ */
842
+ __publicField(this, "removeScriptingObject", (objectId) => {
843
+ const obj = this.getScriptingObject(objectId);
844
+ if (obj) {
845
+ if (obj._dispose && typeof obj._dispose === "function") {
846
+ obj._dispose();
847
+ }
848
+ __privateGet(this, _publishedObjects).delete(objectId);
849
+ }
850
+ });
851
+ /**
852
+ * set the log level for the host application
853
+ *
854
+ * @param level - log level
855
+ */
856
+ __publicField(this, "setLogLevel", (level) => {
857
+ __privateGet(this, _logger).setLogLevel(level);
858
+ __privateGet(this, _guests).forEach(__privateGet(this, _dispatchConfigEvent));
859
+ __privateGet(this, _logger).debug("Dispatched config events to all guests");
860
+ });
861
+ /**
862
+ * unloads guest application from host
863
+ *
864
+ * @param guestIdOrWindowOrEle unique id of the guest application or window object or dom element of the guest application
865
+ * @throws Error if guestId or Window reference is invalid
866
+ */
867
+ __publicField(this, "unloadGuest", (guestIdOrWindowOrEle) => {
868
+ let guest = typeof guestIdOrWindowOrEle === "string" ? __privateGet(this, _guests).get(guestIdOrWindowOrEle) : null;
869
+ if (!guest) {
870
+ guest = Array.from(__privateGet(this, _guests).values()).find(
871
+ (value) => value.window === guestIdOrWindowOrEle || value.domElement === guestIdOrWindowOrEle
872
+ );
873
+ if (!guest) {
874
+ throw new Error("Invalid guestId or guestWindow reference");
875
+ }
876
+ }
877
+ guest.dispose();
878
+ __privateGet(this, _guests).delete(guest.id);
879
+ __privateGet(this, _logger).audit({
880
+ message: `Guest is removed from host`,
881
+ ...guest.getInfo()
882
+ });
883
+ });
884
+ this.hostId = hostId;
885
+ if (!option?.logger)
886
+ throw new Error("Logger is required");
887
+ __privateSet(this, _logger, option.logger);
888
+ __privateSet(this, _correlationId, (0, import_uuid.v4)());
889
+ __privateSet(this, _remoting, new import_ssf_common.Remoting(__privateGet(this, _logger), __privateGet(this, _correlationId)));
890
+ if (option?.readyStateCallback && typeof option?.readyStateCallback !== "function")
891
+ throw new Error("readyStateCallback must be a function");
892
+ __privateSet(this, _readyStateCallback, option?.readyStateCallback || null);
893
+ __privateGet(this, _remoting).initialize(window);
894
+ __privateGet(this, _connect).call(this);
895
+ __privateGet(this, _monitorPopupGuests).call(this);
896
+ __privateGet(this, _logger).debug(
897
+ `host is initialized. hostId: ${this.hostId}, correlationId: ${__privateGet(this, _correlationId)}`
898
+ );
899
+ }
900
+ }
901
+ _remoting = new WeakMap();
902
+ _correlationId = new WeakMap();
903
+ _logger = new WeakMap();
904
+ _nextGuestId = new WeakMap();
905
+ _guests = new WeakMap();
906
+ _publishedObjects = new WeakMap();
907
+ _readyStateCallback = new WeakMap();
908
+ _popupGuestMonitor = new WeakMap();
909
+ _closeAllPopupGuests = new WeakMap();
910
+ _getGuestForWindow = new WeakMap();
911
+ _getGuestForUrl = new WeakMap();
912
+ _isScriptingObject = new WeakMap();
913
+ _encodeResponse = new WeakMap();
914
+ _encodeException = new WeakMap();
915
+ _dispatchConfigEvent = new WeakMap();
916
+ _invoke = new WeakMap();
917
+ _handleGuestReadyComplete = new WeakMap();
918
+ _handleGuestReady = new WeakMap();
919
+ _handleGuestClose = new WeakMap();
920
+ _handleObjectGet = new WeakMap();
921
+ _handleGuestResize = new WeakMap();
922
+ _handleObjectInvoke = new WeakMap();
923
+ _subscribeToGuestResizeEvent = new WeakMap();
924
+ _connect = new WeakMap();
925
+ _isProxyEvent = new WeakMap();
926
+ _attachGuest = new WeakMap();
927
+ _monitorPopupGuests = new WeakMap();
928
+ _openPopupGuest = new WeakMap();
929
+ _openEmbedGuest = new WeakMap();
930
+ _getGuestUrl = new WeakMap();