@chainsafe/libp2p-yamux

中文文档
8.0.1发布于 2025/10/15Apache-2.0 OR MITTypeScriptES Module构建来源已验证近 30 天下载 23.5万

Yamux stream multiplexer for libp2p

原始使用文档
文档目录3 节
  1. Example - Configure libp2p with Yamux
  2. Example - Using the low-level API
  3. Browser <script> tag

@chainsafe/libp2p-yamux

codecov CI

Yamux stream multiplexer for libp2p

About

This module is a JavaScript implementation of Yamux from Hashicorp designed to be used with js-libp2p.

Example - Configure libp2p with Yamux

import { createLibp2p } from 'libp2p'
import { yamux } from '@chainsafe/libp2p-yamux'

const node = await createLibp2p({
  // ... other options
  streamMuxers: [
    yamux()
  ]
})

Example - Using the low-level API

import { yamux } from '@chainsafe/libp2p-yamux'
import { pipe } from 'it-pipe'
import { duplexPair } from 'it-pair/duplex'
import all from 'it-all'

// Connect two yamux muxers to demo basic stream multiplexing functionality

const clientMuxer = yamux({
  client: true,
  onIncomingStream: stream => {
    // echo data on incoming streams
    pipe(stream, stream)
  },
  onStreamEnd: stream => {
    // do nothing
  }
})()

const serverMuxer = yamux({
  client: false,
  onIncomingStream: stream => {
    // echo data on incoming streams
    pipe(stream, stream)
  },
  onStreamEnd: stream => {
    // do nothing
  }
})()

// `p` is our "connections", what we use to connect the two sides
// In a real application, a connection is usually to a remote computer
const p = duplexPair()

// connect the muxers together
pipe(p[0], clientMuxer, p[0])
pipe(p[1], serverMuxer, p[1])

// now either side can open streams
const stream0 = clientMuxer.newStream()
const stream1 = serverMuxer.newStream()

// Send some data to the other side
const encoder = new TextEncoder()
const data = [encoder.encode('hello'), encoder.encode('world')]
pipe(data, stream0)

// Receive data back
const result = await pipe(stream0, all)

// close a stream
stream1.close()

// close the muxer
clientMuxer.close()

Install

$ npm i @chainsafe/libp2p-yamux

Browser <script> tag

Loading this module through a script tag will make its exports available as ChainsafeLibp2pYamux in the global namespace.

<script src="https://unpkg.com/@chainsafe/libp2p-yamux/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

版本与发布记录

@chainsafe/libp2p-yamux 共发布 34 个版本,首次发布于 2022/09/08;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.1latest2025/10/151.1 年前
8.0.02025/09/251.1 年前
7.0.42025/06/251.4 年前
7.0.32025/06/251.4 年前
7.0.22025/06/181.4 年前
7.0.2-rc.0rc2024/10/112 年前

关于 @chainsafe/libp2p-yamux 的常见问题

@chainsafe/libp2p-yamux 是什么?

Yamux stream multiplexer for libp2p

如何安装 @chainsafe/libp2p-yamux?

在项目目录运行 npm install @chainsafe/libp2p-yamux,或使用 pnpm add / yarn add / bun add。

@chainsafe/libp2p-yamux 支持 TypeScript 吗?

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

@chainsafe/libp2p-yamux 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@chainsafe/libp2p-yamux 可以商用吗?

@chainsafe/libp2p-yamux 使用 Apache-2.0 OR MIT 许可。可在 Apache-2.0、MIT 中任选其一遵守。选择 Apache-2.0 时,宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@chainsafe/libp2p-yamux 的下载量有多少?

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

@chainsafe/libp2p-yamux 还在维护吗?

最新版本 8.0.1 发布于 2025/10/15(1.1 年前);至今共发布 34 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/09/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。