@interopio/fdc3-web-bridge 1.1.1-beta.0 → 1.2.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.
@@ -1582,6 +1582,9 @@
1582
1582
 
1583
1583
  const nonEmptyStringDecoder$4 = string().where((s) => s.length > 0, "Expected a non-empty string");
1584
1584
  const nonNegativeNumberDecoder = number().where((num) => num >= 0, "Expected a non-negative number");
1585
+ const regexDecoder = anyJson().andThen((value) => {
1586
+ return value instanceof RegExp ? anyJson() : fail(`expected a regex, got a ${typeof value}`);
1587
+ });
1585
1588
 
1586
1589
  const intentDefinitionDecoder = object({
1587
1590
  name: nonEmptyStringDecoder$4,
@@ -1908,7 +1911,8 @@
1908
1911
  const decoders$1 = {
1909
1912
  common: {
1910
1913
  nonEmptyStringDecoder: nonEmptyStringDecoder$4,
1911
- nonNegativeNumberDecoder
1914
+ nonNegativeNumberDecoder,
1915
+ regexDecoder
1912
1916
  },
1913
1917
  fdc3: {
1914
1918
  allDefinitionsDecoder,
@@ -1998,7 +2002,7 @@
1998
2002
  channelSelector: optional$1(boolean$1()),
1999
2003
  fdc3Version: nonEmptyStringDecoder$3,
2000
2004
  identityUrl: nonEmptyStringDecoder$3,
2001
- resolver: optional$1(boolean$1())
2005
+ intentResolver: optional$1(boolean$1())
2002
2006
  });
2003
2007
  const wcp1HelloMetaDecoder = object$1({
2004
2008
  connectionAttemptUuid: nonEmptyStringDecoder$3,
@@ -2838,13 +2842,14 @@
2838
2842
  }
2839
2843
  handleHelloConnectionRequest(source, origin, data) {
2840
2844
  if (this._messageChannel) {
2841
- console.warn(`${WCPMessageTypes.WCP1Hello} already heard. Message channel port created`);
2845
+ this.logMessage({ level: "warn", message: `${WCPMessageTypes.WCP1Hello} already heard. Message channel port created` });
2842
2846
  return;
2843
2847
  }
2848
+ this.logMessage({ level: "info", message: `Received ${WCPMessageTypes.WCP1Hello} request: ${JSON.stringify(data)} from origin: ${origin}` });
2844
2849
  const decodedRequest = wcp1HelloDecoder.run(data);
2845
2850
  if (!decodedRequest.ok) {
2846
2851
  const errorMsg = `Received invalid '${WCPMessageTypes.WCP1Hello}' request: ${JSON.stringify(data)}. Reason: ${decodedRequest.error.message} at ${decodedRequest.error.at}`;
2847
- console.error(errorMsg);
2852
+ this.logMessage({ level: "error", message: errorMsg });
2848
2853
  source.postMessage({ error: "MalformedMessage", message: errorMsg }, origin);
2849
2854
  return;
2850
2855
  }
@@ -2855,9 +2860,9 @@
2855
2860
  window.removeEventListener("message", this._globalMessageHandler);
2856
2861
  }
2857
2862
  async handlePortMessage(event) {
2858
- this.logger?.info(`Received port message with data: ${JSON.stringify(event.data)}}`);
2863
+ this.logMessage({ level: "info", message: `Received port message with data: ${JSON.stringify(event.data)}` });
2859
2864
  if (!this._connectionCompleted && event.data?.type !== WCPMessageTypes.WCP6Goodbye && event.data.type !== WCPMessageTypes.WCP4ValidateAppIdentity) {
2860
- console.warn("Web Connection Protocol not completed yet. Ignoring message");
2865
+ this.logMessage({ level: "warn", message: "Web Connection Protocol not completed yet. Ignoring message" });
2861
2866
  return;
2862
2867
  }
2863
2868
  if (event.data?.type === WCPMessageTypes.WCP4ValidateAppIdentity) {
@@ -2867,33 +2872,36 @@
2867
2872
  return this.handleGoodbyeRequest(event.data);
2868
2873
  }
2869
2874
  const result = await this.ioc.domainsController.handleMessage(event.data);
2870
- this.logger?.info(`Sending response message: ${JSON.stringify(result)}`);
2875
+ this.logMessage({ level: "info", message: `Sending response message: ${JSON.stringify(result)}` });
2871
2876
  this.sendMessage(result);
2872
2877
  }
2873
2878
  async handleAppIdentityValidationRequest(data) {
2874
2879
  const decodedRequest = wcp4ValidateAppIdentityDecoder.run(data);
2875
2880
  if (!decodedRequest.ok) {
2876
2881
  const errorMsg = `Received invalid '${WCPMessageTypes.WCP4ValidateAppIdentity}' request: ${JSON.stringify(data)}. Reason: ${decodedRequest.error.message} at ${decodedRequest.error.at}`;
2877
- console.error(errorMsg);
2882
+ this.logMessage({ level: "error", message: errorMsg });
2878
2883
  this.sendMessage({ error: "MalformedMessage", message: errorMsg });
2879
2884
  return;
2880
2885
  }
2881
2886
  const { actualUrl, identityUrl } = data.payload;
2882
2887
  await this.ioController.initializeLibs();
2883
2888
  this.logger = this.ioController.io?.logger.subLogger("bridge.controller");
2884
- this.logger?.info(`IOBrowser and FDC3 libs are initialized. Received ${data.type} request: ${JSON.stringify(data)}`);
2889
+ this.logMessage({ level: "info", message: `IOBrowser and FDC3 libs are initialized. Received ${data.type} request: ${JSON.stringify(data)}` });
2885
2890
  const validationResult = this.ioController.validateIdentity({ actualUrl, identityUrl, helloMsgOrigin: this._helloMessageOrigin });
2886
2891
  if (!validationResult.isValid) {
2887
- this.logger?.info(`App validation failure. Reason: ${validationResult.message}`);
2888
2892
  this.sendValidationFailure(data.meta.connectionAttemptUuid, validationResult.message);
2889
2893
  this.cleanSetup();
2890
2894
  return;
2891
2895
  }
2892
2896
  await this.sendValidationSuccess(data.meta.connectionAttemptUuid, validationResult.appId);
2893
- this.logger?.info(`App validation is successful. Connection is completed`);
2897
+ this.logMessage({ level: "info", message: `App validation is successful. Connection is completed` });
2894
2898
  this._connectionCompleted = true;
2895
2899
  }
2896
2900
  sendHandshake(source, origin, data) {
2901
+ this.ioc.uiConfig = {
2902
+ isChannelSelectorEnabled: !!data.payload.channelSelector,
2903
+ isIntentResolverEnabled: !!data.payload.intentResolver,
2904
+ };
2897
2905
  const handshakeMsg = {
2898
2906
  type: WCPMessageTypes.WCP3Handshake,
2899
2907
  meta: {
@@ -2901,12 +2909,13 @@
2901
2909
  timestamp: new Date()
2902
2910
  },
2903
2911
  payload: {
2904
- channelSelectorUrl: true,
2905
- intentResolverUrl: true,
2906
- fdc3Version: window.fdc3Info.standardVersion,
2912
+ channelSelectorUrl: this.ioc.uiConfig.isChannelSelectorEnabled,
2913
+ intentResolverUrl: this.ioc.uiConfig.isIntentResolverEnabled,
2914
+ fdc3Version: window.fdc3Info?.standardVersion,
2907
2915
  }
2908
2916
  };
2909
2917
  const transfer = this._messageChannel ? [this._messageChannel.port2] : undefined;
2918
+ this.logMessage({ level: "info", message: `Sending ${WCPMessageTypes.WCP3Handshake} message: ${JSON.stringify(handshakeMsg)} to origin: ${origin}` });
2910
2919
  source.postMessage(handshakeMsg, origin, transfer);
2911
2920
  }
2912
2921
  sendValidationFailure(attempt, errorMsg) {
@@ -2920,7 +2929,7 @@
2920
2929
  message: errorMsg
2921
2930
  }
2922
2931
  };
2923
- console.warn(`App Identity Validation failed. Reason: ${errorMsg}`);
2932
+ this.logMessage({ level: "error", message: `App validation failure. Reason: ${errorMsg}. Sending ${WCPMessageTypes.WCP5ValidateAppIdentityFailedResponse} message: ${JSON.stringify(validationFailureMsg)}` });
2924
2933
  this.sendMessage(validationFailureMsg);
2925
2934
  }
2926
2935
  async sendValidationSuccess(attempt, appId) {
@@ -2941,6 +2950,7 @@
2941
2950
  implementationMetadata
2942
2951
  }
2943
2952
  };
2953
+ this.logMessage({ level: "info", message: `App Identity Validation successful. Sending ${WCPMessageTypes.WCP5ValidateAppIdentityResponse} message: ${JSON.stringify(validationSuccessMsg)}` });
2944
2954
  this.sendMessage(validationSuccessMsg);
2945
2955
  }
2946
2956
  cleanSetup() {
@@ -2950,20 +2960,29 @@
2950
2960
  this._connectionCompleted = false;
2951
2961
  this._messageChannel = undefined;
2952
2962
  if (window.__karma__) {
2953
- this.configure();
2954
- this.ioController._initializationStarted = false;
2963
+ this.testCleanupSetup();
2955
2964
  }
2956
2965
  }
2966
+ testCleanupSetup() {
2967
+ this.ioc.resetState();
2968
+ this.ioc.bridgeController.configure();
2969
+ }
2957
2970
  handleGoodbyeRequest(data) {
2958
2971
  const decodedRequest = wcp6GoodbyeRequestDecoder.run(data);
2959
2972
  if (!decodedRequest.ok) {
2960
2973
  const errorMsg = `Received invalid '${WCPMessageTypes.WCP6Goodbye}' request: ${JSON.stringify(data)}. Reason: ${decodedRequest.error.message} at ${decodedRequest.error.at}`;
2961
- console.error(errorMsg);
2974
+ this.logMessage({ level: "error", message: errorMsg });
2962
2975
  this.sendMessage({ error: "MalformedMessage", message: errorMsg });
2963
2976
  return;
2964
2977
  }
2965
2978
  this.cleanSetup();
2966
2979
  }
2980
+ logMessage({ level, message }) {
2981
+ if (this.logger) {
2982
+ return this.logger[level](message);
2983
+ }
2984
+ console[level](message);
2985
+ }
2967
2986
  }
2968
2987
 
2969
2988
  class DomainsController {
@@ -2992,14 +3011,12 @@
2992
3011
  this.logger?.error(`Cannot find domain for message type: ${data.type}`);
2993
3012
  return { error: "MalformedMessage", message: `Received unsupported message type: ${data.type}` };
2994
3013
  }
2995
- this.logger?.info(`Sending request ${data.type} to domain '${domain.name}'`);
2996
3014
  const result = await domain.controller.handleControl(data);
2997
3015
  const decodedResultTypeResponse = responseMessageTypeDecoder.run(result.type);
2998
3016
  if (!decodedResultTypeResponse.ok) {
2999
3017
  this.logger?.error(`Received invalid response type: ${JSON.stringify(data)}`);
3000
3018
  return { error: "MalformedMessage", message: `Received invalid response message type: ${decodedResultTypeResponse.error.message}` };
3001
3019
  }
3002
- this.logger?.info(`Returning domain result: ${JSON.stringify(result)}`);
3003
3020
  return result;
3004
3021
  }
3005
3022
  setupDomains() {
@@ -3353,6 +3370,7 @@
3353
3370
  class IntentsController {
3354
3371
  sendBridgeMessage;
3355
3372
  addPrivateChannel;
3373
+ isIntentResolverEnabled;
3356
3374
  logger;
3357
3375
  listenersCollection = {};
3358
3376
  intentResolutionsCollection = {};
@@ -3368,9 +3386,10 @@
3368
3386
  raiseIntentRequest: { requestType: "raiseIntentRequest", requestDecoder: raiseIntentRequestDecoder, responseType: "raiseIntentResponse", responseDecoder: raiseIntentResponseDecoder, execute: this.handleRaiseIntentRequest.bind(this) },
3369
3387
  raiseIntentResultRequest: { requestType: "raiseIntentResultRequest", requestDecoder: raiseIntentResultRequestDecoder, responseType: "raiseIntentResultResponse", responseDecoder: raiseIntentResultResponseDecoder, execute: this.handleRaiseIntentResultResponse.bind(this) },
3370
3388
  };
3371
- constructor(sendBridgeMessage, addPrivateChannel, logger) {
3389
+ constructor(sendBridgeMessage, addPrivateChannel, isIntentResolverEnabled, logger) {
3372
3390
  this.sendBridgeMessage = sendBridgeMessage;
3373
3391
  this.addPrivateChannel = addPrivateChannel;
3392
+ this.isIntentResolverEnabled = isIntentResolverEnabled;
3374
3393
  this.logger = logger;
3375
3394
  }
3376
3395
  async handleControl(request) {
@@ -3489,7 +3508,10 @@
3489
3508
  intentEventResultPromise.resolve(payload.intentResult);
3490
3509
  return {};
3491
3510
  }
3492
- async handleRaiseIntentForContextRequest({ meta, payload }) {
3511
+ async handleRaiseIntentForContextRequest({ meta, payload, type }) {
3512
+ if (this.isIntentResolverEnabled && !payload.app) {
3513
+ return this.handleRaiseIntentForContextRequestWithResolver({ payload, meta, type });
3514
+ }
3493
3515
  const intentResolution = await window.fdc3?.raiseIntentForContext(payload.context, payload.app);
3494
3516
  if (!intentResolution) {
3495
3517
  return { error: "IntentDeliveryFailed" };
@@ -3504,7 +3526,24 @@
3504
3526
  });
3505
3527
  return { intentResolution: intentResolutionToReturn };
3506
3528
  }
3507
- async handleRaiseIntentRequest({ payload, meta }) {
3529
+ async handleRaiseIntentForContextRequestWithResolver({ payload, meta, type }) {
3530
+ const appIntents = await window.fdc3?.findIntentsByContext(payload.context);
3531
+ if (!appIntents || !appIntents.length) {
3532
+ return { error: "NoAppsFound" };
3533
+ }
3534
+ const allAvailableHandlers = appIntents.map((intent) => intent.apps).flat();
3535
+ if (allAvailableHandlers.length > 1) {
3536
+ this.logger?.info(`Multiple apps found for context type '${payload.context.type}'. Requesting user to select app using the Intent Resolver UI. Apps: ${JSON.stringify(allAvailableHandlers)}`);
3537
+ return { appIntents };
3538
+ }
3539
+ const updatedPayload = { ...payload, app: allAvailableHandlers[0] };
3540
+ this.logger?.info(`Only one app found to handle context type '${payload.context.type}'. Proceeding to raise intent to app: '${updatedPayload.app}'`);
3541
+ return this.handleRaiseIntentForContextRequest({ type, meta, payload: updatedPayload });
3542
+ }
3543
+ async handleRaiseIntentRequest({ payload, meta, type }) {
3544
+ if (this.isIntentResolverEnabled && !payload.app) {
3545
+ return this.handleRaiseIntentRequestWithResolver({ payload, meta, type });
3546
+ }
3508
3547
  const intentResolution = await window.fdc3?.raiseIntent(payload.intent, payload.context, payload.app);
3509
3548
  if (!intentResolution) {
3510
3549
  return { error: "IntentDeliveryFailed" };
@@ -3519,6 +3558,19 @@
3519
3558
  });
3520
3559
  return { intentResolution: intentResolutionToReturn };
3521
3560
  }
3561
+ async handleRaiseIntentRequestWithResolver({ payload, meta, type }) {
3562
+ const appIntent = await window.fdc3?.findIntent(payload.intent, payload.context);
3563
+ if (!appIntent || !appIntent.apps.length) {
3564
+ return { error: "NoAppsFound" };
3565
+ }
3566
+ if (appIntent.apps.length > 1) {
3567
+ this.logger?.info(`Multiple apps found for intent '${payload.intent}'. Requesting user to select app using the Intent Resolver UI. Apps: ${JSON.stringify(appIntent.apps)}`);
3568
+ return { appIntent };
3569
+ }
3570
+ const updatedPayload = { ...payload, app: appIntent.apps[0] };
3571
+ this.logger?.info(`Only one app found to handle intent. Proceeding to raise intent to app: '${updatedPayload.app}'`);
3572
+ return this.handleRaiseIntentRequest({ type, meta, payload: updatedPayload });
3573
+ }
3522
3574
  async handleRaiseIntentResultResponse({ payload, meta }) {
3523
3575
  this.logger?.info(`handleRaiseIntentResultResponse called with payload: ${JSON.stringify(payload)} and meta: ${JSON.stringify(meta)}`);
3524
3576
  const intentResolution = await this.intentResolutionsCollection[meta.requestUuid];
@@ -3531,8 +3583,8 @@
3531
3583
  this.logger?.info(`Received intent result: ${JSON.stringify(result)}`);
3532
3584
  if (!result) {
3533
3585
  const intentStr = payload.intent ? "intent '" + payload.intent + "'" : "";
3534
- const contextStr = payload.context ? "context type '" + payload.context + "'" : "";
3535
- this.logger?.info(`Intent result is void for ${intentStr}${contextStr} and requestUuid: ${meta.requestUuid}`);
3586
+ const contextStr = payload.context ? "context type '" + payload.context.type + "'" : "";
3587
+ this.logger?.info(`Intent result is void for ${intentStr}, ${contextStr} and requestUuid: ${meta.requestUuid}`);
3536
3588
  return { intentResult: undefined };
3537
3589
  }
3538
3590
  const intentResult = this.extractRaiseIntentResult(result);
@@ -3600,6 +3652,22 @@
3600
3652
  _channelsController;
3601
3653
  _intentsController;
3602
3654
  _applicationsController;
3655
+ _uiConfig;
3656
+ resetState() {
3657
+ this._bridgeController = undefined;
3658
+ this._ioController = undefined;
3659
+ this._domainsController = undefined;
3660
+ this._channelsController = undefined;
3661
+ this._intentsController = undefined;
3662
+ this._applicationsController = undefined;
3663
+ this._uiConfig = undefined;
3664
+ }
3665
+ get uiConfig() {
3666
+ return this._uiConfig;
3667
+ }
3668
+ set uiConfig(value) {
3669
+ this._uiConfig = value;
3670
+ }
3603
3671
  get bridgeController() {
3604
3672
  if (!this._bridgeController) {
3605
3673
  this._bridgeController = new BridgeController(this.ioController, this);
@@ -3629,7 +3697,7 @@
3629
3697
  }
3630
3698
  get intentsController() {
3631
3699
  if (!this._intentsController) {
3632
- this._intentsController = new IntentsController(this.bridgeController.sendMessage.bind(this.bridgeController), this.channelsController.addPrivateChannel.bind(this.channelsController), this.ioController.getLogger("intents.controller"));
3700
+ this._intentsController = new IntentsController(this.bridgeController.sendMessage.bind(this.bridgeController), this.channelsController.addPrivateChannel.bind(this.channelsController), this.uiConfig.isIntentResolverEnabled, this.ioController.getLogger("intents.controller"));
3633
3701
  }
3634
3702
  return this._intentsController;
3635
3703
  }
@@ -3641,7 +3709,7 @@
3641
3709
  }
3642
3710
  }
3643
3711
 
3644
- var version = "1.1.1-beta.0";
3712
+ var version = "1.2.0";
3645
3713
 
3646
3714
  window.fdc3WebBridgeInfo = {
3647
3715
  version