{"version":3,"sources":["../src/rpc.ts","../src/rpc-api.ts","../src/rpc-transport.ts"],"names":["SolanaError","SOLANA_ERROR__RPC__API_PLAN_MISSING_FOR_RPC_METHOD","createRpcMessage"],"mappings":";;;;;;AAyDO,SAAS,UACZ,SAAA,EACgB;AAChB,EAAA,OAAO,UAAU,SAAS,CAAA;AAC9B;AAEA,SAAS,UACL,SAAA,EACgB;AAChB,EAAA,OAAO,IAAI,KAAA,CAAM,SAAA,CAAU,GAAA,EAAK;AAAA,IAC5B,cAAA,GAAiB;AACb,MAAA,OAAO,KAAA;AAAA,IACX,CAAA;AAAA,IACA,cAAA,GAAiB;AACb,MAAA,OAAO,KAAA;AAAA,IACX,CAAA;AAAA,IACA,GAAA,CAAI,MAAA,EAAQ,CAAA,EAAG,QAAA,EAAU;AACrB,MAAA,IAAI,MAAM,MAAA,EAAQ;AACd,QAAA,OAAO,MAAA;AAAA,MACX;AACA,MAAA,OAAO,YAAa,SAAA,EAAsB;AACtC,QAAA,MAAM,UAAA,GAAa,EAAE,QAAA,EAAS;AAC9B,QAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,GAAA,CAAI,MAAA,EAAQ,YAAY,QAAQ,CAAA;AAC3D,QAAA,IAAI,CAAC,UAAA,EAAY;AACb,UAAA,MAAM,IAAIA,mBAAYC,yDAAA,EAAoD;AAAA,YACtE,MAAA,EAAQ,UAAA;AAAA,YACR,MAAA,EAAQ;AAAA,WACX,CAAA;AAAA,QACL;AACA,QAAA,MAAM,OAAA,GAAU,UAAA,CAAW,GAAG,SAAS,CAAA;AACvC,QAAA,OAAO,uBAAA,CAAwB,WAAW,OAAO,CAAA;AAAA,MACrD,CAAA;AAAA,IACJ;AAAA,GACH,CAAA;AACL;AAEA,SAAS,uBAAA,CACL,EAAE,SAAA,EAAU,EACZ,IAAA,EAC4B;AAC5B,EAAA,OAAO;AAAA,IACH,MAAM,KAAK,OAAA,EAA8C;AACrD,MAAA,OAAO,MAAM,KAAK,OAAA,CAAQ,EAAE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,CAAA,EAAG;AAC1E,IAAA,OAAO,KAAA;AAAA,EACX;AACA,EAAA,OACI,SAAA,IAAa,OAAA,IACb,OAAA,CAAQ,OAAA,KAAY,KAAA,IACpB,QAAA,IAAY,OAAA,IACZ,OAAO,OAAA,CAAQ,MAAA,KAAW,QAAA,IAC1B,QAAA,IAAY,OAAA;AAEpB","file":"index.node.cjs","sourcesContent":["import { SOLANA_ERROR__RPC__API_PLAN_MISSING_FOR_RPC_METHOD, SolanaError } from '@solana/errors';\nimport { Callable, Flatten, OverloadImplementations, UnionToIntersection } from '@solana/rpc-spec-types';\n\nimport { RpcApi, RpcPlan } from './rpc-api';\nimport { RpcTransport } from './rpc-transport';\n\nexport type RpcConfig = Readonly<{\n api: RpcApi;\n transport: TRpcTransport;\n}>;\n\n/**\n * An object that exposes all of the functions described by `TRpcMethods`.\n *\n * Calling each method returns a {@link PendingRpcRequest | PendingRpcRequest} where\n * `TResponse` is that method's response type.\n */\nexport type Rpc = {\n [TMethodName in keyof TRpcMethods]: PendingRpcRequestBuilder>;\n};\n\n/**\n * Pending requests are the result of calling a supported method on a {@link Rpc} object. They\n * encapsulate all of the information necessary to make the request without actually making it.\n *\n * Calling the {@link PendingRpcRequest.send | `send(options)`} method on a\n * {@link PendingRpcRequest | PendingRpcRequest} will trigger the request and return a\n * promise for `TResponse`.\n */\nexport type PendingRpcRequest = {\n send(options?: RpcSendOptions): Promise;\n};\n\nexport type RpcSendOptions = Readonly<{\n /**\n * An optional signal that you can supply when triggering a {@link PendingRpcRequest} that you\n * might later need to abort.\n */\n abortSignal?: AbortSignal;\n}>;\n\ntype PendingRpcRequestBuilder = UnionToIntersection<\n Flatten<{\n [P in keyof TMethodImplementations]: PendingRpcRequestReturnTypeMapper;\n }>\n>;\n\ntype PendingRpcRequestReturnTypeMapper =\n // Check that this property of the TRpcMethods interface is, in fact, a function.\n TMethodImplementation extends Callable\n ? (...args: Parameters) => PendingRpcRequest>\n : never;\n\n/**\n * Creates a {@link Rpc} instance given a {@link RpcApi | RpcApi} and a\n * {@link RpcTransport} capable of fulfilling them.\n */\nexport function createRpc(\n rpcConfig: RpcConfig,\n): Rpc {\n return makeProxy(rpcConfig);\n}\n\nfunction makeProxy(\n rpcConfig: RpcConfig,\n): Rpc {\n return new Proxy(rpcConfig.api, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get(target, p, receiver) {\n if (p === 'then') {\n return undefined;\n }\n return function (...rawParams: unknown[]) {\n const methodName = p.toString();\n const getApiPlan = Reflect.get(target, methodName, receiver);\n if (!getApiPlan) {\n throw new SolanaError(SOLANA_ERROR__RPC__API_PLAN_MISSING_FOR_RPC_METHOD, {\n method: methodName,\n params: rawParams,\n });\n }\n const apiPlan = getApiPlan(...rawParams);\n return createPendingRpcRequest(rpcConfig, apiPlan);\n };\n },\n }) as Rpc;\n}\n\nfunction createPendingRpcRequest(\n { transport }: RpcConfig,\n plan: RpcPlan,\n): PendingRpcRequest {\n return {\n async send(options?: RpcSendOptions): Promise {\n return await plan.execute({ signal: options?.abortSignal, transport });\n },\n };\n}\n","import {\n Callable,\n createRpcMessage,\n RpcRequestTransformer,\n RpcResponse,\n RpcResponseTransformer,\n} from '@solana/rpc-spec-types';\n\nimport type { RpcTransport } from './rpc-transport';\n\nexport type RpcApiConfig = Readonly<{\n /**\n * An optional function that transforms the {@link RpcRequest} before it is sent to the JSON RPC\n * server.\n *\n * This is useful when the params supplied by the caller need to be transformed before\n * forwarding the message to the server. Use cases for this include applying defaults,\n * forwarding calls to renamed methods, and serializing complex values.\n */\n requestTransformer?: RpcRequestTransformer;\n /**\n * An optional function that transforms the {@link RpcResponse} before it is returned to the\n * caller.\n *\n * Use cases for this include constructing complex data types from serialized data, and throwing\n * exceptions.\n */\n responseTransformer?: RpcResponseTransformer;\n}>;\n\n/**\n * This type allows an {@link RpcApi} to describe how a particular request should be issued to the\n * JSON RPC server.\n *\n * Given a function that was called on a {@link Rpc}, this object exposes an `execute` function that\n * dictates which request will be sent, how the underlying transport will be used, and how the\n * responses will be transformed.\n *\n * This function accepts a {@link RpcTransport} and an `AbortSignal` and asynchronously returns a\n * {@link RpcResponse}. This gives us the opportunity to:\n *\n * - define the `payload` from the requested method name and parameters before passing it to the\n * transport.\n * - call the underlying transport zero, one or multiple times depending on the use-case (e.g.\n * caching or aggregating multiple responses).\n * - transform the response from the JSON RPC server, in case it does not match the `TResponse`\n * specified by the {@link PendingRpcRequest | PendingRpcRequest} returned from that\n * function.\n */\nexport type RpcPlan = {\n execute: (\n config: Readonly<{\n signal?: AbortSignal;\n transport: RpcTransport;\n }>,\n ) => Promise>;\n};\n\n/**\n * For each of `TRpcMethods`, this object exposes a method with the same name that maps between its\n * input arguments and a {@link RpcPlan | RpcPlan} that implements the execution of a\n * JSON RPC request to fetch `TResponse`.\n */\nexport type RpcApi = {\n [MethodName in keyof TRpcMethods]: RpcReturnTypeMapper;\n};\n\ntype RpcReturnTypeMapper = TRpcMethod extends Callable\n ? (...rawParams: unknown[]) => RpcPlan>\n : never;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype RpcApiMethod = (...args: any) => any;\ninterface RpcApiMethods {\n [methodName: string]: RpcApiMethod;\n}\n\n/**\n * Creates a JavaScript proxy that converts _any_ function call called on it to a {@link RpcPlan} by\n * creating an `execute` function that:\n *\n * - sets the transport payload to a JSON RPC v2 payload object with the requested `methodName` and\n * `params` properties, optionally transformed by {@link RpcApiConfig.requestTransformer}.\n * - transforms the transport's response using the {@link RpcApiConfig.responseTransformer}\n * function, if provided.\n *\n * @example\n * ```ts\n * // For example, given this `RpcApi`:\n * const rpcApi = createJsonRpcApi({\n * requestTransformer: (...rawParams) => rawParams.reverse(),\n * responseTransformer: response => response.result,\n * });\n *\n * // ...the following function call:\n * rpcApi.foo('bar', { baz: 'bat' });\n *\n * // ...will produce a `RpcPlan` that:\n * // - Uses the following payload: { id: 1, jsonrpc: '2.0', method: 'foo', params: [{ baz: 'bat' }, 'bar'] }.\n * // - Returns the \"result\" property of the RPC response.\n * ```\n */\nexport function createJsonRpcApi(config?: RpcApiConfig): RpcApi {\n return new Proxy({} as RpcApi, {\n defineProperty() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n get>(\n ...args: Parameters>['get']>>\n ) {\n const [_, p] = args;\n const methodName = p.toString() as keyof TRpcMethods as string;\n return function (\n ...rawParams: Parameters<\n TRpcMethods[TMethodName] extends CallableFunction ? TRpcMethods[TMethodName] : never\n >\n ): RpcPlan> {\n const rawRequest = Object.freeze({ methodName, params: rawParams });\n const request = config?.requestTransformer ? config?.requestTransformer(rawRequest) : rawRequest;\n return Object.freeze(>>{\n execute: async ({ signal, transport }) => {\n const payload = createRpcMessage(request);\n const response = await transport({ payload, signal });\n if (!config?.responseTransformer) {\n return response;\n }\n return config.responseTransformer(response, request);\n },\n });\n };\n },\n });\n}\n","import { RpcResponse } from '@solana/rpc-spec-types';\n\ntype Config = Readonly<{\n /** A value of arbitrary type to be sent to a RPC server */\n payload: unknown;\n /**\n * An optional `AbortSignal` on which the `'abort'` event will be fired if the request should be\n * cancelled.\n */\n signal?: AbortSignal;\n}>;\n\n/**\n * A function that can act as a transport for a {@link Rpc}. It need only return a promise for a\n * response given the supplied config.\n */\nexport type RpcTransport = {\n (config: Config): Promise>;\n};\n\n/**\n * Returns `true` if the given payload is a JSON RPC v2 payload.\n *\n * This means, the payload is an object such that:\n *\n * - It has a `jsonrpc` property with a value of `'2.0'`.\n * - It has a `method` property that is a string.\n * - It has a `params` property of any type.\n *\n * @example\n * ```ts\n * import { isJsonRpcPayload } from '@solana/rpc-spec';\n *\n * if (isJsonRpcPayload(payload)) {\n * const payloadMethod: string = payload.method;\n * const payloadParams: unknown = payload.params;\n * }\n * ```\n */\nexport function isJsonRpcPayload(payload: unknown): payload is Readonly<{\n jsonrpc: '2.0';\n method: string;\n params: unknown;\n}> {\n if (payload == null || typeof payload !== 'object' || Array.isArray(payload)) {\n return false;\n }\n return (\n 'jsonrpc' in payload &&\n payload.jsonrpc === '2.0' &&\n 'method' in payload &&\n typeof payload.method === 'string' &&\n 'params' in payload\n );\n}\n"]}