Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/aegir-45.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Nov 21, 2024
2 parents 7f6fb0b + 77f15d2 commit 23bb089
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 65 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand All @@ -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...')...]
```

Expand All @@ -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')
Expand Down
29 changes: 0 additions & 29 deletions examples/try.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 0 additions & 6 deletions src/convert.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
*
Expand All @@ -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...')...]
* ```
*
Expand All @@ -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')
Expand Down
30 changes: 14 additions & 16 deletions src/multiaddr.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -151,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 {
Expand Down
1 change: 1 addition & 0 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export interface Resolver<ResolveOptions extends AbortOptions = AbortOptions> {
}

export { dnsaddrResolver } from './dnsaddr.js'
export type { DNSADDROptions } from './dnsaddr.js'
16 changes: 16 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit 23bb089

Please sign in to comment.