@metamask/eth-json-rpc-middleware
Advanced tools
Comparing version 15.1.2 to 15.2.0
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine'; | ||
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils'; | ||
import type { AbstractRpcService } from './types'; | ||
/** | ||
* @deprecated Please use {@link JsonRpcRequestWithOrigin} instead. | ||
*/ | ||
export interface PayloadWithOrigin extends JsonRpcRequest { | ||
@@ -16,15 +20,37 @@ origin?: string; | ||
/** | ||
* Create middleware for sending a JSON-RPC request to the given RPC URL. | ||
* Creates middleware for sending a JSON-RPC request through the given RPC | ||
* service. | ||
* | ||
* @param options - Options | ||
* @param options.btoa - Generates a base64-encoded string from a binary string. | ||
* @param options.fetch - The `fetch` function; expected to be equivalent to `window.fetch`. | ||
* @param options.rpcUrl - The URL to send the request to. | ||
* @param options.originHttpHeaderKey - If provider, the origin field for each JSON-RPC request | ||
* will be attached to each outgoing fetch request under this header. | ||
* @param args - The arguments to this function. | ||
* @param args.rpcService - The RPC service to use. | ||
* @param args.options - Options. | ||
* @param args.options.originHttpHeaderKey - If provided, the origin field for | ||
* each JSON-RPC request will be attached to each outgoing fetch request under | ||
* this header. | ||
* @returns The fetch middleware. | ||
*/ | ||
export declare function createFetchMiddleware({ btoa, fetch, rpcUrl, originHttpHeaderKey, }: { | ||
export declare function createFetchMiddleware(args: { | ||
rpcService: AbstractRpcService; | ||
options?: { | ||
originHttpHeaderKey?: string; | ||
}; | ||
}): JsonRpcMiddleware<JsonRpcParams, Json>; | ||
/** | ||
* Creates middleware for sending a JSON-RPC request to the given RPC URL. | ||
* | ||
* @deprecated This overload is deprecated — please pass an `RpcService` | ||
* instance from `@metamask/network-controller` instead. | ||
* @param args - The arguments to this function. | ||
* @param args.btoa - Generates a base64-encoded string from a binary string. | ||
* @param args.fetch - The `fetch` function; expected to be equivalent to | ||
* `window.fetch`. | ||
* @param args.rpcUrl - The URL to send the request to. | ||
* @param args.originHttpHeaderKey - If provided, the origin field for each | ||
* JSON-RPC request will be attached to each outgoing fetch request under this | ||
* header. | ||
* @returns The fetch middleware. | ||
*/ | ||
export declare function createFetchMiddleware(args: { | ||
btoa: (stringToEncode: string) => string; | ||
fetch: typeof global.fetch; | ||
fetch: typeof fetch; | ||
rpcUrl: string; | ||
@@ -36,2 +62,5 @@ originHttpHeaderKey?: string; | ||
* | ||
* @deprecated This function was created to support a now-deprecated signature | ||
* for {@link createFetchMiddleware}. It will be removed in a future major | ||
* version. | ||
* @param options - Options | ||
@@ -38,0 +67,0 @@ * @param options.btoa - Generates a base64-encoded string from a binary string. |
@@ -6,2 +6,3 @@ "use strict"; | ||
const rpc_errors_1 = require("@metamask/rpc-errors"); | ||
const utils_1 = require("@metamask/utils"); | ||
const timeout_1 = require("./utils/timeout"); | ||
@@ -18,21 +19,63 @@ const RETRIABLE_ERRORS = [ | ||
]; | ||
function createFetchMiddleware(args) { | ||
if ('rpcService' in args) { | ||
return createFetchMiddlewareWithRpcService(args); | ||
} | ||
return createFetchMiddlewareWithoutRpcService(args); | ||
} | ||
exports.createFetchMiddleware = createFetchMiddleware; | ||
/** | ||
* Create middleware for sending a JSON-RPC request to the given RPC URL. | ||
* Creates middleware for sending a JSON-RPC request through the given RPC | ||
* service. | ||
* | ||
* @param options - Options | ||
* @param options.btoa - Generates a base64-encoded string from a binary string. | ||
* @param options.fetch - The `fetch` function; expected to be equivalent to `window.fetch`. | ||
* @param options.rpcUrl - The URL to send the request to. | ||
* @param options.originHttpHeaderKey - If provider, the origin field for each JSON-RPC request | ||
* will be attached to each outgoing fetch request under this header. | ||
* @param args - The arguments to this function. | ||
* @param args.rpcService - The RPC service to use. | ||
* @param args.options - Options. | ||
* @param args.options.originHttpHeaderKey - If provided, the origin field for | ||
* each JSON-RPC request will be attached to each outgoing fetch request under | ||
* this header. | ||
* @returns The fetch middleware. | ||
*/ | ||
function createFetchMiddleware({ | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
btoa, | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
fetch, rpcUrl, originHttpHeaderKey, }) { | ||
function createFetchMiddlewareWithRpcService({ rpcService, options = {}, }) { | ||
return (0, json_rpc_engine_1.createAsyncMiddleware)(async (req, res) => { | ||
const headers = 'originHttpHeaderKey' in options && | ||
options.originHttpHeaderKey !== undefined && | ||
req.origin !== undefined | ||
? { [options.originHttpHeaderKey]: req.origin } | ||
: {}; | ||
const jsonRpcResponse = await rpcService.request({ | ||
id: req.id, | ||
jsonrpc: req.jsonrpc, | ||
method: req.method, | ||
params: req.params, | ||
}, { | ||
headers, | ||
}); | ||
if ((0, utils_1.isJsonRpcFailure)(jsonRpcResponse)) { | ||
throw rpc_errors_1.rpcErrors.internal({ | ||
data: jsonRpcResponse.error, | ||
}); | ||
} | ||
// Discard the `id` and `jsonrpc` fields in the response body | ||
// (the JSON-RPC engine will fill those in) | ||
res.result = jsonRpcResponse.result; | ||
}); | ||
} | ||
/** | ||
* Creates middleware for sending a JSON-RPC request to the given RPC URL. | ||
* | ||
* @param args - The arguments to this function. | ||
* @param args.btoa - Generates a base64-encoded string from a binary string. | ||
* @param args.fetch - The `fetch` function; expected to be equivalent to | ||
* `window.fetch`. | ||
* @param args.rpcUrl - The URL to send the request to. | ||
* @param args.originHttpHeaderKey - If provider, the origin field for each | ||
* JSON-RPC request will be attached to each outgoing fetch request under this | ||
* header. | ||
* @returns The fetch middleware. | ||
*/ | ||
function createFetchMiddlewareWithoutRpcService({ btoa: givenBtoa, fetch: givenFetch, rpcUrl, originHttpHeaderKey, }) { | ||
return (0, json_rpc_engine_1.createAsyncMiddleware)(async (req, res, _next) => { | ||
const { fetchUrl, fetchParams } = createFetchConfigFromReq({ | ||
btoa, | ||
btoa: givenBtoa, | ||
req, | ||
@@ -47,3 +90,3 @@ rpcUrl, | ||
try { | ||
const fetchRes = await fetch(fetchUrl, fetchParams); | ||
const fetchRes = await givenFetch(fetchUrl, fetchParams); | ||
// check for http errrors | ||
@@ -78,3 +121,2 @@ checkForHttpErrors(fetchRes); | ||
} | ||
exports.createFetchMiddleware = createFetchMiddleware; | ||
function checkForHttpErrors(fetchRes) { | ||
@@ -114,2 +156,5 @@ // check for errors | ||
* | ||
* @deprecated This function was created to support a now-deprecated signature | ||
* for {@link createFetchMiddleware}. It will be removed in a future major | ||
* version. | ||
* @param options - Options | ||
@@ -116,0 +161,0 @@ * @param options.btoa - Generates a base64-encoded string from a binary string. |
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine'; | ||
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils'; | ||
import type { Json, JsonRpcParams, JsonRpcRequest, JsonRpcResponse } from '@metamask/utils'; | ||
export interface JsonRpcRequestToCache<Params extends JsonRpcParams> extends JsonRpcRequest<Params> { | ||
@@ -11,1 +11,58 @@ skipCache?: boolean; | ||
export declare type Cache = Record<number, BlockCache>; | ||
/** | ||
* The interface for a service class responsible for making a request to an RPC | ||
* endpoint. | ||
*/ | ||
export declare type AbstractRpcService = { | ||
/** | ||
* Listens for when the RPC service retries the request. | ||
* | ||
* @param listener - The callback to be called when the retry occurs. | ||
* @returns A disposable. | ||
*/ | ||
onRetry: (listener: (data: ({ | ||
error: Error; | ||
} | { | ||
value: unknown; | ||
}) & { | ||
delay: number; | ||
attempt: number; | ||
endpointUrl: string; | ||
}) => void) => { | ||
dispose(): void; | ||
}; | ||
/** | ||
* Listens for when the RPC service retries the request too many times in a | ||
* row. | ||
* | ||
* @param listener - The callback to be called when the circuit is broken. | ||
* @returns A disposable. | ||
*/ | ||
onBreak: (listener: (data: ({ | ||
error: Error; | ||
} | { | ||
value: unknown; | ||
} | { | ||
isolated: true; | ||
}) & { | ||
endpointUrl: string; | ||
}) => void) => { | ||
dispose(): void; | ||
}; | ||
/** | ||
* Listens for when the policy underlying this RPC service detects a slow | ||
* request. | ||
* | ||
* @param listener - The callback to be called when the request is slow. | ||
* @returns A disposable. | ||
*/ | ||
onDegraded: (listener: (data: { | ||
endpointUrl: string; | ||
}) => void) => { | ||
dispose(): void; | ||
}; | ||
/** | ||
* Makes a request to the RPC endpoint. | ||
*/ | ||
request<Params extends JsonRpcParams, Result extends Json>(jsonRpcRequest: JsonRpcRequest<Params>, fetchOptions?: RequestInit): Promise<JsonRpcResponse<Result | null>>; | ||
}; |
{ | ||
"name": "@metamask/eth-json-rpc-middleware", | ||
"version": "15.1.2", | ||
"version": "15.2.0", | ||
"description": "Ethereum-related json-rpc-engine middleware.", | ||
@@ -27,3 +27,4 @@ "repository": { | ||
"prepack": "./scripts/prepack.sh", | ||
"test": "jest", | ||
"test": "jest && yarn build:clean && yarn test:types", | ||
"test:types": "tsd --files 'src/**/*.test-d.ts'", | ||
"test:watch": "jest --watch" | ||
@@ -37,3 +38,3 @@ }, | ||
"@metamask/rpc-errors": "^7.0.2", | ||
"@metamask/utils": "^11.0.1", | ||
"@metamask/utils": "^11.1.0", | ||
"@types/bn.js": "^5.1.5", | ||
@@ -53,2 +54,3 @@ "bn.js": "^5.2.1", | ||
"@metamask/eslint-config-typescript": "^12.1.0", | ||
"@metamask/network-controller": "22.2.0", | ||
"@types/btoa": "^1.2.3", | ||
@@ -74,2 +76,3 @@ "@types/jest": "^27.4.1", | ||
"ts-node": "^10.7.0", | ||
"tsd": "^0.31.2", | ||
"typescript": "~4.8.4" | ||
@@ -92,2 +95,2 @@ }, | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
57
1707
1
170179
30
7088
Updated@metamask/utils@^11.1.0