@@ -9,30 +9,46 @@ import { serializeType, type CustomSerializedType } from 'universal-serialize/sr
9
9
import { MESSAGE_NAME , WILDCARD , SERIALIZATION_TYPE } from '../conf' ;
10
10
import { global } from '../global' ;
11
11
12
+ import { ProxyWindow } from './window' ;
13
+
12
14
global . methods = global . methods || new WeakMap ( ) ;
15
+ global . proxyWindowMethods = global . proxyWindowMethods || { } ;
13
16
14
17
const listenForFunctionCalls = once ( ( ) => {
15
18
global . on ( MESSAGE_NAME . METHOD , { origin : WILDCARD } , ( { source, origin, data } : { source : CrossDomainWindowType , origin : string , data : Object } ) => {
16
- let methods = global . methods . get ( source ) ;
19
+ let { id, name } = data ;
20
+
21
+ return ZalgoPromise . try ( ( ) => {
22
+ let methods = global . methods . get ( source ) || { } ;
23
+ let meth = methods [ data . id ] || global . proxyWindowMethods [ id ] ;
17
24
18
- if ( ! methods ) {
19
- throw new Error ( `Could not find any methods this window has privileges to call ` ) ;
20
- }
25
+ if ( ! meth ) {
26
+ throw new Error ( `Could not find method with id: ${ data . id } ` ) ;
27
+ }
21
28
22
- let meth = methods [ data . id ] ;
29
+ let { proxy , domain , val } = meth ;
23
30
24
- if ( ! meth ) {
25
- throw new Error ( `Could not find method with id: ${ data . id } ` ) ;
26
- }
31
+ if ( ! matchDomain ( domain , origin ) ) {
32
+ throw new Error ( `Method domain ${ meth . domain } does not match origin ${ origin } ` ) ;
33
+ }
34
+
35
+ if ( proxy ) {
36
+ return proxy . matchWindow ( source ) . then ( match => {
37
+ if ( ! match ) {
38
+ throw new Error ( `Proxy window does not match source` ) ;
39
+ }
27
40
28
- if ( ! matchDomain ( meth . domain , origin ) ) {
29
- throw new Error ( `Method domain ${ meth . domain } does not match origin ${ origin } ` ) ;
30
- }
41
+ delete global . proxyWindowMethods [ id ] ;
42
+ return val ;
43
+ } ) ;
44
+ }
45
+
46
+ return val ;
47
+
48
+ } ) . then ( method => {
49
+ return method . apply ( { source, origin, data } , data . args ) ;
31
50
32
- return ZalgoPromise . try ( ( ) => {
33
- return meth . val . apply ( { source, origin, data } , data . args ) ;
34
51
} ) . then ( result => {
35
- const { id, name } = data ;
36
52
return { result, id, name } ;
37
53
} ) ;
38
54
} ) ;
@@ -43,20 +59,19 @@ export type SerializedFunction = CustomSerializedType<typeof SERIALIZATION_TYPE.
43
59
name : string
44
60
} > ;
45
61
46
- export function serializeFunction < T > ( destination : CrossDomainWindowType , domain : string | Array < string > , val : ( ) => ZalgoPromise < T > | T , key : string ) : SerializedFunction {
62
+ export function serializeFunction < T > ( destination : CrossDomainWindowType | ProxyWindow , domain : string | Array < string > , val : ( ) => ZalgoPromise < T > | T , key : string ) : SerializedFunction {
63
+ listenForFunctionCalls ( ) ;
64
+
47
65
let id = uniqueID ( ) ;
66
+ destination = ProxyWindow . unwrap ( destination ) ;
48
67
49
- let methods = global . methods . get ( destination ) ;
50
-
51
- if ( ! methods ) {
52
- methods = { } ;
53
- global . methods . set ( destination , methods ) ;
68
+ if ( ProxyWindow . isProxyWindow ( destination ) ) {
69
+ global . proxyWindowMethods [ id ] = { proxy : destination , domain , val } ;
70
+ } else {
71
+ let methods = global . methods . getOrSet ( destination , ( ) => ( { } ) ) ;
72
+ methods [ id ] = { domain , val } ;
54
73
}
55
74
56
- methods [ id ] = { domain, val } ;
57
-
58
- listenForFunctionCalls ( ) ;
59
-
60
75
return serializeType ( SERIALIZATION_TYPE . CROSS_DOMAIN_FUNCTION , { id, name : val . name || key } ) ;
61
76
}
62
77
0 commit comments