UNPKG

libp2p-pubsub

Version:
86 lines 2.5 kB
// @ts-expect-error no types import DuplexPair from 'it-pair/duplex.js'; import * as PeerIdFactory from 'libp2p-peer-id-factory'; import { PubsubBaseProtocol } from '../../src/index.js'; import { RPC } from '../../src/message/rpc.js'; export const createPeerId = async () => { const peerId = await PeerIdFactory.createEd25519PeerId(); return peerId; }; export class PubsubImplementation extends PubsubBaseProtocol { async _publish() { // ... } _decodeRpc(bytes) { return RPC.decode(bytes); } _encodeRpc(rpc) { return RPC.encode(rpc).finish(); } } export const mockRegistrar = { handle: () => { }, register: () => { }, unregister: () => { } }; export const createMockRegistrar = (registrarRecord) => { const registrar = { handle: (multicodecs, handler) => { const rec = registrarRecord.get(multicodecs[0]) ?? {}; registrarRecord.set(multicodecs[0], { ...rec, handler }); }, register: (topology) => { const { multicodecs } = topology; const rec = registrarRecord.get(multicodecs[0]) ?? {}; registrarRecord.set(multicodecs[0], { ...rec, onConnect: topology._onConnect, onDisconnect: topology._onDisconnect }); return multicodecs[0]; }, unregister: (id) => { registrarRecord.delete(id); }, getConnection(peerId) { throw new Error('Not implemented'); }, peerStore: { on: () => { throw new Error('Not implemented'); }, protoBook: { get: () => { throw new Error('Not implemented'); } }, peers: new Map(), get: (peerId) => { throw new Error('Not implemented'); } }, connectionManager: { on: () => { throw new Error('Not implemented'); } } }; return registrar; }; export const ConnectionPair = () => { const [d0, d1] = DuplexPair(); return [ { stream: d0, newStream: async () => await Promise.resolve({ stream: d0 }) }, { stream: d1, newStream: async () => await Promise.resolve({ stream: d1 }) } ]; }; //# sourceMappingURL=index.js.map