@interopio/iframes-api 0.1.0-beta.0 → 1.0.0

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.
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.iframes = factory());
5
5
  })(this, (function () { 'use strict';
6
6
 
7
- const BrowserPlatformControlName = "T42.Web.Platform.Control";
7
+ const GlueWebPlatformControlName = "T42.Web.Platform.Control";
8
8
  const GlueIFramesControlName = "T42.IFrames.Control";
9
9
 
10
10
  /**
@@ -768,7 +768,7 @@
768
768
  /** See `Decoder.tuple` */
769
769
  Decoder.tuple;
770
770
  /** See `Decoder.dict` */
771
- Decoder.dict;
771
+ var dict = Decoder.dict;
772
772
  /** See `Decoder.optional` */
773
773
  var optional = Decoder.optional;
774
774
  /** See `Decoder.oneOf` */
@@ -802,14 +802,25 @@
802
802
  const iframesOpenAppConfigDecoder = object({
803
803
  appName: nonEmptyStringDecoder,
804
804
  channelId: optional(nonEmptyStringDecoder),
805
+ container: optional(anyJson()),
805
806
  context: optional(anyJson()),
806
- waitForAGMReady: optional(boolean())
807
+ iframeConfig: optional(object({
808
+ className: optional(string()),
809
+ styles: optional(dict(string()))
810
+ }))
807
811
  });
808
812
  const reserveStandaloneIFrameAppDataDecoder = object({
809
- appName: nonEmptyStringDecoder,
810
- waitForAGMReady: boolean()
813
+ appName: nonEmptyStringDecoder
814
+ });
815
+ const iframePermissionsPolicyConfigDecoder = object({
816
+ flags: string()
817
+ });
818
+ const iframeSandboxConfigDecoder = object({
819
+ flags: string()
811
820
  });
812
821
  const reserveStandaloneIFrameAppResultDecoder = object({
822
+ iframePermissionsPolicy: optional(iframePermissionsPolicyConfigDecoder),
823
+ iframeSandbox: optional(iframeSandboxConfigDecoder),
813
824
  instanceId: nonEmptyStringDecoder,
814
825
  url: nonEmptyStringDecoder
815
826
  });
@@ -817,12 +828,17 @@
817
828
  channelId: optional(nonEmptyStringDecoder),
818
829
  context: optional(anyJson()),
819
830
  instanceId: nonEmptyStringDecoder,
820
- name: nonEmptyStringDecoder,
821
831
  parentId: nonEmptyStringDecoder
822
832
  });
833
+ const closeRequestedResultDecoder = object({
834
+ wasClosed: boolean()
835
+ });
823
836
 
824
837
  class Bridge {
825
838
  io;
839
+ operations = {
840
+ operationCheck: { name: "operationCheck", dataDecoder: operationCheckConfigDecoder, resultDecoder: operationCheckResultDecoder }
841
+ };
826
842
  constructor(io) {
827
843
  this.io = io;
828
844
  }
@@ -830,59 +846,55 @@
830
846
  return this.io.interop.instance.instance;
831
847
  }
832
848
  async initiate(handleControlMessage) {
833
- await this.io.interop.registerAsync(GlueIFramesControlName, (args, _, success, error) => {
849
+ await this.io.interop.registerAsync(GlueIFramesControlName, (args, _caller, success, error) => {
834
850
  handleControlMessage(args)
835
- .then((result) => success(result))
836
- .catch((reason) => error(reason));
851
+ .then(success)
852
+ .catch(error);
837
853
  });
838
854
  }
839
- async send(operation, operationData, options, webOptions) {
855
+ async send(domain, operation, operationData, options, webOptions) {
840
856
  if (operation.dataDecoder) {
841
857
  try {
842
858
  operation.dataDecoder.runWithException(operationData);
843
859
  }
844
860
  catch (error) {
845
- throw new Error(`Unexpected Web->Platform outgoing validation error: ${error.message}, for operation: ${operation.name} and input: ${JSON.stringify(error.input)}`);
861
+ throw new Error(`Unexpected IFrames API->Platform outgoing validation error: ${error.message}, for operation: ${operation.name} and input: ${JSON.stringify(error.input)}`);
846
862
  }
847
863
  }
848
- const operationSupported = webOptions?.includeOperationCheck ?
849
- (await this.checkOperationSupported(operation)).isSupported :
850
- true;
864
+ const operationSupported = webOptions?.includeOperationCheck
865
+ ? (await this.checkOperationSupported(domain, operation)).isSupported
866
+ : true;
851
867
  if (!operationSupported) {
852
- throw new Error(`Cannot complete operation: ${operation.name} because this client is connected to a platform which does not support it`);
868
+ throw new Error(`Cannot complete operation: ${operation.name} for domain: ${domain} because this client is connected to a platform which does not support it`);
853
869
  }
854
- const operationResult = await this.transmitMessage(operation, operationData, options);
870
+ const operationResult = await this.transmitMessage(domain, operation, operationData, options);
855
871
  if (operation.resultDecoder) {
856
872
  try {
857
873
  operation.resultDecoder.runWithException(operationResult);
858
874
  }
859
875
  catch (error) {
860
- throw new Error(`Unexpected Web<-Platform incoming validation error: ${error.message}, for operation: ${operation.name} and input: ${JSON.stringify(error.input)}`);
876
+ throw new Error(`Unexpected IFrames API<-Platform incoming validation error: ${error.message}, for operation: ${operation.name} and input: ${JSON.stringify(error.input)}`);
861
877
  }
862
878
  }
863
879
  return operationResult;
864
880
  }
865
- async checkOperationSupported(operation) {
881
+ async checkOperationSupported(domain, operation) {
866
882
  try {
867
- const result = await this.send({
868
- name: "operationCheck",
869
- dataDecoder: operationCheckConfigDecoder,
870
- resultDecoder: operationCheckResultDecoder
871
- }, { operation: operation.name });
883
+ const result = await this.send(domain, this.operations.operationCheck, { operation: operation.name });
872
884
  return result;
873
885
  }
874
886
  catch {
875
887
  return { isSupported: false };
876
888
  }
877
889
  }
878
- async transmitMessage(operation, data, options) {
890
+ async transmitMessage(domain, operation, data, options) {
879
891
  const browserGlobal = window.glue42core || window.iobrowser;
880
892
  const platformTarget = browserGlobal.communicationId;
881
- const messageData = { domain: "iframes", data, operation: operation.name };
893
+ const messageData = { domain, data, operation: operation.name };
882
894
  let invocationResult;
883
895
  const baseErrorMessage = `Internal Platform Communication Error. Attempted operation: ${JSON.stringify(operation.name)} with data: ${JSON.stringify(data)}. `;
884
896
  try {
885
- invocationResult = await this.io.interop.invoke(BrowserPlatformControlName, messageData, platformTarget ? { instance: platformTarget } : undefined, options);
897
+ invocationResult = await this.io.interop.invoke(GlueWebPlatformControlName, messageData, platformTarget ? { instance: platformTarget } : undefined, options);
886
898
  if (!invocationResult) {
887
899
  throw new Error("Received unsupported result from the platform - empty result");
888
900
  }
@@ -1077,17 +1089,18 @@
1077
1089
  reserveStandaloneIFrameApp: { name: "reserveStandaloneIFrameApp", dataDecoder: reserveStandaloneIFrameAppDataDecoder, resultDecoder: reserveStandaloneIFrameAppResultDecoder },
1078
1090
  registerStandaloneIFrameApp: { name: "registerStandaloneIFrameApp", dataDecoder: registerStandaloneIFrameAppDataDecoder },
1079
1091
  unregisterStandaloneIFrameApp: { name: "unregisterStandaloneIFrameApp", dataDecoder: simpleWindowDecoder },
1080
- closeRequested: { name: "closeRequested", dataDecoder: simpleWindowDecoder }
1092
+ closeRequested: { name: "closeRequested", dataDecoder: simpleWindowDecoder, resultDecoder: closeRequestedResultDecoder }
1081
1093
  };
1082
1094
 
1083
1095
  class IFramesController {
1096
+ logger;
1084
1097
  bridge;
1085
1098
  registry = CallbackRegistryFactory();
1086
- instances = new Map();
1087
- pendingCloseResolvers = new Map();
1099
+ frames = new Map();
1100
+ confirmFrameClosedTimeoutMs = 15 * 1000;
1088
1101
  supportedOperationsNames = [];
1089
- confirmFrameClosedTimeoutMs = 15000;
1090
- constructor(bridge) {
1102
+ constructor(logger, bridge) {
1103
+ this.logger = logger;
1091
1104
  this.bridge = bridge;
1092
1105
  this.addOperationsExecutors();
1093
1106
  }
@@ -1095,7 +1108,6 @@
1095
1108
  const api = {
1096
1109
  openApp: this.openApp.bind(this),
1097
1110
  onCloseRequested: this.onCloseRequested.bind(this),
1098
- confirmFrameClosed: this.confirmFrameClosed.bind(this),
1099
1111
  version,
1100
1112
  };
1101
1113
  return Object.freeze(api);
@@ -1104,6 +1116,7 @@
1104
1116
  const operationName = iframesOperationTypesDecoder.runWithException(args.operation);
1105
1117
  const operation = operations[operationName];
1106
1118
  if (!operation.execute) {
1119
+ this.logger.debug(`Operation '${operationName}' has no executor defined`);
1107
1120
  return;
1108
1121
  }
1109
1122
  let operationData = args.data;
@@ -1113,24 +1126,23 @@
1113
1126
  return operation.execute(operationData);
1114
1127
  }
1115
1128
  addOperationsExecutors() {
1116
- operations.operationCheck.execute = async (config) => handleOperationCheck(this.supportedOperationsNames, config.operation);
1117
1129
  operations.closeRequested.execute = this.handleCloseRequested.bind(this);
1130
+ operations.operationCheck.execute = async (config) => handleOperationCheck(this.supportedOperationsNames, config.operation);
1118
1131
  this.supportedOperationsNames = getSupportedOperationsNames(operations);
1119
1132
  }
1120
1133
  async openApp(config) {
1121
- const { appName, channelId, context, waitForAGMReady = true } = iframesOpenAppConfigDecoder.runWithException(config);
1122
- const { instanceId, url } = await this.reserveStandaloneIFrameApp({ appName, waitForAGMReady });
1134
+ const { appName, channelId, container = document.body, context, iframeConfig } = iframesOpenAppConfigDecoder.runWithException(config);
1135
+ if (!(container instanceof HTMLElement)) {
1136
+ throw new TypeError("'container' must be an instance of HTMLElement");
1137
+ }
1138
+ this.assertContainerConnected(container);
1139
+ const { iframePermissionsPolicy, iframeSandbox, instanceId, url } = await this.reserveStandaloneIFrameApp({ appName });
1123
1140
  const parentId = this.bridge.getMyInstanceId();
1124
- const iframe = this.generateIframeElement(url, instanceId);
1125
- document.body.appendChild(iframe);
1141
+ const iframe = this.generateIframeElement({ className: iframeConfig?.className, iframePermissionsPolicy, iframeSandbox, instanceId, styles: iframeConfig?.styles, url });
1142
+ this.assertContainerConnected(container);
1143
+ container.appendChild(iframe);
1126
1144
  try {
1127
- await this.registerStandaloneIFrameApp({
1128
- channelId,
1129
- context,
1130
- instanceId,
1131
- name: `${appName}_${instanceId}`,
1132
- parentId
1133
- });
1145
+ await this.registerStandaloneIFrameApp({ channelId, context, instanceId, parentId });
1134
1146
  }
1135
1147
  catch (error) {
1136
1148
  iframe.remove();
@@ -1142,59 +1154,78 @@
1142
1154
  iframe,
1143
1155
  parentId
1144
1156
  });
1145
- this.instances.set(instanceId, instance);
1157
+ this.frames.set(instanceId, { instance, isClosing: false });
1146
1158
  return instance;
1147
1159
  }
1148
- generateIframeElement(url, instanceId) {
1160
+ assertContainerConnected(container) {
1161
+ if (!container.isConnected) {
1162
+ throw new Error("'container' must be connected to the DOM");
1163
+ }
1164
+ }
1165
+ generateIframeElement(config) {
1166
+ const { className, iframePermissionsPolicy, iframeSandbox, instanceId, styles, url } = config;
1149
1167
  const iframe = document.createElement("iframe");
1150
- iframe.name = `${instanceId}#wsp`;
1168
+ iframe.name = `${instanceId}#ifr`;
1151
1169
  iframe.id = instanceId;
1152
1170
  iframe.src = url;
1153
- iframe.style.position = "absolute";
1154
- iframe.style.top = "30000px";
1155
- iframe.style.left = "30000px";
1156
- iframe.style.width = "0px";
1157
- iframe.style.height = "0px";
1171
+ if (className) {
1172
+ iframe.className = className;
1173
+ }
1174
+ if (styles) {
1175
+ Object.assign(iframe.style, styles);
1176
+ }
1177
+ if (iframePermissionsPolicy) {
1178
+ iframe.setAttribute("allow", iframePermissionsPolicy.flags);
1179
+ }
1180
+ if (iframeSandbox) {
1181
+ iframe.setAttribute("sandbox", iframeSandbox.flags);
1182
+ }
1158
1183
  return iframe;
1159
1184
  }
1160
1185
  async reserveStandaloneIFrameApp(data) {
1161
- return this.bridge.send(operations.reserveStandaloneIFrameApp, data, undefined, { includeOperationCheck: true });
1186
+ return this.bridge.send("iframes", operations.reserveStandaloneIFrameApp, data);
1162
1187
  }
1163
1188
  async registerStandaloneIFrameApp(data) {
1164
- await this.bridge.send(operations.registerStandaloneIFrameApp, data, undefined, { includeOperationCheck: true });
1189
+ await this.bridge.send("iframes", operations.registerStandaloneIFrameApp, data);
1165
1190
  }
1166
1191
  async handleCloseRequested({ windowId }) {
1167
- const instance = this.instances.get(windowId);
1168
- if (!instance) {
1169
- throw new Error(`Cannot handle close request for iframe ${windowId}, because no local instance was found`);
1192
+ const frame = this.frames.get(windowId);
1193
+ if (!frame) {
1194
+ this.logger.warn(`Cannot handle close request for iframe ${windowId}, because no local instance was found`);
1195
+ return { wasClosed: false };
1196
+ }
1197
+ if (frame.isClosing) {
1198
+ this.logger.warn(`Close request for iframe ${windowId} is already in progress`);
1199
+ return { wasClosed: false };
1170
1200
  }
1171
1201
  const { promise, resolve } = wrapPromise();
1172
- this.pendingCloseResolvers.set(windowId, resolve);
1173
- this.registry.execute("on-close-requested", instance);
1202
+ frame.isClosing = true;
1203
+ let isHandled = false;
1204
+ const confirmClose = async () => {
1205
+ if (isHandled) {
1206
+ this.logger.debug(`Close for iframe ${windowId} already handled, ignoring duplicate confirmClose call`);
1207
+ return;
1208
+ }
1209
+ isHandled = true;
1210
+ this.frames.delete(windowId);
1211
+ frame.instance.iframe.remove();
1212
+ await this.bridge.send("iframes", operations.unregisterStandaloneIFrameApp, { windowId });
1213
+ resolve();
1214
+ };
1215
+ this.registry.execute("close-requested", frame.instance, confirmClose);
1174
1216
  try {
1175
1217
  await PromiseWrap(() => promise, this.confirmFrameClosedTimeoutMs);
1218
+ return { wasClosed: true };
1176
1219
  }
1177
1220
  catch {
1178
- this.pendingCloseResolvers.delete(windowId);
1179
- throw new Error(`IFrame close for ${windowId} timed out waiting for client to be closed`);
1221
+ isHandled = true;
1222
+ frame.isClosing = false;
1223
+ this.logger.warn(`Close for iframe ${windowId} timed out waiting for client to be closed`);
1224
+ return { wasClosed: false };
1180
1225
  }
1181
1226
  }
1182
1227
  onCloseRequested(callback) {
1183
- return this.registry.add("on-close-requested", callback);
1184
- }
1185
- async confirmFrameClosed(id) {
1186
- const resolve = this.pendingCloseResolvers.get(id);
1187
- const instance = this.instances.get(id);
1188
- if (!instance) {
1189
- this.pendingCloseResolvers.delete(id);
1190
- resolve?.();
1191
- return;
1192
- }
1193
- instance.iframe.remove();
1194
- await this.bridge.send(operations.unregisterStandaloneIFrameApp, { windowId: id }, undefined, { includeOperationCheck: true });
1195
- this.instances.delete(id);
1196
- this.pendingCloseResolvers.delete(id);
1197
- resolve?.();
1228
+ return this.registry.add("close-requested", callback);
1198
1229
  }
1199
1230
  }
1200
1231
 
@@ -1213,13 +1244,13 @@
1213
1244
  }
1214
1245
  get iframesController() {
1215
1246
  if (!this._iframesController) {
1216
- this._iframesController = new IFramesController(this.bridge);
1247
+ this._iframesController = new IFramesController(this.io.logger.subLogger("iframes.iframes.controller"), this.bridge);
1217
1248
  }
1218
1249
  return this._iframesController;
1219
1250
  }
1220
1251
  }
1221
1252
 
1222
- var version = "0.1.0-beta.0";
1253
+ var version = "1.0.0";
1223
1254
 
1224
1255
  const factoryFunction = async (io) => {
1225
1256
  const ioc = new IoC(io);