-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote-engine-worker.js
65 lines (56 loc) · 1.62 KB
/
remote-engine-worker.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
import {zbencode, zbdecode} from 'zjs';
//
// XXX make this point to a remote server in production
const rendererHost = `https://local-renderer.webaverse.com`;
//
export const makeRemoteWorker = startUrl => {
return new RemoteEngineWorker(startUrl);
};
//
class RemoteEngineWorker {
constructor(startUrl) {
this.startUrl = startUrl;
}
async waitForLoad() {
// nothing
}
async request(funcName, args = [], {
signal = null,
} = {}) {
// console.log('remote engine request', {
// funcName,
// args,
// });
const u = new URL(`${rendererHost}/api`);
u.searchParams.set('start_url', this.startUrl);
u.searchParams.set('funcName', funcName);
// u.searchParams.set('args', JSON.stringify(args));
// cache ?? u.searchParams.set('cache', cache);
const body = zbencode(args);
const res = await fetch(u, {
method: 'POST',
body,
});
// console.log('got res', res);
if (res.ok) {
const arrayBuffer = await res.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
// console.log('got uint8Array', uint8Array);
try {
const result = zbdecode(uint8Array);
// console.log('got result', result);
return result;
} catch(err) {
// console.warn(err);
const textDecoder = new TextDecoder();
const text = textDecoder.decode(uint8Array);
console.warn('offscreen engine error', err, text);
}
} else {
throw new Error('failed to fetch: ' + res.status);
}
}
destroy() {
// nothing
}
}