@interopio/fdc3-web-bridge 1.1.1-beta.0 → 1.3.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,18 @@
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
+ });
1588
+ const urlDecoder = nonEmptyStringDecoder$4.andThen((url) => {
1589
+ try {
1590
+ new URL(url);
1591
+ return nonEmptyStringDecoder$4;
1592
+ }
1593
+ catch {
1594
+ return fail("Should be a valid URL");
1595
+ }
1596
+ });
1585
1597
 
1586
1598
  const intentDefinitionDecoder = object({
1587
1599
  name: nonEmptyStringDecoder$4,
@@ -1908,7 +1920,9 @@
1908
1920
  const decoders$1 = {
1909
1921
  common: {
1910
1922
  nonEmptyStringDecoder: nonEmptyStringDecoder$4,
1911
- nonNegativeNumberDecoder
1923
+ nonNegativeNumberDecoder,
1924
+ regexDecoder,
1925
+ urlDecoder
1912
1926
  },
1913
1927
  fdc3: {
1914
1928
  allDefinitionsDecoder,
@@ -1998,7 +2012,7 @@
1998
2012
  channelSelector: optional$1(boolean$1()),
1999
2013
  fdc3Version: nonEmptyStringDecoder$3,
2000
2014
  identityUrl: nonEmptyStringDecoder$3,
2001
- resolver: optional$1(boolean$1())
2015
+ intentResolver: optional$1(boolean$1())
2002
2016
  });
2003
2017
  const wcp1HelloMetaDecoder = object$1({
2004
2018
  connectionAttemptUuid: nonEmptyStringDecoder$3,
@@ -2838,13 +2852,14 @@
2838
2852
  }
2839
2853
  handleHelloConnectionRequest(source, origin, data) {
2840
2854
  if (this._messageChannel) {
2841
- console.warn(`${WCPMessageTypes.WCP1Hello} already heard. Message channel port created`);
2855
+ this.logMessage({ level: "warn", message: `${WCPMessageTypes.WCP1Hello} already heard. Message channel port created` });
2842
2856
  return;
2843
2857
  }
2858
+ this.logMessage({ level: "info", message: `Received ${WCPMessageTypes.WCP1Hello} request: ${JSON.stringify(data)} from origin: ${origin}` });
2844
2859
  const decodedRequest = wcp1HelloDecoder.run(data);
2845
2860
  if (!decodedRequest.ok) {
2846
2861
  const errorMsg = `Received invalid '${WCPMessageTypes.WCP1Hello}' request: ${JSON.stringify(data)}. Reason: ${decodedRequest.error.message} at ${decodedRequest.error.at}`;
2847
- console.error(errorMsg);
2862
+ this.logMessage({ level: "error", message: errorMsg });
2848
2863
  source.postMessage({ error: "MalformedMessage", message: errorMsg }, origin);
2849
2864
  return;
2850
2865
  }
@@ -2855,9 +2870,9 @@
2855
2870
  window.removeEventListener("message", this._globalMessageHandler);
2856
2871
  }
2857
2872
  async handlePortMessage(event) {
2858
- this.logger?.info(`Received port message with data: ${JSON.stringify(event.data)}}`);
2873
+ this.logMessage({ level: "info", message: `Received port message with data: ${JSON.stringify(event.data)}` });
2859
2874
  if (!this._connectionCompleted && event.data?.type !== WCPMessageTypes.WCP6Goodbye && event.data.type !== WCPMessageTypes.WCP4ValidateAppIdentity) {
2860
- console.warn("Web Connection Protocol not completed yet. Ignoring message");
2875
+ this.logMessage({ level: "warn", message: "Web Connection Protocol not completed yet. Ignoring message" });
2861
2876
  return;
2862
2877
  }
2863
2878
  if (event.data?.type === WCPMessageTypes.WCP4ValidateAppIdentity) {
@@ -2867,33 +2882,36 @@
2867
2882
  return this.handleGoodbyeRequest(event.data);
2868
2883
  }
2869
2884
  const result = await this.ioc.domainsController.handleMessage(event.data);
2870
- this.logger?.info(`Sending response message: ${JSON.stringify(result)}`);
2885
+ this.logMessage({ level: "info", message: `Sending response message: ${JSON.stringify(result)}` });
2871
2886
  this.sendMessage(result);
2872
2887
  }
2873
2888
  async handleAppIdentityValidationRequest(data) {
2874
2889
  const decodedRequest = wcp4ValidateAppIdentityDecoder.run(data);
2875
2890
  if (!decodedRequest.ok) {
2876
2891
  const errorMsg = `Received invalid '${WCPMessageTypes.WCP4ValidateAppIdentity}' request: ${JSON.stringify(data)}. Reason: ${decodedRequest.error.message} at ${decodedRequest.error.at}`;
2877
- console.error(errorMsg);
2892
+ this.logMessage({ level: "error", message: errorMsg });
2878
2893
  this.sendMessage({ error: "MalformedMessage", message: errorMsg });
2879
2894
  return;
2880
2895
  }
2881
2896
  const { actualUrl, identityUrl } = data.payload;
2882
2897
  await this.ioController.initializeLibs();
2883
2898
  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)}`);
2899
+ this.logMessage({ level: "info", message: `IOBrowser and FDC3 libs are initialized. Received ${data.type} request: ${JSON.stringify(data)}` });
2885
2900
  const validationResult = this.ioController.validateIdentity({ actualUrl, identityUrl, helloMsgOrigin: this._helloMessageOrigin });
2886
2901
  if (!validationResult.isValid) {
2887
- this.logger?.info(`App validation failure. Reason: ${validationResult.message}`);
2888
2902
  this.sendValidationFailure(data.meta.connectionAttemptUuid, validationResult.message);
2889
2903
  this.cleanSetup();
2890
2904
  return;
2891
2905
  }
2892
2906
  await this.sendValidationSuccess(data.meta.connectionAttemptUuid, validationResult.appId);
2893
- this.logger?.info(`App validation is successful. Connection is completed`);
2907
+ this.logMessage({ level: "info", message: `App validation is successful. Connection is completed` });
2894
2908
  this._connectionCompleted = true;
2895
2909
  }
2896
2910
  sendHandshake(source, origin, data) {
2911
+ this.ioc.uiConfig = {
2912
+ isChannelSelectorEnabled: !!data.payload.channelSelector,
2913
+ isIntentResolverEnabled: !!data.payload.intentResolver,
2914
+ };
2897
2915
  const handshakeMsg = {
2898
2916
  type: WCPMessageTypes.WCP3Handshake,
2899
2917
  meta: {
@@ -2901,12 +2919,13 @@
2901
2919
  timestamp: new Date()
2902
2920
  },
2903
2921
  payload: {
2904
- channelSelectorUrl: true,
2905
- intentResolverUrl: true,
2906
- fdc3Version: window.fdc3Info.standardVersion,
2922
+ channelSelectorUrl: this.ioc.uiConfig.isChannelSelectorEnabled,
2923
+ intentResolverUrl: this.ioc.uiConfig.isIntentResolverEnabled,
2924
+ fdc3Version: window.fdc3Info?.standardVersion,
2907
2925
  }
2908
2926
  };
2909
2927
  const transfer = this._messageChannel ? [this._messageChannel.port2] : undefined;
2928
+ this.logMessage({ level: "info", message: `Sending ${WCPMessageTypes.WCP3Handshake} message: ${JSON.stringify(handshakeMsg)} to origin: ${origin}` });
2910
2929
  source.postMessage(handshakeMsg, origin, transfer);
2911
2930
  }
2912
2931
  sendValidationFailure(attempt, errorMsg) {
@@ -2920,7 +2939,7 @@
2920
2939
  message: errorMsg
2921
2940
  }
2922
2941
  };
2923
- console.warn(`App Identity Validation failed. Reason: ${errorMsg}`);
2942
+ this.logMessage({ level: "error", message: `App validation failure. Reason: ${errorMsg}. Sending ${WCPMessageTypes.WCP5ValidateAppIdentityFailedResponse} message: ${JSON.stringify(validationFailureMsg)}` });
2924
2943
  this.sendMessage(validationFailureMsg);
2925
2944
  }
2926
2945
  async sendValidationSuccess(attempt, appId) {
@@ -2941,6 +2960,7 @@
2941
2960
  implementationMetadata
2942
2961
  }
2943
2962
  };
2963
+ this.logMessage({ level: "info", message: `App Identity Validation successful. Sending ${WCPMessageTypes.WCP5ValidateAppIdentityResponse} message: ${JSON.stringify(validationSuccessMsg)}` });
2944
2964
  this.sendMessage(validationSuccessMsg);
2945
2965
  }
2946
2966
  cleanSetup() {
@@ -2950,20 +2970,29 @@
2950
2970
  this._connectionCompleted = false;
2951
2971
  this._messageChannel = undefined;
2952
2972
  if (window.__karma__) {
2953
- this.configure();
2954
- this.ioController._initializationStarted = false;
2973
+ this.testCleanupSetup();
2955
2974
  }
2956
2975
  }
2976
+ testCleanupSetup() {
2977
+ this.ioc.resetState();
2978
+ this.ioc.bridgeController.configure();
2979
+ }
2957
2980
  handleGoodbyeRequest(data) {
2958
2981
  const decodedRequest = wcp6GoodbyeRequestDecoder.run(data);
2959
2982
  if (!decodedRequest.ok) {
2960
2983
  const errorMsg = `Received invalid '${WCPMessageTypes.WCP6Goodbye}' request: ${JSON.stringify(data)}. Reason: ${decodedRequest.error.message} at ${decodedRequest.error.at}`;
2961
- console.error(errorMsg);
2984
+ this.logMessage({ level: "error", message: errorMsg });
2962
2985
  this.sendMessage({ error: "MalformedMessage", message: errorMsg });
2963
2986
  return;
2964
2987
  }
2965
2988
  this.cleanSetup();
2966
2989
  }
2990
+ logMessage({ level, message }) {
2991
+ if (this.logger) {
2992
+ return this.logger[level](message);
2993
+ }
2994
+ console[level](message);
2995
+ }
2967
2996
  }
2968
2997
 
2969
2998
  class DomainsController {
@@ -2992,14 +3021,12 @@
2992
3021
  this.logger?.error(`Cannot find domain for message type: ${data.type}`);
2993
3022
  return { error: "MalformedMessage", message: `Received unsupported message type: ${data.type}` };
2994
3023
  }
2995
- this.logger?.info(`Sending request ${data.type} to domain '${domain.name}'`);
2996
3024
  const result = await domain.controller.handleControl(data);
2997
3025
  const decodedResultTypeResponse = responseMessageTypeDecoder.run(result.type);
2998
3026
  if (!decodedResultTypeResponse.ok) {
2999
3027
  this.logger?.error(`Received invalid response type: ${JSON.stringify(data)}`);
3000
3028
  return { error: "MalformedMessage", message: `Received invalid response message type: ${decodedResultTypeResponse.error.message}` };
3001
3029
  }
3002
- this.logger?.info(`Returning domain result: ${JSON.stringify(result)}`);
3003
3030
  return result;
3004
3031
  }
3005
3032
  setupDomains() {
@@ -3353,6 +3380,7 @@
3353
3380
  class IntentsController {
3354
3381
  sendBridgeMessage;
3355
3382
  addPrivateChannel;
3383
+ isIntentResolverEnabled;
3356
3384
  logger;
3357
3385
  listenersCollection = {};
3358
3386
  intentResolutionsCollection = {};
@@ -3368,9 +3396,10 @@
3368
3396
  raiseIntentRequest: { requestType: "raiseIntentRequest", requestDecoder: raiseIntentRequestDecoder, responseType: "raiseIntentResponse", responseDecoder: raiseIntentResponseDecoder, execute: this.handleRaiseIntentRequest.bind(this) },
3369
3397
  raiseIntentResultRequest: { requestType: "raiseIntentResultRequest", requestDecoder: raiseIntentResultRequestDecoder, responseType: "raiseIntentResultResponse", responseDecoder: raiseIntentResultResponseDecoder, execute: this.handleRaiseIntentResultResponse.bind(this) },
3370
3398
  };
3371
- constructor(sendBridgeMessage, addPrivateChannel, logger) {
3399
+ constructor(sendBridgeMessage, addPrivateChannel, isIntentResolverEnabled, logger) {
3372
3400
  this.sendBridgeMessage = sendBridgeMessage;
3373
3401
  this.addPrivateChannel = addPrivateChannel;
3402
+ this.isIntentResolverEnabled = isIntentResolverEnabled;
3374
3403
  this.logger = logger;
3375
3404
  }
3376
3405
  async handleControl(request) {
@@ -3489,7 +3518,10 @@
3489
3518
  intentEventResultPromise.resolve(payload.intentResult);
3490
3519
  return {};
3491
3520
  }
3492
- async handleRaiseIntentForContextRequest({ meta, payload }) {
3521
+ async handleRaiseIntentForContextRequest({ meta, payload, type }) {
3522
+ if (this.isIntentResolverEnabled && !payload.app) {
3523
+ return this.handleRaiseIntentForContextRequestWithResolver({ payload, meta, type });
3524
+ }
3493
3525
  const intentResolution = await window.fdc3?.raiseIntentForContext(payload.context, payload.app);
3494
3526
  if (!intentResolution) {
3495
3527
  return { error: "IntentDeliveryFailed" };
@@ -3504,7 +3536,24 @@
3504
3536
  });
3505
3537
  return { intentResolution: intentResolutionToReturn };
3506
3538
  }
3507
- async handleRaiseIntentRequest({ payload, meta }) {
3539
+ async handleRaiseIntentForContextRequestWithResolver({ payload, meta, type }) {
3540
+ const appIntents = await window.fdc3?.findIntentsByContext(payload.context);
3541
+ if (!appIntents || !appIntents.length) {
3542
+ return { error: "NoAppsFound" };
3543
+ }
3544
+ const allAvailableHandlers = appIntents.map((intent) => intent.apps).flat();
3545
+ if (allAvailableHandlers.length > 1) {
3546
+ 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)}`);
3547
+ return { appIntents };
3548
+ }
3549
+ const updatedPayload = { ...payload, app: allAvailableHandlers[0] };
3550
+ this.logger?.info(`Only one app found to handle context type '${payload.context.type}'. Proceeding to raise intent to app: '${updatedPayload.app}'`);
3551
+ return this.handleRaiseIntentForContextRequest({ type, meta, payload: updatedPayload });
3552
+ }
3553
+ async handleRaiseIntentRequest({ payload, meta, type }) {
3554
+ if (this.isIntentResolverEnabled && !payload.app) {
3555
+ return this.handleRaiseIntentRequestWithResolver({ payload, meta, type });
3556
+ }
3508
3557
  const intentResolution = await window.fdc3?.raiseIntent(payload.intent, payload.context, payload.app);
3509
3558
  if (!intentResolution) {
3510
3559
  return { error: "IntentDeliveryFailed" };
@@ -3519,6 +3568,19 @@
3519
3568
  });
3520
3569
  return { intentResolution: intentResolutionToReturn };
3521
3570
  }
3571
+ async handleRaiseIntentRequestWithResolver({ payload, meta, type }) {
3572
+ const appIntent = await window.fdc3?.findIntent(payload.intent, payload.context);
3573
+ if (!appIntent || !appIntent.apps.length) {
3574
+ return { error: "NoAppsFound" };
3575
+ }
3576
+ if (appIntent.apps.length > 1) {
3577
+ 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)}`);
3578
+ return { appIntent };
3579
+ }
3580
+ const updatedPayload = { ...payload, app: appIntent.apps[0] };
3581
+ this.logger?.info(`Only one app found to handle intent. Proceeding to raise intent to app: '${updatedPayload.app}'`);
3582
+ return this.handleRaiseIntentRequest({ type, meta, payload: updatedPayload });
3583
+ }
3522
3584
  async handleRaiseIntentResultResponse({ payload, meta }) {
3523
3585
  this.logger?.info(`handleRaiseIntentResultResponse called with payload: ${JSON.stringify(payload)} and meta: ${JSON.stringify(meta)}`);
3524
3586
  const intentResolution = await this.intentResolutionsCollection[meta.requestUuid];
@@ -3531,8 +3593,8 @@
3531
3593
  this.logger?.info(`Received intent result: ${JSON.stringify(result)}`);
3532
3594
  if (!result) {
3533
3595
  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}`);
3596
+ const contextStr = payload.context ? "context type '" + payload.context.type + "'" : "";
3597
+ this.logger?.info(`Intent result is void for ${intentStr}, ${contextStr} and requestUuid: ${meta.requestUuid}`);
3536
3598
  return { intentResult: undefined };
3537
3599
  }
3538
3600
  const intentResult = this.extractRaiseIntentResult(result);
@@ -3600,6 +3662,22 @@
3600
3662
  _channelsController;
3601
3663
  _intentsController;
3602
3664
  _applicationsController;
3665
+ _uiConfig;
3666
+ resetState() {
3667
+ this._bridgeController = undefined;
3668
+ this._ioController = undefined;
3669
+ this._domainsController = undefined;
3670
+ this._channelsController = undefined;
3671
+ this._intentsController = undefined;
3672
+ this._applicationsController = undefined;
3673
+ this._uiConfig = undefined;
3674
+ }
3675
+ get uiConfig() {
3676
+ return this._uiConfig;
3677
+ }
3678
+ set uiConfig(value) {
3679
+ this._uiConfig = value;
3680
+ }
3603
3681
  get bridgeController() {
3604
3682
  if (!this._bridgeController) {
3605
3683
  this._bridgeController = new BridgeController(this.ioController, this);
@@ -3629,7 +3707,7 @@
3629
3707
  }
3630
3708
  get intentsController() {
3631
3709
  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"));
3710
+ 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
3711
  }
3634
3712
  return this._intentsController;
3635
3713
  }
@@ -3641,7 +3719,7 @@
3641
3719
  }
3642
3720
  }
3643
3721
 
3644
- var version = "1.1.1-beta.0";
3722
+ var version = "1.3.0";
3645
3723
 
3646
3724
  window.fdc3WebBridgeInfo = {
3647
3725
  version