UNPKG

peerbit

Version:
64 lines 2.32 kB
import { noise } from "@chainsafe/libp2p-noise"; import { yamux } from "@chainsafe/libp2p-yamux"; import { identify } from "@libp2p/identify"; import { DirectBlock } from "@peerbit/blocks"; import { DefaultCryptoKeychain, keychain, } from "@peerbit/keychain"; import { DirectSub } from "@peerbit/pubsub"; import { createLibp2p, } from "libp2p"; import { listen, relay, transports } from "./transports.js"; export const createLibp2pExtended = (opts = { services: { blocks: (c) => new DirectBlock(c), pubsub: (c) => new DirectSub(c), keychain: keychain(), }, }) => { let extraServices = {}; if (opts.services?.["relay"] === null) { delete opts.services?.["relay"]; } else if (!opts.services?.["relay"]) { const relayComponent = relay(); if (relayComponent) { // will be null in browser extraServices["relay"] = relayComponent; } } if (!opts.services?.["identify"]) { extraServices["identify"] = identify(); } return createLibp2p({ ...opts, connectionManager: { inboundStreamProtocolNegotiationTimeout: 1e4, inboundUpgradeTimeout: 1e4, outboundStreamProtocolNegotiationTimeout: 1e4, reconnectRetries: 0, // https://github.com/libp2p/js-libp2p/issues/3289 ...opts.connectionManager, }, addresses: { listen: listen(), ...opts.addresses, }, connectionMonitor: { abortConnectionOnPingFailure: false, ...opts?.connectionMonitor, }, transports: opts.transports || transports(), connectionEncrypters: opts.connectionEncrypters || [noise()], streamMuxers: opts.streamMuxers || [yamux()], services: { pubsub: opts.services?.pubsub || ((c) => new DirectSub(c, { canRelayMessage: true, // auto dial true // auto prune true })), blocks: opts.services?.blocks || ((c) => new DirectBlock(c)), keychain: opts.services?.keychain || ((c) => new DefaultCryptoKeychain()), ...opts.services, ...extraServices, }, }); }; //# sourceMappingURL=libp2p.js.map