Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit cdfbeb6

Browse files
committed
Allow deserialization using proxy window
1 parent 17844f4 commit cdfbeb6

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

src/serialize/function.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

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';
44
import { ZalgoPromise } from 'zalgo-promise/src';
55
import { once, uniqueID } from 'belter/src';
66
import { serializeType, type CustomSerializedType } from 'universal-serialize/src';
@@ -29,20 +29,20 @@ const listenForFunctionCalls = once(() => {
2929
let meth = methods[data.id] || proxyWindowMethods.get(id);
3030

3131
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) }`);
3333
}
3434

3535
let { proxy, domain, val } = meth;
3636

3737
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) }`);
3939
}
4040

4141
if (proxy) {
4242
// $FlowFixMe
4343
return proxy.matchWindow(source).then(match => {
4444
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) }`);
4646
}
4747
return val;
4848
});
@@ -87,23 +87,28 @@ export function serializeFunction<T>(destination : CrossDomainWindowType | Proxy
8787
return serializeType(SERIALIZATION_TYPE.CROSS_DOMAIN_FUNCTION, { id, name: val.name || key });
8888
}
8989

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+
}
91100

92101
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))
95103
.then(({ data }) => data.result);
96104
}
97105

98106
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 });
101108
};
102109

103110
crossDomainFunctionWrapper.__name__ = name;
104111
crossDomainFunctionWrapper.__xdomain__ = true;
105-
106-
crossDomainFunctionWrapper.source = source;
107112
crossDomainFunctionWrapper.origin = origin;
108113

109114
return crossDomainFunctionWrapper;

src/serialize/promise.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ export function serializePromise(destination : CrossDomainWindowType | ProxyWind
1919
});
2020
}
2121

22-
export function deserializePromise<T>(source : CrossDomainWindowType, origin : string, { then } : { then : Function }) : ZalgoPromise<T> {
22+
export function deserializePromise<T>(source : CrossDomainWindowType | ProxyWindow, origin : string, { then } : { then : Function }) : ZalgoPromise<T> {
2323
return new ZalgoPromise(then);
2424
}

src/serialize/serialize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function serializeMessage<T : mixed>(destination : CrossDomainWindowType
1919
});
2020
}
2121

22-
export function deserializeMessage<T : mixed>(source : CrossDomainWindowType, origin : string, message : string) : T {
22+
export function deserializeMessage<T : mixed>(source : CrossDomainWindowType | ProxyWindow, origin : string, message : string) : T {
2323
return deserialize(message, {
2424
[ SERIALIZATION_TYPE.CROSS_DOMAIN_ZALGO_PROMISE ]: (serializedPromise) => deserializePromise(source, origin, serializedPromise),
2525
[ SERIALIZATION_TYPE.CROSS_DOMAIN_FUNCTION ]: (serializedFunction) => deserializeFunction(source, origin, serializedFunction),

src/serialize/window.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,6 @@ export function serializeWindow(destination : CrossDomainWindowType | ProxyWindo
237237
return serializeType(SERIALIZATION_TYPE.CROSS_DOMAIN_WINDOW, ProxyWindow.serialize(win));
238238
}
239239

240-
export function deserializeWindow(source : CrossDomainWindowType, origin : string, win : SerializedProxyWindow) : ProxyWindow {
240+
export function deserializeWindow(source : CrossDomainWindowType | ProxyWindow, origin : string, win : SerializedProxyWindow) : ProxyWindow {
241241
return ProxyWindow.deserialize(win);
242242
}

0 commit comments

Comments
 (0)