lib-http.js
1 // This library file contains the interface to the http service. 2 // You don't need to edit it. 3 // 4 // This functionality will later be merged into the agoric-sdk. 5 6 // @ts-check 7 /* global harden */ 8 import { E } from '@endo/eventual-send'; 9 import { Far } from '@endo/marshal'; 10 11 export const makeWebSocketHandler = (http, makeConnectionHandler) => { 12 return Far('commandHandler', { 13 // This creates the commandHandler for the http service to handle inbound 14 // websocket requests. 15 getCommandHandler() { 16 const channelToConnHandler = new WeakMap(); 17 const handler = Far('handler', { 18 // Executed upon an error in handling the websocket. 19 onError(obj, { channelHandle }) { 20 const connHandler = channelToConnHandler.get(channelHandle); 21 console.error('Have error', obj); 22 if (connHandler.onError) { 23 connHandler.onError(obj); 24 } 25 }, 26 27 // These hooks are run when the websocket is opened or closed. 28 onOpen(obj, meta) { 29 const { channelHandle } = meta; 30 const send = (objToSend) => E(http).send(objToSend, [channelHandle]); 31 const connHandler = makeConnectionHandler(send, meta); 32 channelToConnHandler.set(channelHandle, connHandler); 33 if (connHandler.onOpen) { 34 connHandler.onOpen(obj); 35 } 36 }, 37 onClose(obj, { channelHandle }) { 38 const connHandler = channelToConnHandler.get(channelHandle); 39 channelToConnHandler.delete(channelHandle); 40 if (connHandler.onClose) { 41 connHandler.onClose(obj); 42 } 43 }, 44 onMessage(obj, { channelHandle }) { 45 const connHandler = channelToConnHandler.get(channelHandle); 46 return connHandler.onMessage(obj); 47 }, 48 }); 49 return harden(handler); 50 }, 51 }); 52 };