Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: encode enums correctly (#332)
Browse files Browse the repository at this point in the history
Updates protons and regenerates protobuf code to encode enums correctly
  • Loading branch information
achingbrain authored May 10, 2022
1 parent 760a2cd commit af1e701
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/crypto": "^0.22.11",
"@libp2p/crypto": "^0.22.12",
"@libp2p/interfaces": "^1.3.32",
"@libp2p/logger": "^1.1.4",
"@libp2p/peer-id": "^1.1.10",
Expand Down Expand Up @@ -162,7 +162,7 @@
"p-map": "^5.3.0",
"p-queue": "^7.2.0",
"private-ip": "^2.3.3",
"protons-runtime": "^1.0.2",
"protons-runtime": "^1.0.4",
"streaming-iterables": "^6.0.0",
"timeout-abort-controller": "^3.0.0",
"uint8arrays": "^3.0.0",
Expand All @@ -187,7 +187,7 @@
"lodash.random": "^3.2.0",
"lodash.range": "^3.2.0",
"p-retry": "^5.0.0",
"protons": "^3.0.2",
"protons": "^3.0.4",
"sinon": "^13.0.1",
"ts-sinon": "^2.0.2",
"which": "^2.0.2"
Expand Down
29 changes: 24 additions & 5 deletions src/message/dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/no-namespace */

import { encodeMessage, decodeMessage, message, bytes, string, enumeration, int32 } from 'protons-runtime'
import type { Codec } from 'protons-runtime'

export interface Record {
key?: Uint8Array
Expand All @@ -12,7 +13,7 @@ export interface Record {
}

export namespace Record {
export const codec = () => {
export const codec = (): Codec<Record> => {
return message<Record>({
1: { name: 'key', codec: bytes, optional: true },
2: { name: 'value', codec: bytes, optional: true },
Expand Down Expand Up @@ -50,31 +51,49 @@ export namespace Message {
PING = 'PING'
}

enum __MessageTypeValues {
PUT_VALUE = 0,
GET_VALUE = 1,
ADD_PROVIDER = 2,
GET_PROVIDERS = 3,
FIND_NODE = 4,
PING = 5
}

export namespace MessageType {
export const codec = () => {
return enumeration<typeof MessageType>(MessageType)
return enumeration<typeof MessageType>(__MessageTypeValues)
}
}

export enum ConnectionType {
NOT_CONNECTED = 'NOT_CONNECTED',
CONNECTED = 'CONNECTED',
CAN_CONNECT = 'CAN_CONNECT',
CANNOT_CONNECT = 'CANNOT_CONNECT'
}

enum __ConnectionTypeValues {
NOT_CONNECTED = 0,
CONNECTED = 1,
CAN_CONNECT = 2,
CANNOT_CONNECT = 3
}

export namespace ConnectionType {
export const codec = () => {
return enumeration<typeof ConnectionType>(ConnectionType)
return enumeration<typeof ConnectionType>(__ConnectionTypeValues)
}
}

export interface Peer {
id?: Uint8Array
addrs: Uint8Array[]
connection?: Message.ConnectionType
}

export namespace Peer {
export const codec = () => {
export const codec = (): Codec<Peer> => {
return message<Peer>({
1: { name: 'id', codec: bytes, optional: true },
2: { name: 'addrs', codec: bytes, repeats: true },
Expand All @@ -91,7 +110,7 @@ export namespace Message {
}
}

export const codec = () => {
export const codec = (): Codec<Message> => {
return message<Message>({
1: { name: 'type', codec: Message.MessageType.codec(), optional: true },
10: { name: 'clusterLevelRaw', codec: int32, optional: true },
Expand Down

0 comments on commit af1e701

Please sign in to comment.