-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor cross domain communications (#19430)
Co-authored-by: Chris Breiding <chrisbreiding@gmail.com>
- Loading branch information
1 parent
00dae70
commit 388bfa4
Showing
11 changed files
with
243 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import debugFn from 'debug' | ||
import { EventEmitter } from 'events' | ||
|
||
const debug = debugFn('cypress:driver:multi-domain') | ||
|
||
const CROSS_DOMAIN_PREFIX = 'cross:domain:' | ||
|
||
/** | ||
* Primary domain communicator. Responsible for sending/receiving events throughout | ||
* the driver responsible for multi-domain communication, as well as sending/receiving events to/from the | ||
* spec bridge communicator, respectively. | ||
* | ||
* The 'postMessage' method is used to send events to the spec bridge communicator, while | ||
* the 'message' event is used to receive messages from the spec bridge communicator. | ||
* All events communicating across domains are prefixed with 'cross:domain:' under the hood. | ||
* See https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage for more details. | ||
* @extends EventEmitter | ||
*/ | ||
export class PrimaryDomainCommunicator extends EventEmitter { | ||
private windowReference | ||
private crossDomainDriverWindow | ||
|
||
/** | ||
* Initializes the event handler to receive messages from the spec bridge. | ||
* @param {Window} win - a reference to the window object in the primary domain. | ||
* @returns {Void} | ||
*/ | ||
initialize (win) { | ||
if (this.windowReference) return | ||
|
||
this.windowReference = win | ||
|
||
this.windowReference.top.addEventListener('message', ({ data, source }) => { | ||
// currently used for tests, can be removed later | ||
if (data?.actual) return | ||
|
||
// check if message is cross domain and if so, feed the message into | ||
// the cross domain bus with args and strip prefix | ||
if (data?.event?.includes(CROSS_DOMAIN_PREFIX)) { | ||
const messageName = data.event.replace(CROSS_DOMAIN_PREFIX, '') | ||
|
||
// NOTE: need a special case here for 'window:before:load' | ||
// where we need to set the crossDomainDriverWindow to source to | ||
// communicate back to the iframe | ||
if (messageName === 'window:before:load') { | ||
this.crossDomainDriverWindow = source | ||
} else { | ||
this.emit(messageName, data.data) | ||
} | ||
|
||
return | ||
} | ||
|
||
debug('Unexpected postMessage:', data) | ||
}, false) | ||
} | ||
|
||
/** | ||
* Events to be sent to the spec bridge communicator instance. | ||
* @param {string} event - the name of the event to be sent. | ||
* @param {any} data - any meta data to be sent with the event. | ||
*/ | ||
toSpecBridge (event: string, data: any) { | ||
this.crossDomainDriverWindow.postMessage({ | ||
event, | ||
data, | ||
}, '*') | ||
} | ||
} | ||
|
||
/** | ||
* Spec bridge domain communicator. Responsible for sending/receiving events to/from the | ||
* primary domain communicator, respectively. | ||
* | ||
* The 'postMessage' method is used to send events to the primary communicator, while | ||
* the 'message' event is used to receive messages from the primary communicator. | ||
* All events communicating across domains are prefixed with 'cross:domain:' under the hood. | ||
* See https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage for more details. | ||
* @extends EventEmitter | ||
*/ | ||
export class SpecBridgeDomainCommunicator extends EventEmitter { | ||
private windowReference | ||
|
||
/** | ||
* Initializes the event handler to receive messages from the primary domain. | ||
* @param {Window} win - a reference to the window object in the spec bridge/iframe. | ||
* @returns {Void} | ||
*/ | ||
initialize (win) { | ||
if (this.windowReference) return | ||
|
||
this.windowReference = win | ||
|
||
this.windowReference.addEventListener('message', ({ data }) => { | ||
if (!data) return | ||
|
||
this.emit(data.event, data.data) | ||
}, false) | ||
} | ||
|
||
/** | ||
* Events to be sent to the primary communicator instance. | ||
* @param {string} event - the name of the event to be sent. | ||
* @param {any} data - any meta data to be sent with the event. | ||
*/ | ||
toPrimary (event: string, data: any) { | ||
let prefixedEvent = `${CROSS_DOMAIN_PREFIX}${event}` | ||
|
||
this.windowReference.top.postMessage({ event: prefixedEvent, data }, '*') | ||
} | ||
} |
Oops, something went wrong.