@@ -87,7 +87,7 @@ export function initializeCommunication(
87
87
apiVersionTag : string ,
88
88
) : Promise < InitializeResponse > {
89
89
// Listen for messages post to our window
90
- CommunicationPrivate . messageListener = async ( evt : DOMMessageEvent ) : Promise < void > => await processMessage ( evt ) ;
90
+ CommunicationPrivate . messageListener = ( evt : DOMMessageEvent ) : Promise < void > => processIncomingMessage ( evt ) ;
91
91
92
92
// If we are in an iframe, our parent window is the one hosting us (i.e., window.parent); otherwise,
93
93
// it's the window that opened us (i.e., window.opener)
@@ -108,7 +108,7 @@ export function initializeCommunication(
108
108
const extendedWindow = Communication . currentWindow as unknown as ExtendedWindow ;
109
109
if ( extendedWindow . nativeInterface ) {
110
110
GlobalVars . isFramelessWindow = true ;
111
- extendedWindow . onNativeMessage = handleParentMessage ;
111
+ extendedWindow . onNativeMessage = handleIncomingMessageFromParent ;
112
112
} else {
113
113
// at this point we weren't able to find a parent to talk to, no way initialization will succeed
114
114
return Promise . reject ( new Error ( 'Initialization Failed. No Parent window found.' ) ) ;
@@ -368,7 +368,9 @@ export function sendNestedAuthRequestToTopWindow(message: string): NestedAppAuth
368
368
const targetWindow = Communication . topWindow ;
369
369
const request = createNestedAppAuthRequest ( message ) ;
370
370
371
- logger ( 'Message %i information: %o' , request . uuid , { actionName : request . func } ) ;
371
+ logger ( 'Message %s information: %o' , getMessageIdsAsLogString ( request ) , {
372
+ actionName : request . func ,
373
+ } ) ;
372
374
373
375
return sendRequestToTargetWindowHelper ( targetWindow , request ) as NestedAppAuthRequest ;
374
376
}
@@ -389,7 +391,11 @@ function sendRequestToTargetWindowHelper(
389
391
390
392
if ( GlobalVars . isFramelessWindow ) {
391
393
if ( Communication . currentWindow && Communication . currentWindow . nativeInterface ) {
392
- logger ( `Sending message %i to ${ targetWindowName } via framelessPostMessage interface` , request . uuidAsString ) ;
394
+ logger (
395
+ 'Sending message %s to %s via framelessPostMessage interface' ,
396
+ getMessageIdsAsLogString ( request ) ,
397
+ targetWindowName ,
398
+ ) ;
393
399
( Communication . currentWindow as ExtendedWindow ) . nativeInterface . framelessPostMessage ( JSON . stringify ( request ) ) ;
394
400
}
395
401
} else {
@@ -398,10 +404,10 @@ function sendRequestToTargetWindowHelper(
398
404
// If the target window isn't closed and we already know its origin, send the message right away; otherwise,
399
405
// queue the message and send it after the origin is established
400
406
if ( targetWindow && targetOrigin ) {
401
- logger ( ` Sending message %i to ${ targetWindowName } via postMessage` , request . uuidAsString ) ;
407
+ logger ( ' Sending message %s to %s via postMessage' , getMessageIdsAsLogString ( request ) , targetWindowName ) ;
402
408
targetWindow . postMessage ( request , targetOrigin ) ;
403
409
} else {
404
- logger ( ` Adding message %i to ${ targetWindowName } message queue` , request . uuidAsString ) ;
410
+ logger ( ' Adding message %s to %s message queue' , getMessageIdsAsLogString ( request ) , targetWindowName ) ;
405
411
getTargetMessageQueue ( targetWindow ) . push ( messageRequest ) ;
406
412
}
407
413
}
@@ -424,21 +430,24 @@ function sendMessageToParentHelper(
424
430
const targetWindow = Communication . parentWindow ;
425
431
const request = createMessageRequest ( apiVersionTag , actionName , args ) ;
426
432
427
- logger ( 'Message %i information: %o' , request . uuid , { actionName, args } ) ;
433
+ logger ( 'Message %s information: %o' , getMessageIdsAsLogString ( request ) , { actionName, args } ) ;
428
434
429
435
return sendRequestToTargetWindowHelper ( targetWindow , request ) ;
430
436
}
431
437
432
- const processMessageLogger = communicationLogger . extend ( 'processMessage ' ) ;
438
+ const processIncomingMessageLogger = communicationLogger . extend ( 'processIncomingMessage ' ) ;
433
439
434
440
/**
435
441
* @internal
436
442
* Limited to Microsoft-internal use
437
443
*/
438
- async function processMessage ( evt : DOMMessageEvent ) : Promise < void > {
444
+ async function processIncomingMessage ( evt : DOMMessageEvent ) : Promise < void > {
439
445
// Process only if we received a valid message
440
446
if ( ! evt || ! evt . data || typeof evt . data !== 'object' ) {
441
- processMessageLogger ( 'Unrecognized message format received by app, message being ignored. Message: %o' , evt ) ;
447
+ processIncomingMessageLogger (
448
+ 'Unrecognized message format received by app, message being ignored. Message: %o' ,
449
+ evt ,
450
+ ) ;
442
451
return ;
443
452
}
444
453
@@ -448,20 +457,23 @@ async function processMessage(evt: DOMMessageEvent): Promise<void> {
448
457
const messageSource = evt . source || ( evt . originalEvent && evt . originalEvent . source ) ;
449
458
const messageOrigin = evt . origin || ( evt . originalEvent && evt . originalEvent . origin ) ;
450
459
451
- return shouldProcessMessage ( messageSource , messageOrigin ) . then ( ( result ) => {
460
+ return shouldProcessIncomingMessage ( messageSource , messageOrigin ) . then ( ( result ) => {
452
461
if ( ! result ) {
453
- processMessageLogger (
454
- 'Message being ignored by app because it is either coming from the current window or a different window with an invalid origin' ,
462
+ processIncomingMessageLogger (
463
+ 'Message being ignored by app because it is either coming from the current window or a different window with an invalid origin, message: %o, source: %o, origin: %o' ,
464
+ evt ,
465
+ messageSource ,
466
+ messageOrigin ,
455
467
) ;
456
468
return ;
457
469
}
458
470
// Update our parent and child relationships based on this message
459
471
updateRelationships ( messageSource , messageOrigin ) ;
460
472
// Handle the message
461
473
if ( messageSource === Communication . parentWindow ) {
462
- handleParentMessage ( evt ) ;
474
+ handleIncomingMessageFromParent ( evt ) ;
463
475
} else if ( messageSource === Communication . childWindow ) {
464
- handleChildMessage ( evt ) ;
476
+ handleIncomingMessageFromChild ( evt ) ;
465
477
}
466
478
} ) ;
467
479
}
@@ -511,7 +523,7 @@ function processAuthBridgeMessage(evt: MessageEvent, onMessageReceived: (respons
511
523
return ;
512
524
}
513
525
514
- if ( ! shouldProcessMessage ( messageSource , messageOrigin ) ) {
526
+ if ( ! shouldProcessIncomingMessage ( messageSource , messageOrigin ) ) {
515
527
logger (
516
528
'Message being ignored by app because it is either coming from the current window or a different window with an invalid origin' ,
517
529
) ;
@@ -542,7 +554,7 @@ function processAuthBridgeMessage(evt: MessageEvent, onMessageReceived: (respons
542
554
onMessageReceived ( message ) ;
543
555
}
544
556
545
- const shouldProcessMessageLogger = communicationLogger . extend ( 'shouldProcessMessage ' ) ;
557
+ const shouldProcessIncomingMessageLogger = communicationLogger . extend ( 'shouldProcessIncomingMessage ' ) ;
546
558
547
559
/**
548
560
* @hidden
@@ -551,11 +563,11 @@ const shouldProcessMessageLogger = communicationLogger.extend('shouldProcessMess
551
563
* @internal
552
564
* Limited to Microsoft-internal use
553
565
*/
554
- async function shouldProcessMessage ( messageSource : Window , messageOrigin : string ) : Promise < boolean > {
566
+ async function shouldProcessIncomingMessage ( messageSource : Window , messageOrigin : string ) : Promise < boolean > {
555
567
// Process if message source is a different window and if origin is either in
556
568
// Teams' pre-known whitelist or supplied as valid origin by user during initialization
557
569
if ( Communication . currentWindow && messageSource === Communication . currentWindow ) {
558
- shouldProcessMessageLogger ( 'Should not process message because it is coming from the current window' ) ;
570
+ shouldProcessIncomingMessageLogger ( 'Should not process message because it is coming from the current window' ) ;
559
571
return false ;
560
572
} else if (
561
573
Communication . currentWindow &&
@@ -569,13 +581,13 @@ async function shouldProcessMessage(messageSource: Window, messageOrigin: string
569
581
try {
570
582
messageOriginURL = new URL ( messageOrigin ) ;
571
583
} catch ( _ ) {
572
- shouldProcessMessageLogger ( 'Message has an invalid origin of %s' , messageOrigin ) ;
584
+ shouldProcessIncomingMessageLogger ( 'Message has an invalid origin of %s' , messageOrigin ) ;
573
585
return false ;
574
586
}
575
587
576
588
const isOriginValid = await validateOrigin ( messageOriginURL ) ;
577
589
if ( ! isOriginValid ) {
578
- shouldProcessMessageLogger ( 'Message has an invalid origin of %s' , messageOrigin ) ;
590
+ shouldProcessIncomingMessageLogger ( 'Message has an invalid origin of %s' , messageOrigin ) ;
579
591
}
580
592
return isOriginValid ;
581
593
}
@@ -619,14 +631,14 @@ function updateRelationships(messageSource: Window, messageOrigin: string): void
619
631
flushMessageQueue ( Communication . childWindow ) ;
620
632
}
621
633
622
- const handleParentMessageLogger = communicationLogger . extend ( 'handleParentMessage ' ) ;
634
+ const handleIncomingMessageFromParentLogger = communicationLogger . extend ( 'handleIncomingMessageFromParent ' ) ;
623
635
624
636
/**
625
637
* @internal
626
638
* Limited to Microsoft-internal use
627
639
*/
628
640
function retrieveMessageUUIDFromResponse ( response : MessageResponse ) : MessageUUID | undefined {
629
- const logger = handleParentMessageLogger ;
641
+ const logger = handleIncomingMessageFromParentLogger ;
630
642
if ( response . uuid ) {
631
643
const responseUUID = response . uuid ;
632
644
const callbackUUID = retrieveMessageUUIDFromCallback ( CommunicationPrivate . callbacks , responseUUID ) ;
@@ -644,10 +656,7 @@ function retrieveMessageUUIDFromResponse(response: MessageResponse): MessageUUID
644
656
} else {
645
657
return CommunicationPrivate . legacyMessageIdsToUuidMap [ response . id ] ;
646
658
}
647
- logger (
648
- `Received a message with uuid: ${ response . uuid ?. toString ( ) } and legacyId: %i that failed to produce a callbackId` ,
649
- response . id ,
650
- ) ;
659
+ logger ( 'Received message %s that failed to produce a callbackId' , getMessageIdsAsLogString ( response ) ) ;
651
660
return undefined ;
652
661
}
653
662
@@ -657,7 +666,7 @@ function retrieveMessageUUIDFromResponse(response: MessageResponse): MessageUUID
657
666
*
658
667
* This function is used to compare a new MessageUUID object value to the key values in the specified callback and retrieving that key
659
668
* We use this because two objects with the same value are not considered equivalent therefore we can't use the new MessageUUID object
660
- * as a key to retrieve the value assosciated with it and should use this function instead.
669
+ * as a key to retrieve the value associated with it and should use this function instead.
661
670
*/
662
671
function retrieveMessageUUIDFromCallback (
663
672
map : Map < MessageUUID , Function > ,
@@ -695,8 +704,8 @@ function removeMessageHandlers(message: MessageResponse, map: Map<MessageUUID, F
695
704
* @internal
696
705
* Limited to Microsoft-internal use
697
706
*/
698
- function handleParentMessage ( evt : DOMMessageEvent ) : void {
699
- const logger = handleParentMessageLogger ;
707
+ function handleIncomingMessageFromParent ( evt : DOMMessageEvent ) : void {
708
+ const logger = handleIncomingMessageFromParentLogger ;
700
709
701
710
if ( 'id' in evt . data && typeof evt . data . id === 'number' ) {
702
711
// Call any associated Communication.callbacks
@@ -705,37 +714,49 @@ function handleParentMessage(evt: DOMMessageEvent): void {
705
714
const callbackId = retrieveMessageUUIDFromResponse ( message ) ;
706
715
if ( callbackId ) {
707
716
const callback = CommunicationPrivate . callbacks . get ( callbackId ) ;
708
- logger ( 'Received a response from parent for message %i ' , callbackId ) ;
717
+ logger ( 'Received a response from parent for message %s ' , callbackId . toString ( ) ) ;
709
718
if ( callback ) {
710
- logger ( 'Invoking the registered callback for message %i with arguments %o' , callbackId , message . args ) ;
719
+ logger (
720
+ 'Invoking the registered callback for message %s with arguments %o' ,
721
+ callbackId . toString ( ) ,
722
+ message . args ,
723
+ ) ;
711
724
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
712
725
// @ts -ignore
713
726
callback . apply ( null , [ ...message . args , message . isPartialResponse ] ) ;
714
727
715
728
// Remove the callback to ensure that the callback is called only once and to free up memory if response is a complete response
716
729
if ( ! isPartialResponse ( evt ) ) {
717
- logger ( 'Removing registered callback for message %i ' , callbackId ) ;
730
+ logger ( 'Removing registered callback for message %s ' , callbackId . toString ( ) ) ;
718
731
removeMessageHandlers ( message , CommunicationPrivate . callbacks ) ;
719
732
}
720
733
}
721
734
const promiseCallback = CommunicationPrivate . promiseCallbacks . get ( callbackId ) ;
722
735
if ( promiseCallback ) {
723
- logger ( 'Invoking the registered promise callback for message %i with arguments %o' , callbackId , message . args ) ;
736
+ logger (
737
+ 'Invoking the registered promise callback for message %s with arguments %o' ,
738
+ callbackId . toString ( ) ,
739
+ message . args ,
740
+ ) ;
724
741
promiseCallback ( message . args ) ;
725
742
726
- logger ( 'Removing registered promise callback for message %i ' , callbackId ) ;
743
+ logger ( 'Removing registered promise callback for message %s ' , callbackId . toString ( ) ) ;
727
744
removeMessageHandlers ( message , CommunicationPrivate . promiseCallbacks ) ;
728
745
}
729
746
const portCallback = CommunicationPrivate . portCallbacks . get ( callbackId ) ;
730
747
if ( portCallback ) {
731
- logger ( 'Invoking the registered port callback for message %i with arguments %o' , callbackId , message . args ) ;
748
+ logger (
749
+ 'Invoking the registered port callback for message %s with arguments %o' ,
750
+ callbackId . toString ( ) ,
751
+ message . args ,
752
+ ) ;
732
753
let port : MessagePort | undefined ;
733
754
if ( evt . ports && evt . ports [ 0 ] instanceof MessagePort ) {
734
755
port = evt . ports [ 0 ] ;
735
756
}
736
757
portCallback ( port , message . args ) ;
737
758
738
- logger ( 'Removing registered port callback for message %i ' , callbackId ) ;
759
+ logger ( 'Removing registered port callback for message %s ' , callbackId . toString ( ) ) ;
739
760
removeMessageHandlers ( message , CommunicationPrivate . portCallbacks ) ;
740
761
}
741
762
if ( message . uuid ) {
@@ -745,7 +766,7 @@ function handleParentMessage(evt: DOMMessageEvent): void {
745
766
} else if ( 'func' in evt . data && typeof evt . data . func === 'string' ) {
746
767
// Delegate the request to the proper handler
747
768
const message = evt . data as MessageRequest ;
748
- logger ( 'Received an action message %s from parent' , message . func ) ;
769
+ logger ( 'Received a message from parent %s, action: "%s"' , getMessageIdsAsLogString ( message ) , message . func ) ;
749
770
callHandler ( message . func , message . args ) ;
750
771
} else {
751
772
logger ( 'Received an unknown message: %O' , evt ) ;
@@ -760,28 +781,47 @@ function isPartialResponse(evt: DOMMessageEvent): boolean {
760
781
return evt . data . isPartialResponse === true ;
761
782
}
762
783
784
+ const handleIncomingMessageFromChildLogger = communicationLogger . extend ( 'handleIncomingMessageFromChild' ) ;
785
+
763
786
/**
764
787
* @internal
765
788
* Limited to Microsoft-internal use
766
789
*/
767
- function handleChildMessage ( evt : DOMMessageEvent ) : void {
790
+ function handleIncomingMessageFromChild ( evt : DOMMessageEvent ) : void {
768
791
if ( 'id' in evt . data && 'func' in evt . data ) {
769
792
// Try to delegate the request to the proper handler, if defined
770
793
const message = deserializeMessageRequest ( evt . data as SerializedMessageRequest ) ;
771
794
const [ called , result ] = callHandler ( message . func , message . args ) ;
772
795
if ( called && typeof result !== 'undefined' ) {
796
+ handleIncomingMessageFromChildLogger (
797
+ 'Returning message %s from child back to child, action: %s.' ,
798
+ getMessageIdsAsLogString ( message ) ,
799
+ message . func ,
800
+ ) ;
801
+
773
802
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
774
803
// @ts -ignore
775
804
sendMessageResponseToChild ( message . id , message . uuid , Array . isArray ( result ) ? result : [ result ] ) ;
776
805
} else {
777
806
// No handler, proxy to parent
807
+
808
+ handleIncomingMessageFromChildLogger (
809
+ 'Relaying message %s from child to parent, action: %s. Relayed message will have a new id.' ,
810
+ getMessageIdsAsLogString ( message ) ,
811
+ message . func ,
812
+ ) ;
813
+
778
814
sendMessageToParent (
779
815
getApiVersionTag ( ApiVersionNumber . V_2 , ApiName . Tasks_StartTask ) ,
780
816
message . func ,
781
817
message . args ,
782
818
( ...args : any [ ] ) : void => {
783
819
if ( Communication . childWindow ) {
784
820
const isPartialResponse = args . pop ( ) ;
821
+ handleIncomingMessageFromChildLogger (
822
+ 'Message from parent being relayed to child, id: %s' ,
823
+ getMessageIdsAsLogString ( message ) ,
824
+ ) ;
785
825
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
786
826
// @ts -ignore
787
827
sendMessageResponseToChild ( message . id , message . uuid , args , isPartialResponse ) ;
@@ -869,8 +909,9 @@ function flushMessageQueue(targetWindow: Window | any): void {
869
909
870
910
/* eslint-disable-next-line strict-null-checks/all */ /* Fix tracked by 5730662 */
871
911
flushMessageQueueLogger (
872
- 'Flushing message %i from ' + target + ' message queue via postMessage.' ,
873
- request ?. uuidAsString ,
912
+ 'Flushing message %s from %s message queue via postMessage.' ,
913
+ getMessageIdsAsLogString ( request ) ,
914
+ target ,
874
915
) ;
875
916
876
917
targetWindow . postMessage ( request , targetOrigin ) ;
@@ -1020,3 +1061,21 @@ function createMessageEvent(func: string, args?: any[]): MessageRequest {
1020
1061
args : args || [ ] ,
1021
1062
} ;
1022
1063
}
1064
+
1065
+ function getMessageIdsAsLogString (
1066
+ message :
1067
+ | SerializedMessageRequest
1068
+ | SerializedMessageResponse
1069
+ | MessageRequestWithRequiredProperties
1070
+ | MessageRequest
1071
+ | MessageResponse
1072
+ | NestedAppAuthRequest ,
1073
+ ) : string {
1074
+ if ( 'uuidAsString' in message ) {
1075
+ return `${ message . uuidAsString } (legacy id: ${ message . id } )` ;
1076
+ } else if ( 'uuid' in message && message . uuid !== undefined ) {
1077
+ return `${ message . uuid . toString ( ) } (legacy id: ${ message . id } )` ;
1078
+ } else {
1079
+ return `legacy id: ${ message . id } (no uuid)` ;
1080
+ }
1081
+ }
0 commit comments