diff --git a/.github/workflows/js-test-and-release.yml b/.github/workflows/js-test-and-release.yml index c6897e3b..33a18608 100644 --- a/.github/workflows/js-test-and-release.yml +++ b/.github/workflows/js-test-and-release.yml @@ -140,6 +140,9 @@ jobs: with: docker-token: ${{ secrets.DOCKER_TOKEN }} docker-username: ${{ secrets.DOCKER_USERNAME }} + - run: npm run --if-present docs + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: npm run --if-present release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/package.json b/package.json index d233e9bb..47866658 100644 --- a/package.json +++ b/package.json @@ -161,7 +161,8 @@ "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", "test:node": "aegir test -t node --cov", "test:electron-main": "aegir test -t electron-main", - "release": "aegir release" + "release": "aegir release", + "docs": "aegir docs" }, "dependencies": { "@chainsafe/is-ip": "^2.0.1", @@ -173,7 +174,7 @@ }, "devDependencies": { "@types/varint": "^6.0.0", - "aegir": "^37.4.5", + "aegir": "^37.6.2", "sinon": "^15.0.0", "util": "^0.12.3" }, diff --git a/src/codec.ts b/src/codec.ts index 83ddd4c9..c8f3a5a4 100644 --- a/src/codec.ts +++ b/src/codec.ts @@ -3,13 +3,12 @@ import { getProtocol } from './protocols-table.js' import varint from 'varint' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import type { Protocol } from './protocols-table.js' -import type { StringTuple, Tuple } from './index.js' +import type { StringTuple, Tuple, Protocol } from './index.js' /** * string -> [[str name, str addr]... ] */ -export function stringToStringTuples (str: string) { +export function stringToStringTuples (str: string): string[][] { const tuples = [] const parts = str.split('/').slice(1) // skip first empty elem if (parts.length === 1 && parts[0] === '') { @@ -51,7 +50,7 @@ export function stringToStringTuples (str: string) { /** * [[str name, str addr]... ] -> string */ -export function stringTuplesToString (tuples: StringTuple[]) { +export function stringTuplesToString (tuples: StringTuple[]): string { const parts: string[] = [] tuples.map((tup) => { const proto = protoFromTuple(tup) @@ -99,7 +98,7 @@ export function tuplesToStringTuples (tuples: Tuple[]): StringTuple[] { /** * [[int code, Uint8Array ]... ] -> Uint8Array */ -export function tuplesToBytes (tuples: Tuple[]) { +export function tuplesToBytes (tuples: Tuple[]): Uint8Array { return fromBytes(uint8ArrayConcat(tuples.map((tup) => { const proto = protoFromTuple(tup) let buf = Uint8Array.from(varint.encode(proto.code)) @@ -112,7 +111,10 @@ export function tuplesToBytes (tuples: Tuple[]) { }))) } -export function sizeForAddr (p: Protocol, addr: Uint8Array | number[]) { +/** + * For the passed address, return the serialized size + */ +export function sizeForAddr (p: Protocol, addr: Uint8Array | number[]): number { if (p.size > 0) { return p.size / 8 } else if (p.size === 0) { @@ -158,7 +160,7 @@ export function bytesToTuples (buf: Uint8Array): Tuple[] { /** * Uint8Array -> String */ -export function bytesToString (buf: Uint8Array) { +export function bytesToString (buf: Uint8Array): string { const a = bytesToTuples(buf) const b = tuplesToStringTuples(a) return stringTuplesToString(b) @@ -167,7 +169,7 @@ export function bytesToString (buf: Uint8Array) { /** * String -> Uint8Array */ -export function stringToBytes (str: string) { +export function stringToBytes (str: string): Uint8Array { str = cleanPath(str) const a = stringToStringTuples(str) const b = stringTuplesToTuples(a) @@ -178,14 +180,14 @@ export function stringToBytes (str: string) { /** * String -> Uint8Array */ -export function fromString (str: string) { +export function fromString (str: string): Uint8Array { return stringToBytes(str) } /** * Uint8Array -> Uint8Array */ -export function fromBytes (buf: Uint8Array) { +export function fromBytes (buf: Uint8Array): Uint8Array { const err = validateBytes(buf) if (err != null) { throw err @@ -201,19 +203,19 @@ export function validateBytes (buf: Uint8Array): Error | undefined { } } -export function isValidBytes (buf: Uint8Array) { +export function isValidBytes (buf: Uint8Array): boolean { return validateBytes(buf) === undefined } -export function cleanPath (str: string) { +export function cleanPath (str: string): string { return '/' + str.trim().split('/').filter((a) => a).join('/') } -export function ParseError (str: string) { +export function ParseError (str: string): Error { return new Error('Error parsing address: ' + str) } -export function protoFromTuple (tup: any[]) { +export function protoFromTuple (tup: any[]): Protocol { const proto = getProtocol(tup[0]) return proto } diff --git a/src/convert.ts b/src/convert.ts index 4447e73f..49669a53 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -1,3 +1,8 @@ +/** + * @packageDocumentation + * + * Provides methods for converting + */ import * as ip from './ip.js' import { getProtocol } from './protocols-table.js' @@ -14,7 +19,9 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat' /** * converts (serializes) addresses */ -export function convert (proto: string, a: string | Uint8Array) { +export function convert (proto: string, a: string): Uint8Array +export function convert (proto: string, a: Uint8Array): string +export function convert (proto: string, a: string | Uint8Array): Uint8Array | string { if (a instanceof Uint8Array) { return convertToString(proto, a) } else { @@ -25,7 +32,7 @@ export function convert (proto: string, a: string | Uint8Array) { /** * Convert [code,Uint8Array] to string */ -export function convertToString (proto: number | string, buf: Uint8Array) { +export function convertToString (proto: number | string, buf: Uint8Array): string { const protocol = getProtocol(proto) switch (protocol.code) { case 4: // ipv4 @@ -59,7 +66,7 @@ export function convertToString (proto: number | string, buf: Uint8Array) { } } -export function convertToBytes (proto: string | number, str: string) { +export function convertToBytes (proto: string | number, str: string): Uint8Array { const protocol = getProtocol(proto) switch (protocol.code) { case 4: // ipv4 @@ -101,14 +108,14 @@ const anybaseDecoder = (function () { return acc })() -function ip2bytes (ipString: string) { +function ip2bytes (ipString: string): Uint8Array { if (!ip.isIP(ipString)) { throw new Error('invalid ip address') } return ip.toBytes(ipString) } -function bytes2ip (ipBuff: Uint8Array) { +function bytes2ip (ipBuff: Uint8Array): string { const ipString = ip.toString(ipBuff, 0, ipBuff.length) if (ipString == null) { throw new Error('ipBuff is required') @@ -119,7 +126,7 @@ function bytes2ip (ipBuff: Uint8Array) { return ipString } -function port2bytes (port: number) { +function port2bytes (port: number): Uint8Array { const buf = new ArrayBuffer(2) const view = new DataView(buf) view.setUint16(0, port) @@ -127,18 +134,18 @@ function port2bytes (port: number) { return new Uint8Array(buf) } -function bytes2port (buf: Uint8Array) { +function bytes2port (buf: Uint8Array): number { const view = new DataView(buf.buffer) return view.getUint16(buf.byteOffset) } -function str2bytes (str: string) { +function str2bytes (str: string): Uint8Array { const buf = uint8ArrayFromString(str) const size = Uint8Array.from(varint.encode(buf.length)) return uint8ArrayConcat([size, buf], size.length + buf.length) } -function bytes2str (buf: Uint8Array) { +function bytes2str (buf: Uint8Array): string { const size = varint.decode(buf) buf = buf.slice(varint.decode.bytes) @@ -149,7 +156,7 @@ function bytes2str (buf: Uint8Array) { return uint8ArrayToString(buf) } -function mh2bytes (hash: string) { +function mh2bytes (hash: string): Uint8Array { let mh if (hash[0] === 'Q' || hash[0] === '1') { @@ -163,7 +170,7 @@ function mh2bytes (hash: string) { return uint8ArrayConcat([size, mh], size.length + mh.length) } -function mb2bytes (mbstr: string) { +function mb2bytes (mbstr: string): Uint8Array { const mb = anybaseDecoder.decode(mbstr) const size = Uint8Array.from(varint.encode(mb.length)) return uint8ArrayConcat([size, mb], size.length + mb.length) @@ -182,7 +189,7 @@ function bytes2mb (buf: Uint8Array) { /** * Converts bytes to bas58btc string */ -function bytes2mh (buf: Uint8Array) { +function bytes2mh (buf: Uint8Array): string { const size = varint.decode(buf) const address = buf.slice(varint.decode.bytes) @@ -193,7 +200,7 @@ function bytes2mh (buf: Uint8Array) { return uint8ArrayToString(address, 'base58btc') } -function onion2bytes (str: string) { +function onion2bytes (str: string): Uint8Array { const addr = str.split(':') if (addr.length !== 2) { throw new Error(`failed to parse onion addr: ["'${addr.join('", "')}'"]' does not contain a port number`) @@ -214,7 +221,7 @@ function onion2bytes (str: string) { return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length) } -function onion32bytes (str: string) { +function onion32bytes (str: string): Uint8Array { const addr = str.split(':') if (addr.length !== 2) { throw new Error(`failed to parse onion addr: ["'${addr.join('", "')}'"]' does not contain a port number`) @@ -234,7 +241,7 @@ function onion32bytes (str: string) { return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length) } -function bytes2onion (buf: Uint8Array) { +function bytes2onion (buf: Uint8Array): string { const addrBytes = buf.slice(0, buf.length - 2) const portBytes = buf.slice(buf.length - 2) const addr = uint8ArrayToString(addrBytes, 'base32') diff --git a/src/index.ts b/src/index.ts index daa52472..194605ba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,17 @@ +/** + * @packageDocumentation + * + * An implementation of a Multiaddr in JavaScript + * + * @example + * + * ```js + * import { multiaddr } from '@multiformats/multiaddr' + * + * const ma = multiaddr('/ip4/127.0.0.1/tcp/1234') + * ``` + */ + import * as codec from './codec.js' import { getProtocol, names } from './protocols-table.js' import varint from 'varint' @@ -21,6 +35,9 @@ const P2P_CODES = [ getProtocol('ipfs').code ] +/** + * Protocols are present in the protocol table + */ export interface Protocol { code: number size: number @@ -29,6 +46,9 @@ export interface Protocol { path?: boolean | undefined } +/** + * A plain JavaScript object representation of a {@link Multiaddr} + */ export interface MultiaddrObject { family: 4 | 6 host: string @@ -36,24 +56,46 @@ export interface MultiaddrObject { port: number } +/** + * A NodeAddress is an IPv4/IPv6 address/TCP port combination + */ export interface NodeAddress { family: 4 | 6 address: string port: number } +/** + * These types can be parsed into a {@link Multiaddr} object + */ export type MultiaddrInput = string | Multiaddr | Uint8Array | null +/** + * A Resolver is a function that takes a {@link Multiaddr} and resolves it into one + * or more string representations of that {@link Multiaddr}. + */ export interface Resolver { (addr: Multiaddr, options?: AbortOptions): Promise } +/** + * A code/value pair + */ export type Tuple = [number, Uint8Array?] +/** + * A code/value pair with the value as a string + */ export type StringTuple = [number, string?] +/** + * Allows aborting long-lived operations + */ export interface AbortOptions { signal?: AbortSignal } +/** + * All configured {@link Resolver}s + */ export const resolvers = new Map() const symbol = Symbol.for('@multiformats/js-multiaddr/multiaddr') @@ -65,7 +107,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001').toString() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').toString() * // '/ip4/127.0.0.1/tcp/4001' * ``` */ @@ -76,7 +120,9 @@ export interface Multiaddr { * * @example * ```js - * JSON.stringify(new Multiaddr('/ip4/127.0.0.1/tcp/4001')) + * import { multiaddr } from '@multiformats/multiaddr' + * + * JSON.stringify(multiaddr('/ip4/127.0.0.1/tcp/4001')) * // '/ip4/127.0.0.1/tcp/4001' * ``` */ @@ -87,7 +133,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions() * // { family: 4, host: '127.0.0.1', transport: 'tcp', port: 4001 } * ``` */ @@ -101,7 +149,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001').protos() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').protos() * // [ { code: 4, size: 32, name: 'ip4' }, * // { code: 6, size: 16, name: 'tcp' } ] * ``` @@ -114,7 +164,9 @@ export interface Multiaddr { * * @example * ```js - * Multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes() * // [ 4, 6 ] * ``` */ @@ -126,7 +178,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames() * // [ 'ip4', 'tcp' ] * ``` */ @@ -137,7 +191,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr("/ip4/127.0.0.1/tcp/4001").tuples() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').tuples() * // [ [ 4, ], [ 6, ] ] * ``` */ @@ -150,7 +206,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr("/ip4/127.0.0.1/tcp/4001").stringTuples() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').stringTuples() * // [ [ 4, '127.0.0.1' ], [ 6, '4001' ] ] * ``` */ @@ -161,10 +219,12 @@ export interface Multiaddr { * * @example * ```js - * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080') + * import { multiaddr } from '@multiformats/multiaddr' + * + * const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080') * // * - * const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001') + * const mh2 = multiaddr('/ip4/127.0.0.1/tcp/4001') * // * * const mh3 = mh1.encapsulate(mh2) @@ -183,10 +243,12 @@ export interface Multiaddr { * * @example * ```js - * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080') + * import { multiaddr } from '@multiformats/multiaddr' + * + * const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080') * // * - * const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001') + * const mh2 = multiaddr('/ip4/127.0.0.1/tcp/4001') * // * * const mh3 = mh1.encapsulate(mh2) @@ -208,13 +270,15 @@ export interface Multiaddr { * * @example * ```js - * const addr = new Multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC') + * import { multiaddr } from '@multiformats/multiaddr' + * + * const addr = multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC') * // * * addr.decapsulateCode(421).toString() * // '/ip4/0.0.0.0/tcp/8080' * - * new Multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString() + * multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString() * // '/ip4/127.0.0.1/tcp/8080' * ``` */ @@ -225,7 +289,9 @@ export interface Multiaddr { * * @example * ```js - * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string') + * import { multiaddr } from '@multiformats/multiaddr' + * + * const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string') * // * * // should return QmValidBase58string or null if the id is missing or invalid @@ -239,7 +305,9 @@ export interface Multiaddr { * * @example * ```js - * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock') + * import { multiaddr } from '@multiformats/multiaddr' + * + * const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock') * // * * // should return utf8 string or null if the id is missing or invalid @@ -253,10 +321,12 @@ export interface Multiaddr { * * @example * ```js - * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080') + * import { multiaddr } from '@multiformats/multiaddr' + * + * const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080') * // * - * const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001') + * const mh2 = multiaddr('/ip4/127.0.0.1/tcp/4001') * // * * mh1.equals(mh1) @@ -273,8 +343,10 @@ export interface Multiaddr { * * @example * ```js - * Multiaddr.resolvers.set('dnsaddr', resolverFunction) - * const mh1 = new Multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb') + * import { multiaddr, resolvers } from '@multiformats/multiaddr' + * + * resolvers.set('dnsaddr', resolverFunction) + * const mh1 = multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb') * const resolvedMultiaddrs = await mh1.resolve() * // [ * // , @@ -294,7 +366,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress() * // {family: 4, address: '127.0.0.1', port: 4001} * ``` */ @@ -309,13 +383,15 @@ export interface Multiaddr { * * @example * ```js - * const mh1 = new Multiaddr('/ip4/127.0.0.1/tcp/4001') + * import { multiaddr } from '@multiformats/multiaddr' + * + * const mh1 = multiaddr('/ip4/127.0.0.1/tcp/4001') * // - * const mh2 = new Multiaddr('/ip4/192.168.2.1/tcp/5001') + * const mh2 = multiaddr('/ip4/192.168.2.1/tcp/5001') * // * const mh3 = mh1.encapsulate(mh2) * // - * const mh4 = new Multiaddr('/ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a') + * const mh4 = multiaddr('/ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a') * // * mh1.isThinWaistAddress() * // true @@ -336,7 +412,9 @@ export interface Multiaddr { * * @example * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001').inspect() + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001').inspect() * // '' * ``` */ @@ -348,7 +426,9 @@ export interface Multiaddr { * * @example * ```js - * Multiaddr.fromNodeAddress({address: '127.0.0.1', port: '4001'}, 'tcp') + * import { fromNodeAddress } from '@multiformats/multiaddr' + * + * fromNodeAddress({address: '127.0.0.1', port: '4001'}, 'tcp') * // * ``` */ @@ -374,7 +454,18 @@ export function fromNodeAddress (addr: NodeAddress, transport: string): Multiadd } /** - * Returns if something is a Multiaddr that is a name + * Returns if something is a {@link Multiaddr} that is a resolvable name + * + * @example + * + * ```js + * import { isName, multiaddr } from '@multiformats/multiaddr' + * + * isName(multiaddr('/ip4/127.0.0.1')) + * // false + * isName(multiaddr('/dns/ipfs.io')) + * // true + * ``` */ export function isName (addr: Multiaddr): boolean { if (!isMultiaddr(addr)) { @@ -386,17 +477,25 @@ export function isName (addr: Multiaddr): boolean { } /** - * Check if object is a CID instance + * Check if object is a {@link Multiaddr} instance + * + * @example + * + * ```js + * import { isMultiaddr, multiaddr } from '@multiformats/multiaddr' + * + * isMultiaddr(5) + * // false + * isMultiaddr(multiaddr('/ip4/127.0.0.1')) + * // true + * ``` */ export function isMultiaddr (value: any): value is Multiaddr { return Boolean(value?.[symbol]) } /** - * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from - * a Uint8Array, String or another Multiaddr instance - * public key. - * + * Creates a {@link Multiaddr} from a {@link MultiaddrInput} */ class DefaultMultiaddr implements Multiaddr { public bytes: Uint8Array @@ -406,15 +505,6 @@ class DefaultMultiaddr implements Multiaddr { [symbol]: boolean = true - /** - * @example - * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001') - * // - * ``` - * - * @param {MultiaddrInput} [addr] - If String or Uint8Array, needs to adhere to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format) - */ constructor (addr?: MultiaddrInput) { // default if (addr == null) { @@ -596,7 +686,7 @@ class DefaultMultiaddr implements Multiaddr { return path } - equals (addr: { bytes: Uint8Array }) { + equals (addr: { bytes: Uint8Array }): boolean { return uint8ArrayEquals(this.bytes, addr.bytes) } @@ -631,7 +721,7 @@ class DefaultMultiaddr implements Multiaddr { } } - isThinWaistAddress (addr?: Multiaddr) { + isThinWaistAddress (addr?: Multiaddr): boolean { const protos = (addr ?? this).protos() if (protos.length !== 2) { @@ -654,27 +744,29 @@ class DefaultMultiaddr implements Multiaddr { * * @example * ```js - * console.log(new Multiaddr('/ip4/127.0.0.1/tcp/4001')) + * import { multiaddr } from '@multiformats/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001') * // '' * ``` */ - [inspect] () { + [inspect] (): string { return this.inspect() } - inspect () { - return '' + inspect (): string { + return `` } } /** - * Static factory + * A function that takes a {@link MultiaddrInput} and returns a {@link Multiaddr} * * @example * ```js - * new Multiaddr('/ip4/127.0.0.1/tcp/4001') + * import { multiaddr } from '@libp2p/multiaddr' + * + * multiaddr('/ip4/127.0.0.1/tcp/4001') * // * ``` * diff --git a/src/protocols-table.ts b/src/protocols-table.ts index 8d864cdd..d88294be 100644 --- a/src/protocols-table.ts +++ b/src/protocols-table.ts @@ -1,10 +1,4 @@ -export interface Protocol { - code: number - size: number - name: string - resolvable?: boolean - path?: boolean -} +import type { Protocol } from './index.js' const V = -1 export const names: Record = {} @@ -67,7 +61,19 @@ export function createProtocol (code: number, size: number, name: string, resolv } } -export function getProtocol (proto: number | string) { +/** + * For the passed proto string or number, return a {@link Protocol} + * + * @example + * + * ```js + * import { protocol } from '@multiformats/multiaddr' + * + * console.info(protocol(4)) + * // { code: 4, size: 32, name: 'ip4', resolvable: false, path: false } + * ``` + */ +export function getProtocol (proto: number | string): Protocol { if (typeof proto === 'number') { if (codes[proto] != null) { return codes[proto] diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index d9aaf806..f89a8663 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -1,3 +1,9 @@ +/** + * @packageDocumentation + * + * Provides strategies for resolving multiaddrs. + */ + import { getProtocol } from '../protocols-table.js' import Resolver from './dns.js' import type { AbortOptions, Multiaddr } from '../index.js' @@ -6,8 +12,26 @@ const { code: dnsaddrCode } = getProtocol('dnsaddr') /** * Resolver for dnsaddr addresses. + * + * @example + * + * ```typescript + * import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers' + * import { multiaddr } from '@multiformats/multiaddr' + * + * const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io') + * const addresses = await dnsaddrResolver(ma) + * + * console.info(addresses) + * //[ + * // '/dnsaddr/am6.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', + * // '/dnsaddr/ny5.bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa', + * // '/dnsaddr/sg1.bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt', + * // '/dnsaddr/sv15.bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN' + * //] + * ``` */ -export async function dnsaddrResolver (addr: Multiaddr, options: AbortOptions = {}) { +export async function dnsaddrResolver (addr: Multiaddr, options: AbortOptions = {}): Promise { const resolver = new Resolver() if (options.signal != null) {