Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix: make pubsub message types consistent #4145

Merged
merged 3 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/core-api/PUBSUB.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
| Name | Type | Description |
| ---- | ---- | ----------- |
| topic | `String` | The topic name |
| handler | `Function<(msg) => {}>` | Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: String, sequenceNumber: bigint, data: Uint8Array, topicIDs: Array<String>}` |
| handler | `Function<(msg) => {}>` | Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: PeerId, sequenceNumber: bigint, data: Uint8Array, topicIDs: Array<String>}` |

### Options

Expand Down
1 change: 1 addition & 0 deletions packages/interface-ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@ipld/dag-cbor": "^7.0.0",
"@ipld/dag-pb": "^2.1.3",
"@libp2p/crypto": "^0.22.9",
"@libp2p/interfaces": "^2.0.4",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/peer-id-factory": "^1.0.10",
"@libp2p/websockets": "^1.0.8",
Expand Down
2 changes: 2 additions & 0 deletions packages/interface-ipfs-core/src/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { isWebWorker, isNode } from 'ipfs-utils/src/env.js'
import sinon from 'sinon'
import defer from 'p-defer'
import pWaitFor from 'p-wait-for'
import { isPeerId } from '@libp2p/interfaces/peer-id'

/**
* @typedef {import('ipfsd-ctl').Factory} Factory
Expand Down Expand Up @@ -87,6 +88,7 @@ export function testSubscribe (factory, options) {
expect(msg).to.have.property('sequenceNumber')
expect(msg.sequenceNumber).to.be.a('BigInt')
expect(msg.topic).to.eq(topic)
expect(isPeerId(msg.from)).to.be.true()
expect(msg.from.toString()).to.equal(ipfs1Id.id.toString())
})

Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@libp2p/crypto": "^0.22.9",
"@libp2p/delegated-content-routing": "^1.0.3",
"@libp2p/delegated-peer-routing": "^1.0.3",
"@libp2p/interfaces": "^2.0.2",
"@libp2p/interfaces": "^2.0.4",
"@libp2p/kad-dht": "^1.0.3",
"@libp2p/logger": "^1.1.4",
"@libp2p/mplex": "^1.0.5",
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-grpc-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"@improbable-eng/grpc-web": "^0.15.0",
"@libp2p/logger": "^1.1.4",
"@libp2p/peer-id": "^1.1.10",
"change-case": "^4.1.1",
"err-code": "^3.0.1",
"ipfs-core-types": "^0.11.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-grpc-client/src/core-api/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
import { subscriptions } from './subscriptions.js'
import defer from 'p-defer'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { peerIdFromString } from '@libp2p/peer-id'

/**
* @param {import('@improbable-eng/grpc-web').grpc} grpc
Expand Down Expand Up @@ -37,7 +38,7 @@ export function grpcPubsubSubscribe (grpc, service, opts) {
} else {
/** @type {import('@libp2p/interfaces/pubsub').Message} */
const msg = {
from: result.from,
from: peerIdFromString(result.from),
sequenceNumber: result.sequenceNumber == null ? undefined : BigInt(`0x${uint8ArrayToString(result.sequenceNumber, 'base16')}`),
data: result.data,
topic: result.topic
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { logger } from '@libp2p/logger'
import { configure } from '../lib/configure.js'
import { toUrlSearchParams } from '../lib/to-url-search-params.js'
import { textToUrlSafeRpc, rpcToText, rpcToBytes, rpcToBigInt } from '../lib/http-rpc-wire-format.js'
import { peerIdFromString } from '@libp2p/peer-id'
const log = logger('ipfs-http-client:pubsub:subscribe')

/**
Expand Down Expand Up @@ -107,7 +108,7 @@ async function readMessages (response, { onMessage, onEnd, onError }) {
}

onMessage({
from: msg.from,
from: peerIdFromString(msg.from),
data: rpcToBytes(msg.data),
sequenceNumber: rpcToBigInt(msg.seqno),
topic: rpcToText(msg.topicIDs[0])
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@hapi/content": "^5.0.2",
"@hapi/hapi": "^20.0.0",
"@ipld/dag-pb": "^2.1.3",
"@libp2p/interfaces": "^2.0.2",
"@libp2p/interfaces": "^2.0.4",
"@libp2p/logger": "^1.1.4",
"@libp2p/peer-id": "^1.1.10",
"any-signal": "^3.0.0",
Expand Down