@atomrigslab/providers

中文文档
1.1.0发布于 2024/07/11MITTypeScriptCommonJS近 30 天下载 1.9万

A JavaScript Ethereum provider that connects to the wallet over a stream.

原始使用文档
文档目录7 节
  1. Usage
  2. Types
  3. Do Not Modify the Provider
  4. Contributing
  5. Setup
  6. Testing and Linting
  7. Release & Publishing

MetaMask Providers

The Ethereum provider object injected by MetaMask into various environments. Contains a lot of implementation details specific to MetaMask, and is probably not suitable for out-of-the-box use with other wallets.

The BaseProvider implements the Ethereum JavaScript provider specification (EIP-1193), but must be modified by a sub-class in order to function. StreamProvider is such a sub-class, which synchronizes its state and marshals JSON-RPC messages via a duplex stream. MetamaskInpageProvider further extends StreamProvider to support legacy provider interfaces in addition to EIP-1193, and is used to instantiate the object injected by MetaMask into web pages as window.ethereum.

Usage

import { initializeProvider } from '@metamask/providers';

// Create a stream to a remote provider:
const metamaskStream = new LocalMessageDuplexStream({
  name: 'inpage',
  target: 'contentscript',
});

// this will initialize the provider and set it as window.ethereum
initializeProvider({
  connectionStream: metamaskStream,
});

const { ethereum } = window;

Types

Types are exposed at index.d.ts. They require Node.js EventEmitter and Duplex stream types, which you can grab from e.g. @types/node.

Do Not Modify the Provider

The Provider object should not be mutated by consumers under any circumstances. The maintainers of this package will neither fix nor take responsbility for bugs caused by third parties mutating the provider object.

Contributing

Setup

  • Install Node.js version 12
    • If you are using nvm (recommended) running nvm use will automatically choose the right node version for you.
  • Install Yarn v1
  • Run yarn setup to install dependencies and run any requried post-install scripts
    • Warning: Do not use the yarn / yarn install command directly. Use yarn setup instead. The normal install command will skip required post-install scripts, leaving your development environment in an invalid state.

Testing and Linting

Run yarn test to run the tests once. To run tests on file changes, run yarn test:watch.

Run yarn lint to run the linter, or run yarn lint:fix to run the linter and fix any automatically fixable issues.

Release & Publishing

The project follows the same release process as the other libraries in the MetaMask organization:

  1. Create a release branch
    • For a typical release, this would be based on main
    • To update an older maintained major version, base the release branch on the major version branch (e.g. 1.x)
  2. Update the changelog
  3. Update version in package.json file (e.g. yarn version --minor --no-git-tag-version)
  4. Create a pull request targeting the base branch (e.g. main or 1.x)
  5. Code review and QA
  6. Once approved, the PR is squashed & merged
  7. The commit on the base branch is tagged
  8. The tag can be published as needed

版本与发布记录

@atomrigslab/providers 共发布 6 个版本,首次发布于 2023/02/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2024/07/112 年前
1.0.12023/08/163 年前
1.0.1-beta.3beta2023/08/163 年前
0.0.32023/03/063 年前
0.0.22023/02/273 年前
0.0.12023/02/273 年前

关于 @atomrigslab/providers 的常见问题

@atomrigslab/providers 是什么?

A JavaScript Ethereum provider that connects to the wallet over a stream.

如何安装 @atomrigslab/providers?

在项目目录运行 npm install @atomrigslab/providers,或使用 pnpm add / yarn add / bun add。

@atomrigslab/providers 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@atomrigslab/providers 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@atomrigslab/providers 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@atomrigslab/providers 可以商用吗?

@atomrigslab/providers 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@atomrigslab/providers 的下载量有多少?

根据 npm 官方下载统计,@atomrigslab/providers 在 2026/10/07 – 2026/11/05 这 30 天内被下载 18,958 次,日均约 632 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atomrigslab/providers 还在维护吗?

最新版本 1.1.0 发布于 2024/07/11(2 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。