Skip to content

Commit

Permalink
feat!: close streams gracefully (#213)
Browse files Browse the repository at this point in the history
- Updates all libp2p related deps
- Stream close methods are now async

BREAKING CHANGE: stream close methods are now asyc, requires libp2p@0.46.x or later
  • Loading branch information
achingbrain authored Jul 31, 2023
1 parent 7414a71 commit 92eebfa
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 126 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"release": "aegir run release"
},
"dependencies": {
"aegir": "^39.0.10"
"aegir": "^40.0.2"
},
"workspaces": [
"packages/*"
Expand Down
39 changes: 14 additions & 25 deletions packages/libp2p-daemon-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
"keywords": [
"libp2p"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
Expand Down Expand Up @@ -134,33 +130,26 @@
"release": "aegir release"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^9.0.0",
"@libp2p/daemon-protocol": "^4.0.0",
"@libp2p/interface-connection": "^5.0.1",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/interface-transport": "^4.0.0",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^2.0.0",
"@libp2p/tcp": "^7.0.1",
"@multiformats/multiaddr": "^12.0.0",
"@libp2p/interface": "^0.1.0",
"@libp2p/kad-dht": "^10.0.0",
"@libp2p/logger": "^3.0.0",
"@libp2p/peer-id": "^3.0.0",
"@libp2p/tcp": "^8.0.0",
"@multiformats/multiaddr": "^12.1.3",
"it-stream-types": "^2.0.1",
"multiformats": "^11.0.0",
"uint8arraylist": "^2.3.2"
"multiformats": "^12.0.1",
"uint8arraylist": "^2.4.3"
},
"devDependencies": {
"@libp2p/daemon-server": "^5.0.0",
"@libp2p/interface-dht": "^2.0.0",
"@libp2p/interface-libp2p": "3.0.0",
"@libp2p/interface-mocks": "^11.0.0",
"@libp2p/interface-peer-store": "^2.0.0",
"@libp2p/interface-pubsub": "^4.0.0",
"@types/sinon": "^10.0.14",
"aegir": "^39.0.10",
"@libp2p/interface-compliance-tests": "^4.0.0",
"aegir": "^40.0.1",
"it-all": "^3.0.1",
"it-pipe": "^3.0.0",
"sinon": "^15.0.1",
"it-pipe": "^3.0.1",
"sinon": "^15.1.2",
"sinon-ts": "^1.0.0",
"uint8arrays": "^4.0.2"
"uint8arrays": "^4.0.4"
}
}
6 changes: 3 additions & 3 deletions packages/libp2p-daemon-client/src/dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
DHTRequest,
DHTResponse
} from '@libp2p/daemon-protocol'
import { isPeerId, type PeerId } from '@libp2p/interface-peer-id'
import { CodeError } from '@libp2p/interfaces/errors'
import { CodeError } from '@libp2p/interface/errors'
import { isPeerId, type PeerId } from '@libp2p/interface/peer-id'
import { peerIdFromBytes } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { CID } from 'multiformats/cid'
import type { DaemonClient } from './index.js'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { PeerInfo } from '@libp2p/interface/peer-info'

export class DHT {
private readonly client: DaemonClient
Expand Down
10 changes: 5 additions & 5 deletions packages/libp2p-daemon-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { type PSMessage, Request, Response, StreamInfo } from '@libp2p/daemon-protocol'
import { StreamHandler } from '@libp2p/daemon-protocol/stream-handler'
import { passThroughUpgrader } from '@libp2p/daemon-protocol/upgrader'
import { isPeerId, type PeerId } from '@libp2p/interface-peer-id'
import { CodeError } from '@libp2p/interfaces/errors'
import { CodeError } from '@libp2p/interface/errors'
import { isPeerId, type PeerId } from '@libp2p/interface/peer-id'
import { logger } from '@libp2p/logger'
import { peerIdFromBytes } from '@libp2p/peer-id'
import { tcp } from '@libp2p/tcp'
import { multiaddr, isMultiaddr } from '@multiformats/multiaddr'
import { DHT } from './dht.js'
import { Pubsub } from './pubsub.js'
import type { MultiaddrConnection } from '@libp2p/interface-connection'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { Transport } from '@libp2p/interface-transport'
import type { MultiaddrConnection } from '@libp2p/interface/connection'
import type { PeerInfo } from '@libp2p/interface/peer-info'
import type { Transport } from '@libp2p/interface/transport'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Duplex, Source } from 'it-stream-types'
import type { CID } from 'multiformats/cid'
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p-daemon-client/src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
PSRequest,
PSMessage
} from '@libp2p/daemon-protocol'
import { CodeError } from '@libp2p/interfaces/errors'
import { CodeError } from '@libp2p/interface/errors'
import { peerIdFromBytes } from '@libp2p/peer-id'
import type { DaemonClient, Subscription } from './index.js'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { PeerId } from '@libp2p/interface/peer-id'

export class Pubsub {
private readonly client: DaemonClient
Expand Down
14 changes: 7 additions & 7 deletions packages/libp2p-daemon-client/test/dht.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

import { createServer, type Libp2pServer } from '@libp2p/daemon-server'
import { type DualDHT, type ValueEvent, type FinalPeerEvent, type PeerResponseEvent, MessageType, EventTypes, type DHT } from '@libp2p/interface-dht'
import { type DualKadDHT, type ValueEvent, type FinalPeerEvent, type PeerResponseEvent, MessageType, EventTypes, type KadDHT } from '@libp2p/kad-dht'
import { peerIdFromString } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
Expand All @@ -11,8 +11,8 @@ import sinon from 'sinon'
import { type StubbedInstance, stubInterface } from 'sinon-ts'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { createClient, type DaemonClient } from '../src/index.js'
import type { Libp2p } from '@libp2p/interface-libp2p'
import type { PubSub } from '@libp2p/interface-pubsub'
import type { GossipSub } from '@chainsafe/libp2p-gossipsub'
import type { Libp2p } from '@libp2p/interface'

const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/12345')

Expand All @@ -23,14 +23,14 @@ function match (cid: CID): sinon.SinonMatcher {
describe('daemon dht client', function () {
this.timeout(30e3)

let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let libp2p: StubbedInstance<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>
let server: Libp2pServer
let client: DaemonClient
let dht: StubbedInstance<DualDHT>
let dht: StubbedInstance<DualKadDHT>

beforeEach(async function () {
dht = stubInterface<DualDHT>()
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
dht = stubInterface<DualKadDHT>()
libp2p = stubInterface<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>()
libp2p.services.dht = dht

server = createServer(defaultMultiaddr, libp2p)
Expand Down
16 changes: 8 additions & 8 deletions packages/libp2p-daemon-client/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
/* eslint-env mocha */

import { createServer, type Libp2pServer } from '@libp2p/daemon-server'
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks'
import { isPeerId } from '@libp2p/interface-peer-id'
import { isPeerId } from '@libp2p/interface/peer-id'
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks'
import { peerIdFromString } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import sinon from 'sinon'
import { type StubbedInstance, stubInterface } from 'sinon-ts'
import { createClient, type DaemonClient } from '../src/index.js'
import type { DHT } from '@libp2p/interface-dht'
import type { Libp2p } from '@libp2p/interface-libp2p'
import type { PeerStore } from '@libp2p/interface-peer-store'
import type { PubSub } from '@libp2p/interface-pubsub'
import type { GossipSub } from '@chainsafe/libp2p-gossipsub'
import type { Libp2p } from '@libp2p/interface'
import type { PeerStore } from '@libp2p/interface/peer-store'
import type { KadDHT } from '@libp2p/kad-dht'

const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/0')

describe('daemon client', function () {
this.timeout(30e3)

let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let libp2p: StubbedInstance<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>
let server: Libp2pServer
let client: DaemonClient

beforeEach(async function () {
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
libp2p = stubInterface<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>()
libp2p.peerStore = stubInterface<PeerStore>()

server = createServer(defaultMultiaddr, libp2p)
Expand Down
14 changes: 7 additions & 7 deletions packages/libp2p-daemon-client/test/pubsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import sinon from 'sinon'
import { type StubbedInstance, stubInterface } from 'sinon-ts'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { createClient, type DaemonClient } from '../src/index.js'
import type { DHT } from '@libp2p/interface-dht'
import type { Libp2p } from '@libp2p/interface-libp2p'
import type { PubSub } from '@libp2p/interface-pubsub'
import type { GossipSub } from '@chainsafe/libp2p-gossipsub'
import type { Libp2p } from '@libp2p/interface'
import type { KadDHT } from '@libp2p/kad-dht'

const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/12345')

describe('daemon pubsub client', function () {
this.timeout(30e3)

let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let libp2p: StubbedInstance<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>
let server: Libp2pServer
let client: DaemonClient
let pubsub: StubbedInstance<PubSub>
let pubsub: StubbedInstance<GossipSub>

beforeEach(async function () {
pubsub = stubInterface<PubSub>()
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
pubsub = stubInterface<GossipSub>()
libp2p = stubInterface<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>()
libp2p.services.pubsub = pubsub

server = createServer(defaultMultiaddr, libp2p)
Expand Down
14 changes: 7 additions & 7 deletions packages/libp2p-daemon-client/test/stream.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

import { createServer, type Libp2pServer } from '@libp2p/daemon-server'
import { mockRegistrar, connectionPair } from '@libp2p/interface-mocks'
import { mockRegistrar, connectionPair } from '@libp2p/interface-compliance-tests/mocks'
import { peerIdFromString } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
Expand All @@ -12,22 +12,22 @@ import { type StubbedInstance, stubInterface } from 'sinon-ts'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { createClient, type DaemonClient } from '../src/index.js'
import type { DHT } from '@libp2p/interface-dht'
import type { Libp2p } from '@libp2p/interface-libp2p'
import type { PeerStore } from '@libp2p/interface-peer-store'
import type { PubSub } from '@libp2p/interface-pubsub'
import type { GossipSub } from '@chainsafe/libp2p-gossipsub'
import type { Libp2p } from '@libp2p/interface'
import type { PeerStore } from '@libp2p/interface/peer-store'
import type { KadDHT } from '@libp2p/kad-dht'

const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/0')

describe('daemon stream client', function () {
this.timeout(50e3)

let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let libp2p: StubbedInstance<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>
let server: Libp2pServer
let client: DaemonClient

beforeEach(async function () {
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
libp2p = stubInterface<Libp2p<{ dht: KadDHT, pubsub: GossipSub }>>()
libp2p.peerStore = stubInterface<PeerStore>()

server = createServer(defaultMultiaddr, libp2p)
Expand Down
5 changes: 5 additions & 0 deletions packages/libp2p-daemon-client/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"entryPoints": [
"./src/index.ts"
]
}
13 changes: 5 additions & 8 deletions packages/libp2p-daemon-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-daemon/issues"
},
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
Expand Down Expand Up @@ -157,13 +153,14 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/interface-transport": "^4.0.0",
"it-handshake": "^4.0.0",
"@libp2p/interface": "^0.1.0",
"@libp2p/logger": "^3.0.0",
"it-handshake": "^4.1.3",
"protons-runtime": "^5.0.0",
"uint8arraylist": "^2.3.2"
"uint8arraylist": "^2.4.3"
},
"devDependencies": {
"aegir": "^39.0.10",
"aegir": "^40.0.1",
"protons": "^7.0.2"
}
}
2 changes: 1 addition & 1 deletion packages/libp2p-daemon-protocol/src/upgrader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Upgrader } from '@libp2p/interface-transport'
import type { Upgrader } from '@libp2p/interface/transport'

export const passThroughUpgrader: Upgrader = {
// @ts-expect-error should return a connection
Expand Down
7 changes: 7 additions & 0 deletions packages/libp2p-daemon-protocol/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"entryPoints": [
"./src/index.ts",
"./src/stream-handler.ts",
"./src/upgrader.ts"
]
}
35 changes: 14 additions & 21 deletions packages/libp2p-daemon-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
"keywords": [
"libp2p"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
Expand Down Expand Up @@ -138,26 +134,23 @@
"release": "aegir release"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^9.0.0",
"@libp2p/daemon-protocol": "^4.0.0",
"@libp2p/interface-connection": "^5.0.1",
"@libp2p/interface-dht": "^2.0.0",
"@libp2p/interface-libp2p": "^3.0.0",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-pubsub": "^4.0.0",
"@libp2p/interface-transport": "^4.0.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^2.0.0",
"@libp2p/tcp": "^7.0.1",
"@multiformats/multiaddr": "^12.0.0",
"it-drain": "^3.0.1",
"it-length-prefixed": "^9.0.0",
"it-pipe": "^3.0.0",
"it-pushable": "^3.0.0",
"multiformats": "^11.0.0",
"uint8arrays": "^4.0.2"
"@libp2p/interface": "^0.1.0",
"@libp2p/kad-dht": "^10.0.0",
"@libp2p/logger": "^3.0.0",
"@libp2p/peer-id": "^3.0.0",
"@libp2p/tcp": "^8.0.0",
"@multiformats/multiaddr": "^12.1.3",
"it-drain": "^3.0.2",
"it-length-prefixed": "^9.0.1",
"it-pipe": "^3.0.1",
"it-pushable": "^3.2.0",
"multiformats": "^12.0.1",
"uint8arrays": "^4.0.4"
},
"devDependencies": {
"aegir": "^39.0.10",
"aegir": "^40.0.1",
"sinon-ts": "^1.0.0"
}
}
8 changes: 4 additions & 4 deletions packages/libp2p-daemon-server/src/dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import {
import { logger } from '@libp2p/logger'
import drain from 'it-drain'
import { ErrorResponse, OkResponse } from './responses.js'
import type { DHT } from '@libp2p/interface-dht'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { KadDHT } from '@libp2p/kad-dht'
import type { CID } from 'multiformats/cid'

const log = logger('libp2p:daemon-server:dht')

export interface DHTOperationsInit {
dht: DHT
dht: KadDHT
}

export class DHTOperations {
private readonly dht: DHT
private readonly dht: KadDHT

constructor (init: DHTOperationsInit) {
const { dht } = init
Expand Down
Loading

0 comments on commit 92eebfa

Please sign in to comment.