UNPKG

@libp2p/websockets

Version:

JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport spec

30 lines 849 B
/** * Adds properties/methods to a `WebSocket` instance from the `ws` module to be * compatible with the `globalThis.WebSocket` API */ export function toWebSocket(ws) { Object.defineProperty(ws, 'url', { value: '', writable: false }); // @ts-expect-error not a WS/WebSocket method ws.dispatchEvent = (evt) => { if (evt.type === 'close') { ws.emit('close'); } if (evt.type === 'open') { ws.emit('open'); } if (evt.type === 'message') { const m = evt; ws.emit('data', m.data); } if (evt.type === 'error') { ws.emit('error', new Error('An error occurred')); } ws.emit(evt.type, evt); }; // @ts-expect-error ws is now WebSocket return ws; } //# sourceMappingURL=utils.js.map