(req)
| 930 | |
| 931 | let port; |
| 932 | async function onMessage(req) { |
| 933 | if (req.buf) req.buf = atob(req.buf); |
| 934 | console.log('req', req); |
| 935 | |
| 936 | let response = { op: req.op, error: unix.EIO }; |
| 937 | let didTimeout = false, timeout = setTimeout(() => { |
| 938 | // timeout is very useful because some operations just hang |
| 939 | // (like trying to take a screenshot, until the tab is focused) |
| 940 | didTimeout = true; console.error('timeout'); |
| 941 | port.postMessage({ id: req.id, op: req.op, error: unix.ETIMEDOUT }); |
| 942 | }, 1000); |
| 943 | |
| 944 | /* console.time(req.op + ':' + req.path);*/ |
| 945 | try { |
| 946 | const [route, vars] = tryMatchRoute(req.path); |
| 947 | response = await route[req.op]({...req, ...vars}); |
| 948 | response.op = req.op; |
| 949 | if (response.buf) { |
| 950 | if (response.buf instanceof Uint8Array) { |
| 951 | response.buf = await utf8ArrayToBase64(response.buf); |
| 952 | } else { |
| 953 | response.buf = btoa(response.buf); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | } catch (e) { |
| 958 | console.error(e); |
| 959 | response = { |
| 960 | op: req.op, |
| 961 | error: e instanceof UnixError ? e.error : unix.EIO |
| 962 | }; |
| 963 | } |
| 964 | /* console.timeEnd(req.op + ':' + req.path);*/ |
| 965 | |
| 966 | if (!didTimeout) { |
| 967 | clearTimeout(timeout); |
| 968 | |
| 969 | console.log('resp', response); |
| 970 | response.id = req.id; |
| 971 | port.postMessage(response); |
| 972 | } |
| 973 | }; |
| 974 | |
| 975 | function tryConnect() { |
| 976 | // Safari is very weird -- it has this native app that we have to talk to, |
no test coverage detected