@elliemae/ssf-guest 2.0.0-next.41 → 2.0.0-next.42

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.
package/dist/cjs/guest.js CHANGED
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
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
6
  var __export = (target, all) => {
8
7
  for (var name in all)
9
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -17,28 +16,6 @@ var __copyProps = (to, from, except, desc) => {
17
16
  return to;
18
17
  };
19
18
  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
19
  var guest_exports = {};
43
20
  __export(guest_exports, {
44
21
  SSFGuest: () => SSFGuest
@@ -50,7 +27,6 @@ var import_pui_diagnostics = require("@elliemae/pui-diagnostics");
50
27
  var import_proxy = require("./proxy.js");
51
28
  var import_proxyEvent = require("./proxyEvent.js");
52
29
  var import_utils = require("./utils.js");
53
- var _correlationId, _logger, _remoting, _created, _scriptElement, _isConnected, _eventListeners, _id, _title, _url, _hostOrigin, _hostWindow, _getGuestInfo, _fromJSON, _invoke, _handleResponse, _handleConfigChangeEvent, _handleFocusEvent, _handleObjectEvent;
54
30
  var ResponseType = /* @__PURE__ */ ((ResponseType2) => {
55
31
  ResponseType2["OBJECT"] = "object";
56
32
  ResponseType2["VALUE"] = "value";
@@ -81,419 +57,400 @@ if ((0, import_utils.isEmbedded)()) {
81
57
  }
82
58
  const DIAGNOSTICS_URL = "https://api.ellielabs.com/diagnostics/v2/logging";
83
59
  class SSFGuest {
60
+ /* eslint-enable indent */
61
+ #correlationId;
62
+ #logger;
63
+ #remoting;
64
+ #created = false;
65
+ #scriptElement = null;
66
+ #isConnected = false;
67
+ #eventListeners = /* @__PURE__ */ new Map();
68
+ #id = null;
69
+ #title = null;
70
+ #url = null;
71
+ #hostOrigin = null;
72
+ #hostWindow = null;
84
73
  /**
85
74
  * Create new guest
86
75
  *
87
76
  * @param {GuestOption} options - options for the guest
88
77
  */
89
78
  constructor(options) {
90
- /* eslint-enable indent */
91
- __privateAdd(this, _correlationId, void 0);
92
- __privateAdd(this, _logger, void 0);
93
- __privateAdd(this, _remoting, void 0);
94
- __privateAdd(this, _created, false);
95
- __privateAdd(this, _scriptElement, null);
96
- __privateAdd(this, _isConnected, false);
97
- __privateAdd(this, _eventListeners, /* @__PURE__ */ new Map());
98
- __privateAdd(this, _id, null);
99
- __privateAdd(this, _title, null);
100
- __privateAdd(this, _url, null);
101
- __privateAdd(this, _hostOrigin, null);
102
- __privateAdd(this, _hostWindow, null);
103
- __privateAdd(this, _getGuestInfo, () => ({
104
- guestId: __privateGet(this, _id),
105
- guestTitle: __privateGet(this, _title),
106
- guestUrl: __privateGet(this, _url)
107
- }));
108
- /**
109
- * Marshals scripting object JSON into a proxy object
110
- *
111
- * @param soJSON scripting object JSON
112
- * @returns marshaled proxy object
113
- */
114
- __privateAdd(this, _fromJSON, (soJSON) => {
115
- if (!soJSON || !soJSON.objectId) {
116
- __privateGet(this, _logger).error(
117
- "Deserialization of scripting object failed. Object does not have an Object ID."
118
- );
119
- throw new Error("Cannot deserialize object JSON into proxy.");
120
- }
121
- const ctrl = new import_proxy.Proxy(soJSON.objectId, soJSON.objectType);
122
- if (soJSON.functions) {
123
- soJSON.functions.forEach((functionName) => {
124
- Object.defineProperty(ctrl, functionName, {
125
- value: async (...args) => __privateGet(this, _invoke).call(this, {
126
- objectId: ctrl.id,
127
- functionName,
128
- functionParams: [].slice.call(args)
129
- }),
130
- enumerable: true
131
- });
132
- });
133
- }
134
- if (soJSON.events) {
135
- soJSON.events.forEach((eventName) => {
136
- Object.defineProperty(ctrl, eventName, {
137
- value: new import_proxyEvent.ProxyEvent({
138
- objectId: ctrl.id,
139
- name: eventName
140
- }),
141
- enumerable: true
142
- });
143
- });
144
- }
145
- __privateGet(this, _logger).debug(
146
- `Created guest proxy for scripting object (id = "${ctrl.id}")`
147
- );
148
- return ctrl;
79
+ const {
80
+ console: logToConsole = false,
81
+ // eslint-disable-next-line no-underscore-dangle
82
+ url = window.__ICE__?.diagnosticsUrl || DIAGNOSTICS_URL,
83
+ index,
84
+ team,
85
+ appName
86
+ } = options?.logger || {};
87
+ const transport = logToConsole ? (0, import_pui_diagnostics.Console)() : (0, import_pui_diagnostics.http)(url);
88
+ this.#logger = (0, import_pui_diagnostics.logger)({
89
+ transport,
90
+ index,
91
+ team,
92
+ appName
93
+ });
94
+ (0, import_pui_diagnostics.webvitals)(this.#logger);
95
+ (0, import_pui_diagnostics.logUnhandledErrors)(this.#logger);
96
+ this.#correlationId = (0, import_uuid.v4)();
97
+ this.#remoting = new import_microfe_common.Remoting(this.#logger, this.#correlationId);
98
+ this.#logger.audit({
99
+ message: "Guest created",
100
+ guestUrl: window.location.href,
101
+ correlationId: this.#correlationId
149
102
  });
150
- __privateAdd(this, _invoke, async ({
151
- objectId,
152
- functionName,
153
- functionParams
154
- }) => {
155
- __privateGet(this, _logger).debug(
156
- `Invoking scripting object method ${objectId}.${functionName}()...`
103
+ }
104
+ #getGuestInfo = () => ({
105
+ guestId: this.#id,
106
+ guestTitle: this.#title,
107
+ guestUrl: this.#url
108
+ });
109
+ /**
110
+ * Marshals scripting object JSON into a proxy object
111
+ *
112
+ * @param soJSON scripting object JSON
113
+ * @returns marshaled proxy object
114
+ */
115
+ #fromJSON = (soJSON) => {
116
+ if (!soJSON || !soJSON.objectId) {
117
+ this.#logger.error(
118
+ "Deserialization of scripting object failed. Object does not have an Object ID."
157
119
  );
158
- const promises = [];
159
- if (functionParams) {
160
- for (let i = 0; i < functionParams.length; i += 1) {
161
- const p = functionParams[i];
162
- if (p instanceof Promise) {
163
- promises.push(
164
- p.then((val) => {
165
- functionParams[i] = val;
166
- })
167
- );
168
- }
169
- }
170
- }
171
- await Promise.all(promises);
172
- const response = await __privateGet(this, _remoting).invoke({
173
- targetWin: __privateGet(this, _hostWindow),
174
- targetOrigin: __privateGet(this, _hostOrigin),
175
- messageType: import_microfe_common.MessageType.ObjectInvoke,
176
- messageBody: {
177
- objectId,
178
- functionName,
179
- functionParams
180
- }
120
+ throw new Error("Cannot deserialize object JSON into proxy.");
121
+ }
122
+ const ctrl = new import_proxy.Proxy(soJSON.objectId, soJSON.objectType);
123
+ if (soJSON.functions) {
124
+ soJSON.functions.forEach((functionName) => {
125
+ Object.defineProperty(ctrl, functionName, {
126
+ value: async (...args) => this.#invoke({
127
+ objectId: ctrl.id,
128
+ functionName,
129
+ functionParams: [].slice.call(args)
130
+ }),
131
+ enumerable: true
132
+ });
181
133
  });
182
- const retVal = __privateGet(this, _handleResponse).call(this, response);
183
- __privateGet(this, _logger).audit({
184
- message: "Guest proxy invoked Scripting Object method",
185
- scriptingObject: objectId,
186
- scriptingMethod: functionName,
187
- ...__privateGet(this, _getGuestInfo).call(this)
134
+ }
135
+ if (soJSON.events) {
136
+ soJSON.events.forEach((eventName) => {
137
+ Object.defineProperty(ctrl, eventName, {
138
+ value: new import_proxyEvent.ProxyEvent({
139
+ objectId: ctrl.id,
140
+ name: eventName
141
+ }),
142
+ enumerable: true
143
+ });
188
144
  });
189
- return retVal;
190
- });
191
- __privateAdd(this, _handleResponse, (response) => {
192
- if (response?.type === "object" /* OBJECT */ && "object" in response) {
193
- return __privateGet(this, _fromJSON).call(this, response.object);
145
+ }
146
+ this.#logger.debug(
147
+ `Created guest proxy for scripting object (id = "${ctrl.id}")`
148
+ );
149
+ return ctrl;
150
+ };
151
+ #invoke = async ({
152
+ objectId,
153
+ functionName,
154
+ functionParams
155
+ }) => {
156
+ this.#logger.debug(
157
+ `Invoking scripting object method ${objectId}.${functionName}()...`
158
+ );
159
+ const promises = [];
160
+ if (functionParams) {
161
+ for (let i = 0; i < functionParams.length; i += 1) {
162
+ const p = functionParams[i];
163
+ if (p instanceof Promise) {
164
+ promises.push(
165
+ p.then((val) => {
166
+ functionParams[i] = val;
167
+ })
168
+ );
169
+ }
194
170
  }
195
- if (response?.type === "value" /* VALUE */) {
196
- return response.value;
171
+ }
172
+ await Promise.all(promises);
173
+ const response = await this.#remoting.invoke({
174
+ targetWin: this.#hostWindow,
175
+ targetOrigin: this.#hostOrigin,
176
+ messageType: import_microfe_common.MessageType.ObjectInvoke,
177
+ messageBody: {
178
+ objectId,
179
+ functionName,
180
+ functionParams
197
181
  }
198
- return response;
199
182
  });
200
- __privateAdd(this, _handleConfigChangeEvent, ({ body }) => {
201
- if (body?.logLevel) {
202
- __privateGet(this, _logger).setLogLevel(body.logLevel);
203
- __privateGet(this, _logger).debug(`Log level changed by host to ${body.logLevel}`);
204
- }
205
- __privateSet(this, _id, body?.guestId ?? null);
206
- __privateSet(this, _title, body?.guestTitle ?? null);
207
- __privateSet(this, _url, body?.guestUrl ?? null);
208
- __privateGet(this, _logger).audit({
209
- message: "Received configuration from Host",
210
- ...__privateGet(this, _getGuestInfo).call(this)
211
- });
183
+ const retVal = this.#handleResponse(response);
184
+ this.#logger.audit({
185
+ message: "Guest proxy invoked Scripting Object method",
186
+ scriptingObject: objectId,
187
+ scriptingMethod: functionName,
188
+ ...this.#getGuestInfo()
212
189
  });
213
- __privateAdd(this, _handleFocusEvent, () => {
214
- window.focus();
190
+ return retVal;
191
+ };
192
+ #handleResponse = (response) => {
193
+ if (response?.type === "object" /* OBJECT */ && "object" in response) {
194
+ return this.#fromJSON(response.object);
195
+ }
196
+ if (response?.type === "value" /* VALUE */) {
197
+ return response.value;
198
+ }
199
+ return response;
200
+ };
201
+ #handleConfigChangeEvent = ({ body }) => {
202
+ if (body?.logLevel) {
203
+ this.#logger.setLogLevel(body.logLevel);
204
+ this.#logger.debug(`Log level changed by host to ${body.logLevel}`);
205
+ }
206
+ this.#id = body?.guestId ?? null;
207
+ this.#title = body?.guestTitle ?? null;
208
+ this.#url = body?.guestUrl ?? null;
209
+ this.#logger.audit({
210
+ message: "Received configuration from Host",
211
+ ...this.#getGuestInfo()
215
212
  });
216
- __privateAdd(this, _handleObjectEvent, async ({
217
- sourceWin,
218
- sourceOrigin,
219
- requestId,
220
- body
221
- }) => {
222
- const object = __privateGet(this, _fromJSON).call(this, body.object);
223
- if (object) {
224
- const eventId = (0, import_microfe_common.getEventId)(object.id, body.eventName);
225
- __privateGet(this, _logger).debug(`Received event "${eventId}" from host`);
226
- let listeners;
227
- if (body.eventHandler) {
228
- listeners = [
229
- {
230
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
231
- callback: window[body.eventHandler],
232
- token: (0, import_uuid.v4)()
233
- }
234
- ];
235
- } else {
236
- listeners = __privateGet(this, _eventListeners).get(eventId) || [];
237
- }
238
- const promises = [];
239
- listeners.forEach((callbackInfo) => {
240
- if (callbackInfo?.callback) {
241
- __privateGet(this, _logger).debug(
242
- `Invoking event subscriber ${callbackInfo.callback.name} for event ${eventId}`
243
- );
244
- const retVal = callbackInfo.callback({
245
- obj: object,
246
- eventName: body.eventName,
247
- eventParams: body.eventParams,
248
- eventOptions: body.eventOptions
249
- });
250
- if (retVal instanceof Promise) {
251
- promises.push(retVal);
252
- } else if (typeof retVal !== "undefined") {
253
- promises.push(Promise.resolve(retVal));
254
- }
213
+ };
214
+ #handleFocusEvent = () => {
215
+ window.focus();
216
+ };
217
+ #handleObjectEvent = async ({
218
+ sourceWin,
219
+ sourceOrigin,
220
+ requestId,
221
+ body
222
+ }) => {
223
+ const object = this.#fromJSON(body.object);
224
+ if (object) {
225
+ const eventId = (0, import_microfe_common.getEventId)(object.id, body.eventName);
226
+ this.#logger.debug(`Received event "${eventId}" from host`);
227
+ let listeners;
228
+ if (body.eventHandler) {
229
+ listeners = [
230
+ {
231
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
232
+ callback: window[body.eventHandler],
233
+ token: (0, import_uuid.v4)()
255
234
  }
256
- });
257
- if (requestId) {
258
- const values = await Promise.all(promises);
259
- __privateGet(this, _remoting).respond({
260
- targetWin: sourceWin,
261
- targetOrigin: sourceOrigin,
262
- requestId,
263
- response: values
264
- });
265
- __privateGet(this, _logger).audit({
266
- message: "Guest proxy processed event from host and responded",
267
- scriptingEventId: eventId,
268
- scriptingObject: object.id,
269
- ...__privateGet(this, _getGuestInfo).call(this)
270
- });
271
- } else {
272
- __privateGet(this, _logger).audit({
273
- message: "Guest proxy processed event from host",
274
- scriptingEventId: eventId,
275
- scriptingObject: object.id,
276
- ...__privateGet(this, _getGuestInfo).call(this)
235
+ ];
236
+ } else {
237
+ listeners = this.#eventListeners.get(eventId) || [];
238
+ }
239
+ const promises = [];
240
+ listeners.forEach((callbackInfo) => {
241
+ if (callbackInfo?.callback) {
242
+ this.#logger.debug(
243
+ `Invoking event subscriber ${callbackInfo.callback.name} for event ${eventId}`
244
+ );
245
+ const retVal = callbackInfo.callback({
246
+ obj: object,
247
+ eventName: body.eventName,
248
+ eventParams: body.eventParams,
249
+ eventOptions: body.eventOptions
277
250
  });
251
+ if (retVal instanceof Promise) {
252
+ promises.push(retVal);
253
+ } else if (typeof retVal !== "undefined") {
254
+ promises.push(Promise.resolve(retVal));
255
+ }
278
256
  }
279
- }
280
- });
281
- /**
282
- * Initialize guest using script
283
- *
284
- * @param scriptUri uri of the script
285
- * @param containerElement dom element to inject the script at
286
- */
287
- __publicField(this, "addScript", async (scriptUri, containerElement) => {
288
- if (!__privateGet(this, _created) && scriptUri) {
289
- await this.connect();
290
- const scriptEle = document.createElement("script");
291
- scriptEle.setAttribute("src", scriptUri);
292
- await new Promise((resolve) => {
293
- const onScriptLoad = () => {
294
- scriptEle?.removeEventListener?.("load", onScriptLoad);
295
- __privateSet(this, _created, true);
296
- resolve();
297
- };
298
- scriptEle.addEventListener("load", onScriptLoad);
299
- __privateSet(this, _scriptElement, containerElement.appendChild(scriptEle));
300
- });
301
- }
302
- });
303
- /**
304
- * Close the connection to the host
305
- */
306
- __publicField(this, "close", () => {
307
- if (!__privateGet(this, _isConnected))
308
- return;
309
- __privateGet(this, _remoting).send({
310
- targetWin: __privateGet(this, _hostWindow),
311
- targetOrigin: __privateGet(this, _hostOrigin),
312
- messageType: import_microfe_common.MessageType.GuestClose,
313
- messageBody: {}
314
- });
315
- __privateGet(this, _remoting).removeSender({
316
- origin: __privateGet(this, _hostOrigin),
317
- window: __privateGet(this, _hostWindow)
318
257
  });
319
- __privateGet(this, _remoting).close();
320
- __privateSet(this, _isConnected, false);
321
- __privateGet(this, _logger).audit({
322
- message: "Guest disconnected from host",
323
- ...__privateGet(this, _getGuestInfo).call(this)
324
- });
325
- __privateSet(this, _id, null);
326
- __privateSet(this, _url, null);
327
- __privateSet(this, _title, null);
328
- });
329
- /**
330
- * Connect to the host
331
- *
332
- * @param {ConnectParam} param - reference to the guest window or options
333
- */
334
- __publicField(this, "connect", async (param) => {
335
- if (!__privateGet(this, _isConnected)) {
336
- let guestWindow = window;
337
- let guestOptions = { ...capabilities };
338
- if (param) {
339
- guestWindow = param.window || guestWindow;
340
- const clonedOptions = { ...param };
341
- delete clonedOptions.window;
342
- guestOptions = Object.assign(guestOptions, clonedOptions);
343
- }
344
- const hostDetails = await (0, import_utils.getHostDetails)();
345
- __privateSet(this, _hostOrigin, hostDetails.origin);
346
- __privateSet(this, _hostWindow, hostDetails.window);
347
- __privateGet(this, _remoting).initialize(guestWindow);
348
- __privateGet(this, _remoting).addSender({
349
- origin: __privateGet(this, _hostOrigin),
350
- window: __privateGet(this, _hostWindow)
351
- });
352
- __privateGet(this, _remoting).listen({
353
- messageType: import_microfe_common.MessageType.ObjectEvent,
354
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
355
- callback: __privateGet(this, _handleObjectEvent).bind(this)
356
- });
357
- __privateGet(this, _remoting).listen({
358
- messageType: import_microfe_common.MessageType.HostConfig,
359
- callback: __privateGet(this, _handleConfigChangeEvent).bind(this)
258
+ if (requestId) {
259
+ const values = await Promise.all(promises);
260
+ this.#remoting.respond({
261
+ targetWin: sourceWin,
262
+ targetOrigin: sourceOrigin,
263
+ requestId,
264
+ response: values
360
265
  });
361
- __privateGet(this, _remoting).listen({
362
- messageType: import_microfe_common.MessageType.GuestFocus,
363
- callback: __privateGet(this, _handleFocusEvent).bind(this)
266
+ this.#logger.audit({
267
+ message: "Guest proxy processed event from host and responded",
268
+ scriptingEventId: eventId,
269
+ scriptingObject: object.id,
270
+ ...this.#getGuestInfo()
364
271
  });
365
- __privateGet(this, _remoting).send({
366
- targetWin: __privateGet(this, _hostWindow),
367
- targetOrigin: __privateGet(this, _hostOrigin),
368
- messageType: import_microfe_common.MessageType.GuestReady,
369
- messageBody: guestOptions
370
- });
371
- __privateSet(this, _isConnected, true);
372
- __privateGet(this, _logger).audit({
373
- message: "Guest connected to host",
374
- guestUrl: window.location.href
272
+ } else {
273
+ this.#logger.audit({
274
+ message: "Guest proxy processed event from host",
275
+ scriptingEventId: eventId,
276
+ scriptingObject: object.id,
277
+ ...this.#getGuestInfo()
375
278
  });
376
279
  }
377
- });
378
- /**
379
- * Get scripting object proxy by id
380
- *
381
- * @param objectId unique id of the scripting object
382
- * @returns scripting object proxy
383
- */
384
- __publicField(this, "getObject", async (objectId) => {
385
- __privateGet(this, _logger).debug(
386
- `Retrieving scripting object "${objectId}" from host...`
387
- );
388
- const response = await __privateGet(this, _remoting).invoke({
389
- targetWin: __privateGet(this, _hostWindow),
390
- targetOrigin: __privateGet(this, _hostOrigin),
391
- messageType: import_microfe_common.MessageType.ObjectGet,
392
- messageBody: {
393
- objectId
394
- }
395
- });
396
- const obj = __privateGet(this, _fromJSON).call(this, response.object);
397
- __privateGet(this, _logger).audit({
398
- message: "Received scripting object from host",
399
- scriptingObject: objectId,
400
- ...__privateGet(this, _getGuestInfo).call(this)
280
+ }
281
+ };
282
+ /**
283
+ * Initialize guest using script
284
+ *
285
+ * @param scriptUri uri of the script
286
+ * @param containerElement dom element to inject the script at
287
+ */
288
+ addScript = async (scriptUri, containerElement) => {
289
+ if (!this.#created && scriptUri) {
290
+ await this.connect();
291
+ const scriptEle = document.createElement("script");
292
+ scriptEle.setAttribute("src", scriptUri);
293
+ await new Promise((resolve) => {
294
+ const onScriptLoad = () => {
295
+ scriptEle?.removeEventListener?.("load", onScriptLoad);
296
+ this.#created = true;
297
+ resolve();
298
+ };
299
+ scriptEle.addEventListener("load", onScriptLoad);
300
+ this.#scriptElement = containerElement.appendChild(scriptEle);
401
301
  });
402
- return obj;
403
- });
404
- /**
405
- * remove the guest from the host
406
- */
407
- __publicField(this, "removeScript", () => {
408
- if (!__privateGet(this, _created))
409
- return;
410
- if (__privateGet(this, _scriptElement)) {
411
- const { parentNode } = __privateGet(this, _scriptElement);
412
- parentNode?.removeChild?.(__privateGet(this, _scriptElement));
413
- }
302
+ }
303
+ };
304
+ /**
305
+ * Close the connection to the host
306
+ */
307
+ close = () => {
308
+ if (!this.#isConnected)
309
+ return;
310
+ this.#remoting.send({
311
+ targetWin: this.#hostWindow,
312
+ targetOrigin: this.#hostOrigin,
313
+ messageType: import_microfe_common.MessageType.GuestClose,
314
+ messageBody: {}
414
315
  });
415
- /**
416
- * Set the log level
417
- *
418
- * @param {LogLevels} logLevel - log level
419
- * @param level
420
- */
421
- __publicField(this, "setLogLevel", (level) => {
422
- __privateGet(this, _logger).setLogLevel(level);
423
- __privateGet(this, _logger).debug(`Log level changed by guest to ${level}`);
316
+ this.#remoting.removeSender({
317
+ origin: this.#hostOrigin,
318
+ window: this.#hostWindow
424
319
  });
425
- /**
426
- * subscribe to an scripting object event
427
- *
428
- * @param {GuestSubscribeParam<EventId, AppEvents[EventId]>} param - parameters for subscribing to an event
429
- * @returns subscription token
430
- */
431
- __publicField(this, "subscribe", (param) => {
432
- const { eventId, callback } = param;
433
- __privateGet(this, _logger).debug(`Registering subscription for event ${eventId}`);
434
- const listeners = __privateGet(this, _eventListeners).get(eventId) || [];
435
- const token = (0, import_uuid.v4)();
436
- listeners.push({ callback, token });
437
- __privateGet(this, _eventListeners).set(eventId, listeners);
438
- return token;
320
+ this.#remoting.close();
321
+ this.#isConnected = false;
322
+ this.#logger.audit({
323
+ message: "Guest disconnected from host",
324
+ ...this.#getGuestInfo()
439
325
  });
440
- /**
441
- * unsubscribe from an scripting object event
442
- *
443
- * @param {GuestUnsubscribeParam<EventId>} param - parameters for unsubscribing from an event
444
- */
445
- __publicField(this, "unsubscribe", (param) => {
446
- const { eventId, token } = param;
447
- let listeners = __privateGet(this, _eventListeners).get(eventId);
448
- if (listeners) {
449
- listeners = listeners.filter(
450
- (callbackInfo) => callbackInfo.token !== token
451
- );
452
- __privateGet(this, _eventListeners).set(eventId, listeners);
326
+ this.#id = null;
327
+ this.#url = null;
328
+ this.#title = null;
329
+ };
330
+ /**
331
+ * Connect to the host
332
+ *
333
+ * @param {ConnectParam} param - reference to the guest window or options
334
+ */
335
+ connect = async (param) => {
336
+ if (!this.#isConnected) {
337
+ let guestWindow = window;
338
+ let guestOptions = { ...capabilities };
339
+ if (param) {
340
+ guestWindow = param.window || guestWindow;
341
+ const clonedOptions = { ...param };
342
+ delete clonedOptions.window;
343
+ guestOptions = Object.assign(guestOptions, clonedOptions);
344
+ }
345
+ const hostDetails = await (0, import_utils.getHostDetails)();
346
+ this.#hostOrigin = hostDetails.origin;
347
+ this.#hostWindow = hostDetails.window;
348
+ this.#remoting.initialize(guestWindow);
349
+ this.#remoting.addSender({
350
+ origin: this.#hostOrigin,
351
+ window: this.#hostWindow
352
+ });
353
+ this.#remoting.listen({
354
+ messageType: import_microfe_common.MessageType.ObjectEvent,
355
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
356
+ callback: this.#handleObjectEvent.bind(this)
357
+ });
358
+ this.#remoting.listen({
359
+ messageType: import_microfe_common.MessageType.HostConfig,
360
+ callback: this.#handleConfigChangeEvent.bind(this)
361
+ });
362
+ this.#remoting.listen({
363
+ messageType: import_microfe_common.MessageType.GuestFocus,
364
+ callback: this.#handleFocusEvent.bind(this)
365
+ });
366
+ this.#remoting.send({
367
+ targetWin: this.#hostWindow,
368
+ targetOrigin: this.#hostOrigin,
369
+ messageType: import_microfe_common.MessageType.GuestReady,
370
+ messageBody: guestOptions
371
+ });
372
+ this.#isConnected = true;
373
+ this.#logger.audit({
374
+ message: "Guest connected to host",
375
+ guestUrl: window.location.href
376
+ });
377
+ }
378
+ };
379
+ /**
380
+ * Get scripting object proxy by id
381
+ *
382
+ * @param objectId unique id of the scripting object
383
+ * @returns scripting object proxy
384
+ */
385
+ getObject = async (objectId) => {
386
+ this.#logger.debug(
387
+ `Retrieving scripting object "${objectId}" from host...`
388
+ );
389
+ const response = await this.#remoting.invoke({
390
+ targetWin: this.#hostWindow,
391
+ targetOrigin: this.#hostOrigin,
392
+ messageType: import_microfe_common.MessageType.ObjectGet,
393
+ messageBody: {
394
+ objectId
453
395
  }
454
396
  });
455
- const {
456
- console: logToConsole = false,
457
- // eslint-disable-next-line no-underscore-dangle
458
- url = window.__ICE__?.diagnosticsUrl || DIAGNOSTICS_URL,
459
- index,
460
- team,
461
- appName
462
- } = options?.logger || {};
463
- const transport = logToConsole ? (0, import_pui_diagnostics.Console)() : (0, import_pui_diagnostics.http)(url);
464
- __privateSet(this, _logger, (0, import_pui_diagnostics.logger)({
465
- transport,
466
- index,
467
- team,
468
- appName
469
- }));
470
- (0, import_pui_diagnostics.webvitals)(__privateGet(this, _logger));
471
- (0, import_pui_diagnostics.logUnhandledErrors)(__privateGet(this, _logger));
472
- __privateSet(this, _correlationId, (0, import_uuid.v4)());
473
- __privateSet(this, _remoting, new import_microfe_common.Remoting(__privateGet(this, _logger), __privateGet(this, _correlationId)));
474
- __privateGet(this, _logger).audit({
475
- message: "Guest created",
476
- guestUrl: window.location.href,
477
- correlationId: __privateGet(this, _correlationId)
397
+ const obj = this.#fromJSON(response.object);
398
+ this.#logger.audit({
399
+ message: "Received scripting object from host",
400
+ scriptingObject: objectId,
401
+ ...this.#getGuestInfo()
478
402
  });
479
- }
403
+ return obj;
404
+ };
405
+ /**
406
+ * remove the guest from the host
407
+ */
408
+ removeScript = () => {
409
+ if (!this.#created)
410
+ return;
411
+ if (this.#scriptElement) {
412
+ const { parentNode } = this.#scriptElement;
413
+ parentNode?.removeChild?.(this.#scriptElement);
414
+ }
415
+ };
416
+ /**
417
+ * Set the log level
418
+ *
419
+ * @param {LogLevels} logLevel - log level
420
+ * @param level
421
+ */
422
+ setLogLevel = (level) => {
423
+ this.#logger.setLogLevel(level);
424
+ this.#logger.debug(`Log level changed by guest to ${level}`);
425
+ };
426
+ /**
427
+ * subscribe to an scripting object event
428
+ *
429
+ * @param {GuestSubscribeParam<EventId, AppEvents[EventId]>} param - parameters for subscribing to an event
430
+ * @returns subscription token
431
+ */
432
+ subscribe = (param) => {
433
+ const { eventId, callback } = param;
434
+ this.#logger.debug(`Registering subscription for event ${eventId}`);
435
+ const listeners = this.#eventListeners.get(eventId) || [];
436
+ const token = (0, import_uuid.v4)();
437
+ listeners.push({ callback, token });
438
+ this.#eventListeners.set(eventId, listeners);
439
+ return token;
440
+ };
441
+ /**
442
+ * unsubscribe from an scripting object event
443
+ *
444
+ * @param {GuestUnsubscribeParam<EventId>} param - parameters for unsubscribing from an event
445
+ */
446
+ unsubscribe = (param) => {
447
+ const { eventId, token } = param;
448
+ let listeners = this.#eventListeners.get(eventId);
449
+ if (listeners) {
450
+ listeners = listeners.filter(
451
+ (callbackInfo) => callbackInfo.token !== token
452
+ );
453
+ this.#eventListeners.set(eventId, listeners);
454
+ }
455
+ };
480
456
  }
481
- _correlationId = new WeakMap();
482
- _logger = new WeakMap();
483
- _remoting = new WeakMap();
484
- _created = new WeakMap();
485
- _scriptElement = new WeakMap();
486
- _isConnected = new WeakMap();
487
- _eventListeners = new WeakMap();
488
- _id = new WeakMap();
489
- _title = new WeakMap();
490
- _url = new WeakMap();
491
- _hostOrigin = new WeakMap();
492
- _hostWindow = new WeakMap();
493
- _getGuestInfo = new WeakMap();
494
- _fromJSON = new WeakMap();
495
- _invoke = new WeakMap();
496
- _handleResponse = new WeakMap();
497
- _handleConfigChangeEvent = new WeakMap();
498
- _handleFocusEvent = new WeakMap();
499
- _handleObjectEvent = new WeakMap();