|
| 1 | +/// <reference types="node" /> |
| 2 | + |
| 3 | +declare interface NetOptions { |
| 4 | + family: 'ipv4' | 'ipv6'; |
| 5 | + host: string; |
| 6 | + transport: string; |
| 7 | + port: number; |
| 8 | +} |
| 9 | + |
| 10 | +declare interface Protocol { |
| 11 | + code: number; |
| 12 | + size: number; |
| 13 | + name: string; |
| 14 | + resolvable: boolean; |
| 15 | + path: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +declare interface Protocols { |
| 19 | + table: [number, number, string, boolean?, boolean?][]; |
| 20 | + names: Record<string, Protocol>; |
| 21 | + codes: Record<number, Protocol>; |
| 22 | + object(code: number, size: number, name: string, resolvable?: any, path?: any): Protocol; |
| 23 | +} |
| 24 | + |
| 25 | +declare interface NodeAddress { |
| 26 | + family: 4 | 6; |
| 27 | + address: string; |
| 28 | + port: number; |
| 29 | +} |
| 30 | + |
| 31 | +declare type MultiaddrInput = string | Buffer | Multiaddr; |
| 32 | + |
| 33 | +declare class Multiaddr { |
| 34 | + /** |
| 35 | + * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from |
| 36 | + * a Buffer, String or another Multiaddr instance |
| 37 | + * public key. |
| 38 | + * @param addr - If String or Buffer, needs to adhere |
| 39 | + * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) |
| 40 | + */ |
| 41 | + constructor(addr?: MultiaddrInput); |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns Multiaddr as a String |
| 45 | + */ |
| 46 | + toString(): string; |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns Multiaddr as a JSON encoded object |
| 50 | + */ |
| 51 | + toJSON(): string; |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns Multiaddr as a convinient options object to be used with net.createConnection |
| 55 | + */ |
| 56 | + toOptions(): NetOptions; |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns Multiaddr as a human-readable string |
| 60 | + */ |
| 61 | + inspect(): string; |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the protocols the Multiaddr is defined with, as an array of objects, in |
| 65 | + * left-to-right order. Each object contains the protocol code, protocol name, |
| 66 | + * and the size of its address space in bits. |
| 67 | + * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) |
| 68 | + */ |
| 69 | + protos(): Protocol[]; |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the codes of the protocols in left-to-right order. |
| 73 | + * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) |
| 74 | + */ |
| 75 | + protoCodes(): number[]; |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the names of the protocols in left-to-right order. |
| 79 | + * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv) |
| 80 | + */ |
| 81 | + protoNames(): string[]; |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns a tuple of parts |
| 85 | + */ |
| 86 | + tuples(): [number, Buffer][]; |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns a tuple of string/number parts |
| 90 | + */ |
| 91 | + stringTuples(): [number, string | number][]; |
| 92 | + |
| 93 | + /** |
| 94 | + * Encapsulates a Multiaddr in another Multiaddr |
| 95 | + */ |
| 96 | + encapsulate(addr: MultiaddrInput): Multiaddr; |
| 97 | + |
| 98 | + /** |
| 99 | + * Decapsulates a Multiaddr from another Multiaddr |
| 100 | + */ |
| 101 | + decapsulate(addr: MultiaddrInput): Multiaddr; |
| 102 | + |
| 103 | + /** |
| 104 | + * A more reliable version of `decapsulate` if you are targeting a |
| 105 | + * specific code, such as 421 (the `p2p` protocol code). The last index of the code |
| 106 | + * will be removed from the `Multiaddr`, and a new instance will be returned. |
| 107 | + * If the code is not present, the original `Multiaddr` is returned. |
| 108 | + */ |
| 109 | + decapsulateCode(code: number): Multiaddr; |
| 110 | + |
| 111 | + /** |
| 112 | + * Extract the peerId if the multiaddr contains one |
| 113 | + */ |
| 114 | + getPeerId(): string; |
| 115 | + |
| 116 | + /** |
| 117 | + * Extract the path if the multiaddr contains one |
| 118 | + */ |
| 119 | + getPath(): string | null; |
| 120 | + |
| 121 | + /** |
| 122 | + * Checks if two Multiaddrs are the same |
| 123 | + */ |
| 124 | + equals(addr: Multiaddr): boolean; |
| 125 | + |
| 126 | + /** |
| 127 | + * Gets a Multiaddrs node-friendly address object. Note that protocol information |
| 128 | + * is left out: in Node (and most network systems) the protocol is unknowable |
| 129 | + * given only the address. |
| 130 | + * |
| 131 | + * Has to be a ThinWaist Address, otherwise throws error |
| 132 | + */ |
| 133 | + nodeAddress(): NodeAddress; |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns if a Multiaddr is a Thin Waist address or not. |
| 137 | + * |
| 138 | + * Thin Waist is if a Multiaddr adheres to the standard combination of: |
| 139 | + * |
| 140 | + * `{IPv4, IPv6}/{TCP, UDP}` |
| 141 | + */ |
| 142 | + isThinWaistAddress(addr?: Multiaddr): boolean; |
| 143 | +} |
| 144 | + |
| 145 | +declare interface MultiaddrClass { |
| 146 | + /** |
| 147 | + * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from |
| 148 | + * a Buffer, String or another Multiaddr instance |
| 149 | + * public key. |
| 150 | + * @param addr - If String or Buffer, needs to adhere |
| 151 | + * to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) |
| 152 | + */ |
| 153 | + (addr?: MultiaddrInput): Multiaddr; |
| 154 | + |
| 155 | + /** |
| 156 | + * Creates a Multiaddr from a node-friendly address object |
| 157 | + */ |
| 158 | + fromNodeAddress(addr: NodeAddress, transport: string): Multiaddr; |
| 159 | + |
| 160 | + /** |
| 161 | + * Object containing table, names and codes of all supported protocols. |
| 162 | + * To get the protocol values from a Multiaddr, you can use |
| 163 | + * [`.protos()`](#multiaddrprotos), |
| 164 | + * [`.protoCodes()`](#multiaddrprotocodes) or |
| 165 | + * [`.protoNames()`](#multiaddrprotonames) |
| 166 | + */ |
| 167 | + protocols: Protocols; |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns if something is a Multiaddr |
| 171 | + */ |
| 172 | + isMultiaddr(addr: unknown): addr is Multiaddr; |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns if something is a Multiaddr that is a name |
| 176 | + */ |
| 177 | + isName(addr: Multiaddr): boolean; |
| 178 | + |
| 179 | + /** |
| 180 | + * Returns an array of multiaddrs, by resolving the multiaddr that is a name |
| 181 | + */ |
| 182 | + resolve(addr: Multiaddr): Promise<Multiaddr[]> |
| 183 | +} |
| 184 | + |
| 185 | +export = MultiaddrClass; |
0 commit comments