|
| 1 | +import type {HandlerProps, MuxedStream, Connection} from "libp2p"; |
| 2 | +import {Connection as Conn} from "libp2p-interfaces/src/connection"; |
| 3 | +import PeerId from "peer-id"; |
| 4 | +import {Multiaddr} from "multiaddr"; |
| 5 | +import {EventEmitter} from "events"; |
| 6 | +// @ts-ignore |
| 7 | +import pair from "it-pair"; |
| 8 | +import type BufferList from "bl/BufferList"; |
| 9 | +import drain from "it-drain"; |
| 10 | + |
| 11 | +class MockAddressBook { |
| 12 | + addrs: {[key: string]: Multiaddr[]} = {}; |
| 13 | + |
| 14 | + add(pid: PeerId, addrs: Multiaddr[]) { |
| 15 | + this.addrs[pid.toString()] = addrs; |
| 16 | + return this; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +export class MockLibp2p { |
| 21 | + streamId = 0; |
| 22 | + handlers: {[key: string]: (props: HandlerProps) => void} = {}; |
| 23 | + |
| 24 | + peerId: PeerId; |
| 25 | + connectionManager = new EventEmitter(); |
| 26 | + peerStore = { |
| 27 | + addressBook: new MockAddressBook(), |
| 28 | + }; |
| 29 | + |
| 30 | + sources: {[key: string]: AsyncIterable<BufferList>} = {}; |
| 31 | + |
| 32 | + constructor(peerId: PeerId) { |
| 33 | + this.peerId = peerId; |
| 34 | + } |
| 35 | + |
| 36 | + handle(protocol: string, handler: (props: HandlerProps) => void) { |
| 37 | + this.handlers[protocol] = handler; |
| 38 | + } |
| 39 | + |
| 40 | + unhandle(protocol: string | string[]) { |
| 41 | + const protos = Array.isArray(protocol) ? protocol : [protocol]; |
| 42 | + protos.forEach((p) => { |
| 43 | + delete this.handlers[p]; |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + async dial( |
| 48 | + peer: string | PeerId | Multiaddr, |
| 49 | + options?: any |
| 50 | + ): Promise<Connection> { |
| 51 | + const localAddr = new Multiaddr("/ip4/127.0.0.1/tcp/8080"); |
| 52 | + const remoteAddr = new Multiaddr("/ip4/127.0.0.1/tcp/8081"); |
| 53 | + |
| 54 | + const [localPeer, remotePeer] = [ |
| 55 | + PeerId.createFromB58String( |
| 56 | + "12D3KooWSoLzampfxc4t3sy9z7yq1Cgzbi7zGXpV7nvt5hfeKUhR" |
| 57 | + ), |
| 58 | + PeerId.createFromB58String( |
| 59 | + "12D3KooWSoLzampfxc4t3sy9z7yq1Cgzbi7zGXpV7nvt5hfeKRhU" |
| 60 | + ), |
| 61 | + ]; |
| 62 | + const openStreams: MuxedStream[] = []; |
| 63 | + let streamId = 0; |
| 64 | + |
| 65 | + return new Conn({ |
| 66 | + localPeer: localPeer, |
| 67 | + remotePeer: remotePeer, |
| 68 | + localAddr, |
| 69 | + remoteAddr, |
| 70 | + stat: { |
| 71 | + timeline: { |
| 72 | + open: Date.now() - 10, |
| 73 | + upgraded: Date.now(), |
| 74 | + }, |
| 75 | + direction: "outbound", |
| 76 | + encryption: "/noise", |
| 77 | + multiplexer: "/mplex/6.7.0", |
| 78 | + }, |
| 79 | + newStream: async (protocols) => { |
| 80 | + const id = streamId++; |
| 81 | + const stream = pair(); |
| 82 | + |
| 83 | + stream.close = () => stream.sink([]); |
| 84 | + stream.id = id; |
| 85 | + |
| 86 | + openStreams.push(stream); |
| 87 | + |
| 88 | + return { |
| 89 | + stream, |
| 90 | + protocol: protocols[0], |
| 91 | + }; |
| 92 | + }, |
| 93 | + close: async () => {}, |
| 94 | + getStreams: () => openStreams, |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + async dialProtocol( |
| 99 | + peer: PeerId, |
| 100 | + protocols: string[] | string, |
| 101 | + options?: any |
| 102 | + ): Promise<{stream: MuxedStream; protocol: string}> { |
| 103 | + const id = "" + this.streamId++; |
| 104 | + const stream: MuxedStream = |
| 105 | + id in this.sources |
| 106 | + ? { |
| 107 | + source: this.sources[id], |
| 108 | + sink: drain, |
| 109 | + } |
| 110 | + : pair(); |
| 111 | + stream.close = () => {}; |
| 112 | + stream.id = id; |
| 113 | + |
| 114 | + const conn = { |
| 115 | + stream, |
| 116 | + protocol: typeof protocols === "string" ? protocols : protocols[0], |
| 117 | + }; |
| 118 | + if (id in this.sources) { |
| 119 | + // @ts-ignore |
| 120 | + this.handlers[conn.protocol]({stream, connection: conn}); |
| 121 | + } |
| 122 | + return conn; |
| 123 | + } |
| 124 | +} |
0 commit comments