Skip to content

Commit

Permalink
fix!: remove @libp2p/components (#90)
Browse files Browse the repository at this point in the history
`@libp2p/components` is a choke-point for our dependency graph as it depends on every interface, meaning when one interface revs a major `@libp2p/components` major has to change too which means every module depending on it also needs a major.

Switch instead to constructor injection of simple objects that let modules declare their dependencies on interfaces directly instead of indirectly via `@libp2p/components`

Refs libp2p/js-libp2p-components#6

BREAKING CHANGE: modules no longer implement `Initializable` instead switching to constructor injection
  • Loading branch information
achingbrain authored Oct 13, 2022
1 parent 4f472d3 commit a3816b1
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 46 deletions.
35 changes: 16 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# @libp2p/delegated-peer-routing <!-- omit in toc -->

[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![IRC](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-delegated-peer-routing.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-delegated-peer-routing/actions/workflows/js-test-and-release.yml)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-delegated-peer-routing/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-delegated-peer-routing/actions/workflows/js-test-and-release.yml)

> Leverage other peers in the libp2p network to perform Peer Routing calls.
Expand Down Expand Up @@ -37,30 +36,28 @@ npm install ipfs-http-client libp2p-delegated-peer-routing
## Example

```js
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { DelegatedPeerRouting } from '@libp2p/delegated-peer-routing')
import { createLibp2p } from 'libp2p'
import { delegatedPeerRouting } from '@libp2p/delegated-peer-routing')
import { create as createIpfsHttpClient } from 'ipfs-http-client')

// default is to use ipfs.io
const routing = new DelegatedPeerRouting(createIpfsHttpClient({
const client = createIpfsHttpClient({
// use default api settings
protocol: 'https',
port: 443,
host: 'node0.delegate.ipfs.io'
}))

try {
for await (const event of routing.findPeer('peerid')) {
console.log('query event', event)
}
} catch (err) {
console.error(err)
}

const peerId = await createEd25519PeerId()
for await (const event of routing.getClosestPeers(peerId.id)) {
console.log('query event', event)
}
})

const node = await createLibp2p({
peerRouting: [
delegatedPeerRouting(client)
]
//.. other config
})
await node.start()

const peerInfo = await node.peerRouting.findPeer('peerid')
console.log('peerInfo', peerInfo)
```

## License
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"@libp2p/peer-id": "^1.1.11",
"any-signal": "^3.0.1",
"err-code": "^3.0.1",
"multiformats": "^9.6.3",
"multiformats": "^10.0.0",
"p-defer": "^4.0.0",
"p-queue": "^7.2.0"
},
Expand All @@ -155,7 +155,8 @@
"ipfsd-ctl": "^12.0.2",
"it-all": "^1.0.6",
"it-drain": "^1.0.5",
"uint8arrays": "^3.0.0",
"timeout-abort-controller": "^3.0.0",
"uint8arrays": "^4.0.2",
"wherearewe": "^2.0.1"
},
"browser": {
Expand Down
110 changes: 106 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import defer from 'p-defer'
import errCode from 'err-code'
import anySignal from 'any-signal'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { IPFSHTTPClient, HTTPClientExtraOptions } from 'ipfs-http-client'
import type { AbortOptions } from 'ipfs-core-types/src/utils'
import type { PeerRouting } from '@libp2p/interface-peer-routing'
import type { PeerInfo } from '@libp2p/interface-peer-info'
Expand All @@ -17,16 +16,115 @@ const log = logger('libp2p-delegated-peer-routing')
const DEFAULT_TIMEOUT = 30e3 // 30 second default
const CONCURRENT_HTTP_REQUESTS = 4

export class DelegatedPeerRouting implements PeerRouting, Startable {
private readonly client: IPFSHTTPClient
export interface HTTPClientExtraOptions {
headers?: Record<string, string>
searchParams?: URLSearchParams
}

export enum EventTypes {
SENDING_QUERY = 0,
PEER_RESPONSE,
FINAL_PEER,
QUERY_ERROR,
PROVIDER,
VALUE,
ADDING_PEER,
DIALING_PEER
}

/**
* The types of messages set/received during DHT queries
*/
export enum MessageType {
PUT_VALUE = 0,
GET_VALUE,
ADD_PROVIDER,
GET_PROVIDERS,
FIND_NODE,
PING
}

export type MessageName = keyof typeof MessageType

export interface DHTRecord {
key: Uint8Array
value: Uint8Array
timeReceived?: Date
}

export interface SendingQueryEvent {
type: EventTypes.SENDING_QUERY
name: 'SENDING_QUERY'
}

export interface PeerResponseEvent {
from: PeerId
type: EventTypes.PEER_RESPONSE
name: 'PEER_RESPONSE'
messageType: MessageType
messageName: MessageName
providers: PeerInfo[]
closer: PeerInfo[]
record?: DHTRecord
}

export interface FinalPeerEvent {
peer: PeerInfo
type: EventTypes.FINAL_PEER
name: 'FINAL_PEER'
}

export interface QueryErrorEvent {
type: EventTypes.QUERY_ERROR
name: 'QUERY_ERROR'
error: Error
}

export interface ProviderEvent {
type: EventTypes.PROVIDER
name: 'PROVIDER'
providers: PeerInfo[]
}

export interface ValueEvent {
type: EventTypes.VALUE
name: 'VALUE'
value: Uint8Array
}

export interface AddingPeerEvent {
type: EventTypes.ADDING_PEER
name: 'ADDING_PEER'
peer: PeerId
}

export interface DialingPeerEvent {
peer: PeerId
type: EventTypes.DIALING_PEER
name: 'DIALING_PEER'
}

export type QueryEvent = SendingQueryEvent | PeerResponseEvent | FinalPeerEvent | QueryErrorEvent | ProviderEvent | ValueEvent | AddingPeerEvent | DialingPeerEvent

export interface Delegate {
getEndpointConfig: () => { protocol: string, host: string, port: string }

dht: {
findPeer: (peerId: PeerId, options?: AbortOptions) => AsyncIterable<QueryEvent>
query: (peerId: PeerId | CID, options?: AbortOptions) => AsyncIterable<QueryEvent>
}
}

class DelegatedPeerRouting implements PeerRouting, Startable {
private readonly client: Delegate
private readonly httpQueue: PQueue
private started: boolean
private abortController: AbortController

/**
* Create a new DelegatedPeerRouting instance
*/
constructor (client: IPFSHTTPClient) {
constructor (client: Delegate) {
if (client == null) {
throw new Error('missing ipfs http client')
}
Expand Down Expand Up @@ -153,3 +251,7 @@ export class DelegatedPeerRouting implements PeerRouting, Startable {
}
}
}

export function delegatedPeerRouting (client: Delegate): (components?: any) => PeerRouting {
return () => new DelegatedPeerRouting(client)
}
Loading

0 comments on commit a3816b1

Please sign in to comment.