-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: close streams gracefully (#1864)
- Refactors `.close`, `closeRead` and `.closeWrite` methods on the `Stream` interface to be async - The `Connection` interface now has `.close` and `.abort` methods - `.close` on `Stream`s and `Connection`s wait for the internal message queues to empty before closing - `.abort` on `Stream`s and `Connection`s close the underlying stream immediately and discards any unsent data - `.reset` is removed from the `Stream` interface - instead call `.abort(err)` to signal a local error - `.reset` is still present on the `AbstractStream` class - the muxer implementation should call this to signal a remote error - `@chainsafe/libp2p-yamux` now uses the `AbstractStream` class from `@libp2p/interface` the same as `@libp2p/mplex` and `@libp2p/webrtc` - all the logic around the \*checks notes* 17 different ways to close a stream is contained there Follow-up PRs will be necessary to `@chainsafe/libp2p-yamux`, `@chainsafe/libp2p-gossipsub` and `@chainsafe/libp2p-noise` though they will not block the release as their code is temporarily added to this repo to let CI run. Fixes #1793 Fixes #656 BREAKING CHANGE: the `.close`, `closeRead` and `closeWrite` methods on the `Stream` interface are now asynchronous
- Loading branch information
1 parent
e9cafd3
commit b36ec7f
Showing
132 changed files
with
4,337 additions
and
2,208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
console.log("Everything is defined in the test folder") | ||
// Everything is defined in the test folder | ||
|
||
export { } | ||
export { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{ | ||
"name": "@chainsafe/libp2p-noise", | ||
"version": "12.0.1", | ||
"author": "ChainSafe <info@chainsafe.io>", | ||
"license": "Apache-2.0 OR MIT", | ||
"homepage": "https://github.com/ChainSafe/js-libp2p-noise#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ChainSafe/js-libp2p-noise.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/ChainSafe/js-libp2p-noise/issues" | ||
}, | ||
"keywords": [ | ||
"crypto", | ||
"libp2p", | ||
"noise" | ||
], | ||
"engines": { | ||
"node": ">=16.0.0", | ||
"npm": ">=7.0.0" | ||
}, | ||
"type": "module", | ||
"types": "./dist/src/index.d.ts", | ||
"files": [ | ||
"src", | ||
"dist", | ||
"!dist/test", | ||
"!**/*.tsbuildinfo" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": "ipfs", | ||
"parserOptions": { | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"@typescript-eslint/no-unused-vars": "error", | ||
"@typescript-eslint/explicit-function-return-type": "warn", | ||
"@typescript-eslint/strict-boolean-expressions": "off" | ||
}, | ||
"ignorePatterns": [ | ||
"src/proto/payload.js", | ||
"src/proto/payload.d.ts", | ||
"test/fixtures/node-globals.js" | ||
] | ||
}, | ||
"scripts": { | ||
"bench": "node benchmarks/benchmark.js", | ||
"clean": "aegir clean", | ||
"dep-check": "aegir dep-check", | ||
"build": "aegir build", | ||
"lint": "aegir lint", | ||
"lint:fix": "aegir lint --fix", | ||
"test": "aegir test", | ||
"test:node": "aegir test -t node", | ||
"test:browser": "aegir test -t browser -t webworker", | ||
"test:electron-main": "aegir test -t electron-main", | ||
"docs": "aegir docs", | ||
"proto:gen": "protons ./src/proto/payload.proto", | ||
"prepublish": "npm run build" | ||
}, | ||
"dependencies": { | ||
"@libp2p/crypto": "^1.0.11", | ||
"@libp2p/interface": "~0.0.1", | ||
"@libp2p/logger": "^2.1.1", | ||
"@libp2p/peer-id": "^2.0.0", | ||
"@stablelib/chacha20poly1305": "^1.0.1", | ||
"@noble/hashes": "^1.3.0", | ||
"@stablelib/x25519": "^1.0.3", | ||
"it-length-prefixed": "^9.0.1", | ||
"it-length-prefixed-stream": "^1.0.0", | ||
"it-byte-stream": "^1.0.0", | ||
"it-pair": "^2.0.2", | ||
"it-pipe": "^3.0.1", | ||
"it-stream-types": "^2.0.1", | ||
"protons-runtime": "^5.0.0", | ||
"uint8arraylist": "^2.3.2", | ||
"uint8arrays": "^4.0.2" | ||
}, | ||
"devDependencies": { | ||
"@libp2p/interface-compliance-tests": "^3.0.0", | ||
"@libp2p/peer-id-factory": "^2.0.0", | ||
"@types/sinon": "^10.0.14", | ||
"aegir": "^39.0.5", | ||
"iso-random-stream": "^2.0.2", | ||
"protons": "^7.0.0", | ||
"sinon": "^15.0.0" | ||
}, | ||
"browser": { | ||
"./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js", | ||
"util": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type bytes = Uint8Array | ||
export type bytes32 = Uint8Array | ||
export type bytes16 = Uint8Array | ||
|
||
export type uint64 = number |
12 changes: 12 additions & 0 deletions
12
packages/connection-encryption-noise/src/@types/handshake-interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { bytes } from './basic.js' | ||
import type { NoiseSession } from './handshake.js' | ||
import type { NoiseExtensions } from '../proto/payload.js' | ||
import type { PeerId } from '@libp2p/interface/peer-id' | ||
|
||
export interface IHandshake { | ||
session: NoiseSession | ||
remotePeer: PeerId | ||
remoteExtensions: NoiseExtensions | ||
encrypt: (plaintext: bytes, session: NoiseSession) => bytes | ||
decrypt: (ciphertext: bytes, session: NoiseSession, dst?: Uint8Array) => { plaintext: bytes, valid: boolean } | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/connection-encryption-noise/src/@types/handshake.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { bytes, bytes32, uint64 } from './basic.js' | ||
import type { KeyPair } from './libp2p.js' | ||
import type { Nonce } from '../nonce.js' | ||
|
||
export type Hkdf = [bytes, bytes, bytes] | ||
|
||
export interface MessageBuffer { | ||
ne: bytes32 | ||
ns: bytes | ||
ciphertext: bytes | ||
} | ||
|
||
export interface CipherState { | ||
k: bytes32 | ||
// For performance reasons, the nonce is represented as a Nonce object | ||
// The nonce is treated as a uint64, even though the underlying `number` only has 52 safely-available bits. | ||
n: Nonce | ||
} | ||
|
||
export interface SymmetricState { | ||
cs: CipherState | ||
ck: bytes32 // chaining key | ||
h: bytes32 // handshake hash | ||
} | ||
|
||
export interface HandshakeState { | ||
ss: SymmetricState | ||
s: KeyPair | ||
e?: KeyPair | ||
rs: bytes32 | ||
re: bytes32 | ||
psk: bytes32 | ||
} | ||
|
||
export interface NoiseSession { | ||
hs: HandshakeState | ||
h?: bytes32 | ||
cs1?: CipherState | ||
cs2?: CipherState | ||
mc: uint64 | ||
i: boolean | ||
} | ||
|
||
export interface INoisePayload { | ||
identityKey: bytes | ||
identitySig: bytes | ||
data: bytes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { bytes32 } from './basic.js' | ||
import type { NoiseExtensions } from '../proto/payload.js' | ||
import type { ConnectionEncrypter } from '@libp2p/interface/connection-encrypter' | ||
|
||
export interface KeyPair { | ||
publicKey: bytes32 | ||
privateKey: bytes32 | ||
} | ||
|
||
export interface INoiseConnection extends ConnectionEncrypter<NoiseExtensions> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const NOISE_MSG_MAX_LENGTH_BYTES = 65535 | ||
export const NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG = NOISE_MSG_MAX_LENGTH_BYTES - 16 | ||
|
||
export const DUMP_SESSION_KEYS = Boolean(globalThis.process?.env?.DUMP_SESSION_KEYS) |
Oops, something went wrong.