Skip to content

Commit 0f68898

Browse files
authored
feat!: streams as EventTargets (#3218)
- Converts streams from streaming iterables (e.g. `{ source, sink }`) to `EventTarget`s that have a `send` method and emit `message` events. - Consolidates: - Stream reading/writing/closing into `AbstractMessageStream` - MultiaddrConnection reading/writing/closing into `AbstractMultiaddrConnection` - Stream multiplexing into `AbstractStreamMultiplexer` Fixes #3226 BREAKING CHANGE: - Streams are now EventTargets, not streaming iterables - Stream handlers accept `stream, connection`, not `{ stream, connection }`
1 parent f5932c2 commit 0f68898

File tree

488 files changed

+27811
-13736
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

488 files changed

+27811
-13736
lines changed

.github/dictionary.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ additionals
1414
SECG
1515
Certicom
1616
RSAES
17+
dialback
18+
chacha
19+
peerStore
20+
xxhandshake
21+
zerolen
22+
connmanager
1723
reprovide
1824
reprovider
1925
reproviding

interop/node-version.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"onlyDial": true
1010
},
1111
"webrtc",
12-
"webrtc-direct",
13-
"quic-v1"
12+
"webrtc-direct"
1413
],
1514
"secureChannels": ["noise", "tls"],
1615
"muxers": ["yamux", "mplex"]

interop/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
"dep-check": "aegir dep-check"
1212
},
1313
"devDependencies": {
14-
"@chainsafe/libp2p-noise": "^16.1.3",
15-
"@chainsafe/libp2p-quic": "^1.1.1",
16-
"@chainsafe/libp2p-yamux": "^7.0.1",
14+
"@libp2p/noise": "^16.1.3",
15+
"@libp2p/yamux": "^7.0.1",
1716
"@libp2p/circuit-relay-v2": "^3.2.14",
1817
"@libp2p/identify": "^3.0.32",
1918
"@libp2p/interface": "^2.10.2",

interop/test/dialer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { multiaddr } from '@multiformats/multiaddr'
55
import { getLibp2p } from './fixtures/get-libp2p.js'
66
import { redisProxy } from './fixtures/redis-proxy.js'
77
import type { Libp2p } from '@libp2p/interface'
8-
import type { PingService } from '@libp2p/ping'
8+
import type { Ping } from '@libp2p/ping'
99

1010
const isDialer: boolean = process.env.is_dialer === 'true'
1111
const timeoutMs: number = parseInt(process.env.test_timeout_secs ?? '180') * 1000
@@ -17,7 +17,7 @@ describe('ping test (dialer)', function () {
1717

1818
// make the default timeout longer than the listener timeout
1919
this.timeout(timeoutMs + 30_000)
20-
let node: Libp2p<{ ping: PingService }>
20+
let node: Libp2p<{ ping: Ping }>
2121

2222
beforeEach(async () => {
2323
node = await getLibp2p()

interop/test/fixtures/get-libp2p.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/* eslint-disable complexity */
22

3-
import { noise } from '@chainsafe/libp2p-noise'
4-
import { quic } from '@chainsafe/libp2p-quic'
5-
import { yamux } from '@chainsafe/libp2p-yamux'
3+
// import { quic } from '@chainsafe/libp2p-quic'
64
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
75
import { identify } from '@libp2p/identify'
86
import { mplex } from '@libp2p/mplex'
7+
import { noise } from '@libp2p/noise'
98
import { ping } from '@libp2p/ping'
109
import { tcp } from '@libp2p/tcp'
1110
import { tls } from '@libp2p/tls'
1211
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
1312
import { webSockets } from '@libp2p/websockets'
1413
import { webTransport } from '@libp2p/webtransport'
14+
import { yamux } from '@libp2p/yamux'
1515
import { createLibp2p } from 'libp2p'
1616
import type { Identify } from '@libp2p/identify'
1717
import type { Libp2p } from '@libp2p/interface'
@@ -83,12 +83,13 @@ export async function getLibp2p (): Promise<Libp2p<{ ping: PingService }>> {
8383
listen: isDialer ? [] : [`/ip4/${IP}/tcp/0/wss`]
8484
}
8585
break
86-
case 'quic-v1':
87-
options.transports = [quic()]
88-
options.addresses = {
89-
listen: isDialer ? [] : [`/ip4/${IP}/udp/0/quic-v1`]
90-
}
91-
break
86+
// TODO: re-enable quic after v3 release
87+
// case 'quic-v1':
88+
// options.transports = [quic()]
89+
// options.addresses = {
90+
// listen: isDialer ? [] : [`/ip4/${IP}/udp/0/quic-v1`]
91+
// }
92+
// break
9293
default:
9394
throw new Error(`Unknown transport: ${TRANSPORT ?? '???'}`)
9495
}
@@ -98,7 +99,8 @@ export async function getLibp2p (): Promise<Libp2p<{ ping: PingService }>> {
9899
switch (TRANSPORT) {
99100
case 'webtransport':
100101
case 'webrtc-direct':
101-
case 'quic-v1':
102+
// TODO: re-enable quic after v3 release
103+
// case 'quic-v1':
102104
skipSecureChannel = true
103105
skipMuxer = true
104106
break
@@ -122,7 +124,7 @@ export async function getLibp2p (): Promise<Libp2p<{ ping: PingService }>> {
122124
options.connectionEncrypters = [tls()]
123125
break
124126
default:
125-
throw new Error(`Unknown secure channel: ${SECURE_CHANNEL ?? ''}`)
127+
throw new Error(`Unknown secure channel: ${SECURE_CHANNEL ?? ''} for transport ${TRANSPORT ?? ''}`)
126128
}
127129
}
128130

interop/test/fixtures/relay.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { noise } from '@chainsafe/libp2p-noise'
2-
import { yamux } from '@chainsafe/libp2p-yamux'
31
import { circuitRelayServer } from '@libp2p/circuit-relay-v2'
42
import { identify } from '@libp2p/identify'
3+
import { noise } from '@libp2p/noise'
54
import { webSockets } from '@libp2p/websockets'
5+
import { yamux } from '@libp2p/yamux'
66
import { createLibp2p } from 'libp2p'
77
import type { Libp2p } from '@libp2p/interface'
88

interop/test/listener.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { multiaddr } from '@multiformats/multiaddr'
55
import { getLibp2p } from './fixtures/get-libp2p.js'
66
import { redisProxy } from './fixtures/redis-proxy.js'
77
import type { Libp2p } from '@libp2p/interface'
8-
import type { PingService } from '@libp2p/ping'
8+
import type { Ping } from '@libp2p/ping'
99
import type { Multiaddr } from '@multiformats/multiaddr'
1010

1111
const isDialer: boolean = process.env.is_dialer === 'true'
@@ -18,7 +18,7 @@ describe('ping test (listener)', function () {
1818

1919
// make the default timeout longer than the listener timeout
2020
this.timeout(timeoutMs + 30_000)
21-
let node: Libp2p<{ ping: PingService }>
21+
let node: Libp2p<{ ping: Ping }>
2222

2323
beforeEach(async () => {
2424
node = await getLibp2p()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
/** @type {import('aegir').PartialOptions} */
3+
export default {
4+
docs: {
5+
entryPoint: "src/index.ts"
6+
}
7+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# @chainsafe/libp2p-noise
2+
3+
![npm](https://img.shields.io/npm/v/@chainsafe/libp2p-noise)
4+
[![](https://img.shields.io/github/actions/workflow/status/ChainSafe/js-libp2p-noise/js-test-and-release.yml?branch=master)](https://github.com/ChainSafe/js-libp2p-noise/actions)
5+
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](https://libp2p.io/)
6+
![](https://img.shields.io/github/issues-raw/ChainSafe/js-libp2p-noise)
7+
[![License Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
8+
[![License MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9+
![](https://img.shields.io/badge/npm-%3E%3D7.0.0-orange.svg?style=flat-square)
10+
![](https://img.shields.io/badge/Node.js-%3E%3D16.0.0-orange.svg?style=flat-square)
11+
![](https://img.shields.io/badge/browsers-last%202%20versions%2C%20not%20ie%20%3C%3D11-orange)
12+
[![Twitter](https://img.shields.io/twitter/follow/ChainSafeth.svg?label=Twitter)](https://twitter.com/ChainSafeth)
13+
[![Discord](https://img.shields.io/discord/593655374469660673.svg?label=Discord\&logo=discord)](https://discord.gg/Q6A3YA2)
14+
15+
> Noise libp2p handshake for js-libp2p
16+
17+
# About
18+
19+
<!--
20+
21+
!IMPORTANT!
22+
23+
Everything in this README between "# About" and "# Install" is automatically
24+
generated and will be overwritten the next time the doc generator is run.
25+
26+
To make changes to this section, please update the @packageDocumentation section
27+
of src/index.js or src/index.ts
28+
29+
To experiment with formatting, please run "npm run docs" from the root of this
30+
repo and examine the changes made.
31+
32+
-->
33+
34+
This repository contains TypeScript implementation of noise protocol, an encryption protocol used in libp2p.
35+
36+
## Usage
37+
38+
Install with `yarn add @chainsafe/libp2p-noise` or `npm i @chainsafe/libp2p-noise`.
39+
40+
Example of using default noise configuration and passing it to the libp2p config:
41+
42+
```ts
43+
import {createLibp2p} from "libp2p"
44+
import {noise} from "@libp2p/noise"
45+
46+
//custom noise configuration, pass it instead of `noise()`
47+
//x25519 private key
48+
const n = noise({ staticNoiseKey });
49+
50+
const libp2p = await createLibp2p({
51+
connectionEncrypters: [noise()],
52+
//... other options
53+
})
54+
```
55+
56+
See the [NoiseInit](https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/noise.ts#L22-L30) interface for noise configuration options.
57+
58+
## API
59+
60+
This module exposes an implementation of the [ConnectionEncrypter](https://libp2p.github.io/js-libp2p/interfaces/_libp2p_interface.ConnectionEncrypter.html) interface.
61+
62+
## Bring your own crypto
63+
64+
You can provide a custom crypto implementation (instead of the default, based on [@noble](https://paulmillr.com/noble/)) by adding a `crypto` field to the init argument passed to the `Noise` factory.
65+
66+
The implementation must conform to the `ICryptoInterface`, defined in <https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/crypto.ts>
67+
68+
# Install
69+
70+
```console
71+
$ npm i @chainsafe/libp2p-noise
72+
```
73+
74+
## Browser `<script>` tag
75+
76+
Loading this module through a script tag will make its exports available as `ChainsafeLibp2pNoise` in the global namespace.
77+
78+
```html
79+
<script src="https://unpkg.com/@chainsafe/libp2p-noise/dist/index.min.js"></script>
80+
```
81+
82+
# API Docs
83+
84+
- <https://ChainSafe.github.io/js-libp2p-noise>
85+
86+
# License
87+
88+
Licensed under either of
89+
90+
- Apache 2.0, ([LICENSE-APACHE](https://github.com/ChainSafe/js-libp2p-noise/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
91+
- MIT ([LICENSE-MIT](https://github.com/ChainSafe/js-libp2p-noise/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
92+
93+
# Contribution
94+
95+
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.

0 commit comments

Comments
 (0)