From 107056582d8b90e5fc3ea78aac6ec7f89d955d56 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 14 Apr 2021 11:55:28 +0100 Subject: [PATCH 1/4] chore: replace protons with protobufjs Replaces protons with the official protobufjs module. --- package.json | 15 +- src/decision-engine/index.js | 2 +- src/decision-engine/ledger.js | 2 +- src/types/message/entry.js | 2 +- src/types/message/index.js | 61 +- src/types/message/message.d.ts | 379 ++++++++++ src/types/message/message.js | 1048 ++++++++++++++++++++++++++ src/types/message/message.proto | 43 ++ src/types/message/message.proto.d.ts | 73 -- src/types/message/message.proto.js | 48 -- src/types/wantlist/entry.js | 2 +- src/types/wantlist/index.js | 2 +- test/types/message.spec.js | 19 +- tsconfig.json | 3 + 14 files changed, 1524 insertions(+), 175 deletions(-) create mode 100644 src/types/message/message.d.ts create mode 100644 src/types/message/message.js create mode 100644 src/types/message/message.proto delete mode 100644 src/types/message/message.proto.d.ts delete mode 100644 src/types/message/message.proto.js diff --git a/package.json b/package.json index 4a1e52b3..d57bf3ba 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,10 @@ "src" ], "scripts": { - "prepare": "aegir build --no-bundle", + "build": "npm run build:proto && npm run build:proto-types && npm run build:bundle", + "build:proto": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/types/message/message.js src/types/message/message.proto", + "build:proto-types": "pbts -o src/types/message/message.d.ts src/types/message/message.js", + "build:bundle": "aegir build", "test": "aegir test", "test:browser": "aegir test -t browser -t webworker", "test:node": "aegir test -t node", @@ -63,13 +66,13 @@ "devDependencies": { "@nodeutils/defaults-deep": "^1.1.0", "@types/debug": "^4.1.5", - "aegir": "^31.0.4", + "aegir": "^33.1.0", "assert": "^2.0.0", "benchmark": "^2.1.4", "delay": "^5.0.0", "ipfs-repo": "^9.0.0", "ipfs-utils": "^6.0.1", - "iso-random-stream": "^1.1.1", + "iso-random-stream": "^2.0.0", "it-all": "^1.0.5", "it-drain": "^1.0.4", "libp2p": "^0.30.9", @@ -87,7 +90,7 @@ "peer-id": "^0.14.3", "promisify-es6": "^1.0.3", "rimraf": "^3.0.2", - "sinon": "^9.2.4", + "sinon": "^10.0.0", "stats-lite": "^2.2.0", "uuid": "^8.3.2" }, @@ -99,7 +102,7 @@ "debug": "^4.2.0", "ipfs-core-types": "^0.3.1", "ipld-block": "^0.11.0", - "it-length-prefixed": "^3.1.0", + "it-length-prefixed": "^5.0.2", "it-pipe": "^1.1.0", "just-debounce-it": "^1.1.0", "libp2p-interfaces": "^0.8.3", @@ -108,7 +111,7 @@ "multihashing-async": "^2.1.2", "native-abort-controller": "^1.0.3", "process": "^0.11.10", - "protons": "^2.0.0", + "protobufjs": "^6.10.2", "readable-stream": "^3.6.0", "streaming-iterables": "^5.0.4", "uint8arrays": "^2.1.3", diff --git a/src/decision-engine/index.js b/src/decision-engine/index.js index f2395cdd..39541acd 100644 --- a/src/decision-engine/index.js +++ b/src/decision-engine/index.js @@ -379,7 +379,7 @@ class DecisionEngine { /** * @private - * @param {import('../types/message/message.proto').WantType} wantType + * @param {import('../types/message/message').Message.Wantlist.WantType} wantType * @param {number} blockSize */ _sendAsBlock (wantType, blockSize) { diff --git a/src/decision-engine/ledger.js b/src/decision-engine/ledger.js index 48f4fe3e..17cad269 100644 --- a/src/decision-engine/ledger.js +++ b/src/decision-engine/ledger.js @@ -45,7 +45,7 @@ class Ledger { * * @param {CID} cid * @param {number} priority - * @param {import('../types/message/message.proto').WantType} wantType + * @param {import('../types/message/message').Message.Wantlist.WantType} wantType * @returns {void} */ wants (cid, priority, wantType) { diff --git a/src/types/message/entry.js b/src/types/message/entry.js index 65a509b6..14a61fb7 100644 --- a/src/types/message/entry.js +++ b/src/types/message/entry.js @@ -6,7 +6,7 @@ module.exports = class BitswapMessageEntry { /** * @param {import('cids')} cid * @param {number} priority - * @param {import('./message.proto').WantType} wantType + * @param {import('./message').Message.Wantlist.WantType} wantType * @param {boolean} [cancel] * @param {boolean} [sendDontHave] */ diff --git a/src/types/message/index.js b/src/types/message/index.js index 9d2e1ead..d8aea23a 100644 --- a/src/types/message/index.js +++ b/src/types/message/index.js @@ -7,7 +7,7 @@ const { getName } = require('multicodec') const vd = require('varint-decoder') const multihashing = require('multihashing-async') const { isMapEqual } = require('../../utils') -const { Message } = require('./message.proto') +const { Message } = require('./message') const Entry = require('./entry') class BitswapMessage { @@ -22,7 +22,7 @@ class BitswapMessage { /** @type {Map} */ this.blocks = new Map() - /** @type {Map} */ + /** @type {Map} */ this.blockPresences = new Map() this.pendingBytes = 0 } @@ -37,7 +37,7 @@ class BitswapMessage { * * @param {CID} cid * @param {number} priority - * @param {import('./message.proto').WantType} [wantType] + * @param {import('./message').Message.Wantlist.WantType | null} [wantType] * @param {boolean} [cancel] * @param {boolean} [sendDontHave] * @returns {void} @@ -123,7 +123,6 @@ class BitswapMessage { * @returns {Uint8Array} */ serializeToBitswap100 () { - /** @type {import('./message.proto').Message100} */ const msg = { wantlist: { entries: Array.from(this.wantlist.values()).map((entry) => { @@ -132,17 +131,14 @@ class BitswapMessage { priority: Number(entry.priority), cancel: Boolean(entry.cancel) } - }) + }), + full: this.full ? true : undefined }, blocks: Array.from(this.blocks.values()) .map((block) => block.data) } - if (this.full) { - msg.wantlist.full = true - } - - return Message.encode(msg) + return Message.encode(msg).finish() } /** @@ -152,7 +148,6 @@ class BitswapMessage { * @returns {Uint8Array} */ serializeToBitswap110 () { - /** @type {import('./message.proto').Message110} */ const msg = { wantlist: { entries: Array.from(this.wantlist.values()).map((entry) => { @@ -163,36 +158,38 @@ class BitswapMessage { cancel: Boolean(entry.cancel), sendDontHave: Boolean(entry.sendDontHave) } - }) + }), + full: this.full ? true : undefined }, + /** @type {import('./message').Message.BlockPresence[]} */ blockPresences: [], + + /** @type {{ prefix: Uint8Array, data: Uint8Array }[]} */ payload: [], pendingBytes: this.pendingBytes } - if (this.full) { - msg.wantlist.full = true - } - this.blocks.forEach((block) => { - msg.payload.push({ - prefix: block.cid.prefix, - data: block.data - }) + msg.payload.push( + new Message.Block({ + prefix: block.cid.prefix, + data: block.data + }) + ) }) for (const [cidStr, bpType] of this.blockPresences) { - msg.blockPresences.push({ + msg.blockPresences.push(new Message.BlockPresence({ cid: new CID(cidStr).bytes, type: bpType - }) + })) } if (this.pendingBytes > 0) { msg.pendingBytes = this.pendingBytes } - return Message.encode(msg) + return Message.encode(msg).finish() } /** @@ -222,9 +219,7 @@ class BitswapMessage { } /** - * * @param {Uint8Array} raw - * @returns {Promise} */ BitswapMessage.deserialize = async (raw) => { const decoded = Message.decode(raw) @@ -232,17 +227,25 @@ BitswapMessage.deserialize = async (raw) => { const isFull = (decoded.wantlist && decoded.wantlist.full) || false const msg = new BitswapMessage(isFull) - if (decoded.wantlist) { + if (decoded.wantlist && decoded.wantlist.entries) { decoded.wantlist.entries.forEach((entry) => { + if (!entry.block) { + return + } // note: entry.block is the CID here const cid = new CID(entry.block) - msg.addEntry(cid, entry.priority, entry.wantType, entry.cancel, entry.sendDontHave) + msg.addEntry(cid, entry.priority || 0, entry.wantType, Boolean(entry.cancel), Boolean(entry.sendDontHave)) }) } if (decoded.blockPresences) { decoded.blockPresences.forEach((blockPresence) => { + if (!blockPresence.cid) { + return + } + const cid = new CID(blockPresence.cid) + if (blockPresence.type === BitswapMessage.BlockPresenceType.Have) { msg.addHave(cid) } else { @@ -297,15 +300,11 @@ BitswapMessage.blockPresenceSize = (cid) => { BitswapMessage.Entry = Entry BitswapMessage.WantType = { - /** @type {import('./message.proto').WantBlock} */ Block: Message.Wantlist.WantType.Block, - /** @type {import('./message.proto').HaveBlock} */ Have: Message.Wantlist.WantType.Have } BitswapMessage.BlockPresenceType = { - /** @type {import('./message.proto').Have} */ Have: Message.BlockPresenceType.Have, - /** @type {import('./message.proto').DontHave} */ DontHave: Message.BlockPresenceType.DontHave } module.exports = BitswapMessage diff --git a/src/types/message/message.d.ts b/src/types/message/message.d.ts new file mode 100644 index 00000000..9aec19cd --- /dev/null +++ b/src/types/message/message.d.ts @@ -0,0 +1,379 @@ +import * as $protobuf from "protobufjs"; +/** Properties of a Message. */ +export interface IMessage { + + /** Message wantlist */ + wantlist?: (Message.IWantlist|null); + + /** Message blocks */ + blocks?: (Uint8Array[]|null); + + /** Message payload */ + payload?: (Message.IBlock[]|null); + + /** Message blockPresences */ + blockPresences?: (Message.IBlockPresence[]|null); + + /** Message pendingBytes */ + pendingBytes?: (number|null); +} + +/** Represents a Message. */ +export class Message implements IMessage { + + /** + * Constructs a new Message. + * @param [p] Properties to set + */ + constructor(p?: IMessage); + + /** Message wantlist. */ + public wantlist?: (Message.IWantlist|null); + + /** Message blocks. */ + public blocks: Uint8Array[]; + + /** Message payload. */ + public payload: Message.IBlock[]; + + /** Message blockPresences. */ + public blockPresences: Message.IBlockPresence[]; + + /** Message pendingBytes. */ + public pendingBytes: number; + + /** + * Encodes the specified Message message. Does not implicitly {@link 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: 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): 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 }): 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: Message, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this Message to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; +} + +export namespace Message { + + /** Properties of a Wantlist. */ + interface IWantlist { + + /** Wantlist entries */ + entries?: (Message.Wantlist.IEntry[]|null); + + /** Wantlist full */ + full?: (boolean|null); + } + + /** Represents a Wantlist. */ + class Wantlist implements IWantlist { + + /** + * Constructs a new Wantlist. + * @param [p] Properties to set + */ + constructor(p?: Message.IWantlist); + + /** Wantlist entries. */ + public entries: Message.Wantlist.IEntry[]; + + /** Wantlist full. */ + public full: boolean; + + /** + * Encodes the specified Wantlist message. Does not implicitly {@link Message.Wantlist.verify|verify} messages. + * @param m Wantlist message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: Message.IWantlist, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a Wantlist message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns Wantlist + * @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): Message.Wantlist; + + /** + * Creates a Wantlist message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns Wantlist + */ + public static fromObject(d: { [k: string]: any }): Message.Wantlist; + + /** + * Creates a plain object from a Wantlist message. Also converts values to other types if specified. + * @param m Wantlist + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: Message.Wantlist, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this Wantlist to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + namespace Wantlist { + + /** WantType enum. */ + enum WantType { + Block = 0, + Have = 1 + } + + /** Properties of an Entry. */ + interface IEntry { + + /** Entry block */ + block?: (Uint8Array|null); + + /** Entry priority */ + priority?: (number|null); + + /** Entry cancel */ + cancel?: (boolean|null); + + /** Entry wantType */ + wantType?: (Message.Wantlist.WantType|null); + + /** Entry sendDontHave */ + sendDontHave?: (boolean|null); + } + + /** Represents an Entry. */ + class Entry implements IEntry { + + /** + * Constructs a new Entry. + * @param [p] Properties to set + */ + constructor(p?: Message.Wantlist.IEntry); + + /** Entry block. */ + public block: Uint8Array; + + /** Entry priority. */ + public priority: number; + + /** Entry cancel. */ + public cancel: boolean; + + /** Entry wantType. */ + public wantType: Message.Wantlist.WantType; + + /** Entry sendDontHave. */ + public sendDontHave: boolean; + + /** + * Encodes the specified Entry message. Does not implicitly {@link Message.Wantlist.Entry.verify|verify} messages. + * @param m Entry message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: Message.Wantlist.IEntry, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes an Entry message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns Entry + * @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): Message.Wantlist.Entry; + + /** + * Creates an Entry message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns Entry + */ + public static fromObject(d: { [k: string]: any }): Message.Wantlist.Entry; + + /** + * Creates a plain object from an Entry message. Also converts values to other types if specified. + * @param m Entry + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: Message.Wantlist.Entry, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this Entry to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + } + + /** Properties of a Block. */ + interface IBlock { + + /** Block prefix */ + prefix?: (Uint8Array|null); + + /** Block data */ + data?: (Uint8Array|null); + } + + /** Represents a Block. */ + class Block implements IBlock { + + /** + * Constructs a new Block. + * @param [p] Properties to set + */ + constructor(p?: Message.IBlock); + + /** Block prefix. */ + public prefix: Uint8Array; + + /** Block data. */ + public data: Uint8Array; + + /** + * Encodes the specified Block message. Does not implicitly {@link Message.Block.verify|verify} messages. + * @param m Block message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: Message.IBlock, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a Block message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns Block + * @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): Message.Block; + + /** + * Creates a Block message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns Block + */ + public static fromObject(d: { [k: string]: any }): Message.Block; + + /** + * Creates a plain object from a Block message. Also converts values to other types if specified. + * @param m Block + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: Message.Block, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this Block to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } + + /** BlockPresenceType enum. */ + enum BlockPresenceType { + Have = 0, + DontHave = 1 + } + + /** Properties of a BlockPresence. */ + interface IBlockPresence { + + /** BlockPresence cid */ + cid?: (Uint8Array|null); + + /** BlockPresence type */ + type?: (Message.BlockPresenceType|null); + } + + /** Represents a BlockPresence. */ + class BlockPresence implements IBlockPresence { + + /** + * Constructs a new BlockPresence. + * @param [p] Properties to set + */ + constructor(p?: Message.IBlockPresence); + + /** BlockPresence cid. */ + public cid: Uint8Array; + + /** BlockPresence type. */ + public type: Message.BlockPresenceType; + + /** + * Encodes the specified BlockPresence message. Does not implicitly {@link Message.BlockPresence.verify|verify} messages. + * @param m BlockPresence message or plain object to encode + * @param [w] Writer to encode to + * @returns Writer + */ + public static encode(m: Message.IBlockPresence, w?: $protobuf.Writer): $protobuf.Writer; + + /** + * Decodes a BlockPresence message from the specified reader or buffer. + * @param r Reader or buffer to decode from + * @param [l] Message length if known beforehand + * @returns BlockPresence + * @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): Message.BlockPresence; + + /** + * Creates a BlockPresence message from a plain object. Also converts values to their respective internal types. + * @param d Plain object + * @returns BlockPresence + */ + public static fromObject(d: { [k: string]: any }): Message.BlockPresence; + + /** + * Creates a plain object from a BlockPresence message. Also converts values to other types if specified. + * @param m BlockPresence + * @param [o] Conversion options + * @returns Plain object + */ + public static toObject(m: Message.BlockPresence, o?: $protobuf.IConversionOptions): { [k: string]: any }; + + /** + * Converts this BlockPresence to JSON. + * @returns JSON object + */ + public toJSON(): { [k: string]: any }; + } +} diff --git a/src/types/message/message.js b/src/types/message/message.js new file mode 100644 index 00000000..72acd53d --- /dev/null +++ b/src/types/message/message.js @@ -0,0 +1,1048 @@ +/*eslint-disable*/ +"use strict"; + +var $protobuf = require("protobufjs/minimal"); + +// Common aliases +var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; + +// Exported root namespace +var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {}); + +$root.Message = (function() { + + /** + * Properties of a Message. + * @exports IMessage + * @interface IMessage + * @property {Message.IWantlist|null} [wantlist] Message wantlist + * @property {Array.|null} [blocks] Message blocks + * @property {Array.|null} [payload] Message payload + * @property {Array.|null} [blockPresences] Message blockPresences + * @property {number|null} [pendingBytes] Message pendingBytes + */ + + /** + * Constructs a new Message. + * @exports Message + * @classdesc Represents a Message. + * @implements IMessage + * @constructor + * @param {IMessage=} [p] Properties to set + */ + function Message(p) { + this.blocks = []; + this.payload = []; + this.blockPresences = []; + 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 wantlist. + * @member {Message.IWantlist|null|undefined} wantlist + * @memberof Message + * @instance + */ + Message.prototype.wantlist = null; + + /** + * Message blocks. + * @member {Array.} blocks + * @memberof Message + * @instance + */ + Message.prototype.blocks = $util.emptyArray; + + /** + * Message payload. + * @member {Array.} payload + * @memberof Message + * @instance + */ + Message.prototype.payload = $util.emptyArray; + + /** + * Message blockPresences. + * @member {Array.} blockPresences + * @memberof Message + * @instance + */ + Message.prototype.blockPresences = $util.emptyArray; + + /** + * Message pendingBytes. + * @member {number} pendingBytes + * @memberof Message + * @instance + */ + Message.prototype.pendingBytes = 0; + + /** + * Encodes the specified Message message. Does not implicitly {@link Message.verify|verify} messages. + * @function encode + * @memberof Message + * @static + * @param {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.wantlist != null && Object.hasOwnProperty.call(m, "wantlist")) + $root.Message.Wantlist.encode(m.wantlist, w.uint32(10).fork()).ldelim(); + if (m.blocks != null && m.blocks.length) { + for (var i = 0; i < m.blocks.length; ++i) + w.uint32(18).bytes(m.blocks[i]); + } + if (m.payload != null && m.payload.length) { + for (var i = 0; i < m.payload.length; ++i) + $root.Message.Block.encode(m.payload[i], w.uint32(26).fork()).ldelim(); + } + if (m.blockPresences != null && m.blockPresences.length) { + for (var i = 0; i < m.blockPresences.length; ++i) + $root.Message.BlockPresence.encode(m.blockPresences[i], w.uint32(34).fork()).ldelim(); + } + if (m.pendingBytes != null && Object.hasOwnProperty.call(m, "pendingBytes")) + w.uint32(40).int32(m.pendingBytes); + return w; + }; + + /** + * Decodes a Message message from the specified reader or buffer. + * @function decode + * @memberof Message + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {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.Message(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.wantlist = $root.Message.Wantlist.decode(r, r.uint32()); + break; + case 2: + if (!(m.blocks && m.blocks.length)) + m.blocks = []; + m.blocks.push(r.bytes()); + break; + case 3: + if (!(m.payload && m.payload.length)) + m.payload = []; + m.payload.push($root.Message.Block.decode(r, r.uint32())); + break; + case 4: + if (!(m.blockPresences && m.blockPresences.length)) + m.blockPresences = []; + m.blockPresences.push($root.Message.BlockPresence.decode(r, r.uint32())); + break; + case 5: + m.pendingBytes = r.int32(); + 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 Message + * @static + * @param {Object.} d Plain object + * @returns {Message} Message + */ + Message.fromObject = function fromObject(d) { + if (d instanceof $root.Message) + return d; + var m = new $root.Message(); + if (d.wantlist != null) { + if (typeof d.wantlist !== "object") + throw TypeError(".Message.wantlist: object expected"); + m.wantlist = $root.Message.Wantlist.fromObject(d.wantlist); + } + if (d.blocks) { + if (!Array.isArray(d.blocks)) + throw TypeError(".Message.blocks: array expected"); + m.blocks = []; + for (var i = 0; i < d.blocks.length; ++i) { + if (typeof d.blocks[i] === "string") + $util.base64.decode(d.blocks[i], m.blocks[i] = $util.newBuffer($util.base64.length(d.blocks[i])), 0); + else if (d.blocks[i].length) + m.blocks[i] = d.blocks[i]; + } + } + if (d.payload) { + if (!Array.isArray(d.payload)) + throw TypeError(".Message.payload: array expected"); + m.payload = []; + for (var i = 0; i < d.payload.length; ++i) { + if (typeof d.payload[i] !== "object") + throw TypeError(".Message.payload: object expected"); + m.payload[i] = $root.Message.Block.fromObject(d.payload[i]); + } + } + if (d.blockPresences) { + if (!Array.isArray(d.blockPresences)) + throw TypeError(".Message.blockPresences: array expected"); + m.blockPresences = []; + for (var i = 0; i < d.blockPresences.length; ++i) { + if (typeof d.blockPresences[i] !== "object") + throw TypeError(".Message.blockPresences: object expected"); + m.blockPresences[i] = $root.Message.BlockPresence.fromObject(d.blockPresences[i]); + } + } + if (d.pendingBytes != null) { + m.pendingBytes = d.pendingBytes | 0; + } + return m; + }; + + /** + * Creates a plain object from a Message message. Also converts values to other types if specified. + * @function toObject + * @memberof Message + * @static + * @param {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.blocks = []; + d.payload = []; + d.blockPresences = []; + } + if (o.defaults) { + d.wantlist = null; + d.pendingBytes = 0; + } + if (m.wantlist != null && m.hasOwnProperty("wantlist")) { + d.wantlist = $root.Message.Wantlist.toObject(m.wantlist, o); + } + if (m.blocks && m.blocks.length) { + d.blocks = []; + for (var j = 0; j < m.blocks.length; ++j) { + d.blocks[j] = o.bytes === String ? $util.base64.encode(m.blocks[j], 0, m.blocks[j].length) : o.bytes === Array ? Array.prototype.slice.call(m.blocks[j]) : m.blocks[j]; + } + } + if (m.payload && m.payload.length) { + d.payload = []; + for (var j = 0; j < m.payload.length; ++j) { + d.payload[j] = $root.Message.Block.toObject(m.payload[j], o); + } + } + if (m.blockPresences && m.blockPresences.length) { + d.blockPresences = []; + for (var j = 0; j < m.blockPresences.length; ++j) { + d.blockPresences[j] = $root.Message.BlockPresence.toObject(m.blockPresences[j], o); + } + } + if (m.pendingBytes != null && m.hasOwnProperty("pendingBytes")) { + d.pendingBytes = m.pendingBytes; + } + return d; + }; + + /** + * Converts this Message to JSON. + * @function toJSON + * @memberof Message + * @instance + * @returns {Object.} JSON object + */ + Message.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + Message.Wantlist = (function() { + + /** + * Properties of a Wantlist. + * @memberof Message + * @interface IWantlist + * @property {Array.|null} [entries] Wantlist entries + * @property {boolean|null} [full] Wantlist full + */ + + /** + * Constructs a new Wantlist. + * @memberof Message + * @classdesc Represents a Wantlist. + * @implements IWantlist + * @constructor + * @param {Message.IWantlist=} [p] Properties to set + */ + function Wantlist(p) { + this.entries = []; + 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]]; + } + + /** + * Wantlist entries. + * @member {Array.} entries + * @memberof Message.Wantlist + * @instance + */ + Wantlist.prototype.entries = $util.emptyArray; + + /** + * Wantlist full. + * @member {boolean} full + * @memberof Message.Wantlist + * @instance + */ + Wantlist.prototype.full = false; + + /** + * Encodes the specified Wantlist message. Does not implicitly {@link Message.Wantlist.verify|verify} messages. + * @function encode + * @memberof Message.Wantlist + * @static + * @param {Message.IWantlist} m Wantlist message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + Wantlist.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.entries != null && m.entries.length) { + for (var i = 0; i < m.entries.length; ++i) + $root.Message.Wantlist.Entry.encode(m.entries[i], w.uint32(10).fork()).ldelim(); + } + if (m.full != null && Object.hasOwnProperty.call(m, "full")) + w.uint32(16).bool(m.full); + return w; + }; + + /** + * Decodes a Wantlist message from the specified reader or buffer. + * @function decode + * @memberof Message.Wantlist + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {Message.Wantlist} Wantlist + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + Wantlist.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.Message.Wantlist(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + if (!(m.entries && m.entries.length)) + m.entries = []; + m.entries.push($root.Message.Wantlist.Entry.decode(r, r.uint32())); + break; + case 2: + m.full = r.bool(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a Wantlist message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof Message.Wantlist + * @static + * @param {Object.} d Plain object + * @returns {Message.Wantlist} Wantlist + */ + Wantlist.fromObject = function fromObject(d) { + if (d instanceof $root.Message.Wantlist) + return d; + var m = new $root.Message.Wantlist(); + if (d.entries) { + if (!Array.isArray(d.entries)) + throw TypeError(".Message.Wantlist.entries: array expected"); + m.entries = []; + for (var i = 0; i < d.entries.length; ++i) { + if (typeof d.entries[i] !== "object") + throw TypeError(".Message.Wantlist.entries: object expected"); + m.entries[i] = $root.Message.Wantlist.Entry.fromObject(d.entries[i]); + } + } + if (d.full != null) { + m.full = Boolean(d.full); + } + return m; + }; + + /** + * Creates a plain object from a Wantlist message. Also converts values to other types if specified. + * @function toObject + * @memberof Message.Wantlist + * @static + * @param {Message.Wantlist} m Wantlist + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + Wantlist.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.arrays || o.defaults) { + d.entries = []; + } + if (o.defaults) { + d.full = false; + } + if (m.entries && m.entries.length) { + d.entries = []; + for (var j = 0; j < m.entries.length; ++j) { + d.entries[j] = $root.Message.Wantlist.Entry.toObject(m.entries[j], o); + } + } + if (m.full != null && m.hasOwnProperty("full")) { + d.full = m.full; + } + return d; + }; + + /** + * Converts this Wantlist to JSON. + * @function toJSON + * @memberof Message.Wantlist + * @instance + * @returns {Object.} JSON object + */ + Wantlist.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + /** + * WantType enum. + * @name Message.Wantlist.WantType + * @enum {number} + * @property {number} Block=0 Block value + * @property {number} Have=1 Have value + */ + Wantlist.WantType = (function() { + var valuesById = {}, values = Object.create(valuesById); + values[valuesById[0] = "Block"] = 0; + values[valuesById[1] = "Have"] = 1; + return values; + })(); + + Wantlist.Entry = (function() { + + /** + * Properties of an Entry. + * @memberof Message.Wantlist + * @interface IEntry + * @property {Uint8Array|null} [block] Entry block + * @property {number|null} [priority] Entry priority + * @property {boolean|null} [cancel] Entry cancel + * @property {Message.Wantlist.WantType|null} [wantType] Entry wantType + * @property {boolean|null} [sendDontHave] Entry sendDontHave + */ + + /** + * Constructs a new Entry. + * @memberof Message.Wantlist + * @classdesc Represents an Entry. + * @implements IEntry + * @constructor + * @param {Message.Wantlist.IEntry=} [p] Properties to set + */ + function Entry(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]]; + } + + /** + * Entry block. + * @member {Uint8Array} block + * @memberof Message.Wantlist.Entry + * @instance + */ + Entry.prototype.block = $util.newBuffer([]); + + /** + * Entry priority. + * @member {number} priority + * @memberof Message.Wantlist.Entry + * @instance + */ + Entry.prototype.priority = 0; + + /** + * Entry cancel. + * @member {boolean} cancel + * @memberof Message.Wantlist.Entry + * @instance + */ + Entry.prototype.cancel = false; + + /** + * Entry wantType. + * @member {Message.Wantlist.WantType} wantType + * @memberof Message.Wantlist.Entry + * @instance + */ + Entry.prototype.wantType = 0; + + /** + * Entry sendDontHave. + * @member {boolean} sendDontHave + * @memberof Message.Wantlist.Entry + * @instance + */ + Entry.prototype.sendDontHave = false; + + /** + * Encodes the specified Entry message. Does not implicitly {@link Message.Wantlist.Entry.verify|verify} messages. + * @function encode + * @memberof Message.Wantlist.Entry + * @static + * @param {Message.Wantlist.IEntry} m Entry message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + Entry.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.block != null && Object.hasOwnProperty.call(m, "block")) + w.uint32(10).bytes(m.block); + if (m.priority != null && Object.hasOwnProperty.call(m, "priority")) + w.uint32(16).int32(m.priority); + if (m.cancel != null && Object.hasOwnProperty.call(m, "cancel")) + w.uint32(24).bool(m.cancel); + if (m.wantType != null && Object.hasOwnProperty.call(m, "wantType")) + w.uint32(32).int32(m.wantType); + if (m.sendDontHave != null && Object.hasOwnProperty.call(m, "sendDontHave")) + w.uint32(40).bool(m.sendDontHave); + return w; + }; + + /** + * Decodes an Entry message from the specified reader or buffer. + * @function decode + * @memberof Message.Wantlist.Entry + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {Message.Wantlist.Entry} Entry + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + Entry.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.Message.Wantlist.Entry(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.block = r.bytes(); + break; + case 2: + m.priority = r.int32(); + break; + case 3: + m.cancel = r.bool(); + break; + case 4: + m.wantType = r.int32(); + break; + case 5: + m.sendDontHave = r.bool(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates an Entry message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof Message.Wantlist.Entry + * @static + * @param {Object.} d Plain object + * @returns {Message.Wantlist.Entry} Entry + */ + Entry.fromObject = function fromObject(d) { + if (d instanceof $root.Message.Wantlist.Entry) + return d; + var m = new $root.Message.Wantlist.Entry(); + if (d.block != null) { + if (typeof d.block === "string") + $util.base64.decode(d.block, m.block = $util.newBuffer($util.base64.length(d.block)), 0); + else if (d.block.length) + m.block = d.block; + } + if (d.priority != null) { + m.priority = d.priority | 0; + } + if (d.cancel != null) { + m.cancel = Boolean(d.cancel); + } + switch (d.wantType) { + case "Block": + case 0: + m.wantType = 0; + break; + case "Have": + case 1: + m.wantType = 1; + break; + } + if (d.sendDontHave != null) { + m.sendDontHave = Boolean(d.sendDontHave); + } + return m; + }; + + /** + * Creates a plain object from an Entry message. Also converts values to other types if specified. + * @function toObject + * @memberof Message.Wantlist.Entry + * @static + * @param {Message.Wantlist.Entry} m Entry + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + Entry.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.defaults) { + if (o.bytes === String) + d.block = ""; + else { + d.block = []; + if (o.bytes !== Array) + d.block = $util.newBuffer(d.block); + } + d.priority = 0; + d.cancel = false; + d.wantType = o.enums === String ? "Block" : 0; + d.sendDontHave = false; + } + if (m.block != null && m.hasOwnProperty("block")) { + d.block = o.bytes === String ? $util.base64.encode(m.block, 0, m.block.length) : o.bytes === Array ? Array.prototype.slice.call(m.block) : m.block; + } + if (m.priority != null && m.hasOwnProperty("priority")) { + d.priority = m.priority; + } + if (m.cancel != null && m.hasOwnProperty("cancel")) { + d.cancel = m.cancel; + } + if (m.wantType != null && m.hasOwnProperty("wantType")) { + d.wantType = o.enums === String ? $root.Message.Wantlist.WantType[m.wantType] : m.wantType; + } + if (m.sendDontHave != null && m.hasOwnProperty("sendDontHave")) { + d.sendDontHave = m.sendDontHave; + } + return d; + }; + + /** + * Converts this Entry to JSON. + * @function toJSON + * @memberof Message.Wantlist.Entry + * @instance + * @returns {Object.} JSON object + */ + Entry.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return Entry; + })(); + + return Wantlist; + })(); + + Message.Block = (function() { + + /** + * Properties of a Block. + * @memberof Message + * @interface IBlock + * @property {Uint8Array|null} [prefix] Block prefix + * @property {Uint8Array|null} [data] Block data + */ + + /** + * Constructs a new Block. + * @memberof Message + * @classdesc Represents a Block. + * @implements IBlock + * @constructor + * @param {Message.IBlock=} [p] Properties to set + */ + function Block(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]]; + } + + /** + * Block prefix. + * @member {Uint8Array} prefix + * @memberof Message.Block + * @instance + */ + Block.prototype.prefix = $util.newBuffer([]); + + /** + * Block data. + * @member {Uint8Array} data + * @memberof Message.Block + * @instance + */ + Block.prototype.data = $util.newBuffer([]); + + /** + * Encodes the specified Block message. Does not implicitly {@link Message.Block.verify|verify} messages. + * @function encode + * @memberof Message.Block + * @static + * @param {Message.IBlock} m Block message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + Block.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.prefix != null && Object.hasOwnProperty.call(m, "prefix")) + w.uint32(10).bytes(m.prefix); + if (m.data != null && Object.hasOwnProperty.call(m, "data")) + w.uint32(18).bytes(m.data); + return w; + }; + + /** + * Decodes a Block message from the specified reader or buffer. + * @function decode + * @memberof Message.Block + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {Message.Block} Block + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + Block.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.Message.Block(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.prefix = r.bytes(); + break; + case 2: + m.data = r.bytes(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a Block message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof Message.Block + * @static + * @param {Object.} d Plain object + * @returns {Message.Block} Block + */ + Block.fromObject = function fromObject(d) { + if (d instanceof $root.Message.Block) + return d; + var m = new $root.Message.Block(); + if (d.prefix != null) { + if (typeof d.prefix === "string") + $util.base64.decode(d.prefix, m.prefix = $util.newBuffer($util.base64.length(d.prefix)), 0); + else if (d.prefix.length) + m.prefix = d.prefix; + } + 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; + } + return m; + }; + + /** + * Creates a plain object from a Block message. Also converts values to other types if specified. + * @function toObject + * @memberof Message.Block + * @static + * @param {Message.Block} m Block + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + Block.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.defaults) { + if (o.bytes === String) + d.prefix = ""; + else { + d.prefix = []; + if (o.bytes !== Array) + d.prefix = $util.newBuffer(d.prefix); + } + if (o.bytes === String) + d.data = ""; + else { + d.data = []; + if (o.bytes !== Array) + d.data = $util.newBuffer(d.data); + } + } + if (m.prefix != null && m.hasOwnProperty("prefix")) { + d.prefix = o.bytes === String ? $util.base64.encode(m.prefix, 0, m.prefix.length) : o.bytes === Array ? Array.prototype.slice.call(m.prefix) : m.prefix; + } + 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; + } + return d; + }; + + /** + * Converts this Block to JSON. + * @function toJSON + * @memberof Message.Block + * @instance + * @returns {Object.} JSON object + */ + Block.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return Block; + })(); + + /** + * BlockPresenceType enum. + * @name Message.BlockPresenceType + * @enum {number} + * @property {number} Have=0 Have value + * @property {number} DontHave=1 DontHave value + */ + Message.BlockPresenceType = (function() { + var valuesById = {}, values = Object.create(valuesById); + values[valuesById[0] = "Have"] = 0; + values[valuesById[1] = "DontHave"] = 1; + return values; + })(); + + Message.BlockPresence = (function() { + + /** + * Properties of a BlockPresence. + * @memberof Message + * @interface IBlockPresence + * @property {Uint8Array|null} [cid] BlockPresence cid + * @property {Message.BlockPresenceType|null} [type] BlockPresence type + */ + + /** + * Constructs a new BlockPresence. + * @memberof Message + * @classdesc Represents a BlockPresence. + * @implements IBlockPresence + * @constructor + * @param {Message.IBlockPresence=} [p] Properties to set + */ + function BlockPresence(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]]; + } + + /** + * BlockPresence cid. + * @member {Uint8Array} cid + * @memberof Message.BlockPresence + * @instance + */ + BlockPresence.prototype.cid = $util.newBuffer([]); + + /** + * BlockPresence type. + * @member {Message.BlockPresenceType} type + * @memberof Message.BlockPresence + * @instance + */ + BlockPresence.prototype.type = 0; + + /** + * Encodes the specified BlockPresence message. Does not implicitly {@link Message.BlockPresence.verify|verify} messages. + * @function encode + * @memberof Message.BlockPresence + * @static + * @param {Message.IBlockPresence} m BlockPresence message or plain object to encode + * @param {$protobuf.Writer} [w] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + BlockPresence.encode = function encode(m, w) { + if (!w) + w = $Writer.create(); + if (m.cid != null && Object.hasOwnProperty.call(m, "cid")) + w.uint32(10).bytes(m.cid); + if (m.type != null && Object.hasOwnProperty.call(m, "type")) + w.uint32(16).int32(m.type); + return w; + }; + + /** + * Decodes a BlockPresence message from the specified reader or buffer. + * @function decode + * @memberof Message.BlockPresence + * @static + * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from + * @param {number} [l] Message length if known beforehand + * @returns {Message.BlockPresence} BlockPresence + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + BlockPresence.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.Message.BlockPresence(); + while (r.pos < c) { + var t = r.uint32(); + switch (t >>> 3) { + case 1: + m.cid = r.bytes(); + break; + case 2: + m.type = r.int32(); + break; + default: + r.skipType(t & 7); + break; + } + } + return m; + }; + + /** + * Creates a BlockPresence message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof Message.BlockPresence + * @static + * @param {Object.} d Plain object + * @returns {Message.BlockPresence} BlockPresence + */ + BlockPresence.fromObject = function fromObject(d) { + if (d instanceof $root.Message.BlockPresence) + return d; + var m = new $root.Message.BlockPresence(); + if (d.cid != null) { + if (typeof d.cid === "string") + $util.base64.decode(d.cid, m.cid = $util.newBuffer($util.base64.length(d.cid)), 0); + else if (d.cid.length) + m.cid = d.cid; + } + switch (d.type) { + case "Have": + case 0: + m.type = 0; + break; + case "DontHave": + case 1: + m.type = 1; + break; + } + return m; + }; + + /** + * Creates a plain object from a BlockPresence message. Also converts values to other types if specified. + * @function toObject + * @memberof Message.BlockPresence + * @static + * @param {Message.BlockPresence} m BlockPresence + * @param {$protobuf.IConversionOptions} [o] Conversion options + * @returns {Object.} Plain object + */ + BlockPresence.toObject = function toObject(m, o) { + if (!o) + o = {}; + var d = {}; + if (o.defaults) { + if (o.bytes === String) + d.cid = ""; + else { + d.cid = []; + if (o.bytes !== Array) + d.cid = $util.newBuffer(d.cid); + } + d.type = o.enums === String ? "Have" : 0; + } + if (m.cid != null && m.hasOwnProperty("cid")) { + d.cid = o.bytes === String ? $util.base64.encode(m.cid, 0, m.cid.length) : o.bytes === Array ? Array.prototype.slice.call(m.cid) : m.cid; + } + if (m.type != null && m.hasOwnProperty("type")) { + d.type = o.enums === String ? $root.Message.BlockPresenceType[m.type] : m.type; + } + return d; + }; + + /** + * Converts this BlockPresence to JSON. + * @function toJSON + * @memberof Message.BlockPresence + * @instance + * @returns {Object.} JSON object + */ + BlockPresence.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return BlockPresence; + })(); + + return Message; +})(); + +module.exports = $root; diff --git a/src/types/message/message.proto b/src/types/message/message.proto new file mode 100644 index 00000000..ea7f1652 --- /dev/null +++ b/src/types/message/message.proto @@ -0,0 +1,43 @@ +// from https://github.com/ipfs/go-bitswap/blob/master/message/pb/message.proto +syntax = "proto3"; + +message Message { + + message Wantlist { + enum WantType { + Block = 0; + Have = 1; + } + + message Entry { + bytes block = 1; // the block cid (cidV0 in bitswap 1.0.0, cidV1 in bitswap 1.1.0) + int32 priority = 2; // the priority (normalized). default to 1 + bool cancel = 3; // whether this revokes an entry + WantType wantType = 4; // Note: defaults to enum 0, ie Block + bool sendDontHave = 5; // Note: defaults to false + } + + repeated Entry entries = 1; // a list of wantlist entries + bool full = 2; // whether this is the full wantlist. default to false + } + + message Block { + bytes prefix = 1; // CID prefix (cid version, multicodec and multihash prefix (type + length) + bytes data = 2; + } + + enum BlockPresenceType { + Have = 0; + DontHave = 1; + } + message BlockPresence { + bytes cid = 1; + BlockPresenceType type = 2; + } + + Wantlist wantlist = 1; + repeated bytes blocks = 2; // used to send Blocks in bitswap 1.0.0 + repeated Block payload = 3; // used to send Blocks in bitswap 1.1.0 + repeated BlockPresence blockPresences = 4; + int32 pendingBytes = 5; +} diff --git a/src/types/message/message.proto.d.ts b/src/types/message/message.proto.d.ts deleted file mode 100644 index 312df051..00000000 --- a/src/types/message/message.proto.d.ts +++ /dev/null @@ -1,73 +0,0 @@ - -export interface MessageProto { - decode: (bytes: Uint8Array) => MessageData - encode: (value: Message100|Message110) => Uint8Array - BlockPresenceType: { - Have: Have - DontHave: DontHave - } - Wantlist: { - WantType: { - Block: WantBlock - Have: HaveBlock - } - } -} - -export interface MessageData { - wantlist?: WantList - blockPresences: BlockPresence[] - - blocks: Uint8Array[] - payload: Block[] - - pendingBytes: number -} - -export interface Message110 { - wantlist: WantList - blockPresences: BlockPresence[] - - payload: Block[] - pendingBytes: number -} - -export interface Message100 { - wantlist: WantList - - blocks: Uint8Array[] -} - -export interface BlockPresence { - cid: Uint8Array - type: BlockPresenceType -} - -export interface WantList { - entries: Entry[] - full?: boolean -} - -export type WantBlock = 0 -export type HaveBlock = 1 -export type WantType = WantBlock | HaveBlock - -export type Have = 0 -export type DontHave = 1 -export type BlockPresenceType = Have | DontHave - -export interface Entry { - block: Uint8Array - priority: number - cancel: boolean - wantType?: WantType - sendDontHave?: boolean -} - -export interface Block { - prefix: Uint8Array - data: Uint8Array -} - -declare var Message: MessageProto -export { Message } diff --git a/src/types/message/message.proto.js b/src/types/message/message.proto.js deleted file mode 100644 index 03e0d114..00000000 --- a/src/types/message/message.proto.js +++ /dev/null @@ -1,48 +0,0 @@ -// @ts-nocheck -'use strict' - -const protons = require('protons') -// from: https://github.com/ipfs/go-ipfs/blob/master/exchange/bitswap/message/pb/message.proto - -module.exports = protons(` - message Message { - message Wantlist { - enum WantType { - Block = 0; - Have = 1; - } - - message Entry { - // changed from string to bytes, it makes a difference in JavaScript - optional bytes block = 1; // the block cid (cidV0 in bitswap 1.0.0, cidV1 in bitswap 1.1.0) - optional int32 priority = 2; // the priority (normalized). default to 1 - optional bool cancel = 3; // whether this revokes an entry - WantType wantType = 4; // Note: defaults to enum 0, ie Block - bool sendDontHave = 5; // Note: defaults to false - } - - repeated Entry entries = 1; // a list of wantlist entries - optional bool full = 2; // whether this is the full wantlist. default to false - } - - message Block { - optional bytes prefix = 1; // CID prefix (cid version, multicodec and multihash prefix (type + length) - optional bytes data = 2; - } - - enum BlockPresenceType { - Have = 0; - DontHave = 1; - } - message BlockPresence { - bytes cid = 1; - BlockPresenceType type = 2; - } - - optional Wantlist wantlist = 1; - repeated bytes blocks = 2; // used to send Blocks in bitswap 1.0.0 - repeated Block payload = 3; // used to send Blocks in bitswap 1.1.0 - repeated BlockPresence blockPresences = 4; - int32 pendingBytes = 5; - } -`) diff --git a/src/types/wantlist/entry.js b/src/types/wantlist/entry.js index 2e46f188..25120063 100644 --- a/src/types/wantlist/entry.js +++ b/src/types/wantlist/entry.js @@ -11,7 +11,7 @@ class WantListEntry { /** * @param {import('cids')} cid * @param {number} priority - * @param {import('../message/message.proto').WantType} wantType + * @param {import('../message/message').Message.Wantlist.WantType} wantType */ constructor (cid, priority, wantType) { // Keep track of how many requests we have for this key diff --git a/src/types/wantlist/index.js b/src/types/wantlist/index.js index 54408112..56ce4899 100644 --- a/src/types/wantlist/index.js +++ b/src/types/wantlist/index.js @@ -25,7 +25,7 @@ class Wantlist { /** * @param {CID} cid * @param {number} priority - * @param {import('../message/message.proto').WantType} wantType + * @param {import('../message/message').Message.Wantlist.WantType} wantType * @returns {void} */ add (cid, priority, wantType) { diff --git a/test/types/message.spec.js b/test/types/message.spec.js index 7dcc84af..ff020d7f 100644 --- a/test/types/message.spec.js +++ b/test/types/message.spec.js @@ -10,7 +10,7 @@ const testDataPath = 'test/fixtures/serialized-from-go' const rawMessageFullWantlist = loadFixture(testDataPath + '/bitswap110-message-full-wantlist') const rawMessageOneBlock = loadFixture(testDataPath + '/bitswap110-message-one-block') -const { Message } = require('../../src/types/message/message.proto') +const { Message } = require('../../src/types/message/message') const BitswapMessage = require('../../src/types/message') const makeBlock = require('../utils/make-block') @@ -25,19 +25,14 @@ describe('BitswapMessage', () => { }) describe('.addEntry', () => { - it('want type defaults to want block', () => { + it('want type defaults to want block', async () => { const cid = cids[1] const msg = new BitswapMessage(true) msg.addEntry(cid, 1) const serialized = msg.serializeToBitswap100() - expect(Message.decode(serialized).wantlist.entries[0]).to.be.eql({ - block: cid.bytes, - priority: 1, - cancel: false, - sendDontHave: false, - wantType: Message.Wantlist.WantType.Block - }) + const deserialized = await BitswapMessage.deserialize(serialized) + expect(deserialized.wantlist.get(cid.toString())).to.have.nested.property('entry.wantType', Message.Wantlist.WantType.Block) }) it('updates priority only if same want type', () => { @@ -146,7 +141,7 @@ describe('BitswapMessage', () => { b1.data, b2.data ] - }) + }).finish() const msg = await BitswapMessage.deserialize(raw) expect(msg.full).to.equal(true) @@ -195,7 +190,7 @@ describe('BitswapMessage', () => { type: BitswapMessage.BlockPresenceType.Have }], pendingBytes: 10 - }) + }).finish() const msg = await BitswapMessage.deserialize(raw) expect(msg.full).to.equal(true) @@ -334,7 +329,7 @@ describe('BitswapMessage', () => { const res = await BitswapMessage.deserialize(goEncoded) expect(res).to.eql(msg) - expect(msg.serializeToBitswap100()).to.eql(goEncoded) + expect(msg.serializeToBitswap100()).to.equalBytes(goEncoded) }) describe.skip('bitswap 1.1.0 message', () => { diff --git a/tsconfig.json b/tsconfig.json index 0cbdc31a..c8837bf9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,5 +5,8 @@ }, "include": [ "src" + ], + "exclude": [ + "src/types/message/message.js" ] } From 263a370d042e05d2e492eaec5812a2f45c569609 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 14 Apr 2021 12:20:17 +0100 Subject: [PATCH 2/4] chore: bundlesize --- .aegir.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.aegir.js b/.aegir.js index f5a5d320..a7dfe366 100644 --- a/.aegir.js +++ b/.aegir.js @@ -27,7 +27,7 @@ module.exports = { } }, build: { - bundlesizeMax: '61kB', + bundlesizeMax: '62kB', config: esbuild } } From 7caccc8912de3fbc12e6a5ec051777d800562485 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 14 Apr 2021 13:49:17 +0100 Subject: [PATCH 3/4] chore: pr comments Co-authored-by: Vasco Santos --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index c8837bf9..d4de8c9b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,6 @@ "src" ], "exclude": [ - "src/types/message/message.js" + "src/types/message/message.js" // generated file ] } From 16481093c7c851a3be2ecea3303b76961b4348a9 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 14 Apr 2021 13:49:59 +0100 Subject: [PATCH 4/4] chore: bundlesize again --- .aegir.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.aegir.js b/.aegir.js index a7dfe366..f23405d8 100644 --- a/.aegir.js +++ b/.aegir.js @@ -27,7 +27,7 @@ module.exports = { } }, build: { - bundlesizeMax: '62kB', + bundlesizeMax: '63KB', config: esbuild } }