UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 4.61 kB
{"version":3,"file":"closeWebSocket.mjs","sourceRoot":"","sources":["../../src/permitted/closeWebSocket.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAOjE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,8BAA8B;AAG5E,OAAO,EAAE,cAAc,EAAE,gCAAsB;AAG/C,MAAM,SAAS,GAAiD;IAC9D,aAAa,EAAE,IAAI;IACnB,cAAc,EAAE,IAAI;CACrB,CAAC;AAOF,MAAM,8BAA8B,GAAG,MAAM,CAAC;IAC5C,EAAE,EAAE,MAAM,EAAE;CACb,CAAC,CAAC;AAOH;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAI9B;IACF,WAAW,EAAE,CAAC,qBAAqB,CAAC;IACpC,cAAc,EAAE,4BAA4B;IAC5C,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,SAAS,4BAA4B,CACnC,GAA6C,EAC7C,GAAiD,EACjD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,cAAc,EAA6B;IAE5D,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,CAAC;QACjD,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAEvB,IAAI,CAAC;QACH,MAAM,EAAE,EAAE,EAAE,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QAC1C,cAAc,CAAC,EAAE,CAAC,CAAC;QACnB,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;IACpB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,8BAA8B,CAAC,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n JsonRpcRequest,\n CloseWebSocketParams,\n CloseWebSocketResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport { create, object, string, StructError } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<CloseWebSocketMethodHooks> = {\n hasPermission: true,\n closeWebSocket: true,\n};\n\nexport type CloseWebSocketMethodHooks = {\n hasPermission: (permissionName: string) => boolean;\n closeWebSocket: (id: string) => void;\n};\n\nconst CloseWebSocketParametersStruct = object({\n id: string(),\n});\n\nexport type CloseWebSocketParameters = InferMatching<\n typeof CloseWebSocketParametersStruct,\n CloseWebSocketParams\n>;\n\n/**\n * Handler for the `snap_closeWebSocket` method.\n */\nexport const closeWebSocketHandler: PermittedHandlerExport<\n CloseWebSocketMethodHooks,\n CloseWebSocketParams,\n CloseWebSocketResult\n> = {\n methodNames: ['snap_closeWebSocket'],\n implementation: closeWebSocketImplementation,\n hookNames,\n};\n\n/**\n * The `snap_closeWebSocket` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.\n * @param hooks.closeWebSocket - The function to close a WebSocket.\n * @returns Nothing.\n */\nfunction closeWebSocketImplementation(\n req: JsonRpcRequest<CloseWebSocketParameters>,\n res: PendingJsonRpcResponse<CloseWebSocketResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, closeWebSocket }: CloseWebSocketMethodHooks,\n): void {\n if (!hasPermission(SnapEndowments.NetworkAccess)) {\n return end(providerErrors.unauthorized());\n }\n\n const { params } = req;\n\n try {\n const { id } = getValidatedParams(params);\n closeWebSocket(id);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_closeWebSocket method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): CloseWebSocketParameters {\n try {\n return create(params, CloseWebSocketParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}