|
1 | 1 | /* @flow */
|
2 | 2 |
|
3 |
| -import { matchDomain, type CrossDomainWindowType, type DomainMatcher } from 'cross-domain-utils/src'; |
| 3 | +import { matchDomain, getDomain, type CrossDomainWindowType, type DomainMatcher } from 'cross-domain-utils/src'; |
4 | 4 | import { ZalgoPromise } from 'zalgo-promise/src';
|
5 | 5 | import { once, uniqueID } from 'belter/src';
|
6 | 6 | import { serializeType, type CustomSerializedType } from 'universal-serialize/src';
|
@@ -29,20 +29,20 @@ const listenForFunctionCalls = once(() => {
|
29 | 29 | let meth = methods[data.id] || proxyWindowMethods.get(id);
|
30 | 30 |
|
31 | 31 | if (!meth) {
|
32 |
| - throw new Error(`Could not find method with id: ${ data.id }`); |
| 32 | + throw new Error(`Could not find method '${ data.name }' with id: ${ data.id } in ${ getDomain(window) }`); |
33 | 33 | }
|
34 | 34 |
|
35 | 35 | let { proxy, domain, val } = meth;
|
36 | 36 |
|
37 | 37 | if (!matchDomain(domain, origin)) {
|
38 |
| - throw new Error(`Method domain ${ JSON.stringify(meth.domain) } does not match origin ${ origin }`); |
| 38 | + throw new Error(`Method '${ data.name }' domain ${ JSON.stringify(meth.domain) } does not match origin ${ origin } in ${ getDomain(window) }`); |
39 | 39 | }
|
40 | 40 |
|
41 | 41 | if (proxy) {
|
42 | 42 | // $FlowFixMe
|
43 | 43 | return proxy.matchWindow(source).then(match => {
|
44 | 44 | if (!match) {
|
45 |
| - throw new Error(`Proxy window does not match source`); |
| 45 | + throw new Error(`Method call '${ data.name }' failed - proxy window does not match source in ${ getDomain(window) }`); |
46 | 46 | }
|
47 | 47 | return val;
|
48 | 48 | });
|
@@ -87,23 +87,28 @@ export function serializeFunction<T>(destination : CrossDomainWindowType | Proxy
|
87 | 87 | return serializeType(SERIALIZATION_TYPE.CROSS_DOMAIN_FUNCTION, { id, name: val.name || key });
|
88 | 88 | }
|
89 | 89 |
|
90 |
| -export function deserializeFunction<T>(source : CrossDomainWindowType, origin : string, { id, name } : { id : string, name : string }) : (...args : $ReadOnlyArray<mixed>) => ZalgoPromise<T> { |
| 90 | +export function deserializeFunction<T>(source : CrossDomainWindowType | ProxyWindow, origin : string, { id, name } : { id : string, name : string }) : (...args : $ReadOnlyArray<mixed>) => ZalgoPromise<T> { |
| 91 | + |
| 92 | + function innerWrapper<X : mixed>(args : $ReadOnlyArray<mixed>, opts? : Object = {}) : ZalgoPromise<X> { |
| 93 | + return ZalgoPromise.try(() => { |
| 94 | + // $FlowFixMe |
| 95 | + return ProxyWindow.isProxyWindow(source) ? source.awaitWindow() : source; |
| 96 | + }).then(win => { |
| 97 | + return global.send(win, MESSAGE_NAME.METHOD, { id, name, args }, { domain: origin, ...opts }); |
| 98 | + }); |
| 99 | + } |
91 | 100 |
|
92 | 101 | function crossDomainFunctionWrapper<X : mixed>() : ZalgoPromise<X> {
|
93 |
| - let args = Array.prototype.slice.call(arguments); |
94 |
| - return global.send(source, MESSAGE_NAME.METHOD, { id, name, args }, { domain: origin }) |
| 102 | + return innerWrapper(Array.prototype.slice.call(arguments)) |
95 | 103 | .then(({ data }) => data.result);
|
96 | 104 | }
|
97 | 105 |
|
98 | 106 | crossDomainFunctionWrapper.fireAndForget = function crossDomainFireAndForgetFunctionWrapper<X : mixed>() : ZalgoPromise<X> {
|
99 |
| - let args = Array.prototype.slice.call(arguments); |
100 |
| - return global.send(source, MESSAGE_NAME.METHOD, { id, name, args }, { domain: origin, fireAndForget: true }); |
| 107 | + return innerWrapper(Array.prototype.slice.call(arguments), { fireAndForget: true }); |
101 | 108 | };
|
102 | 109 |
|
103 | 110 | crossDomainFunctionWrapper.__name__ = name;
|
104 | 111 | crossDomainFunctionWrapper.__xdomain__ = true;
|
105 |
| - |
106 |
| - crossDomainFunctionWrapper.source = source; |
107 | 112 | crossDomainFunctionWrapper.origin = origin;
|
108 | 113 |
|
109 | 114 | return crossDomainFunctionWrapper;
|
|
0 commit comments