-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
243 lines (208 loc) · 5.77 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const MESSAGE = 'message'
let _messageId = 0
function resolveOrigin(url) {
const a = document.createElement('a')
a.href = url
return a.origin || `${a.protocol}//${a.hostname}`
}
function resolveValue(model, property) {
const unwrappedContext = typeof model[property] === 'function'
? model[property]() : model[property];
return Promise.resolve(unwrappedContext);
}
function messageUID() {
return ++_messageId
}
function distrust(e, origin) {
if (e.origin !== origin) return false
if (typeof e.data === 'object') return false
if (!('type' in e.data)) return false
}
/**
* parent frame api
*/
class ParentAPI {
constructor(info) {
this.parent = info.parent
this.child = info.child
this.childOrigin = info.childOrigin
this.frame = info.frame
this.events = {}
this.listener = (e) => {
if (distrust(e, this.childOrigin)) return
if (e.data.type === 'emit') {
const {name, data} = e.data.value
if (name in this.events) {
this.events[name].call(this, data)
}
}
}
this.parent.addEventListener(MESSAGE, this.listener, false)
}
/**
* get iframe model property
* @param {string} property iframe modle field name
* @returns {Promise}
*/
get(property) {
return new Promise((resolve, reject) => {
const uid = messageUID()
const transact = e => {
if (e.data.type === 'reply' && e.data.uid === uid) {
this.parent.removeEventListener(MESSAGE, transact, false);
resolve(e.data.value);
}
}
this.parent.addEventListener(MESSAGE, transact, false)
this.child.postMessage({
type: 'request',
property,
uid
}, this.childOrigin)
})
}
/**
* invoke iframe model function property
* @param {string} property iframe model function name
* @param {Object} data functoin argument
*/
call(property, data) {
this.child.postMessage({
type: 'call',
property,
data
}, this.childOrigin)
}
/**
* add iframe event handle
* @param {string} name iframe emit event name
* @param {function} callback event handler function
*/
on(name, callback) {
this.events[name] = callback
}
/**
* destroy iframe
*/
destroy() {
this.parent.removeEventListener(MESSAGE, this.listener, false)
this.frame.parentNode.removeChild(this.frame)
}
}
/**
* child frame api
*/
class ChildAPI {
constructor(info) {
this.parent = info.parent
this.parentOrigin = info.parentOrigin
this.child = info.child
this.model = info.model
this.child.addEventListener(MESSAGE, e => {
if (distrust(e, this.parentOrigin)) return
const {type, property, uid, data} = e.data
if (type === 'call') {
if (property in this.model && typeof this.model[property] === 'function')
this.model[property].call(this, data)
} else if (type === 'request') {
resolveValue(this.model, property).then(value => {
e.source.postMessage({
type: 'reply',
value,
uid
}, e.origin)
})
}
}, false)
}
/**
* iframe emit event and data to parent
* @param {string} name event name
* @param {Object} data transfer data
*/
emit(name, data) {
this.parent.postMessage({
type: 'emit',
value: {name, data}
}, this.parentOrigin)
}
}
class Client {
/**
* iframe communicate component, defined in child frame
* @param {Object} model iframe model
* @returns {Promise} return {@link ChildAPI} Promise Object
*/
constructor(model) {
this.child = window
this.parent = this.child.parent
this.model = model
return this.sendHandshakeReply()
}
sendHandshakeReply() {
return new Promise((resolve, reject) => {
const shake = (e) => {
if (e.data.type === 'handshake') {
this.child.removeEventListener(MESSAGE, shake, false)
this.parentOrigin = e.origin
e.source.postMessage({
type: 'handshake-reply',
}, e.origin)
Object.assign(this.model, e.data.model)
resolve(new ChildAPI(this))
} else
reject('Handshake reply failed.')
}
this.child.addEventListener(MESSAGE, shake, false)
})
}
}
class Postbox {
/**
* iframe communicate component, defined in parent frame
* @param {Object} options
* @param {element} [options.container=document.body] element to inject iframe into
* @param {string} options.url iframe's url
* @param {Object} [options.model] model send to iframe
* @returns {Promise} return {@link ParentAPI} Promise Object
*/
constructor(options) {
const {container, url, model} = options
this.parent = window
this.frame = document.createElement('iframe');
(container || document.body).appendChild(this.frame)
this.child = this.frame.contentWindow || this.frame.contentDocument.parentWindow
this.model = model || {}
return this.sendHandshake(url)
}
sendHandshake(url) {
const childOrigin = resolveOrigin(url)
return new Promise((resolve, reject) => {
const reply = (e) => {
// receive handshake reply from iframe
if (e.data.type === 'handshake-reply') {
this.parent.removeEventListener(MESSAGE, reply, false)
this.childOrigin = e.origin
resolve(new ParentAPI(this))
} else
reject('Handshake failed')
}
this.parent.addEventListener(MESSAGE, reply, false)
// send handshake to iframe
const loaded = (e) => {
this.child.postMessage({
type: 'handshake',
model: this.model
}, childOrigin)
}
this.frame.onload = loaded
this.frame.src = url
})
}
}
/**
*
* @type {Client}
*/
Postbox.Client = Client
module.exports = Postbox