-
Notifications
You must be signed in to change notification settings - Fork 272
/
api.ts
179 lines (167 loc) · 4.48 KB
/
api.ts
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
import { PHPResponse, PHPResponseData } from '@php-wasm/universal';
import * as Comlink from 'comlink';
export type WithAPIState = {
/**
* Resolves to true when the remote API is ready for
* Comlink communication, but not necessarily fully initialized yet.
*/
isConnected: () => Promise<void>;
/**
* Resolves to true when the remote API is declares it's
* fully loaded and ready to be used.
*/
isReady: () => Promise<void>;
};
export type RemoteAPI<T> = Comlink.Remote<T> & WithAPIState;
export function consumeAPI<APIType>(
remote: Worker | Window
): RemoteAPI<APIType> {
setupTransferHandlers();
const endpoint =
remote instanceof Worker ? remote : Comlink.windowEndpoint(remote);
/**
* This shouldn't be necessary, but Comlink doesn't seem to
* handle the initial isConnected() call correctly unless it's
* explicitly provided here. This is especially weird
* since the only thing this proxy does is to call the
* isConnected() method on the remote API.
*
* @TODO: Remove this workaround.
*/
const api = Comlink.wrap<APIType & WithAPIState>(endpoint);
const methods = proxyClone(api);
return new Proxy(methods, {
get: (target, prop) => {
if (prop === 'isConnected') {
return async () => {
/*
* If exposeAPI() is called after this function,
* the isConnected() call will hang forever. Let's
* retry it a few times.
*/
for (let i = 0; i < 10; i++) {
try {
await runWithTimeout(api.isConnected(), 200);
break;
} catch (e) {
// Timeout exceeded, try again
}
}
};
}
return (api as any)[prop];
},
}) as unknown as RemoteAPI<APIType>;
}
async function runWithTimeout<T>(
promise: Promise<T>,
timeout: number
): Promise<T> {
return new Promise<T>((resolve, reject) => {
setTimeout(reject, timeout);
promise.then(resolve);
});
}
export type PublicAPI<Methods, PipedAPI = unknown> = RemoteAPI<
Methods & PipedAPI
>;
export function exposeAPI<Methods, PipedAPI>(
apiMethods?: Methods,
pipedApi?: PipedAPI
): [() => void, PublicAPI<Methods, PipedAPI>] {
setupTransferHandlers();
const connected = Promise.resolve();
let setReady: any;
const ready = new Promise((resolve) => {
setReady = resolve;
});
const methods = proxyClone(apiMethods);
const exposedApi = new Proxy(methods, {
get: (target, prop) => {
if (prop === 'isConnected') {
return () => connected;
} else if (prop === 'isReady') {
return () => ready;
} else if (prop in target) {
return target[prop];
}
return (pipedApi as any)?.[prop];
},
}) as unknown as PublicAPI<Methods, PipedAPI>;
Comlink.expose(
exposedApi,
typeof window !== 'undefined'
? Comlink.windowEndpoint(self.parent)
: undefined
);
return [setReady, exposedApi];
}
let isTransferHandlersSetup = false;
function setupTransferHandlers() {
if (isTransferHandlersSetup) {
return;
}
isTransferHandlersSetup = true;
Comlink.transferHandlers.set('EVENT', {
canHandle: (obj): obj is CustomEvent => obj instanceof CustomEvent,
serialize: (ev: CustomEvent) => {
return [
{
detail: ev.detail,
},
[],
];
},
deserialize: (obj) => obj,
});
Comlink.transferHandlers.set('FUNCTION', {
canHandle: (obj: unknown): obj is Function => typeof obj === 'function',
serialize(obj: Function) {
console.debug('[Comlink][Performance] Proxying a function');
const { port1, port2 } = new MessageChannel();
Comlink.expose(obj, port1);
return [port2, [port2]];
},
deserialize(port: any) {
port.start();
return Comlink.wrap(port);
},
});
Comlink.transferHandlers.set('PHPResponse', {
canHandle: (obj: unknown): obj is PHPResponseData =>
typeof obj === 'object' &&
obj !== null &&
'headers' in obj &&
'bytes' in obj &&
'errors' in obj &&
'exitCode' in obj &&
'httpStatusCode' in obj,
serialize(obj: PHPResponse): [PHPResponseData, Transferable[]] {
return [obj.toRawData(), []];
},
deserialize(responseData: PHPResponseData): PHPResponse {
return PHPResponse.fromRawData(responseData);
},
});
}
function proxyClone(object: any): any {
return new Proxy(object, {
get(target, prop) {
switch (typeof target[prop]) {
case 'function':
return (...args: any[]) => target[prop](...args);
case 'object':
if (target[prop] === null) {
return target[prop];
}
return proxyClone(target[prop]);
case 'undefined':
case 'number':
case 'string':
return target[prop];
default:
return Comlink.proxy(target[prop]);
}
},
});
}