Skip to content

Commit

Permalink
deps: update project deps to support no-copy operations (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Aug 3, 2022
1 parent 4b3b854 commit 6a129b7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ updates:
interval: daily
time: "10:00"
open-pull-requests-limit: 10
commit-message:
prefix: "deps"
prefix-development: "deps(dev)"
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@
"release": "patch"
},
{
"type": "chore",
"type": "docs",
"release": "patch"
},
{
"type": "docs",
"type": "test",
"release": "patch"
},
{
"type": "test",
"type": "deps",
"release": "patch"
},
{
Expand Down Expand Up @@ -111,7 +111,11 @@
},
{
"type": "docs",
"section": "Trivial Changes"
"section": "Documentation"
},
{
"type": "deps",
"section": "Dependencies"
},
{
"type": "test",
Expand Down Expand Up @@ -144,24 +148,25 @@
},
"dependencies": {
"@libp2p/interface-peer-id": "^1.0.2",
"@libp2p/interface-pubsub": "^1.0.3",
"@libp2p/interface-pubsub": "^2.0.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/pubsub": "^3.0.1",
"protons-runtime": "^1.0.3",
"protons-runtime": "^2.0.2",
"uint8arraylist": "^2.1.1",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@libp2p/components": "^2.0.0",
"@libp2p/interface-mocks": "^3.0.1",
"@libp2p/interface-pubsub-compliance-tests": "^1.0.1",
"@libp2p/interface-pubsub-compliance-tests": "^2.0.1",
"@libp2p/peer-collections": "^2.0.0",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/peer-id-factory": "^1.0.9",
"@multiformats/multiaddr": "^10.1.8",
"aegir": "^37.2.0",
"multiformats": "^9.4.5",
"p-wait-for": "^4.1.0",
"protons": "^3.0.3",
"p-wait-for": "^5.0.0",
"protons": "^4.0.1",
"sinon": "^14.0.0",
"wherearewe": "^1.0.0"
}
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { PubSubInit, Message, PubSubRPC, PubSubRPCMessage, PublishResult }
import type { PeerId } from '@libp2p/interface-peer-id'
import { logger } from '@libp2p/logger'
import { RPC } from './message/rpc.js'
import type { Uint8ArrayList } from 'uint8arraylist'

const log = logger('libp2p:floodsub')

Expand Down Expand Up @@ -43,22 +44,22 @@ export class FloodSub extends PubSubBaseProtocol {
/**
* Decode a Uint8Array into an RPC object
*/
decodeRpc (bytes: Uint8Array): PubSubRPC {
decodeRpc (bytes: Uint8Array | Uint8ArrayList): PubSubRPC {
return RPC.decode(bytes)
}

/**
* Encode an RPC object into a Uint8Array
*/
encodeRpc (rpc: PubSubRPC): Uint8Array {
encodeRpc (rpc: PubSubRPC): Uint8ArrayList {
return RPC.encode(rpc)
}

decodeMessage (bytes: Uint8Array): PubSubRPCMessage {
decodeMessage (bytes: Uint8Array | Uint8ArrayList): PubSubRPCMessage {
return RPC.Message.decode(bytes)
}

encodeMessage (rpc: PubSubRPCMessage): Uint8Array {
encodeMessage (rpc: PubSubRPCMessage): Uint8ArrayList {
return RPC.Message.encode(rpc)
}

Expand Down
57 changes: 30 additions & 27 deletions src/message/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* eslint-disable @typescript-eslint/no-namespace */

import { encodeMessage, decodeMessage, message, bool, string, bytes, uint64 } from 'protons-runtime'
import type { Codec } from 'protons-runtime'
import type { Uint8ArrayList } from 'uint8arraylist'

export interface RPC {
subscriptions: RPC.SubOpts[]
Expand All @@ -16,21 +18,22 @@ export namespace RPC {
}

export namespace SubOpts {
export const codec = () => {
export const codec = (): Codec<SubOpts> => {
return message<SubOpts>({
1: { name: 'subscribe', codec: bool, optional: true },
2: { name: 'topic', codec: string, optional: true }
})
}

export const encode = (obj: SubOpts): Uint8Array => {
export const encode = (obj: SubOpts): Uint8ArrayList => {
return encodeMessage(obj, SubOpts.codec())
}

export const decode = (buf: Uint8Array): SubOpts => {
export const decode = (buf: Uint8Array | Uint8ArrayList): SubOpts => {
return decodeMessage(buf, SubOpts.codec())
}
}

export interface Message {
from?: Uint8Array
data?: Uint8Array
Expand All @@ -41,7 +44,7 @@ export namespace RPC {
}

export namespace Message {
export const codec = () => {
export const codec = (): Codec<Message> => {
return message<Message>({
1: { name: 'from', codec: bytes, optional: true },
2: { name: 'data', codec: bytes, optional: true },
Expand All @@ -52,28 +55,28 @@ export namespace RPC {
})
}

export const encode = (obj: Message): Uint8Array => {
export const encode = (obj: Message): Uint8ArrayList => {
return encodeMessage(obj, Message.codec())
}

export const decode = (buf: Uint8Array): Message => {
export const decode = (buf: Uint8Array | Uint8ArrayList): Message => {
return decodeMessage(buf, Message.codec())
}
}

export const codec = () => {
export const codec = (): Codec<RPC> => {
return message<RPC>({
1: { name: 'subscriptions', codec: RPC.SubOpts.codec(), repeats: true },
2: { name: 'messages', codec: RPC.Message.codec(), repeats: true },
3: { name: 'control', codec: ControlMessage.codec(), optional: true }
})
}

export const encode = (obj: RPC): Uint8Array => {
export const encode = (obj: RPC): Uint8ArrayList => {
return encodeMessage(obj, RPC.codec())
}

export const decode = (buf: Uint8Array): RPC => {
export const decode = (buf: Uint8Array | Uint8ArrayList): RPC => {
return decodeMessage(buf, RPC.codec())
}
}
Expand All @@ -86,7 +89,7 @@ export interface ControlMessage {
}

export namespace ControlMessage {
export const codec = () => {
export const codec = (): Codec<ControlMessage> => {
return message<ControlMessage>({
1: { name: 'ihave', codec: ControlIHave.codec(), repeats: true },
2: { name: 'iwant', codec: ControlIWant.codec(), repeats: true },
Expand All @@ -95,11 +98,11 @@ export namespace ControlMessage {
})
}

export const encode = (obj: ControlMessage): Uint8Array => {
export const encode = (obj: ControlMessage): Uint8ArrayList => {
return encodeMessage(obj, ControlMessage.codec())
}

export const decode = (buf: Uint8Array): ControlMessage => {
export const decode = (buf: Uint8Array | Uint8ArrayList): ControlMessage => {
return decodeMessage(buf, ControlMessage.codec())
}
}
Expand All @@ -110,18 +113,18 @@ export interface ControlIHave {
}

export namespace ControlIHave {
export const codec = () => {
export const codec = (): Codec<ControlIHave> => {
return message<ControlIHave>({
1: { name: 'topic', codec: string, optional: true },
2: { name: 'messageIDs', codec: bytes, repeats: true }
})
}

export const encode = (obj: ControlIHave): Uint8Array => {
export const encode = (obj: ControlIHave): Uint8ArrayList => {
return encodeMessage(obj, ControlIHave.codec())
}

export const decode = (buf: Uint8Array): ControlIHave => {
export const decode = (buf: Uint8Array | Uint8ArrayList): ControlIHave => {
return decodeMessage(buf, ControlIHave.codec())
}
}
Expand All @@ -131,17 +134,17 @@ export interface ControlIWant {
}

export namespace ControlIWant {
export const codec = () => {
export const codec = (): Codec<ControlIWant> => {
return message<ControlIWant>({
1: { name: 'messageIDs', codec: bytes, repeats: true }
})
}

export const encode = (obj: ControlIWant): Uint8Array => {
export const encode = (obj: ControlIWant): Uint8ArrayList => {
return encodeMessage(obj, ControlIWant.codec())
}

export const decode = (buf: Uint8Array): ControlIWant => {
export const decode = (buf: Uint8Array | Uint8ArrayList): ControlIWant => {
return decodeMessage(buf, ControlIWant.codec())
}
}
Expand All @@ -151,17 +154,17 @@ export interface ControlGraft {
}

export namespace ControlGraft {
export const codec = () => {
export const codec = (): Codec<ControlGraft> => {
return message<ControlGraft>({
1: { name: 'topic', codec: string, optional: true }
})
}

export const encode = (obj: ControlGraft): Uint8Array => {
export const encode = (obj: ControlGraft): Uint8ArrayList => {
return encodeMessage(obj, ControlGraft.codec())
}

export const decode = (buf: Uint8Array): ControlGraft => {
export const decode = (buf: Uint8Array | Uint8ArrayList): ControlGraft => {
return decodeMessage(buf, ControlGraft.codec())
}
}
Expand All @@ -173,19 +176,19 @@ export interface ControlPrune {
}

export namespace ControlPrune {
export const codec = () => {
export const codec = (): Codec<ControlPrune> => {
return message<ControlPrune>({
1: { name: 'topic', codec: string, optional: true },
2: { name: 'peers', codec: PeerInfo.codec(), repeats: true },
3: { name: 'backoff', codec: uint64, optional: true }
})
}

export const encode = (obj: ControlPrune): Uint8Array => {
export const encode = (obj: ControlPrune): Uint8ArrayList => {
return encodeMessage(obj, ControlPrune.codec())
}

export const decode = (buf: Uint8Array): ControlPrune => {
export const decode = (buf: Uint8Array | Uint8ArrayList): ControlPrune => {
return decodeMessage(buf, ControlPrune.codec())
}
}
Expand All @@ -196,18 +199,18 @@ export interface PeerInfo {
}

export namespace PeerInfo {
export const codec = () => {
export const codec = (): Codec<PeerInfo> => {
return message<PeerInfo>({
1: { name: 'peerID', codec: bytes, optional: true },
2: { name: 'signedPeerRecord', codec: bytes, optional: true }
})
}

export const encode = (obj: PeerInfo): Uint8Array => {
export const encode = (obj: PeerInfo): Uint8ArrayList => {
return encodeMessage(obj, PeerInfo.codec())
}

export const decode = (buf: Uint8Array): PeerInfo => {
export const decode = (buf: Uint8Array | Uint8ArrayList): PeerInfo => {
return decodeMessage(buf, PeerInfo.codec())
}
}
4 changes: 2 additions & 2 deletions test/floodsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ describe('floodsub', () => {
const [to, rpc] = spy.getCall(0).args

const expected: Message = {
from: floodsub.components.getPeerId(),
type: 'unsigned',
data: message,
topic
}

expect(to).to.eql(otherPeer)
expect(rpc).to.have.nested.property('messages[0]').that.deep.equals(expected)
expect(rpc).to.have.nested.property('messages[0]').that.containSubset(expected)
})

it('does not send received message back to original sender', async () => {
Expand Down

0 comments on commit 6a129b7

Please sign in to comment.