UNPKG

@metamask/snaps-rpc-methods

Version:
63 lines 1.85 kB
import { providerErrors } from "@metamask/rpc-errors"; import { SnapEndowments } from "../endowments/index.mjs"; /** * Get the connected WebSockets for the Snap. * * @example * ```json name="Manifest" * { * "initialPermissions": { * "endowment:network-access": {} * } * } * ``` * ```ts name="Usage" * const webSockets = await snap.request({ method: 'snap_getWebSockets' }); * console.log(webSockets); * // Example output: * // [ * // { * // id: 'websocket-1', * // url: 'wss://example.com/socket', * // protocols: ['protocol1', 'protocol2'], * // }, * // { * // id: 'websocket-2', * // url: 'ws://example.org/endpoint', * // protocols: [], * // }, * // ] * ``` */ export const getWebSocketsHandler = { implementation: getWebSocketsImplementation, actionNames: [ 'PermissionController:hasPermission', 'WebSocketService:getAll', ], }; /** * The `snap_getWebSockets` method implementation. * * @param req - The JSON-RPC request object. * @param res - The JSON-RPC response object. * @param _next - The `json-rpc-engine` "next" callback. Not used by this function. * @param end - The `json-rpc-engine` "end" callback. * @param _hooks - The RPC method hooks. Not used by this function. * @param messenger - The messenger used to call controller actions. * @returns Nothing. */ function getWebSocketsImplementation(req, res, _next, end, _hooks, messenger) { const { origin } = req; if (!messenger.call('PermissionController:hasPermission', origin, SnapEndowments.NetworkAccess)) { return end(providerErrors.unauthorized()); } try { res.result = messenger.call('WebSocketService:getAll', origin); } catch (error) { return end(error); } return end(); } //# sourceMappingURL=getWebSockets.mjs.map