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