Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devDeps: bump @metamask/eslint-config* from v9 to ^12.1.0 #90

Merged
merged 14 commits into from
Sep 20, 2023
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ module.exports = {
],

ignorePatterns: ['!.eslintrc.js', '!.prettierrc.js', 'dist/'],

rules: {
'id-denylist': 'off',
},
};
4 changes: 3 additions & 1 deletion .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
node-version: [14.x, 16.x]
node-version: [14.x, 16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -33,6 +33,8 @@ jobs:
- run: yarn allow-scripts
- run: yarn build
- run: yarn lint
# def remove before node.js v140
if: "${{ !startsWith(matrix.node-version, '14') }}"
- run: yarn test
- name: Validate RC changelog
if: ${{ startsWith(github.head_ref, 'release/') }}
Expand Down
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,31 @@
"devDependencies": {
"@lavamoat/allow-scripts": "^2.3.1",
"@metamask/auto-changelog": "^2.5.0",
"@metamask/eslint-config": "^9.0.0",
"@metamask/eslint-config-jest": "^9.0.0",
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@types/jest": "^26.0.13",
"@types/node": "^14.18.53",
"@types/node-fetch": "^2.6.1",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"eslint": "^7.23.0",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^2.7.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^24.3.4",
"eslint-plugin-jsdoc": "^36.1.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.4.2",
"prettier": "^2.6.1",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jsdoc": "43.0.7",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"jest": "^28.1.3",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.2.17",
"rimraf": "^3.0.2",
"ts-jest": "^26.3.0",
"ts-jest": "^28.0.8",
"ts-node": "^10.7.0",
"typescript": "~4.4.0"
"typescript": "~4.8.4"
},
"engines": {
"node": ">=14.0.0"
Expand Down
28 changes: 13 additions & 15 deletions src/create-infura-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import type { PendingJsonRpcResponse } from 'json-rpc-engine';
import { createAsyncMiddleware } from 'json-rpc-engine';
import type { EthereumRpcError } from 'eth-rpc-errors';
import { ethErrors } from 'eth-rpc-errors';
import { createAsyncMiddleware } from 'json-rpc-engine';
import type { PendingJsonRpcResponse } from 'json-rpc-engine';
// eslint-disable-next-line @typescript-eslint/no-shadow
import fetch from 'node-fetch';

import { fetchConfigFromReq } from './fetch-config-from-req';
import { projectLogger, createModuleLogger } from './logging-utils';
import type {
ExtendedJsonRpcRequest,
InfuraJsonRpcSupportedNetwork,
RequestHeaders,
} from './types';
import { fetchConfigFromReq } from './fetch-config-from-req';
import { projectLogger, createModuleLogger } from './logging-utils';

export interface CreateInfuraMiddlewareOptions {
export type CreateInfuraMiddlewareOptions = {
network?: InfuraJsonRpcSupportedNetwork;
maxAttempts?: number;
source?: string;
projectId: string;
headers?: Record<string, string>;
}
};

const log = createModuleLogger(projectLogger, 'create-infura-middleware');
const RETRIABLE_ERRORS = [
Expand All @@ -33,7 +35,6 @@ const RETRIABLE_ERRORS = [
/**
* Builds [`json-rpc-engine`](https://github.com/MetaMask/json-rpc-engine)-compatible middleware designed
* for interfacing with Infura's JSON-RPC endpoints.
*
* @param opts - The options.
* @param opts.network - A network that Infura supports; plugs into
* `https://${network}.infura.io` (default: 'mainnet').
Expand All @@ -58,6 +59,7 @@ export function createInfuraMiddleware({
}

if (!headers || typeof headers !== 'object') {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Invalid value for 'headers': "${headers}"`);
}

Expand Down Expand Up @@ -104,7 +106,9 @@ export function createInfuraMiddleware({
res,
err,
);
const errMsg = `InfuraProvider - cannot complete request. All retries exhausted.\nOriginal Error:\n${err.toString()}\n\n`;
const errMsg = `InfuraProvider - cannot complete request. All retries exhausted.\nOriginal Error:\n${
err.toString() as string
}\n\n`;
const retriesExhaustedErr = new Error(errMsg);
throw retriesExhaustedErr;
}
Expand All @@ -127,7 +131,6 @@ export function createInfuraMiddleware({
/**
* Makes a request to Infura, updating the given response object if the response
* has a "successful" status code or throwing an error otherwise.
*
* @param network - A network that Infura supports; plugs into
* `https://${network}.infura.io`.
* @param projectId - The Infura project id.
Expand Down Expand Up @@ -191,7 +194,6 @@ async function performFetch(
/**
* Builds a JSON-RPC 2.0 internal error object describing a rate-limiting
* error.
*
* @returns The error object.
*/
function createRatelimitError(): EthereumRpcError<undefined> {
Expand All @@ -201,7 +203,6 @@ function createRatelimitError(): EthereumRpcError<undefined> {

/**
* Builds a JSON-RPC 2.0 internal error object describing a timeout error.
*
* @returns The error object.
*/
function createTimeoutError(): EthereumRpcError<undefined> {
Expand All @@ -212,7 +213,6 @@ function createTimeoutError(): EthereumRpcError<undefined> {

/**
* Builds a JSON-RPC 2.0 internal error object.
*
* @param msg - The message.
* @returns The error object.
*/
Expand All @@ -225,7 +225,6 @@ function createInternalError(msg: string): EthereumRpcError<undefined> {
* intermittent. In these cases we can attempt the request again with the
* assumption that the error is unlikely to occur again. Here we determine if we
* have received such an error.
*
* @param err - The error object.
* @returns Whether the request that produced the error can be retried.
*/
Expand All @@ -236,11 +235,10 @@ function isRetriableError(err: any): boolean {

/**
* A utility function that promisifies `setTimeout`.
*
* @param length - The number of milliseconds to wait.
* @returns A promise that resolves after the given time has elapsed.
*/
function timeout(length: number): Promise<void> {
async function timeout(length: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, length);
});
Expand Down
10 changes: 4 additions & 6 deletions src/create-provider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { JsonRpcEngine } from 'json-rpc-engine';
import { providerFromEngine } from '@metamask/eth-json-rpc-provider';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import {
createInfuraMiddleware,
CreateInfuraMiddlewareOptions,
} from './create-infura-middleware';
import { JsonRpcEngine } from 'json-rpc-engine';

import type { CreateInfuraMiddlewareOptions } from './create-infura-middleware';
import { createInfuraMiddleware } from './create-infura-middleware';

/**
* Creates a provider (as defined in
* [`eth-json-rpc-provider`](https://github.com/MetaMask/eth-json-rpc-provider)
* which is preloaded with middleware specialized for interfacing with Infura
* JSON-RPC endpoints.
*
* @param opts - Options to {@link createInfuraMiddleware}.
* @returns The provider as returned by `providerFromEngine` (a part of
* [`eth-json-rpc-provider`](https://github.com/MetaMask/eth-json-rpc-provider)).
Expand Down
20 changes: 5 additions & 15 deletions src/fetch-config-from-req.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ describe('fetchConfigFromReq', () => {
body: JSON.parse(fetchParams.body),
};

expect(fetchUrl).toStrictEqual(
'https://mainnet.infura.io/v3/abcdef1234567890',
);
expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890');

expect(decodedFetchParams).toStrictEqual({
method: 'POST',
Expand Down Expand Up @@ -53,9 +51,7 @@ describe('fetchConfigFromReq', () => {
body: JSON.parse(fetchParams.body),
};

expect(fetchUrl).toStrictEqual(
'https://mainnet.infura.io/v3/abcdef1234567890',
);
expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890');

expect(decodedFetchParams).toStrictEqual({
method: 'POST',
Expand Down Expand Up @@ -91,9 +87,7 @@ describe('fetchConfigFromReq', () => {
body: JSON.parse(fetchParams.body),
};

expect(fetchUrl).toStrictEqual(
'https://mainnet.infura.io/v3/abcdef1234567890',
);
expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890');

expect(decodedFetchParams).toStrictEqual({
method: 'POST',
Expand Down Expand Up @@ -130,9 +124,7 @@ describe('fetchConfigFromReq', () => {
body: JSON.parse(fetchParams.body),
};

expect(fetchUrl).toStrictEqual(
'https://mainnet.infura.io/v3/abcdef1234567890',
);
expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890');

expect(decodedFetchParams).toStrictEqual({
method: 'POST',
Expand Down Expand Up @@ -167,9 +159,7 @@ describe('fetchConfigFromReq', () => {
body: JSON.parse(fetchParams.body),
});

expect(fetchUrl).toStrictEqual(
'https://mainnet.infura.io/v3/abcdef1234567890',
);
expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890');

expect(decodedFetchParams.body).toStrictEqual({
id: 1,
Expand Down
8 changes: 4 additions & 4 deletions src/fetch-config-from-req.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { JsonRpcRequest } from 'json-rpc-engine';

import type {
ExtendedJsonRpcRequest,
RequestHeaders,
InfuraJsonRpcSupportedNetwork,
} from './types';

interface FetchConfig {
type FetchConfig = {
fetchUrl: string;
fetchParams: {
method: string;
headers: RequestHeaders;
body: string;
};
}
};

/**
* Determines the arguments to feed into `fetch` in order to make a request to
* Infura.
*
* @param options - The options.
* @param options.network - A network that Infura supports; plugs into
* `https://${network}.infura.io`.
Expand All @@ -44,6 +44,7 @@ export function fetchConfigFromReq({
req: ExtendedJsonRpcRequest<unknown>;
source?: string;
}): FetchConfig {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const requestOrigin = req.origin || 'internal';
const headers = Object.assign({}, extraHeaders, {
Accept: 'application/json',
Expand All @@ -67,7 +68,6 @@ export function fetchConfigFromReq({
/**
* Strips out extra keys from a request object that could be rejected by strict
* nodes like parity.
*
* @param req - The original request object obtained via the middleware stack.
* @returns An object that describes a JSON-RPC request.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { JsonRpcRequest } from 'json-rpc-engine';

export type RequestHeaders = Record<string, string>;

export type ExtendedJsonRpcRequest<T> = JsonRpcRequest<T> & { origin?: string };
export type ExtendedJsonRpcRequest<Params> = JsonRpcRequest<Params> & {
origin?: string;
};

/**
* These are networks:
Expand Down
Loading
Loading