From 823b45fc786d0603f3260b9de4c7694dbe91533a Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 21 Nov 2024 15:29:23 +0000 Subject: [PATCH 1/2] docs: fix dnsaddrResolver name (#392) Fixes documentation errors and adds doc-check to prevent regressions. Fixes #388 --- README.md | 14 +++++++------- examples/try.js | 29 ----------------------------- package.json | 1 + src/convert.ts | 6 ------ src/index.ts | 14 +++++++------- src/multiaddr.ts | 14 -------------- src/resolvers/index.ts | 1 + 7 files changed, 16 insertions(+), 63 deletions(-) delete mode 100644 examples/try.js diff --git a/README.md b/README.md index fc17c5cd..23c918a9 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,8 @@ A standard way to represent addresses that ```TypeScript import { multiaddr } from '@multiformats/multiaddr' -const addr = multiaddr("/ip4/127.0.0.1/udp/1234") -// Multiaddr(/ip4/127.0.0.1/udp/1234) -const addr = multiaddr("/ip4/127.0.0.1/udp/1234") +const addr = multiaddr('/ip4/127.0.0.1/udp/1234') // Multiaddr(/ip4/127.0.0.1/udp/1234) addr.bytes @@ -77,9 +75,9 @@ DNSADDR addresses can resolve to multiple multiaddrs, since there is no limit to ```TypeScript import { multiaddr, resolvers } from '@multiformats/multiaddr' -import { dnsaddr } from '@multiformats/multiaddr/resolvers' +import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers' -resolvers.set('dnsaddr', dnsaddr) +resolvers.set('dnsaddr', dnsaddrResolver) const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io') @@ -88,7 +86,7 @@ const resolved = await ma.resolve({ signal: AbortSignal.timeout(5000) }) -console.info(await ma.resolve(resolved) +console.info(resolved) // [Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...')...] ``` @@ -102,7 +100,9 @@ import { dns } from '@multiformats/dns' import { dnsJsonOverHttps } from '@multiformats/dns/resolvers' const resolver = dns({ - '.': dnsJsonOverHttps('https://cloudflare-dns.com/dns-query') + resolvers: { + '.': dnsJsonOverHttps('https://cloudflare-dns.com/dns-query') + } }) const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io') diff --git a/examples/try.js b/examples/try.js deleted file mode 100644 index 60eee4c4..00000000 --- a/examples/try.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict' - -const { Multiaddr } = require('../src') -// eslint-disable-next-line no-console -const log = console.log - -const addr = new Multiaddr('/ip4/127.0.0.1/udp/1234') -log(addr) -log(addr.bytes) -log(addr.toString()) -log(new Multiaddr(addr.bytes)) - -log(addr.protoCodes()) -log(addr.protoNames()) -log(addr.protos()) - -log(addr.nodeAddress()) -log(Multiaddr.fromNodeAddress(addr.nodeAddress(), 'udp')) - -log(addr.encapsulate('/sctp/5678')) -log(addr.decapsulate('/udp')) - -const printer = new Multiaddr('/ip4/192.168.0.13/tcp/80') -const proxy = new Multiaddr('/ip4/10.20.30.40/tcp/443') -const printerOverProxy = proxy.encapsulate(printer) -log(printerOverProxy) - -const proxyAgain = printerOverProxy.decapsulate('/ip4') -log(proxyAgain) diff --git a/package.json b/package.json index 8ff6ecfd..d12ac8e4 100644 --- a/package.json +++ b/package.json @@ -154,6 +154,7 @@ "clean": "aegir clean", "lint": "aegir lint", "dep-check": "aegir dep-check", + "doc-check": "aegir doc-check", "build": "aegir build", "test": "aegir test -f", "test:chrome": "aegir test -t browser --cov", diff --git a/src/convert.ts b/src/convert.ts index 7c48c56a..3cbb8321 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -1,9 +1,3 @@ -/** - * @packageDocumentation - * - * Provides methods for converting - */ - import { IpNet } from '@chainsafe/netmask' import { base32 } from 'multiformats/bases/base32' import { base58btc } from 'multiformats/bases/base58' diff --git a/src/index.ts b/src/index.ts index dfec2b63..47e691a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,10 +13,8 @@ * * ```TypeScript * import { multiaddr } from '@multiformats/multiaddr' - * const addr = multiaddr("/ip4/127.0.0.1/udp/1234") - * // Multiaddr(/ip4/127.0.0.1/udp/1234) * - * const addr = multiaddr("/ip4/127.0.0.1/udp/1234") + * const addr = multiaddr('/ip4/127.0.0.1/udp/1234') * // Multiaddr(/ip4/127.0.0.1/udp/1234) * * addr.bytes @@ -55,9 +53,9 @@ * * ```TypeScript * import { multiaddr, resolvers } from '@multiformats/multiaddr' - * import { dnsaddr } from '@multiformats/multiaddr/resolvers' + * import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers' * - * resolvers.set('dnsaddr', dnsaddr) + * resolvers.set('dnsaddr', dnsaddrResolver) * * const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io') * @@ -66,7 +64,7 @@ * signal: AbortSignal.timeout(5000) * }) * - * console.info(await ma.resolve(resolved) + * console.info(resolved) * // [Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...')...] * ``` * @@ -80,7 +78,9 @@ * import { dnsJsonOverHttps } from '@multiformats/dns/resolvers' * * const resolver = dns({ - * '.': dnsJsonOverHttps('https://cloudflare-dns.com/dns-query') + * resolvers: { + * '.': dnsJsonOverHttps('https://cloudflare-dns.com/dns-query') + * } * }) * * const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io') diff --git a/src/multiaddr.ts b/src/multiaddr.ts index 07895338..a99470e5 100644 --- a/src/multiaddr.ts +++ b/src/multiaddr.ts @@ -1,17 +1,3 @@ -/** - * @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 { base58btc } from 'multiformats/bases/base58' import { CID } from 'multiformats/cid' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index d55a8cc2..756a04d0 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -5,3 +5,4 @@ export interface Resolver { } export { dnsaddrResolver } from './dnsaddr.js' +export type { DNSADDROptions } from './dnsaddr.js' From 77f15d29b07e7b7e2fb9e3ef39cd0e1da0982388 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 21 Nov 2024 15:29:31 +0000 Subject: [PATCH 2/2] fix: do not allow modifying tuples (#391) Return a clone of the string/tuple list to prevent external modification of internal state. --- src/multiaddr.ts | 16 ++++++++++++++-- test/index.spec.ts | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/multiaddr.ts b/src/multiaddr.ts index a99470e5..52320217 100644 --- a/src/multiaddr.ts +++ b/src/multiaddr.ts @@ -137,11 +137,23 @@ export class Multiaddr implements MultiaddrInterface { } tuples (): Array<[number, Uint8Array?]> { - return this.#tuples + return this.#tuples.map(([code, value]) => { + if (value == null) { + return [code] + } + + return [code, value] + }) } stringTuples (): Array<[number, string?]> { - return this.#stringTuples + return this.#stringTuples.map(([code, value]) => { + if (value == null) { + return [code] + } + + return [code, value] + }) } encapsulate (addr: MultiaddrInput): Multiaddr { diff --git a/test/index.spec.ts b/test/index.spec.ts index bc4e6211..18bf5845 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -731,6 +731,14 @@ describe('helpers', () => { [302] ]) }) + + it('does not allow modifying parts', () => { + const ma = multiaddr('/ip4/0.0.0.0/tcp/1234') + const tuples = ma.tuples() + tuples[0][0] = 41 + + expect(ma.toOptions()).to.have.property('family', 4) + }) }) describe('.stringTuples', () => { @@ -741,6 +749,14 @@ describe('helpers', () => { [302] ]) }) + + it('does not allow modifying string parts', () => { + const ma = multiaddr('/ip4/0.0.0.0/tcp/1234') + const tuples = ma.stringTuples() + tuples[0][0] = 41 + + expect(ma.toOptions()).to.have.property('family', 4) + }) }) describe('.decapsulate', () => {