Skip to content

Commit

Permalink
fix: typescript enums need to be value-compatible (#2433)
Browse files Browse the repository at this point in the history
As of TypeScript 5.4.x enums need to have the same values to be
compatible so re-use the exported enum from the message definition.

Refs: https://devblogs.microsoft.com/typescript/announcing-typescript-5-4/#new-enum-assignability-restrictions
  • Loading branch information
achingbrain authored Mar 11, 2024
1 parent ab46600 commit 83ef371
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
10 changes: 2 additions & 8 deletions packages/kad-dht/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
*/

import { KadDHT as KadDHTClass } from './kad-dht.js'
import { MessageType } from './message/dht.js'
import { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughMapper } from './utils.js'
import type { ProvidersInit } from './providers.js'
import type { Libp2pEvents, ComponentLogger, TypedEventTarget, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
Expand Down Expand Up @@ -109,14 +110,7 @@ export enum EventTypes {
/**
* The types of messages sent to peers during DHT queries
*/
export enum MessageType {
PUT_VALUE = 0,
GET_VALUE,
ADD_PROVIDER,
GET_PROVIDERS,
FIND_NODE,
PING
}
export { MessageType }

export type MessageName = keyof typeof MessageType

Expand Down
9 changes: 4 additions & 5 deletions packages/kad-dht/src/query/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CustomEvent } from '@libp2p/interface'
import { MessageType } from '../message/dht.js'
import type { SendQueryEvent, PeerResponseEvent, DialPeerEvent, AddPeerEvent, ValueEvent, ProviderEvent, QueryErrorEvent, FinalPeerEvent } from '../index.js'
import type { MessageType, SendQueryEvent, PeerResponseEvent, DialPeerEvent, AddPeerEvent, ValueEvent, ProviderEvent, QueryErrorEvent, FinalPeerEvent } from '../index.js'
import type { Libp2pRecord } from '../record/index.js'
import type { PeerId, PeerInfo } from '@libp2p/interface'
import type { ProgressOptions } from 'progress-events'
Expand All @@ -16,23 +15,23 @@ export function sendQueryEvent (fields: QueryEventFields, options: ProgressOptio
name: 'SEND_QUERY',
type: 0,
messageName: fields.type,
messageType: MessageType[fields.type]
messageType: fields.type
}

options.onProgress?.(new CustomEvent('kad-dht:query:send-query', { detail: event }))

return event
}

export interface PeerResponseEventField {
export interface PeerResponseEventFields {
from: PeerId
messageType: MessageType
closer?: PeerInfo[]
providers?: PeerInfo[]
record?: Libp2pRecord
}

export function peerResponseEvent (fields: PeerResponseEventField, options: ProgressOptions = {}): PeerResponseEvent {
export function peerResponseEvent (fields: PeerResponseEventFields, options: ProgressOptions = {}): PeerResponseEvent {
const event: PeerResponseEvent = {
...fields,
name: 'PEER_RESPONSE',
Expand Down
6 changes: 3 additions & 3 deletions packages/kad-dht/test/kad-dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import sinon from 'sinon'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import * as c from '../src/constants.js'
import { EventTypes, type FinalPeerEvent, MessageType, type QueryEvent, type ValueEvent } from '../src/index.js'
import { MessageType as PBMessageType } from '../src/message/dht.js'
import { EventTypes, MessageType } from '../src/index.js'
import { peerResponseEvent } from '../src/query/events.js'
import { Libp2pRecord } from '../src/record/index.js'
import * as kadUtils from '../src/utils.js'
Expand All @@ -25,6 +24,7 @@ import { createValues } from './utils/create-values.js'
import { countDiffPeers } from './utils/index.js'
import { sortClosestPeers } from './utils/sort-closest-peers.js'
import { TestDHT } from './utils/test-dht.js'
import type { FinalPeerEvent, QueryEvent, ValueEvent } from '../src/index.js'
import type { KadDHT } from '../src/kad-dht.js'
import type { PeerId } from '@libp2p/interface'
import type { CID } from 'multiformats/cid'
Expand Down Expand Up @@ -475,7 +475,7 @@ describe('KadDHT', () => {

for (const [peerId, msg] of calls) {
expect(idsB58).includes(peerId.toString())
expect(msg.type).equals(PBMessageType.ADD_PROVIDER)
expect(msg.type).equals(MessageType.ADD_PROVIDER)
expect(valuesBuffs).includes(msg.key)
expect(msg.providers.length).equals(1)
expect(peerIdFromBytes(msg.providers[0].id).toString()).equals(idsB58[3])
Expand Down

0 comments on commit 83ef371

Please sign in to comment.