From 1b14088b16b23078e740ef8560351065a143fb10 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 7 Apr 2021 19:34:31 +0200 Subject: [PATCH] chore: update deps and remove protons --- .aegir.js | 22 +- .eslintignore | 1 + .github/workflows/main.yml | 26 +- package.json | 25 +- scripts/node-globals.js | 1 + test/2-nodes.spec.js | 8 +- test/floodsub.spec.js | 4 +- ts/index.ts | 86 +- ts/message/index.ts | 130 -- ts/message/rpc.d.ts | 624 +++++++++ ts/message/rpc.js | 1760 ++++++++++++++++++++++++ ts/message/{rpc.proto.ts => rpc.proto} | 5 +- ts/utils/create-gossip-rpc.ts | 10 +- 13 files changed, 2487 insertions(+), 215 deletions(-) create mode 100644 .eslintignore create mode 100644 scripts/node-globals.js delete mode 100644 ts/message/index.ts create mode 100644 ts/message/rpc.d.ts create mode 100644 ts/message/rpc.js rename ts/message/{rpc.proto.ts => rpc.proto} (98%) diff --git a/.aegir.js b/.aegir.js index 0b07357d..58af970b 100644 --- a/.aegir.js +++ b/.aegir.js @@ -5,6 +5,7 @@ * set up a libp2p instance for browser nodes to relay through * before tests start */ +const path = require('path') const Libp2p = require('libp2p') const PeerId = require('peer-id') @@ -52,15 +53,20 @@ const after = async () => { await libp2p.stop() } +/** @type {import('aegir').Options["build"]["config"]} */ +const esbuild = { + inject: [path.join(__dirname, './scripts/node-globals.js')] +} + +/** @type {import('aegir').PartialOptions} */ module.exports = { - hooks: { - pre: before, - post: after - }, - webpack: { - node: { - // this is needed until bcrypto stops using node buffers in browser code - Buffer: true + test: { + before, + after, + browser: { + config: { + buildConfig: esbuild + } } } } diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..5237d0e4 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +ts/message/rpc* \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 56c18de3..8a11529b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,10 +12,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - run: yarn - - run: yarn lint - - run: yarn prebuild - - run: yarn aegir dep-check + - run: npm install + - run: npm run lint + - run: npm run prebuild + - run: npx aegir dep-check test-node: needs: check runs-on: ${{ matrix.os }} @@ -29,8 +29,8 @@ jobs: - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node }} - - run: yarn - - run: yarn prebuild + - run: npm install + - run: npm run prebuild - run: npx nyc --reporter=lcov aegir test -t node -- --bail - uses: codecov/codecov-action@v1 test-chrome: @@ -38,14 +38,16 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - run: yarn - - run: yarn prebuild - - run: npx aegir test -t browser -t webworker --bail + - run: npm install + - run: npm run prebuild + - run: npx aegir test -t browser --bail + - run: npx aegir test -t webworker --bail test-firefox: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - run: yarn - - run: yarn prebuild - - run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless + - run: npm install + - run: npm run prebuild + - run: npx aegir test -t browser --bail -- --browsers FirefoxHeadless + - run: npx aegir test -t webworker --bail -- --browsers FirefoxHeadless diff --git a/package.json b/package.json index f54062ac..e297aaec 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,12 @@ "types": "src/index.d.ts", "scripts": { "lint": "eslint --ext .ts ts", - "release": "aegir release", - "prebuild": "tsc", - "build": "aegir build", + "release": "aegir release --no-types", + "prebuild": "tsc && cp -R ts/message src", + "build": "npm run build:proto && npm run build:proto-types && npm run build:types", + "build:proto": "pbjs -t static-module --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o ts/message/rpc.js ./ts/message/rpc.proto", + "build:proto-types": "pbts -o ts/message/rpc.d.ts ts/message/rpc.js", + "build:types": "aegir build --no-types", "pretest": "tsc", "test": "aegir test", "test:node": "aegir test --target node", @@ -40,22 +43,22 @@ "denque": "^1.4.1", "err-code": "^2.0.0", "it-pipe": "^1.0.1", - "libp2p-interfaces": "^0.8.0", + "libp2p-interfaces": "libp2p/js-libp2p-interfaces#chore/update-pubsub-interface", "peer-id": "^0.14.0", - "protons": "^2.0.0", + "protobufjs": "^6.10.2", "time-cache": "^0.3.0", - "uint8arrays": "^1.1.0" + "uint8arrays": "^2.1.4" }, "devDependencies": { "@types/chai": "^4.2.3", - "@types/mocha": "^7.0.2", + "@types/mocha": "^8.2.2", "@typescript-eslint/eslint-plugin": "^3.0.2", "@typescript-eslint/parser": "^3.0.2", - "aegir": "^28.0.0", + "aegir": "^33.0.0", "benchmark": "^2.1.4", "chai": "^4.2.0", "chai-spies": "^1.0.0", - "delay": "^4.3.0", + "delay": "^5.0.0", "detect-node": "^2.0.4", "dirty-chai": "^2.0.1", "eslint": "^7.1.0", @@ -74,12 +77,14 @@ "lodash": "^4.17.15", "mocha": "^7.1.1", "multiaddr": "^8.0.0", + "os": "^0.1.1", "p-retry": "^4.2.0", "p-times": "^2.1.0", "p-wait-for": "^3.1.0", "promisify-es6": "^1.0.3", "sinon": "^9.0.2", - "typescript": "4.0.x" + "typescript": "4.0.x", + "url": "^0.11.0" }, "peerDependencies": { "libp2p": "^0.30.0" diff --git a/scripts/node-globals.js b/scripts/node-globals.js new file mode 100644 index 00000000..511837b3 --- /dev/null +++ b/scripts/node-globals.js @@ -0,0 +1 @@ +export const { Buffer } = require('buffer') diff --git a/test/2-nodes.spec.js b/test/2-nodes.spec.js index d324623d..77635945 100644 --- a/test/2-nodes.spec.js +++ b/test/2-nodes.spec.js @@ -75,7 +75,9 @@ describe('2 nodes', () => { expectSet(nodes[1].topics.get(topic), [nodes[0].peerId.toB58String()]) expect(changedPeerId.toB58String()).to.equal(first(nodes[0].peers).id.toB58String()) - expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: true }]) + expect(changedSubs).to.have.lengthOf(1) + expect(changedSubs[0].topicID).to.equal(topic) + expect(changedSubs[0].subscribe).to.equal(true) // await heartbeats await Promise.all([ @@ -204,7 +206,9 @@ describe('2 nodes', () => { expect(nodes[1].peers.size).to.equal(1) expectSet(nodes[1].topics.get(topic), []) expect(changedPeerId.toB58String()).to.equal(first(nodes[1].peers).id.toB58String()) - expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: false }]) + expect(changedSubs).to.have.lengthOf(1) + expect(changedSubs[0].topicID).to.equal(topic) + expect(changedSubs[0].subscribe).to.equal(false) }) it('Publish to a topic after unsubscribe', async () => { diff --git a/test/floodsub.spec.js b/test/floodsub.spec.js index f2cca90f..cde49827 100644 --- a/test/floodsub.spec.js +++ b/test/floodsub.spec.js @@ -128,7 +128,9 @@ describe('gossipsub fallbacks to floodsub', () => { expectSet(nodeFs.topics.get(topic), [nodeGs.peerId.toB58String()]) expect(changedPeerId.toB58String()).to.equal(first(nodeGs.peers).id.toB58String()) - expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: true }]) + expect(changedSubs).to.have.lengthOf(1) + expect(changedSubs[0].topicID).to.equal(topic) + expect(changedSubs[0].subscribe).to.equal(true) }) }) diff --git a/ts/index.ts b/ts/index.ts index 3fc0edd2..691eca3b 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,10 +1,6 @@ import Pubsub, { InMessage, utils } from 'libp2p-interfaces/src/pubsub' import { MessageCache } from './message-cache' -import { - RPCCodec, - RPC, Message, - ControlMessage, ControlIHave, ControlGraft, ControlIWant, ControlPrune, PeerInfo -} from './message' +import { RPC, IRPC } from './message/rpc' import * as constants from './constants' import { Heartbeat } from './heartbeat' import { getGossipPeers } from './get-gossip-peers' @@ -76,8 +72,8 @@ class Gossipsub extends Pubsub { mesh: Map> fanout: Map> lastpub: Map - gossip: Map - control: Map + gossip: Map + control: Map peerhave:Map iasked:Map backoff: Map> @@ -96,7 +92,7 @@ class Gossipsub extends Pubsub { _libp2p: Libp2p _options: GossipOptions _directPeerInitial: NodeJS.Timeout - log: Debugger + log: Debugger & { err: Debugger } // eslint-disable-next-line @typescript-eslint/ban-types emit: (event: string | symbol, ...args: any[]) => boolean @@ -201,7 +197,7 @@ class Gossipsub extends Pubsub { * Map of pending messages to gossip * peer id => control messages * - * @type {Map> } + * @type {Map> } */ this.gossip = new Map() @@ -209,7 +205,7 @@ class Gossipsub extends Pubsub { * Map of control messages * peer id => control message * - * @type {Map} + * @type {Map} */ this.control = new Map() @@ -279,7 +275,7 @@ class Gossipsub extends Pubsub { * @returns {RPC} */ _decodeRpc (bytes: Uint8Array) { - return RPCCodec.decode(bytes) + return RPC.decode(bytes) } /** @@ -290,7 +286,7 @@ class Gossipsub extends Pubsub { * @returns {Uint8Array} */ _encodeRpc (rpc: RPC) { - return RPCCodec.encode(rpc) + return RPC.encode(rpc).finish() } /** @@ -378,18 +374,18 @@ class Gossipsub extends Pubsub { /** * Handles an rpc control message from a peer * @param {string} id peer id - * @param {ControlMessage} controlMsg + * @param {RPC.IControlMessage} controlMsg * @returns {void} */ - _processRpcControlMessage (id: string, controlMsg: ControlMessage): void { + _processRpcControlMessage (id: string, controlMsg: RPC.IControlMessage): void { if (!controlMsg) { return } - const iwant = this._handleIHave(id, controlMsg.ihave) - const ihave = this._handleIWant(id, controlMsg.iwant) - const prune = this._handleGraft(id, controlMsg.graft) - this._handlePrune(id, controlMsg.prune) + const iwant = controlMsg.ihave ? this._handleIHave(id, controlMsg.ihave) : [] + const ihave = controlMsg.iwant ? this._handleIWant(id, controlMsg.iwant) : [] + const prune = controlMsg.graft ? this._handleGraft(id, controlMsg.graft) : [] + controlMsg.prune && this._handlePrune(id, controlMsg.prune) if (!iwant.length && !ihave.length && !prune.length) { return @@ -450,10 +446,10 @@ class Gossipsub extends Pubsub { /** * Handles IHAVE messages * @param {string} id peer id - * @param {Array} ihave - * @returns {ControlIWant} + * @param {Array} ihave + * @returns {RPC.IControlIWant} */ - _handleIHave (id: string, ihave: ControlIHave[]): ControlIWant[] { + _handleIHave (id: string, ihave: RPC.IControlIHave[]): RPC.IControlIWant[] { if (!ihave.length) { return [] } @@ -491,7 +487,7 @@ class Gossipsub extends Pubsub { const iwant = new Map() ihave.forEach(({ topicID, messageIDs }) => { - if (!topicID || !this.mesh.has(topicID)) { + if (!topicID || !messageIDs || !this.mesh.has(topicID)) { return } @@ -537,10 +533,10 @@ class Gossipsub extends Pubsub { * Handles IWANT messages * Returns messages to send back to peer * @param {string} id peer id - * @param {Array} iwant - * @returns {Array} + * @param {Array} iwant + * @returns {Array} */ - _handleIWant (id: string, iwant: ControlIWant[]): Message[] { + _handleIWant (id: string, iwant: RPC.IControlIWant[]): RPC.IMessage[] { if (!iwant.length) { return [] } @@ -557,7 +553,7 @@ class Gossipsub extends Pubsub { const ihave = new Map() iwant.forEach(({ messageIDs }) => { - messageIDs.forEach((msgID) => { + messageIDs && messageIDs.forEach((msgID) => { const [msg, count] = this.messageCache.getForPeer(msgID, id) if (!msg) { return @@ -586,10 +582,10 @@ class Gossipsub extends Pubsub { /** * Handles Graft messages * @param {string} id peer id - * @param {Array} graft - * @return {Array} + * @param {Array} graft + * @return {Array} */ - _handleGraft (id: string, graft: ControlGraft[]): ControlPrune[] { + _handleGraft (id: string, graft: RPC.IControlGraft[]): RPC.IControlPrune[] { const prune: string[] = [] const score = this.score.score(id) const now = this._now() @@ -682,10 +678,10 @@ class Gossipsub extends Pubsub { /** * Handles Prune messages * @param {string} id peer id - * @param {Array} prune + * @param {Array} prune * @returns {void} */ - _handlePrune (id: string, prune: ControlPrune[]): void { + _handlePrune (id: string, prune: RPC.IControlPrune[]): void { const score = this.score.score(id) prune.forEach(({ topicID, backoff, peers }) => { if (!topicID) { @@ -811,10 +807,10 @@ class Gossipsub extends Pubsub { /** * Maybe attempt connection given signed peer records - * @param {PeerInfo[]} peers + * @param {RPC.IPeerInfo[]} peers * @returns {Promise} */ - async _pxConnect (peers: PeerInfo[]): Promise { + async _pxConnect (peers: RPC.IPeerInfo[]): Promise { if (peers.length > constants.GossipsubPrunePeers) { shuffle(peers) peers = peers.slice(0, constants.GossipsubPrunePeers) @@ -1137,7 +1133,7 @@ class Gossipsub extends Pubsub { /** * @override */ - _sendRpc (id: string, outRpc: RPC): void { + _sendRpc (id: string, outRpc: IRPC): void { const peerStreams = this.peers.get(id) if (!peerStreams || !peerStreams.isWritable) { return @@ -1157,10 +1153,10 @@ class Gossipsub extends Pubsub { this.gossip.delete(id) } - peerStreams.write(RPCCodec.encode(outRpc)) + peerStreams.write(RPC.encode(outRpc).finish()) } - _piggybackControl (id: string, outRpc: RPC, ctrl: ControlMessage): void { + _piggybackControl (id: string, outRpc: IRPC, ctrl: RPC.IControlMessage): void { const tograft = (ctrl.graft || []) .filter(({ topicID }) => (topicID && this.mesh.get(topicID) || new Set()).has(id)) const toprune = (ctrl.prune || []) @@ -1171,14 +1167,14 @@ class Gossipsub extends Pubsub { } if (outRpc.control) { - outRpc.control.graft = outRpc.control.graft.concat(tograft) - outRpc.control.prune = outRpc.control.prune.concat(toprune) + outRpc.control.graft = outRpc.control.graft && outRpc.control.graft.concat(tograft) + outRpc.control.prune = outRpc.control.prune && outRpc.control.prune.concat(toprune) } else { outRpc.control = { ihave: [], iwant: [], graft: tograft, prune: toprune } } } - _piggybackGossip (id: string, outRpc: RPC, ihave: ControlIHave[]): void { + _piggybackGossip (id: string, outRpc: IRPC, ihave: RPC.IControlIHave[]): void { if (!outRpc.control) { outRpc.control = { ihave: [], iwant: [], graft: [], prune: [] } } @@ -1194,7 +1190,7 @@ class Gossipsub extends Pubsub { const doPX = this._options.doPX for (const [id, topics] of tograft) { const graft = topics.map((topicID) => ({ topicID })) - let prune: ControlPrune[] = [] + let prune: RPC.IControlPrune[] = [] // If a peer also has prunes, process them now const pruning = toprune.get(id) if (pruning) { @@ -1305,10 +1301,10 @@ class Gossipsub extends Pubsub { /** * Adds new IHAVE messages to pending gossip * @param {PeerStreams} peerStreams - * @param {Array} controlIHaveMsgs + * @param {Array} controlIHaveMsgs * @returns {void} */ - _pushGossip (id: string, controlIHaveMsgs: ControlIHave): void { + _pushGossip (id: string, controlIHaveMsgs: RPC.IControlIHave): void { this.log('Add gossip to %s', id) const gossip = this.gossip.get(id) || [] this.gossip.set(id, gossip.concat(controlIHaveMsgs)) @@ -1327,9 +1323,9 @@ class Gossipsub extends Pubsub { * @param {string} id * @param {string} topic * @param {boolean} doPX - * @returns {ControlPrune} + * @returns {RPC.IControlPrune} */ - _makePrune (id: string, topic: string, doPX: boolean): ControlPrune { + _makePrune (id: string, topic: string, doPX: boolean): RPC.IControlPrune { if (this.peers.get(id)!.protocol === constants.GossipsubIDv10) { // Gossipsub v1.0 -- no backoff, the peer won't be able to parse it anyway return { @@ -1340,7 +1336,7 @@ class Gossipsub extends Pubsub { // backoff is measured in seconds // GossipsubPruneBackoff is measured in milliseconds const backoff = constants.GossipsubPruneBackoff / 1000 - const px: PeerInfo[] = [] + const px: RPC.IPeerInfo[] = [] if (doPX) { // select peers for Peer eXchange const peers = getGossipPeers(this, topic, constants.GossipsubPrunePeers, (xid: string): boolean => { diff --git a/ts/message/index.ts b/ts/message/index.ts deleted file mode 100644 index 3959bafc..00000000 --- a/ts/message/index.ts +++ /dev/null @@ -1,130 +0,0 @@ -import rpcProtoStr from './rpc.proto' -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore -import protons = require('protons') - -const rpcProto = protons(rpcProtoStr) - -// These interfaces follow the protobuf definitions in ./rpc.proto.js - -/** - * Subscription Options - */ -export interface SubOpts { - /** - * Whether to subscribe of unsubscribe - * true for subscribe, false for unsubscribe - */ - subscribe?: boolean - /** - * Topic ID - */ - topicID?: string -} - -/** - * Pubsub message - */ -export interface Message { - /** - * Peer id of the author of the message - * - * Note: This is not necessarily the peer who sent the RPC this message is contained in - */ - from?: Uint8Array - /** - * Opaque blob of data - */ - data?: Uint8Array - /** - * 64-bit big-endian uint - * - * No two messages on a topic from the same peer should have the same seqno value - */ - seqno?: Uint8Array - /** - * Set of topics being published to - */ - topicIDs: string[] - /** - * Signature of the message - * - * The signature is computed over the marshalled message protobuf excluding the key field - * The protobuf bloc is prefixed by the string `libp2p-pubsub:` before signing - */ - signature?: Uint8Array - /** - * Signing key - */ - key?: Uint8Array -} - -type Overwrite = { - [P in Exclude]: T1[P] -} & T2; - -/** - * IHAVE control message - * Sent to notify a peer with a list of messages that were recently seen by the local router in the included topic id - */ -export interface ControlIHave { - topicID?: string - messageIDs: Uint8Array[] -} - -/** - * IWANT control message - * Sent to request the full content of messages whose IDs were announced by a remote peer in an IHAVE message - */ -export interface ControlIWant { - messageIDs: Uint8Array[] -} - -/** - * GRAFT control message - * Sent to notify a peer that it has been added to the local router's mesh for the included topic id - */ -export interface ControlGraft { - topicID?: string -} - -/** - * PRUNE control message - * Sent to notify a peer that it has been removed from the local router's mesh for the included topic id - */ -export interface ControlPrune { - topicID?: string - peers: PeerInfo[] - backoff?: number -} - -export interface PeerInfo { - peerID?: Uint8Array - signedPeerRecord?: Uint8Array -} - -/** - * Gossip control message container - */ -export interface ControlMessage { - ihave: ControlIHave[] - iwant: ControlIWant[] - graft: ControlGraft[] - prune: ControlPrune[] -} - -/** - * Gossipsub RPC message - */ -export interface RPC { - subscriptions: SubOpts[] - msgs: Message[] - control?: ControlMessage -} - -interface ProtoCodec { - encode(obj: T): Uint8Array - decode(buf: Uint8Array): T -} - -export const RPCCodec = rpcProto.RPC as ProtoCodec diff --git a/ts/message/rpc.d.ts b/ts/message/rpc.d.ts new file mode 100644 index 00000000..c35d73f5 --- /dev/null +++ b/ts/message/rpc.d.ts @@ -0,0 +1,624 @@ +import * as $protobuf from "protobufjs"; +/** Properties of a RPC. */ +export interface IRPC { + + /** RPC subscriptions */ + subscriptions?: (RPC.ISubOpts[]|null); + + /** RPC msgs */ + msgs?: (RPC.IMessage[]|null); + + /** RPC control */ + control?: (RPC.IControlMessage|null); +} + +/** Represents a RPC. */ +export class RPC implements IRPC { + + /** + * Constructs a new RPC. + * @param [p] Properties to set + */ + constructor(p?: IRPC); + + /** RPC subscriptions. */ + public subscriptions: RPC.ISubOpts[]; + + /** RPC msgs. */ + public msgs: RPC.IMessage[]; + + /** RPC control. */ + public control?: (RPC.IControlMessage|null); + + /** + * Encodes the specified RPC message. Does not implicitly {@link RPC.verify|verify} messages. + * @param m RPC message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: IRPC, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a RPC message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns RPC + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC; + + /** + * Creates a RPC message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns RPC + */ + public static fromObject(d: { [k: string]: any }): RPC; + + /** + * Creates a plain object from a RPC message. Also converts values to other types if specified. + * @param m RPC + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this RPC to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; +} + +export namespace RPC { + + /** Properties of a SubOpts. */ + interface ISubOpts { + + /** SubOpts subscribe */ + subscribe?: (boolean|null); + + /** SubOpts topicID */ + topicID?: (string|null); + } + + /** Represents a SubOpts. */ + class SubOpts implements ISubOpts { + + /** + * Constructs a new SubOpts. + * @param [p] Properties to set + */ + constructor(p?: RPC.ISubOpts); + + /** SubOpts subscribe. */ + public subscribe: boolean; + + /** SubOpts topicID. */ + public topicID: string; + + /** + * Encodes the specified SubOpts message. Does not implicitly {@link RPC.SubOpts.verify|verify} messages. + * @param m SubOpts message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.ISubOpts, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a SubOpts message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns SubOpts + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.SubOpts; + + /** + * Creates a SubOpts message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns SubOpts + */ + public static fromObject(d: { [k: string]: any }): RPC.SubOpts; + + /** + * Creates a plain object from a SubOpts message. Also converts values to other types if specified. + * @param m SubOpts + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.SubOpts, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this SubOpts to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** Properties of a Message. */ + interface IMessage { + + /** Message from */ + from?: (Uint8Array|null); + + /** Message data */ + data?: (Uint8Array|null); + + /** Message seqno */ + seqno?: (Uint8Array|null); + + /** Message topicIDs */ + topicIDs?: (string[]|null); + + /** Message signature */ + signature?: (Uint8Array|null); + + /** Message key */ + key?: (Uint8Array|null); + } + + /** Represents a Message. */ + class Message implements IMessage { + + /** + * Constructs a new Message. + * @param [p] Properties to set + */ + constructor(p?: RPC.IMessage); + + /** Message from. */ + public from: Uint8Array; + + /** Message data. */ + public data: Uint8Array; + + /** Message seqno. */ + public seqno: Uint8Array; + + /** Message topicIDs. */ + public topicIDs: string[]; + + /** Message signature. */ + public signature: Uint8Array; + + /** Message key. */ + public key: Uint8Array; + + /** + * Encodes the specified Message message. Does not implicitly {@link RPC.Message.verify|verify} messages. + * @param m Message message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.IMessage, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a Message message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns Message + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.Message; + + /** + * Creates a Message message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns Message + */ + public static fromObject(d: { [k: string]: any }): RPC.Message; + + /** + * Creates a plain object from a Message message. Also converts values to other types if specified. + * @param m Message + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.Message, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this Message to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** Properties of a ControlMessage. */ + interface IControlMessage { + + /** ControlMessage ihave */ + ihave?: (RPC.IControlIHave[]|null); + + /** ControlMessage iwant */ + iwant?: (RPC.IControlIWant[]|null); + + /** ControlMessage graft */ + graft?: (RPC.IControlGraft[]|null); + + /** ControlMessage prune */ + prune?: (RPC.IControlPrune[]|null); + } + + /** Represents a ControlMessage. */ + class ControlMessage implements IControlMessage { + + /** + * Constructs a new ControlMessage. + * @param [p] Properties to set + */ + constructor(p?: RPC.IControlMessage); + + /** ControlMessage ihave. */ + public ihave: RPC.IControlIHave[]; + + /** ControlMessage iwant. */ + public iwant: RPC.IControlIWant[]; + + /** ControlMessage graft. */ + public graft: RPC.IControlGraft[]; + + /** ControlMessage prune. */ + public prune: RPC.IControlPrune[]; + + /** + * Encodes the specified ControlMessage message. Does not implicitly {@link RPC.ControlMessage.verify|verify} messages. + * @param m ControlMessage message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.IControlMessage, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a ControlMessage message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns ControlMessage + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.ControlMessage; + + /** + * Creates a ControlMessage message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns ControlMessage + */ + public static fromObject(d: { [k: string]: any }): RPC.ControlMessage; + + /** + * Creates a plain object from a ControlMessage message. Also converts values to other types if specified. + * @param m ControlMessage + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.ControlMessage, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this ControlMessage to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** Properties of a ControlIHave. */ + interface IControlIHave { + + /** ControlIHave topicID */ + topicID?: (string|null); + + /** ControlIHave messageIDs */ + messageIDs?: (Uint8Array[]|null); + } + + /** Represents a ControlIHave. */ + class ControlIHave implements IControlIHave { + + /** + * Constructs a new ControlIHave. + * @param [p] Properties to set + */ + constructor(p?: RPC.IControlIHave); + + /** ControlIHave topicID. */ + public topicID: string; + + /** ControlIHave messageIDs. */ + public messageIDs: Uint8Array[]; + + /** + * Encodes the specified ControlIHave message. Does not implicitly {@link RPC.ControlIHave.verify|verify} messages. + * @param m ControlIHave message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.IControlIHave, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a ControlIHave message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns ControlIHave + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.ControlIHave; + + /** + * Creates a ControlIHave message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns ControlIHave + */ + public static fromObject(d: { [k: string]: any }): RPC.ControlIHave; + + /** + * Creates a plain object from a ControlIHave message. Also converts values to other types if specified. + * @param m ControlIHave + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.ControlIHave, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this ControlIHave to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** Properties of a ControlIWant. */ + interface IControlIWant { + + /** ControlIWant messageIDs */ + messageIDs?: (Uint8Array[]|null); + } + + /** Represents a ControlIWant. */ + class ControlIWant implements IControlIWant { + + /** + * Constructs a new ControlIWant. + * @param [p] Properties to set + */ + constructor(p?: RPC.IControlIWant); + + /** ControlIWant messageIDs. */ + public messageIDs: Uint8Array[]; + + /** + * Encodes the specified ControlIWant message. Does not implicitly {@link RPC.ControlIWant.verify|verify} messages. + * @param m ControlIWant message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.IControlIWant, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a ControlIWant message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns ControlIWant + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.ControlIWant; + + /** + * Creates a ControlIWant message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns ControlIWant + */ + public static fromObject(d: { [k: string]: any }): RPC.ControlIWant; + + /** + * Creates a plain object from a ControlIWant message. Also converts values to other types if specified. + * @param m ControlIWant + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.ControlIWant, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this ControlIWant to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** Properties of a ControlGraft. */ + interface IControlGraft { + + /** ControlGraft topicID */ + topicID?: (string|null); + } + + /** Represents a ControlGraft. */ + class ControlGraft implements IControlGraft { + + /** + * Constructs a new ControlGraft. + * @param [p] Properties to set + */ + constructor(p?: RPC.IControlGraft); + + /** ControlGraft topicID. */ + public topicID: string; + + /** + * Encodes the specified ControlGraft message. Does not implicitly {@link RPC.ControlGraft.verify|verify} messages. + * @param m ControlGraft message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.IControlGraft, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a ControlGraft message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns ControlGraft + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.ControlGraft; + + /** + * Creates a ControlGraft message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns ControlGraft + */ + public static fromObject(d: { [k: string]: any }): RPC.ControlGraft; + + /** + * Creates a plain object from a ControlGraft message. Also converts values to other types if specified. + * @param m ControlGraft + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.ControlGraft, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this ControlGraft to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** Properties of a ControlPrune. */ + interface IControlPrune { + + /** ControlPrune topicID */ + topicID?: (string|null); + + /** ControlPrune peers */ + peers?: (RPC.IPeerInfo[]|null); + + /** ControlPrune backoff */ + backoff?: (number|null); + } + + /** Represents a ControlPrune. */ + class ControlPrune implements IControlPrune { + + /** + * Constructs a new ControlPrune. + * @param [p] Properties to set + */ + constructor(p?: RPC.IControlPrune); + + /** ControlPrune topicID. */ + public topicID: string; + + /** ControlPrune peers. */ + public peers: RPC.IPeerInfo[]; + + /** ControlPrune backoff. */ + public backoff: number; + + /** + * Encodes the specified ControlPrune message. Does not implicitly {@link RPC.ControlPrune.verify|verify} messages. + * @param m ControlPrune message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.IControlPrune, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a ControlPrune message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns ControlPrune + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.ControlPrune; + + /** + * Creates a ControlPrune message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns ControlPrune + */ + public static fromObject(d: { [k: string]: any }): RPC.ControlPrune; + + /** + * Creates a plain object from a ControlPrune message. Also converts values to other types if specified. + * @param m ControlPrune + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.ControlPrune, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this ControlPrune to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** Properties of a PeerInfo. */ + interface IPeerInfo { + + /** PeerInfo peerID */ + peerID?: (Uint8Array|null); + + /** PeerInfo signedPeerRecord */ + signedPeerRecord?: (Uint8Array|null); + } + + /** Represents a PeerInfo. */ + class PeerInfo implements IPeerInfo { + + /** + * Constructs a new PeerInfo. + * @param [p] Properties to set + */ + constructor(p?: RPC.IPeerInfo); + + /** PeerInfo peerID. */ + public peerID: Uint8Array; + + /** PeerInfo signedPeerRecord. */ + public signedPeerRecord: Uint8Array; + + /** + * Encodes the specified PeerInfo message. Does not implicitly {@link RPC.PeerInfo.verify|verify} messages. + * @param m PeerInfo message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: RPC.IPeerInfo, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a PeerInfo message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns PeerInfo + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.PeerInfo; + + /** + * Creates a PeerInfo message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns PeerInfo + */ + public static fromObject(d: { [k: string]: any }): RPC.PeerInfo; + + /** + * Creates a plain object from a PeerInfo message. Also converts values to other types if specified. + * @param m PeerInfo + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: RPC.PeerInfo, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this PeerInfo to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } +} diff --git a/ts/message/rpc.js b/ts/message/rpc.js new file mode 100644 index 00000000..6af31bb9 --- /dev/null +++ b/ts/message/rpc.js @@ -0,0 +1,1760 @@ +/*eslint-disable*/ +(function(global, factory) { /* global define, require, module */ + + /* AMD */ if (typeof define === 'function' && define.amd) + define(["protobufjs/minimal"], factory); + + /* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports) + module.exports = factory(require("protobufjs/minimal")); + +})(this, function($protobuf) { + "use strict"; + + // Common aliases + var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; + + // Exported root namespace + var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {}); + + $root.RPC = (function() { + + /** + * Properties of a RPC. + * @exports IRPC + * @interface IRPC + * @property {Array.|null} [subscriptions] RPC subscriptions + * @property {Array.|null} [msgs] RPC msgs + * @property {RPC.IControlMessage|null} [control] RPC control + */ + + /** + * Constructs a new RPC. + * @exports RPC + * @classdesc Represents a RPC. + * @implements IRPC + * @constructor + * @param {IRPC=} [p] Properties to set + */ + function RPC(p) { + this.subscriptions = []; + this.msgs = []; + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * RPC subscriptions. + * @member {Array.} subscriptions + * @memberof RPC + * @instance + */ + RPC.prototype.subscriptions = $util.emptyArray; + + /** + * RPC msgs. + * @member {Array.} msgs + * @memberof RPC + * @instance + */ + RPC.prototype.msgs = $util.emptyArray; + + /** + * RPC control. + * @member {RPC.IControlMessage|null|undefined} control + * @memberof RPC + * @instance + */ + RPC.prototype.control = null; + + /** + * Encodes the specified RPC message. Does not implicitly {@link RPC.verify|verify} messages. + * @function encode + * @memberof RPC + * @static + * @param {IRPC} m RPC message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + RPC.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.subscriptions != null && m.subscriptions.length) { + for (var i = 0; i < m.subscriptions.length; ++i) + $root.RPC.SubOpts.encode(m.subscriptions[i], w.uint32(10).fork()).ldelim(); + } + if (m.msgs != null && m.msgs.length) { + for (var i = 0; i < m.msgs.length; ++i) + $root.RPC.Message.encode(m.msgs[i], w.uint32(18).fork()).ldelim(); + } + if (m.control != null && Object.hasOwnProperty.call(m, "control")) + $root.RPC.ControlMessage.encode(m.control, w.uint32(26).fork()).ldelim(); + return w; + }; + + /** + * Decodes a RPC message from the specified reader or buffer. + * @function decode + * @memberof RPC + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC} RPC + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + RPC.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + if (!(m.subscriptions && m.subscriptions.length)) + m.subscriptions = []; + m.subscriptions.push($root.RPC.SubOpts.decode(r, r.uint32())); + break; + case 2: + if (!(m.msgs && m.msgs.length)) + m.msgs = []; + m.msgs.push($root.RPC.Message.decode(r, r.uint32())); + break; + case 3: + m.control = $root.RPC.ControlMessage.decode(r, r.uint32()); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a RPC message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC + * @static + * @param {Object.} d Plain object + * @returns {RPC} RPC + */ + RPC.fromObject = function fromObject(d) { + if (d instanceof $root.RPC) + return d; + var m = new $root.RPC(); + if (d.subscriptions) { + if (!Array.isArray(d.subscriptions)) + throw TypeError(".RPC.subscriptions: array expected"); + m.subscriptions = []; + for (var i = 0; i < d.subscriptions.length; ++i) { + if (typeof d.subscriptions[i] !== "object") + throw TypeError(".RPC.subscriptions: object expected"); + m.subscriptions[i] = $root.RPC.SubOpts.fromObject(d.subscriptions[i]); + } + } + if (d.msgs) { + if (!Array.isArray(d.msgs)) + throw TypeError(".RPC.msgs: array expected"); + m.msgs = []; + for (var i = 0; i < d.msgs.length; ++i) { + if (typeof d.msgs[i] !== "object") + throw TypeError(".RPC.msgs: object expected"); + m.msgs[i] = $root.RPC.Message.fromObject(d.msgs[i]); + } + } + if (d.control != null) { + if (typeof d.control !== "object") + throw TypeError(".RPC.control: object expected"); + m.control = $root.RPC.ControlMessage.fromObject(d.control); + } + return m; + }; + + /** + * Creates a plain object from a RPC message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC + * @static + * @param {RPC} m RPC + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + RPC.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.arrays || o.defaults) { + d.subscriptions = []; + d.msgs = []; + } + if (o.defaults) { + d.control = null; + } + if (m.subscriptions && m.subscriptions.length) { + d.subscriptions = []; + for (var j = 0; j < m.subscriptions.length; ++j) { + d.subscriptions[j] = $root.RPC.SubOpts.toObject(m.subscriptions[j], o); + } + } + if (m.msgs && m.msgs.length) { + d.msgs = []; + for (var j = 0; j < m.msgs.length; ++j) { + d.msgs[j] = $root.RPC.Message.toObject(m.msgs[j], o); + } + } + if (m.control != null && m.hasOwnProperty("control")) { + d.control = $root.RPC.ControlMessage.toObject(m.control, o); + } + return d; + }; + + /** + * Converts this RPC to JSON. + * @function toJSON + * @memberof RPC + * @instance + * @returns {Object.} JSON object + */ + RPC.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + RPC.SubOpts = (function() { + + /** + * Properties of a SubOpts. + * @memberof RPC + * @interface ISubOpts + * @property {boolean|null} [subscribe] SubOpts subscribe + * @property {string|null} [topicID] SubOpts topicID + */ + + /** + * Constructs a new SubOpts. + * @memberof RPC + * @classdesc Represents a SubOpts. + * @implements ISubOpts + * @constructor + * @param {RPC.ISubOpts=} [p] Properties to set + */ + function SubOpts(p) { + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * SubOpts subscribe. + * @member {boolean} subscribe + * @memberof RPC.SubOpts + * @instance + */ + SubOpts.prototype.subscribe = false; + + /** + * SubOpts topicID. + * @member {string} topicID + * @memberof RPC.SubOpts + * @instance + */ + SubOpts.prototype.topicID = ""; + + /** + * Encodes the specified SubOpts message. Does not implicitly {@link RPC.SubOpts.verify|verify} messages. + * @function encode + * @memberof RPC.SubOpts + * @static + * @param {RPC.ISubOpts} m SubOpts message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + SubOpts.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.subscribe != null && Object.hasOwnProperty.call(m, "subscribe")) + w.uint32(8).bool(m.subscribe); + if (m.topicID != null && Object.hasOwnProperty.call(m, "topicID")) + w.uint32(18).string(m.topicID); + return w; + }; + + /** + * Decodes a SubOpts message from the specified reader or buffer. + * @function decode + * @memberof RPC.SubOpts + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.SubOpts} SubOpts + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + SubOpts.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.SubOpts(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.subscribe = r.bool(); + break; + case 2: + m.topicID = r.string(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a SubOpts message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.SubOpts + * @static + * @param {Object.} d Plain object + * @returns {RPC.SubOpts} SubOpts + */ + SubOpts.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.SubOpts) + return d; + var m = new $root.RPC.SubOpts(); + if (d.subscribe != null) { + m.subscribe = Boolean(d.subscribe); + } + if (d.topicID != null) { + m.topicID = String(d.topicID); + } + return m; + }; + + /** + * Creates a plain object from a SubOpts message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.SubOpts + * @static + * @param {RPC.SubOpts} m SubOpts + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + SubOpts.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.defaults) { + d.subscribe = false; + d.topicID = ""; + } + if (m.subscribe != null && m.hasOwnProperty("subscribe")) { + d.subscribe = m.subscribe; + } + if (m.topicID != null && m.hasOwnProperty("topicID")) { + d.topicID = m.topicID; + } + return d; + }; + + /** + * Converts this SubOpts to JSON. + * @function toJSON + * @memberof RPC.SubOpts + * @instance + * @returns {Object.} JSON object + */ + SubOpts.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return SubOpts; + })(); + + RPC.Message = (function() { + + /** + * Properties of a Message. + * @memberof RPC + * @interface IMessage + * @property {Uint8Array|null} [from] Message from + * @property {Uint8Array|null} [data] Message data + * @property {Uint8Array|null} [seqno] Message seqno + * @property {Array.|null} [topicIDs] Message topicIDs + * @property {Uint8Array|null} [signature] Message signature + * @property {Uint8Array|null} [key] Message key + */ + + /** + * Constructs a new Message. + * @memberof RPC + * @classdesc Represents a Message. + * @implements IMessage + * @constructor + * @param {RPC.IMessage=} [p] Properties to set + */ + function Message(p) { + this.topicIDs = []; + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * Message from. + * @member {Uint8Array} from + * @memberof RPC.Message + * @instance + */ + Message.prototype.from = $util.newBuffer([]); + + /** + * Message data. + * @member {Uint8Array} data + * @memberof RPC.Message + * @instance + */ + Message.prototype.data = $util.newBuffer([]); + + /** + * Message seqno. + * @member {Uint8Array} seqno + * @memberof RPC.Message + * @instance + */ + Message.prototype.seqno = $util.newBuffer([]); + + /** + * Message topicIDs. + * @member {Array.} topicIDs + * @memberof RPC.Message + * @instance + */ + Message.prototype.topicIDs = $util.emptyArray; + + /** + * Message signature. + * @member {Uint8Array} signature + * @memberof RPC.Message + * @instance + */ + Message.prototype.signature = $util.newBuffer([]); + + /** + * Message key. + * @member {Uint8Array} key + * @memberof RPC.Message + * @instance + */ + Message.prototype.key = $util.newBuffer([]); + + /** + * Encodes the specified Message message. Does not implicitly {@link RPC.Message.verify|verify} messages. + * @function encode + * @memberof RPC.Message + * @static + * @param {RPC.IMessage} m Message message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + Message.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.from != null && Object.hasOwnProperty.call(m, "from")) + w.uint32(10).bytes(m.from); + if (m.data != null && Object.hasOwnProperty.call(m, "data")) + w.uint32(18).bytes(m.data); + if (m.seqno != null && Object.hasOwnProperty.call(m, "seqno")) + w.uint32(26).bytes(m.seqno); + if (m.topicIDs != null && m.topicIDs.length) { + for (var i = 0; i < m.topicIDs.length; ++i) + w.uint32(34).string(m.topicIDs[i]); + } + if (m.signature != null && Object.hasOwnProperty.call(m, "signature")) + w.uint32(42).bytes(m.signature); + if (m.key != null && Object.hasOwnProperty.call(m, "key")) + w.uint32(50).bytes(m.key); + return w; + }; + + /** + * Decodes a Message message from the specified reader or buffer. + * @function decode + * @memberof RPC.Message + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.Message} Message + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + Message.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.Message(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.from = r.bytes(); + break; + case 2: + m.data = r.bytes(); + break; + case 3: + m.seqno = r.bytes(); + break; + case 4: + if (!(m.topicIDs && m.topicIDs.length)) + m.topicIDs = []; + m.topicIDs.push(r.string()); + break; + case 5: + m.signature = r.bytes(); + break; + case 6: + m.key = r.bytes(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a Message message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.Message + * @static + * @param {Object.} d Plain object + * @returns {RPC.Message} Message + */ + Message.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.Message) + return d; + var m = new $root.RPC.Message(); + if (d.from != null) { + if (typeof d.from === "string") + $util.base64.decode(d.from, m.from = $util.newBuffer($util.base64.length(d.from)), 0); + else if (d.from.length) + m.from = d.from; + } + if (d.data != null) { + if (typeof d.data === "string") + $util.base64.decode(d.data, m.data = $util.newBuffer($util.base64.length(d.data)), 0); + else if (d.data.length) + m.data = d.data; + } + if (d.seqno != null) { + if (typeof d.seqno === "string") + $util.base64.decode(d.seqno, m.seqno = $util.newBuffer($util.base64.length(d.seqno)), 0); + else if (d.seqno.length) + m.seqno = d.seqno; + } + if (d.topicIDs) { + if (!Array.isArray(d.topicIDs)) + throw TypeError(".RPC.Message.topicIDs: array expected"); + m.topicIDs = []; + for (var i = 0; i < d.topicIDs.length; ++i) { + m.topicIDs[i] = String(d.topicIDs[i]); + } + } + if (d.signature != null) { + if (typeof d.signature === "string") + $util.base64.decode(d.signature, m.signature = $util.newBuffer($util.base64.length(d.signature)), 0); + else if (d.signature.length) + m.signature = d.signature; + } + if (d.key != null) { + if (typeof d.key === "string") + $util.base64.decode(d.key, m.key = $util.newBuffer($util.base64.length(d.key)), 0); + else if (d.key.length) + m.key = d.key; + } + return m; + }; + + /** + * Creates a plain object from a Message message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.Message + * @static + * @param {RPC.Message} m Message + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + Message.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.arrays || o.defaults) { + d.topicIDs = []; + } + if (o.defaults) { + if (o.bytes === String) + d.from = ""; + else { + d.from = []; + if (o.bytes !== Array) + d.from = $util.newBuffer(d.from); + } + if (o.bytes === String) + d.data = ""; + else { + d.data = []; + if (o.bytes !== Array) + d.data = $util.newBuffer(d.data); + } + if (o.bytes === String) + d.seqno = ""; + else { + d.seqno = []; + if (o.bytes !== Array) + d.seqno = $util.newBuffer(d.seqno); + } + if (o.bytes === String) + d.signature = ""; + else { + d.signature = []; + if (o.bytes !== Array) + d.signature = $util.newBuffer(d.signature); + } + if (o.bytes === String) + d.key = ""; + else { + d.key = []; + if (o.bytes !== Array) + d.key = $util.newBuffer(d.key); + } + } + if (m.from != null && m.hasOwnProperty("from")) { + d.from = o.bytes === String ? $util.base64.encode(m.from, 0, m.from.length) : o.bytes === Array ? Array.prototype.slice.call(m.from) : m.from; + } + if (m.data != null && m.hasOwnProperty("data")) { + d.data = o.bytes === String ? $util.base64.encode(m.data, 0, m.data.length) : o.bytes === Array ? Array.prototype.slice.call(m.data) : m.data; + } + if (m.seqno != null && m.hasOwnProperty("seqno")) { + d.seqno = o.bytes === String ? $util.base64.encode(m.seqno, 0, m.seqno.length) : o.bytes === Array ? Array.prototype.slice.call(m.seqno) : m.seqno; + } + if (m.topicIDs && m.topicIDs.length) { + d.topicIDs = []; + for (var j = 0; j < m.topicIDs.length; ++j) { + d.topicIDs[j] = m.topicIDs[j]; + } + } + if (m.signature != null && m.hasOwnProperty("signature")) { + d.signature = o.bytes === String ? $util.base64.encode(m.signature, 0, m.signature.length) : o.bytes === Array ? Array.prototype.slice.call(m.signature) : m.signature; + } + if (m.key != null && m.hasOwnProperty("key")) { + d.key = o.bytes === String ? $util.base64.encode(m.key, 0, m.key.length) : o.bytes === Array ? Array.prototype.slice.call(m.key) : m.key; + } + return d; + }; + + /** + * Converts this Message to JSON. + * @function toJSON + * @memberof RPC.Message + * @instance + * @returns {Object.} JSON object + */ + Message.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return Message; + })(); + + RPC.ControlMessage = (function() { + + /** + * Properties of a ControlMessage. + * @memberof RPC + * @interface IControlMessage + * @property {Array.|null} [ihave] ControlMessage ihave + * @property {Array.|null} [iwant] ControlMessage iwant + * @property {Array.|null} [graft] ControlMessage graft + * @property {Array.|null} [prune] ControlMessage prune + */ + + /** + * Constructs a new ControlMessage. + * @memberof RPC + * @classdesc Represents a ControlMessage. + * @implements IControlMessage + * @constructor + * @param {RPC.IControlMessage=} [p] Properties to set + */ + function ControlMessage(p) { + this.ihave = []; + this.iwant = []; + this.graft = []; + this.prune = []; + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * ControlMessage ihave. + * @member {Array.} ihave + * @memberof RPC.ControlMessage + * @instance + */ + ControlMessage.prototype.ihave = $util.emptyArray; + + /** + * ControlMessage iwant. + * @member {Array.} iwant + * @memberof RPC.ControlMessage + * @instance + */ + ControlMessage.prototype.iwant = $util.emptyArray; + + /** + * ControlMessage graft. + * @member {Array.} graft + * @memberof RPC.ControlMessage + * @instance + */ + ControlMessage.prototype.graft = $util.emptyArray; + + /** + * ControlMessage prune. + * @member {Array.} prune + * @memberof RPC.ControlMessage + * @instance + */ + ControlMessage.prototype.prune = $util.emptyArray; + + /** + * Encodes the specified ControlMessage message. Does not implicitly {@link RPC.ControlMessage.verify|verify} messages. + * @function encode + * @memberof RPC.ControlMessage + * @static + * @param {RPC.IControlMessage} m ControlMessage message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + ControlMessage.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.ihave != null && m.ihave.length) { + for (var i = 0; i < m.ihave.length; ++i) + $root.RPC.ControlIHave.encode(m.ihave[i], w.uint32(10).fork()).ldelim(); + } + if (m.iwant != null && m.iwant.length) { + for (var i = 0; i < m.iwant.length; ++i) + $root.RPC.ControlIWant.encode(m.iwant[i], w.uint32(18).fork()).ldelim(); + } + if (m.graft != null && m.graft.length) { + for (var i = 0; i < m.graft.length; ++i) + $root.RPC.ControlGraft.encode(m.graft[i], w.uint32(26).fork()).ldelim(); + } + if (m.prune != null && m.prune.length) { + for (var i = 0; i < m.prune.length; ++i) + $root.RPC.ControlPrune.encode(m.prune[i], w.uint32(34).fork()).ldelim(); + } + return w; + }; + + /** + * Decodes a ControlMessage message from the specified reader or buffer. + * @function decode + * @memberof RPC.ControlMessage + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.ControlMessage} ControlMessage + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + ControlMessage.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.ControlMessage(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + if (!(m.ihave && m.ihave.length)) + m.ihave = []; + m.ihave.push($root.RPC.ControlIHave.decode(r, r.uint32())); + break; + case 2: + if (!(m.iwant && m.iwant.length)) + m.iwant = []; + m.iwant.push($root.RPC.ControlIWant.decode(r, r.uint32())); + break; + case 3: + if (!(m.graft && m.graft.length)) + m.graft = []; + m.graft.push($root.RPC.ControlGraft.decode(r, r.uint32())); + break; + case 4: + if (!(m.prune && m.prune.length)) + m.prune = []; + m.prune.push($root.RPC.ControlPrune.decode(r, r.uint32())); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a ControlMessage message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.ControlMessage + * @static + * @param {Object.} d Plain object + * @returns {RPC.ControlMessage} ControlMessage + */ + ControlMessage.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.ControlMessage) + return d; + var m = new $root.RPC.ControlMessage(); + if (d.ihave) { + if (!Array.isArray(d.ihave)) + throw TypeError(".RPC.ControlMessage.ihave: array expected"); + m.ihave = []; + for (var i = 0; i < d.ihave.length; ++i) { + if (typeof d.ihave[i] !== "object") + throw TypeError(".RPC.ControlMessage.ihave: object expected"); + m.ihave[i] = $root.RPC.ControlIHave.fromObject(d.ihave[i]); + } + } + if (d.iwant) { + if (!Array.isArray(d.iwant)) + throw TypeError(".RPC.ControlMessage.iwant: array expected"); + m.iwant = []; + for (var i = 0; i < d.iwant.length; ++i) { + if (typeof d.iwant[i] !== "object") + throw TypeError(".RPC.ControlMessage.iwant: object expected"); + m.iwant[i] = $root.RPC.ControlIWant.fromObject(d.iwant[i]); + } + } + if (d.graft) { + if (!Array.isArray(d.graft)) + throw TypeError(".RPC.ControlMessage.graft: array expected"); + m.graft = []; + for (var i = 0; i < d.graft.length; ++i) { + if (typeof d.graft[i] !== "object") + throw TypeError(".RPC.ControlMessage.graft: object expected"); + m.graft[i] = $root.RPC.ControlGraft.fromObject(d.graft[i]); + } + } + if (d.prune) { + if (!Array.isArray(d.prune)) + throw TypeError(".RPC.ControlMessage.prune: array expected"); + m.prune = []; + for (var i = 0; i < d.prune.length; ++i) { + if (typeof d.prune[i] !== "object") + throw TypeError(".RPC.ControlMessage.prune: object expected"); + m.prune[i] = $root.RPC.ControlPrune.fromObject(d.prune[i]); + } + } + return m; + }; + + /** + * Creates a plain object from a ControlMessage message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.ControlMessage + * @static + * @param {RPC.ControlMessage} m ControlMessage + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + ControlMessage.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.arrays || o.defaults) { + d.ihave = []; + d.iwant = []; + d.graft = []; + d.prune = []; + } + if (m.ihave && m.ihave.length) { + d.ihave = []; + for (var j = 0; j < m.ihave.length; ++j) { + d.ihave[j] = $root.RPC.ControlIHave.toObject(m.ihave[j], o); + } + } + if (m.iwant && m.iwant.length) { + d.iwant = []; + for (var j = 0; j < m.iwant.length; ++j) { + d.iwant[j] = $root.RPC.ControlIWant.toObject(m.iwant[j], o); + } + } + if (m.graft && m.graft.length) { + d.graft = []; + for (var j = 0; j < m.graft.length; ++j) { + d.graft[j] = $root.RPC.ControlGraft.toObject(m.graft[j], o); + } + } + if (m.prune && m.prune.length) { + d.prune = []; + for (var j = 0; j < m.prune.length; ++j) { + d.prune[j] = $root.RPC.ControlPrune.toObject(m.prune[j], o); + } + } + return d; + }; + + /** + * Converts this ControlMessage to JSON. + * @function toJSON + * @memberof RPC.ControlMessage + * @instance + * @returns {Object.} JSON object + */ + ControlMessage.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return ControlMessage; + })(); + + RPC.ControlIHave = (function() { + + /** + * Properties of a ControlIHave. + * @memberof RPC + * @interface IControlIHave + * @property {string|null} [topicID] ControlIHave topicID + * @property {Array.|null} [messageIDs] ControlIHave messageIDs + */ + + /** + * Constructs a new ControlIHave. + * @memberof RPC + * @classdesc Represents a ControlIHave. + * @implements IControlIHave + * @constructor + * @param {RPC.IControlIHave=} [p] Properties to set + */ + function ControlIHave(p) { + this.messageIDs = []; + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * ControlIHave topicID. + * @member {string} topicID + * @memberof RPC.ControlIHave + * @instance + */ + ControlIHave.prototype.topicID = ""; + + /** + * ControlIHave messageIDs. + * @member {Array.} messageIDs + * @memberof RPC.ControlIHave + * @instance + */ + ControlIHave.prototype.messageIDs = $util.emptyArray; + + /** + * Encodes the specified ControlIHave message. Does not implicitly {@link RPC.ControlIHave.verify|verify} messages. + * @function encode + * @memberof RPC.ControlIHave + * @static + * @param {RPC.IControlIHave} m ControlIHave message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + ControlIHave.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.topicID != null && Object.hasOwnProperty.call(m, "topicID")) + w.uint32(10).string(m.topicID); + if (m.messageIDs != null && m.messageIDs.length) { + for (var i = 0; i < m.messageIDs.length; ++i) + w.uint32(18).bytes(m.messageIDs[i]); + } + return w; + }; + + /** + * Decodes a ControlIHave message from the specified reader or buffer. + * @function decode + * @memberof RPC.ControlIHave + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.ControlIHave} ControlIHave + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + ControlIHave.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.ControlIHave(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.topicID = r.string(); + break; + case 2: + if (!(m.messageIDs && m.messageIDs.length)) + m.messageIDs = []; + m.messageIDs.push(r.bytes()); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a ControlIHave message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.ControlIHave + * @static + * @param {Object.} d Plain object + * @returns {RPC.ControlIHave} ControlIHave + */ + ControlIHave.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.ControlIHave) + return d; + var m = new $root.RPC.ControlIHave(); + if (d.topicID != null) { + m.topicID = String(d.topicID); + } + if (d.messageIDs) { + if (!Array.isArray(d.messageIDs)) + throw TypeError(".RPC.ControlIHave.messageIDs: array expected"); + m.messageIDs = []; + for (var i = 0; i < d.messageIDs.length; ++i) { + if (typeof d.messageIDs[i] === "string") + $util.base64.decode(d.messageIDs[i], m.messageIDs[i] = $util.newBuffer($util.base64.length(d.messageIDs[i])), 0); + else if (d.messageIDs[i].length) + m.messageIDs[i] = d.messageIDs[i]; + } + } + return m; + }; + + /** + * Creates a plain object from a ControlIHave message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.ControlIHave + * @static + * @param {RPC.ControlIHave} m ControlIHave + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + ControlIHave.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.arrays || o.defaults) { + d.messageIDs = []; + } + if (o.defaults) { + d.topicID = ""; + } + if (m.topicID != null && m.hasOwnProperty("topicID")) { + d.topicID = m.topicID; + } + if (m.messageIDs && m.messageIDs.length) { + d.messageIDs = []; + for (var j = 0; j < m.messageIDs.length; ++j) { + d.messageIDs[j] = o.bytes === String ? $util.base64.encode(m.messageIDs[j], 0, m.messageIDs[j].length) : o.bytes === Array ? Array.prototype.slice.call(m.messageIDs[j]) : m.messageIDs[j]; + } + } + return d; + }; + + /** + * Converts this ControlIHave to JSON. + * @function toJSON + * @memberof RPC.ControlIHave + * @instance + * @returns {Object.} JSON object + */ + ControlIHave.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return ControlIHave; + })(); + + RPC.ControlIWant = (function() { + + /** + * Properties of a ControlIWant. + * @memberof RPC + * @interface IControlIWant + * @property {Array.|null} [messageIDs] ControlIWant messageIDs + */ + + /** + * Constructs a new ControlIWant. + * @memberof RPC + * @classdesc Represents a ControlIWant. + * @implements IControlIWant + * @constructor + * @param {RPC.IControlIWant=} [p] Properties to set + */ + function ControlIWant(p) { + this.messageIDs = []; + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * ControlIWant messageIDs. + * @member {Array.} messageIDs + * @memberof RPC.ControlIWant + * @instance + */ + ControlIWant.prototype.messageIDs = $util.emptyArray; + + /** + * Encodes the specified ControlIWant message. Does not implicitly {@link RPC.ControlIWant.verify|verify} messages. + * @function encode + * @memberof RPC.ControlIWant + * @static + * @param {RPC.IControlIWant} m ControlIWant message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + ControlIWant.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.messageIDs != null && m.messageIDs.length) { + for (var i = 0; i < m.messageIDs.length; ++i) + w.uint32(10).bytes(m.messageIDs[i]); + } + return w; + }; + + /** + * Decodes a ControlIWant message from the specified reader or buffer. + * @function decode + * @memberof RPC.ControlIWant + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.ControlIWant} ControlIWant + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + ControlIWant.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.ControlIWant(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + if (!(m.messageIDs && m.messageIDs.length)) + m.messageIDs = []; + m.messageIDs.push(r.bytes()); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a ControlIWant message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.ControlIWant + * @static + * @param {Object.} d Plain object + * @returns {RPC.ControlIWant} ControlIWant + */ + ControlIWant.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.ControlIWant) + return d; + var m = new $root.RPC.ControlIWant(); + if (d.messageIDs) { + if (!Array.isArray(d.messageIDs)) + throw TypeError(".RPC.ControlIWant.messageIDs: array expected"); + m.messageIDs = []; + for (var i = 0; i < d.messageIDs.length; ++i) { + if (typeof d.messageIDs[i] === "string") + $util.base64.decode(d.messageIDs[i], m.messageIDs[i] = $util.newBuffer($util.base64.length(d.messageIDs[i])), 0); + else if (d.messageIDs[i].length) + m.messageIDs[i] = d.messageIDs[i]; + } + } + return m; + }; + + /** + * Creates a plain object from a ControlIWant message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.ControlIWant + * @static + * @param {RPC.ControlIWant} m ControlIWant + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + ControlIWant.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.arrays || o.defaults) { + d.messageIDs = []; + } + if (m.messageIDs && m.messageIDs.length) { + d.messageIDs = []; + for (var j = 0; j < m.messageIDs.length; ++j) { + d.messageIDs[j] = o.bytes === String ? $util.base64.encode(m.messageIDs[j], 0, m.messageIDs[j].length) : o.bytes === Array ? Array.prototype.slice.call(m.messageIDs[j]) : m.messageIDs[j]; + } + } + return d; + }; + + /** + * Converts this ControlIWant to JSON. + * @function toJSON + * @memberof RPC.ControlIWant + * @instance + * @returns {Object.} JSON object + */ + ControlIWant.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return ControlIWant; + })(); + + RPC.ControlGraft = (function() { + + /** + * Properties of a ControlGraft. + * @memberof RPC + * @interface IControlGraft + * @property {string|null} [topicID] ControlGraft topicID + */ + + /** + * Constructs a new ControlGraft. + * @memberof RPC + * @classdesc Represents a ControlGraft. + * @implements IControlGraft + * @constructor + * @param {RPC.IControlGraft=} [p] Properties to set + */ + function ControlGraft(p) { + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * ControlGraft topicID. + * @member {string} topicID + * @memberof RPC.ControlGraft + * @instance + */ + ControlGraft.prototype.topicID = ""; + + /** + * Encodes the specified ControlGraft message. Does not implicitly {@link RPC.ControlGraft.verify|verify} messages. + * @function encode + * @memberof RPC.ControlGraft + * @static + * @param {RPC.IControlGraft} m ControlGraft message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + ControlGraft.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.topicID != null && Object.hasOwnProperty.call(m, "topicID")) + w.uint32(10).string(m.topicID); + return w; + }; + + /** + * Decodes a ControlGraft message from the specified reader or buffer. + * @function decode + * @memberof RPC.ControlGraft + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.ControlGraft} ControlGraft + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + ControlGraft.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.ControlGraft(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.topicID = r.string(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a ControlGraft message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.ControlGraft + * @static + * @param {Object.} d Plain object + * @returns {RPC.ControlGraft} ControlGraft + */ + ControlGraft.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.ControlGraft) + return d; + var m = new $root.RPC.ControlGraft(); + if (d.topicID != null) { + m.topicID = String(d.topicID); + } + return m; + }; + + /** + * Creates a plain object from a ControlGraft message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.ControlGraft + * @static + * @param {RPC.ControlGraft} m ControlGraft + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + ControlGraft.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.defaults) { + d.topicID = ""; + } + if (m.topicID != null && m.hasOwnProperty("topicID")) { + d.topicID = m.topicID; + } + return d; + }; + + /** + * Converts this ControlGraft to JSON. + * @function toJSON + * @memberof RPC.ControlGraft + * @instance + * @returns {Object.} JSON object + */ + ControlGraft.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return ControlGraft; + })(); + + RPC.ControlPrune = (function() { + + /** + * Properties of a ControlPrune. + * @memberof RPC + * @interface IControlPrune + * @property {string|null} [topicID] ControlPrune topicID + * @property {Array.|null} [peers] ControlPrune peers + * @property {number|null} [backoff] ControlPrune backoff + */ + + /** + * Constructs a new ControlPrune. + * @memberof RPC + * @classdesc Represents a ControlPrune. + * @implements IControlPrune + * @constructor + * @param {RPC.IControlPrune=} [p] Properties to set + */ + function ControlPrune(p) { + this.peers = []; + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * ControlPrune topicID. + * @member {string} topicID + * @memberof RPC.ControlPrune + * @instance + */ + ControlPrune.prototype.topicID = ""; + + /** + * ControlPrune peers. + * @member {Array.} peers + * @memberof RPC.ControlPrune + * @instance + */ + ControlPrune.prototype.peers = $util.emptyArray; + + /** + * ControlPrune backoff. + * @member {number} backoff + * @memberof RPC.ControlPrune + * @instance + */ + ControlPrune.prototype.backoff = $util.Long ? $util.Long.fromBits(0,0,true) : 0; + + /** + * Encodes the specified ControlPrune message. Does not implicitly {@link RPC.ControlPrune.verify|verify} messages. + * @function encode + * @memberof RPC.ControlPrune + * @static + * @param {RPC.IControlPrune} m ControlPrune message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + ControlPrune.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.topicID != null && Object.hasOwnProperty.call(m, "topicID")) + w.uint32(10).string(m.topicID); + if (m.peers != null && m.peers.length) { + for (var i = 0; i < m.peers.length; ++i) + $root.RPC.PeerInfo.encode(m.peers[i], w.uint32(18).fork()).ldelim(); + } + if (m.backoff != null && Object.hasOwnProperty.call(m, "backoff")) + w.uint32(24).uint64(m.backoff); + return w; + }; + + /** + * Decodes a ControlPrune message from the specified reader or buffer. + * @function decode + * @memberof RPC.ControlPrune + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.ControlPrune} ControlPrune + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + ControlPrune.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.ControlPrune(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.topicID = r.string(); + break; + case 2: + if (!(m.peers && m.peers.length)) + m.peers = []; + m.peers.push($root.RPC.PeerInfo.decode(r, r.uint32())); + break; + case 3: + m.backoff = r.uint64(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a ControlPrune message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.ControlPrune + * @static + * @param {Object.} d Plain object + * @returns {RPC.ControlPrune} ControlPrune + */ + ControlPrune.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.ControlPrune) + return d; + var m = new $root.RPC.ControlPrune(); + if (d.topicID != null) { + m.topicID = String(d.topicID); + } + if (d.peers) { + if (!Array.isArray(d.peers)) + throw TypeError(".RPC.ControlPrune.peers: array expected"); + m.peers = []; + for (var i = 0; i < d.peers.length; ++i) { + if (typeof d.peers[i] !== "object") + throw TypeError(".RPC.ControlPrune.peers: object expected"); + m.peers[i] = $root.RPC.PeerInfo.fromObject(d.peers[i]); + } + } + if (d.backoff != null) { + if ($util.Long) + (m.backoff = $util.Long.fromValue(d.backoff)).unsigned = true; + else if (typeof d.backoff === "string") + m.backoff = parseInt(d.backoff, 10); + else if (typeof d.backoff === "number") + m.backoff = d.backoff; + else if (typeof d.backoff === "object") + m.backoff = new $util.LongBits(d.backoff.low >>> 0, d.backoff.high >>> 0).toNumber(true); + } + return m; + }; + + /** + * Creates a plain object from a ControlPrune message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.ControlPrune + * @static + * @param {RPC.ControlPrune} m ControlPrune + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + ControlPrune.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.arrays || o.defaults) { + d.peers = []; + } + if (o.defaults) { + d.topicID = ""; + if ($util.Long) { + var n = new $util.Long(0, 0, true); + d.backoff = o.longs === String ? n.toString() : o.longs === Number ? n.toNumber() : n; + } else + d.backoff = o.longs === String ? "0" : 0; + } + if (m.topicID != null && m.hasOwnProperty("topicID")) { + d.topicID = m.topicID; + } + if (m.peers && m.peers.length) { + d.peers = []; + for (var j = 0; j < m.peers.length; ++j) { + d.peers[j] = $root.RPC.PeerInfo.toObject(m.peers[j], o); + } + } + if (m.backoff != null && m.hasOwnProperty("backoff")) { + if (typeof m.backoff === "number") + d.backoff = o.longs === String ? String(m.backoff) : m.backoff; + else + d.backoff = o.longs === String ? $util.Long.prototype.toString.call(m.backoff) : o.longs === Number ? new $util.LongBits(m.backoff.low >>> 0, m.backoff.high >>> 0).toNumber(true) : m.backoff; + } + return d; + }; + + /** + * Converts this ControlPrune to JSON. + * @function toJSON + * @memberof RPC.ControlPrune + * @instance + * @returns {Object.} JSON object + */ + ControlPrune.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return ControlPrune; + })(); + + RPC.PeerInfo = (function() { + + /** + * Properties of a PeerInfo. + * @memberof RPC + * @interface IPeerInfo + * @property {Uint8Array|null} [peerID] PeerInfo peerID + * @property {Uint8Array|null} [signedPeerRecord] PeerInfo signedPeerRecord + */ + + /** + * Constructs a new PeerInfo. + * @memberof RPC + * @classdesc Represents a PeerInfo. + * @implements IPeerInfo + * @constructor + * @param {RPC.IPeerInfo=} [p] Properties to set + */ + function PeerInfo(p) { + if (p) + for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) + if (p[ks[i]] != null) + this[ks[i]] = p[ks[i]]; + } + + /** + * PeerInfo peerID. + * @member {Uint8Array} peerID + * @memberof RPC.PeerInfo + * @instance + */ + PeerInfo.prototype.peerID = $util.newBuffer([]); + + /** + * PeerInfo signedPeerRecord. + * @member {Uint8Array} signedPeerRecord + * @memberof RPC.PeerInfo + * @instance + */ + PeerInfo.prototype.signedPeerRecord = $util.newBuffer([]); + + /** + * Encodes the specified PeerInfo message. Does not implicitly {@link RPC.PeerInfo.verify|verify} messages. + * @function encode + * @memberof RPC.PeerInfo + * @static + * @param {RPC.IPeerInfo} m PeerInfo message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + PeerInfo.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.peerID != null && Object.hasOwnProperty.call(m, "peerID")) + w.uint32(10).bytes(m.peerID); + if (m.signedPeerRecord != null && Object.hasOwnProperty.call(m, "signedPeerRecord")) + w.uint32(18).bytes(m.signedPeerRecord); + return w; + }; + + /** + * Decodes a PeerInfo message from the specified reader or buffer. + * @function decode + * @memberof RPC.PeerInfo + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {RPC.PeerInfo} PeerInfo + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + PeerInfo.decode = function decode(r, l) { + if (!(r instanceof $Reader)) + r = $Reader.create(r); + var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.PeerInfo(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.peerID = r.bytes(); + break; + case 2: + m.signedPeerRecord = r.bytes(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a PeerInfo message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof RPC.PeerInfo + * @static + * @param {Object.} d Plain object + * @returns {RPC.PeerInfo} PeerInfo + */ + PeerInfo.fromObject = function fromObject(d) { + if (d instanceof $root.RPC.PeerInfo) + return d; + var m = new $root.RPC.PeerInfo(); + if (d.peerID != null) { + if (typeof d.peerID === "string") + $util.base64.decode(d.peerID, m.peerID = $util.newBuffer($util.base64.length(d.peerID)), 0); + else if (d.peerID.length) + m.peerID = d.peerID; + } + if (d.signedPeerRecord != null) { + if (typeof d.signedPeerRecord === "string") + $util.base64.decode(d.signedPeerRecord, m.signedPeerRecord = $util.newBuffer($util.base64.length(d.signedPeerRecord)), 0); + else if (d.signedPeerRecord.length) + m.signedPeerRecord = d.signedPeerRecord; + } + return m; + }; + + /** + * Creates a plain object from a PeerInfo message. Also converts values to other types if specified. + * @function toObject + * @memberof RPC.PeerInfo + * @static + * @param {RPC.PeerInfo} m PeerInfo + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + PeerInfo.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.defaults) { + if (o.bytes === String) + d.peerID = ""; + else { + d.peerID = []; + if (o.bytes !== Array) + d.peerID = $util.newBuffer(d.peerID); + } + if (o.bytes === String) + d.signedPeerRecord = ""; + else { + d.signedPeerRecord = []; + if (o.bytes !== Array) + d.signedPeerRecord = $util.newBuffer(d.signedPeerRecord); + } + } + if (m.peerID != null && m.hasOwnProperty("peerID")) { + d.peerID = o.bytes === String ? $util.base64.encode(m.peerID, 0, m.peerID.length) : o.bytes === Array ? Array.prototype.slice.call(m.peerID) : m.peerID; + } + if (m.signedPeerRecord != null && m.hasOwnProperty("signedPeerRecord")) { + d.signedPeerRecord = o.bytes === String ? $util.base64.encode(m.signedPeerRecord, 0, m.signedPeerRecord.length) : o.bytes === Array ? Array.prototype.slice.call(m.signedPeerRecord) : m.signedPeerRecord; + } + return d; + }; + + /** + * Converts this PeerInfo to JSON. + * @function toJSON + * @memberof RPC.PeerInfo + * @instance + * @returns {Object.} JSON object + */ + PeerInfo.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return PeerInfo; + })(); + + return RPC; + })(); + + return $root; +}); diff --git a/ts/message/rpc.proto.ts b/ts/message/rpc.proto similarity index 98% rename from ts/message/rpc.proto.ts rename to ts/message/rpc.proto index 6dae3881..009d8242 100644 --- a/ts/message/rpc.proto.ts +++ b/ts/message/rpc.proto @@ -1,4 +1,5 @@ -export default ` +syntax = "proto3"; + message RPC { repeated SubOpts subscriptions = 1; repeated Message msgs = 2; @@ -48,4 +49,4 @@ message RPC { optional bytes peerID = 1; optional bytes signedPeerRecord = 2; } -}` +} \ No newline at end of file diff --git a/ts/utils/create-gossip-rpc.ts b/ts/utils/create-gossip-rpc.ts index b628fbca..43110f32 100644 --- a/ts/utils/create-gossip-rpc.ts +++ b/ts/utils/create-gossip-rpc.ts @@ -1,14 +1,14 @@ 'use strict' -import { RPC, Message, ControlMessage } from '../message' +import { RPC, IRPC } from '../message/rpc' /** * Create a gossipsub RPC object - * @param {Array} msgs - * @param {Partial} control - * @returns {RPC} + * @param {Array} msgs + * @param {Partial} control + * @returns {IRPC} */ -export function createGossipRpc (msgs: Message[] = [], control: Partial = {}): RPC { +export function createGossipRpc (msgs: RPC.IMessage[] = [], control: Partial = {}): IRPC { return { subscriptions: [], msgs: msgs,