diff --git a/package.json b/package.json index 9eb24ed4d8..53dceefa33 100644 --- a/package.json +++ b/package.json @@ -171,8 +171,8 @@ "@chainsafe/libp2p-noise": "^9.0.0", "@chainsafe/libp2p-yamux": "^3.0.0", "@libp2p/bootstrap": "^5.0.0", - "@libp2p/daemon-client": "^3.0.1", - "@libp2p/daemon-server": "^3.0.1", + "@libp2p/daemon-client": "^3.0.5", + "@libp2p/daemon-server": "^3.0.4", "@libp2p/floodsub": "^5.0.0", "@libp2p/interface-compliance-tests": "^3.0.2", "@libp2p/interface-connection-encrypter-compliance-tests": "^3.0.0", @@ -205,7 +205,7 @@ "protons": "^6.0.0", "rimraf": "^3.0.2", "sinon": "^14.0.0", - "ts-sinon": "^2.0.2" + "sinon-ts": "^1.0.0" }, "browser": { "nat-api": false diff --git a/src/circuit/pb/index.ts b/src/circuit/pb/index.ts index 44b0127cec..533fd39669 100644 --- a/src/circuit/pb/index.ts +++ b/src/circuit/pb/index.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 { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' @@ -87,32 +89,31 @@ export namespace CircuitRelay { 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.id != null) { - writer.uint32(10) - writer.bytes(obj.id) - } else { - throw new Error('Protocol error: required field "id" was not found in object') + if (opts.writeDefaults === true || (obj.id != null && obj.id.byteLength > 0)) { + w.uint32(10) + w.bytes(obj.id) } if (obj.addrs != null) { for (const value of obj.addrs) { - writer.uint32(18) - writer.bytes(value) + w.uint32(18) + w.bytes(value) } - } else { - throw new Error('Protocol error: required field "addrs" was not found in object') } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { - const obj: any = {} + const obj: any = { + id: new Uint8Array(0), + addrs: [] + } const end = length == null ? reader.len : reader.pos + length @@ -124,7 +125,6 @@ export namespace CircuitRelay { obj.id = reader.bytes() break case 2: - obj.addrs = obj.addrs ?? [] obj.addrs.push(reader.bytes()) break default: @@ -133,16 +133,6 @@ export namespace CircuitRelay { } } - obj.addrs = obj.addrs ?? [] - - if (obj.id == null) { - throw new Error('Protocol error: value for required field "id" was not found in protobuf') - } - - if (obj.addrs == null) { - throw new Error('Protocol error: value for required field "addrs" was not found in protobuf') - } - return obj }) } @@ -163,33 +153,37 @@ export namespace CircuitRelay { 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.type != null) { - writer.uint32(8) - CircuitRelay.Type.codec().encode(obj.type, writer) + w.uint32(8) + CircuitRelay.Type.codec().encode(obj.type, w) } if (obj.srcPeer != null) { - writer.uint32(18) - CircuitRelay.Peer.codec().encode(obj.srcPeer, writer) + w.uint32(18) + CircuitRelay.Peer.codec().encode(obj.srcPeer, w, { + writeDefaults: false + }) } if (obj.dstPeer != null) { - writer.uint32(26) - CircuitRelay.Peer.codec().encode(obj.dstPeer, writer) + w.uint32(26) + CircuitRelay.Peer.codec().encode(obj.dstPeer, w, { + writeDefaults: false + }) } if (obj.code != null) { - writer.uint32(32) - CircuitRelay.Status.codec().encode(obj.code, writer) + w.uint32(32) + CircuitRelay.Status.codec().encode(obj.code, w) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { const obj: any = {} diff --git a/src/fetch/pb/proto.ts b/src/fetch/pb/proto.ts index d4997ea4fc..1f41f6e506 100644 --- a/src/fetch/pb/proto.ts +++ b/src/fetch/pb/proto.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, enumeration } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' @@ -14,23 +16,23 @@ export namespace FetchRequest { 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.identifier != null) { - writer.uint32(10) - writer.string(obj.identifier) - } else { - throw new Error('Protocol error: required field "identifier" was not found in object') + if (opts.writeDefaults === true || obj.identifier !== '') { + w.uint32(10) + w.string(obj.identifier) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { - const obj: any = {} + const obj: any = { + identifier: '' + } const end = length == null ? reader.len : reader.pos + length @@ -47,10 +49,6 @@ export namespace FetchRequest { } } - if (obj.identifier == null) { - throw new Error('Protocol error: value for required field "identifier" was not found in protobuf') - } - return obj }) } @@ -95,30 +93,29 @@ export namespace FetchResponse { 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.status != null) { - writer.uint32(8) - FetchResponse.StatusCode.codec().encode(obj.status, writer) - } else { - throw new Error('Protocol error: required field "status" was not found in object') + if (opts.writeDefaults === true || (obj.status != null && __StatusCodeValues[obj.status] !== 0)) { + w.uint32(8) + FetchResponse.StatusCode.codec().encode(obj.status, w) } - if (obj.data != null) { - writer.uint32(18) - writer.bytes(obj.data) - } else { - throw new Error('Protocol error: required field "data" was not found in object') + if (opts.writeDefaults === true || (obj.data != null && obj.data.byteLength > 0)) { + w.uint32(18) + w.bytes(obj.data) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { - const obj: any = {} + const obj: any = { + status: StatusCode.OK, + data: new Uint8Array(0) + } const end = length == null ? reader.len : reader.pos + length @@ -138,14 +135,6 @@ export namespace FetchResponse { } } - if (obj.status == null) { - throw new Error('Protocol error: value for required field "status" was not found in protobuf') - } - - if (obj.data == null) { - throw new Error('Protocol error: value for required field "data" was not found in protobuf') - } - return obj }) } diff --git a/src/identify/pb/message.ts b/src/identify/pb/message.ts index 882b5b67a2..2498ac37bf 100644 --- a/src/identify/pb/message.ts +++ b/src/identify/pb/message.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' @@ -20,59 +22,58 @@ export namespace Identify { 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.protocolVersion != null) { - writer.uint32(42) - writer.string(obj.protocolVersion) + w.uint32(42) + w.string(obj.protocolVersion) } if (obj.agentVersion != null) { - writer.uint32(50) - writer.string(obj.agentVersion) + w.uint32(50) + w.string(obj.agentVersion) } if (obj.publicKey != null) { - writer.uint32(10) - writer.bytes(obj.publicKey) + w.uint32(10) + w.bytes(obj.publicKey) } if (obj.listenAddrs != null) { for (const value of obj.listenAddrs) { - writer.uint32(18) - writer.bytes(value) + w.uint32(18) + w.bytes(value) } - } else { - throw new Error('Protocol error: required field "listenAddrs" was not found in object') } if (obj.observedAddr != null) { - writer.uint32(34) - writer.bytes(obj.observedAddr) + w.uint32(34) + w.bytes(obj.observedAddr) } if (obj.protocols != null) { for (const value of obj.protocols) { - writer.uint32(26) - writer.string(value) + w.uint32(26) + w.string(value) } - } else { - throw new Error('Protocol error: required field "protocols" was not found in object') } if (obj.signedPeerRecord != null) { - writer.uint32(66) - writer.bytes(obj.signedPeerRecord) + w.uint32(66) + w.bytes(obj.signedPeerRecord) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { - const obj: any = {} + const obj: any = { + listenAddrs: [], + protocols: [] + } const end = length == null ? reader.len : reader.pos + length @@ -90,14 +91,12 @@ export namespace Identify { obj.publicKey = reader.bytes() break case 2: - obj.listenAddrs = obj.listenAddrs ?? [] obj.listenAddrs.push(reader.bytes()) break case 4: obj.observedAddr = reader.bytes() break case 3: - obj.protocols = obj.protocols ?? [] obj.protocols.push(reader.string()) break case 8: @@ -109,17 +108,6 @@ export namespace Identify { } } - obj.listenAddrs = obj.listenAddrs ?? [] - obj.protocols = obj.protocols ?? [] - - if (obj.listenAddrs == null) { - throw new Error('Protocol error: value for required field "listenAddrs" was not found in protobuf') - } - - if (obj.protocols == null) { - throw new Error('Protocol error: value for required field "protocols" was not found in protobuf') - } - return obj }) } diff --git a/src/insecure/pb/proto.ts b/src/insecure/pb/proto.ts index 1ff2468f5c..e800904eb4 100644 --- a/src/insecure/pb/proto.ts +++ b/src/insecure/pb/proto.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, enumeration } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' @@ -15,23 +17,25 @@ export namespace Exchange { 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.id != null) { - writer.uint32(10) - writer.bytes(obj.id) + w.uint32(10) + w.bytes(obj.id) } if (obj.pubkey != null) { - writer.uint32(18) - PublicKey.codec().encode(obj.pubkey, writer) + w.uint32(18) + PublicKey.codec().encode(obj.pubkey, w, { + writeDefaults: false + }) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { const obj: any = {} @@ -99,30 +103,29 @@ export namespace PublicKey { 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.Type != null) { - writer.uint32(8) - KeyType.codec().encode(obj.Type, writer) - } else { - throw new Error('Protocol error: required field "Type" was not found in object') + if (opts.writeDefaults === true || (obj.Type != null && __KeyTypeValues[obj.Type] !== 0)) { + w.uint32(8) + KeyType.codec().encode(obj.Type, w) } - if (obj.Data != null) { - writer.uint32(18) - writer.bytes(obj.Data) - } else { - throw new Error('Protocol error: required field "Data" was not found in object') + if (opts.writeDefaults === true || (obj.Data != null && obj.Data.byteLength > 0)) { + w.uint32(18) + w.bytes(obj.Data) } if (opts.lengthDelimited !== false) { - writer.ldelim() + w.ldelim() } }, (reader, length) => { - const obj: any = {} + const obj: any = { + Type: KeyType.RSA, + Data: new Uint8Array(0) + } const end = length == null ? reader.len : reader.pos + length @@ -142,14 +145,6 @@ export namespace PublicKey { } } - if (obj.Type == null) { - throw new Error('Protocol error: value for required field "Type" was not found in protobuf') - } - - if (obj.Data == null) { - throw new Error('Protocol error: value for required field "Data" was not found in protobuf') - } - return obj }) } diff --git a/test/addresses/address-manager.spec.ts b/test/addresses/address-manager.spec.ts index f08b0444ba..57404029d9 100644 --- a/test/addresses/address-manager.spec.ts +++ b/test/addresses/address-manager.spec.ts @@ -6,7 +6,7 @@ import { AddressFilter, DefaultAddressManager } from '../../src/address-manager/ import { createNode } from '../utils/creators/peer.js' import { createFromJSON } from '@libp2p/peer-id-factory' import Peers from '../fixtures/peers.js' -import { stubInterface } from 'ts-sinon' +import { stubInterface } from 'sinon-ts' import type { TransportManager } from '@libp2p/interface-transport' import type { PeerId } from '@libp2p/interface-peer-id' import type { Libp2p } from '../../src/index.js' diff --git a/test/connection-manager/auto-dialler.spec.ts b/test/connection-manager/auto-dialler.spec.ts index 453b79ce7d..16d7296d34 100644 --- a/test/connection-manager/auto-dialler.spec.ts +++ b/test/connection-manager/auto-dialler.spec.ts @@ -5,7 +5,7 @@ import { AutoDialler } from '../../src/connection-manager/auto-dialler.js' import pWaitFor from 'p-wait-for' import delay from 'delay' import { createEd25519PeerId } from '@libp2p/peer-id-factory' -import { stubInterface } from 'ts-sinon' +import { stubInterface } from 'sinon-ts' import type { ConnectionManager } from '@libp2p/interface-connection-manager' import type { PeerStore, Peer } from '@libp2p/interface-peer-store' diff --git a/test/connection-manager/index.node.ts b/test/connection-manager/index.node.ts index 879d85211c..a00699ef7e 100644 --- a/test/connection-manager/index.node.ts +++ b/test/connection-manager/index.node.ts @@ -9,7 +9,7 @@ import type { PeerId } from '@libp2p/interface-peer-id' import { DefaultConnectionManager } from '../../src/connection-manager/index.js' import { CustomEvent } from '@libp2p/interfaces/events' import * as STATUS from '@libp2p/interface-connection/status' -import { stubInterface } from 'ts-sinon' +import { stubInterface } from 'sinon-ts' import type { KeyBook, PeerStore } from '@libp2p/interface-peer-store' import sinon from 'sinon' import pWaitFor from 'p-wait-for' diff --git a/test/connection-manager/index.spec.ts b/test/connection-manager/index.spec.ts index bb3d71dfc2..4d37dd8f8f 100644 --- a/test/connection-manager/index.spec.ts +++ b/test/connection-manager/index.spec.ts @@ -12,7 +12,7 @@ import { CustomEvent } from '@libp2p/interfaces/events' import { KEEP_ALIVE } from '@libp2p/interface-peer-store/tags' import pWaitFor from 'p-wait-for' import { multiaddr } from '@multiformats/multiaddr' -import { stubInterface } from 'ts-sinon' +import { stubInterface } from 'sinon-ts' import type { Dialer } from '@libp2p/interface-connection-manager' import type { Connection } from '@libp2p/interface-connection' import type { Metrics } from '@libp2p/interface-metrics' diff --git a/test/content-routing/content-routing.node.ts b/test/content-routing/content-routing.node.ts index e4c6d5db74..61cda7c182 100644 --- a/test/content-routing/content-routing.node.ts +++ b/test/content-routing/content-routing.node.ts @@ -14,7 +14,7 @@ import type { Libp2p } from '../../src/index.js' import type { PeerInfo } from '@libp2p/interface-peer-info' import type { Libp2pNode } from '../../src/libp2p.js' import type { ContentRouting } from '@libp2p/interface-content-routing' -import { StubbedInstance, stubInterface } from 'ts-sinon' +import { StubbedInstance, stubInterface } from 'sinon-ts' import { peerIdFromString } from '@libp2p/peer-id' describe('content-routing', () => { diff --git a/test/dialing/dial-request.spec.ts b/test/dialing/dial-request.spec.ts index 2a0674a46b..c6500805b3 100644 --- a/test/dialing/dial-request.spec.ts +++ b/test/dialing/dial-request.spec.ts @@ -14,7 +14,7 @@ import type { Metrics } from '@libp2p/interface-metrics' import type { PeerStore } from '@libp2p/interface-peer-store' import type { TransportManager } from '@libp2p/interface-transport' import type { ConnectionGater } from '@libp2p/interface-connection' -import { stubInterface } from 'ts-sinon' +import { stubInterface } from 'sinon-ts' const error = new Error('dial failure') describe('Dial Request', () => { diff --git a/test/identify/push.spec.ts b/test/identify/push.spec.ts index fb3c6e722e..415573b1c7 100644 --- a/test/identify/push.spec.ts +++ b/test/identify/push.spec.ts @@ -23,7 +23,7 @@ import { CustomEvent } from '@libp2p/interfaces/events' import delay from 'delay' import { pEvent } from 'p-event' import { start, stop } from '@libp2p/interfaces/startable' -import { stubInterface } from 'ts-sinon' +import { stubInterface } from 'sinon-ts' import type { Dialer } from '@libp2p/interface-connection-manager' import type { Metrics } from '@libp2p/interface-metrics' import { DefaultComponents } from '../../src/components.js' diff --git a/test/nat-manager/nat-manager.node.ts b/test/nat-manager/nat-manager.node.ts index 87f11193d9..107173ca77 100644 --- a/test/nat-manager/nat-manager.node.ts +++ b/test/nat-manager/nat-manager.node.ts @@ -11,7 +11,7 @@ import Peers from '../fixtures/peers.js' import { codes } from '../../src/errors.js' import { createFromJSON } from '@libp2p/peer-id-factory' import type { NatAPI } from '@achingbrain/nat-port-mapper' -import { StubbedInstance, stubInterface } from 'ts-sinon' +import { StubbedInstance, stubInterface } from 'sinon-ts' import { start, stop } from '@libp2p/interfaces/startable' import { DefaultComponents } from '../../src/components.js' diff --git a/test/peer-discovery/index.spec.ts b/test/peer-discovery/index.spec.ts index 5358523061..b217b20ec5 100644 --- a/test/peer-discovery/index.spec.ts +++ b/test/peer-discovery/index.spec.ts @@ -7,7 +7,7 @@ import { createPeerId } from '../utils/creators/peer.js' import type { PeerId } from '@libp2p/interface-peer-id' import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js' import type { Startable } from '@libp2p/interfaces/startable' -import { stubInterface } from 'ts-sinon' +import { stubInterface } from 'sinon-ts' import type { PeerDiscovery } from '@libp2p/interface-peer-discovery' describe('peer discovery', () => { diff --git a/test/peer-routing/peer-routing.node.ts b/test/peer-routing/peer-routing.node.ts index a3bc91c07b..f54427eb3b 100644 --- a/test/peer-routing/peer-routing.node.ts +++ b/test/peer-routing/peer-routing.node.ts @@ -18,7 +18,7 @@ import { EventTypes, MessageType } from '@libp2p/interface-dht' import type { PeerInfo } from '@libp2p/interface-peer-info' import { kadDHT } from '@libp2p/kad-dht' import type { PeerRouting } from '@libp2p/interface-peer-routing' -import { StubbedInstance, stubInterface } from 'ts-sinon' +import { StubbedInstance, stubInterface } from 'sinon-ts' describe('peer-routing', () => { let peerId: PeerId diff --git a/test/relay/auto-relay.node.ts b/test/relay/auto-relay.node.ts index 90c99610ea..806e70d20f 100644 --- a/test/relay/auto-relay.node.ts +++ b/test/relay/auto-relay.node.ts @@ -11,7 +11,7 @@ import type { Libp2pNode } from '../../src/libp2p.js' import type { Options as PWaitForOptions } from 'p-wait-for' import { createRelayOptions, createNodeOptions } from './utils.js' import { protocols } from '@multiformats/multiaddr' -import { StubbedInstance, stubInterface } from 'ts-sinon' +import { StubbedInstance, stubInterface } from 'sinon-ts' import type { ContentRouting } from '@libp2p/interface-content-routing' async function usingAsRelay (node: Libp2pNode, relay: Libp2pNode, opts?: PWaitForOptions) {