(event: MessageEvent<JSONRPCRequest>)
| 33 | }; |
| 34 | |
| 35 | async function handleRPCMessage(event: MessageEvent<JSONRPCRequest>) { |
| 36 | if (typeof event.data !== "object" || event.data === null) { |
| 37 | return sendError(null, JSONRPC_ERRORS.INVALID_REQUEST); |
| 38 | } |
| 39 | |
| 40 | const { id = null, method, params } = event.data; |
| 41 | |
| 42 | if (typeof method !== "string") { |
| 43 | return sendError(id, JSONRPC_ERRORS.INVALID_REQUEST); |
| 44 | } |
| 45 | |
| 46 | if (!methods[method]) { |
| 47 | return sendError(id, JSONRPC_ERRORS.METHOD_NOT_FOUND); |
| 48 | } |
| 49 | |
| 50 | try { |
| 51 | const result = await methods[method](params); |
| 52 | if (id !== null) { |
| 53 | sendResponse(id, result); |
| 54 | } |
| 55 | } catch (error) { |
| 56 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 57 | sendError(id, { |
| 58 | code: JSONRPC_ERRORS.INTERNAL_ERROR.code, |
| 59 | message: errorMessage, |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | function sendResponse(id: string | number | null, result: unknown) { |
| 65 | if (window.parent !== window) { |
no test coverage detected