-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFrameBridge.js
166 lines (136 loc) · 4.26 KB
/
IFrameBridge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const EventEmitter = require('eventemitter3');
const inherits = require('inherits');
const BridgePayload = require('./BridgePayload');
const CONSTS = require('./consts');
const {enqueue, flushQueue} = require('./util');
const {HandshakeTimeoutError} = require('./errors');
const DEFAULT_OPTIONS = {
/**
* The window to communicate with (inside an iFrame). If given, the bridge will work in server mode and initiate
* the connection. Otherwise, it will listen for incoming handshake and assign source of that message as targetWindow.
*/
targetWindow: null,
/**
* Origin to use when connecting to other window (only in server mode)
*/
origin: '*',
/**
* How often do we attempt to reach the iFrame (only in server mode)
*/
handshake_interval: 200,
/**
* How long do we attempt to reach the other iFrame before we error out
*/
handshake_timeout: 5000,
/**
* Max number of messages to queue
*/
queue_limit: 100,
};
/**
* Very basic wrapper around window.postMessage. Handles handshaking and JSON conversion.
* Makes sure you don't lose any messages.
* @param options Overrides for IFrameBridge.DEFAULT_OPTIONS
* @extends EventEmitter
*/
function IFrameBridge(options = DEFAULT_OPTIONS) {
options = Object.assign({}, IFrameBridge.DEFAULT_OPTIONS, options);
const _serverMode = !!options.targetWindow;
let _targetWindow = options.targetWindow;
const _listenWindow = options.listenWindow || window;
let _initPromise = null;
let _handshakeResponseHandler = null;
let _connected = false;
const _handshakeQueue = [];
const _eventEmitter = new EventEmitter();
_listenWindow.addEventListener('message', receiveMessage, false);
Object.assign(this, /** @lends IFrameBridge.prototype */ {
init,
postMessage,
on: _eventEmitter.on.bind(_eventEmitter),
addListener: _eventEmitter.addListener.bind(_eventEmitter),
once: _eventEmitter.once.bind(_eventEmitter),
off: _eventEmitter.off.bind(_eventEmitter),
removeListener: _eventEmitter.removeListener.bind(_eventEmitter),
removeAllListeners: _eventEmitter.removeAllListeners.bind(_eventEmitter),
});
function init() {
if (_connected) {
return Promise.resolve();
}
if (_initPromise) {
return _initPromise;
}
_initPromise = new Promise((resolve, reject) => {
let handshakeInterval;
let handshakeTimeout;
_handshakeResponseHandler = (messageEvent) => {
_connected = true;
_initPromise = null;
_handshakeResponseHandler = null;
clearInterval(handshakeInterval);
clearTimeout(handshakeTimeout);
resolve();
if (!_serverMode) {
_targetWindow = messageEvent.source;
doPostMessage(BridgePayload.createHandshake());
}
flushQueue(_handshakeQueue, doPostMessage);
};
if (_serverMode) {
handshakeInterval = setInterval(sendHandshakeMessage, options.handshake_interval)
sendHandshakeMessage();
}
if (options.handshake_timeout) {
handshakeTimeout = setTimeout(() => {
clearInterval(handshakeInterval);
_initPromise = null;
_handshakeResponseHandler = null;
reject(new HandshakeTimeoutError(options.handshake_timeout));
}, options.handshake_timeout);
}
function sendHandshakeMessage() {
doPostMessage(BridgePayload.createHandshake());
}
});
return _initPromise;
}
function postMessage(name, data) {
const payload = BridgePayload.createAndValidate(name, data);
if (!_connected) {
enqueue(_handshakeQueue, payload, options.queue_limit);
return;
}
return doPostMessage(payload);
}
function doPostMessage(payload) {
const data = JSON.stringify(payload);
_targetWindow.postMessage(data, options.origin);
}
function receiveMessage(messageEvent) {
/** @type BridgePayload */
let payload;
try {
payload = new BridgePayload(JSON.parse(messageEvent.data));
}
catch (_) {
return;
}
if (!payload.__bridge_payload__) {
return;
}
if (payload.name === CONSTS.HANDSHAKE_MESSAGE_NAME) {
if (_handshakeResponseHandler) {
_handshakeResponseHandler(messageEvent);
}
return;
}
if (payload.name) {
_eventEmitter.emit(payload.name, payload.data);
}
_eventEmitter.emit(CONSTS.MESSAGE_EVENT_NAME, payload);
}
}
inherits(IFrameBridge, EventEmitter);
IFrameBridge.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
module.exports = IFrameBridge;