@@ -52,7 +52,8 @@ import {
5252 DataTransform ,
5353 rejectReasonFromAcceptance ,
5454 MsgIdToStrFn ,
55- MessageId
55+ MessageId ,
56+ PublishOpts
5657} from './types.js'
5758import { buildRawMessage , validateToRawMessage } from './utils/buildRawMessage.js'
5859import { msgIdFnStrictNoSign , msgIdFnStrictSign } from './utils/msgIdFn.js'
@@ -1995,7 +1996,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
19951996 *
19961997 * For messages not from us, this class uses `forwardMessage`.
19971998 */
1998- async publish ( topic : TopicStr , data : Uint8Array ) : Promise < PublishResult > {
1999+ async publish ( topic : TopicStr , data : Uint8Array , opts ?: PublishOpts ) : Promise < PublishResult > {
19992000 const transformedData = this . dataTransform ? this . dataTransform . outboundTransform ( topic , data ) : data
20002001
20012002 if ( this . publishConfig == null ) {
@@ -2018,7 +2019,10 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
20182019 const { tosend, tosendCount } = this . selectPeersToPublish ( topic )
20192020 const willSendToSelf = this . opts . emitSelf === true && this . subscriptions . has ( topic )
20202021
2021- if ( tosend . size === 0 && ! this . opts . allowPublishToZeroPeers && ! willSendToSelf ) {
2022+ // Current publish opt takes precedence global opts, while preserving false value
2023+ const allowPublishToZeroPeers = opts ?. allowPublishToZeroPeers ?? this . opts . allowPublishToZeroPeers
2024+
2025+ if ( tosend . size === 0 && ! allowPublishToZeroPeers && ! willSendToSelf ) {
20222026 throw Error ( 'PublishError.InsufficientPeers' )
20232027 }
20242028
0 commit comments