diff --git a/package.json b/package.json index 76f9ea4..45a332b 100644 --- a/package.json +++ b/package.json @@ -159,11 +159,11 @@ "it-foreach": "^0.1.1", "it-map": "^1.0.6", "it-pipe": "^2.0.3", - "multiformats": "^9.6.3", - "protons-runtime": "^3.1.0", + "multiformats": "^10.0.0", + "protons-runtime": "^4.0.1", "uint8-varint": "^1.0.2", "uint8arraylist": "^2.1.0", - "uint8arrays": "^3.0.0", + "uint8arrays": "^4.0.2", "varint": "^6.0.0" }, "devDependencies": { @@ -171,7 +171,7 @@ "@libp2p/peer-id-factory": "^1.0.0", "@types/varint": "^6.0.0", "aegir": "^37.3.0", - "protons": "^5.1.0", + "protons": "^6.0.0", "sinon": "^14.0.0" } } diff --git a/src/envelope/envelope.ts b/src/envelope/envelope.ts index 2e56828..fdd22ad 100644 --- a/src/envelope/envelope.ts +++ b/src/envelope/envelope.ts @@ -1,5 +1,7 @@ /* eslint-disable import/export */ +/* eslint-disable complexity */ /* eslint-disable @typescript-eslint/no-namespace */ +/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ import { encodeMessage, decodeMessage, message } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' @@ -17,41 +19,33 @@ export namespace Envelope { export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, writer, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { - writer.fork() + w.fork() } - if (obj.publicKey != null) { - writer.uint32(10) - writer.bytes(obj.publicKey) - } else { - throw new Error('Protocol error: required field "publicKey" was not found in object') + if (opts.writeDefaults === true || (obj.publicKey != null && obj.publicKey.byteLength > 0)) { + w.uint32(10) + w.bytes(obj.publicKey) } - if (obj.payloadType != null) { - writer.uint32(18) - writer.bytes(obj.payloadType) - } else { - throw new Error('Protocol error: required field "payloadType" was not found in object') + if (opts.writeDefaults === true || (obj.payloadType != null && obj.payloadType.byteLength > 0)) { + w.uint32(18) + w.bytes(obj.payloadType) } - if (obj.payload != null) { - writer.uint32(26) - writer.bytes(obj.payload) - } else { - throw new Error('Protocol error: required field "payload" was not found in object') + if (opts.writeDefaults === true || (obj.payload != null && obj.payload.byteLength > 0)) { + w.uint32(26) + w.bytes(obj.payload) } - if (obj.signature != null) { - writer.uint32(42) - writer.bytes(obj.signature) - } else { - throw new Error('Protocol error: required field "signature" was not found in object') + if (opts.writeDefaults === true || (obj.signature != null && obj.signature.byteLength > 0)) { + w.uint32(42) + w.bytes(obj.signature) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { const obj: any = { @@ -85,22 +79,6 @@ export namespace Envelope { } } - if (obj.publicKey == null) { - throw new Error('Protocol error: value for required field "publicKey" was not found in protobuf') - } - - if (obj.payloadType == null) { - throw new Error('Protocol error: value for required field "payloadType" was not found in protobuf') - } - - if (obj.payload == null) { - throw new Error('Protocol error: value for required field "payload" was not found in protobuf') - } - - if (obj.signature == null) { - throw new Error('Protocol error: value for required field "signature" was not found in protobuf') - } - return obj }) } diff --git a/src/peer-record/consts.js b/src/peer-record/consts.ts similarity index 100% rename from src/peer-record/consts.js rename to src/peer-record/consts.ts diff --git a/src/peer-record/peer-record.ts b/src/peer-record/peer-record.ts index 21d4554..5ff83fe 100644 --- a/src/peer-record/peer-record.ts +++ b/src/peer-record/peer-record.ts @@ -1,5 +1,7 @@ /* eslint-disable import/export */ +/* eslint-disable complexity */ /* eslint-disable @typescript-eslint/no-namespace */ +/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ import { encodeMessage, decodeMessage, message } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' @@ -21,20 +23,18 @@ export namespace PeerRecord { export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, writer, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { - writer.fork() + w.fork() } - if (obj.multiaddr != null) { - writer.uint32(10) - writer.bytes(obj.multiaddr) - } else { - throw new Error('Protocol error: required field "multiaddr" was not found in object') + if (opts.writeDefaults === true || (obj.multiaddr != null && obj.multiaddr.byteLength > 0)) { + w.uint32(10) + w.bytes(obj.multiaddr) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { const obj: any = { @@ -56,10 +56,6 @@ export namespace PeerRecord { } } - if (obj.multiaddr == null) { - throw new Error('Protocol error: value for required field "multiaddr" was not found in protobuf') - } - return obj }) } @@ -80,36 +76,32 @@ export namespace PeerRecord { export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, writer, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { - writer.fork() + w.fork() } - if (obj.peerId != null) { - writer.uint32(10) - writer.bytes(obj.peerId) - } else { - throw new Error('Protocol error: required field "peerId" was not found in object') + if (opts.writeDefaults === true || (obj.peerId != null && obj.peerId.byteLength > 0)) { + w.uint32(10) + w.bytes(obj.peerId) } - if (obj.seq != null) { - writer.uint32(16) - writer.uint64(obj.seq) - } else { - throw new Error('Protocol error: required field "seq" was not found in object') + if (opts.writeDefaults === true || obj.seq !== 0n) { + w.uint32(16) + w.uint64(obj.seq) } if (obj.addresses != null) { for (const value of obj.addresses) { - writer.uint32(26) - PeerRecord.AddressInfo.codec().encode(value, writer) + w.uint32(26) + PeerRecord.AddressInfo.codec().encode(value, w, { + writeDefaults: true + }) } - } else { - throw new Error('Protocol error: required field "addresses" was not found in object') } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { const obj: any = { @@ -139,14 +131,6 @@ export namespace PeerRecord { } } - if (obj.peerId == null) { - throw new Error('Protocol error: value for required field "peerId" was not found in protobuf') - } - - if (obj.seq == null) { - throw new Error('Protocol error: value for required field "seq" was not found in protobuf') - } - return obj }) } diff --git a/tsconfig.json b/tsconfig.json index d552f8e..13a3599 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,9 +6,5 @@ "include": [ "src", "test" - ], - "exclude": [ - "src/envelope/envelope.js", - "src/peer-record/peer-record.js" ] }