Skip to content

Commit

Permalink
chore: rename dht events
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 17, 2024
1 parent 99b6dcc commit efef473
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import EchoServer from 'aegir/echo-server'
/** @type {import('aegir').PartialOptions} */
export default {
build: {
bundlesizeMax: '49KB'
bundlesizeMax: '42KB'
},
test: {
/**
Expand Down
54 changes: 25 additions & 29 deletions src/dht/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import type { HTTPRPCClient } from '../lib/core.js'
import type { PeerId, PeerInfo } from '@libp2p/interface'
import type { CID } from 'multiformats/cid'

export interface DHTProvideOptions extends HTTPRPCOptions {
recursive?: boolean
}

export enum DHTEventTypes {
export enum RoutingEventTypes {
SENDING_QUERY = 0,
PEER_RESPONSE,
FINAL_PEER,
Expand All @@ -22,7 +18,7 @@ export enum DHTEventTypes {
/**
* The types of messages set/received during DHT queries
*/
export enum DHTMessageType {
export enum RoutingMessageType {
PUT_VALUE = 0,
GET_VALUE,
ADD_PROVIDER,
Expand All @@ -31,73 +27,73 @@ export enum DHTMessageType {
PING
}

export type DHTMessageName = keyof typeof DHTMessageType
export type RoutingMessageName = keyof typeof RoutingMessageType

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

export interface DHTSendingQueryEvent {
type: DHTEventTypes.SENDING_QUERY
export interface RoutingSendingQueryEvent {
type: RoutingEventTypes.SENDING_QUERY
name: 'SENDING_QUERY'
}

export interface DHTPeerResponseEvent {
export interface RoutingPeerResponseEvent {
from: PeerId
type: DHTEventTypes.PEER_RESPONSE
type: RoutingEventTypes.PEER_RESPONSE
name: 'PEER_RESPONSE'
messageType: DHTMessageType
messageName: DHTMessageName
messageType: RoutingMessageType
messageName: RoutingMessageName
providers: PeerInfo[]
closer: PeerInfo[]
record?: DHTRecord
record?: RoutingRecord
}

export interface DHTFinalPeerEvent {
export interface RoutingFinalPeerEvent {
peer: PeerInfo
type: DHTEventTypes.FINAL_PEER
type: RoutingEventTypes.FINAL_PEER
name: 'FINAL_PEER'
}

export interface DHTQueryErrorEvent {
type: DHTEventTypes.QUERY_ERROR
export interface RoutingQueryErrorEvent {
type: RoutingEventTypes.QUERY_ERROR
name: 'QUERY_ERROR'
error: Error
}

export interface DHTProviderEvent {
type: DHTEventTypes.PROVIDER
export interface RoutingProviderEvent {
type: RoutingEventTypes.PROVIDER
name: 'PROVIDER'
providers: PeerInfo[]
}

export interface DHTValueEvent {
type: DHTEventTypes.VALUE
export interface RoutingValueEvent {
type: RoutingEventTypes.VALUE
name: 'VALUE'
value: Uint8Array
}

export interface DHTAddingPeerEvent {
type: DHTEventTypes.ADDING_PEER
export interface RoutingAddingPeerEvent {
type: RoutingEventTypes.ADDING_PEER
name: 'ADDING_PEER'
peer: PeerId
}

export interface DHTDialingPeerEvent {
export interface RoutingDialingPeerEvent {
peer: PeerId
type: DHTEventTypes.DIALING_PEER
type: RoutingEventTypes.DIALING_PEER
name: 'DIALING_PEER'
}

export type DHTQueryEvent = DHTSendingQueryEvent | DHTPeerResponseEvent | DHTFinalPeerEvent | DHTQueryErrorEvent | DHTProviderEvent | DHTValueEvent | DHTAddingPeerEvent | DHTDialingPeerEvent
export type RoutingQueryEvent = RoutingSendingQueryEvent | RoutingPeerResponseEvent | RoutingFinalPeerEvent | RoutingQueryErrorEvent | RoutingProviderEvent | RoutingValueEvent | RoutingAddingPeerEvent | RoutingDialingPeerEvent

export interface DHTAPI {
/**
* Find the closest peers to a given `PeerId` or `CID`, by querying the DHT.
*/
query(peerId: PeerId | CID, options?: HTTPRPCOptions): AsyncIterable<DHTQueryEvent>
query(peerId: PeerId | CID, options?: HTTPRPCOptions): AsyncIterable<RoutingQueryEvent>
}

export function createDHT (client: HTTPRPCClient): DHTAPI {
Expand Down
4 changes: 2 additions & 2 deletions src/dht/map-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
AddingPeer,
DialingPeer
} from './response-types.js'
import type { DHTQueryEvent } from './index.js'
import type { RoutingQueryEvent } from './index.js'

export function mapEvent (event: any): DHTQueryEvent {
export function mapEvent (event: any): RoutingQueryEvent {
if (event.Type === SendingQuery) {
return {
name: 'SENDING_QUERY',
Expand Down
12 changes: 6 additions & 6 deletions src/routing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createFindProvs } from './find-provs.js'
import { createGet } from './get.js'
import { createProvide } from './provide.js'
import { createPut } from './put.js'
import type { DHTQueryEvent } from '../dht/index.js'
import type { RoutingQueryEvent } from '../dht/index.js'
import type { HTTPRPCOptions } from '../index.js'
import type { HTTPRPCClient } from '../lib/core.js'
import type { PeerId } from '@libp2p/interface'
Expand Down Expand Up @@ -34,11 +34,11 @@ export interface RoutingProvideOptions extends HTTPRPCOptions {
}

export interface RoutingAPI {
findPeer(peerId: string | PeerId, options?: HTTPRPCOptions): AsyncIterable<DHTQueryEvent>
findProvs(key: string | CID, options?: RoutingFindProvsOptions): AsyncIterable<DHTQueryEvent>
get(key: string | Uint8Array, options?: HTTPRPCOptions): AsyncIterable<DHTQueryEvent>
put(key: string | Uint8Array, value: Uint8Array, options?: RoutingPutOptions): AsyncIterable<DHTQueryEvent>
provide(key: string | string[] | CID | CID[], options?: RoutingProvideOptions): AsyncIterable<DHTQueryEvent>
findPeer(peerId: string | PeerId, options?: HTTPRPCOptions): AsyncIterable<RoutingQueryEvent>
findProvs(key: string | CID, options?: RoutingFindProvsOptions): AsyncIterable<RoutingQueryEvent>
get(key: string | Uint8Array, options?: HTTPRPCOptions): AsyncIterable<RoutingQueryEvent>
put(key: string | Uint8Array, value: Uint8Array, options?: RoutingPutOptions): AsyncIterable<RoutingQueryEvent>
provide(key: string | string[] | CID | CID[], options?: RoutingProvideOptions): AsyncIterable<RoutingQueryEvent>
}

export function createRouting (client: HTTPRPCClient): RoutingAPI {
Expand Down
4 changes: 2 additions & 2 deletions test/interface-tests/src/routing/find-peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import drain from 'it-drain'
import { ensureReachable } from '../dht/utils.js'
import { getDescribe, getIt, type MochaConfig } from '../utils/mocha.js'
import testTimeout from '../utils/test-timeout.js'
import type { DHTQueryEvent } from '../../../../src/dht/index.js'
import type { RoutingQueryEvent } from '../../../../src/dht/index.js'
import type { KuboRPCClient } from '../../../../src/index.js'
import type { KuboRPCFactory } from '../index.js'

Expand Down Expand Up @@ -63,7 +63,7 @@ export function testFindPeer (factory: KuboRPCFactory, options: MochaConfig): vo
it('should fail to find other peer if peer does not exist', async function () {
const events = await all(nodeA.routing.findPeer(peerIdFromString('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ')))

const groupedEvents: Record<string, DHTQueryEvent[]> = events.reduce<Record<string, DHTQueryEvent[]>>((all, current) => {
const groupedEvents: Record<string, RoutingQueryEvent[]> = events.reduce<Record<string, RoutingQueryEvent[]>>((all, current) => {
if (all[current.name] != null) {
all[current.name].push(current)
} else {
Expand Down

0 comments on commit efef473

Please sign in to comment.